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] == '-') {
128 if (strlen(cp) > 2) {
154 preserveMountPoints = 1;
161 fprintf(stderr, "Unknown option: '%s'\n", cp);
162 fprintf(stderr, USAGE);
169 fprintf(stderr, USAGE);
173 strncpy(file1, argv[0], MAXPATHLEN);
174 strncpy(file2, argv[1], MAXPATHLEN);
182 * Make sure the parent directory of this file exists. Returns
183 * 1 if it exists, 0 otherwise. Note: the owner argument
184 * is a hack. All directories made will have this owner.
187 MakeParent(char *file, afs_int32 owner)
189 char parent[MAXPATHLEN];
193 strlcpy(parent, file, sizeof parent);
195 p = strrchr(parent, '/');
197 strlcpy(parent, ".", sizeof parent);
198 } else if (p > parent) {
204 if (stat(parent, &s) < 0) {
205 if (!MakeParent(parent, owner))
209 printf("Creating directory %s\n", parent);
214 chown(parent, owner, -1);
222 * This does the bulk of the work of the program. Handle one file,
223 * possibly copying subfiles if this is a directory
226 Copy(char *file1, char *file2, short recursive, int level)
228 struct stat s1, s2; /*Stat blocks */
229 struct ViceIoctl blob;
230 char aclspace[MAXACL];
231 afs_int32 rcode = 0, code;
234 code = lstat(file1, &s1);
236 fprintf(stderr, "Can't find %s\n", file1);
240 code = lstat(file2, &s2);
242 if (!MakeParent(file2, s1.st_uid))
247 if ((s1.st_mode & S_IFMT) == S_IFREG) {
249 * -------------------- Copy regular file --------------------
252 char buf[4096]; /* Must be bigger than sizeof (*head) */
253 struct timeval tv[2];
254 char tmpfile[MAXPATHLEN], newName[MAXPATHLEN];
257 printf("Level %d: File %s to %s\n", level, file1, file2);
261 /* Wonder if there is a security hole */
262 if (((s1.st_mode & 04002) == 04002) || ((s1.st_mode & 04020) == 04020)
263 || ((s1.st_mode & 02002) == 02002)) {
265 "WARNING: Mode-bits security hole in files %s and %s\n",
269 if (!goods2 || (s1.st_mtime != s2.st_mtime) || (s1.st_size != s2.st_size)) { /*c */
270 /* Don't ovewrite a write protected file (unless force: -f) */
271 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
273 "File %s is write protected against its owner; not changed\n",
279 printf(" Copy file %s to %s (%u Bytes)\n", file1, file2,
280 (unsigned int) s1.st_size);
284 strlcpy(tmpfile, file2, sizeof tmpfile); /* Name of temporary file */
285 strlcat(tmpfile, ".UPD", sizeof tmpfile);
287 /* open file1 for input */
288 f1 = open(file1, O_RDONLY);
290 fprintf(stderr, "Unable to open input file %s: %s\n",
291 file1, strerror(errno));
295 /* open temporary output file */
296 f2 = open(tmpfile, (O_WRONLY | O_CREAT | O_TRUNC), s1.st_mode);
298 fprintf(stderr, "Unable to open output file %s: %s\n",
299 tmpfile, strerror(errno));
305 /* Copy file1 to temporary file */
306 while ((n = read(f1, buf, sizeof(buf))) > 0) {
307 if (write(f2, buf, n) != n) {
309 "Write failed, file %s must be copied again.\n",
314 /* preserve access and modification times: ("-x" disables) */
316 tv[0].tv_sec = s1.st_atime;
318 tv[1].tv_sec = s1.st_mtime;
323 /* Close the files */
331 /* Rename file2 to file2.old. [-r] */
332 if (renameTargets && goods2) {
333 strlcpy(newName, file2, sizeof newName);
334 strlcat(newName, ".old", sizeof newName);
336 printf(" Renaming %s to %s\n", file2, newName);
339 if (rename(file2, newName) < 0) {
340 fprintf(stderr, "Rename of %s to %s failed.\n", file2,
345 /* Rename temporary file to file2 */
346 code = rename(tmpfile, file2);
348 fprintf(stderr, "Rename of %s to %s failed.\n", tmpfile,
353 /* Re-stat file2 and compare file sizes */
354 code = lstat(file2, &s2);
356 fprintf(stderr, "WARNING: Unable to stat new file %s\n",
360 if (s1.st_size != s2.st_size) {
362 "WARNING: New file %s is %u bytes long; should be %u\n",
363 file2, (unsigned int) s2.st_size,
364 (unsigned int) 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 = AFS_PIOCTL_MAXSIZE;
759 memset(space, 0, AFS_PIOCTL_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));