VirtualBox

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

Last change on this file since 371 was 371, checked in by bird, 19 years ago

Fixed linux bustage and ported install to linux.

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