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 /* ************************************************************* */
59 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
61 extern char *sys_errlist[];
65 short renameTargets = 0;
67 short preserveDate = 1;
68 short preserveMountPoints = 0;
69 short forceOverwrite = 0;
74 char file1[MAXPATHLEN], file2[MAXPATHLEN];
77 static char space[MAXSIZE];
86 static void ScanArgs(int argc, char *argv[]);
87 static short MakeParent(char *file, afs_int32 owner);
88 static int Copy(char *file1, char *file2, short recursive, int level);
89 static int isMountPoint(char *name, struct ViceIoctl *blob);
92 /* ************************************************************ */
96 /* ************************************************************ */
98 #include "AFS_component_version_number.c"
101 main(int argc, char *argv[])
105 * The following signal action for AIX is necessary so that in case of a
106 * crash (i.e. core is generated) we can include the user's data section
107 * in the core dump. Unfortunately, by default, only a partial core is
108 * generated which, in many cases, isn't too useful.
110 struct sigaction nsa;
112 sigemptyset(&nsa.sa_mask);
113 nsa.sa_handler = SIG_DFL;
114 nsa.sa_flags = SA_FULLDUMP;
115 sigaction(SIGSEGV, &nsa, NULL);
117 #if !defined (AFS_AIX_ENV) && !defined (AFS_HPUX_ENV)
118 pageSize = getpagesize();
120 ScanArgs(argc, argv);
122 /* now read each line of the CopyList */
123 if (Copy(file1, file2, !oneLevel, 0))
124 return(1); /* some type of failure */
130 #define USAGE "usage: up [-v1frxm] from to\n"
132 ScanArgs(int argc, char *argv[])
134 /* skip program name */
137 /* check for -flag options */
138 while (argc > 0 && *argv[0] == '-') {
163 preserveMountPoints = 1;
167 fprintf(stderr, "Unknown option: '%c'\n", *cp);
168 fprintf(stderr, USAGE);
175 fprintf(stderr, USAGE);
179 strncpy(file1, argv[0], MAXPATHLEN);
180 strncpy(file2, argv[1], MAXPATHLEN);
188 * Make sure the parent directory of this file exists. Returns
189 * 1 if it exists, 0 otherwise. Note: the owner argument
190 * is a hack. All directories made will have this owner.
193 MakeParent(char *file, afs_int32 owner)
195 char parent[MAXPATHLEN];
199 strcpy(parent, file);
201 p = strrchr(parent, '/');
204 } else if (p > parent) {
210 if (stat(parent, &s) < 0) {
211 if (!MakeParent(parent, owner))
215 printf("Creating directory %s\n", parent);
220 chown(parent, owner, -1);
228 * This does the bulk of the work of the program. Handle one file,
229 * possibly copying subfiles if this is a directory
232 Copy(char *file1, char *file2, short recursive, int level)
234 struct stat s1, s2; /*Stat blocks */
235 struct ViceIoctl blob;
236 char aclspace[MAXACL];
237 afs_int32 rcode = 0, code;
240 code = lstat(file1, &s1);
242 fprintf(stderr, "Can't find %s\n", file1);
246 code = lstat(file2, &s2);
248 if (!MakeParent(file2, s1.st_uid))
253 if ((s1.st_mode & S_IFMT) == S_IFREG) {
255 * -------------------- Copy regular file --------------------
258 char buf[4096]; /* Must be bigger than sizeof (*head) */
259 struct timeval tv[2];
260 char tmpfile[MAXPATHLEN], newName[MAXPATHLEN];
263 printf("Level %d: File %s to %s\n", level, file1, file2);
267 /* Wonder if there is a security hole */
268 if (((s1.st_mode & 04002) == 04002) || ((s1.st_mode & 04020) == 04020)
269 || ((s1.st_mode & 02002) == 02002)) {
271 "WARNING: Mode-bits security hole in files %s and %s\n",
275 if (!goods2 || (s1.st_mtime != s2.st_mtime) || (s1.st_size != s2.st_size)) { /*c */
276 /* Don't ovewrite a write protected file (unless force: -f) */
277 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
279 "File %s is write protected against its owner; not changed\n",
285 printf(" Copy file %s to %s (%u Bytes)\n", file1, file2,
290 strcpy(tmpfile, file2); /* Name of temporary file */
291 strcat(tmpfile, ".UPD");
293 /* open file1 for input */
294 f1 = open(file1, O_RDONLY);
296 fprintf(stderr, "Unable to open input file %s, ", file1);
297 if (errno >= sys_nerr)
298 fprintf(stderr, "error code = %d\n", errno);
300 fprintf(stderr, "%s\n", sys_errlist[errno]);
304 /* open temporary output file */
305 f2 = open(tmpfile, (O_WRONLY | O_CREAT | O_TRUNC), s1.st_mode);
307 fprintf(stderr, "Unable to open output file %s, ", tmpfile);
308 if (errno >= sys_nerr)
309 fprintf(stderr, "error code = %d\n", errno);
311 fprintf(stderr, "%s\n", sys_errlist[errno]);
317 /* Copy file1 to temporary file */
318 while ((n = read(f1, buf, sizeof(buf))) > 0) {
319 if (write(f2, buf, n) != n) {
321 "Write failed, file %s must be copied again.\n",
326 /* preserve access and modification times: ("-x" disables) */
328 tv[0].tv_sec = s1.st_atime;
330 tv[1].tv_sec = s1.st_mtime;
335 /* Close the files */
343 /* Rename file2 to file2.old. [-r] */
344 if (renameTargets && goods2) {
345 strcpy(newName, file2);
346 strcat(newName, ".old");
348 printf(" Renaming %s to %s\n", file2, newName);
351 if (rename(file2, newName) < 0) {
352 fprintf(stderr, "Rename of %s to %s failed.\n", file2,
357 /* Rename temporary file to file2 */
358 code = rename(tmpfile, file2);
360 fprintf(stderr, "Rename of %s to %s failed.\n", tmpfile,
365 /* Re-stat file2 and compare file sizes */
366 code = lstat(file2, &s2);
368 fprintf(stderr, "WARNING: Unable to stat new file %s\n",
372 if (s1.st_size != s2.st_size) {
374 "WARNING: New file %s is %u bytes long; should be %u\n",
375 file2, s2.st_size, s1.st_size);
380 /* Set the user-id */
381 if (s2.st_uid != s1.st_uid) {
383 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
386 code = chown(file2, s1.st_uid, -1);
388 fprintf(stderr, "Unable to set owner-id for %s to %d\n",
392 s1.st_mode &= ~04000; /* Don't set suid bit */
396 /* Set the group-id */
397 if (s2.st_gid != s1.st_gid) {
399 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
402 code = chown(file2, -1, s1.st_gid);
404 fprintf(stderr, "Unable to set group-id for %s to %d\n",
408 s1.st_mode &= ~02000; /* Don't set sgid bit */
412 /* Set the mode bits */
413 if (s1.st_mode != s2.st_mode) {
415 printf(" Set mode-bit for %s to %o\n", file2,
416 (s1.st_mode & 07777));
419 code = chmod(file2, s1.st_mode);
421 fprintf(stderr, "Unable to set mode-bits for %s to %d\n",
428 else if ((s1.st_mode & S_IFMT) == S_IFLNK) {
430 * --------------------- Copy symlink --------------------
432 char linkvalue[MAXPATHLEN + 1];
436 printf("Level %d: Symbolic link %s to %s\n", level, file1, file2);
440 /* Don't ovewrite a write protected directory (unless force: -f) */
441 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
443 "Link %s is write protected against its owner; not changed\n",
449 printf(" Copy symbolic link %s->%s to %s\n", file1, linkvalue,
454 n = readlink(file1, linkvalue, sizeof(linkvalue));
456 fprintf(stderr, "Could not read symbolic link %s\n", file1);
457 perror("read link ");
462 unlink(file2); /* Always make the new link (it was easier) */
464 code = symlink(linkvalue, file2);
466 fprintf(stderr, "Could not create symbolic link %s\n", file2);
467 perror("create link ");
471 /*Dealing with symlink */
472 else if (preserveMountPoints && (code = isMountPoint(file1, &blob))) {
474 * --------------------- Copy mount point --------------------
478 perror("checking for mount point ");
482 printf("Level %d: Mount point %s to %s\n", level, file1, file2);
486 /* Don't ovewrite a write protected directory (unless force: -f) */
487 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
489 "Target %s is write protected against its owner; not changed\n",
495 printf(" Copy mount point %s for vol %s to %s\n", file1,
500 unlink(file2); /* Always make the new link (it was easier) */
502 strcat(blob.out, "."); /* stupid convention; these end with a period */
503 code = symlink(blob.out, file2);
505 fprintf(stderr, "Could not create mount point %s for vol %s\n",
507 perror("create mount point ");
512 /*Dealing with mount point */
513 else if (((s1.st_mode & S_IFMT) == S_IFDIR)
514 && (recursive || (level == 0))) {
516 * ----------------------- Copy directory -----------------------
522 char f1[MAXPATHLEN], f2[MAXPATHLEN];
527 printf("Level %d: Directory %s to %s\n", level, file1, file2);
531 /* Don't ovewrite a write protected directory (unless force: -f) */
532 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
534 "Directory %s is write protected against its owner; not changed\n",
541 p1 = f1 + strlen(f1);
542 p2 = f2 + strlen(f2);
543 if (p1 == f1 || p1[-1] != '/')
545 if (p2 == f2 || p2[-1] != '/')
548 dir = opendir(file1);
550 fprintf(stderr, "Couldn't open %s\n", file1);
554 while ((d = readdir(dir)) != NULL) {
555 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
557 strcpy(p1, d->d_name);
558 strcpy(p2, d->d_name);
559 code = Copy(f1, f2, recursive, level + 1);
561 rcode = 1; /* remember errors */
567 printf("Level %d: Copied directory %s to %s\n", level, file1,
572 mkdir(file2, 0777); /* Handle case where MakeParent not invoked. */
575 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
578 code = chown(file2, s1.st_uid, -1);
580 fprintf(stderr, "Unable to set owner-id for %s to %d\n", file2,
583 s1.st_mode &= ~04000; /* Don't set suid bit */
587 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
590 code = chown(file2, -1, s1.st_gid);
592 fprintf(stderr, "Unable to set group-id for %s to %d\n", file2,
595 s1.st_mode &= ~02000; /* Don't set sgid bit */
599 printf(" Set mode-bit for %s to %o\n", file2,
600 (s1.st_mode & 07777));
603 code = chmod(file2, s1.st_mode);
605 fprintf(stderr, "Unable to set mode-bits for %s to %d\n", file2,
613 printf(" Set acls for %s\n", file2);
620 blob.out_size = MAXACL;
623 /* Get an old-style ACL and convert it */
625 printf(" Getting old style acl\n");
629 for (i = 1; i < strlen(file1); i++)
632 strcpy(aclspace, &file1[i]);
634 blob.in_size = 1 + strlen(aclspace);
635 tfd = open(file1, O_RDONLY, 0);
637 perror("old-acl open ");
640 code = ioctl(tfd, _VICEIOCTL(4), &blob);
643 if (errno == EINVAL) {
646 printf(" _VICEIOCTL(4) returns EINVAL\n");
653 /* Now convert the thing. */
654 oacl = (struct OldAcl *)(aclspace + 4);
655 sprintf(tacl, "%d\n%d\n", oacl->nplus, oacl->nminus);
656 strcat(tacl, oacl->data);
657 strcpy(aclspace, tacl);
658 } /*Grab and convert old-style ACL */
660 /* Get a new-style ACL */
662 printf(" Getting new style acl\n");
666 code = pioctl(file1, _VICEIOCTL(2), &blob, 1);
668 if (errno == EINVAL) {
671 printf(" _VICEIOCTL(2) returns EINVAL\n");
679 } /*Grab new-style ACL */
682 * Now, set the new-style ACL.
686 printf(" Setting new style acl\n");
692 blob.in_size = 1 + strlen(aclspace);
693 code = pioctl(file2, _VICEIOCTL(1), &blob, 1);
695 if (errno == EINVAL) {
698 printf(" _VICEIOCTL(1) returns EINVAL\n");
702 fprintf(stderr, "Couldn't set acls for %s\n", file2);
709 printf("Not setting acls\n");
719 isMountPoint(char *name, struct ViceIoctl *blob)
722 char true_name[1024]; /*dirname */
723 char parent_dir[1024]; /*Parent directory of true name */
724 char *last_component; /*Last component of true name */
726 sprintf(true_name, "%s%s", (name[0] == '/') ? "" : "./", name);
729 * Find rightmost slash, if any.
731 last_component = (char *)strrchr(true_name, '/');
732 if (last_component) {
734 * Found it. Designate everything before it as the parent directory,
735 * everything after it as the final component.
737 strncpy(parent_dir, true_name, last_component - true_name);
738 parent_dir[last_component - true_name] = 0;
739 last_component++; /*Skip the slash */
742 * No slash appears in the given file name. Set parent_dir to the current
743 * directory, and the last component as the given name.
745 strcpy(parent_dir, ".");
746 last_component = true_name;
749 if (strcmp(last_component, ".") == 0 || strcmp(last_component, "..") == 0) {
751 "up: you may not use '.' or '..' as the last component\n");
752 fprintf(stderr, "up: of a name in the 'up' command.\n");
756 blob->in = last_component;
757 blob->in_size = strlen(last_component) + 1;
758 blob->out_size = MAXSIZE;
760 memset(space, 0, MAXSIZE);
762 code = pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, blob, 0);
765 printf("'%s' is a mount point for volume '%s'\n", name, space);
769 if (errno == EINVAL) {
770 /* printf( "'%s' is not a mount point.\n", name);
775 fprintf(stderr, "problem examining '%s' in '%s'.\n",
776 last_component, parent_dir);
778 /* Die(errno, (ti->data ? ti->data : parent_dir));