cp.c revision 1.5 1 /*
2 * Copyright (c) 1988 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * David Hitz of Auspex Systems Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 char copyright[] =
39 "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
40 All rights reserved.\n";
41 #endif /* not lint */
42
43 #ifndef lint
44 /*static char sccsid[] = "from: @(#)cp.c 5.26 (Berkeley) 10/27/91";*/
45 static char rcsid[] = "$Id: cp.c,v 1.5 1993/08/07 03:14:55 mycroft Exp $";
46 #endif /* not lint */
47
48 /*
49 * cp copies source files to target files.
50 *
51 * The global PATH_T structures "to" and "from" always contain paths to the
52 * current source and target files, respectively. Since cp does not change
53 * directories, these paths can be either absolute or dot-realative.
54 *
55 * The basic algorithm is to initialize "to" and "from", and then call the
56 * recursive copy() function to do the actual work. If "from" is a file,
57 * copy copies the data. If "from" is a directory, copy creates the
58 * corresponding "to" directory, and calls itself recursively on all of
59 * the entries in the "from" directory.
60 */
61
62 #include <sys/param.h>
63 #include <sys/stat.h>
64 #include <sys/mman.h>
65 #include <sys/time.h>
66 #include <dirent.h>
67 #include <fcntl.h>
68 #include <errno.h>
69 #include <unistd.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include "extern.h"
74
75 static void copy __P((void));
76 static void copy_dir __P((void));
77 static void copy_fifo __P((struct stat *, int));
78 static void copy_file __P((struct stat *, int));
79 static void copy_link __P((int));
80 static void copy_special __P((struct stat *, int));
81 static void setfile __P((struct stat *, int));
82 static void usage __P((void));
83
84 PATH_T from = { from.p_path, "" };
85 PATH_T to = { to.p_path, "" };
86
87 uid_t myuid;
88 int exit_val, myumask;
89 int iflag, pflag, orflag, rflag;
90 int (*statfcn)();
91 char *progname;
92
93 main(argc, argv)
94 int argc;
95 char **argv;
96 {
97 extern int optind;
98 struct stat to_stat;
99 register int c, r;
100 int symfollow, lstat(), stat();
101 char *old_to, *p;
102
103 /*
104 * The utility cp(1) is used by mv(1) -- except for usage statements,
105 * print the "called as" program name.
106 */
107 progname = (p = rindex(*argv,'/')) ? ++p : *argv;
108
109 symfollow = 0;
110 while ((c = getopt(argc, argv, "Rfhipr")) != EOF) {
111 switch ((char)c) {
112 case 'f':
113 iflag = 0;
114 break;
115 case 'h':
116 symfollow = 1;
117 break;
118 case 'i':
119 iflag = isatty(fileno(stdin));
120 break;
121 case 'p':
122 pflag = 1;
123 break;
124 case 'R':
125 rflag = 1;
126 break;
127 case 'r':
128 orflag = 1;
129 break;
130 case '?':
131 default:
132 usage();
133 break;
134 }
135 }
136 argc -= optind;
137 argv += optind;
138
139 if (argc < 2)
140 usage();
141
142 if (rflag && orflag) {
143 (void)fprintf(stderr,
144 "cp: the -R and -r options are mutually exclusive.\n");
145 exit(1);
146 }
147
148 myuid = getuid();
149
150 /* copy the umask for explicit mode setting */
151 myumask = umask(0);
152 (void)umask(myumask);
153
154 /* consume last argument first. */
155 if (!path_set(&to, argv[--argc]))
156 exit(1);
157
158 statfcn = symfollow || !rflag ? stat : lstat;
159
160 /*
161 * Cp has two distinct cases:
162 *
163 * % cp [-rip] source target
164 * % cp [-rip] source1 ... directory
165 *
166 * In both cases, source can be either a file or a directory.
167 *
168 * In (1), the target becomes a copy of the source. That is, if the
169 * source is a file, the target will be a file, and likewise for
170 * directories.
171 *
172 * In (2), the real target is not directory, but "directory/source".
173 */
174
175 r = stat(to.p_path, &to_stat);
176 if (r == -1 && errno != ENOENT) {
177 err("%s: %s", to.p_path, strerror(errno));
178 exit(1);
179 }
180 if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
181 /*
182 * Case (1). Target is not a directory.
183 */
184 if (argc > 1) {
185 usage();
186 exit(1);
187 }
188 if (!path_set(&from, *argv))
189 exit(1);
190 copy();
191 }
192 else {
193 /*
194 * Case (2). Target is a directory.
195 */
196 for (;; ++argv) {
197 if (!path_set(&from, *argv))
198 continue;
199 if (!(old_to =
200 path_append(&to, path_basename(&from), -1)))
201 continue;
202 copy();
203 if (!--argc)
204 break;
205 path_restore(&to, old_to);
206 }
207 }
208 exit(exit_val);
209 }
210
211 /* copy file or directory at "from" to "to". */
212 static void
213 copy()
214 {
215 struct stat from_stat, to_stat;
216 int dne, statval;
217
218 statval = statfcn(from.p_path, &from_stat);
219 if (statval == -1) {
220 err("%s: %s", from.p_path, strerror(errno));
221 return;
222 }
223
224 /* not an error, but need to remember it happened */
225 if (stat(to.p_path, &to_stat) == -1)
226 dne = 1;
227 else {
228 if (to_stat.st_dev == from_stat.st_dev &&
229 to_stat.st_ino == from_stat.st_ino) {
230 (void)fprintf(stderr,
231 "%s: %s and %s are identical (not copied).\n",
232 progname, to.p_path, from.p_path);
233 exit_val = 1;
234 return;
235 }
236 dne = 0;
237 }
238
239 switch(from_stat.st_mode & S_IFMT) {
240 case S_IFLNK:
241 copy_link(!dne);
242 return;
243 case S_IFDIR:
244 if (!rflag && !orflag) {
245 (void)fprintf(stderr,
246 "%s: %s is a directory (not copied).\n",
247 progname, from.p_path);
248 exit_val = 1;
249 return;
250 }
251 if (dne) {
252 /*
253 * If the directory doesn't exist, create the new
254 * one with the from file mode plus owner RWX bits,
255 * modified by the umask. Trade-off between being
256 * able to write the directory (if from directory is
257 * 555) and not causing a permissions race. If the
258 * umask blocks owner writes cp fails.
259 */
260 if (mkdir(to.p_path, from_stat.st_mode|S_IRWXU) < 0) {
261 err("%s: %s", to.p_path, strerror(errno));
262 return;
263 }
264 }
265 else if (!S_ISDIR(to_stat.st_mode)) {
266 (void)fprintf(stderr, "%s: %s: not a directory.\n",
267 progname, to.p_path);
268 return;
269 }
270 copy_dir();
271 /*
272 * If not -p and directory didn't exist, set it to be the
273 * same as the from directory, umodified by the umask;
274 * arguably wrong, but it's been that way forever.
275 */
276 if (pflag)
277 setfile(&from_stat, 0);
278 else if (dne)
279 (void)chmod(to.p_path, from_stat.st_mode);
280 return;
281 case S_IFCHR:
282 case S_IFBLK:
283 if (rflag) {
284 copy_special(&from_stat, !dne);
285 return;
286 }
287 break;
288 case S_IFIFO:
289 if (rflag) {
290 copy_fifo(&from_stat, !dne);
291 return;
292 }
293 break;
294 }
295 copy_file(&from_stat, dne);
296 }
297
298 static void
299 copy_file(fs, dne)
300 struct stat *fs;
301 int dne;
302 {
303 static char buf[MAXBSIZE];
304 register int from_fd, to_fd, rcount, wcount;
305 struct stat to_stat;
306 char *p;
307
308 if ((from_fd = open(from.p_path, O_RDONLY, 0)) == -1) {
309 err("%s: %s", from.p_path, strerror(errno));
310 return;
311 }
312
313 /*
314 * If the file exists and we're interactive, verify with the user.
315 * If the file DNE, set the mode to be the from file, minus setuid
316 * bits, modified by the umask; arguably wrong, but it makes copying
317 * executables work right and it's been that way forever. (The
318 * other choice is 666 or'ed with the execute bits on the from file
319 * modified by the umask.)
320 */
321 if (!dne) {
322 if (iflag) {
323 int checkch, ch;
324
325 (void)fprintf(stderr, "overwrite %s? ", to.p_path);
326 checkch = ch = getchar();
327 while (ch != '\n' && ch != EOF)
328 ch = getchar();
329 if (checkch != 'y') {
330 (void)close(from_fd);
331 return;
332 }
333 }
334 to_fd = open(to.p_path, O_WRONLY|O_TRUNC, 0);
335 } else
336 to_fd = open(to.p_path, O_WRONLY|O_CREAT|O_TRUNC,
337 fs->st_mode & ~(S_ISUID|S_ISGID));
338
339 if (to_fd == -1) {
340 err("%s: %s", to.p_path, strerror(errno));
341 (void)close(from_fd);
342 return;
343 }
344
345 /*
346 * Mmap and write if less than 8M (the limit is so we don't totally
347 * trash memory on big files. This is really a minor hack, but it
348 * wins some CPU back.
349 */
350 if (fs->st_size <= 8 * 1048576) {
351 if ((p = mmap(NULL, fs->st_size, PROT_READ,
352 MAP_FILE, from_fd, (off_t)0)) == (char *)-1)
353 err("%s: %s", from.p_path, strerror(errno));
354 if (write(to_fd, p, fs->st_size) != fs->st_size)
355 err("%s: %s", to.p_path, strerror(errno));
356 } else {
357 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
358 wcount = write(to_fd, buf, rcount);
359 if (rcount != wcount || wcount == -1) {
360 err("%s: %s", to.p_path, strerror(errno));
361 break;
362 }
363 }
364 if (rcount < 0)
365 err("%s: %s", from.p_path, strerror(errno));
366 }
367 if (pflag)
368 setfile(fs, to_fd);
369 /*
370 * If the source was setuid or setgid, lose the bits unless the
371 * copy is owned by the same user and group.
372 */
373 else if (fs->st_mode & (S_ISUID|S_ISGID) && fs->st_uid == myuid)
374 if (fstat(to_fd, &to_stat))
375 err("%s: %s", to.p_path, strerror(errno));
376 #define RETAINBITS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
377 else if (fs->st_gid == to_stat.st_gid && fchmod(to_fd,
378 fs->st_mode & RETAINBITS & ~myumask))
379 err("%s: %s", to.p_path, strerror(errno));
380 (void)close(from_fd);
381 if (close(to_fd))
382 err("%s: %s", to.p_path, strerror(errno));
383 }
384
385 static void
386 copy_dir()
387 {
388 struct stat from_stat;
389 struct dirent *dp, **dir_list;
390 register int dir_cnt, i;
391 char *old_from, *old_to;
392
393 dir_cnt = scandir(from.p_path, &dir_list, NULL, NULL);
394 if (dir_cnt == -1) {
395 (void)fprintf(stderr, "%s: can't read directory %s.\n",
396 progname, from.p_path);
397 exit_val = 1;
398 }
399
400 /*
401 * Instead of handling directory entries in the order they appear
402 * on disk, do non-directory files before directory files.
403 * There are two reasons to do directories last. The first is
404 * efficiency. Files tend to be in the same cylinder group as
405 * their parent, whereas directories tend not to be. Copying files
406 * all at once reduces seeking. Second, deeply nested tree's
407 * could use up all the file descriptors if we didn't close one
408 * directory before recursivly starting on the next.
409 */
410 /* copy files */
411 for (i = 0; i < dir_cnt; ++i) {
412 dp = dir_list[i];
413 if (dp->d_namlen <= 2 && dp->d_name[0] == '.'
414 && (dp->d_name[1] == NULL || dp->d_name[1] == '.'))
415 goto done;
416 if (!(old_from =
417 path_append(&from, dp->d_name, (int)dp->d_namlen)))
418 goto done;
419
420 if (statfcn(from.p_path, &from_stat) < 0) {
421 err("%s: %s", dp->d_name, strerror(errno));
422 path_restore(&from, old_from);
423 goto done;
424 }
425 if (S_ISDIR(from_stat.st_mode)) {
426 path_restore(&from, old_from);
427 continue;
428 }
429 if (old_to = path_append(&to, dp->d_name, (int)dp->d_namlen)) {
430 copy();
431 path_restore(&to, old_to);
432 }
433 path_restore(&from, old_from);
434 done: dir_list[i] = NULL;
435 free(dp);
436 }
437
438 /* copy directories */
439 for (i = 0; i < dir_cnt; ++i) {
440 dp = dir_list[i];
441 if (!dp)
442 continue;
443 if (!(old_from =
444 path_append(&from, dp->d_name, (int)dp->d_namlen))) {
445 free(dp);
446 continue;
447 }
448 if (!(old_to =
449 path_append(&to, dp->d_name, (int)dp->d_namlen))) {
450 free(dp);
451 path_restore(&from, old_from);
452 continue;
453 }
454 copy();
455 free(dp);
456 path_restore(&from, old_from);
457 path_restore(&to, old_to);
458 }
459 free(dir_list);
460 }
461
462 static void
463 copy_link(exists)
464 int exists;
465 {
466 int len;
467 char link[MAXPATHLEN];
468
469 if ((len = readlink(from.p_path, link, sizeof(link))) == -1) {
470 err("readlink: %s: %s", from.p_path, strerror(errno));
471 return;
472 }
473 link[len] = '\0';
474 if (exists && unlink(to.p_path)) {
475 err("unlink: %s: %s", to.p_path, strerror(errno));
476 return;
477 }
478 if (symlink(link, to.p_path)) {
479 err("symlink: %s: %s", link, strerror(errno));
480 return;
481 }
482 }
483
484 static void
485 copy_fifo(from_stat, exists)
486 struct stat *from_stat;
487 int exists;
488 {
489 if (exists && unlink(to.p_path)) {
490 err("unlink: %s: %s", to.p_path, strerror(errno));
491 return;
492 }
493 if (mkfifo(to.p_path, from_stat->st_mode)) {
494 err("mkfifo: %s: %s", to.p_path, strerror(errno));
495 return;
496 }
497 if (pflag)
498 setfile(from_stat, 0);
499 }
500
501 static void
502 copy_special(from_stat, exists)
503 struct stat *from_stat;
504 int exists;
505 {
506 if (exists && unlink(to.p_path)) {
507 err("unlink: %s: %s", to.p_path, strerror(errno));
508 return;
509 }
510 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
511 err("mknod: %s: %s", to.p_path, strerror(errno));
512 return;
513 }
514 if (pflag)
515 setfile(from_stat, 0);
516 }
517
518 static void
519 setfile(fs, fd)
520 register struct stat *fs;
521 int fd;
522 {
523 static struct timeval tv[2];
524
525 fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
526
527 tv[0].tv_sec = fs->st_atime;
528 tv[1].tv_sec = fs->st_mtime;
529 if (utimes(to.p_path, tv))
530 err("utimes: %s: %s", to.p_path, strerror(errno));
531 /*
532 * Changing the ownership probably won't succeed, unless we're root
533 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
534 * the mode; current BSD behavior is to remove all setuid bits on
535 * chown. If chown fails, lose setuid/setgid bits.
536 */
537 if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
538 chown(to.p_path, fs->st_uid, fs->st_gid)) {
539 if (errno != EPERM)
540 err("chown: %s: %s", to.p_path, strerror(errno));
541 fs->st_mode &= ~(S_ISUID|S_ISGID);
542 }
543 if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode))
544 err("chown: %s: %s", to.p_path, strerror(errno));
545 }
546
547 static void
548 usage()
549 {
550 (void)fprintf(stderr,
551 "usage: cp [-Rfhip] src target;\n cp [-Rfhip] src1 ... srcN directory\n");
552 exit(1);
553 }
554
555 #if __STDC__
556 #include <stdarg.h>
557 #else
558 #include <varargs.h>
559 #endif
560
561 void
562 #if __STDC__
563 err(const char *fmt, ...)
564 #else
565 err(fmt, va_alist)
566 char *fmt;
567 va_dcl
568 #endif
569 {
570 va_list ap;
571 #if __STDC__
572 va_start(ap, fmt);
573 #else
574 va_start(ap);
575 #endif
576 (void)fprintf(stderr, "%s: ", progname);
577 (void)vfprintf(stderr, fmt, ap);
578 va_end(ap);
579 (void)fprintf(stderr, "\n");
580 exit_val = 1;
581 }
582