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