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>
46 #include <afs/venus.h>
48 /* ************************************************************* */
52 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
54 extern char *sys_errlist[];
57 Boolean verbose = false;
58 Boolean renameTargets = false;
59 Boolean oneLevel = false;
60 Boolean preserveDate = true;
61 Boolean preserveMountPoints = false;
62 Boolean forceOverwrite = false;
65 Boolean setacl = true;
66 Boolean oldAcl = false;
67 char file1[MAXPATHLEN], file2[MAXPATHLEN];
70 static char space[MAXSIZE];
79 /* ************************************************************ */
83 /* ************************************************************ */
85 #include "AFS_component_version_number.c"
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 exit(1); /* some type of failure */
124 /* skip program name */
127 /* check for -flag options */
128 while (argc > 0 && *argv[0] == '-') {
141 renameTargets = true;
145 forceOverwrite = true;
149 preserveDate = false;
153 preserveMountPoints = true;
157 fprintf(stderr, "Unknown option: '%c'\n", *cp);
158 fprintf(stderr, "usage: up [-v1frxm] from to\n");
165 fprintf(stderr, "usage: up [-v1frx] from to\n");
169 strncpy(file1, argv[0], MAXPATHLEN);
170 strncpy(file2, argv[1], MAXPATHLEN);
178 * Make sure the parent directory of this file exists. Returns
179 * true if it exists, false otherwise. Note: the owner argument
180 * is a hack. All directories made will have this owner.
182 Boolean MakeParent(file, owner)
186 char parent[MAXPATHLEN];
190 strcpy(parent, file);
192 p = strrchr(parent, '/');
196 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
224 Copy(file1, file2, recursive, level)
225 char *file1; /* input file name */
226 char *file2; /* output file name */
227 Boolean recursive; /* true if directory should be copied */
228 int level; /* level of recursion: 0, 1, ... */
231 struct stat s1, s2; /*Stat blocks*/
232 struct ViceIoctl blob;
233 char aclspace[MAXACL];
234 afs_int32 rcode = 0, code;
237 code = lstat(file1, &s1);
239 fprintf(stderr,"Can't find %s\n",file1);
243 code = lstat(file2, &s2);
245 if (!MakeParent(file2,s1.st_uid))
250 if ((s1.st_mode & S_IFMT) == S_IFREG) {
252 * -------------------- Copy regular file --------------------
255 char buf[4096]; /* Must be bigger than sizeof (*head) */
256 struct timeval tv[2];
257 char tmpfile[MAXPATHLEN], newName[MAXPATHLEN];
260 printf("Level %d: File %s to %s\n", level, file1, file2);
264 /* Wonder if there is a security hole */
265 if ( ((s1.st_mode & 04002) == 04002) ||
266 ((s1.st_mode & 04020) == 04020) ||
267 ((s1.st_mode & 02002) == 02002) ) {
268 fprintf(stderr, "WARNING: Mode-bits security hole in files %s and %s\n",
272 if (!goods2 || (s1.st_mtime != s2.st_mtime) || (s1.st_size != s2.st_size)) { /*c*/
273 /* Don't ovewrite a write protected file (unless force: -f) */
274 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
276 "File %s is write protected against its owner; not changed\n",
282 printf(" Copy file %s to %s (%u Bytes)\n", file1, file2, s1.st_size);
286 strcpy(tmpfile, file2); /* Name of temporary file */
287 strcat(tmpfile,".UPD");
289 /* open file1 for input */
290 f1 = open(file1, O_RDONLY);
292 fprintf(stderr, "Unable to open input file %s, ", file1);
293 if (errno >= sys_nerr)
294 fprintf(stderr, "error code = %d\n", errno);
296 fprintf(stderr, "%s\n", sys_errlist[errno]);
300 /* open temporary output file */
301 f2 = open(tmpfile, (O_WRONLY | O_CREAT | O_TRUNC), s1.st_mode);
303 fprintf(stderr, "Unable to open output file %s, ", tmpfile);
304 if (errno >= sys_nerr)
305 fprintf(stderr, "error code = %d\n", errno);
307 fprintf(stderr, "%s\n", sys_errlist[errno]);
313 /* Copy file1 to temporary file */
314 while ((n = read(f1, buf, sizeof(buf))) > 0) {
315 if (write(f2, buf, n) != n) {
316 fprintf(stderr,"Write failed, file %s must be copied again.\n", file2);
320 /* preserve access and modification times: ("-x" disables)*/
322 tv[0].tv_sec = s1.st_atime;
324 tv[1].tv_sec = s1.st_mtime;
329 /* Close the files */
337 /* Rename file2 to file2.old. [-r] */
338 if (renameTargets && goods2) {
339 strcpy(newName, file2);
340 strcat(newName, ".old");
342 printf(" Renaming %s to %s\n", file2, newName);
345 if (rename(file2, newName) < 0) {
346 fprintf(stderr, "Rename of %s to %s failed.\n", file2, newName);
350 /* Rename temporary file to file2 */
351 code = rename(tmpfile, file2);
353 fprintf(stderr, "Rename of %s to %s failed.\n", tmpfile, file2);
357 /* Re-stat file2 and compare file sizes */
358 code = lstat(file2, &s2);
360 fprintf(stderr, "WARNING: Unable to stat new file %s\n", file2);
363 if (s1.st_size != s2.st_size) {
364 fprintf(stderr, "WARNING: New file %s is %u bytes long; should be %u\n",
365 file2, s2.st_size, s1.st_size);
369 /* Set the user-id */
370 if (s2.st_uid != s1.st_uid) {
372 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
375 code = chown(file2, s1.st_uid, -1);
377 fprintf(stderr, "Unable to set owner-id for %s to %d\n", file2, s1.st_uid);
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", file2, s1.st_gid);
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, (s1.st_mode & 07777));
405 code = chmod(file2, s1.st_mode);
407 fprintf(stderr, "Unable to set mode-bits for %s to %d\n", file2, s1.st_mode);
413 else if ((s1.st_mode & S_IFMT) == S_IFLNK) {
415 * --------------------- Copy symlink --------------------
417 char linkvalue[MAXPATHLEN+1];
421 printf("Level %d: Symbolic link %s to %s\n", level, file1, file2);
425 /* Don't ovewrite a write protected directory (unless force: -f) */
426 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
428 "Link %s is write protected against its owner; not changed\n",
434 printf(" Copy symbolic link %s->%s to %s\n", file1, linkvalue, file2);
438 n = readlink(file1, linkvalue, sizeof(linkvalue));
440 fprintf(stderr, "Could not read symbolic link %s\n", file1);
441 perror("read link ");
446 unlink(file2); /* Always make the new link (it was easier) */
448 code = symlink(linkvalue, file2);
450 fprintf(stderr, "Could not create symbolic link %s\n", file2);
451 perror("create link ");
454 } /*Dealing with symlink*/
456 else if ( preserveMountPoints && (code=isMountPoint( file1, &blob )) ) {
458 * --------------------- Copy mount point --------------------
462 perror("checking for mount point ");
466 printf("Level %d: Mount point %s to %s\n", level, file1, file2);
470 /* Don't ovewrite a write protected directory (unless force: -f) */
471 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
473 "Target %s is write protected against its owner; not changed\n",
479 printf(" Copy mount point %s for vol %s to %s\n", file1, blob.out, file2);
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", file2, blob.out);
489 perror("create mount point ");
493 } /*Dealing with mount point*/
495 else if (((s1.st_mode & S_IFMT) == S_IFDIR) && (recursive || (level == 0))) {
497 * ----------------------- Copy directory -----------------------
503 char f1[MAXPATHLEN], f2[MAXPATHLEN];
508 printf("Level %d: Directory %s to %s\n", level, file1, file2);
512 /* Don't ovewrite a write protected directory (unless force: -f) */
513 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
515 "Directory %s is write protected against its owner; not changed\n",
522 p1 = f1 + strlen(f1);
523 p2 = f2 + strlen(f2);
524 if (p1 == f1 || p1[-1] != '/')
526 if (p2 == f2 || p2[-1] != '/')
529 dir = opendir(file1);
531 fprintf(stderr, "Couldn't open %s\n", file1);
535 while ((d = readdir(dir)) != NULL) {
536 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
538 strcpy(p1, d->d_name);
539 strcpy(p2, d->d_name);
540 code = Copy(f1, f2, recursive, level + 1);
541 if (code && !rcode) rcode = 1; /* remember errors */
547 printf("Level %d: Copied directory %s to %s\n", level, file1, file2);
551 mkdir(file2, 0777); /* Handle case where MakeParent not invoked. */
554 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
557 code = chown(file2, s1.st_uid, -1);
559 fprintf(stderr, "Unable to set owner-id for %s to %d\n", file2, s1.st_uid);
561 s1.st_mode &= ~04000; /* Don't set suid bit */
565 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
568 code = chown(file2, -1, s1.st_gid);
570 fprintf(stderr, "Unable to set group-id for %s to %d\n", file2, s1.st_gid);
572 s1.st_mode &= ~02000; /* Don't set sgid bit */
576 printf(" Set mode-bit for %s to %o\n", file2, (s1.st_mode & 07777));
579 code = chmod(file2, s1.st_mode);
581 fprintf(stderr, "Unable to set mode-bits for %s to %d\n", file2, s1.st_mode);
586 if (setacl == true) {
588 printf(" Set acls for %s\n", file2);
595 blob.out_size = MAXACL;
598 /* Get an old-style ACL and convert it */
599 for (i=1; i<strlen(file1); i++)
600 if (file1[i] == '/') break;
601 strcpy(aclspace, &file1[i]);
603 blob.in_size = 1+strlen(aclspace);
604 tfd = open(file1, O_RDONLY, 0);
606 perror("old-acl open ");
609 code = ioctl(tfd, _VICEIOCTL(4), &blob);
612 if (errno == EINVAL) {
619 /* Now convert the thing. */
620 oacl = (struct OldAcl *) (aclspace+4);
621 sprintf(tacl, "%d\n%d\n", oacl->nplus, oacl->nminus);
622 strcat(tacl, oacl->data);
623 strcpy(aclspace, tacl);
624 } /*Grab and convert old-style ACL*/
626 /* Get a new-style ACL */
627 code = pioctl(file1, _VICEIOCTL(2), &blob, 1);
629 if (errno == EINVAL) {
637 } /*Grab new-style ACL*/
640 * Now, set the new-style ACL.
642 if (setacl == true) {
646 blob.in_size = 1+strlen(aclspace);
647 code = pioctl(file2, _VICEIOCTL(1), &blob, 1);
649 if (errno == EINVAL) {
653 fprintf(stderr, "Couldn't set acls for %s\n", file2);
659 if (setacl == false) {
660 printf("Not setting acls\n");
669 int isMountPoint( name, blob )
671 struct ViceIoctl *blob;
674 char true_name[1024]; /*dirname*/
675 char parent_dir[1024]; /*Parent directory of true name*/
676 char *last_component; /*Last component of true name*/
678 sprintf(true_name, "%s%s",
679 (name[0] == '/') ? "" : "./",
683 * Find rightmost slash, if any.
685 last_component = (char *) strrchr(true_name, '/');
686 if (last_component) {
688 * Found it. Designate everything before it as the parent directory,
689 * everything after it as the final component.
691 strncpy(parent_dir, true_name, last_component - true_name);
692 parent_dir[last_component - true_name] = 0;
693 last_component++; /*Skip the slash*/
697 * No slash appears in the given file name. Set parent_dir to the current
698 * directory, and the last component as the given name.
700 strcpy(parent_dir, ".");
701 last_component = true_name;
704 if (strcmp(last_component, ".") == 0 || strcmp(last_component, "..") == 0) {
705 fprintf(stderr, "up: you may not use '.' or '..' as the last component\n");
706 fprintf(stderr, "up: of a name in the 'up' command.\n");
710 blob->in = last_component;
711 blob->in_size = strlen(last_component)+1;
712 blob->out_size = MAXSIZE;
714 memset(space, 0, MAXSIZE);
716 code = pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, blob, 0);
719 printf("'%s' is a mount point for volume '%s'\n", name, space);
724 if (errno == EINVAL) {
725 /* printf( "'%s' is not a mount point.\n", name);
731 fprintf( stderr, "problem examining '%s' in '%s'.\n", last_component, parent_dir );
733 /* Die(errno, (ti->data ? ti->data : parent_dir));