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>
22 #include <afs/afs_args.h>
23 #include <sys/param.h>
33 #include <sys/ioctl.h>
37 #include <sys/ioctl.h>
39 #ifdef HAVE_NETINET_IN_H
40 #include <netinet/in.h>
46 #include <afs/venus.h>
47 #include <afs/sys_prototypes.h>
48 #include <afs/afsutil.h>
49 #include <afs/afs_consts.h>
51 /* ************************************************************* */
56 short renameTargets = 0;
58 short preserveDate = 1;
59 short preserveMountPoints = 0;
60 short forceOverwrite = 0;
64 char file1[MAXPATHLEN], file2[MAXPATHLEN];
66 static char space[AFS_PIOCTL_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 ScanArgs(argc, argv);
108 /* now read each line of the CopyList */
109 if (Copy(file1, file2, !oneLevel, 0))
110 return(1); /* some type of failure */
116 #define USAGE "usage: up [-v1frxm] from to\n"
118 ScanArgs(int argc, char *argv[])
120 /* skip program name */
123 /* check for -flag options */
124 while (argc > 0 && *argv[0] == '-') {
127 if (strlen(cp) > 2) {
153 preserveMountPoints = 1;
160 fprintf(stderr, "Unknown option: '%s'\n", cp);
161 fprintf(stderr, USAGE);
168 fprintf(stderr, USAGE);
172 strncpy(file1, argv[0], MAXPATHLEN);
173 strncpy(file2, argv[1], MAXPATHLEN);
181 * Make sure the parent directory of this file exists. Returns
182 * 1 if it exists, 0 otherwise. Note: the owner argument
183 * is a hack. All directories made will have this owner.
186 MakeParent(char *file, afs_int32 owner)
188 char parent[MAXPATHLEN];
192 strlcpy(parent, file, sizeof parent);
194 p = strrchr(parent, '/');
196 strlcpy(parent, ".", sizeof parent);
197 } else if (p > parent) {
203 if (stat(parent, &s) < 0) {
204 if (!MakeParent(parent, owner))
208 printf("Creating directory %s\n", parent);
213 chown(parent, owner, -1);
221 * This does the bulk of the work of the program. Handle one file,
222 * possibly copying subfiles if this is a directory
225 Copy(char *file1, char *file2, short recursive, int level)
227 struct stat s1, s2; /*Stat blocks */
228 struct ViceIoctl blob;
229 char aclspace[MAXACL];
230 afs_int32 rcode = 0, code;
233 code = lstat(file1, &s1);
235 fprintf(stderr, "Can't find %s\n", file1);
239 code = lstat(file2, &s2);
241 if (!MakeParent(file2, s1.st_uid))
246 if ((s1.st_mode & S_IFMT) == S_IFREG) {
248 * -------------------- Copy regular file --------------------
251 char buf[4096]; /* Must be bigger than sizeof (*head) */
252 struct timeval tv[2];
253 char tmpfile[MAXPATHLEN], newName[MAXPATHLEN];
256 printf("Level %d: File %s to %s\n", level, file1, file2);
260 /* Wonder if there is a security hole */
261 if (((s1.st_mode & 04002) == 04002) || ((s1.st_mode & 04020) == 04020)
262 || ((s1.st_mode & 02002) == 02002)) {
264 "WARNING: Mode-bits security hole in files %s and %s\n",
268 if (!goods2 || (s1.st_mtime != s2.st_mtime) || (s1.st_size != s2.st_size)) { /*c */
269 /* Don't ovewrite a write protected file (unless force: -f) */
270 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
272 "File %s is write protected against its owner; not changed\n",
278 printf(" Copy file %s to %s (%u Bytes)\n", file1, file2,
279 (unsigned int) s1.st_size);
283 strlcpy(tmpfile, file2, sizeof tmpfile); /* Name of temporary file */
284 strlcat(tmpfile, ".UPD", sizeof tmpfile);
286 /* open file1 for input */
287 f1 = open(file1, O_RDONLY);
289 fprintf(stderr, "Unable to open input file %s: %s\n",
290 file1, strerror(errno));
294 /* open temporary output file */
295 f2 = open(tmpfile, (O_WRONLY | O_CREAT | O_TRUNC), s1.st_mode);
297 fprintf(stderr, "Unable to open output file %s: %s\n",
298 tmpfile, strerror(errno));
304 /* Copy file1 to temporary file */
305 while ((n = read(f1, buf, sizeof(buf))) > 0) {
306 if (write(f2, buf, n) != n) {
308 "Write failed, file %s must be copied again.\n",
313 /* preserve access and modification times: ("-x" disables) */
315 tv[0].tv_sec = s1.st_atime;
317 tv[1].tv_sec = s1.st_mtime;
322 /* Close the files */
330 /* Rename file2 to file2.old. [-r] */
331 if (renameTargets && goods2) {
332 strlcpy(newName, file2, sizeof newName);
333 strlcat(newName, ".old", sizeof newName);
335 printf(" Renaming %s to %s\n", file2, newName);
338 if (rename(file2, newName) < 0) {
339 fprintf(stderr, "Rename of %s to %s failed.\n", file2,
344 /* Rename temporary file to file2 */
345 code = rename(tmpfile, file2);
347 fprintf(stderr, "Rename of %s to %s failed.\n", tmpfile,
352 /* Re-stat file2 and compare file sizes */
353 code = lstat(file2, &s2);
355 fprintf(stderr, "WARNING: Unable to stat new file %s\n",
359 if (s1.st_size != s2.st_size) {
361 "WARNING: New file %s is %u bytes long; should be %u\n",
362 file2, (unsigned int) s2.st_size,
363 (unsigned int) s1.st_size);
368 /* Set the user-id */
369 if (s2.st_uid != s1.st_uid) {
371 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
374 code = chown(file2, s1.st_uid, -1);
376 fprintf(stderr, "Unable to set owner-id for %s to %d\n",
380 s1.st_mode &= ~04000; /* Don't set suid bit */
384 /* Set the group-id */
385 if (s2.st_gid != s1.st_gid) {
387 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
390 code = chown(file2, -1, s1.st_gid);
392 fprintf(stderr, "Unable to set group-id for %s to %d\n",
396 s1.st_mode &= ~02000; /* Don't set sgid bit */
400 /* Set the mode bits */
401 if (s1.st_mode != s2.st_mode) {
403 printf(" Set mode-bit for %s to %o\n", file2,
404 (s1.st_mode & 07777));
407 code = chmod(file2, s1.st_mode);
409 fprintf(stderr, "Unable to set mode-bits for %s to %d\n",
416 else if ((s1.st_mode & S_IFMT) == S_IFLNK) {
418 * --------------------- Copy symlink --------------------
420 char linkvalue[MAXPATHLEN + 1];
424 printf("Level %d: Symbolic link %s to %s\n", level, file1, file2);
428 /* Don't ovewrite a write protected directory (unless force: -f) */
429 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
431 "Link %s is write protected against its owner; not changed\n",
437 printf(" Copy symbolic link %s->%s to %s\n", file1, linkvalue,
442 n = readlink(file1, linkvalue, sizeof(linkvalue));
444 fprintf(stderr, "Could not read symbolic link %s\n", file1);
445 perror("read link ");
450 unlink(file2); /* Always make the new link (it was easier) */
452 code = symlink(linkvalue, file2);
454 fprintf(stderr, "Could not create symbolic link %s\n", file2);
455 perror("create link ");
459 /*Dealing with symlink */
460 else if (preserveMountPoints && (code = isMountPoint(file1, &blob))) {
462 * --------------------- Copy mount point --------------------
466 perror("checking for mount point ");
470 printf("Level %d: Mount point %s to %s\n", level, file1, file2);
474 /* Don't ovewrite a write protected directory (unless force: -f) */
475 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
477 "Target %s is write protected against its owner; not changed\n",
483 printf(" Copy mount point %s for vol %s to %s\n", file1,
488 unlink(file2); /* Always make the new link (it was easier) */
490 strcat(blob.out, "."); /* stupid convention; these end with a period */
491 code = symlink(blob.out, file2);
493 fprintf(stderr, "Could not create mount point %s for vol %s\n",
495 perror("create mount point ");
500 /*Dealing with mount point */
501 else if (((s1.st_mode & S_IFMT) == S_IFDIR)
502 && (recursive || (level == 0))) {
504 * ----------------------- Copy directory -----------------------
510 char f1[MAXPATHLEN], f2[MAXPATHLEN];
513 struct timeval tv[2];
516 printf("Level %d: Directory %s to %s\n", level, file1, file2);
520 /* Don't ovewrite a write protected directory (unless force: -f) */
521 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
523 "Directory %s is write protected against its owner; not changed\n",
528 strlcpy(f1, file1, sizeof f1);
529 strlcpy(f2, file2, sizeof f2);
530 p1 = f1 + strlen(f1);
531 p2 = f2 + strlen(f2);
532 if (p1 == f1 || p1[-1] != '/')
534 if (p2 == f2 || p2[-1] != '/')
537 dir = opendir(file1);
539 fprintf(stderr, "Couldn't open %s\n", file1);
543 while ((d = readdir(dir)) != NULL) {
544 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
546 strlcpy(p1, d->d_name, sizeof f1 - (p1 - f1));
547 strlcpy(p2, d->d_name, sizeof f2 - (p2 - f2));
548 code = Copy(f1, f2, recursive, level + 1);
550 rcode = 1; /* remember errors */
556 printf("Level %d: Copied directory %s to %s\n", level, file1,
561 mkdir(file2, 0777); /* Handle case where MakeParent not invoked. */
564 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
567 code = chown(file2, s1.st_uid, -1);
569 fprintf(stderr, "Unable to set owner-id for %s to %d\n", file2,
572 s1.st_mode &= ~04000; /* Don't set suid bit */
576 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
579 code = chown(file2, -1, s1.st_gid);
581 fprintf(stderr, "Unable to set group-id for %s to %d\n", file2,
584 s1.st_mode &= ~02000; /* Don't set sgid bit */
588 printf(" Set mode-bit for %s to %o\n", file2,
589 (s1.st_mode & 07777));
592 code = chmod(file2, s1.st_mode);
594 fprintf(stderr, "Unable to set mode-bits for %s to %d\n", file2,
602 printf(" Set acls for %s\n", file2);
609 blob.out_size = MAXACL;
612 /* Get an old-style ACL and convert it */
614 printf(" Getting old style acl\n");
618 for (i = 1; i < strlen(file1); i++)
621 strlcpy(aclspace, &file1[i], sizeof aclspace);
623 blob.in_size = 1 + strlen(aclspace);
624 tfd = open(file1, O_RDONLY, 0);
626 perror("old-acl open ");
629 code = ioctl(tfd, _VICEIOCTL(4), &blob);
632 if (errno == EINVAL) {
635 printf(" _VICEIOCTL(4) returns EINVAL\n");
642 /* Now convert the thing. */
643 oacl = (struct OldAcl *)(aclspace + 4);
644 sprintf(tacl, "%d\n%d\n", oacl->nplus, oacl->nminus);
645 strlcat(tacl, oacl->data, sizeof tacl);
646 strlcpy(aclspace, tacl, sizeof aclspace);
647 } /*Grab and convert old-style ACL */
649 /* Get a new-style ACL */
651 printf(" Getting new style acl\n");
655 code = pioctl(file1, _VICEIOCTL(2), &blob, 1);
657 if (errno == EINVAL) {
660 printf(" _VICEIOCTL(2) returns EINVAL\n");
668 } /*Grab new-style ACL */
671 * Now, set the new-style ACL.
675 printf(" Setting new style acl\n");
681 blob.in_size = 1 + strlen(aclspace);
682 code = pioctl(file2, _VICEIOCTL(1), &blob, 1);
684 if (errno == EINVAL) {
687 printf(" _VICEIOCTL(1) returns EINVAL\n");
691 fprintf(stderr, "Couldn't set acls for %s\n", file2);
698 printf("Not setting acls\n");
702 /* preserve access and modification times: ("-x" disables) */
704 tv[0].tv_sec = s1.st_atime;
706 tv[1].tv_sec = s1.st_mtime;
717 isMountPoint(char *name, struct ViceIoctl *blob)
720 char true_name[1024]; /*dirname */
721 char parent_dir[1024]; /*Parent directory of true name */
722 char *last_component; /*Last component of true name */
724 sprintf(true_name, "%s%s", (name[0] == '/') ? "" : "./", name);
727 * Find rightmost slash, if any.
729 last_component = (char *)strrchr(true_name, '/');
730 if (last_component) {
732 * Found it. Designate everything before it as the parent directory,
733 * everything after it as the final component.
735 strncpy(parent_dir, true_name, last_component - true_name);
736 parent_dir[last_component - true_name] = 0;
737 last_component++; /*Skip the slash */
740 * No slash appears in the given file name. Set parent_dir to the current
741 * directory, and the last component as the given name.
743 strlcpy(parent_dir, ".", sizeof parent_dir);
744 last_component = true_name;
747 if (strcmp(last_component, ".") == 0 || strcmp(last_component, "..") == 0) {
749 "up: you may not use '.' or '..' as the last component\n");
750 fprintf(stderr, "up: of a name in the 'up' command.\n");
754 blob->in = last_component;
755 blob->in_size = strlen(last_component) + 1;
756 blob->out_size = AFS_PIOCTL_MAXSIZE;
758 memset(space, 0, AFS_PIOCTL_MAXSIZE);
760 code = pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, blob, 0);
763 printf("'%s' is a mount point for volume '%s'\n", name, space);
767 if (errno == EINVAL) {
768 /* printf( "'%s' is not a mount point.\n", name);
773 fprintf(stderr, "problem examining '%s' in '%s'.\n",
774 last_component, parent_dir);
776 /* Die(errno, (ti->data ? ti->data : parent_dir));