VirtualBox

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

Last change on this file since 1284 was 1246, checked in by bird, 17 years ago

Solaris build fixes.

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