static int copy_if_needed(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf) { switch (tflag) { case FTW_D: return mkdir_dst(fpath, sb); break; case FTW_F: return copy_file(fpath, sb); break; case FTW_SL: return copy_link(fpath, sb); break; case FTW_DNR: case FTW_NS: case FTW_DP: case FTW_SLN: // Should not happen : indicate failure return 1; } // continue return 0; }
/** * initiate the cow-copy action */ int cow_cp(const char *path, int branch_ro, int branch_rw, bool copy_dir) { DBG("%s\n", path); // create the path to the file path_create_cutlast(path, branch_ro, branch_rw); char from[PATHLEN_MAX], to[PATHLEN_MAX]; if (BUILD_PATH(from, uopt.branches[branch_ro].path, path)) RETURN(-ENAMETOOLONG); if (BUILD_PATH(to, uopt.branches[branch_rw].path, path)) RETURN(-ENAMETOOLONG); setlocale(LC_ALL, ""); struct cow cow; cow.uid = getuid(); // Copy the umask for explicit mode setting. cow.umask = umask(0); umask(cow.umask); cow.from_path = from; cow.to_path = to; struct stat buf; lstat(cow.from_path, &buf); cow.stat = &buf; int res; switch (buf.st_mode & S_IFMT) { case S_IFLNK: res = copy_link(&cow); break; case S_IFDIR: if (copy_dir) { res = copy_directory(path, branch_ro, branch_rw); } else { res = path_create(path, branch_ro, branch_rw); } break; case S_IFBLK: case S_IFCHR: res = copy_special(&cow); break; case S_IFIFO: res = copy_fifo(&cow); break; case S_IFSOCK: USYSLOG(LOG_WARNING, "COW of sockets not supported: %s\n", cow.from_path); RETURN(1); default: res = copy_file(&cow); } RETURN(res); }
int copy_object(string_t from, string_t to){ struct stat statbuf; if(stat(from,&statbuf)) return 1; if(S_ISDIR (statbuf.st_mode)) { printf("cp: directory ``%s'' was skipped\n",from); return 1; } if(S_ISSOCK(statbuf.st_mode)) { printf("cp: socket ``%s'' was skipped\n",from); return 1; } if(S_ISFIFO(statbuf.st_mode)) { printf("cp: named pipe ``%s'' was skipped\n",from); return 1; } if(S_ISBLK (statbuf.st_mode)) { printf("cp: block device ``%s'' was skipped\n",from); return 1; } if(S_ISCHR (statbuf.st_mode)) { printf("cp: char device ``%s'' was skipped\n",from); return 1; } if(S_ISLNK (statbuf.st_mode)) return copy_link(from,to); if(S_ISREG (statbuf.st_mode)) return copy_file(from,to); printf("cp: special file ``%s'' was skipped\n",from); return 1; }
/** * initiate the cow-copy action */ int cow_cp(const char *path, int branch_ro, int branch_rw) { DBG_IN(); // create the path to the file path_create_cutlast(path, branch_ro, branch_rw); char from[PATHLEN_MAX], to[PATHLEN_MAX]; snprintf(from, PATHLEN_MAX, "%s%s", uopt.branches[branch_ro].path, path); snprintf(to, PATHLEN_MAX, "%s%s", uopt.branches[branch_rw].path, path); setlocale(LC_ALL, ""); struct cow cow; cow.uid = getuid(); // Copy the umask for explicit mode setting. cow.umask = umask(0); umask(cow.umask); cow.from_path = from; cow.to_path = to; struct stat buf; lstat(cow.from_path, &buf); cow.stat = &buf; int res; switch (buf.st_mode & S_IFMT) { case S_IFLNK: res = copy_link(&cow); break; case S_IFDIR: res = copy_directory(path, branch_ro, branch_rw); break; case S_IFBLK: case S_IFCHR: res = copy_special(&cow); break; case S_IFIFO: res = copy_fifo(&cow); break; case S_IFSOCK: usyslog(LOG_WARNING, "COW of sockets not supported: %s\n", cow.from_path); return 1; default: res = copy_file(&cow); } return res; }
static void duplicate_link(const char *src, const char *dest, struct stat *s) { char *rsrc = check(src); // we drop the result and use the original name char *rdest = check(dest); uid_t uid = s->st_uid; gid_t gid = s->st_gid; mode_t mode = s->st_mode; // build destination file name char *name; // char *ptr = strrchr(rsrc, '/'); char *ptr = strrchr(src, '/'); ptr++; if (asprintf(&name, "%s/%s", rdest, ptr) == -1) errExit("asprintf"); // copy copy_link(rsrc, name, mode, uid, gid); free(name); free(rsrc); free(rdest); }
int copy(char *argv[], enum op type, int fts_options) { struct stat to_stat; FTS *ftsp; FTSENT *curr; int base, nlen, rval; char *p, *target_mid; base = 0; if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL) err(1, NULL); for (rval = 0; (curr = fts_read(ftsp)) != NULL;) { switch (curr->fts_info) { case FTS_NS: case FTS_DNR: case FTS_ERR: warnx("%s: %s", curr->fts_path, strerror(curr->fts_errno)); rval = 1; continue; case FTS_DC: warnx("%s: directory causes a cycle", curr->fts_path); rval = 1; continue; } /* * If we are in case (2) or (3) above, we need to append the * source name to the target name. */ if (type != FILE_TO_FILE) { /* * Need to remember the roots of traversals to create * correct pathnames. If there's a directory being * copied to a non-existent directory, e.g. * cp -R a/dir noexist * the resulting path name should be noexist/foo, not * noexist/dir/foo (where foo is a file in dir), which * is the case where the target exists. * * Also, check for "..". This is for correct path * concatenation for paths ending in "..", e.g. * cp -R .. /tmp * Paths ending in ".." are changed to ".". This is * tricky, but seems the easiest way to fix the problem. * * XXX * Since the first level MUST be FTS_ROOTLEVEL, base * is always initialized. */ if (curr->fts_level == FTS_ROOTLEVEL) { if (type != DIR_TO_DNE) { p = find_last_component(curr->fts_path); base = p - curr->fts_path; if (!strcmp(&curr->fts_path[base], "..")) base += 1; } else base = curr->fts_pathlen; } p = &curr->fts_path[base]; nlen = curr->fts_pathlen - base; target_mid = to.target_end; if (*p != '/' && target_mid[-1] != '/') *target_mid++ = '/'; *target_mid = '\0'; if (target_mid - to.p_path + nlen >= PATH_MAX) { warnx("%s%s: name too long (not copied)", to.p_path, p); rval = 1; continue; } (void)strncat(target_mid, p, nlen); to.p_end = target_mid + nlen; *to.p_end = '\0'; } /* Not an error but need to remember it happened */ if (stat(to.p_path, &to_stat) == -1) { if (curr->fts_info == FTS_DP) continue; /* * We use fts_pointer as a boolean to indicate that * we created this directory ourselves. We'll use * this later on via the fts_dne macro to decide * whether or not to set the directory mode during * the post-order pass. */ curr->fts_pointer = (void *)1; } else { /* * Set directory mode/user/times on the post-order * pass. We can't do this earlier because the mode * may not allow us write permission. Furthermore, * if we set the times during the pre-order pass, * they will get changed later when the directory * is populated. */ if (curr->fts_info == FTS_DP) { if (!S_ISDIR(to_stat.st_mode)) continue; /* * If not -p and directory didn't exist, set * it to be the same as the from directory, * unmodified by the umask; arguably wrong, * but it's been that way forever. */ if (pflag && setfile(curr->fts_statp, -1)) rval = 1; else if (fts_dne(curr)) (void)chmod(to.p_path, curr->fts_statp->st_mode); continue; } if (to_stat.st_dev == curr->fts_statp->st_dev && to_stat.st_ino == curr->fts_statp->st_ino) { warnx("%s and %s are identical (not copied).", to.p_path, curr->fts_path); rval = 1; if (S_ISDIR(curr->fts_statp->st_mode)) (void)fts_set(ftsp, curr, FTS_SKIP); continue; } if (!S_ISDIR(curr->fts_statp->st_mode) && S_ISDIR(to_stat.st_mode)) { warnx("cannot overwrite directory %s with non-directory %s", to.p_path, curr->fts_path); rval = 1; continue; } } switch (curr->fts_statp->st_mode & S_IFMT) { case S_IFLNK: if (copy_link(curr, !fts_dne(curr))) rval = 1; break; case S_IFDIR: if (!Rflag && !rflag) { warnx("%s is a directory (not copied).", curr->fts_path); (void)fts_set(ftsp, curr, FTS_SKIP); rval = 1; break; } /* * If the directory doesn't exist, create the new * one with the from file mode plus owner RWX bits, * modified by the umask. Trade-off between being * able to write the directory (if from directory is * 555) and not causing a permissions race. If the * umask blocks owner writes, we fail.. */ if (fts_dne(curr)) { if (mkdir(to.p_path, curr->fts_statp->st_mode | S_IRWXU) < 0) err(1, "%s", to.p_path); } else if (!S_ISDIR(to_stat.st_mode)) errc(1, ENOTDIR, "%s", to.p_path); break; case S_IFBLK: case S_IFCHR: if (Rflag) { if (copy_special(curr->fts_statp, !fts_dne(curr))) rval = 1; } else if (copy_file(curr, fts_dne(curr))) rval = 1; break; case S_IFIFO: if (Rflag) { if (copy_fifo(curr->fts_statp, !fts_dne(curr))) rval = 1; } else if (copy_file(curr, fts_dne(curr))) rval = 1; break; case S_IFSOCK: warnc(EOPNOTSUPP, "%s", curr->fts_path); break; default: if (copy_file(curr, fts_dne(curr))) rval = 1; break; } } if (errno) err(1, "fts_read"); (void)fts_close(ftsp); return (rval); }
static int fs_copydir(const char *infname, const struct stat *st, int ftype, struct FTW *sftw) { (void) st; (void) sftw; assert(infname); assert(*infname != '\0'); assert(outpath); assert(*outpath != '\0'); assert(inpath); // check size limit if (size_limit_reached) return 0; char *outfname; if (asprintf(&outfname, "%s%s", outpath, infname + strlen(inpath)) == -1) errExit("asprintf"); //printf("outpaht %s\n", outpath); //printf("inpath %s\n", inpath); //printf("infname %s\n", infname); //printf("outfname %s\n\n", outfname); // don't copy it if we already have the file struct stat s; if (stat(outfname, &s) == 0) { if (first) first = 0; else fprintf(stderr, "Warning fcopy: skipping %s, file already present\n", infname); free(outfname); return 0; } // extract mode and ownership if (stat(infname, &s) != 0) { fprintf(stderr, "Warning fcopy: skipping %s, cannot find inode\n", infname); free(outfname); return 0; } uid_t uid = s.st_uid; gid_t gid = s.st_gid; mode_t mode = s.st_mode; // recalculate size if ((s.st_size + size_cnt) > COPY_LIMIT) { fprintf(stderr, "Error fcopy: size limit of %dMB reached\n", (COPY_LIMIT / 1024) / 1024); size_limit_reached = 1; free(outfname); return 0; } file_cnt++; size_cnt += s.st_size; if(ftype == FTW_F) { copy_file(infname, outfname, mode, uid, gid); } else if (ftype == FTW_D) { mkdir_attr(outfname, mode, uid, gid); } else if (ftype == FTW_SL) { copy_link(infname, outfname, mode, uid, gid); } return(0); }
int copy(char *argv[], enum op type, int fts_options) { struct stat to_stat; FTS *ftsp; FTSENT *curr; int base, dne, sval; int this_failed, any_failed; size_t nlen; char *p, *target_mid; base = 0; /* XXX gcc -Wuninitialized (see comment below) */ if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL) err(EXIT_FAILURE, "%s", argv[0]); /* NOTREACHED */ for (any_failed = 0; (curr = fts_read(ftsp)) != NULL;) { this_failed = 0; switch (curr->fts_info) { case FTS_NS: case FTS_DNR: case FTS_ERR: warnx("%s: %s", curr->fts_path, strerror(curr->fts_errno)); this_failed = any_failed = 1; continue; case FTS_DC: /* Warn, continue. */ warnx("%s: directory causes a cycle", curr->fts_path); this_failed = any_failed = 1; continue; } /* * If we are in case (2) or (3) above, we need to append the * source name to the target name. */ if (type != FILE_TO_FILE) { if ((curr->fts_namelen + to.target_end - to.p_path + 1) > MAXPATHLEN) { warnx("%s/%s: name too long (not copied)", to.p_path, curr->fts_name); this_failed = any_failed = 1; continue; } /* * Need to remember the roots of traversals to create * correct pathnames. If there's a directory being * copied to a non-existent directory, e.g. * cp -R a/dir noexist * the resulting path name should be noexist/foo, not * noexist/dir/foo (where foo is a file in dir), which * is the case where the target exists. * * Also, check for "..". This is for correct path * concatentation for paths ending in "..", e.g. * cp -R .. /tmp * Paths ending in ".." are changed to ".". This is * tricky, but seems the easiest way to fix the problem. * * XXX * Since the first level MUST be FTS_ROOTLEVEL, base * is always initialized. */ if (curr->fts_level == FTS_ROOTLEVEL) { if (type != DIR_TO_DNE) { p = strrchr(curr->fts_path, '/'); base = (p == NULL) ? 0 : (int)(p - curr->fts_path + 1); if (!strcmp(&curr->fts_path[base], "..")) base += 1; } else base = curr->fts_pathlen; } p = &curr->fts_path[base]; nlen = curr->fts_pathlen - base; target_mid = to.target_end; if (*p != '/' && target_mid[-1] != '/') *target_mid++ = '/'; *target_mid = 0; if (target_mid - to.p_path + nlen >= PATH_MAX) { warnx("%s%s: name too long (not copied)", to.p_path, p); this_failed = any_failed = 1; continue; } (void)strncat(target_mid, p, nlen); to.p_end = target_mid + nlen; *to.p_end = 0; STRIP_TRAILING_SLASH(to); } sval = Pflag ? lstat(to.p_path, &to_stat) : stat(to.p_path, &to_stat); /* Not an error but need to remember it happened */ if (sval == -1) dne = 1; else { if (to_stat.st_dev == curr->fts_statp->st_dev && to_stat.st_ino == curr->fts_statp->st_ino) { warnx("%s and %s are identical (not copied).", to.p_path, curr->fts_path); this_failed = any_failed = 1; if (S_ISDIR(curr->fts_statp->st_mode)) (void)fts_set(ftsp, curr, FTS_SKIP); continue; } if (!S_ISDIR(curr->fts_statp->st_mode) && S_ISDIR(to_stat.st_mode)) { warnx("cannot overwrite directory %s with non-directory %s", to.p_path, curr->fts_path); this_failed = any_failed = 1; continue; } dne = 0; } switch (curr->fts_statp->st_mode & S_IFMT) { case S_IFLNK: /* Catch special case of a non dangling symlink */ if((fts_options & FTS_LOGICAL) || ((fts_options & FTS_COMFOLLOW) && curr->fts_level == 0)) { if (copy_file(curr, dne)) this_failed = any_failed = 1; } else { if (copy_link(curr, !dne)) this_failed = any_failed = 1; } break; case S_IFDIR: if (!Rflag && !rflag) { if (curr->fts_info == FTS_D) warnx("%s is a directory (not copied).", curr->fts_path); (void)fts_set(ftsp, curr, FTS_SKIP); this_failed = any_failed = 1; break; } /* * Directories get noticed twice: * In the first pass, create it if needed. * In the second pass, after the children have been copied, set the permissions. */ if (curr->fts_info == FTS_D) /* First pass */ { /* * If the directory doesn't exist, create the new * one with the from file mode plus owner RWX bits, * modified by the umask. Trade-off between being * able to write the directory (if from directory is * 555) and not causing a permissions race. If the * umask blocks owner writes, we fail.. */ pushdne(dne); if (dne) { if (mkdir(to.p_path, curr->fts_statp->st_mode | S_IRWXU) < 0) err(EXIT_FAILURE, "%s", to.p_path); /* NOTREACHED */ } else if (!S_ISDIR(to_stat.st_mode)) { errno = ENOTDIR; err(EXIT_FAILURE, "%s", to.p_path); /* NOTREACHED */ } } else if (curr->fts_info == FTS_DP) /* Second pass */ { /* * If not -p and directory didn't exist, set it to be * the same as the from directory, umodified by the * umask; arguably wrong, but it's been that way * forever. */ if (pflag && setfile(curr->fts_statp, 0)) this_failed = any_failed = 1; else if ((dne = popdne())) (void)chmod(to.p_path, curr->fts_statp->st_mode); } else { warnx("directory %s encountered when not expected.", curr->fts_path); this_failed = any_failed = 1; break; } break; case S_IFBLK: case S_IFCHR: if (Rflag) { if (copy_special(curr->fts_statp, !dne)) this_failed = any_failed = 1; } else if (copy_file(curr, dne)) this_failed = any_failed = 1; break; case S_IFIFO: if (Rflag) { if (copy_fifo(curr->fts_statp, !dne)) this_failed = any_failed = 1; } else if (copy_file(curr, dne)) this_failed = any_failed = 1; break; default: if (copy_file(curr, dne)) this_failed = any_failed = 1; break; } if (vflag && !this_failed) (void)printf("%s -> %s\n", curr->fts_path, to.p_path); } if (errno) { err(EXIT_FAILURE, "fts_read"); /* NOTREACHED */ } (void)fts_close(ftsp); return (any_failed); }
int copy(rtems_shell_cp_globals* cp_globals, char *argv[], enum op type, int fts_options) { struct stat to_stat; FTS *ftsp; FTSENT *curr; int base = 0, dne, badcp, rval; size_t nlen; char *p, *target_mid; mode_t mask, mode; /* * Keep an inverted copy of the umask, for use in correcting * permissions on created directories when not using -p. */ mask = ~umask(0777); umask(~mask); if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL) err(exit_jump, 1, "fts_open"); for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) { switch (curr->fts_info) { case FTS_NS: case FTS_DNR: case FTS_ERR: warnx("%s: %s", curr->fts_path, strerror(curr->fts_errno)); badcp = rval = 1; continue; case FTS_DC: /* Warn, continue. */ warnx("%s: directory causes a cycle", curr->fts_path); badcp = rval = 1; continue; default: ; } /* * If we are in case (2) or (3) above, we need to append the * source name to the target name. */ if (type != FILE_TO_FILE) { /* * Need to remember the roots of traversals to create * correct pathnames. If there's a directory being * copied to a non-existent directory, e.g. * cp -R a/dir noexist * the resulting path name should be noexist/foo, not * noexist/dir/foo (where foo is a file in dir), which * is the case where the target exists. * * Also, check for "..". This is for correct path * concatenation for paths ending in "..", e.g. * cp -R .. /tmp * Paths ending in ".." are changed to ".". This is * tricky, but seems the easiest way to fix the problem. * * XXX * Since the first level MUST be FTS_ROOTLEVEL, base * is always initialized. */ if (curr->fts_level == FTS_ROOTLEVEL) { if (type != DIR_TO_DNE) { p = strrchr(curr->fts_path, '/'); base = (p == NULL) ? 0 : (int)(p - curr->fts_path + 1); if (!strcmp(&curr->fts_path[base], "..")) base += 1; } else base = curr->fts_pathlen; } p = &curr->fts_path[base]; nlen = curr->fts_pathlen - base; target_mid = to.target_end; if (*p != '/' && target_mid[-1] != '/') *target_mid++ = '/'; *target_mid = 0; if (target_mid - to.p_path + nlen >= PATH_MAX) { warnx("%s%s: name too long (not copied)", to.p_path, p); badcp = rval = 1; continue; } (void)strncat(target_mid, p, nlen); to.p_end = target_mid + nlen; *to.p_end = 0; STRIP_TRAILING_SLASH(to); } if (curr->fts_info == FTS_DP) { /* * We are nearly finished with this directory. If we * didn't actually copy it, or otherwise don't need to * change its attributes, then we are done. */ if (!curr->fts_number) continue; /* * If -p is in effect, set all the attributes. * Otherwise, set the correct permissions, limited * by the umask. Optimise by avoiding a chmod() * if possible (which is usually the case if we * made the directory). Note that mkdir() does not * honour setuid, setgid and sticky bits, but we * normally want to preserve them on directories. */ if (pflag) { if (setfile(cp_globals, curr->fts_statp, -1)) rval = 1; if (preserve_dir_acls(curr->fts_statp, curr->fts_accpath, to.p_path) != 0) rval = 1; } else { mode = curr->fts_statp->st_mode; if ((mode & (S_ISUID | S_ISGID | S_ISTXT)) || ((mode | S_IRWXU) & mask) != (mode & mask)) if (chmod(to.p_path, mode & mask) != 0){ warn("chmod: %s", to.p_path); rval = 1; } } continue; } /* Not an error but need to remember it happened */ if (stat(to.p_path, &to_stat) == -1) dne = 1; else { if (to_stat.st_dev == curr->fts_statp->st_dev && to_stat.st_ino == curr->fts_statp->st_ino) { warnx("%s and %s are identical (not copied).", to.p_path, curr->fts_path); badcp = rval = 1; if (S_ISDIR(curr->fts_statp->st_mode)) (void)fts_set(ftsp, curr, FTS_SKIP); continue; } if (!S_ISDIR(curr->fts_statp->st_mode) && S_ISDIR(to_stat.st_mode)) { warnx("cannot overwrite directory %s with " "non-directory %s", to.p_path, curr->fts_path); badcp = rval = 1; continue; } dne = 0; } switch (curr->fts_statp->st_mode & S_IFMT) { case S_IFLNK: /* Catch special case of a non-dangling symlink */ if ((fts_options & FTS_LOGICAL) || ((fts_options & FTS_COMFOLLOW) && curr->fts_level == 0)) { if (copy_file(cp_globals, curr, dne)) badcp = rval = 1; } else { if (copy_link(cp_globals, curr, !dne)) badcp = rval = 1; } break; case S_IFDIR: if (!Rflag) { warnx("%s is a directory (not copied).", curr->fts_path); (void)fts_set(ftsp, curr, FTS_SKIP); badcp = rval = 1; break; } /* * If the directory doesn't exist, create the new * one with the from file mode plus owner RWX bits, * modified by the umask. Trade-off between being * able to write the directory (if from directory is * 555) and not causing a permissions race. If the * umask blocks owner writes, we fail.. */ if (dne) { if (mkdir(to.p_path, curr->fts_statp->st_mode | S_IRWXU) < 0) err(exit_jump, 1, "%s", to.p_path); } else if (!S_ISDIR(to_stat.st_mode)) { errno = ENOTDIR; err(exit_jump, 1, "%s", to.p_path); } /* * Arrange to correct directory attributes later * (in the post-order phase) if this is a new * directory, or if the -p flag is in effect. */ curr->fts_number = pflag || dne; break; case S_IFBLK: case S_IFCHR: if (Rflag) { if (copy_special(cp_globals, curr->fts_statp, !dne)) badcp = rval = 1; } else { if (copy_file(cp_globals, curr, dne)) badcp = rval = 1; } break; case S_IFSOCK: warnx("%s is a socket (not copied).", curr->fts_path); case S_IFIFO: if (Rflag) { if (copy_fifo(cp_globals, curr->fts_statp, !dne)) badcp = rval = 1; } else { if (copy_file(cp_globals, curr, dne)) badcp = rval = 1; } break; default: if (copy_file(cp_globals, curr, dne)) badcp = rval = 1; break; } if (vflag && !badcp) (void)printf("%s -> %s\n", curr->fts_path, to.p_path); } if (errno) err(exit_jump, 1, "fts_read"); fts_close(ftsp); return (rval); }