VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/cp.c@ 1162

Last change on this file since 1162 was 1141, checked in by bird, 17 years ago

unused

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