VirtualBox

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

Last change on this file since 1223 was 1194, checked in by bird, 17 years ago

Fixed a couple of slash problems in cp and install (dos based systems).

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