1 /* $NetBSD: cp.c,v 1.52 2009/09/29 13:30:17 pooka Exp $ */
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
35 #include <sys/cdefs.h>
38 "@(#) Copyright (c) 1988, 1993, 1994\
39 The Regents of the University of California. All rights reserved.");
44 static char sccsid
[] = "@(#)cp.c 8.5 (Berkeley) 4/29/95";
46 __RCSID("$NetBSD: cp.c,v 1.52 2009/09/29 13:30:17 pooka Exp $");
51 * Cp copies source files to target files.
53 * The global PATH_T structure "to" always contains the path to the
54 * current target file. Since fts(3) does not change directories,
55 * this path can be either absolute or dot-relative.
57 * The basic algorithm is to initialize "to" and use fts(3) to traverse
58 * the file hierarchy rooted in the argument list. A trivial case is the
59 * case of 'cp file1 file2'. The more interesting case is the case of
60 * 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the
61 * path (relative to the root of the traversal) is appended to dir (stored
62 * in "to") to form the final target path.
65 #include <sys/param.h>
80 #define STRIP_TRAILING_SLASH(p) { \
81 while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \
82 *--(p).p_end = '\0'; \
85 static char empty
[] = "";
86 PATH_T to
= { .p_end
= to
.p_path
, .target_end
= empty
};
89 int Hflag
, Lflag
, Rflag
, Pflag
, fflag
, iflag
, pflag
, rflag
, vflag
, Nflag
;
92 enum op
{ FILE_TO_FILE
, FILE_TO_DIR
, DIR_TO_DNE
};
94 int main(int, char *[]);
95 int copy(char *[], enum op
, int);
98 main(int argc
, char *argv
[])
100 struct stat to_stat
, tmp_stat
;
102 int ch
, fts_options
, r
, have_trailing_slash
;
105 setprogname(argv
[0]);
106 (void)setlocale(LC_ALL
, "");
108 Hflag
= Lflag
= Pflag
= Rflag
= 0;
109 while ((ch
= getopt(argc
, argv
, "HLNPRfiprv")) != -1)
134 iflag
= isatty(fileno(stdin
));
157 fts_options
= FTS_NOCHDIR
| FTS_PHYSICAL
;
161 "the -R and -r options may not be specified together.");
164 if (Hflag
|| Lflag
|| Pflag
) {
166 "the -H, -L, and -P options may not be specified with the -r option.");
169 fts_options
&= ~FTS_PHYSICAL
;
170 fts_options
|= FTS_LOGICAL
;
175 fts_options
|= FTS_COMFOLLOW
;
177 fts_options
&= ~FTS_PHYSICAL
;
178 fts_options
|= FTS_LOGICAL
;
181 fts_options
&= ~FTS_PHYSICAL
;
182 fts_options
|= FTS_LOGICAL
| FTS_COMFOLLOW
;
187 /* Copy the umask for explicit mode setting. */
189 (void)umask(myumask
);
191 /* Save the target base in "to". */
192 target
= argv
[--argc
];
193 if (strlcpy(to
.p_path
, target
, sizeof(to
.p_path
)) >= sizeof(to
.p_path
))
194 errx(EXIT_FAILURE
, "%s: name too long", target
);
195 to
.p_end
= to
.p_path
+ strlen(to
.p_path
);
196 have_trailing_slash
= (to
.p_end
[-1] == '/');
197 if (have_trailing_slash
)
198 STRIP_TRAILING_SLASH(to
);
199 to
.target_end
= to
.p_end
;
201 /* Set end of argument list for fts(3). */
205 * Cp has two distinct cases:
207 * cp [-R] source target
208 * cp [-R] source1 ... sourceN directory
210 * In both cases, source can be either a file or a directory.
212 * In (1), the target becomes a copy of the source. That is, if the
213 * source is a file, the target will be a file, and likewise for
216 * In (2), the real target is not directory, but "directory/source".
219 r
= lstat(to
.p_path
, &to_stat
);
221 r
= stat(to
.p_path
, &to_stat
);
222 if (r
== -1 && errno
!= ENOENT
) {
223 err(EXIT_FAILURE
, "%s", to
.p_path
);
226 if (r
== -1 || !S_ISDIR(to_stat
.st_mode
)) {
228 * Case (1). Target is not a directory.
233 * Need to detect the case:
235 * Where dir is a directory and foo does not exist, where
236 * we want pathname concatenations turned on but not for
237 * the initial mkdir().
240 if (rflag
|| (Rflag
&& (Lflag
|| Hflag
)))
241 r
= stat(*argv
, &tmp_stat
);
243 r
= lstat(*argv
, &tmp_stat
);
245 err(EXIT_FAILURE
, "%s", *argv
);
249 if (S_ISDIR(tmp_stat
.st_mode
) && (Rflag
|| rflag
))
256 if (have_trailing_slash
&& type
== FILE_TO_FILE
) {
258 errx(1, "directory %s does not exist",
261 errx(1, "%s is not a directory", to
.p_path
);
265 * Case (2). Target is a directory.
271 * make "cp -rp src/ dst" behave like "cp -rp src dst" not
272 * like "cp -rp src/. dst"
274 for (src
= argv
; *src
; src
++) {
275 size_t len
= strlen(*src
);
276 while (len
-- > 1 && (*src
)[len
] == '/')
280 exit(copy(argv
, type
, fts_options
));
284 static int dnestack
[MAXPATHLEN
]; /* unlikely we'll have more nested dirs */
285 static ssize_t dnesp
;
290 dnestack
[dnesp
++] = dne
;
291 assert(dnesp
< MAXPATHLEN
);
299 rv
= dnestack
[--dnesp
];
305 copy(char *argv
[], enum op type
, int fts_options
)
311 int this_failed
, any_failed
;
313 char *p
, *target_mid
;
315 base
= 0; /* XXX gcc -Wuninitialized (see comment below) */
317 if ((ftsp
= fts_open(argv
, fts_options
, NULL
)) == NULL
)
318 err(EXIT_FAILURE
, "%s", argv
[0]);
320 for (any_failed
= 0; (curr
= fts_read(ftsp
)) != NULL
;) {
322 switch (curr
->fts_info
) {
326 warnx("%s: %s", curr
->fts_path
,
327 strerror(curr
->fts_errno
));
328 this_failed
= any_failed
= 1;
330 case FTS_DC
: /* Warn, continue. */
331 warnx("%s: directory causes a cycle", curr
->fts_path
);
332 this_failed
= any_failed
= 1;
337 * If we are in case (2) or (3) above, we need to append the
338 * source name to the target name.
340 if (type
!= FILE_TO_FILE
) {
341 if ((curr
->fts_namelen
+
342 to
.target_end
- to
.p_path
+ 1) > MAXPATHLEN
) {
343 warnx("%s/%s: name too long (not copied)",
344 to
.p_path
, curr
->fts_name
);
345 this_failed
= any_failed
= 1;
350 * Need to remember the roots of traversals to create
351 * correct pathnames. If there's a directory being
352 * copied to a non-existent directory, e.g.
353 * cp -R a/dir noexist
354 * the resulting path name should be noexist/foo, not
355 * noexist/dir/foo (where foo is a file in dir), which
356 * is the case where the target exists.
358 * Also, check for "..". This is for correct path
359 * concatentation for paths ending in "..", e.g.
361 * Paths ending in ".." are changed to ".". This is
362 * tricky, but seems the easiest way to fix the problem.
365 * Since the first level MUST be FTS_ROOTLEVEL, base
366 * is always initialized.
368 if (curr
->fts_level
== FTS_ROOTLEVEL
) {
369 if (type
!= DIR_TO_DNE
) {
370 p
= strrchr(curr
->fts_path
, '/');
371 base
= (p
== NULL
) ? 0 :
372 (int)(p
- curr
->fts_path
+ 1);
374 if (!strcmp(&curr
->fts_path
[base
],
378 base
= curr
->fts_pathlen
;
381 p
= &curr
->fts_path
[base
];
382 nlen
= curr
->fts_pathlen
- base
;
383 target_mid
= to
.target_end
;
384 if (*p
!= '/' && target_mid
[-1] != '/')
388 if (target_mid
- to
.p_path
+ nlen
>= PATH_MAX
) {
389 warnx("%s%s: name too long (not copied)",
391 this_failed
= any_failed
= 1;
394 (void)strncat(target_mid
, p
, nlen
);
395 to
.p_end
= target_mid
+ nlen
;
397 STRIP_TRAILING_SLASH(to
);
400 sval
= Pflag
? lstat(to
.p_path
, &to_stat
) : stat(to
.p_path
, &to_stat
);
401 /* Not an error but need to remember it happened */
405 if (to_stat
.st_dev
== curr
->fts_statp
->st_dev
&&
406 to_stat
.st_ino
== curr
->fts_statp
->st_ino
) {
407 warnx("%s and %s are identical (not copied).",
408 to
.p_path
, curr
->fts_path
);
409 this_failed
= any_failed
= 1;
410 if (S_ISDIR(curr
->fts_statp
->st_mode
))
411 (void)fts_set(ftsp
, curr
, FTS_SKIP
);
414 if (!S_ISDIR(curr
->fts_statp
->st_mode
) &&
415 S_ISDIR(to_stat
.st_mode
)) {
416 warnx("cannot overwrite directory %s with non-directory %s",
417 to
.p_path
, curr
->fts_path
);
418 this_failed
= any_failed
= 1;
424 switch (curr
->fts_statp
->st_mode
& S_IFMT
) {
426 /* Catch special case of a non dangling symlink */
427 if((fts_options
& FTS_LOGICAL
) ||
428 ((fts_options
& FTS_COMFOLLOW
) && curr
->fts_level
== 0)) {
429 if (copy_file(curr
, dne
))
430 this_failed
= any_failed
= 1;
432 if (copy_link(curr
, !dne
))
433 this_failed
= any_failed
= 1;
437 if (!Rflag
&& !rflag
) {
438 if (curr
->fts_info
== FTS_D
)
439 warnx("%s is a directory (not copied).",
441 (void)fts_set(ftsp
, curr
, FTS_SKIP
);
442 this_failed
= any_failed
= 1;
447 * Directories get noticed twice:
448 * In the first pass, create it if needed.
449 * In the second pass, after the children have been copied, set the permissions.
451 if (curr
->fts_info
== FTS_D
) /* First pass */
454 * If the directory doesn't exist, create the new
455 * one with the from file mode plus owner RWX bits,
456 * modified by the umask. Trade-off between being
457 * able to write the directory (if from directory is
458 * 555) and not causing a permissions race. If the
459 * umask blocks owner writes, we fail..
464 curr
->fts_statp
->st_mode
| S_IRWXU
) < 0)
465 err(EXIT_FAILURE
, "%s",
468 } else if (!S_ISDIR(to_stat
.st_mode
)) {
470 err(EXIT_FAILURE
, "%s",
475 else if (curr
->fts_info
== FTS_DP
) /* Second pass */
478 * If not -p and directory didn't exist, set it to be
479 * the same as the from directory, umodified by the
480 * umask; arguably wrong, but it's been that way
483 if (pflag
&& setfile(curr
->fts_statp
, 0))
484 this_failed
= any_failed
= 1;
485 else if ((dne
= popdne()))
486 (void)chmod(to
.p_path
,
487 curr
->fts_statp
->st_mode
);
491 warnx("directory %s encountered when not expected.",
493 this_failed
= any_failed
= 1;
501 if (copy_special(curr
->fts_statp
, !dne
))
502 this_failed
= any_failed
= 1;
504 if (copy_file(curr
, dne
))
505 this_failed
= any_failed
= 1;
509 if (copy_fifo(curr
->fts_statp
, !dne
))
510 this_failed
= any_failed
= 1;
512 if (copy_file(curr
, dne
))
513 this_failed
= any_failed
= 1;
516 if (copy_file(curr
, dne
))
517 this_failed
= any_failed
= 1;
520 if (vflag
&& !this_failed
)
521 (void)printf("%s -> %s\n", curr
->fts_path
, to
.p_path
);
524 err(EXIT_FAILURE
, "fts_read");
527 (void)fts_close(ftsp
);