VirtualBox

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

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

Always use the GNU make getopt stuff.

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