cp.c revision 1.28.8.1 1 /* $NetBSD: cp.c,v 1.28.8.1 2000/07/27 16:15:28 itojun Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * David Hitz of Auspex Systems Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __COPYRIGHT(
42 "@(#) Copyright (c) 1988, 1993, 1994\n\
43 The Regents of the University of California. All rights reserved.\n");
44 #endif /* not lint */
45
46 #ifndef lint
47 #if 0
48 static char sccsid[] = "@(#)cp.c 8.5 (Berkeley) 4/29/95";
49 #else
50 __RCSID("$NetBSD: cp.c,v 1.28.8.1 2000/07/27 16:15:28 itojun Exp $");
51 #endif
52 #endif /* not lint */
53
54 /*
55 * Cp copies source files to target files.
56 *
57 * The global PATH_T structure "to" always contains the path to the
58 * current target file. Since fts(3) does not change directories,
59 * this path can be either absolute or dot-relative.
60 *
61 * The basic algorithm is to initialize "to" and use fts(3) to traverse
62 * the file hierarchy rooted in the argument list. A trivial case is the
63 * case of 'cp file1 file2'. The more interesting case is the case of
64 * 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the
65 * path (relative to the root of the traversal) is appended to dir (stored
66 * in "to") to form the final target path.
67 */
68
69 #include <sys/param.h>
70 #include <sys/stat.h>
71
72 #include <err.h>
73 #include <errno.h>
74 #include <fts.h>
75 #include <locale.h>
76 #include <stdio.h>
77 #include <string.h>
78 #include <unistd.h>
79
80 #include "extern.h"
81
82 #define STRIP_TRAILING_SLASH(p) { \
83 while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \
84 *--(p).p_end = '\0'; \
85 }
86
87 PATH_T to = { to.p_path, "" };
88
89 uid_t myuid;
90 int Rflag, iflag, pflag, rflag, fflag;
91 mode_t myumask;
92
93 enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
94
95 int main __P((int, char *[]));
96 int copy __P((char *[], enum op, int));
97 int mastercmp __P((const FTSENT **, const FTSENT **));
98
99 int
100 main(argc, argv)
101 int argc;
102 char *argv[];
103 {
104 struct stat to_stat, tmp_stat;
105 enum op type;
106 int Hflag, Lflag, Pflag, ch, fts_options, r;
107 char *target;
108
109 (void)setlocale(LC_ALL, "");
110
111 Hflag = Lflag = Pflag = Rflag = 0;
112 while ((ch = getopt(argc, argv, "HLPRfipr")) != -1)
113 switch (ch) {
114 case 'H':
115 Hflag = 1;
116 Lflag = Pflag = 0;
117 break;
118 case 'L':
119 Lflag = 1;
120 Hflag = Pflag = 0;
121 break;
122 case 'P':
123 Pflag = 1;
124 Hflag = Lflag = 0;
125 break;
126 case 'R':
127 Rflag = 1;
128 break;
129 case 'f':
130 fflag = 1;
131 iflag = 0;
132 break;
133 case 'i':
134 iflag = isatty(fileno(stdin));
135 fflag = 0;
136 break;
137 case 'p':
138 pflag = 1;
139 break;
140 case 'r':
141 rflag = 1;
142 break;
143 case '?':
144 default:
145 usage();
146 break;
147 }
148 argc -= optind;
149 argv += optind;
150
151 if (argc < 2)
152 usage();
153
154 fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
155 if (rflag) {
156 if (Rflag)
157 errx(1,
158 "the -R and -r options may not be specified together.");
159 if (Hflag || Lflag || Pflag)
160 errx(1,
161 "the -H, -L, and -P options may not be specified with the -r option.");
162 fts_options &= ~FTS_PHYSICAL;
163 fts_options |= FTS_LOGICAL;
164 }
165 if (Rflag) {
166 if (Hflag)
167 fts_options |= FTS_COMFOLLOW;
168 if (Lflag) {
169 fts_options &= ~FTS_PHYSICAL;
170 fts_options |= FTS_LOGICAL;
171 }
172 } else {
173 fts_options &= ~FTS_PHYSICAL;
174 fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
175 }
176
177 myuid = getuid();
178
179 /* Copy the umask for explicit mode setting. */
180 myumask = umask(0);
181 (void)umask(myumask);
182
183 /* Save the target base in "to". */
184 target = argv[--argc];
185 if (strlen(target) > MAXPATHLEN)
186 errx(1, "%s: name too long", target);
187 (void)strcpy(to.p_path, target);
188 to.p_end = to.p_path + strlen(to.p_path);
189 if (to.p_path == to.p_end) {
190 *to.p_end++ = '.';
191 *to.p_end = 0;
192 }
193 STRIP_TRAILING_SLASH(to);
194 to.target_end = to.p_end;
195
196 /* Set end of argument list for fts(3). */
197 argv[argc] = NULL;
198
199 /*
200 * Cp has two distinct cases:
201 *
202 * cp [-R] source target
203 * cp [-R] source1 ... sourceN directory
204 *
205 * In both cases, source can be either a file or a directory.
206 *
207 * In (1), the target becomes a copy of the source. That is, if the
208 * source is a file, the target will be a file, and likewise for
209 * directories.
210 *
211 * In (2), the real target is not directory, but "directory/source".
212 */
213 r = stat(to.p_path, &to_stat);
214 if (r == -1 && errno != ENOENT)
215 err(1, "%s", to.p_path);
216 if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
217 /*
218 * Case (1). Target is not a directory.
219 */
220 if (argc > 1) {
221 usage();
222 exit(1);
223 }
224 /*
225 * Need to detect the case:
226 * cp -R dir foo
227 * Where dir is a directory and foo does not exist, where
228 * we want pathname concatenations turned on but not for
229 * the initial mkdir().
230 */
231 if (r == -1) {
232 if (rflag || (Rflag && (Lflag || Hflag)))
233 r = stat(*argv, &tmp_stat);
234 else
235 r = lstat(*argv, &tmp_stat);
236 if (r == -1)
237 err(1, "%s", *argv);
238
239 if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag))
240 type = DIR_TO_DNE;
241 else
242 type = FILE_TO_FILE;
243 } else
244 type = FILE_TO_FILE;
245 } else {
246 /*
247 * Case (2). Target is a directory.
248 */
249 type = FILE_TO_DIR;
250 }
251
252 exit (copy(argv, type, fts_options));
253 /* NOTREACHED */
254 }
255
256 int
257 copy(argv, type, fts_options)
258 char *argv[];
259 enum op type;
260 int fts_options;
261 {
262 struct stat to_stat;
263 FTS *ftsp;
264 FTSENT *curr;
265 int base, dne, nlen, rval;
266 char *p, *tmp;
267
268 base = 0; /* XXX gcc -Wuninitialized (see comment below) */
269
270 if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
271 err(1, "%s", argv[0]);
272 for (rval = 0; (curr = fts_read(ftsp)) != NULL;) {
273 switch (curr->fts_info) {
274 case FTS_NS:
275 case FTS_DNR:
276 case FTS_ERR:
277 warnx("%s: %s",
278 curr->fts_path, strerror(curr->fts_errno));
279 rval = 1;
280 continue;
281 case FTS_DC: /* Warn, continue. */
282 warnx("%s: directory causes a cycle", curr->fts_path);
283 rval = 1;
284 continue;
285 }
286
287 /*
288 * If we are in case (2) or (3) above, we need to append the
289 * source name to the target name.
290 */
291 if (type != FILE_TO_FILE) {
292 if ((curr->fts_namelen +
293 to.target_end - to.p_path + 1) > MAXPATHLEN) {
294 warnx("%s/%s: name too long (not copied)",
295 to.p_path, curr->fts_name);
296 rval = 1;
297 continue;
298 }
299
300 /*
301 * Need to remember the roots of traversals to create
302 * correct pathnames. If there's a directory being
303 * copied to a non-existent directory, e.g.
304 * cp -R a/dir noexist
305 * the resulting path name should be noexist/foo, not
306 * noexist/dir/foo (where foo is a file in dir), which
307 * is the case where the target exists.
308 *
309 * Also, check for "..". This is for correct path
310 * concatentation for paths ending in "..", e.g.
311 * cp -R .. /tmp
312 * Paths ending in ".." are changed to ".". This is
313 * tricky, but seems the easiest way to fix the problem.
314 *
315 * XXX
316 * Since the first level MUST be FTS_ROOTLEVEL, base
317 * is always initialized.
318 */
319 if (curr->fts_level == FTS_ROOTLEVEL) {
320 if (type != DIR_TO_DNE) {
321 p = strrchr(curr->fts_path, '/');
322 base = (p == NULL) ? 0 :
323 (int)(p - curr->fts_path + 1);
324
325 if (!strcmp(&curr->fts_path[base],
326 ".."))
327 base += 1;
328 } else
329 base = curr->fts_pathlen;
330 }
331
332 p = &curr->fts_path[base];
333 nlen = curr->fts_pathlen - base;
334
335 tmp = to.target_end;
336 if (*p != '/' && *(tmp - 1) != '/')
337 *tmp++ = '/';
338 *tmp = 0;
339
340 (void)strncat(tmp, p, nlen);
341 to.p_end = tmp + nlen;
342 *to.p_end = 0;
343 STRIP_TRAILING_SLASH(to);
344 }
345
346 /* Not an error but need to remember it happened */
347 if (stat(to.p_path, &to_stat) == -1)
348 dne = 1;
349 else {
350 if (to_stat.st_dev == curr->fts_statp->st_dev &&
351 to_stat.st_ino == curr->fts_statp->st_ino) {
352 warnx("%s and %s are identical (not copied).",
353 to.p_path, curr->fts_path);
354 rval = 1;
355 if (S_ISDIR(curr->fts_statp->st_mode))
356 (void)fts_set(ftsp, curr, FTS_SKIP);
357 continue;
358 }
359 if (!S_ISDIR(curr->fts_statp->st_mode) &&
360 S_ISDIR(to_stat.st_mode)) {
361 warnx("cannot overwrite directory %s with non-directory %s",
362 to.p_path, curr->fts_path);
363 rval = 1;
364 continue;
365 }
366 dne = 0;
367 }
368
369 switch (curr->fts_statp->st_mode & S_IFMT) {
370 case S_IFLNK:
371 /* Catch special case of a non dangling symlink */
372 if((fts_options & FTS_LOGICAL) ||
373 ((fts_options & FTS_COMFOLLOW) && curr->fts_level == 0)) {
374 if (copy_file(curr, dne))
375 rval = 1;
376 } else {
377 if (copy_link(curr, !dne))
378 rval = 1;
379 }
380 break;
381 case S_IFDIR:
382 if (!Rflag && !rflag) {
383 if (curr->fts_info == FTS_D)
384 warnx("%s is a directory (not copied).",
385 curr->fts_path);
386 (void)fts_set(ftsp, curr, FTS_SKIP);
387 rval = 1;
388 break;
389 }
390
391 /*
392 * Directories get noticed twice:
393 * In the first pass, create it if needed.
394 * In the second pass, after the children have been copied, set the permissions.
395 */
396 if (curr->fts_info == FTS_D) /* First pass */
397 {
398 /*
399 * If the directory doesn't exist, create the new
400 * one with the from file mode plus owner RWX bits,
401 * modified by the umask. Trade-off between being
402 * able to write the directory (if from directory is
403 * 555) and not causing a permissions race. If the
404 * umask blocks owner writes, we fail..
405 */
406 if (dne) {
407 if (mkdir(to.p_path,
408 curr->fts_statp->st_mode | S_IRWXU) < 0)
409 err(1, "%s", to.p_path);
410 } else if (!S_ISDIR(to_stat.st_mode)) {
411 errno = ENOTDIR;
412 err(1, "%s", to.p_path);
413 }
414 }
415 else if (curr->fts_info == FTS_DP) /* Second pass */
416 {
417 /*
418 * If not -p and directory didn't exist, set it to be
419 * the same as the from directory, umodified by the
420 * umask; arguably wrong, but it's been that way
421 * forever.
422 */
423 if (pflag && setfile(curr->fts_statp, 0))
424 rval = 1;
425 else if (dne)
426 (void)chmod(to.p_path,
427 curr->fts_statp->st_mode);
428 }
429 else
430 {
431 warnx("directory %s encountered when not expected.", curr->fts_path);
432 rval = 1;
433 break;
434 }
435
436 break;
437 case S_IFBLK:
438 case S_IFCHR:
439 if (Rflag) {
440 if (copy_special(curr->fts_statp, !dne))
441 rval = 1;
442 } else
443 if (copy_file(curr, dne))
444 rval = 1;
445 break;
446 case S_IFIFO:
447 if (Rflag) {
448 if (copy_fifo(curr->fts_statp, !dne))
449 rval = 1;
450 } else
451 if (copy_file(curr, dne))
452 rval = 1;
453 break;
454 default:
455 if (copy_file(curr, dne))
456 rval = 1;
457 break;
458 }
459 }
460 if (errno)
461 err(1, "fts_read");
462 return (rval);
463 }
464
465 /*
466 * mastercmp --
467 * The comparison function for the copy order. The order is to copy
468 * non-directory files before directory files. The reason for this
469 * is because files tend to be in the same cylinder group as their
470 * parent directory, whereas directories tend not to be. Copying the
471 * files first reduces seeking.
472 */
473 int
474 mastercmp(a, b)
475 const FTSENT **a, **b;
476 {
477 int a_info, b_info;
478
479 a_info = (*a)->fts_info;
480 if (a_info == FTS_ERR || a_info == FTS_NS || a_info == FTS_DNR)
481 return (0);
482 b_info = (*b)->fts_info;
483 if (b_info == FTS_ERR || b_info == FTS_NS || b_info == FTS_DNR)
484 return (0);
485 if (a_info == FTS_D)
486 return (-1);
487 if (b_info == FTS_D)
488 return (1);
489 return (0);
490 }
491