cp.c revision 1.22 1 /* $NetBSD: cp.c,v 1.22 1998/07/28 05:31:22 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.22 1998/07/28 05:31:22 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 /* NOTREACHED */
224 }
225 /*
226 * Need to detect the case:
227 * cp -R dir foo
228 * Where dir is a directory and foo does not exist, where
229 * we want pathname concatenations turned on but not for
230 * the initial mkdir().
231 */
232 if (r == -1) {
233 if (rflag || (Rflag && (Lflag || Hflag)))
234 r = stat(*argv, &tmp_stat);
235 else
236 r = lstat(*argv, &tmp_stat);
237 if (r == -1)
238 err(1, "%s", *argv);
239
240 if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag))
241 type = DIR_TO_DNE;
242 else
243 type = FILE_TO_FILE;
244 } else
245 type = FILE_TO_FILE;
246 } else
247 /*
248 * Case (2). Target is a directory.
249 */
250 type = FILE_TO_DIR;
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, argv[0]);
272 for (rval = 0; (curr = fts_read(ftsp)) != NULL;) {
273 switch (curr->fts_info) {
274 case FTS_NS:
275 case FTS_ERR:
276 warnx("%s: %s",
277 curr->fts_path, strerror(curr->fts_errno));
278 rval = 1;
279 continue;
280 case FTS_DC: /* Warn, continue. */
281 warnx("%s: directory causes a cycle", curr->fts_path);
282 rval = 1;
283 continue;
284 case FTS_DP: /* Ignore, continue. */
285 continue;
286 }
287
288 /*
289 * If we are in case (2) or (3) above, we need to append the
290 * source name to the target name.
291 */
292 if (type != FILE_TO_FILE) {
293 if ((curr->fts_namelen +
294 to.target_end - to.p_path + 1) > MAXPATHLEN) {
295 warnx("%s/%s: name too long (not copied)",
296 to.p_path, curr->fts_name);
297 rval = 1;
298 continue;
299 }
300
301 /*
302 * Need to remember the roots of traversals to create
303 * correct pathnames. If there's a directory being
304 * copied to a non-existent directory, e.g.
305 * cp -R a/dir noexist
306 * the resulting path name should be noexist/foo, not
307 * noexist/dir/foo (where foo is a file in dir), which
308 * is the case where the target exists.
309 *
310 * Also, check for "..". This is for correct path
311 * concatentation for paths ending in "..", e.g.
312 * cp -R .. /tmp
313 * Paths ending in ".." are changed to ".". This is
314 * tricky, but seems the easiest way to fix the problem.
315 *
316 * XXX
317 * Since the first level MUST be FTS_ROOTLEVEL, base
318 * is always initialized.
319 */
320 if (curr->fts_level == FTS_ROOTLEVEL)
321 if (type != DIR_TO_DNE) {
322 p = strrchr(curr->fts_path, '/');
323 base = (p == NULL) ? 0 :
324 (int)(p - curr->fts_path + 1);
325
326 if (!strcmp(&curr->fts_path[base],
327 ".."))
328 base += 1;
329 } else
330 base = curr->fts_pathlen;
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 if (copy_link(curr, !dne))
372 rval = 1;
373 break;
374 case S_IFDIR:
375 if (!Rflag && !rflag) {
376 warnx("%s is a directory (not copied).",
377 curr->fts_path);
378 (void)fts_set(ftsp, curr, FTS_SKIP);
379 rval = 1;
380 break;
381 }
382 /*
383 * If the directory doesn't exist, create the new
384 * one with the from file mode plus owner RWX bits,
385 * modified by the umask. Trade-off between being
386 * able to write the directory (if from directory is
387 * 555) and not causing a permissions race. If the
388 * umask blocks owner writes, we fail..
389 */
390 if (dne) {
391 if (mkdir(to.p_path,
392 curr->fts_statp->st_mode | S_IRWXU) < 0)
393 err(1, "%s", to.p_path);
394 } else if (!S_ISDIR(to_stat.st_mode)) {
395 errno = ENOTDIR;
396 err(1, "%s", to.p_path);
397 }
398 /*
399 * If not -p and directory didn't exist, set it to be
400 * the same as the from directory, umodified by the
401 * umask; arguably wrong, but it's been that way
402 * forever.
403 */
404 if (pflag && setfile(curr->fts_statp, 0))
405 rval = 1;
406 else if (dne)
407 (void)chmod(to.p_path,
408 curr->fts_statp->st_mode);
409 break;
410 case S_IFBLK:
411 case S_IFCHR:
412 if (Rflag) {
413 if (copy_special(curr->fts_statp, !dne))
414 rval = 1;
415 } else
416 if (copy_file(curr, dne))
417 rval = 1;
418 break;
419 case S_IFIFO:
420 if (Rflag) {
421 if (copy_fifo(curr->fts_statp, !dne))
422 rval = 1;
423 } else
424 if (copy_file(curr, dne))
425 rval = 1;
426 break;
427 default:
428 if (copy_file(curr, dne))
429 rval = 1;
430 break;
431 }
432 }
433 if (errno)
434 err(1, "fts_read");
435 return (rval);
436 }
437
438 /*
439 * mastercmp --
440 * The comparison function for the copy order. The order is to copy
441 * non-directory files before directory files. The reason for this
442 * is because files tend to be in the same cylinder group as their
443 * parent directory, whereas directories tend not to be. Copying the
444 * files first reduces seeking.
445 */
446 int
447 mastercmp(a, b)
448 const FTSENT **a, **b;
449 {
450 int a_info, b_info;
451
452 a_info = (*a)->fts_info;
453 if (a_info == FTS_ERR || a_info == FTS_NS || a_info == FTS_DNR)
454 return (0);
455 b_info = (*b)->fts_info;
456 if (b_info == FTS_ERR || b_info == FTS_NS || b_info == FTS_DNR)
457 return (0);
458 if (a_info == FTS_D)
459 return (-1);
460 if (b_info == FTS_D)
461 return (1);
462 return (0);
463 }
464