2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
14 /* missing type from C language */
23 #include <afs/afs_args.h>
24 #include <sys/param.h>
34 #include <sys/ioctl.h>
38 #include <sys/ioctl.h>
40 #ifdef HAVE_NETINET_IN_H
41 #include <netinet/in.h>
47 #include <afs/venus.h>
48 #include <afs/sys_prototypes.h>
49 #include <afs/afsutil.h>
50 #include <afs/afs_consts.h>
52 /* ************************************************************* */
57 short renameTargets = 0;
59 short preserveDate = 1;
60 short preserveMountPoints = 0;
61 short forceOverwrite = 0;
65 char file1[MAXPATHLEN], file2[MAXPATHLEN];
67 static char space[AFS_PIOCTL_MAXSIZE];
76 static void ScanArgs(int argc, char *argv[]);
77 static short MakeParent(char *file, afs_int32 owner);
78 static int Copy(char *file1, char *file2, short recursive, int level);
79 static int isMountPoint(char *name, struct ViceIoctl *blob);
82 /* ************************************************************ */
86 /* ************************************************************ */
88 #include "AFS_component_version_number.c"
91 main(int argc, char *argv[])
95 * The following signal action for AIX is necessary so that in case of a
96 * crash (i.e. core is generated) we can include the user's data section
97 * in the core dump. Unfortunately, by default, only a partial core is
98 * generated which, in many cases, isn't too useful.
100 struct sigaction nsa;
102 sigemptyset(&nsa.sa_mask);
103 nsa.sa_handler = SIG_DFL;
104 nsa.sa_flags = SA_FULLDUMP;
105 sigaction(SIGSEGV, &nsa, NULL);
107 ScanArgs(argc, argv);
109 /* now read each line of the CopyList */
110 if (Copy(file1, file2, !oneLevel, 0))
111 return(1); /* some type of failure */
117 #define USAGE "usage: up [-v1frxm] from to\n"
119 ScanArgs(int argc, char *argv[])
121 /* skip program name */
124 /* check for -flag options */
125 while (argc > 0 && *argv[0] == '-') {
150 preserveMountPoints = 1;
154 fprintf(stderr, "Unknown option: '%c'\n", *cp);
155 fprintf(stderr, USAGE);
162 fprintf(stderr, USAGE);
166 strncpy(file1, argv[0], MAXPATHLEN);
167 strncpy(file2, argv[1], MAXPATHLEN);
175 * Make sure the parent directory of this file exists. Returns
176 * 1 if it exists, 0 otherwise. Note: the owner argument
177 * is a hack. All directories made will have this owner.
180 MakeParent(char *file, afs_int32 owner)
182 char parent[MAXPATHLEN];
186 strlcpy(parent, file, sizeof parent);
188 p = strrchr(parent, '/');
190 strlcpy(parent, ".", sizeof parent);
191 } else if (p > parent) {
197 if (stat(parent, &s) < 0) {
198 if (!MakeParent(parent, owner))
202 printf("Creating directory %s\n", parent);
207 chown(parent, owner, -1);
215 * This does the bulk of the work of the program. Handle one file,
216 * possibly copying subfiles if this is a directory
219 Copy(char *file1, char *file2, short recursive, int level)
221 struct stat s1, s2; /*Stat blocks */
222 struct ViceIoctl blob;
223 char aclspace[MAXACL];
224 afs_int32 rcode = 0, code;
227 code = lstat(file1, &s1);
229 fprintf(stderr, "Can't find %s\n", file1);
233 code = lstat(file2, &s2);
235 if (!MakeParent(file2, s1.st_uid))
240 if ((s1.st_mode & S_IFMT) == S_IFREG) {
242 * -------------------- Copy regular file --------------------
245 char buf[4096]; /* Must be bigger than sizeof (*head) */
246 struct timeval tv[2];
247 char tmpfile[MAXPATHLEN], newName[MAXPATHLEN];
250 printf("Level %d: File %s to %s\n", level, file1, file2);
254 /* Wonder if there is a security hole */
255 if (((s1.st_mode & 04002) == 04002) || ((s1.st_mode & 04020) == 04020)
256 || ((s1.st_mode & 02002) == 02002)) {
258 "WARNING: Mode-bits security hole in files %s and %s\n",
262 if (!goods2 || (s1.st_mtime != s2.st_mtime) || (s1.st_size != s2.st_size)) { /*c */
263 /* Don't ovewrite a write protected file (unless force: -f) */
264 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
266 "File %s is write protected against its owner; not changed\n",
272 printf(" Copy file %s to %s (%u Bytes)\n", file1, file2,
273 (unsigned int) s1.st_size);
277 strlcpy(tmpfile, file2, sizeof tmpfile); /* Name of temporary file */
278 strlcat(tmpfile, ".UPD", sizeof tmpfile);
280 /* open file1 for input */
281 f1 = open(file1, O_RDONLY);
283 fprintf(stderr, "Unable to open input file %s: %s\n",
284 file1, strerror(errno));
288 /* open temporary output file */
289 f2 = open(tmpfile, (O_WRONLY | O_CREAT | O_TRUNC), s1.st_mode);
291 fprintf(stderr, "Unable to open output file %s: %s\n",
292 tmpfile, strerror(errno));
298 /* Copy file1 to temporary file */
299 while ((n = read(f1, buf, sizeof(buf))) > 0) {
300 if (write(f2, buf, n) != n) {
302 "Write failed, file %s must be copied again.\n",
307 /* preserve access and modification times: ("-x" disables) */
309 tv[0].tv_sec = s1.st_atime;
311 tv[1].tv_sec = s1.st_mtime;
316 /* Close the files */
324 /* Rename file2 to file2.old. [-r] */
325 if (renameTargets && goods2) {
326 strlcpy(newName, file2, sizeof newName);
327 strlcat(newName, ".old", sizeof newName);
329 printf(" Renaming %s to %s\n", file2, newName);
332 if (rename(file2, newName) < 0) {
333 fprintf(stderr, "Rename of %s to %s failed.\n", file2,
338 /* Rename temporary file to file2 */
339 code = rename(tmpfile, file2);
341 fprintf(stderr, "Rename of %s to %s failed.\n", tmpfile,
346 /* Re-stat file2 and compare file sizes */
347 code = lstat(file2, &s2);
349 fprintf(stderr, "WARNING: Unable to stat new file %s\n",
353 if (s1.st_size != s2.st_size) {
355 "WARNING: New file %s is %u bytes long; should be %u\n",
356 file2, (unsigned int) s2.st_size,
357 (unsigned int) s1.st_size);
362 /* Set the user-id */
363 if (s2.st_uid != s1.st_uid) {
365 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
368 code = chown(file2, s1.st_uid, -1);
370 fprintf(stderr, "Unable to set owner-id for %s to %d\n",
374 s1.st_mode &= ~04000; /* Don't set suid bit */
378 /* Set the group-id */
379 if (s2.st_gid != s1.st_gid) {
381 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
384 code = chown(file2, -1, s1.st_gid);
386 fprintf(stderr, "Unable to set group-id for %s to %d\n",
390 s1.st_mode &= ~02000; /* Don't set sgid bit */
394 /* Set the mode bits */
395 if (s1.st_mode != s2.st_mode) {
397 printf(" Set mode-bit for %s to %o\n", file2,
398 (s1.st_mode & 07777));
401 code = chmod(file2, s1.st_mode);
403 fprintf(stderr, "Unable to set mode-bits for %s to %d\n",
410 else if ((s1.st_mode & S_IFMT) == S_IFLNK) {
412 * --------------------- Copy symlink --------------------
414 char linkvalue[MAXPATHLEN + 1];
418 printf("Level %d: Symbolic link %s to %s\n", level, file1, file2);
422 /* Don't ovewrite a write protected directory (unless force: -f) */
423 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
425 "Link %s is write protected against its owner; not changed\n",
431 printf(" Copy symbolic link %s->%s to %s\n", file1, linkvalue,
436 n = readlink(file1, linkvalue, sizeof(linkvalue));
438 fprintf(stderr, "Could not read symbolic link %s\n", file1);
439 perror("read link ");
444 unlink(file2); /* Always make the new link (it was easier) */
446 code = symlink(linkvalue, file2);
448 fprintf(stderr, "Could not create symbolic link %s\n", file2);
449 perror("create link ");
453 /*Dealing with symlink */
454 else if (preserveMountPoints && (code = isMountPoint(file1, &blob))) {
456 * --------------------- Copy mount point --------------------
460 perror("checking for mount point ");
464 printf("Level %d: Mount point %s to %s\n", level, file1, file2);
468 /* Don't ovewrite a write protected directory (unless force: -f) */
469 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
471 "Target %s is write protected against its owner; not changed\n",
477 printf(" Copy mount point %s for vol %s to %s\n", file1,
482 unlink(file2); /* Always make the new link (it was easier) */
484 strcat(blob.out, "."); /* stupid convention; these end with a period */
485 code = symlink(blob.out, file2);
487 fprintf(stderr, "Could not create mount point %s for vol %s\n",
489 perror("create mount point ");
494 /*Dealing with mount point */
495 else if (((s1.st_mode & S_IFMT) == S_IFDIR)
496 && (recursive || (level == 0))) {
498 * ----------------------- Copy directory -----------------------
504 char f1[MAXPATHLEN], f2[MAXPATHLEN];
507 struct timeval tv[2];
510 printf("Level %d: Directory %s to %s\n", level, file1, file2);
514 /* Don't ovewrite a write protected directory (unless force: -f) */
515 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
517 "Directory %s is write protected against its owner; not changed\n",
522 strlcpy(f1, file1, sizeof f1);
523 strlcpy(f2, file2, sizeof f2);
524 p1 = f1 + strlen(f1);
525 p2 = f2 + strlen(f2);
526 if (p1 == f1 || p1[-1] != '/')
528 if (p2 == f2 || p2[-1] != '/')
531 dir = opendir(file1);
533 fprintf(stderr, "Couldn't open %s\n", file1);
537 while ((d = readdir(dir)) != NULL) {
538 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
540 strlcpy(p1, d->d_name, sizeof f1 - (p1 - f1));
541 strlcpy(p2, d->d_name, sizeof f2 - (p2 - f2));
542 code = Copy(f1, f2, recursive, level + 1);
544 rcode = 1; /* remember errors */
550 printf("Level %d: Copied directory %s to %s\n", level, file1,
555 mkdir(file2, 0777); /* Handle case where MakeParent not invoked. */
558 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
561 code = chown(file2, s1.st_uid, -1);
563 fprintf(stderr, "Unable to set owner-id for %s to %d\n", file2,
566 s1.st_mode &= ~04000; /* Don't set suid bit */
570 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
573 code = chown(file2, -1, s1.st_gid);
575 fprintf(stderr, "Unable to set group-id for %s to %d\n", file2,
578 s1.st_mode &= ~02000; /* Don't set sgid bit */
582 printf(" Set mode-bit for %s to %o\n", file2,
583 (s1.st_mode & 07777));
586 code = chmod(file2, s1.st_mode);
588 fprintf(stderr, "Unable to set mode-bits for %s to %d\n", file2,
596 printf(" Set acls for %s\n", file2);
603 blob.out_size = MAXACL;
606 /* Get an old-style ACL and convert it */
608 printf(" Getting old style acl\n");
612 for (i = 1; i < strlen(file1); i++)
615 strlcpy(aclspace, &file1[i], sizeof aclspace);
617 blob.in_size = 1 + strlen(aclspace);
618 tfd = open(file1, O_RDONLY, 0);
620 perror("old-acl open ");
623 code = ioctl(tfd, _VICEIOCTL(4), &blob);
626 if (errno == EINVAL) {
629 printf(" _VICEIOCTL(4) returns EINVAL\n");
636 /* Now convert the thing. */
637 oacl = (struct OldAcl *)(aclspace + 4);
638 sprintf(tacl, "%d\n%d\n", oacl->nplus, oacl->nminus);
639 strlcat(tacl, oacl->data, sizeof tacl);
640 strlcpy(aclspace, tacl, sizeof aclspace);
641 } /*Grab and convert old-style ACL */
643 /* Get a new-style ACL */
645 printf(" Getting new style acl\n");
649 code = pioctl(file1, _VICEIOCTL(2), &blob, 1);
651 if (errno == EINVAL) {
654 printf(" _VICEIOCTL(2) returns EINVAL\n");
662 } /*Grab new-style ACL */
665 * Now, set the new-style ACL.
669 printf(" Setting new style acl\n");
675 blob.in_size = 1 + strlen(aclspace);
676 code = pioctl(file2, _VICEIOCTL(1), &blob, 1);
678 if (errno == EINVAL) {
681 printf(" _VICEIOCTL(1) returns EINVAL\n");
685 fprintf(stderr, "Couldn't set acls for %s\n", file2);
692 printf("Not setting acls\n");
696 /* preserve access and modification times: ("-x" disables) */
698 tv[0].tv_sec = s1.st_atime;
700 tv[1].tv_sec = s1.st_mtime;
711 isMountPoint(char *name, struct ViceIoctl *blob)
714 char true_name[1024]; /*dirname */
715 char parent_dir[1024]; /*Parent directory of true name */
716 char *last_component; /*Last component of true name */
718 sprintf(true_name, "%s%s", (name[0] == '/') ? "" : "./", name);
721 * Find rightmost slash, if any.
723 last_component = (char *)strrchr(true_name, '/');
724 if (last_component) {
726 * Found it. Designate everything before it as the parent directory,
727 * everything after it as the final component.
729 strncpy(parent_dir, true_name, last_component - true_name);
730 parent_dir[last_component - true_name] = 0;
731 last_component++; /*Skip the slash */
734 * No slash appears in the given file name. Set parent_dir to the current
735 * directory, and the last component as the given name.
737 strlcpy(parent_dir, ".", sizeof parent_dir);
738 last_component = true_name;
741 if (strcmp(last_component, ".") == 0 || strcmp(last_component, "..") == 0) {
743 "up: you may not use '.' or '..' as the last component\n");
744 fprintf(stderr, "up: of a name in the 'up' command.\n");
748 blob->in = last_component;
749 blob->in_size = strlen(last_component) + 1;
750 blob->out_size = AFS_PIOCTL_MAXSIZE;
752 memset(space, 0, AFS_PIOCTL_MAXSIZE);
754 code = pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, blob, 0);
757 printf("'%s' is a mount point for volume '%s'\n", name, space);
761 if (errno == EINVAL) {
762 /* printf( "'%s' is not a mount point.\n", name);
767 fprintf(stderr, "problem examining '%s' in '%s'.\n",
768 last_component, parent_dir);
770 /* Die(errno, (ti->data ? ti->data : parent_dir));