2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * David Hitz of Auspex Systems Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char const copyright[] =
36 "@(#) Copyright (c) 1988, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)cp.c 8.2 (Berkeley) 4/1/94";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
48 * Cp copies source files to target files.
50 * The global PATH_T structure "to" always contains the path to the
51 * current target file. Since fts(3) does not change directories,
52 * this path can be either absolute or dot-relative.
54 * The basic algorithm is to initialize "to" and use fts(3) to traverse
55 * the file hierarchy rooted in the argument list. A trivial case is the
56 * case of 'cp file1 file2'. The more interesting case is the case of
57 * 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the
58 * path (relative to the root of the traversal) is appended to dir (stored
59 * in "to") to form the final target path.
62 #include <sys/types.h>
77 #define STRIP_TRAILING_SLASH(p) { \
78 while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \
82 static char emptystring[] = "";
84 PATH_T to = { to.p_path, emptystring, "" };
86 int fflag, iflag, lflag, nflag, pflag, vflag;
87 static int Rflag, rflag;
88 volatile sig_atomic_t info;
90 enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
92 static int copy(char *[], enum op, int);
93 static int mastercmp(const FTSENT * const *, const FTSENT * const *);
94 static void siginfo(int __unused);
97 main(int argc, char *argv[])
99 struct stat to_stat, tmp_stat;
101 int Hflag, Lflag, ch, fts_options, r, have_trailing_slash;
104 fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
106 while ((ch = getopt(argc, argv, "HLPRafilnprvx")) != -1)
153 fts_options |= FTS_XDEV;
166 errx(1, "the -R and -r options may not be specified together");
171 fts_options |= FTS_COMFOLLOW;
173 fts_options &= ~FTS_PHYSICAL;
174 fts_options |= FTS_LOGICAL;
177 fts_options &= ~FTS_PHYSICAL;
178 fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
180 (void)signal(SIGINFO, siginfo);
182 /* Save the target base in "to". */
183 target = argv[--argc];
184 if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path))
185 errx(1, "%s: name too long", target);
186 to.p_end = to.p_path + strlen(to.p_path);
187 if (to.p_path == to.p_end) {
191 have_trailing_slash = (to.p_end[-1] == '/');
192 if (have_trailing_slash)
193 STRIP_TRAILING_SLASH(to);
194 to.target_end = to.p_end;
196 /* Set end of argument list for fts(3). */
200 * Cp has two distinct cases:
202 * cp [-R] source target
203 * cp [-R] source1 ... sourceN directory
205 * In both cases, source can be either a file or a directory.
207 * In (1), the target becomes a copy of the source. That is, if the
208 * source is a file, the target will be a file, and likewise for
211 * In (2), the real target is not directory, but "directory/source".
213 r = stat(to.p_path, &to_stat);
214 if (r == -1 && errno != ENOENT)
215 err(1, "%s", to.p_path);
216 if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
218 * Case (1). Target is not a directory.
221 errx(1, "%s is not a directory", to.p_path);
224 * Need to detect the case:
226 * Where dir is a directory and foo does not exist, where
227 * we want pathname concatenations turned on but not for
228 * the initial mkdir().
231 if (Rflag && (Lflag || Hflag))
232 stat(*argv, &tmp_stat);
234 lstat(*argv, &tmp_stat);
236 if (S_ISDIR(tmp_stat.st_mode) && Rflag)
243 if (have_trailing_slash && type == FILE_TO_FILE) {
245 errx(1, "directory %s does not exist",
248 errx(1, "%s is not a directory", to.p_path);
252 * Case (2). Target is a directory.
256 exit (copy(argv, type, fts_options));
260 copy(char *argv[], enum op type, int fts_options)
265 int base = 0, dne, badcp, rval;
267 char *p, *target_mid;
271 * Keep an inverted copy of the umask, for use in correcting
272 * permissions on created directories when not using -p.
277 if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
279 for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) {
280 switch (curr->fts_info) {
285 curr->fts_path, strerror(curr->fts_errno));
288 case FTS_DC: /* Warn, continue. */
289 warnx("%s: directory causes a cycle", curr->fts_path);
297 * If we are in case (2) or (3) above, we need to append the
298 * source name to the target name.
300 if (type != FILE_TO_FILE) {
302 * Need to remember the roots of traversals to create
303 * correct pathnames. If there's a directory being
304 * copied to a non-existent directory, e.g.
305 * cp -R a/dir noexist
306 * the resulting path name should be noexist/foo, not
307 * noexist/dir/foo (where foo is a file in dir), which
308 * is the case where the target exists.
310 * Also, check for "..". This is for correct path
311 * concatenation for paths ending in "..", e.g.
313 * Paths ending in ".." are changed to ".". This is
314 * tricky, but seems the easiest way to fix the problem.
317 * Since the first level MUST be FTS_ROOTLEVEL, base
318 * is always initialized.
320 if (curr->fts_level == FTS_ROOTLEVEL) {
321 if (type != DIR_TO_DNE) {
322 p = strrchr(curr->fts_path, '/');
323 base = (p == NULL) ? 0 :
324 (int)(p - curr->fts_path + 1);
326 if (!strcmp(&curr->fts_path[base],
330 base = curr->fts_pathlen;
333 p = &curr->fts_path[base];
334 nlen = curr->fts_pathlen - base;
335 target_mid = to.target_end;
336 if (*p != '/' && target_mid[-1] != '/')
339 if (target_mid - to.p_path + nlen >= PATH_MAX) {
340 warnx("%s%s: name too long (not copied)",
345 (void)strncat(target_mid, p, nlen);
346 to.p_end = target_mid + nlen;
348 STRIP_TRAILING_SLASH(to);
351 if (curr->fts_info == FTS_DP) {
353 * We are nearly finished with this directory. If we
354 * didn't actually copy it, or otherwise don't need to
355 * change its attributes, then we are done.
357 if (!curr->fts_number)
360 * If -p is in effect, set all the attributes.
361 * Otherwise, set the correct permissions, limited
362 * by the umask. Optimise by avoiding a chmod()
363 * if possible (which is usually the case if we
364 * made the directory). Note that mkdir() does not
365 * honour setuid, setgid and sticky bits, but we
366 * normally want to preserve them on directories.
369 if (setfile(curr->fts_statp, -1))
371 if (preserve_dir_acls(curr->fts_statp,
372 curr->fts_accpath, to.p_path) != 0)
375 mode = curr->fts_statp->st_mode;
376 if ((mode & (S_ISUID | S_ISGID | S_ISTXT)) ||
377 ((mode | S_IRWXU) & mask) != (mode & mask))
378 if (chmod(to.p_path, mode & mask) != 0){
379 warn("chmod: %s", to.p_path);
386 /* Not an error but need to remember it happened */
387 if (stat(to.p_path, &to_stat) == -1)
390 if (to_stat.st_dev == curr->fts_statp->st_dev &&
391 to_stat.st_ino == curr->fts_statp->st_ino) {
392 warnx("%s and %s are identical (not copied).",
393 to.p_path, curr->fts_path);
395 if (S_ISDIR(curr->fts_statp->st_mode))
396 (void)fts_set(ftsp, curr, FTS_SKIP);
399 if (!S_ISDIR(curr->fts_statp->st_mode) &&
400 S_ISDIR(to_stat.st_mode)) {
401 warnx("cannot overwrite directory %s with "
403 to.p_path, curr->fts_path);
410 switch (curr->fts_statp->st_mode & S_IFMT) {
412 /* Catch special case of a non-dangling symlink */
413 if ((fts_options & FTS_LOGICAL) ||
414 ((fts_options & FTS_COMFOLLOW) &&
415 curr->fts_level == 0)) {
416 if (copy_file(curr, dne))
419 if (copy_link(curr, !dne))
425 warnx("%s is a directory (not copied).",
427 (void)fts_set(ftsp, curr, FTS_SKIP);
432 * If the directory doesn't exist, create the new
433 * one with the from file mode plus owner RWX bits,
434 * modified by the umask. Trade-off between being
435 * able to write the directory (if from directory is
436 * 555) and not causing a permissions race. If the
437 * umask blocks owner writes, we fail..
441 curr->fts_statp->st_mode | S_IRWXU) < 0)
442 err(1, "%s", to.p_path);
443 } else if (!S_ISDIR(to_stat.st_mode)) {
445 err(1, "%s", to.p_path);
448 * Arrange to correct directory attributes later
449 * (in the post-order phase) if this is a new
450 * directory, or if the -p flag is in effect.
452 curr->fts_number = pflag || dne;
457 if (copy_special(curr->fts_statp, !dne))
460 if (copy_file(curr, dne))
465 warnx("%s is a socket (not copied).",
470 if (copy_fifo(curr->fts_statp, !dne))
473 if (copy_file(curr, dne))
478 if (copy_file(curr, dne))
483 (void)printf("%s -> %s\n", curr->fts_path, to.p_path);
493 * The comparison function for the copy order. The order is to copy
494 * non-directory files before directory files. The reason for this
495 * is because files tend to be in the same cylinder group as their
496 * parent directory, whereas directories tend not to be. Copying the
497 * files first reduces seeking.
500 mastercmp(const FTSENT * const *a, const FTSENT * const *b)
504 a_info = (*a)->fts_info;
505 if (a_info == FTS_ERR || a_info == FTS_NS || a_info == FTS_DNR)
507 b_info = (*b)->fts_info;
508 if (b_info == FTS_ERR || b_info == FTS_NS || b_info == FTS_DNR)
518 siginfo(int sig __unused)