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>
47 #include <afs/venus.h>
49 /* ************************************************************* */
54 short renameTargets = 0;
56 short preserveDate = 1;
57 short preserveMountPoints = 0;
58 short forceOverwrite = 0;
63 char file1[MAXPATHLEN], file2[MAXPATHLEN];
66 static char space[MAXSIZE];
75 static void ScanArgs(int argc, char *argv[]);
76 static short MakeParent(char *file, afs_int32 owner);
77 static int Copy(char *file1, char *file2, short recursive, int level);
78 static int isMountPoint(char *name, struct ViceIoctl *blob);
81 /* ************************************************************ */
85 /* ************************************************************ */
87 #include "AFS_component_version_number.c"
90 main(int argc, char *argv[])
94 * The following signal action for AIX is necessary so that in case of a
95 * crash (i.e. core is generated) we can include the user's data section
96 * in the core dump. Unfortunately, by default, only a partial core is
97 * generated which, in many cases, isn't too useful.
101 sigemptyset(&nsa.sa_mask);
102 nsa.sa_handler = SIG_DFL;
103 nsa.sa_flags = SA_FULLDUMP;
104 sigaction(SIGSEGV, &nsa, NULL);
106 #if !defined (AFS_AIX_ENV) && !defined (AFS_HPUX_ENV)
107 pageSize = getpagesize();
109 ScanArgs(argc, argv);
111 /* now read each line of the CopyList */
112 if (Copy(file1, file2, !oneLevel, 0))
113 return(1); /* some type of failure */
119 #define USAGE "usage: up [-v1frxm] from to\n"
121 ScanArgs(int argc, char *argv[])
123 /* skip program name */
126 /* check for -flag options */
127 while (argc > 0 && *argv[0] == '-') {
152 preserveMountPoints = 1;
156 fprintf(stderr, "Unknown option: '%c'\n", *cp);
157 fprintf(stderr, USAGE);
164 fprintf(stderr, USAGE);
168 strncpy(file1, argv[0], MAXPATHLEN);
169 strncpy(file2, argv[1], MAXPATHLEN);
177 * Make sure the parent directory of this file exists. Returns
178 * 1 if it exists, 0 otherwise. Note: the owner argument
179 * is a hack. All directories made will have this owner.
182 MakeParent(char *file, afs_int32 owner)
184 char parent[MAXPATHLEN];
188 strlcpy(parent, file, sizeof parent);
190 p = strrchr(parent, '/');
192 strlcpy(parent, ".", sizeof parent);
193 } else if (p > parent) {
199 if (stat(parent, &s) < 0) {
200 if (!MakeParent(parent, owner))
204 printf("Creating directory %s\n", parent);
209 chown(parent, owner, -1);
217 * This does the bulk of the work of the program. Handle one file,
218 * possibly copying subfiles if this is a directory
221 Copy(char *file1, char *file2, short recursive, int level)
223 struct stat s1, s2; /*Stat blocks */
224 struct ViceIoctl blob;
225 char aclspace[MAXACL];
226 afs_int32 rcode = 0, code;
229 code = lstat(file1, &s1);
231 fprintf(stderr, "Can't find %s\n", file1);
235 code = lstat(file2, &s2);
237 if (!MakeParent(file2, s1.st_uid))
242 if ((s1.st_mode & S_IFMT) == S_IFREG) {
244 * -------------------- Copy regular file --------------------
247 char buf[4096]; /* Must be bigger than sizeof (*head) */
248 struct timeval tv[2];
249 char tmpfile[MAXPATHLEN], newName[MAXPATHLEN];
252 printf("Level %d: File %s to %s\n", level, file1, file2);
256 /* Wonder if there is a security hole */
257 if (((s1.st_mode & 04002) == 04002) || ((s1.st_mode & 04020) == 04020)
258 || ((s1.st_mode & 02002) == 02002)) {
260 "WARNING: Mode-bits security hole in files %s and %s\n",
264 if (!goods2 || (s1.st_mtime != s2.st_mtime) || (s1.st_size != s2.st_size)) { /*c */
265 /* Don't ovewrite a write protected file (unless force: -f) */
266 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
268 "File %s is write protected against its owner; not changed\n",
274 printf(" Copy file %s to %s (%u Bytes)\n", file1, file2,
279 strlcpy(tmpfile, file2, sizeof tmpfile); /* Name of temporary file */
280 strlcat(tmpfile, ".UPD", sizeof tmpfile);
282 /* open file1 for input */
283 f1 = open(file1, O_RDONLY);
285 fprintf(stderr, "Unable to open input file %s: %s\n",
286 file1, strerror(errno));
290 /* open temporary output file */
291 f2 = open(tmpfile, (O_WRONLY | O_CREAT | O_TRUNC), s1.st_mode);
293 fprintf(stderr, "Unable to open output file %s: %s\n",
294 tmpfile, strerror(errno));
300 /* Copy file1 to temporary file */
301 while ((n = read(f1, buf, sizeof(buf))) > 0) {
302 if (write(f2, buf, n) != n) {
304 "Write failed, file %s must be copied again.\n",
309 /* preserve access and modification times: ("-x" disables) */
311 tv[0].tv_sec = s1.st_atime;
313 tv[1].tv_sec = s1.st_mtime;
318 /* Close the files */
326 /* Rename file2 to file2.old. [-r] */
327 if (renameTargets && goods2) {
328 strlcpy(newName, file2, sizeof newName);
329 strlcat(newName, ".old", sizeof newName);
331 printf(" Renaming %s to %s\n", file2, newName);
334 if (rename(file2, newName) < 0) {
335 fprintf(stderr, "Rename of %s to %s failed.\n", file2,
340 /* Rename temporary file to file2 */
341 code = rename(tmpfile, file2);
343 fprintf(stderr, "Rename of %s to %s failed.\n", tmpfile,
348 /* Re-stat file2 and compare file sizes */
349 code = lstat(file2, &s2);
351 fprintf(stderr, "WARNING: Unable to stat new file %s\n",
355 if (s1.st_size != s2.st_size) {
357 "WARNING: New file %s is %u bytes long; should be %u\n",
358 file2, s2.st_size, s1.st_size);
363 /* Set the user-id */
364 if (s2.st_uid != s1.st_uid) {
366 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
369 code = chown(file2, s1.st_uid, -1);
371 fprintf(stderr, "Unable to set owner-id for %s to %d\n",
375 s1.st_mode &= ~04000; /* Don't set suid bit */
379 /* Set the group-id */
380 if (s2.st_gid != s1.st_gid) {
382 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
385 code = chown(file2, -1, s1.st_gid);
387 fprintf(stderr, "Unable to set group-id for %s to %d\n",
391 s1.st_mode &= ~02000; /* Don't set sgid bit */
395 /* Set the mode bits */
396 if (s1.st_mode != s2.st_mode) {
398 printf(" Set mode-bit for %s to %o\n", file2,
399 (s1.st_mode & 07777));
402 code = chmod(file2, s1.st_mode);
404 fprintf(stderr, "Unable to set mode-bits for %s to %d\n",
411 else if ((s1.st_mode & S_IFMT) == S_IFLNK) {
413 * --------------------- Copy symlink --------------------
415 char linkvalue[MAXPATHLEN + 1];
419 printf("Level %d: Symbolic link %s to %s\n", level, file1, file2);
423 /* Don't ovewrite a write protected directory (unless force: -f) */
424 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
426 "Link %s is write protected against its owner; not changed\n",
432 printf(" Copy symbolic link %s->%s to %s\n", file1, linkvalue,
437 n = readlink(file1, linkvalue, sizeof(linkvalue));
439 fprintf(stderr, "Could not read symbolic link %s\n", file1);
440 perror("read link ");
445 unlink(file2); /* Always make the new link (it was easier) */
447 code = symlink(linkvalue, file2);
449 fprintf(stderr, "Could not create symbolic link %s\n", file2);
450 perror("create link ");
454 /*Dealing with symlink */
455 else if (preserveMountPoints && (code = isMountPoint(file1, &blob))) {
457 * --------------------- Copy mount point --------------------
461 perror("checking for mount point ");
465 printf("Level %d: Mount point %s to %s\n", level, file1, file2);
469 /* Don't ovewrite a write protected directory (unless force: -f) */
470 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
472 "Target %s is write protected against its owner; not changed\n",
478 printf(" Copy mount point %s for vol %s to %s\n", file1,
483 unlink(file2); /* Always make the new link (it was easier) */
485 strcat(blob.out, "."); /* stupid convention; these end with a period */
486 code = symlink(blob.out, file2);
488 fprintf(stderr, "Could not create mount point %s for vol %s\n",
490 perror("create mount point ");
495 /*Dealing with mount point */
496 else if (((s1.st_mode & S_IFMT) == S_IFDIR)
497 && (recursive || (level == 0))) {
499 * ----------------------- Copy directory -----------------------
505 char f1[MAXPATHLEN], f2[MAXPATHLEN];
508 struct timeval tv[2];
511 printf("Level %d: Directory %s to %s\n", level, file1, file2);
515 /* Don't ovewrite a write protected directory (unless force: -f) */
516 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
518 "Directory %s is write protected against its owner; not changed\n",
523 strlcpy(f1, file1, sizeof f1);
524 strlcpy(f2, file2, sizeof f2);
525 p1 = f1 + strlen(f1);
526 p2 = f2 + strlen(f2);
527 if (p1 == f1 || p1[-1] != '/')
529 if (p2 == f2 || p2[-1] != '/')
532 dir = opendir(file1);
534 fprintf(stderr, "Couldn't open %s\n", file1);
538 while ((d = readdir(dir)) != NULL) {
539 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
541 strlcpy(p1, d->d_name, sizeof f1 - (p1 - f1));
542 strlcpy(p2, d->d_name, sizeof f2 - (p2 - f2));
543 code = Copy(f1, f2, recursive, level + 1);
545 rcode = 1; /* remember errors */
551 printf("Level %d: Copied directory %s to %s\n", level, file1,
556 mkdir(file2, 0777); /* Handle case where MakeParent not invoked. */
559 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
562 code = chown(file2, s1.st_uid, -1);
564 fprintf(stderr, "Unable to set owner-id for %s to %d\n", file2,
567 s1.st_mode &= ~04000; /* Don't set suid bit */
571 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
574 code = chown(file2, -1, s1.st_gid);
576 fprintf(stderr, "Unable to set group-id for %s to %d\n", file2,
579 s1.st_mode &= ~02000; /* Don't set sgid bit */
583 printf(" Set mode-bit for %s to %o\n", file2,
584 (s1.st_mode & 07777));
587 code = chmod(file2, s1.st_mode);
589 fprintf(stderr, "Unable to set mode-bits for %s to %d\n", file2,
597 printf(" Set acls for %s\n", file2);
604 blob.out_size = MAXACL;
607 /* Get an old-style ACL and convert it */
609 printf(" Getting old style acl\n");
613 for (i = 1; i < strlen(file1); i++)
616 strlcpy(aclspace, &file1[i], sizeof aclspace);
618 blob.in_size = 1 + strlen(aclspace);
619 tfd = open(file1, O_RDONLY, 0);
621 perror("old-acl open ");
624 code = ioctl(tfd, _VICEIOCTL(4), &blob);
627 if (errno == EINVAL) {
630 printf(" _VICEIOCTL(4) returns EINVAL\n");
637 /* Now convert the thing. */
638 oacl = (struct OldAcl *)(aclspace + 4);
639 sprintf(tacl, "%d\n%d\n", oacl->nplus, oacl->nminus);
640 strlcat(tacl, oacl->data, sizeof tacl);
641 strlcpy(aclspace, tacl, sizeof aclspace);
642 } /*Grab and convert old-style ACL */
644 /* Get a new-style ACL */
646 printf(" Getting new style acl\n");
650 code = pioctl(file1, _VICEIOCTL(2), &blob, 1);
652 if (errno == EINVAL) {
655 printf(" _VICEIOCTL(2) returns EINVAL\n");
663 } /*Grab new-style ACL */
666 * Now, set the new-style ACL.
670 printf(" Setting new style acl\n");
676 blob.in_size = 1 + strlen(aclspace);
677 code = pioctl(file2, _VICEIOCTL(1), &blob, 1);
679 if (errno == EINVAL) {
682 printf(" _VICEIOCTL(1) returns EINVAL\n");
686 fprintf(stderr, "Couldn't set acls for %s\n", file2);
693 printf("Not setting acls\n");
697 /* preserve access and modification times: ("-x" disables) */
699 tv[0].tv_sec = s1.st_atime;
701 tv[1].tv_sec = s1.st_mtime;
712 isMountPoint(char *name, struct ViceIoctl *blob)
715 char true_name[1024]; /*dirname */
716 char parent_dir[1024]; /*Parent directory of true name */
717 char *last_component; /*Last component of true name */
719 sprintf(true_name, "%s%s", (name[0] == '/') ? "" : "./", name);
722 * Find rightmost slash, if any.
724 last_component = (char *)strrchr(true_name, '/');
725 if (last_component) {
727 * Found it. Designate everything before it as the parent directory,
728 * everything after it as the final component.
730 strncpy(parent_dir, true_name, last_component - true_name);
731 parent_dir[last_component - true_name] = 0;
732 last_component++; /*Skip the slash */
735 * No slash appears in the given file name. Set parent_dir to the current
736 * directory, and the last component as the given name.
738 strlcpy(parent_dir, ".", sizeof parent_dir);
739 last_component = true_name;
742 if (strcmp(last_component, ".") == 0 || strcmp(last_component, "..") == 0) {
744 "up: you may not use '.' or '..' as the last component\n");
745 fprintf(stderr, "up: of a name in the 'up' command.\n");
749 blob->in = last_component;
750 blob->in_size = strlen(last_component) + 1;
751 blob->out_size = MAXSIZE;
753 memset(space, 0, MAXSIZE);
755 code = pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, blob, 0);
758 printf("'%s' is a mount point for volume '%s'\n", name, space);
762 if (errno == EINVAL) {
763 /* printf( "'%s' is not a mount point.\n", name);
768 fprintf(stderr, "problem examining '%s' in '%s'.\n",
769 last_component, parent_dir);
771 /* Die(errno, (ti->data ? ti->data : parent_dir));