VirtualBox

source: kBuild/trunk/src/gmake/kmkbuiltin/cp.c@ 557

Last change on this file since 557 was 557, checked in by bird, 18 years ago

Initial Mac OS X / Darwin bootstrapping.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette