VirtualBox

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

Last change on this file since 1422 was 1328, checked in by bird, 17 years ago

include our getopt.h

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