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>
15 #include <afs/afs_args.h>
21 #include <afs/venus.h>
22 #include <afs/sys_prototypes.h>
23 #include <afs/afsutil.h>
24 #include <afs/afs_consts.h>
26 /* ************************************************************* */
31 short renameTargets = 0;
33 short preserveDate = 1;
34 short preserveMountPoints = 0;
35 short forceOverwrite = 0;
39 char file1[MAXPATHLEN], file2[MAXPATHLEN];
41 static char space[AFS_PIOCTL_MAXSIZE];
50 static void ScanArgs(int argc, char *argv[]);
51 static short MakeParent(char *file, afs_int32 owner);
52 static int Copy(char *file1, char *file2, short recursive, int level);
53 static int isMountPoint(char *name, struct ViceIoctl *blob);
56 /* ************************************************************ */
60 /* ************************************************************ */
62 #include "AFS_component_version_number.c"
65 main(int argc, char *argv[])
69 * The following signal action for AIX is necessary so that in case of a
70 * crash (i.e. core is generated) we can include the user's data section
71 * in the core dump. Unfortunately, by default, only a partial core is
72 * generated which, in many cases, isn't too useful.
76 sigemptyset(&nsa.sa_mask);
77 nsa.sa_handler = SIG_DFL;
78 nsa.sa_flags = SA_FULLDUMP;
79 sigaction(SIGSEGV, &nsa, NULL);
83 /* now read each line of the CopyList */
84 if (Copy(file1, file2, !oneLevel, 0))
85 return(1); /* some type of failure */
91 #define USAGE "usage: up [-v1frxm] from to\n"
93 ScanArgs(int argc, char *argv[])
95 /* skip program name */
98 /* check for -flag options */
99 while (argc > 0 && *argv[0] == '-') {
102 if (strlen(cp) > 2) {
128 preserveMountPoints = 1;
135 fprintf(stderr, "Unknown option: '%s'\n", cp);
136 fprintf(stderr, USAGE);
143 fprintf(stderr, USAGE);
147 strncpy(file1, argv[0], MAXPATHLEN);
148 strncpy(file2, argv[1], MAXPATHLEN);
156 * Make sure the parent directory of this file exists. Returns
157 * 1 if it exists, 0 otherwise. Note: the owner argument
158 * is a hack. All directories made will have this owner.
161 MakeParent(char *file, afs_int32 owner)
163 char parent[MAXPATHLEN];
167 strlcpy(parent, file, sizeof parent);
169 p = strrchr(parent, '/');
171 strlcpy(parent, ".", sizeof parent);
172 } else if (p > parent) {
178 if (stat(parent, &s) < 0) {
179 if (!MakeParent(parent, owner))
183 printf("Creating directory %s\n", parent);
188 chown(parent, owner, -1);
196 * This does the bulk of the work of the program. Handle one file,
197 * possibly copying subfiles if this is a directory
200 Copy(char *file1, char *file2, short recursive, int level)
202 struct stat s1, s2; /*Stat blocks */
203 struct ViceIoctl blob;
204 char aclspace[MAXACL];
205 afs_int32 rcode = 0, code;
208 code = lstat(file1, &s1);
210 fprintf(stderr, "Can't find %s\n", file1);
214 code = lstat(file2, &s2);
216 if (!MakeParent(file2, s1.st_uid))
221 if ((s1.st_mode & S_IFMT) == S_IFREG) {
223 * -------------------- Copy regular file --------------------
226 char buf[4096]; /* Must be bigger than sizeof (*head) */
227 struct timeval tv[2];
228 char tmpfile[MAXPATHLEN], newName[MAXPATHLEN];
231 printf("Level %d: File %s to %s\n", level, file1, file2);
235 /* Wonder if there is a security hole */
236 if (((s1.st_mode & 04002) == 04002) || ((s1.st_mode & 04020) == 04020)
237 || ((s1.st_mode & 02002) == 02002)) {
239 "WARNING: Mode-bits security hole in files %s and %s\n",
243 if (!goods2 || (s1.st_mtime != s2.st_mtime) || (s1.st_size != s2.st_size)) { /*c */
244 /* Don't ovewrite a write protected file (unless force: -f) */
245 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
247 "File %s is write protected against its owner; not changed\n",
253 printf(" Copy file %s to %s (%u Bytes)\n", file1, file2,
254 (unsigned int) s1.st_size);
258 strlcpy(tmpfile, file2, sizeof tmpfile); /* Name of temporary file */
259 strlcat(tmpfile, ".UPD", sizeof tmpfile);
261 /* open file1 for input */
262 f1 = open(file1, O_RDONLY);
264 fprintf(stderr, "Unable to open input file %s: %s\n",
265 file1, strerror(errno));
269 /* open temporary output file */
270 f2 = open(tmpfile, (O_WRONLY | O_CREAT | O_TRUNC), s1.st_mode);
272 fprintf(stderr, "Unable to open output file %s: %s\n",
273 tmpfile, strerror(errno));
279 /* Copy file1 to temporary file */
280 while ((n = read(f1, buf, sizeof(buf))) > 0) {
281 if (write(f2, buf, n) != n) {
283 "Write failed, file %s must be copied again.\n",
288 /* preserve access and modification times: ("-x" disables) */
290 tv[0].tv_sec = s1.st_atime;
292 tv[1].tv_sec = s1.st_mtime;
297 /* Close the files */
305 /* Rename file2 to file2.old. [-r] */
306 if (renameTargets && goods2) {
307 strlcpy(newName, file2, sizeof newName);
308 strlcat(newName, ".old", sizeof newName);
310 printf(" Renaming %s to %s\n", file2, newName);
313 if (rename(file2, newName) < 0) {
314 fprintf(stderr, "Rename of %s to %s failed.\n", file2,
319 /* Rename temporary file to file2 */
320 code = rename(tmpfile, file2);
322 fprintf(stderr, "Rename of %s to %s failed.\n", tmpfile,
327 /* Re-stat file2 and compare file sizes */
328 code = lstat(file2, &s2);
330 fprintf(stderr, "WARNING: Unable to stat new file %s\n",
334 if (s1.st_size != s2.st_size) {
336 "WARNING: New file %s is %u bytes long; should be %u\n",
337 file2, (unsigned int) s2.st_size,
338 (unsigned int) s1.st_size);
343 /* Set the user-id */
344 if (s2.st_uid != s1.st_uid) {
346 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
349 code = chown(file2, s1.st_uid, -1);
351 fprintf(stderr, "Unable to set owner-id for %s to %d\n",
355 s1.st_mode &= ~04000; /* Don't set suid bit */
359 /* Set the group-id */
360 if (s2.st_gid != s1.st_gid) {
362 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
365 code = chown(file2, -1, s1.st_gid);
367 fprintf(stderr, "Unable to set group-id for %s to %d\n",
371 s1.st_mode &= ~02000; /* Don't set sgid bit */
375 /* Set the mode bits */
376 if (s1.st_mode != s2.st_mode) {
378 printf(" Set mode-bit for %s to %o\n", file2,
379 (s1.st_mode & 07777));
382 code = chmod(file2, s1.st_mode);
384 fprintf(stderr, "Unable to set mode-bits for %s to %d\n",
391 else if ((s1.st_mode & S_IFMT) == S_IFLNK) {
393 * --------------------- Copy symlink --------------------
395 char linkvalue[MAXPATHLEN + 1];
399 printf("Level %d: Symbolic link %s to %s\n", level, file1, file2);
403 /* Don't ovewrite a write protected directory (unless force: -f) */
404 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
406 "Link %s is write protected against its owner; not changed\n",
412 printf(" Copy symbolic link %s->%s to %s\n", file1, linkvalue,
417 n = readlink(file1, linkvalue, sizeof(linkvalue));
419 fprintf(stderr, "Could not read symbolic link %s\n", file1);
420 perror("read link ");
425 unlink(file2); /* Always make the new link (it was easier) */
427 code = symlink(linkvalue, file2);
429 fprintf(stderr, "Could not create symbolic link %s\n", file2);
430 perror("create link ");
434 /*Dealing with symlink */
435 else if (preserveMountPoints && (code = isMountPoint(file1, &blob))) {
437 * --------------------- Copy mount point --------------------
441 perror("checking for mount point ");
445 printf("Level %d: Mount point %s to %s\n", level, file1, file2);
449 /* Don't ovewrite a write protected directory (unless force: -f) */
450 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
452 "Target %s is write protected against its owner; not changed\n",
458 printf(" Copy mount point %s for vol %s to %s\n", file1,
463 unlink(file2); /* Always make the new link (it was easier) */
465 strcat(blob.out, "."); /* stupid convention; these end with a period */
466 code = symlink(blob.out, file2);
468 fprintf(stderr, "Could not create mount point %s for vol %s\n",
470 perror("create mount point ");
475 /*Dealing with mount point */
476 else if (((s1.st_mode & S_IFMT) == S_IFDIR)
477 && (recursive || (level == 0))) {
479 * ----------------------- Copy directory -----------------------
485 char f1[MAXPATHLEN], f2[MAXPATHLEN];
488 struct timeval tv[2];
491 printf("Level %d: Directory %s to %s\n", level, file1, file2);
495 /* Don't ovewrite a write protected directory (unless force: -f) */
496 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
498 "Directory %s is write protected against its owner; not changed\n",
503 strlcpy(f1, file1, sizeof f1);
504 strlcpy(f2, file2, sizeof f2);
505 p1 = f1 + strlen(f1);
506 p2 = f2 + strlen(f2);
507 if (p1 == f1 || p1[-1] != '/')
509 if (p2 == f2 || p2[-1] != '/')
512 dir = opendir(file1);
514 fprintf(stderr, "Couldn't open %s\n", file1);
518 while ((d = readdir(dir)) != NULL) {
519 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
521 strlcpy(p1, d->d_name, sizeof f1 - (p1 - f1));
522 strlcpy(p2, d->d_name, sizeof f2 - (p2 - f2));
523 code = Copy(f1, f2, recursive, level + 1);
525 rcode = 1; /* remember errors */
531 printf("Level %d: Copied directory %s to %s\n", level, file1,
536 mkdir(file2, 0777); /* Handle case where MakeParent not invoked. */
539 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
542 code = chown(file2, s1.st_uid, -1);
544 fprintf(stderr, "Unable to set owner-id for %s to %d\n", file2,
547 s1.st_mode &= ~04000; /* Don't set suid bit */
551 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
554 code = chown(file2, -1, s1.st_gid);
556 fprintf(stderr, "Unable to set group-id for %s to %d\n", file2,
559 s1.st_mode &= ~02000; /* Don't set sgid bit */
563 printf(" Set mode-bit for %s to %o\n", file2,
564 (s1.st_mode & 07777));
567 code = chmod(file2, s1.st_mode);
569 fprintf(stderr, "Unable to set mode-bits for %s to %d\n", file2,
577 printf(" Set acls for %s\n", file2);
584 blob.out_size = MAXACL;
587 /* Get an old-style ACL and convert it */
589 printf(" Getting old style acl\n");
593 for (i = 1; i < strlen(file1); i++)
596 strlcpy(aclspace, &file1[i], sizeof aclspace);
598 blob.in_size = 1 + strlen(aclspace);
599 tfd = open(file1, O_RDONLY, 0);
601 perror("old-acl open ");
604 code = ioctl(tfd, _VICEIOCTL(4), &blob);
607 if (errno == EINVAL) {
610 printf(" _VICEIOCTL(4) returns EINVAL\n");
617 /* Now convert the thing. */
618 oacl = (struct OldAcl *)(aclspace + 4);
619 sprintf(tacl, "%d\n%d\n", oacl->nplus, oacl->nminus);
620 strlcat(tacl, oacl->data, sizeof tacl);
621 strlcpy(aclspace, tacl, sizeof aclspace);
622 } /*Grab and convert old-style ACL */
624 /* Get a new-style ACL */
626 printf(" Getting new style acl\n");
630 code = pioctl(file1, _VICEIOCTL(2), &blob, 1);
632 if (errno == EINVAL) {
635 printf(" _VICEIOCTL(2) returns EINVAL\n");
643 } /*Grab new-style ACL */
646 * Now, set the new-style ACL.
650 printf(" Setting new style acl\n");
656 blob.in_size = 1 + strlen(aclspace);
657 code = pioctl(file2, _VICEIOCTL(1), &blob, 1);
659 if (errno == EINVAL) {
662 printf(" _VICEIOCTL(1) returns EINVAL\n");
666 fprintf(stderr, "Couldn't set acls for %s\n", file2);
673 printf("Not setting acls\n");
677 /* preserve access and modification times: ("-x" disables) */
679 tv[0].tv_sec = s1.st_atime;
681 tv[1].tv_sec = s1.st_mtime;
692 isMountPoint(char *name, struct ViceIoctl *blob)
695 char true_name[1024]; /*dirname */
696 char parent_dir[1024]; /*Parent directory of true name */
697 char *last_component; /*Last component of true name */
699 sprintf(true_name, "%s%s", (name[0] == '/') ? "" : "./", name);
702 * Find rightmost slash, if any.
704 last_component = (char *)strrchr(true_name, '/');
705 if (last_component) {
707 * Found it. Designate everything before it as the parent directory,
708 * everything after it as the final component.
710 strncpy(parent_dir, true_name, last_component - true_name);
711 parent_dir[last_component - true_name] = 0;
712 last_component++; /*Skip the slash */
715 * No slash appears in the given file name. Set parent_dir to the current
716 * directory, and the last component as the given name.
718 strlcpy(parent_dir, ".", sizeof parent_dir);
719 last_component = true_name;
722 if (strcmp(last_component, ".") == 0 || strcmp(last_component, "..") == 0) {
724 "up: you may not use '.' or '..' as the last component\n");
725 fprintf(stderr, "up: of a name in the 'up' command.\n");
729 blob->in = last_component;
730 blob->in_size = strlen(last_component) + 1;
731 blob->out_size = AFS_PIOCTL_MAXSIZE;
733 memset(space, 0, AFS_PIOCTL_MAXSIZE);
735 code = pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, blob, 0);
738 printf("'%s' is a mount point for volume '%s'\n", name, space);
742 if (errno == EINVAL) {
743 /* printf( "'%s' is not a mount point.\n", name);
748 fprintf(stderr, "problem examining '%s' in '%s'.\n",
749 last_component, parent_dir);
751 /* Die(errno, (ti->data ? ti->data : parent_dir));