Home | History | Annotate | Line # | Download | only in cp
cp.c revision 1.7
      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.7 1993/11/09 18:57:54 jtc 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' && 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 		if (munmap(p, fs->st_size) < 0)
    357 			err("%s: %s", from.p_path, strerror(errno));
    358 	} else {
    359 		while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
    360 			wcount = write(to_fd, buf, rcount);
    361 			if (rcount != wcount || wcount == -1) {
    362 				err("%s: %s", to.p_path, strerror(errno));
    363 				break;
    364 			}
    365 		}
    366 		if (rcount < 0)
    367 			err("%s: %s", from.p_path, strerror(errno));
    368 	}
    369 	if (pflag)
    370 		setfile(fs, to_fd);
    371 	/*
    372 	 * If the source was setuid or setgid, lose the bits unless the
    373 	 * copy is owned by the same user and group.
    374 	 */
    375 	else if (fs->st_mode & (S_ISUID|S_ISGID) && fs->st_uid == myuid)
    376 		if (fstat(to_fd, &to_stat))
    377 			err("%s: %s", to.p_path, strerror(errno));
    378 #define	RETAINBITS	(S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
    379 		else if (fs->st_gid == to_stat.st_gid && fchmod(to_fd,
    380 		    fs->st_mode & RETAINBITS & ~myumask))
    381 			err("%s: %s", to.p_path, strerror(errno));
    382 	(void)close(from_fd);
    383 	if (close(to_fd))
    384 		err("%s: %s", to.p_path, strerror(errno));
    385 }
    386 
    387 static void
    388 copy_dir()
    389 {
    390 	struct stat from_stat;
    391 	struct dirent *dp, **dir_list;
    392 	register int dir_cnt, i;
    393 	char *old_from, *old_to;
    394 
    395 	dir_cnt = scandir(from.p_path, &dir_list, NULL, NULL);
    396 	if (dir_cnt == -1) {
    397 		(void)fprintf(stderr, "%s: can't read directory %s.\n",
    398 		    progname, from.p_path);
    399 		exit_val = 1;
    400 	}
    401 
    402 	/*
    403 	 * Instead of handling directory entries in the order they appear
    404 	 * on disk, do non-directory files before directory files.
    405 	 * There are two reasons to do directories last.  The first is
    406 	 * efficiency.  Files tend to be in the same cylinder group as
    407 	 * their parent, whereas directories tend not to be.  Copying files
    408 	 * all at once reduces seeking.  Second, deeply nested tree's
    409 	 * could use up all the file descriptors if we didn't close one
    410 	 * directory before recursivly starting on the next.
    411 	 */
    412 	/* copy files */
    413 	for (i = 0; i < dir_cnt; ++i) {
    414 		dp = dir_list[i];
    415 		if (dp->d_namlen <= 2 && dp->d_name[0] == '.'
    416 		    && (dp->d_name[1] == NULL || dp->d_name[1] == '.'))
    417 			goto done;
    418 		if (!(old_from =
    419 		    path_append(&from, dp->d_name, (int)dp->d_namlen)))
    420 			goto done;
    421 
    422 		if (statfcn(from.p_path, &from_stat) < 0) {
    423 			err("%s: %s", dp->d_name, strerror(errno));
    424 			path_restore(&from, old_from);
    425 			goto done;
    426 		}
    427 		if (S_ISDIR(from_stat.st_mode)) {
    428 			path_restore(&from, old_from);
    429 			continue;
    430 		}
    431 		if (old_to = path_append(&to, dp->d_name, (int)dp->d_namlen)) {
    432 			copy();
    433 			path_restore(&to, old_to);
    434 		}
    435 		path_restore(&from, old_from);
    436 done:		dir_list[i] = NULL;
    437 		free(dp);
    438 	}
    439 
    440 	/* copy directories */
    441 	for (i = 0; i < dir_cnt; ++i) {
    442 		dp = dir_list[i];
    443 		if (!dp)
    444 			continue;
    445 		if (!(old_from =
    446 		    path_append(&from, dp->d_name, (int)dp->d_namlen))) {
    447 			free(dp);
    448 			continue;
    449 		}
    450 		if (!(old_to =
    451 		    path_append(&to, dp->d_name, (int)dp->d_namlen))) {
    452 			free(dp);
    453 			path_restore(&from, old_from);
    454 			continue;
    455 		}
    456 		copy();
    457 		free(dp);
    458 		path_restore(&from, old_from);
    459 		path_restore(&to, old_to);
    460 	}
    461 	free(dir_list);
    462 }
    463 
    464 static void
    465 copy_link(exists)
    466 	int exists;
    467 {
    468 	int len;
    469 	char link[MAXPATHLEN];
    470 
    471 	if ((len = readlink(from.p_path, link, sizeof(link))) == -1) {
    472 		err("readlink: %s: %s", from.p_path, strerror(errno));
    473 		return;
    474 	}
    475 	link[len] = '\0';
    476 	if (exists && unlink(to.p_path)) {
    477 		err("unlink: %s: %s", to.p_path, strerror(errno));
    478 		return;
    479 	}
    480 	if (symlink(link, to.p_path)) {
    481 		err("symlink: %s: %s", link, strerror(errno));
    482 		return;
    483 	}
    484 }
    485 
    486 static void
    487 copy_fifo(from_stat, exists)
    488 	struct stat *from_stat;
    489 	int exists;
    490 {
    491 	if (exists && unlink(to.p_path)) {
    492 		err("unlink: %s: %s", to.p_path, strerror(errno));
    493 		return;
    494 	}
    495 	if (mkfifo(to.p_path, from_stat->st_mode)) {
    496 		err("mkfifo: %s: %s", to.p_path, strerror(errno));
    497 		return;
    498 	}
    499 	if (pflag)
    500 		setfile(from_stat, 0);
    501 }
    502 
    503 static void
    504 copy_special(from_stat, exists)
    505 	struct stat *from_stat;
    506 	int exists;
    507 {
    508 	if (exists && unlink(to.p_path)) {
    509 		err("unlink: %s: %s", to.p_path, strerror(errno));
    510 		return;
    511 	}
    512 	if (mknod(to.p_path, from_stat->st_mode,  from_stat->st_rdev)) {
    513 		err("mknod: %s: %s", to.p_path, strerror(errno));
    514 		return;
    515 	}
    516 	if (pflag)
    517 		setfile(from_stat, 0);
    518 }
    519 
    520 static void
    521 setfile(fs, fd)
    522 	register struct stat *fs;
    523 	int fd;
    524 {
    525 	static struct timeval tv[2];
    526 
    527 	fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
    528 
    529 	tv[0].tv_sec = fs->st_atime;
    530 	tv[1].tv_sec = fs->st_mtime;
    531 	if (utimes(to.p_path, tv))
    532 		err("utimes: %s: %s", to.p_path, strerror(errno));
    533 	/*
    534 	 * Changing the ownership probably won't succeed, unless we're root
    535 	 * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
    536 	 * the mode; current BSD behavior is to remove all setuid bits on
    537 	 * chown.  If chown fails, lose setuid/setgid bits.
    538 	 */
    539 	if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
    540 	    chown(to.p_path, fs->st_uid, fs->st_gid)) {
    541 		if (errno != EPERM)
    542 			err("chown: %s: %s", to.p_path, strerror(errno));
    543 		fs->st_mode &= ~(S_ISUID|S_ISGID);
    544 	}
    545 	if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode))
    546 		err("chown: %s: %s", to.p_path, strerror(errno));
    547 }
    548 
    549 static void
    550 usage()
    551 {
    552 	(void)fprintf(stderr,
    553 "usage: cp [-Rfhip] src target;\n       cp [-Rfhip] src1 ... srcN directory\n");
    554 	exit(1);
    555 }
    556 
    557 #if __STDC__
    558 #include <stdarg.h>
    559 #else
    560 #include <varargs.h>
    561 #endif
    562 
    563 void
    564 #if __STDC__
    565 err(const char *fmt, ...)
    566 #else
    567 err(fmt, va_alist)
    568 	char *fmt;
    569         va_dcl
    570 #endif
    571 {
    572 	va_list ap;
    573 #if __STDC__
    574 	va_start(ap, fmt);
    575 #else
    576 	va_start(ap);
    577 #endif
    578 	(void)fprintf(stderr, "%s: ", progname);
    579 	(void)vfprintf(stderr, fmt, ap);
    580 	va_end(ap);
    581 	(void)fprintf(stderr, "\n");
    582 	exit_val = 1;
    583 }
    584