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