cp.c revision 1.23 1 /* $NetBSD: cp.c,v 1.23 1998/07/28 11:41:40 mycroft 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.23 1998/07/28 11:41:40 mycroft 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 #include <sys/mman.h>
72 #include <sys/time.h>
73
74 #include <dirent.h>
75 #include <err.h>
76 #include <errno.h>
77 #include <fcntl.h>
78 #include <fts.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <unistd.h>
83
84 #include "extern.h"
85
86 #define STRIP_TRAILING_SLASH(p) { \
87 while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \
88 *--(p).p_end = 0; \
89 }
90
91 PATH_T to = { to.p_path, "" };
92
93 uid_t myuid;
94 int Rflag, iflag, pflag, rflag;
95 mode_t myumask;
96
97 enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
98
99 int main __P((int, char *[]));
100 int copy __P((char *[], enum op, int));
101 int mastercmp __P((const FTSENT **, const FTSENT **));
102
103 int
104 main(argc, argv)
105 int argc;
106 char *argv[];
107 {
108 struct stat to_stat, tmp_stat;
109 enum op type;
110 int Hflag, Lflag, Pflag, ch, fts_options, r;
111 char *target;
112
113 Hflag = Lflag = Pflag = Rflag = 0;
114 while ((ch = getopt(argc, argv, "HLPRfipr")) != -1)
115 switch (ch) {
116 case 'H':
117 Hflag = 1;
118 Lflag = Pflag = 0;
119 break;
120 case 'L':
121 Lflag = 1;
122 Hflag = Pflag = 0;
123 break;
124 case 'P':
125 Pflag = 1;
126 Hflag = Lflag = 0;
127 break;
128 case 'R':
129 Rflag = 1;
130 break;
131 case 'f':
132 iflag = 0;
133 break;
134 case 'i':
135 iflag = isatty(fileno(stdin));
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;
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 exit (copy(argv, type, fts_options));
252 /* NOTREACHED */
253 }
254
255 int
256 copy(argv, type, fts_options)
257 char *argv[];
258 enum op type;
259 int fts_options;
260 {
261 struct stat to_stat;
262 FTS *ftsp;
263 FTSENT *curr;
264 int base, dne, nlen, rval;
265 char *p, *tmp;
266
267 base = 0; /* XXX gcc -Wuninitialized (see comment below) */
268
269 if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
270 err(1, argv[0]);
271 for (rval = 0; (curr = fts_read(ftsp)) != NULL;) {
272 switch (curr->fts_info) {
273 case FTS_NS:
274 case FTS_ERR:
275 warnx("%s: %s",
276 curr->fts_path, strerror(curr->fts_errno));
277 rval = 1;
278 continue;
279 case FTS_DC: /* Warn, continue. */
280 warnx("%s: directory causes a cycle", curr->fts_path);
281 rval = 1;
282 continue;
283 case FTS_DP: /* Ignore, continue. */
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 p = &curr->fts_path[base];
332 nlen = curr->fts_pathlen - base;
333
334 tmp = to.target_end;
335 if (*p != '/' && *(tmp - 1) != '/')
336 *tmp++ = '/';
337 *tmp = 0;
338
339 (void)strncat(tmp, p, nlen);
340 to.p_end = tmp + nlen;
341 *to.p_end = 0;
342 STRIP_TRAILING_SLASH(to);
343 }
344
345 /* Not an error but need to remember it happened */
346 if (stat(to.p_path, &to_stat) == -1)
347 dne = 1;
348 else {
349 if (to_stat.st_dev == curr->fts_statp->st_dev &&
350 to_stat.st_ino == curr->fts_statp->st_ino) {
351 warnx("%s and %s are identical (not copied).",
352 to.p_path, curr->fts_path);
353 rval = 1;
354 if (S_ISDIR(curr->fts_statp->st_mode))
355 (void)fts_set(ftsp, curr, FTS_SKIP);
356 continue;
357 }
358 if (!S_ISDIR(curr->fts_statp->st_mode) &&
359 S_ISDIR(to_stat.st_mode)) {
360 warnx("cannot overwrite directory %s with non-directory %s",
361 to.p_path, curr->fts_path);
362 rval = 1;
363 continue;
364 }
365 dne = 0;
366 }
367
368 switch (curr->fts_statp->st_mode & S_IFMT) {
369 case S_IFLNK:
370 if (copy_link(curr, !dne))
371 rval = 1;
372 break;
373 case S_IFDIR:
374 if (!Rflag && !rflag) {
375 warnx("%s is a directory (not copied).",
376 curr->fts_path);
377 (void)fts_set(ftsp, curr, FTS_SKIP);
378 rval = 1;
379 break;
380 }
381 /*
382 * If the directory doesn't exist, create the new
383 * one with the from file mode plus owner RWX bits,
384 * modified by the umask. Trade-off between being
385 * able to write the directory (if from directory is
386 * 555) and not causing a permissions race. If the
387 * umask blocks owner writes, we fail..
388 */
389 if (dne) {
390 if (mkdir(to.p_path,
391 curr->fts_statp->st_mode | S_IRWXU) < 0)
392 err(1, "%s", to.p_path);
393 } else if (!S_ISDIR(to_stat.st_mode)) {
394 errno = ENOTDIR;
395 err(1, "%s", to.p_path);
396 }
397 /*
398 * If not -p and directory didn't exist, set it to be
399 * the same as the from directory, umodified by the
400 * umask; arguably wrong, but it's been that way
401 * forever.
402 */
403 if (pflag && setfile(curr->fts_statp, 0))
404 rval = 1;
405 else if (dne)
406 (void)chmod(to.p_path,
407 curr->fts_statp->st_mode);
408 break;
409 case S_IFBLK:
410 case S_IFCHR:
411 if (Rflag) {
412 if (copy_special(curr->fts_statp, !dne))
413 rval = 1;
414 } else
415 if (copy_file(curr, dne))
416 rval = 1;
417 break;
418 case S_IFIFO:
419 if (Rflag) {
420 if (copy_fifo(curr->fts_statp, !dne))
421 rval = 1;
422 } else
423 if (copy_file(curr, dne))
424 rval = 1;
425 break;
426 default:
427 if (copy_file(curr, dne))
428 rval = 1;
429 break;
430 }
431 }
432 if (errno)
433 err(1, "fts_read");
434 return (rval);
435 }
436
437 /*
438 * mastercmp --
439 * The comparison function for the copy order. The order is to copy
440 * non-directory files before directory files. The reason for this
441 * is because files tend to be in the same cylinder group as their
442 * parent directory, whereas directories tend not to be. Copying the
443 * files first reduces seeking.
444 */
445 int
446 mastercmp(a, b)
447 const FTSENT **a, **b;
448 {
449 int a_info, b_info;
450
451 a_info = (*a)->fts_info;
452 if (a_info == FTS_ERR || a_info == FTS_NS || a_info == FTS_DNR)
453 return (0);
454 b_info = (*b)->fts_info;
455 if (b_info == FTS_ERR || b_info == FTS_NS || b_info == FTS_DNR)
456 return (0);
457 if (a_info == FTS_D)
458 return (-1);
459 if (b_info == FTS_D)
460 return (1);
461 return (0);
462 }
463