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>
16 /* missing type from C language */
24 #include <afs/afs_args.h>
25 #include <sys/param.h>
35 #include <sys/ioctl.h>
39 #include <sys/ioctl.h>
41 #ifdef HAVE_NETINET_IN_H
42 #include <netinet/in.h>
53 #include <afs/venus.h>
55 /* ************************************************************* */
60 short renameTargets = 0;
62 short preserveDate = 1;
63 short preserveMountPoints = 0;
64 short forceOverwrite = 0;
69 char file1[MAXPATHLEN], file2[MAXPATHLEN];
72 static char space[MAXSIZE];
81 static void ScanArgs(int argc, char *argv[]);
82 static short MakeParent(char *file, afs_int32 owner);
83 static int Copy(char *file1, char *file2, short recursive, int level);
84 static int isMountPoint(char *name, struct ViceIoctl *blob);
87 /* ************************************************************ */
91 /* ************************************************************ */
93 #include "AFS_component_version_number.c"
96 main(int argc, char *argv[])
100 * The following signal action for AIX is necessary so that in case of a
101 * crash (i.e. core is generated) we can include the user's data section
102 * in the core dump. Unfortunately, by default, only a partial core is
103 * generated which, in many cases, isn't too useful.
105 struct sigaction nsa;
107 sigemptyset(&nsa.sa_mask);
108 nsa.sa_handler = SIG_DFL;
109 nsa.sa_flags = SA_FULLDUMP;
110 sigaction(SIGSEGV, &nsa, NULL);
112 #if !defined (AFS_AIX_ENV) && !defined (AFS_HPUX_ENV)
113 pageSize = getpagesize();
115 ScanArgs(argc, argv);
117 /* now read each line of the CopyList */
118 if (Copy(file1, file2, !oneLevel, 0))
119 return(1); /* some type of failure */
125 #define USAGE "usage: up [-v1frxm] from to\n"
127 ScanArgs(int argc, char *argv[])
129 /* skip program name */
132 /* check for -flag options */
133 while (argc > 0 && *argv[0] == '-') {
158 preserveMountPoints = 1;
162 fprintf(stderr, "Unknown option: '%c'\n", *cp);
163 fprintf(stderr, USAGE);
170 fprintf(stderr, USAGE);
174 strncpy(file1, argv[0], MAXPATHLEN);
175 strncpy(file2, argv[1], MAXPATHLEN);
183 * Make sure the parent directory of this file exists. Returns
184 * 1 if it exists, 0 otherwise. Note: the owner argument
185 * is a hack. All directories made will have this owner.
188 MakeParent(char *file, afs_int32 owner)
190 char parent[MAXPATHLEN];
194 strlcpy(parent, file, sizeof parent);
196 p = strrchr(parent, '/');
198 strlcpy(parent, ".", sizeof parent);
199 } else if (p > parent) {
205 if (stat(parent, &s) < 0) {
206 if (!MakeParent(parent, owner))
210 printf("Creating directory %s\n", parent);
215 chown(parent, owner, -1);
223 * This does the bulk of the work of the program. Handle one file,
224 * possibly copying subfiles if this is a directory
227 Copy(char *file1, char *file2, short recursive, int level)
229 struct stat s1, s2; /*Stat blocks */
230 struct ViceIoctl blob;
231 char aclspace[MAXACL];
232 afs_int32 rcode = 0, code;
235 code = lstat(file1, &s1);
237 fprintf(stderr, "Can't find %s\n", file1);
241 code = lstat(file2, &s2);
243 if (!MakeParent(file2, s1.st_uid))
248 if ((s1.st_mode & S_IFMT) == S_IFREG) {
250 * -------------------- Copy regular file --------------------
253 char buf[4096]; /* Must be bigger than sizeof (*head) */
254 struct timeval tv[2];
255 char tmpfile[MAXPATHLEN], newName[MAXPATHLEN];
258 printf("Level %d: File %s to %s\n", level, file1, file2);
262 /* Wonder if there is a security hole */
263 if (((s1.st_mode & 04002) == 04002) || ((s1.st_mode & 04020) == 04020)
264 || ((s1.st_mode & 02002) == 02002)) {
266 "WARNING: Mode-bits security hole in files %s and %s\n",
270 if (!goods2 || (s1.st_mtime != s2.st_mtime) || (s1.st_size != s2.st_size)) { /*c */
271 /* Don't ovewrite a write protected file (unless force: -f) */
272 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
274 "File %s is write protected against its owner; not changed\n",
280 printf(" Copy file %s to %s (%u Bytes)\n", file1, file2,
285 strlcpy(tmpfile, file2, sizeof tmpfile); /* Name of temporary file */
286 strlcat(tmpfile, ".UPD", sizeof tmpfile);
288 /* open file1 for input */
289 f1 = open(file1, O_RDONLY);
291 fprintf(stderr, "Unable to open input file %s: %s\n",
292 file1, strerror(errno));
296 /* open temporary output file */
297 f2 = open(tmpfile, (O_WRONLY | O_CREAT | O_TRUNC), s1.st_mode);
299 fprintf(stderr, "Unable to open output file %s: %s\n",
300 tmpfile, strerror(errno));
306 /* Copy file1 to temporary file */
307 while ((n = read(f1, buf, sizeof(buf))) > 0) {
308 if (write(f2, buf, n) != n) {
310 "Write failed, file %s must be copied again.\n",
315 /* preserve access and modification times: ("-x" disables) */
317 tv[0].tv_sec = s1.st_atime;
319 tv[1].tv_sec = s1.st_mtime;
324 /* Close the files */
332 /* Rename file2 to file2.old. [-r] */
333 if (renameTargets && goods2) {
334 strlcpy(newName, file2, sizeof newName);
335 strlcat(newName, ".old", sizeof newName);
337 printf(" Renaming %s to %s\n", file2, newName);
340 if (rename(file2, newName) < 0) {
341 fprintf(stderr, "Rename of %s to %s failed.\n", file2,
346 /* Rename temporary file to file2 */
347 code = rename(tmpfile, file2);
349 fprintf(stderr, "Rename of %s to %s failed.\n", tmpfile,
354 /* Re-stat file2 and compare file sizes */
355 code = lstat(file2, &s2);
357 fprintf(stderr, "WARNING: Unable to stat new file %s\n",
361 if (s1.st_size != s2.st_size) {
363 "WARNING: New file %s is %u bytes long; should be %u\n",
364 file2, s2.st_size, s1.st_size);
369 /* Set the user-id */
370 if (s2.st_uid != s1.st_uid) {
372 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
375 code = chown(file2, s1.st_uid, -1);
377 fprintf(stderr, "Unable to set owner-id for %s to %d\n",
381 s1.st_mode &= ~04000; /* Don't set suid bit */
385 /* Set the group-id */
386 if (s2.st_gid != s1.st_gid) {
388 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
391 code = chown(file2, -1, s1.st_gid);
393 fprintf(stderr, "Unable to set group-id for %s to %d\n",
397 s1.st_mode &= ~02000; /* Don't set sgid bit */
401 /* Set the mode bits */
402 if (s1.st_mode != s2.st_mode) {
404 printf(" Set mode-bit for %s to %o\n", file2,
405 (s1.st_mode & 07777));
408 code = chmod(file2, s1.st_mode);
410 fprintf(stderr, "Unable to set mode-bits for %s to %d\n",
417 else if ((s1.st_mode & S_IFMT) == S_IFLNK) {
419 * --------------------- Copy symlink --------------------
421 char linkvalue[MAXPATHLEN + 1];
425 printf("Level %d: Symbolic link %s to %s\n", level, file1, file2);
429 /* Don't ovewrite a write protected directory (unless force: -f) */
430 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
432 "Link %s is write protected against its owner; not changed\n",
438 printf(" Copy symbolic link %s->%s to %s\n", file1, linkvalue,
443 n = readlink(file1, linkvalue, sizeof(linkvalue));
445 fprintf(stderr, "Could not read symbolic link %s\n", file1);
446 perror("read link ");
451 unlink(file2); /* Always make the new link (it was easier) */
453 code = symlink(linkvalue, file2);
455 fprintf(stderr, "Could not create symbolic link %s\n", file2);
456 perror("create link ");
460 /*Dealing with symlink */
461 else if (preserveMountPoints && (code = isMountPoint(file1, &blob))) {
463 * --------------------- Copy mount point --------------------
467 perror("checking for mount point ");
471 printf("Level %d: Mount point %s to %s\n", level, file1, file2);
475 /* Don't ovewrite a write protected directory (unless force: -f) */
476 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
478 "Target %s is write protected against its owner; not changed\n",
484 printf(" Copy mount point %s for vol %s to %s\n", file1,
489 unlink(file2); /* Always make the new link (it was easier) */
491 strcat(blob.out, "."); /* stupid convention; these end with a period */
492 code = symlink(blob.out, file2);
494 fprintf(stderr, "Could not create mount point %s for vol %s\n",
496 perror("create mount point ");
501 /*Dealing with mount point */
502 else if (((s1.st_mode & S_IFMT) == S_IFDIR)
503 && (recursive || (level == 0))) {
505 * ----------------------- Copy directory -----------------------
511 char f1[MAXPATHLEN], f2[MAXPATHLEN];
514 struct timeval tv[2];
517 printf("Level %d: Directory %s to %s\n", level, file1, file2);
521 /* Don't ovewrite a write protected directory (unless force: -f) */
522 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
524 "Directory %s is write protected against its owner; not changed\n",
529 strlcpy(f1, file1, sizeof f1);
530 strlcpy(f2, file2, sizeof f2);
531 p1 = f1 + strlen(f1);
532 p2 = f2 + strlen(f2);
533 if (p1 == f1 || p1[-1] != '/')
535 if (p2 == f2 || p2[-1] != '/')
538 dir = opendir(file1);
540 fprintf(stderr, "Couldn't open %s\n", file1);
544 while ((d = readdir(dir)) != NULL) {
545 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
547 strlcpy(p1, d->d_name, sizeof f1 - (p1 - f1));
548 strlcpy(p2, d->d_name, sizeof f2 - (p2 - f2));
549 code = Copy(f1, f2, recursive, level + 1);
551 rcode = 1; /* remember errors */
557 printf("Level %d: Copied directory %s to %s\n", level, file1,
562 mkdir(file2, 0777); /* Handle case where MakeParent not invoked. */
565 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
568 code = chown(file2, s1.st_uid, -1);
570 fprintf(stderr, "Unable to set owner-id for %s to %d\n", file2,
573 s1.st_mode &= ~04000; /* Don't set suid bit */
577 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
580 code = chown(file2, -1, s1.st_gid);
582 fprintf(stderr, "Unable to set group-id for %s to %d\n", file2,
585 s1.st_mode &= ~02000; /* Don't set sgid bit */
589 printf(" Set mode-bit for %s to %o\n", file2,
590 (s1.st_mode & 07777));
593 code = chmod(file2, s1.st_mode);
595 fprintf(stderr, "Unable to set mode-bits for %s to %d\n", file2,
603 printf(" Set acls for %s\n", file2);
610 blob.out_size = MAXACL;
613 /* Get an old-style ACL and convert it */
615 printf(" Getting old style acl\n");
619 for (i = 1; i < strlen(file1); i++)
622 strlcpy(aclspace, &file1[i], sizeof aclspace);
624 blob.in_size = 1 + strlen(aclspace);
625 tfd = open(file1, O_RDONLY, 0);
627 perror("old-acl open ");
630 code = ioctl(tfd, _VICEIOCTL(4), &blob);
633 if (errno == EINVAL) {
636 printf(" _VICEIOCTL(4) returns EINVAL\n");
643 /* Now convert the thing. */
644 oacl = (struct OldAcl *)(aclspace + 4);
645 sprintf(tacl, "%d\n%d\n", oacl->nplus, oacl->nminus);
646 strlcat(tacl, oacl->data, sizeof tacl);
647 strlcpy(aclspace, tacl, sizeof aclspace);
648 } /*Grab and convert old-style ACL */
650 /* Get a new-style ACL */
652 printf(" Getting new style acl\n");
656 code = pioctl(file1, _VICEIOCTL(2), &blob, 1);
658 if (errno == EINVAL) {
661 printf(" _VICEIOCTL(2) returns EINVAL\n");
669 } /*Grab new-style ACL */
672 * Now, set the new-style ACL.
676 printf(" Setting new style acl\n");
682 blob.in_size = 1 + strlen(aclspace);
683 code = pioctl(file2, _VICEIOCTL(1), &blob, 1);
685 if (errno == EINVAL) {
688 printf(" _VICEIOCTL(1) returns EINVAL\n");
692 fprintf(stderr, "Couldn't set acls for %s\n", file2);
699 printf("Not setting acls\n");
703 /* preserve access and modification times: ("-x" disables) */
705 tv[0].tv_sec = s1.st_atime;
707 tv[1].tv_sec = s1.st_mtime;
718 isMountPoint(char *name, struct ViceIoctl *blob)
721 char true_name[1024]; /*dirname */
722 char parent_dir[1024]; /*Parent directory of true name */
723 char *last_component; /*Last component of true name */
725 sprintf(true_name, "%s%s", (name[0] == '/') ? "" : "./", name);
728 * Find rightmost slash, if any.
730 last_component = (char *)strrchr(true_name, '/');
731 if (last_component) {
733 * Found it. Designate everything before it as the parent directory,
734 * everything after it as the final component.
736 strncpy(parent_dir, true_name, last_component - true_name);
737 parent_dir[last_component - true_name] = 0;
738 last_component++; /*Skip the slash */
741 * No slash appears in the given file name. Set parent_dir to the current
742 * directory, and the last component as the given name.
744 strlcpy(parent_dir, ".", sizeof parent_dir);
745 last_component = true_name;
748 if (strcmp(last_component, ".") == 0 || strcmp(last_component, "..") == 0) {
750 "up: you may not use '.' or '..' as the last component\n");
751 fprintf(stderr, "up: of a name in the 'up' command.\n");
755 blob->in = last_component;
756 blob->in_size = strlen(last_component) + 1;
757 blob->out_size = MAXSIZE;
759 memset(space, 0, MAXSIZE);
761 code = pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, blob, 0);
764 printf("'%s' is a mount point for volume '%s'\n", name, space);
768 if (errno == EINVAL) {
769 /* printf( "'%s' is not a mount point.\n", name);
774 fprintf(stderr, "problem examining '%s' in '%s'.\n",
775 last_component, parent_dir);
777 /* Die(errno, (ti->data ? ti->data : parent_dir));