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 */
25 #include <afs/afs_args.h>
26 #include <sys/param.h>
36 #include <sys/ioctl.h>
40 #include <sys/ioctl.h>
42 #ifdef HAVE_NETINET_IN_H
43 #include <netinet/in.h>
49 #include <afs/venus.h>
50 #include <afs/sys_prototypes.h>
52 /* ************************************************************* */
57 short renameTargets = 0;
59 short preserveDate = 1;
60 short preserveMountPoints = 0;
61 short forceOverwrite = 0;
66 char file1[MAXPATHLEN], file2[MAXPATHLEN];
69 static char space[MAXSIZE];
78 static void ScanArgs(int argc, char *argv[]);
79 static short MakeParent(char *file, afs_int32 owner);
80 static int Copy(char *file1, char *file2, short recursive, int level);
81 static int isMountPoint(char *name, struct ViceIoctl *blob);
84 /* ************************************************************ */
88 /* ************************************************************ */
90 #include "AFS_component_version_number.c"
93 main(int argc, char *argv[])
97 * The following signal action for AIX is necessary so that in case of a
98 * crash (i.e. core is generated) we can include the user's data section
99 * in the core dump. Unfortunately, by default, only a partial core is
100 * generated which, in many cases, isn't too useful.
102 struct sigaction nsa;
104 sigemptyset(&nsa.sa_mask);
105 nsa.sa_handler = SIG_DFL;
106 nsa.sa_flags = SA_FULLDUMP;
107 sigaction(SIGSEGV, &nsa, NULL);
109 #if !defined (AFS_AIX_ENV) && !defined (AFS_HPUX_ENV)
110 pageSize = getpagesize();
112 ScanArgs(argc, argv);
114 /* now read each line of the CopyList */
115 if (Copy(file1, file2, !oneLevel, 0))
116 return(1); /* some type of failure */
122 #define USAGE "usage: up [-v1frxm] from to\n"
124 ScanArgs(int argc, char *argv[])
126 /* skip program name */
129 /* check for -flag options */
130 while (argc > 0 && *argv[0] == '-') {
155 preserveMountPoints = 1;
159 fprintf(stderr, "Unknown option: '%c'\n", *cp);
160 fprintf(stderr, USAGE);
167 fprintf(stderr, USAGE);
171 strncpy(file1, argv[0], MAXPATHLEN);
172 strncpy(file2, argv[1], MAXPATHLEN);
180 * Make sure the parent directory of this file exists. Returns
181 * 1 if it exists, 0 otherwise. Note: the owner argument
182 * is a hack. All directories made will have this owner.
185 MakeParent(char *file, afs_int32 owner)
187 char parent[MAXPATHLEN];
191 strlcpy(parent, file, sizeof parent);
193 p = strrchr(parent, '/');
195 strlcpy(parent, ".", sizeof parent);
196 } else if (p > parent) {
202 if (stat(parent, &s) < 0) {
203 if (!MakeParent(parent, owner))
207 printf("Creating directory %s\n", parent);
212 chown(parent, owner, -1);
220 * This does the bulk of the work of the program. Handle one file,
221 * possibly copying subfiles if this is a directory
224 Copy(char *file1, char *file2, short recursive, int level)
226 struct stat s1, s2; /*Stat blocks */
227 struct ViceIoctl blob;
228 char aclspace[MAXACL];
229 afs_int32 rcode = 0, code;
232 code = lstat(file1, &s1);
234 fprintf(stderr, "Can't find %s\n", file1);
238 code = lstat(file2, &s2);
240 if (!MakeParent(file2, s1.st_uid))
245 if ((s1.st_mode & S_IFMT) == S_IFREG) {
247 * -------------------- Copy regular file --------------------
250 char buf[4096]; /* Must be bigger than sizeof (*head) */
251 struct timeval tv[2];
252 char tmpfile[MAXPATHLEN], newName[MAXPATHLEN];
255 printf("Level %d: File %s to %s\n", level, file1, file2);
259 /* Wonder if there is a security hole */
260 if (((s1.st_mode & 04002) == 04002) || ((s1.st_mode & 04020) == 04020)
261 || ((s1.st_mode & 02002) == 02002)) {
263 "WARNING: Mode-bits security hole in files %s and %s\n",
267 if (!goods2 || (s1.st_mtime != s2.st_mtime) || (s1.st_size != s2.st_size)) { /*c */
268 /* Don't ovewrite a write protected file (unless force: -f) */
269 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
271 "File %s is write protected against its owner; not changed\n",
277 printf(" Copy file %s to %s (%u Bytes)\n", file1, file2,
278 (unsigned int) s1.st_size);
282 strlcpy(tmpfile, file2, sizeof tmpfile); /* Name of temporary file */
283 strlcat(tmpfile, ".UPD", sizeof tmpfile);
285 /* open file1 for input */
286 f1 = open(file1, O_RDONLY);
288 fprintf(stderr, "Unable to open input file %s: %s\n",
289 file1, strerror(errno));
293 /* open temporary output file */
294 f2 = open(tmpfile, (O_WRONLY | O_CREAT | O_TRUNC), s1.st_mode);
296 fprintf(stderr, "Unable to open output file %s: %s\n",
297 tmpfile, strerror(errno));
303 /* Copy file1 to temporary file */
304 while ((n = read(f1, buf, sizeof(buf))) > 0) {
305 if (write(f2, buf, n) != n) {
307 "Write failed, file %s must be copied again.\n",
312 /* preserve access and modification times: ("-x" disables) */
314 tv[0].tv_sec = s1.st_atime;
316 tv[1].tv_sec = s1.st_mtime;
321 /* Close the files */
329 /* Rename file2 to file2.old. [-r] */
330 if (renameTargets && goods2) {
331 strlcpy(newName, file2, sizeof newName);
332 strlcat(newName, ".old", sizeof newName);
334 printf(" Renaming %s to %s\n", file2, newName);
337 if (rename(file2, newName) < 0) {
338 fprintf(stderr, "Rename of %s to %s failed.\n", file2,
343 /* Rename temporary file to file2 */
344 code = rename(tmpfile, file2);
346 fprintf(stderr, "Rename of %s to %s failed.\n", tmpfile,
351 /* Re-stat file2 and compare file sizes */
352 code = lstat(file2, &s2);
354 fprintf(stderr, "WARNING: Unable to stat new file %s\n",
358 if (s1.st_size != s2.st_size) {
360 "WARNING: New file %s is %u bytes long; should be %u\n",
361 file2, (unsigned int) s2.st_size,
362 (unsigned int) s1.st_size);
367 /* Set the user-id */
368 if (s2.st_uid != s1.st_uid) {
370 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
373 code = chown(file2, s1.st_uid, -1);
375 fprintf(stderr, "Unable to set owner-id for %s to %d\n",
379 s1.st_mode &= ~04000; /* Don't set suid bit */
383 /* Set the group-id */
384 if (s2.st_gid != s1.st_gid) {
386 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
389 code = chown(file2, -1, s1.st_gid);
391 fprintf(stderr, "Unable to set group-id for %s to %d\n",
395 s1.st_mode &= ~02000; /* Don't set sgid bit */
399 /* Set the mode bits */
400 if (s1.st_mode != s2.st_mode) {
402 printf(" Set mode-bit for %s to %o\n", file2,
403 (s1.st_mode & 07777));
406 code = chmod(file2, s1.st_mode);
408 fprintf(stderr, "Unable to set mode-bits for %s to %d\n",
415 else if ((s1.st_mode & S_IFMT) == S_IFLNK) {
417 * --------------------- Copy symlink --------------------
419 char linkvalue[MAXPATHLEN + 1];
423 printf("Level %d: Symbolic link %s to %s\n", level, file1, file2);
427 /* Don't ovewrite a write protected directory (unless force: -f) */
428 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
430 "Link %s is write protected against its owner; not changed\n",
436 printf(" Copy symbolic link %s->%s to %s\n", file1, linkvalue,
441 n = readlink(file1, linkvalue, sizeof(linkvalue));
443 fprintf(stderr, "Could not read symbolic link %s\n", file1);
444 perror("read link ");
449 unlink(file2); /* Always make the new link (it was easier) */
451 code = symlink(linkvalue, file2);
453 fprintf(stderr, "Could not create symbolic link %s\n", file2);
454 perror("create link ");
458 /*Dealing with symlink */
459 else if (preserveMountPoints && (code = isMountPoint(file1, &blob))) {
461 * --------------------- Copy mount point --------------------
465 perror("checking for mount point ");
469 printf("Level %d: Mount point %s to %s\n", level, file1, file2);
473 /* Don't ovewrite a write protected directory (unless force: -f) */
474 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
476 "Target %s is write protected against its owner; not changed\n",
482 printf(" Copy mount point %s for vol %s to %s\n", file1,
487 unlink(file2); /* Always make the new link (it was easier) */
489 strcat(blob.out, "."); /* stupid convention; these end with a period */
490 code = symlink(blob.out, file2);
492 fprintf(stderr, "Could not create mount point %s for vol %s\n",
494 perror("create mount point ");
499 /*Dealing with mount point */
500 else if (((s1.st_mode & S_IFMT) == S_IFDIR)
501 && (recursive || (level == 0))) {
503 * ----------------------- Copy directory -----------------------
509 char f1[MAXPATHLEN], f2[MAXPATHLEN];
512 struct timeval tv[2];
515 printf("Level %d: Directory %s to %s\n", level, file1, file2);
519 /* Don't ovewrite a write protected directory (unless force: -f) */
520 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
522 "Directory %s is write protected against its owner; not changed\n",
527 strlcpy(f1, file1, sizeof f1);
528 strlcpy(f2, file2, sizeof f2);
529 p1 = f1 + strlen(f1);
530 p2 = f2 + strlen(f2);
531 if (p1 == f1 || p1[-1] != '/')
533 if (p2 == f2 || p2[-1] != '/')
536 dir = opendir(file1);
538 fprintf(stderr, "Couldn't open %s\n", file1);
542 while ((d = readdir(dir)) != NULL) {
543 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
545 strlcpy(p1, d->d_name, sizeof f1 - (p1 - f1));
546 strlcpy(p2, d->d_name, sizeof f2 - (p2 - f2));
547 code = Copy(f1, f2, recursive, level + 1);
549 rcode = 1; /* remember errors */
555 printf("Level %d: Copied directory %s to %s\n", level, file1,
560 mkdir(file2, 0777); /* Handle case where MakeParent not invoked. */
563 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
566 code = chown(file2, s1.st_uid, -1);
568 fprintf(stderr, "Unable to set owner-id for %s to %d\n", file2,
571 s1.st_mode &= ~04000; /* Don't set suid bit */
575 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
578 code = chown(file2, -1, s1.st_gid);
580 fprintf(stderr, "Unable to set group-id for %s to %d\n", file2,
583 s1.st_mode &= ~02000; /* Don't set sgid bit */
587 printf(" Set mode-bit for %s to %o\n", file2,
588 (s1.st_mode & 07777));
591 code = chmod(file2, s1.st_mode);
593 fprintf(stderr, "Unable to set mode-bits for %s to %d\n", file2,
601 printf(" Set acls for %s\n", file2);
608 blob.out_size = MAXACL;
611 /* Get an old-style ACL and convert it */
613 printf(" Getting old style acl\n");
617 for (i = 1; i < strlen(file1); i++)
620 strlcpy(aclspace, &file1[i], sizeof aclspace);
622 blob.in_size = 1 + strlen(aclspace);
623 tfd = open(file1, O_RDONLY, 0);
625 perror("old-acl open ");
628 code = ioctl(tfd, _VICEIOCTL(4), &blob);
631 if (errno == EINVAL) {
634 printf(" _VICEIOCTL(4) returns EINVAL\n");
641 /* Now convert the thing. */
642 oacl = (struct OldAcl *)(aclspace + 4);
643 sprintf(tacl, "%d\n%d\n", oacl->nplus, oacl->nminus);
644 strlcat(tacl, oacl->data, sizeof tacl);
645 strlcpy(aclspace, tacl, sizeof aclspace);
646 } /*Grab and convert old-style ACL */
648 /* Get a new-style ACL */
650 printf(" Getting new style acl\n");
654 code = pioctl(file1, _VICEIOCTL(2), &blob, 1);
656 if (errno == EINVAL) {
659 printf(" _VICEIOCTL(2) returns EINVAL\n");
667 } /*Grab new-style ACL */
670 * Now, set the new-style ACL.
674 printf(" Setting new style acl\n");
680 blob.in_size = 1 + strlen(aclspace);
681 code = pioctl(file2, _VICEIOCTL(1), &blob, 1);
683 if (errno == EINVAL) {
686 printf(" _VICEIOCTL(1) returns EINVAL\n");
690 fprintf(stderr, "Couldn't set acls for %s\n", file2);
697 printf("Not setting acls\n");
701 /* preserve access and modification times: ("-x" disables) */
703 tv[0].tv_sec = s1.st_atime;
705 tv[1].tv_sec = s1.st_mtime;
716 isMountPoint(char *name, struct ViceIoctl *blob)
719 char true_name[1024]; /*dirname */
720 char parent_dir[1024]; /*Parent directory of true name */
721 char *last_component; /*Last component of true name */
723 sprintf(true_name, "%s%s", (name[0] == '/') ? "" : "./", name);
726 * Find rightmost slash, if any.
728 last_component = (char *)strrchr(true_name, '/');
729 if (last_component) {
731 * Found it. Designate everything before it as the parent directory,
732 * everything after it as the final component.
734 strncpy(parent_dir, true_name, last_component - true_name);
735 parent_dir[last_component - true_name] = 0;
736 last_component++; /*Skip the slash */
739 * No slash appears in the given file name. Set parent_dir to the current
740 * directory, and the last component as the given name.
742 strlcpy(parent_dir, ".", sizeof parent_dir);
743 last_component = true_name;
746 if (strcmp(last_component, ".") == 0 || strcmp(last_component, "..") == 0) {
748 "up: you may not use '.' or '..' as the last component\n");
749 fprintf(stderr, "up: of a name in the 'up' command.\n");
753 blob->in = last_component;
754 blob->in_size = strlen(last_component) + 1;
755 blob->out_size = MAXSIZE;
757 memset(space, 0, MAXSIZE);
759 code = pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, blob, 0);
762 printf("'%s' is a mount point for volume '%s'\n", name, space);
766 if (errno == EINVAL) {
767 /* printf( "'%s' is not a mount point.\n", name);
772 fprintf(stderr, "problem examining '%s' in '%s'.\n",
773 last_component, parent_dir);
775 /* Die(errno, (ti->data ? ti->data : parent_dir));