1 /* Copyright 2008 Rob Landley <rob@landley.net>
3 * See http://opengroup.org/onlinepubs/9699919799/utilities/cp.html
5 * Posix says "cp -Rf dir file" shouldn't delete file, but our -f does.
7 // This is subtle: MV options must be in same order (right to left) as CP
8 // for FLAG_X macros to work out right.
10 USE_CP(NEWTOY(cp, "<2RHLPp"USE_CP_MORE("rdaslvn")"fi[-HLPd]"USE_CP_MORE("[-ni]"), TOYFLAG_BIN))
11 USE_CP_MV(OLDTOY(mv, cp, "<2"USE_CP_MORE("vn")"fi"USE_CP_MORE("[-ni]"), TOYFLAG_BIN))
17 usage: cp [-fipRHLP] SOURCE... DEST
19 Copy files from SOURCE to DEST. If more than one SOURCE, DEST must
22 -f force copy by deleting destination file
23 -i interactive, prompt before overwriting existing DEST
24 -p preserve timestamps, ownership, and permissions
25 -R recurse into subdirectories (DEST must be a directory)
26 -H Follow symlinks listed on command line
27 -L Follow all symlinks
28 -P Do not follow symlinks [default]
31 bool "cp -adlnrsv options"
38 -d don't dereference symlinks
39 -l hard link instead of copy
40 -n no clobber (don't overwrite DEST)
42 -s symlink instead of copy
50 usage: mv [-fi] SOURCE... DEST"
52 -f force copy by deleting destination file
53 -i interactive, prompt before overwriting existing DEST
58 depends on CP_MV && CP_MORE
63 -n no clobber (don't overwrite DEST)
74 // Callback from dirtree_read() for each file/directory under a source dir.
76 int cp_node(struct dirtree *try)
78 int fdout = -1, cfd = try->parent ? try->parent->extra : AT_FDCWD,
79 tfd = dirtree_parentfd(try);
80 unsigned flags = toys.optflags;
81 char *catch = try->parent ? try->name : TT.destname, *err = "%s";
84 if (!dirtree_notdotdot(try)) return 0;
86 // If returning from COMEAGAIN, jump straight to -p logic at end.
87 if (S_ISDIR(try->st.st_mode) && try->again) {
92 // -d is only the same as -r for symlinks, not for directories
93 if (S_ISLNK(try->st.st_mode) & (flags & FLAG_d)) flags |= FLAG_r;
95 // Detect recursive copies via repeated top node (cp -R .. .) or
96 // identical source/target (fun with hardlinks).
97 if ((TT.top.st_dev == try->st.st_dev && TT.top.st_ino == try->st.st_ino
98 && (catch = TT.destname))
99 || (!fstatat(cfd, catch, &cst, 0) && cst.st_dev == try->st.st_dev
100 && cst.st_ino == try->st.st_ino))
102 error_msg("'%s' is '%s'", catch, err = dirtree_path(try, 0));
110 if (!faccessat(cfd, catch, F_OK, 0) && !S_ISDIR(cst.st_mode)) {
113 if (S_ISDIR(try->st.st_dev)) {
114 error_msg("dir at '%s'", s = dirtree_path(try, 0));
117 } else if (flags & FLAG_n) return 0;
118 else if (flags & FLAG_i) {
119 fprintf(stderr, "cp: overwrite '%s'", s = dirtree_path(try, 0));
121 if (!yesno("", 1)) return 0;
125 if (flags & FLAG_v) {
126 char *s = dirtree_path(try, 0);
127 printf("cp '%s'\n", s);
131 // Loop for -f retry after unlink
134 // directory, hardlink, symlink, mknod (char, block, fifo, socket), file
138 if (S_ISDIR(try->st.st_mode)) {
141 if (!(flags & (FLAG_a|FLAG_r|FLAG_R))) {
142 err = "Skipped dir '%s'";
147 // Always make directory writeable to us, so we can create files in it.
149 // Yes, there's a race window between mkdir() and open() so it's
150 // possible that -p can be made to chown a directory other than the one
151 // we created. The closest we can do to closing this is make sure
152 // that what we open _is_ a directory rather than something else.
154 if (!mkdirat(cfd, catch, try->st.st_mode | 0200) || errno == EEXIST)
155 if (-1 != (try->extra = openat(cfd, catch, O_NOFOLLOW)))
156 if (!fstat(try->extra, &st2) && S_ISDIR(st2.st_mode))
157 return DIRTREE_COMEAGAIN
158 | (DIRTREE_SYMFOLLOW*!!(toys.optflags&FLAG_L));
162 } else if (flags & FLAG_l) {
163 if (!linkat(tfd, try->name, cfd, catch, 0)) err = 0;
165 // Copy tree as symlinks. For non-absolute paths this involves
166 // appending the right number of .. entries as you go down the tree.
168 } else if (flags & FLAG_s) {
173 s = dirtree_path(try, 0);
174 for (or = try; or->parent; or = or->parent) dotdots++;
176 if (*or->name == '/') dotdots = 0;
178 char *s2 = xmprintf("% *c%s", 3*dotdots, ' ', s);
182 memcpy(s2, "../", 3);
186 if (!symlinkat(s, cfd, catch)) {
192 // Do something _other_ than copy contents of a file?
193 } else if (!S_ISREG(try->st.st_mode)
194 && (try->parent || (flags & (FLAG_a|FLAG_r))))
198 // make symlink, or make block/char/fifo/socket
199 if (S_ISLNK(try->st.st_mode)
200 ? (0 < (i = readlinkat(tfd, try->name, toybuf, sizeof(toybuf))) &&
201 sizeof(toybuf) > i && !symlinkat(toybuf, cfd, catch))
202 : !mknodat(cfd, catch, try->st.st_mode, try->st.st_rdev))
208 // Copy contents of file.
212 fdin = openat(tfd, try->name, O_RDONLY);
217 fdout = openat(cfd, catch, O_RDWR|O_CREAT|O_TRUNC, try->st.st_mode);
219 xsendfile(fdin, fdout);
225 } while (err && (flags & (FLAG_f|FLAG_n)) && !unlinkat(cfd, catch, 0));
229 if (flags & (FLAG_a|FLAG_p)) {
230 struct timespec times[2];
232 // Inability to set these isn't fatal, some require root access.
234 times[0] = try->st.st_atim;
235 times[1] = try->st.st_mtim;
237 // If we can't get a filehandle to the actual object, use racy functions
238 if (fdout == AT_FDCWD) {
239 fchownat(cfd, catch, try->st.st_uid, try->st.st_gid,
240 AT_SYMLINK_NOFOLLOW);
241 utimensat(cfd, catch, times, AT_SYMLINK_NOFOLLOW);
242 // permission bits already correct for mknod, don't apply to symlink
244 fchown(fdout, try->st.st_uid, try->st.st_gid);
245 futimens(fdout, times);
246 fchmod(fdout, try->st.st_mode);
250 if (fdout != AT_FDCWD) xclose(fdout);
252 if (toys.which->name[0] == 'm')
253 if (unlinkat(tfd, try->name, S_ISDIR(try->st.st_mode) ? AT_REMOVEDIR : 0))
257 if (err) perror_msg(err, catch);
263 char *destname = toys.optargs[--toys.optc];
264 int i, destdir = !stat(destname, &TT.top) && S_ISDIR(TT.top.st_mode);
266 if (toys.optc>1 && !destdir) error_exit("'%s' not directory", destname);
267 if (toys.which->name[0] == 'm') toys.optflags |= FLAG_d|FLAG_p|FLAG_R;
268 if (toys.optflags & (FLAG_a|FLAG_p)) umask(0);
270 // Loop through sources
272 for (i=0; i<toys.optc; i++) {
274 char *src = toys.optargs[i];
277 if (destdir) TT.destname = xmprintf("%s/%s", destname, basename(src));
278 else TT.destname = destname;
281 if (toys.which->name[0] == 'm') rc = rename(src, TT.destname);
283 // Skip nonexistent sources
285 int symfollow = toys.optflags & (FLAG_H|FLAG_L);
287 if (errno != EXDEV || !(new = dirtree_add_node(0, src, symfollow)))
288 perror_msg("bad '%s'", src);
289 else dirtree_handle_callback(new, cp_node);
291 if (destdir) free(TT.destname);