2 * (C) COPYRIGHT IBM CORPORATION 1987
3 * LICENSED MATERIALS - PROPERTY OF IBM
6 /* missing type from C language */
11 /* ************************************************************* */
13 #include <afs/param.h>
20 #include <sys/param.h>
30 #include <sys/ioctl.h>
35 /* ************************************************************* */
39 extern char *index ();
40 extern char *rindex ();
41 #ifndef AFS_LINUX20_ENV
43 extern char *sys_errlist[];
46 Boolean verbose = false;
47 Boolean renameTargets = false;
48 Boolean oneLevel = false;
49 Boolean preserveDate = true;
50 Boolean forceOverwrite = false;
53 Boolean setacl = true;
54 Boolean oldAcl = false;
55 char file1[MAXPATHLEN], file2[MAXPATHLEN];
64 /* ************************************************************ */
68 /* ************************************************************ */
70 #include "AFS_component_version_number.c"
79 * The following signal action for AIX is necessary so that in case of a
80 * crash (i.e. core is generated) we can include the user's data section
81 * in the core dump. Unfortunately, by default, only a partial core is
82 * generated which, in many cases, isn't too useful.
86 sigemptyset(&nsa.sa_mask);
87 nsa.sa_handler = SIG_DFL;
88 nsa.sa_flags = SA_FULLDUMP;
89 sigaction(SIGSEGV, &nsa, NULL);
91 #if !defined (AFS_AIX_ENV) && !defined (AFS_HPUX_ENV)
92 pageSize = getpagesize();
96 /* now read each line of the CopyList */
97 if (Copy(file1, file2, !oneLevel, 0))
98 exit(1); /* some type of failure */
109 /* skip program name */
112 /* check for -flag options */
113 while (argc > 0 && *argv[0] == '-') {
126 renameTargets = true;
130 forceOverwrite = true;
134 preserveDate = false;
138 fprintf(stderr, "Unknown option: '%c'\n", *cp);
139 fprintf(stderr, "usage: up [-v1frx] from to\n");
146 fprintf(stderr, "usage: up [-v1frx] from to\n");
150 strcpy(file1, argv[0]);
151 strcpy(file2, argv[1]);
159 * Make sure the parent directory of this file exists. Returns
160 * true if it exists, false otherwise. Note: the owner argument
161 * is a hack. All directories made will have this owner.
163 Boolean MakeParent(file, owner)
167 char parent[MAXPATHLEN];
171 strcpy(parent, file);
173 p = rindex(parent, '/');
177 else if (p > parent) {
184 if (stat(parent, &s) < 0) {
185 if (!MakeParent(parent, owner))
189 printf("Creating directory %s\n", parent);
194 chown(parent, owner, -1);
202 * This does the bulk of the work of the program. Handle one file,
203 * possibly copying subfiles if this is a directory
205 Copy(file1, file2, recursive, level)
206 char *file1; /* input file name */
207 char *file2; /* output file name */
208 Boolean recursive; /* true if directory should be copied */
209 int level; /* level of recursion: 0, 1, ... */
212 struct stat s1, s2; /*Stat blocks*/
213 struct ViceIoctl blob;
214 char aclspace[MAXACL];
215 afs_int32 rcode = 0, code;
218 code = lstat(file1, &s1);
220 fprintf(stderr,"Can't find %s\n",file1);
224 code = lstat(file2, &s2);
226 if (!MakeParent(file2,s1.st_uid))
231 if ((s1.st_mode & S_IFMT) == S_IFREG) {
233 * -------------------- Copy regular file --------------------
236 char buf[4096]; /* Must be bigger than sizeof (*head) */
237 struct timeval tv[2];
238 char tmpfile[MAXPATHLEN], newName[MAXPATHLEN];
241 printf("Level %d: File %s to %s\n", level, file1, file2);
245 /* Wonder if there is a security hole */
246 if ( ((s1.st_mode & 04002) == 04002) ||
247 ((s1.st_mode & 04020) == 04020) ||
248 ((s1.st_mode & 02002) == 02002) ) {
249 fprintf(stderr, "WARNING: Mode-bits security hole in files %s and %s\n",
253 if (!goods2 || (s1.st_mtime != s2.st_mtime) || (s1.st_size != s2.st_size)) { /*c*/
254 /* Don't ovewrite a write protected file (unless force: -f) */
255 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
257 "File %s is write protected against its owner; not changed\n",
263 printf(" Copy file %s to %s (%u Bytes)\n", file1, file2, s1.st_size);
267 strcpy(tmpfile, file2); /* Name of temporary file */
268 strcat(tmpfile,".UPD");
270 /* open file1 for input */
271 f1 = open(file1, O_RDONLY);
273 fprintf(stderr, "Unable to open input file %s, ", file1);
274 if (errno >= sys_nerr)
275 fprintf(stderr, "error code = %d\n", errno);
277 fprintf(stderr, "%s\n", sys_errlist[errno]);
281 /* open temporary output file */
282 f2 = open(tmpfile, (O_WRONLY | O_CREAT | O_TRUNC), s1.st_mode);
284 fprintf(stderr, "Unable to open output file %s, ", tmpfile);
285 if (errno >= sys_nerr)
286 fprintf(stderr, "error code = %d\n", errno);
288 fprintf(stderr, "%s\n", sys_errlist[errno]);
294 /* Copy file1 to temporary file */
295 while ((n = read(f1, buf, sizeof(buf))) > 0) {
296 if (write(f2, buf, n) != n) {
297 fprintf(stderr,"Write failed, file %s must be copied again.\n", file2);
301 /* preserve access and modification times: ("-x" disables)*/
303 tv[0].tv_sec = s1.st_atime;
305 tv[1].tv_sec = s1.st_mtime;
310 /* Close the files */
318 /* Rename file2 to file2.old. [-r] */
319 if (renameTargets && goods2) {
320 strcpy(newName, file2);
321 strcat(newName, ".old");
323 printf(" Renaming %s to %s\n", file2, newName);
326 if (rename(file2, newName) < 0) {
327 fprintf(stderr, "Rename of %s to %s failed.\n", file2, newName);
331 /* Rename temporary file to file2 */
332 code = rename(tmpfile, file2);
334 fprintf(stderr, "Rename of %s to %s failed.\n", tmpfile, file2);
338 /* Re-stat file2 and compare file sizes */
339 code = lstat(file2, &s2);
341 fprintf(stderr, "WARNING: Unable to stat new file %s\n", file2);
344 if (s1.st_size != s2.st_size) {
345 fprintf(stderr, "WARNING: New file %s is %u bytes long; should be %u\n",
346 file2, s2.st_size, s1.st_size);
350 /* Set the user-id */
351 if (s2.st_uid != s1.st_uid) {
353 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
356 code = chown(file2, s1.st_uid, -1);
358 fprintf(stderr, "Unable to set owner-id for %s to %d\n", file2, s1.st_uid);
361 s1.st_mode &= ~04000; /* Don't set suid bit */
365 /* Set the group-id */
366 if (s2.st_gid != s1.st_gid) {
368 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
371 code = chown(file2, -1, s1.st_gid);
373 fprintf(stderr, "Unable to set group-id for %s to %d\n", file2, s1.st_gid);
376 s1.st_mode &= ~02000; /* Don't set sgid bit */
380 /* Set the mode bits */
381 if (s1.st_mode != s2.st_mode) {
383 printf(" Set mode-bit for %s to %o\n", file2, (s1.st_mode & 07777));
386 code = chmod(file2, s1.st_mode);
388 fprintf(stderr, "Unable to set mode-bits for %s to %d\n", file2, s1.st_mode);
394 else if ((s1.st_mode & S_IFMT) == S_IFLNK) {
396 * --------------------- Copy symlink --------------------
398 char linkvalue[MAXPATHLEN+1];
402 printf("Level %d: Symbolic link %s to %s\n", level, file1, file2);
406 /* Don't ovewrite a write protected directory (unless force: -f) */
407 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
409 "Link %s is write protected against its owner; not changed\n",
415 printf(" Copy symbolic link %s->%s to %s\n", file1, linkvalue, file2);
419 n = readlink(file1, linkvalue, sizeof(linkvalue));
421 fprintf(stderr, "Could not read symbolic link %s\n", file1);
422 perror("read link ");
427 unlink(file2); /* Always make the new link (it was easier) */
429 code = symlink(linkvalue, file2);
431 fprintf(stderr, "Could not create symbolic link %s\n", file2);
432 perror("create link ");
435 } /*Dealing with symlink*/
437 else if (((s1.st_mode & S_IFMT) == S_IFDIR) && (recursive || (level == 0))) {
439 * ----------------------- Copy directory -----------------------
445 char f1[MAXPATHLEN], f2[MAXPATHLEN];
450 printf("Level %d: Directory %s to %s\n", level, file1, file2);
454 /* Don't ovewrite a write protected directory (unless force: -f) */
455 if (!forceOverwrite && goods2 && (s2.st_mode & 0200) == 0) {
457 "Directory %s is write protected against its owner; not changed\n",
464 p1 = f1 + strlen(f1);
465 p2 = f2 + strlen(f2);
466 if (p1 == f1 || p1[-1] != '/')
468 if (p2 == f2 || p2[-1] != '/')
471 dir = opendir(file1);
473 fprintf(stderr, "Couldn't open %s\n", file1);
477 while ((d = readdir(dir)) != NULL) {
478 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
480 strcpy(p1, d->d_name);
481 strcpy(p2, d->d_name);
482 code = Copy(f1, f2, recursive, level + 1);
483 if (code && !rcode) rcode = 1; /* remember errors */
489 printf("Level %d: Copied directory %s to %s\n", level, file1, file2);
493 mkdir(file2, 0777); /* Handle case where MakeParent not invoked. */
496 printf(" Set owner-id for %s to %d\n", file2, s1.st_uid);
499 code = chown(file2, s1.st_uid, -1);
501 fprintf(stderr, "Unable to set owner-id for %s to %d\n", file2, s1.st_uid);
503 s1.st_mode &= ~04000; /* Don't set suid bit */
507 printf(" Set group-id for %s to %d\n", file2, s1.st_gid);
510 code = chown(file2, -1, s1.st_gid);
512 fprintf(stderr, "Unable to set group-id for %s to %d\n", file2, s1.st_gid);
514 s1.st_mode &= ~02000; /* Don't set sgid bit */
518 printf(" Set mode-bit for %s to %o\n", file2, (s1.st_mode & 07777));
521 code = chmod(file2, s1.st_mode);
523 fprintf(stderr, "Unable to set mode-bits for %s to %d\n", file2, s1.st_mode);
528 if (setacl == true) {
530 printf(" Set acls for %s\n", file2);
537 blob.out_size = MAXACL;
540 /* Get an old-style ACL and convert it */
541 for (i=1; i<strlen(file1); i++)
542 if (file1[i] == '/') break;
543 strcpy(aclspace, &file1[i]);
545 blob.in_size = 1+strlen(aclspace);
546 tfd = open(file1, O_RDONLY, 0);
548 perror("old-acl open ");
551 code = ioctl(tfd, _VICEIOCTL(4), &blob);
554 if (errno == EINVAL) {
561 /* Now convert the thing. */
562 oacl = (struct OldAcl *) (aclspace+4);
563 sprintf(tacl, "%d\n%d\n", oacl->nplus, oacl->nminus);
564 strcat(tacl, oacl->data);
565 strcpy(aclspace, tacl);
566 } /*Grab and convert old-style ACL*/
568 /* Get a new-style ACL */
569 code = pioctl(file1, _VICEIOCTL(2), &blob, 1);
571 if (errno == EINVAL) {
579 } /*Grab new-style ACL*/
582 * Now, set the new-style ACL.
584 if (setacl == true) {
588 blob.in_size = 1+strlen(aclspace);
589 code = pioctl(file2, _VICEIOCTL(1), &blob, 1);
591 if (errno == EINVAL) {
595 fprintf(stderr, "Couldn't set acls for %s\n", file2);
601 if (setacl == false) {
602 printf("Not setting acls\n");