]> git.saurik.com Git - apple/file_cmds.git/blame - cp/cp.c
file_cmds-242.tar.gz
[apple/file_cmds.git] / cp / cp.c
CommitLineData
864a4b6e 1/*-
44a7a5ab
A
2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * David Hitz of Auspex Systems Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
44a7a5ab
A
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.
19 *
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
30 * SUCH DAMAGE.
31 */
32
864a4b6e 33#if 0
44a7a5ab 34#ifndef lint
6c780a1f 35static char const copyright[] =
44a7a5ab 36"@(#) Copyright (c) 1988, 1993, 1994\n\
6c780a1f 37 The Regents of the University of California. All rights reserved.\n";
44a7a5ab
A
38#endif /* not lint */
39
40#ifndef lint
6c780a1f 41static char sccsid[] = "@(#)cp.c 8.2 (Berkeley) 4/1/94";
44a7a5ab 42#endif /* not lint */
864a4b6e 43#endif
6c780a1f 44#include <sys/cdefs.h>
864a4b6e 45__FBSDID("$FreeBSD: src/bin/cp/cp.c,v 1.52 2005/09/05 04:36:08 csjp Exp $");
44a7a5ab
A
46
47/*
48 * Cp copies source files to target files.
6c780a1f 49 *
44a7a5ab
A
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.
6c780a1f 53 *
44a7a5ab
A
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.
60 */
61
864a4b6e 62#include <sys/types.h>
44a7a5ab 63#include <sys/stat.h>
44a7a5ab 64
44a7a5ab
A
65#include <err.h>
66#include <errno.h>
44a7a5ab 67#include <fts.h>
6c780a1f 68#include <limits.h>
864a4b6e 69#include <signal.h>
44a7a5ab
A
70#include <stdio.h>
71#include <stdlib.h>
72#include <string.h>
73#include <unistd.h>
74
c59d3020
A
75#ifdef __APPLE__
76#include <copyfile.h>
864a4b6e
A
77#include <get_compat.h>
78#else /* !__APPLE__ */
79#define COMPAT_MODE(a,b) (1)
80#endif /* __APPLE__ */
c59d3020 81
44a7a5ab
A
82#include "extern.h"
83
84#define STRIP_TRAILING_SLASH(p) { \
85 while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \
86 *--(p).p_end = 0; \
87}
88
6c780a1f
A
89static char emptystring[] = "";
90
91PATH_T to = { to.p_path, emptystring, "" };
44a7a5ab 92
6c780a1f 93int fflag, iflag, nflag, pflag, vflag;
864a4b6e
A
94#ifdef __APPLE__
95int Xflag;
96#endif /* __APPLE__ */
6c780a1f 97static int Rflag, rflag;
864a4b6e 98volatile sig_atomic_t info;
44a7a5ab
A
99
100enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
101
6c780a1f 102static int copy(char *[], enum op, int);
864a4b6e 103static void siginfo(int __unused);
44a7a5ab
A
104
105int
6c780a1f 106main(int argc, char *argv[])
44a7a5ab
A
107{
108 struct stat to_stat, tmp_stat;
109 enum op type;
6c780a1f 110 int Hflag, Lflag, Pflag, ch, fts_options, r, have_trailing_slash;
44a7a5ab
A
111 char *target;
112
6c780a1f 113 Hflag = Lflag = Pflag = 0;
40bf83fe 114 while ((ch = getopt(argc, argv, "HLPRXafinprv")) != -1)
44a7a5ab
A
115 switch (ch) {
116 case 'H':
117 Hflag = 1;
118 Lflag = Pflag = 0;
119 break;
120 case 'L':
121 Lflag = 1;
122 Hflag = Pflag = 0;
123 break;
124 case 'P':
125 Pflag = 1;
126 Hflag = Lflag = 0;
127 break;
128 case 'R':
129 Rflag = 1;
130 break;
864a4b6e
A
131 case 'X':
132 Xflag = 1;
133 break;
44a7a5ab
A
134 case 'f':
135 fflag = 1;
c59d3020 136 /* Determine if the STD is SUSv3 or Legacy */
864a4b6e 137 if (COMPAT_MODE("bin/cp", "unix2003"))
c59d3020
A
138 nflag = 0; /* reset nflag, but not iflag */
139 else
140 iflag = nflag = 0; /* reset both */
44a7a5ab
A
141 break;
142 case 'i':
6c780a1f 143 iflag = 1;
864a4b6e
A
144 if (COMPAT_MODE("bin/cp", "unix2003"))
145 nflag = 0; /* reset nflag, but not fflag */
146 else
147 fflag = nflag = 0;
6c780a1f
A
148 break;
149 case 'n':
150 nflag = 1;
151 fflag = iflag = 0;
44a7a5ab
A
152 break;
153 case 'p':
154 pflag = 1;
155 break;
156 case 'r':
157 rflag = 1;
158 break;
6c780a1f
A
159 case 'v':
160 vflag = 1;
161 break;
40bf83fe
A
162 case 'a':
163 pflag = 1;
164 Pflag = 1;
165 Rflag = 1;
166 break;
44a7a5ab
A
167 default:
168 usage();
169 break;
170 }
171 argc -= optind;
172 argv += optind;
173
174 if (argc < 2)
175 usage();
176
177 fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
178 if (rflag) {
179 if (Rflag)
180 errx(1,
181 "the -R and -r options may not be specified together.");
182 if (Hflag || Lflag || Pflag)
183 errx(1,
184 "the -H, -L, and -P options may not be specified with the -r option.");
185 fts_options &= ~FTS_PHYSICAL;
186 fts_options |= FTS_LOGICAL;
187 }
188 if (Rflag) {
189 if (Hflag)
190 fts_options |= FTS_COMFOLLOW;
191 if (Lflag) {
192 fts_options &= ~FTS_PHYSICAL;
193 fts_options |= FTS_LOGICAL;
194 }
195 } else {
196 fts_options &= ~FTS_PHYSICAL;
6c780a1f 197 fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
44a7a5ab 198 }
864a4b6e 199 (void)signal(SIGINFO, siginfo);
44a7a5ab 200
44a7a5ab
A
201 /* Save the target base in "to". */
202 target = argv[--argc];
6c780a1f 203 if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path))
44a7a5ab 204 errx(1, "%s: name too long", target);
44a7a5ab
A
205 to.p_end = to.p_path + strlen(to.p_path);
206 if (to.p_path == to.p_end) {
207 *to.p_end++ = '.';
208 *to.p_end = 0;
209 }
6c780a1f
A
210 have_trailing_slash = (to.p_end[-1] == '/');
211 if (have_trailing_slash)
212 STRIP_TRAILING_SLASH(to);
44a7a5ab
A
213 to.target_end = to.p_end;
214
215 /* Set end of argument list for fts(3). */
6c780a1f
A
216 argv[argc] = NULL;
217
44a7a5ab
A
218 /*
219 * Cp has two distinct cases:
220 *
221 * cp [-R] source target
222 * cp [-R] source1 ... sourceN directory
223 *
224 * In both cases, source can be either a file or a directory.
225 *
226 * In (1), the target becomes a copy of the source. That is, if the
227 * source is a file, the target will be a file, and likewise for
228 * directories.
229 *
230 * In (2), the real target is not directory, but "directory/source".
231 */
232 r = stat(to.p_path, &to_stat);
233 if (r == -1 && errno != ENOENT)
234 err(1, "%s", to.p_path);
235 if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
236 /*
237 * Case (1). Target is not a directory.
6c780a1f 238 */
44a7a5ab
A
239 if (argc > 1) {
240 usage();
241 exit(1);
242 }
243 /*
244 * Need to detect the case:
245 * cp -R dir foo
246 * Where dir is a directory and foo does not exist, where
247 * we want pathname concatenations turned on but not for
248 * the initial mkdir().
249 */
250 if (r == -1) {
251 if (rflag || (Rflag && (Lflag || Hflag)))
6c780a1f 252 stat(*argv, &tmp_stat);
44a7a5ab 253 else
6c780a1f
A
254 lstat(*argv, &tmp_stat);
255
44a7a5ab
A
256 if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag))
257 type = DIR_TO_DNE;
258 else
259 type = FILE_TO_FILE;
260 } else
261 type = FILE_TO_FILE;
6c780a1f
A
262
263 if (have_trailing_slash && type == FILE_TO_FILE) {
264 if (r == -1)
265 errx(1, "directory %s does not exist",
266 to.p_path);
267 else
268 errx(1, "%s is not a directory", to.p_path);
269 }
270 } else
44a7a5ab
A
271 /*
272 * Case (2). Target is a directory.
273 */
274 type = FILE_TO_DIR;
44a7a5ab
A
275
276 exit (copy(argv, type, fts_options));
44a7a5ab
A
277}
278
864a4b6e 279static int
6c780a1f 280copy(char *argv[], enum op type, int fts_options)
44a7a5ab
A
281{
282 struct stat to_stat;
283 FTS *ftsp;
284 FTSENT *curr;
6c780a1f
A
285 int base = 0, dne, badcp, rval;
286 size_t nlen;
287 char *p, *target_mid;
288 mode_t mask, mode;
44a7a5ab 289
6c780a1f
A
290 /*
291 * Keep an inverted copy of the umask, for use in correcting
292 * permissions on created directories when not using -p.
293 */
294 mask = ~umask(0777);
295 umask(~mask);
44a7a5ab 296
64d2f73f 297 if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL)
6c780a1f
A
298 err(1, "fts_open");
299 for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) {
44a7a5ab
A
300 switch (curr->fts_info) {
301 case FTS_NS:
6c780a1f 302 case FTS_DNR:
44a7a5ab
A
303 case FTS_ERR:
304 warnx("%s: %s",
305 curr->fts_path, strerror(curr->fts_errno));
4d0bb651 306 rval = 1;
44a7a5ab
A
307 continue;
308 case FTS_DC: /* Warn, continue. */
309 warnx("%s: directory causes a cycle", curr->fts_path);
4d0bb651 310 rval = 1;
44a7a5ab 311 continue;
6c780a1f
A
312 default:
313 ;
44a7a5ab 314 }
40bf83fe 315#ifdef __APPLE__
4d0bb651
A
316
317#ifdef __clang__
318#pragma clang diagnostic push
319/* clang doesn't like fts_name[1], but we know better... */
320#pragma clang diagnostic ignored "-Warray-bounds"
321#endif
40bf83fe
A
322 /* Skip ._<file> when using copyfile and <file> exists */
323 if ((pflag || !Xflag) && (curr->fts_level != FTS_ROOTLEVEL) &&
324 (curr->fts_namelen > 2) && /* ._\0 is not AppleDouble */
325 (curr->fts_name[0] == '.') && (curr->fts_name[1] == '_')) {
4d0bb651
A
326#ifdef __clang__
327#pragma clang diagnostic pop
328#endif
40bf83fe
A
329 struct stat statbuf;
330 char path[PATH_MAX];
331 char *p = strrchr(curr->fts_path, '/');
332 if (p) {
333 size_t s = p + 2 - curr->fts_path;
334 if (s > sizeof(path)) s = sizeof(path);
335 strlcpy(path, curr->fts_path, s);
336 strlcat(path, curr->fts_name+2, sizeof(path));
337 } else {
338 strlcpy(path, curr->fts_name+2, sizeof(path));
339 }
340 if (!lstat(path, &statbuf)) {
341 continue;
342 }
343 }
344#endif /* __APPLE__ */
44a7a5ab 345 /*
6c780a1f
A
346 * If we are in case (2) or (3) above, we need to append the
347 * source name to the target name.
44a7a5ab
A
348 */
349 if (type != FILE_TO_FILE) {
44a7a5ab
A
350 /*
351 * Need to remember the roots of traversals to create
352 * correct pathnames. If there's a directory being
353 * copied to a non-existent directory, e.g.
354 * cp -R a/dir noexist
355 * the resulting path name should be noexist/foo, not
356 * noexist/dir/foo (where foo is a file in dir), which
357 * is the case where the target exists.
358 *
359 * Also, check for "..". This is for correct path
6c780a1f 360 * concatenation for paths ending in "..", e.g.
44a7a5ab
A
361 * cp -R .. /tmp
362 * Paths ending in ".." are changed to ".". This is
363 * tricky, but seems the easiest way to fix the problem.
364 *
365 * XXX
366 * Since the first level MUST be FTS_ROOTLEVEL, base
367 * is always initialized.
368 */
369 if (curr->fts_level == FTS_ROOTLEVEL) {
370 if (type != DIR_TO_DNE) {
371 p = strrchr(curr->fts_path, '/');
6c780a1f 372 base = (p == NULL) ? 0 :
44a7a5ab
A
373 (int)(p - curr->fts_path + 1);
374
6c780a1f 375 if (!strcmp(&curr->fts_path[base],
44a7a5ab
A
376 ".."))
377 base += 1;
378 } else
379 base = curr->fts_pathlen;
380 }
381
382 p = &curr->fts_path[base];
383 nlen = curr->fts_pathlen - base;
6c780a1f
A
384 target_mid = to.target_end;
385 if (*p != '/' && target_mid[-1] != '/')
386 *target_mid++ = '/';
387 *target_mid = 0;
388 if (target_mid - to.p_path + nlen >= PATH_MAX) {
389 warnx("%s%s: name too long (not copied)",
390 to.p_path, p);
4d0bb651 391 rval = 1;
6c780a1f
A
392 continue;
393 }
394 (void)strncat(target_mid, p, nlen);
395 to.p_end = target_mid + nlen;
44a7a5ab
A
396 *to.p_end = 0;
397 STRIP_TRAILING_SLASH(to);
398 }
399
6c780a1f
A
400 if (curr->fts_info == FTS_DP) {
401 /*
402 * We are nearly finished with this directory. If we
403 * didn't actually copy it, or otherwise don't need to
404 * change its attributes, then we are done.
405 */
406 if (!curr->fts_number)
407 continue;
408 /*
409 * If -p is in effect, set all the attributes.
410 * Otherwise, set the correct permissions, limited
411 * by the umask. Optimise by avoiding a chmod()
412 * if possible (which is usually the case if we
413 * made the directory). Note that mkdir() does not
414 * honour setuid, setgid and sticky bits, but we
415 * normally want to preserve them on directories.
416 */
417 if (pflag) {
864a4b6e
A
418 if (setfile(curr->fts_statp, -1))
419 rval = 1;
c59d3020 420#ifdef __APPLE__
864a4b6e
A
421 /* setfile will fail if writeattr is denied */
422 if (copyfile(curr->fts_path, to.p_path, NULL, COPYFILE_ACL)<0)
423 warn("%s: unable to copy ACL to %s", curr->fts_path, to.p_path);
424#else /* !__APPLE__ */
425 if (preserve_dir_acls(curr->fts_statp,
426 curr->fts_accpath, to.p_path) != 0)
427 rval = 1;
428#endif /* __APPLE__ */
6c780a1f
A
429 } else {
430 mode = curr->fts_statp->st_mode;
431 if ((mode & (S_ISUID | S_ISGID | S_ISTXT)) ||
432 ((mode | S_IRWXU) & mask) != (mode & mask))
433 if (chmod(to.p_path, mode & mask) != 0){
434 warn("chmod: %s", to.p_path);
435 rval = 1;
436 }
437 }
438 continue;
439 }
440
44a7a5ab
A
441 /* Not an error but need to remember it happened */
442 if (stat(to.p_path, &to_stat) == -1)
443 dne = 1;
444 else {
445 if (to_stat.st_dev == curr->fts_statp->st_dev &&
446 to_stat.st_ino == curr->fts_statp->st_ino) {
447 warnx("%s and %s are identical (not copied).",
448 to.p_path, curr->fts_path);
4d0bb651 449 rval = 1;
44a7a5ab
A
450 if (S_ISDIR(curr->fts_statp->st_mode))
451 (void)fts_set(ftsp, curr, FTS_SKIP);
452 continue;
453 }
454 if (!S_ISDIR(curr->fts_statp->st_mode) &&
455 S_ISDIR(to_stat.st_mode)) {
6c780a1f
A
456 warnx("cannot overwrite directory %s with "
457 "non-directory %s",
44a7a5ab 458 to.p_path, curr->fts_path);
4d0bb651 459 rval = 1;
44a7a5ab
A
460 continue;
461 }
462 dne = 0;
463 }
464
465 switch (curr->fts_statp->st_mode & S_IFMT) {
466 case S_IFLNK:
6c780a1f
A
467 /* Catch special case of a non-dangling symlink */
468 if ((fts_options & FTS_LOGICAL) ||
469 ((fts_options & FTS_COMFOLLOW) &&
470 curr->fts_level == 0)) {
471 if (copy_file(curr, dne))
472 badcp = rval = 1;
473 } else {
474 if (copy_link(curr, !dne))
475 badcp = rval = 1;
476 }
44a7a5ab
A
477 break;
478 case S_IFDIR:
479 if (!Rflag && !rflag) {
f383e97b 480 warnx("%s is a directory (not copied).",
864a4b6e 481 curr->fts_path);
44a7a5ab 482 (void)fts_set(ftsp, curr, FTS_SKIP);
6c780a1f 483 badcp = rval = 1;
44a7a5ab
A
484 break;
485 }
6c780a1f
A
486 /*
487 * If the directory doesn't exist, create the new
488 * one with the from file mode plus owner RWX bits,
489 * modified by the umask. Trade-off between being
490 * able to write the directory (if from directory is
491 * 555) and not causing a permissions race. If the
492 * umask blocks owner writes, we fail..
493 */
494 if (dne) {
495 if (mkdir(to.p_path,
40bf83fe 496 curr->fts_statp->st_mode | S_IRWXU) < 0) {
864a4b6e
A
497 if (COMPAT_MODE("bin/cp", "unix2003")) {
498 warn("%s", to.p_path);
499 } else {
500 err(1, "%s", to.p_path);
501 }
40bf83fe 502 }
6c780a1f
A
503 } else if (!S_ISDIR(to_stat.st_mode)) {
504 errno = ENOTDIR;
864a4b6e
A
505 if (COMPAT_MODE("bin/cp", "unix2003")) {
506 warn("%s", to.p_path);
507 } else {
508 err(1, "%s", to.p_path);
509 }
44a7a5ab 510 }
6c780a1f
A
511 /*
512 * Arrange to correct directory attributes later
513 * (in the post-order phase) if this is a new
514 * directory, or if the -p flag is in effect.
515 */
516 curr->fts_number = pflag || dne;
c59d3020 517#ifdef __APPLE__
864a4b6e
A
518 if (!Xflag) {
519 if (copyfile(curr->fts_path, to.p_path, NULL, COPYFILE_XATTR) < 0)
520 warn("%s: unable to copy extended attributes to %s", curr->fts_path, to.p_path);
521 /* ACL and mtime set in postorder traversal */
522 }
523#endif /* __APPLE__ */
44a7a5ab
A
524 break;
525 case S_IFBLK:
526 case S_IFCHR:
527 if (Rflag) {
528 if (copy_special(curr->fts_statp, !dne))
6c780a1f
A
529 badcp = rval = 1;
530 } else {
44a7a5ab 531 if (copy_file(curr, dne))
6c780a1f
A
532 badcp = rval = 1;
533 }
44a7a5ab
A
534 break;
535 case S_IFIFO:
536 if (Rflag) {
537 if (copy_fifo(curr->fts_statp, !dne))
6c780a1f
A
538 badcp = rval = 1;
539 } else {
44a7a5ab 540 if (copy_file(curr, dne))
6c780a1f
A
541 badcp = rval = 1;
542 }
44a7a5ab
A
543 break;
544 default:
545 if (copy_file(curr, dne))
6c780a1f 546 badcp = rval = 1;
44a7a5ab
A
547 break;
548 }
6c780a1f
A
549 if (vflag && !badcp)
550 (void)printf("%s -> %s\n", curr->fts_path, to.p_path);
44a7a5ab
A
551 }
552 if (errno)
553 err(1, "fts_read");
864a4b6e 554 fts_close(ftsp);
44a7a5ab
A
555 return (rval);
556}
557
864a4b6e
A
558static void
559siginfo(int sig __unused)
560{
561
562 info = 1;
563}