2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1988, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * David Hitz of Auspex Systems Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char const copyright[] =
38 "@(#) Copyright (c) 1988, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)cp.c 8.2 (Berkeley) 4/1/94";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
50 * Cp copies source files to target files.
52 * The global PATH_T structure "to" always contains the path to the
53 * current target file. Since fts(3) does not change directories,
54 * this path can be either absolute or dot-relative.
56 * The basic algorithm is to initialize "to" and use fts(3) to traverse
57 * the file hierarchy rooted in the argument list. A trivial case is the
58 * case of 'cp file1 file2'. The more interesting case is the case of
59 * 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the
60 * path (relative to the root of the traversal) is appended to dir (stored
61 * in "to") to form the final target path.
64 #include <sys/types.h>
79 #define STRIP_TRAILING_SLASH(p) { \
80 while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \
84 static char emptystring[] = "";
86 PATH_T to = { to.p_path, emptystring, "" };
88 int fflag, iflag, lflag, nflag, pflag, sflag, vflag;
89 static int Hflag, Lflag, Rflag, rflag;
90 volatile sig_atomic_t info;
92 enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
94 static int copy(char *[], enum op, int);
95 static void siginfo(int __unused);
98 main(int argc, char *argv[])
100 struct stat to_stat, tmp_stat;
102 int Pflag, ch, fts_options, r, have_trailing_slash;
105 fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
107 while ((ch = getopt(argc, argv, "HLPRafilnprsvx")) != -1)
159 fts_options |= FTS_XDEV;
172 errx(1, "the -R and -r options may not be specified together");
174 errx(1, "the -l and -s options may not be specified together");
179 fts_options |= FTS_COMFOLLOW;
181 fts_options &= ~FTS_PHYSICAL;
182 fts_options |= FTS_LOGICAL;
185 fts_options &= ~FTS_PHYSICAL;
186 fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
188 (void)signal(SIGINFO, siginfo);
190 /* Save the target base in "to". */
191 target = argv[--argc];
192 if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path))
193 errx(1, "%s: name too long", target);
194 to.p_end = to.p_path + strlen(to.p_path);
195 if (to.p_path == to.p_end) {
199 have_trailing_slash = (to.p_end[-1] == '/');
200 if (have_trailing_slash)
201 STRIP_TRAILING_SLASH(to);
202 to.target_end = to.p_end;
204 /* Set end of argument list for fts(3). */
208 * Cp has two distinct cases:
210 * cp [-R] source target
211 * cp [-R] source1 ... sourceN directory
213 * In both cases, source can be either a file or a directory.
215 * In (1), the target becomes a copy of the source. That is, if the
216 * source is a file, the target will be a file, and likewise for
219 * In (2), the real target is not directory, but "directory/source".
221 r = stat(to.p_path, &to_stat);
222 if (r == -1 && errno != ENOENT)
223 err(1, "%s", to.p_path);
224 if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
226 * Case (1). Target is not a directory.
229 errx(1, "%s is not a directory", to.p_path);
232 * Need to detect the case:
234 * Where dir is a directory and foo does not exist, where
235 * we want pathname concatenations turned on but not for
236 * the initial mkdir().
239 if (Rflag && (Lflag || Hflag))
240 stat(*argv, &tmp_stat);
242 lstat(*argv, &tmp_stat);
244 if (S_ISDIR(tmp_stat.st_mode) && Rflag)
251 if (have_trailing_slash && type == FILE_TO_FILE) {
253 errx(1, "directory %s does not exist",
256 errx(1, "%s is not a directory", to.p_path);
260 * Case (2). Target is a directory.
264 exit (copy(argv, type, fts_options));
267 /* Does the right thing based on -R + -H/-L/-P */
269 copy_stat(const char *path, struct stat *sb)
273 * For -R -H/-P, we need to lstat() instead; copy() cares about the link
274 * itself rather than the target if we're not following links during the
278 return (stat(path, sb));
279 return (lstat(path, sb));
284 copy(char *argv[], enum op type, int fts_options)
289 int base = 0, dne, badcp, rval;
291 char *p, *target_mid;
295 * Keep an inverted copy of the umask, for use in correcting
296 * permissions on created directories when not using -p.
301 if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL)
303 for (badcp = rval = 0; errno = 0, (curr = fts_read(ftsp)) != NULL;
305 switch (curr->fts_info) {
310 curr->fts_path, strerror(curr->fts_errno));
313 case FTS_DC: /* Warn, continue. */
314 warnx("%s: directory causes a cycle", curr->fts_path);
322 * If we are in case (2) or (3) above, we need to append the
323 * source name to the target name.
325 if (type != FILE_TO_FILE) {
327 * Need to remember the roots of traversals to create
328 * correct pathnames. If there's a directory being
329 * copied to a non-existent directory, e.g.
330 * cp -R a/dir noexist
331 * the resulting path name should be noexist/foo, not
332 * noexist/dir/foo (where foo is a file in dir), which
333 * is the case where the target exists.
335 * Also, check for "..". This is for correct path
336 * concatenation for paths ending in "..", e.g.
338 * Paths ending in ".." are changed to ".". This is
339 * tricky, but seems the easiest way to fix the problem.
342 * Since the first level MUST be FTS_ROOTLEVEL, base
343 * is always initialized.
345 if (curr->fts_level == FTS_ROOTLEVEL) {
346 if (type != DIR_TO_DNE) {
347 p = strrchr(curr->fts_path, '/');
348 base = (p == NULL) ? 0 :
349 (int)(p - curr->fts_path + 1);
351 if (!strcmp(&curr->fts_path[base],
355 base = curr->fts_pathlen;
358 p = &curr->fts_path[base];
359 nlen = curr->fts_pathlen - base;
360 target_mid = to.target_end;
361 if (*p != '/' && target_mid[-1] != '/')
364 if (target_mid - to.p_path + nlen >= PATH_MAX) {
365 warnx("%s%s: name too long (not copied)",
370 (void)strncat(target_mid, p, nlen);
371 to.p_end = target_mid + nlen;
373 STRIP_TRAILING_SLASH(to);
376 if (curr->fts_info == FTS_DP) {
378 * We are nearly finished with this directory. If we
379 * didn't actually copy it, or otherwise don't need to
380 * change its attributes, then we are done.
382 if (!curr->fts_number)
385 * If -p is in effect, set all the attributes.
386 * Otherwise, set the correct permissions, limited
387 * by the umask. Optimise by avoiding a chmod()
388 * if possible (which is usually the case if we
389 * made the directory). Note that mkdir() does not
390 * honour setuid, setgid and sticky bits, but we
391 * normally want to preserve them on directories.
394 if (setfile(curr->fts_statp, -1))
396 if (preserve_dir_acls(curr->fts_statp,
397 curr->fts_accpath, to.p_path) != 0)
400 mode = curr->fts_statp->st_mode;
401 if ((mode & (S_ISUID | S_ISGID | S_ISTXT)) ||
402 ((mode | S_IRWXU) & mask) != (mode & mask))
403 if (chmod(to.p_path, mode & mask) !=
405 warn("chmod: %s", to.p_path);
412 /* Not an error but need to remember it happened. */
413 if (copy_stat(to.p_path, &to_stat) == -1)
416 if (to_stat.st_dev == curr->fts_statp->st_dev &&
417 to_stat.st_ino == curr->fts_statp->st_ino) {
418 warnx("%s and %s are identical (not copied).",
419 to.p_path, curr->fts_path);
421 if (S_ISDIR(curr->fts_statp->st_mode))
422 (void)fts_set(ftsp, curr, FTS_SKIP);
425 if (!S_ISDIR(curr->fts_statp->st_mode) &&
426 S_ISDIR(to_stat.st_mode)) {
427 warnx("cannot overwrite directory %s with "
429 to.p_path, curr->fts_path);
436 switch (curr->fts_statp->st_mode & S_IFMT) {
438 /* Catch special case of a non-dangling symlink. */
439 if ((fts_options & FTS_LOGICAL) ||
440 ((fts_options & FTS_COMFOLLOW) &&
441 curr->fts_level == 0)) {
442 if (copy_file(curr, dne))
445 if (copy_link(curr, !dne))
451 warnx("%s is a directory (not copied).",
453 (void)fts_set(ftsp, curr, FTS_SKIP);
458 * If the directory doesn't exist, create the new
459 * one with the from file mode plus owner RWX bits,
460 * modified by the umask. Trade-off between being
461 * able to write the directory (if from directory is
462 * 555) and not causing a permissions race. If the
463 * umask blocks owner writes, we fail.
467 curr->fts_statp->st_mode | S_IRWXU) < 0)
468 err(1, "%s", to.p_path);
469 } else if (!S_ISDIR(to_stat.st_mode)) {
471 err(1, "%s", to.p_path);
474 * Arrange to correct directory attributes later
475 * (in the post-order phase) if this is a new
476 * directory, or if the -p flag is in effect.
478 curr->fts_number = pflag || dne;
482 if (Rflag && !sflag) {
483 if (copy_special(curr->fts_statp, !dne))
486 if (copy_file(curr, dne))
491 warnx("%s is a socket (not copied).",
495 if (Rflag && !sflag) {
496 if (copy_fifo(curr->fts_statp, !dne))
499 if (copy_file(curr, dne))
504 if (copy_file(curr, dne))
509 (void)printf("%s -> %s\n", curr->fts_path, to.p_path);
518 siginfo(int sig __unused)