]>
git.saurik.com Git - apple/file_cmds.git/blob - cp/cp.c
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: src/bin/cp/cp.c,v 1.52 2005/09/05 04:36:08 csjp Exp $");
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 #include <get_compat.h>
78 #else /* !__APPLE__ */
79 #define COMPAT_MODE(a,b) (1)
80 #endif /* __APPLE__ */
84 #define STRIP_TRAILING_SLASH(p) { \
85 while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \
89 static char emptystring
[] = "";
91 PATH_T to
= { to
.p_path
, emptystring
, "" };
93 int fflag
, iflag
, nflag
, pflag
, vflag
;
96 #endif /* __APPLE__ */
97 static int Rflag
, rflag
;
99 volatile sig_atomic_t info
;
101 enum op
{ FILE_TO_FILE
, FILE_TO_DIR
, DIR_TO_DNE
};
103 static int copy(char *[], enum op
, int);
104 static void siginfo(int __unused
);
107 main(int argc
, char *argv
[])
109 struct stat to_stat
, tmp_stat
;
111 int Hflag
, Lflag
, Pflag
, ch
, fts_options
, r
, have_trailing_slash
;
114 Hflag
= Lflag
= Pflag
= 0;
115 while ((ch
= getopt(argc
, argv
, "cHLPRXafinprv")) != -1)
140 /* Determine if the STD is SUSv3 or Legacy */
141 if (COMPAT_MODE("bin/cp", "unix2003"))
142 nflag
= 0; /* reset nflag, but not iflag */
144 iflag
= nflag
= 0; /* reset both */
148 if (COMPAT_MODE("bin/cp", "unix2003"))
149 nflag
= 0; /* reset nflag, but not fflag */
181 if (cflag
&& Xflag
) {
182 errx(1, "the -c and -X options may not be specified together");
185 fts_options
= FTS_NOCHDIR
| FTS_PHYSICAL
;
189 "the -R and -r options may not be specified together.");
190 if (Hflag
|| Lflag
|| Pflag
)
192 "the -H, -L, and -P options may not be specified with the -r option.");
193 fts_options
&= ~FTS_PHYSICAL
;
194 fts_options
|= FTS_LOGICAL
;
198 fts_options
|= FTS_COMFOLLOW
;
200 fts_options
&= ~FTS_PHYSICAL
;
201 fts_options
|= FTS_LOGICAL
;
204 fts_options
&= ~FTS_PHYSICAL
;
205 fts_options
|= FTS_LOGICAL
| FTS_COMFOLLOW
;
207 (void)signal(SIGINFO
, siginfo
);
209 /* Save the target base in "to". */
210 target
= argv
[--argc
];
211 if (strlcpy(to
.p_path
, target
, sizeof(to
.p_path
)) >= sizeof(to
.p_path
))
212 errx(1, "%s: name too long", target
);
213 to
.p_end
= to
.p_path
+ strlen(to
.p_path
);
214 if (to
.p_path
== to
.p_end
) {
218 have_trailing_slash
= (to
.p_end
[-1] == '/');
219 if (have_trailing_slash
)
220 STRIP_TRAILING_SLASH(to
);
221 to
.target_end
= to
.p_end
;
223 /* Set end of argument list for fts(3). */
227 * Cp has two distinct cases:
229 * cp [-R] source target
230 * cp [-R] source1 ... sourceN directory
232 * In both cases, source can be either a file or a directory.
234 * In (1), the target becomes a copy of the source. That is, if the
235 * source is a file, the target will be a file, and likewise for
238 * In (2), the real target is not directory, but "directory/source".
240 r
= stat(to
.p_path
, &to_stat
);
241 if (r
== -1 && errno
!= ENOENT
)
242 err(1, "%s", to
.p_path
);
243 if (r
== -1 || !S_ISDIR(to_stat
.st_mode
)) {
245 * Case (1). Target is not a directory.
252 * Need to detect the case:
254 * Where dir is a directory and foo does not exist, where
255 * we want pathname concatenations turned on but not for
256 * the initial mkdir().
259 if (rflag
|| (Rflag
&& (Lflag
|| Hflag
)))
260 stat(*argv
, &tmp_stat
);
262 lstat(*argv
, &tmp_stat
);
264 if (S_ISDIR(tmp_stat
.st_mode
) && (Rflag
|| rflag
))
271 if (have_trailing_slash
&& type
== FILE_TO_FILE
) {
273 errx(1, "directory %s does not exist",
276 errx(1, "%s is not a directory", to
.p_path
);
280 * Case (2). Target is a directory.
284 exit (copy(argv
, type
, fts_options
));
288 copy(char *argv
[], enum op type
, int fts_options
)
293 int base
= 0, dne
, badcp
, rval
;
295 char *p
, *target_mid
;
299 * Keep an inverted copy of the umask, for use in correcting
300 * permissions on created directories when not using -p.
305 if ((ftsp
= fts_open(argv
, fts_options
, NULL
)) == NULL
)
307 for (badcp
= rval
= 0; (curr
= fts_read(ftsp
)) != NULL
; badcp
= 0) {
308 switch (curr
->fts_info
) {
313 curr
->fts_path
, strerror(curr
->fts_errno
));
316 case FTS_DC
: /* Warn, continue. */
317 warnx("%s: directory causes a cycle", curr
->fts_path
);
326 #pragma clang diagnostic push
327 /* clang doesn't like fts_name[1], but we know better... */
328 #pragma clang diagnostic ignored "-Warray-bounds"
330 /* Skip ._<file> when using copyfile and <file> exists */
331 if ((pflag
|| !Xflag
) && (curr
->fts_level
!= FTS_ROOTLEVEL
) &&
332 (curr
->fts_namelen
> 2) && /* ._\0 is not AppleDouble */
333 (curr
->fts_name
[0] == '.') && (curr
->fts_name
[1] == '_')) {
335 #pragma clang diagnostic pop
339 char *p
= strrchr(curr
->fts_path
, '/');
341 size_t s
= p
+ 2 - curr
->fts_path
;
342 if (s
> sizeof(path
)) s
= sizeof(path
);
343 strlcpy(path
, curr
->fts_path
, s
);
344 strlcat(path
, curr
->fts_name
+2, sizeof(path
));
346 strlcpy(path
, curr
->fts_name
+2, sizeof(path
));
348 if (!lstat(path
, &statbuf
)) {
352 #endif /* __APPLE__ */
354 * If we are in case (2) or (3) above, we need to append the
355 * source name to the target name.
357 if (type
!= FILE_TO_FILE
) {
359 * Need to remember the roots of traversals to create
360 * correct pathnames. If there's a directory being
361 * copied to a non-existent directory, e.g.
362 * cp -R a/dir noexist
363 * the resulting path name should be noexist/foo, not
364 * noexist/dir/foo (where foo is a file in dir), which
365 * is the case where the target exists.
367 * Also, check for "..". This is for correct path
368 * concatenation for paths ending in "..", e.g.
370 * Paths ending in ".." are changed to ".". This is
371 * tricky, but seems the easiest way to fix the problem.
374 * Since the first level MUST be FTS_ROOTLEVEL, base
375 * is always initialized.
377 if (curr
->fts_level
== FTS_ROOTLEVEL
) {
378 if (type
!= DIR_TO_DNE
) {
379 p
= strrchr(curr
->fts_path
, '/');
380 base
= (p
== NULL
) ? 0 :
381 (int)(p
- curr
->fts_path
+ 1);
383 if (!strcmp(&curr
->fts_path
[base
],
387 base
= curr
->fts_pathlen
;
390 p
= &curr
->fts_path
[base
];
391 nlen
= curr
->fts_pathlen
- base
;
392 target_mid
= to
.target_end
;
393 if (*p
!= '/' && target_mid
[-1] != '/')
396 if (target_mid
- to
.p_path
+ nlen
>= PATH_MAX
) {
397 warnx("%s%s: name too long (not copied)",
402 (void)strncat(target_mid
, p
, nlen
);
403 to
.p_end
= target_mid
+ nlen
;
405 STRIP_TRAILING_SLASH(to
);
408 if (curr
->fts_info
== FTS_DP
) {
410 * We are nearly finished with this directory. If we
411 * didn't actually copy it, or otherwise don't need to
412 * change its attributes, then we are done.
414 if (!curr
->fts_number
)
417 * If -p is in effect, set all the attributes.
418 * Otherwise, set the correct permissions, limited
419 * by the umask. Optimise by avoiding a chmod()
420 * if possible (which is usually the case if we
421 * made the directory). Note that mkdir() does not
422 * honour setuid, setgid and sticky bits, but we
423 * normally want to preserve them on directories.
426 if (setfile(curr
->fts_statp
, -1))
429 /* setfile will fail if writeattr is denied */
430 if (copyfile(curr
->fts_path
, to
.p_path
, NULL
, COPYFILE_ACL
)<0)
431 warn("%s: unable to copy ACL to %s", curr
->fts_path
, to
.p_path
);
432 #else /* !__APPLE__ */
433 if (preserve_dir_acls(curr
->fts_statp
,
434 curr
->fts_accpath
, to
.p_path
) != 0)
436 #endif /* __APPLE__ */
438 mode
= curr
->fts_statp
->st_mode
;
439 if ((mode
& (S_ISUID
| S_ISGID
| S_ISTXT
)) ||
440 ((mode
| S_IRWXU
) & mask
) != (mode
& mask
))
441 if (chmod(to
.p_path
, mode
& mask
) != 0){
442 warn("chmod: %s", to
.p_path
);
449 /* Not an error but need to remember it happened */
450 if (stat(to
.p_path
, &to_stat
) == -1)
453 if (to_stat
.st_dev
== curr
->fts_statp
->st_dev
&&
454 to_stat
.st_ino
== curr
->fts_statp
->st_ino
) {
455 warnx("%s and %s are identical (not copied).",
456 to
.p_path
, curr
->fts_path
);
458 if (S_ISDIR(curr
->fts_statp
->st_mode
))
459 (void)fts_set(ftsp
, curr
, FTS_SKIP
);
462 if (!S_ISDIR(curr
->fts_statp
->st_mode
) &&
463 S_ISDIR(to_stat
.st_mode
)) {
464 warnx("cannot overwrite directory %s with "
466 to
.p_path
, curr
->fts_path
);
473 switch (curr
->fts_statp
->st_mode
& S_IFMT
) {
475 /* Catch special case of a non-dangling symlink */
476 if ((fts_options
& FTS_LOGICAL
) ||
477 ((fts_options
& FTS_COMFOLLOW
) &&
478 curr
->fts_level
== 0)) {
479 if (copy_file(curr
, dne
))
482 if (copy_link(curr
, !dne
))
487 if (!Rflag
&& !rflag
) {
488 warnx("%s is a directory (not copied).",
490 (void)fts_set(ftsp
, curr
, FTS_SKIP
);
495 * If the directory doesn't exist, create the new
496 * one with the from file mode plus owner RWX bits,
497 * modified by the umask. Trade-off between being
498 * able to write the directory (if from directory is
499 * 555) and not causing a permissions race. If the
500 * umask blocks owner writes, we fail..
504 curr
->fts_statp
->st_mode
| S_IRWXU
) < 0) {
505 if (COMPAT_MODE("bin/cp", "unix2003")) {
506 warn("%s", to
.p_path
);
508 err(1, "%s", to
.p_path
);
511 } else if (!S_ISDIR(to_stat
.st_mode
)) {
513 if (COMPAT_MODE("bin/cp", "unix2003")) {
514 warn("%s", to
.p_path
);
516 err(1, "%s", to
.p_path
);
520 * Arrange to correct directory attributes later
521 * (in the post-order phase) if this is a new
522 * directory, or if the -p flag is in effect.
524 curr
->fts_number
= pflag
|| dne
;
527 if (copyfile(curr
->fts_path
, to
.p_path
, NULL
, COPYFILE_XATTR
) < 0)
528 warn("%s: unable to copy extended attributes to %s", curr
->fts_path
, to
.p_path
);
529 /* ACL and mtime set in postorder traversal */
531 #endif /* __APPLE__ */
536 if (copy_special(curr
->fts_statp
, !dne
))
539 if (copy_file(curr
, dne
))
545 if (copy_fifo(curr
->fts_statp
, !dne
))
548 if (copy_file(curr
, dne
))
553 if (copy_file(curr
, dne
))
558 (void)printf("%s -> %s\n", curr
->fts_path
, to
.p_path
);
567 siginfo(int sig __unused
)