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 /* missing type from C language */
26 #include <afs/afs_args.h>
27 #include <sys/param.h>
37 #include <sys/ioctl.h>
41 #include <sys/ioctl.h>
43 #ifdef HAVE_NETINET_IN_H
44 #include <netinet/in.h>
55 #include <afs/venus.h>
57 /* ************************************************************* */
61 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
63 extern char *sys_errlist[];
66 Boolean verbose = false;
67 Boolean renameTargets = false;
68 Boolean oneLevel = false;
69 Boolean preserveDate = true;
70 Boolean preserveMountPoints = false;
71 Boolean forceOverwrite = false;
74 Boolean setacl = true;
75 Boolean oldAcl = false;
76 char file1[MAXPATHLEN], file2[MAXPATHLEN];
79 static char space[MAXSIZE];
88 /* ************************************************************ */
92 /* ************************************************************ */
94 #include "AFS_component_version_number.c"
103 * The following signal action for AIX is necessary so that in case of a
104 * crash (i.e. core is generated) we can include the user's data section
105 * in the core dump. Unfortunately, by default, only a partial core is
106 * generated which, in many cases, isn't too useful.
108 struct sigaction nsa;
110 sigemptyset(&nsa.sa_mask);
111 nsa.sa_handler = SIG_DFL;
112 nsa.sa_flags = SA_FULLDUMP;
113 sigaction(SIGSEGV, &nsa, NULL);
115 #if !defined (AFS_AIX_ENV) && !defined (AFS_HPUX_ENV)
116 pageSize = getpagesize();
118 ScanArgs(argc, argv);
120 /* now read each line of the CopyList */
121 if (Copy(file1, file2, !oneLevel, 0))
122 exit(1); /* some type of failure */
133 /* skip program name */
136 /* check for -flag options */
137 while (argc > 0 && *argv[0] == '-') {
150 renameTargets = true;
154 forceOverwrite = true;
158 preserveDate = false;
162 preserveMountPoints = true;
166 fprintf(stderr, "Unknown option: '%c'\n", *cp);
167 fprintf(stderr, "usage: up [-v1frxm] from to\n");
174 fprintf(stderr, "usage: up [-v1frx] from to\n");
178 strncpy(file1, argv[0], MAXPATHLEN);
179 strncpy(file2, argv[1], MAXPATHLEN);
187 * Make sure the parent directory of this file exists. Returns
188 * true if it exists, false otherwise. Note: the owner argument
189 * is a hack. All directories made will have this owner.
191 Boolean MakeParent(file, owner)
195 char parent[MAXPATHLEN];
199 strcpy(parent, file);
201 p = strrchr(parent, '/');
205 else if (p > parent) {
212 if (stat(parent, &s) < 0) {
213 if (!MakeParent(parent, owner))
217 printf("Creating directory %s\n", parent);
222 chown(parent, owner, -1);
230 * This does the bulk of the work of the program. Handle one file,
231 * possibly copying subfiles if this is a directory
233 Copy(file1, file2, recursive, level)
234 char *file1; /* input file name */
235 char *file2; /* output file name */
236 Boolean recursive; /* true if directory should be copied */
237 int level; /* level of recursion: 0, 1, ... */
240 struct stat s1, s2; /*Stat blocks*/
241 struct ViceIoctl blob;
242 char aclspace[MAXACL];
243 afs_int32 rcode = 0, code;
246 code = lstat(file1, &s1);
248 fprintf(stderr,"Can't find %s\n",file1);
252 code = lstat(file2, &s2);
254 if (!MakeParent(file2,s1.st_uid))
259 if ((s1.st_mode & S_IFMT) == S_IFREG) {
261 * -------------------- Copy regular file --------------------
264 char buf[4096]; /* Must be bigger than sizeof (*head) */
265 struct timeval tv[2];
266 char tmpfile[MAXPATHLEN], newName[MAXPATHLEN];
269 printf("Level %d: File %s to %s\n", level, file1, file2);
273 /* Wonder if there is a security hole */
274 if ( ((s1.st_mode & 04002) == 04002) ||
275 ((s1.st_mode & 04020) == 04020) ||
276 ((s1.st_mode & 02002) == 02002) ) {
277 fprintf(stderr, "WARNING: Mode-bits security hole in files %s and %s\n",
281 if (!goods2 || (s1.st_mtime != s2.st_mtime) || (s1.st_size != s2.st_size)) { /*c*/
282 /* Don't ovewrite a write protected file (unless force: -f) */
283 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
285 "File %s is write protected against its owner; not changed\n",
291 printf(" Copy file %s to %s (%u Bytes)\n", file1, file2, s1.st_size);
295 strcpy(tmpfile, file2); /* Name of temporary file */
296 strcat(tmpfile,".UPD");
298 /* open file1 for input */
299 f1 = open(file1, O_RDONLY);
301 fprintf(stderr, "Unable to open input file %s, ", file1);
302 if (errno >= sys_nerr)
303 fprintf(stderr, "error code = %d\n", errno);
305 fprintf(stderr, "%s\n", sys_errlist[errno]);
309 /* open temporary output file */
310 f2 = open(tmpfile, (O_WRONLY | O_CREAT | O_TRUNC), s1.st_mode);
312 fprintf(stderr, "Unable to open output file %s, ", tmpfile);
313 if (errno >= sys_nerr)
314 fprintf(stderr, "error code = %d\n", errno);
316 fprintf(stderr, "%s\n", sys_errlist[errno]);
322 /* Copy file1 to temporary file */
323 while ((n = read(f1, buf, sizeof(buf))) > 0) {
324 if (write(f2, buf, n) != n) {
325 fprintf(stderr,"Write failed, file %s must be copied again.\n", file2);
329 /* preserve access and modification times: ("-x" disables)*/
331 tv[0].tv_sec = s1.st_atime;
333 tv[1].tv_sec = s1.st_mtime;
338 /* Close the files */
346 /* Rename file2 to file2.old. [-r] */
347 if (renameTargets && goods2) {
348 strcpy(newName, file2);
349 strcat(newName, ".old");
351 printf(" Renaming %s to %s\n", file2, newName);
354 if (rename(file2, newName) < 0) {
355 fprintf(stderr, "Rename of %s to %s failed.\n", file2, newName);
359 /* Rename temporary file to file2 */
360 code = rename(tmpfile, file2);
362 fprintf(stderr, "Rename of %s to %s failed.\n", tmpfile, file2);
366 /* Re-stat file2 and compare file sizes */
367 code = lstat(file2, &s2);
369 fprintf(stderr, "WARNING: Unable to stat new file %s\n", file2);
372 if (s1.st_size != s2.st_size) {
373 fprintf(stderr, "WARNING: New file %s is %u bytes long; should be %u\n",
374 file2, s2.st_size, s1.st_size);
378 /* Set the user-id */
379 if (s2.st_uid != s1.st_uid) {
381 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
384 code = chown(file2, s1.st_uid, -1);
386 fprintf(stderr, "Unable to set owner-id for %s to %d\n", file2, s1.st_uid);
389 s1.st_mode &= ~04000; /* Don't set suid bit */
393 /* Set the group-id */
394 if (s2.st_gid != s1.st_gid) {
396 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
399 code = chown(file2, -1, s1.st_gid);
401 fprintf(stderr, "Unable to set group-id for %s to %d\n", file2, s1.st_gid);
404 s1.st_mode &= ~02000; /* Don't set sgid bit */
408 /* Set the mode bits */
409 if (s1.st_mode != s2.st_mode) {
411 printf(" Set mode-bit for %s to %o\n", file2, (s1.st_mode & 07777));
414 code = chmod(file2, s1.st_mode);
416 fprintf(stderr, "Unable to set mode-bits for %s to %d\n", file2, s1.st_mode);
422 else if ((s1.st_mode & S_IFMT) == S_IFLNK) {
424 * --------------------- Copy symlink --------------------
426 char linkvalue[MAXPATHLEN+1];
430 printf("Level %d: Symbolic link %s to %s\n", level, file1, file2);
434 /* Don't ovewrite a write protected directory (unless force: -f) */
435 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
437 "Link %s is write protected against its owner; not changed\n",
443 printf(" Copy symbolic link %s->%s to %s\n", file1, linkvalue, file2);
447 n = readlink(file1, linkvalue, sizeof(linkvalue));
449 fprintf(stderr, "Could not read symbolic link %s\n", file1);
450 perror("read link ");
455 unlink(file2); /* Always make the new link (it was easier) */
457 code = symlink(linkvalue, file2);
459 fprintf(stderr, "Could not create symbolic link %s\n", file2);
460 perror("create link ");
463 } /*Dealing with symlink*/
465 else if ( preserveMountPoints && (code=isMountPoint( file1, &blob )) ) {
467 * --------------------- Copy mount point --------------------
471 perror("checking for mount point ");
475 printf("Level %d: Mount point %s to %s\n", level, file1, file2);
479 /* Don't ovewrite a write protected directory (unless force: -f) */
480 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
482 "Target %s is write protected against its owner; not changed\n",
488 printf(" Copy mount point %s for vol %s to %s\n", file1, blob.out, file2);
492 unlink(file2); /* Always make the new link (it was easier) */
494 strcat(blob.out, "."); /* stupid convention; these end with a period */
495 code = symlink(blob.out, file2);
497 fprintf(stderr, "Could not create mount point %s for vol %s\n", file2, blob.out);
498 perror("create mount point ");
502 } /*Dealing with mount point*/
504 else if (((s1.st_mode & S_IFMT) == S_IFDIR) && (recursive || (level == 0))) {
506 * ----------------------- Copy directory -----------------------
512 char f1[MAXPATHLEN], f2[MAXPATHLEN];
517 printf("Level %d: Directory %s to %s\n", level, file1, file2);
521 /* Don't ovewrite a write protected directory (unless force: -f) */
522 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
524 "Directory %s is write protected against its owner; not changed\n",
531 p1 = f1 + strlen(f1);
532 p2 = f2 + strlen(f2);
533 if (p1 == f1 || p1[-1] != '/')
535 if (p2 == f2 || p2[-1] != '/')
538 dir = opendir(file1);
540 fprintf(stderr, "Couldn't open %s\n", file1);
544 while ((d = readdir(dir)) != NULL) {
545 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
547 strcpy(p1, d->d_name);
548 strcpy(p2, d->d_name);
549 code = Copy(f1, f2, recursive, level + 1);
550 if (code && !rcode) rcode = 1; /* remember errors */
556 printf("Level %d: Copied directory %s to %s\n", level, file1, file2);
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, s1.st_uid);
570 s1.st_mode &= ~04000; /* Don't set suid bit */
574 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
577 code = chown(file2, -1, s1.st_gid);
579 fprintf(stderr, "Unable to set group-id for %s to %d\n", file2, s1.st_gid);
581 s1.st_mode &= ~02000; /* Don't set sgid bit */
585 printf(" Set mode-bit for %s to %o\n", file2, (s1.st_mode & 07777));
588 code = chmod(file2, s1.st_mode);
590 fprintf(stderr, "Unable to set mode-bits for %s to %d\n", file2, s1.st_mode);
595 if (setacl == true) {
597 printf(" Set acls for %s\n", file2);
604 blob.out_size = MAXACL;
607 /* Get an old-style ACL and convert it */
608 for (i=1; i<strlen(file1); i++)
609 if (file1[i] == '/') break;
610 strcpy(aclspace, &file1[i]);
612 blob.in_size = 1+strlen(aclspace);
613 tfd = open(file1, O_RDONLY, 0);
615 perror("old-acl open ");
618 code = ioctl(tfd, _VICEIOCTL(4), &blob);
621 if (errno == EINVAL) {
628 /* Now convert the thing. */
629 oacl = (struct OldAcl *) (aclspace+4);
630 sprintf(tacl, "%d\n%d\n", oacl->nplus, oacl->nminus);
631 strcat(tacl, oacl->data);
632 strcpy(aclspace, tacl);
633 } /*Grab and convert old-style ACL*/
635 /* Get a new-style ACL */
636 code = pioctl(file1, _VICEIOCTL(2), &blob, 1);
638 if (errno == EINVAL) {
646 } /*Grab new-style ACL*/
649 * Now, set the new-style ACL.
651 if (setacl == true) {
655 blob.in_size = 1+strlen(aclspace);
656 code = pioctl(file2, _VICEIOCTL(1), &blob, 1);
658 if (errno == EINVAL) {
662 fprintf(stderr, "Couldn't set acls for %s\n", file2);
668 if (setacl == false) {
669 printf("Not setting acls\n");
678 int isMountPoint( name, blob )
680 struct ViceIoctl *blob;
683 char true_name[1024]; /*dirname*/
684 char parent_dir[1024]; /*Parent directory of true name*/
685 char *last_component; /*Last component of true name*/
687 sprintf(true_name, "%s%s",
688 (name[0] == '/') ? "" : "./",
692 * Find rightmost slash, if any.
694 last_component = (char *) strrchr(true_name, '/');
695 if (last_component) {
697 * Found it. Designate everything before it as the parent directory,
698 * everything after it as the final component.
700 strncpy(parent_dir, true_name, last_component - true_name);
701 parent_dir[last_component - true_name] = 0;
702 last_component++; /*Skip the slash*/
706 * No slash appears in the given file name. Set parent_dir to the current
707 * directory, and the last component as the given name.
709 strcpy(parent_dir, ".");
710 last_component = true_name;
713 if (strcmp(last_component, ".") == 0 || strcmp(last_component, "..") == 0) {
714 fprintf(stderr, "up: you may not use '.' or '..' as the last component\n");
715 fprintf(stderr, "up: of a name in the 'up' command.\n");
719 blob->in = last_component;
720 blob->in_size = strlen(last_component)+1;
721 blob->out_size = MAXSIZE;
723 memset(space, 0, MAXSIZE);
725 code = pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, blob, 0);
728 printf("'%s' is a mount point for volume '%s'\n", name, space);
733 if (errno == EINVAL) {
734 /* printf( "'%s' is not a mount point.\n", name);
740 fprintf( stderr, "problem examining '%s' in '%s'.\n", last_component, parent_dir );
742 /* Die(errno, (ti->data ? ti->data : parent_dir));