cp.c revision 1.19 1 /* $NetBSD: cp.c,v 1.19 1997/09/14 07:15:28 lukem 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.19 1997/09/14 07:15:28 lukem 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 int 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 stat(*argv, &tmp_stat);
234 else
235 lstat(*argv, &tmp_stat);
236
237 if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag))
238 type = DIR_TO_DNE;
239 else
240 type = FILE_TO_FILE;
241 } else
242 type = FILE_TO_FILE;
243 } else
244 /*
245 * Case (2). Target is a directory.
246 */
247 type = FILE_TO_DIR;
248
249 exit (copy(argv, type, fts_options));
250 }
251
252 int
253 copy(argv, type, fts_options)
254 char *argv[];
255 enum op type;
256 int fts_options;
257 {
258 struct stat to_stat;
259 FTS *ftsp;
260 FTSENT *curr;
261 int base, dne, nlen, rval;
262 char *p, *tmp;
263
264 base = 0; /* XXX gcc -Wuninitialized (see comment below) */
265
266 if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
267 err(1, argv[0]);
268 for (rval = 0; (curr = fts_read(ftsp)) != NULL;) {
269 switch (curr->fts_info) {
270 case FTS_NS:
271 case FTS_ERR:
272 warnx("%s: %s",
273 curr->fts_path, strerror(curr->fts_errno));
274 rval = 1;
275 continue;
276 case FTS_DC: /* Warn, continue. */
277 warnx("%s: directory causes a cycle", curr->fts_path);
278 rval = 1;
279 continue;
280 case FTS_DP: /* Ignore, continue. */
281 continue;
282 }
283
284 /*
285 * If we are in case (2) or (3) above, we need to append the
286 * source name to the target name.
287 */
288 if (type != FILE_TO_FILE) {
289 if ((curr->fts_namelen +
290 to.target_end - to.p_path + 1) > MAXPATHLEN) {
291 warnx("%s/%s: name too long (not copied)",
292 to.p_path, curr->fts_name);
293 rval = 1;
294 continue;
295 }
296
297 /*
298 * Need to remember the roots of traversals to create
299 * correct pathnames. If there's a directory being
300 * copied to a non-existent directory, e.g.
301 * cp -R a/dir noexist
302 * the resulting path name should be noexist/foo, not
303 * noexist/dir/foo (where foo is a file in dir), which
304 * is the case where the target exists.
305 *
306 * Also, check for "..". This is for correct path
307 * concatentation for paths ending in "..", e.g.
308 * cp -R .. /tmp
309 * Paths ending in ".." are changed to ".". This is
310 * tricky, but seems the easiest way to fix the problem.
311 *
312 * XXX
313 * Since the first level MUST be FTS_ROOTLEVEL, base
314 * is always initialized.
315 */
316 if (curr->fts_level == FTS_ROOTLEVEL)
317 if (type != DIR_TO_DNE) {
318 p = strrchr(curr->fts_path, '/');
319 base = (p == NULL) ? 0 :
320 (int)(p - curr->fts_path + 1);
321
322 if (!strcmp(&curr->fts_path[base],
323 ".."))
324 base += 1;
325 } else
326 base = curr->fts_pathlen;
327
328 p = &curr->fts_path[base];
329 nlen = curr->fts_pathlen - base;
330
331 tmp = to.target_end;
332 if (*p != '/' && *(tmp - 1) != '/')
333 *tmp++ = '/';
334 *tmp = 0;
335
336 (void)strncat(tmp, p, nlen);
337 to.p_end = tmp + nlen;
338 *to.p_end = 0;
339 STRIP_TRAILING_SLASH(to);
340 }
341
342 /* Not an error but need to remember it happened */
343 if (stat(to.p_path, &to_stat) == -1)
344 dne = 1;
345 else {
346 if (to_stat.st_dev == curr->fts_statp->st_dev &&
347 to_stat.st_ino == curr->fts_statp->st_ino) {
348 warnx("%s and %s are identical (not copied).",
349 to.p_path, curr->fts_path);
350 rval = 1;
351 if (S_ISDIR(curr->fts_statp->st_mode))
352 (void)fts_set(ftsp, curr, FTS_SKIP);
353 continue;
354 }
355 if (!S_ISDIR(curr->fts_statp->st_mode) &&
356 S_ISDIR(to_stat.st_mode)) {
357 warnx("cannot overwrite directory %s with non-directory %s",
358 to.p_path, curr->fts_path);
359 rval = 1;
360 continue;
361 }
362 dne = 0;
363 }
364
365 switch (curr->fts_statp->st_mode & S_IFMT) {
366 case S_IFLNK:
367 if (copy_link(curr, !dne))
368 rval = 1;
369 break;
370 case S_IFDIR:
371 if (!Rflag && !rflag) {
372 warnx("%s is a directory (not copied).",
373 curr->fts_path);
374 (void)fts_set(ftsp, curr, FTS_SKIP);
375 rval = 1;
376 break;
377 }
378 /*
379 * If the directory doesn't exist, create the new
380 * one with the from file mode plus owner RWX bits,
381 * modified by the umask. Trade-off between being
382 * able to write the directory (if from directory is
383 * 555) and not causing a permissions race. If the
384 * umask blocks owner writes, we fail..
385 */
386 if (dne) {
387 if (mkdir(to.p_path,
388 curr->fts_statp->st_mode | S_IRWXU) < 0)
389 err(1, "%s", to.p_path);
390 } else if (!S_ISDIR(to_stat.st_mode)) {
391 errno = ENOTDIR;
392 err(1, "%s", to.p_path);
393 }
394 /*
395 * If not -p and directory didn't exist, set it to be
396 * the same as the from directory, umodified by the
397 * umask; arguably wrong, but it's been that way
398 * forever.
399 */
400 if (pflag && setfile(curr->fts_statp, 0))
401 rval = 1;
402 else if (dne)
403 (void)chmod(to.p_path,
404 curr->fts_statp->st_mode);
405 break;
406 case S_IFBLK:
407 case S_IFCHR:
408 if (Rflag) {
409 if (copy_special(curr->fts_statp, !dne))
410 rval = 1;
411 } else
412 if (copy_file(curr, dne))
413 rval = 1;
414 break;
415 case S_IFIFO:
416 if (Rflag) {
417 if (copy_fifo(curr->fts_statp, !dne))
418 rval = 1;
419 } else
420 if (copy_file(curr, dne))
421 rval = 1;
422 break;
423 default:
424 if (copy_file(curr, dne))
425 rval = 1;
426 break;
427 }
428 }
429 if (errno)
430 err(1, "fts_read");
431 return (rval);
432 }
433
434 /*
435 * mastercmp --
436 * The comparison function for the copy order. The order is to copy
437 * non-directory files before directory files. The reason for this
438 * is because files tend to be in the same cylinder group as their
439 * parent directory, whereas directories tend not to be. Copying the
440 * files first reduces seeking.
441 */
442 int
443 mastercmp(a, b)
444 const FTSENT **a, **b;
445 {
446 int a_info, b_info;
447
448 a_info = (*a)->fts_info;
449 if (a_info == FTS_ERR || a_info == FTS_NS || a_info == FTS_DNR)
450 return (0);
451 b_info = (*b)->fts_info;
452 if (b_info == FTS_ERR || b_info == FTS_NS || b_info == FTS_DNR)
453 return (0);
454 if (a_info == FTS_D)
455 return (-1);
456 if (b_info == FTS_D)
457 return (1);
458 return (0);
459 }
460