Home | History | Annotate | Line # | Download | only in cp
cp.c revision 1.33
      1 /* $NetBSD: cp.c,v 1.33 2003/08/04 22:31:22 jschauma 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.33 2003/08/04 22:31:22 jschauma 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 
     72 #include <err.h>
     73 #include <errno.h>
     74 #include <fts.h>
     75 #include <locale.h>
     76 #include <stdlib.h>
     77 #include <stdio.h>
     78 #include <string.h>
     79 #include <unistd.h>
     80 
     81 #include "extern.h"
     82 
     83 #define	STRIP_TRAILING_SLASH(p) {					\
     84         while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/')	\
     85                 *--(p).p_end = '\0';					\
     86 }
     87 
     88 PATH_T to = { to.p_path, "" };
     89 
     90 uid_t myuid;
     91 int Rflag, fflag, iflag, pflag, rflag, stdout_ok, vflag;
     92 mode_t myumask;
     93 
     94 enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
     95 
     96 int 	main(int, char *[]);
     97 int 	copy(char *[], enum op, int);
     98 int 	mastercmp(const FTSENT **, const FTSENT **);
     99 
    100 int
    101 main(int argc, char *argv[])
    102 {
    103 	struct stat to_stat, tmp_stat;
    104 	enum op type;
    105 	int Hflag, Lflag, Pflag, ch, fts_options, r;
    106 	char *target;
    107 
    108 	(void)setlocale(LC_ALL, "");
    109 
    110 	Hflag = Lflag = Pflag = Rflag = 0;
    111 	while ((ch = getopt(argc, argv, "HLPRfiprv")) != -1)
    112 		switch (ch) {
    113 		case 'H':
    114 			Hflag = 1;
    115 			Lflag = Pflag = 0;
    116 			break;
    117 		case 'L':
    118 			Lflag = 1;
    119 			Hflag = Pflag = 0;
    120 			break;
    121 		case 'P':
    122 			Pflag = 1;
    123 			Hflag = Lflag = 0;
    124 			break;
    125 		case 'R':
    126 			Rflag = 1;
    127 			break;
    128 		case 'f':
    129 			fflag = 1;
    130 			iflag = 0;
    131 			break;
    132 		case 'i':
    133 			iflag = isatty(fileno(stdin));
    134 			fflag = 0;
    135 			break;
    136 		case 'p':
    137 			pflag = 1;
    138 			break;
    139 		case 'r':
    140 			rflag = 1;
    141 			break;
    142 		case 'v':
    143 			vflag = 1;
    144 			break;
    145 		case '?':
    146 		default:
    147 			usage();
    148 			break;
    149 		}
    150 	argc -= optind;
    151 	argv += optind;
    152 
    153 	if (argc < 2)
    154 		usage();
    155 
    156 	stdout_ok = isatty(STDOUT_FILENO);
    157 
    158 	fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
    159 	if (rflag) {
    160 		if (Rflag) {
    161 			errx(EXIT_FAILURE,
    162 		    "the -R and -r options may not be specified together.");
    163 			/* NOTREACHED */
    164 		}
    165 		if (Hflag || Lflag || Pflag) {
    166 			errx(EXIT_FAILURE,
    167 	"the -H, -L, and -P options may not be specified with the -r option.");
    168 			/* NOTREACHED */
    169 		}
    170 		fts_options &= ~FTS_PHYSICAL;
    171 		fts_options |= FTS_LOGICAL;
    172 	}
    173 	if (Rflag) {
    174 		if (Hflag)
    175 			fts_options |= FTS_COMFOLLOW;
    176 		if (Lflag) {
    177 			fts_options &= ~FTS_PHYSICAL;
    178 			fts_options |= FTS_LOGICAL;
    179 		}
    180 	} else {
    181 		fts_options &= ~FTS_PHYSICAL;
    182 		fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
    183 	}
    184 
    185 	myuid = getuid();
    186 
    187 	/* Copy the umask for explicit mode setting. */
    188 	myumask = umask(0);
    189 	(void)umask(myumask);
    190 
    191 	/* Save the target base in "to". */
    192 	target = argv[--argc];
    193 	if (strlen(target) > MAXPATHLEN) {
    194 		errx(EXIT_FAILURE, "%s: name too long", printescaped(target));
    195 		/* NOTREACHED */
    196 	}
    197 	(void)strcpy(to.p_path, target);
    198 	to.p_end = to.p_path + strlen(to.p_path);
    199         if (to.p_path == to.p_end) {
    200 		*to.p_end++ = '.';
    201 		*to.p_end = 0;
    202 	}
    203         STRIP_TRAILING_SLASH(to);
    204 	to.target_end = to.p_end;
    205 
    206 	/* Set end of argument list for fts(3). */
    207 	argv[argc] = NULL;
    208 
    209 	/*
    210 	 * Cp has two distinct cases:
    211 	 *
    212 	 * cp [-R] source target
    213 	 * cp [-R] source1 ... sourceN directory
    214 	 *
    215 	 * In both cases, source can be either a file or a directory.
    216 	 *
    217 	 * In (1), the target becomes a copy of the source. That is, if the
    218 	 * source is a file, the target will be a file, and likewise for
    219 	 * directories.
    220 	 *
    221 	 * In (2), the real target is not directory, but "directory/source".
    222 	 */
    223 	r = stat(to.p_path, &to_stat);
    224 	if (r == -1 && errno != ENOENT) {
    225 		err(EXIT_FAILURE, "%s", printescaped(to.p_path));
    226 		/* NOTREACHED */
    227 	}
    228 	if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
    229 		/*
    230 		 * Case (1).  Target is not a directory.
    231 		 */
    232 		if (argc > 1) {
    233 			usage();
    234 			exit(1);
    235 		}
    236 		/*
    237 		 * Need to detect the case:
    238 		 *	cp -R dir foo
    239 		 * Where dir is a directory and foo does not exist, where
    240 		 * we want pathname concatenations turned on but not for
    241 		 * the initial mkdir().
    242 		 */
    243 		if (r == -1) {
    244 			if (rflag || (Rflag && (Lflag || Hflag)))
    245 				r = stat(*argv, &tmp_stat);
    246 			else
    247 				r = lstat(*argv, &tmp_stat);
    248 			if (r == -1) {
    249 				err(EXIT_FAILURE, "%s", printescaped(*argv));
    250 				/* NOTREACHED */
    251 			}
    252 
    253 			if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag))
    254 				type = DIR_TO_DNE;
    255 			else
    256 				type = FILE_TO_FILE;
    257 		} else
    258 			type = FILE_TO_FILE;
    259 	} else {
    260 		/*
    261 		 * Case (2).  Target is a directory.
    262 		 */
    263 		type = FILE_TO_DIR;
    264 	}
    265 
    266 	exit(copy(argv, type, fts_options));
    267 	/* NOTREACHED */
    268 }
    269 
    270 int
    271 copy(char *argv[], enum op type, int fts_options)
    272 {
    273 	struct stat to_stat;
    274 	FTS *ftsp;
    275 	FTSENT *curr;
    276 	int base, dne, nlen, rval;
    277 	char *p, *tmp, *fn;
    278 
    279 	base = 0;	/* XXX gcc -Wuninitialized (see comment below) */
    280 
    281 	if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
    282 		err(EXIT_FAILURE, "%s", printescaped(argv[0]));
    283 		/* NOTREACHED */
    284 	for (rval = 0; (curr = fts_read(ftsp)) != NULL;) {
    285 		switch (curr->fts_info) {
    286 		case FTS_NS:
    287 		case FTS_DNR:
    288 		case FTS_ERR:
    289 			fn = printescaped(curr->fts_path);
    290 			warnx("%s: %s", fn, strerror(curr->fts_errno));
    291 			free(fn);
    292 			rval = 1;
    293 			continue;
    294 		case FTS_DC:			/* Warn, continue. */
    295 			fn = printescaped(curr->fts_path);
    296 			warnx("%s: directory causes a cycle", fn);
    297 			free(fn);
    298 			rval = 1;
    299 			continue;
    300 		}
    301 
    302 		/*
    303 		 * If we are in case (2) or (3) above, we need to append the
    304                  * source name to the target name.
    305                  */
    306 		if (type != FILE_TO_FILE) {
    307 			if ((curr->fts_namelen +
    308 			    to.target_end - to.p_path + 1) > MAXPATHLEN) {
    309 				char *tn;
    310 				tn = printescaped(to.p_path);
    311 				fn = printescaped(curr->fts_name);
    312 				warnx("%s/%s: name too long (not copied)", tn, fn);
    313 				free(fn);
    314 				free(tn);
    315 				rval = 1;
    316 				continue;
    317 			}
    318 
    319 			/*
    320 			 * Need to remember the roots of traversals to create
    321 			 * correct pathnames.  If there's a directory being
    322 			 * copied to a non-existent directory, e.g.
    323 			 *	cp -R a/dir noexist
    324 			 * the resulting path name should be noexist/foo, not
    325 			 * noexist/dir/foo (where foo is a file in dir), which
    326 			 * is the case where the target exists.
    327 			 *
    328 			 * Also, check for "..".  This is for correct path
    329 			 * concatentation for paths ending in "..", e.g.
    330 			 *	cp -R .. /tmp
    331 			 * Paths ending in ".." are changed to ".".  This is
    332 			 * tricky, but seems the easiest way to fix the problem.
    333 			 *
    334 			 * XXX
    335 			 * Since the first level MUST be FTS_ROOTLEVEL, base
    336 			 * is always initialized.
    337 			 */
    338 			if (curr->fts_level == FTS_ROOTLEVEL) {
    339 				if (type != DIR_TO_DNE) {
    340 					p = strrchr(curr->fts_path, '/');
    341 					base = (p == NULL) ? 0 :
    342 					    (int)(p - curr->fts_path + 1);
    343 
    344 					if (!strcmp(&curr->fts_path[base],
    345 					    ".."))
    346 						base += 1;
    347 				} else
    348 					base = curr->fts_pathlen;
    349 			}
    350 
    351 			p = &curr->fts_path[base];
    352 			nlen = curr->fts_pathlen - base;
    353 
    354 			tmp = to.target_end;
    355 			if (*p != '/' && *(tmp - 1) != '/')
    356 				*tmp++ = '/';
    357 			*tmp = 0;
    358 
    359 			(void)strncat(tmp, p, nlen);
    360 			to.p_end = tmp + nlen;
    361 			*to.p_end = 0;
    362 			STRIP_TRAILING_SLASH(to);
    363 		}
    364 
    365 		/* Not an error but need to remember it happened */
    366 		if (stat(to.p_path, &to_stat) == -1)
    367 			dne = 1;
    368 		else {
    369 			if (to_stat.st_dev == curr->fts_statp->st_dev &&
    370 			    to_stat.st_ino == curr->fts_statp->st_ino) {
    371 				warnx("%s and %s are identical (not copied).",
    372 				    to.p_path, curr->fts_path);
    373 				rval = 1;
    374 				if (S_ISDIR(curr->fts_statp->st_mode))
    375 					(void)fts_set(ftsp, curr, FTS_SKIP);
    376 				continue;
    377 			}
    378 			if (!S_ISDIR(curr->fts_statp->st_mode) &&
    379 			    S_ISDIR(to_stat.st_mode)) {
    380 				char *tn;
    381 				tn = printescaped(to.p_path);
    382 				fn = printescaped(curr->fts_path);
    383 		warnx("cannot overwrite directory %s with non-directory %s",
    384 				    tn, fn);
    385 				free(tn);
    386 				free(fn);
    387 				rval = 1;
    388 				continue;
    389 			}
    390 			dne = 0;
    391 		}
    392 
    393 		switch (curr->fts_statp->st_mode & S_IFMT) {
    394 		case S_IFLNK:
    395 			/* Catch special case of a non dangling symlink */
    396 			if((fts_options & FTS_LOGICAL) ||
    397 			   ((fts_options & FTS_COMFOLLOW) && curr->fts_level == 0)) {
    398 				if (copy_file(curr, dne))
    399 					rval = 1;
    400 			} else {
    401 				if (copy_link(curr, !dne))
    402 					rval = 1;
    403 			}
    404 			break;
    405 		case S_IFDIR:
    406 			if (!Rflag && !rflag) {
    407 				if (curr->fts_info == FTS_D) {
    408 					fn = printescaped(curr->fts_path);
    409 					warnx("%s is a directory (not copied).",
    410 					    fn);
    411 					free(fn);
    412 				}
    413 				(void)fts_set(ftsp, curr, FTS_SKIP);
    414 				rval = 1;
    415 				break;
    416 			}
    417 
    418                         /*
    419                          * Directories get noticed twice:
    420                          *  In the first pass, create it if needed.
    421                          *  In the second pass, after the children have been copied, set the permissions.
    422                          */
    423 			if (curr->fts_info == FTS_D) /* First pass */
    424 			{
    425 				/*
    426 				 * If the directory doesn't exist, create the new
    427 				 * one with the from file mode plus owner RWX bits,
    428 				 * modified by the umask.  Trade-off between being
    429 				 * able to write the directory (if from directory is
    430 				 * 555) and not causing a permissions race.  If the
    431 				 * umask blocks owner writes, we fail..
    432 				 */
    433 				if (dne) {
    434 					if (mkdir(to.p_path,
    435 					    curr->fts_statp->st_mode | S_IRWXU) < 0)
    436 						err(EXIT_FAILURE, "%s",
    437 						    printescaped(to.p_path));
    438 						/* NOTREACHED */
    439 				} else if (!S_ISDIR(to_stat.st_mode)) {
    440 					errno = ENOTDIR;
    441 					err(EXIT_FAILURE, "%s",
    442 						printescaped(to.p_path));
    443 					/* NOTREACHED */
    444 				}
    445 			}
    446 			else if (curr->fts_info == FTS_DP) /* Second pass */
    447 			{
    448 	                        /*
    449 				 * If not -p and directory didn't exist, set it to be
    450 				 * the same as the from directory, umodified by the
    451                         	 * umask; arguably wrong, but it's been that way
    452                         	 * forever.
    453 				 */
    454 				if (pflag && setfile(curr->fts_statp, 0))
    455 					rval = 1;
    456 				else if (dne)
    457 					(void)chmod(to.p_path,
    458 					    curr->fts_statp->st_mode);
    459 			}
    460 			else
    461                         {
    462 				fn = printescaped(curr->fts_path);
    463                         	warnx("directory %s encountered when not expected.", fn);
    464 				free(fn);
    465                         	rval = 1;
    466                                 break;
    467                         }
    468 
    469 			break;
    470 		case S_IFBLK:
    471 		case S_IFCHR:
    472 			if (Rflag) {
    473 				if (copy_special(curr->fts_statp, !dne))
    474 					rval = 1;
    475 			} else
    476 				if (copy_file(curr, dne))
    477 					rval = 1;
    478 			break;
    479 		case S_IFIFO:
    480 			if (Rflag) {
    481 				if (copy_fifo(curr->fts_statp, !dne))
    482 					rval = 1;
    483 			} else
    484 				if (copy_file(curr, dne))
    485 					rval = 1;
    486 			break;
    487 		default:
    488 			if (copy_file(curr, dne))
    489 				rval = 1;
    490 			break;
    491 		}
    492 		if (vflag) {
    493 			char *tn;
    494 			fn = printescaped(curr->fts_path);
    495 			tn = printescaped(to.p_path);
    496 			(void)printf("%s -> %s\n", fn, tn);
    497 			free(fn);
    498 			free(tn);
    499 		}
    500 	}
    501 	if (errno) {
    502 		err(EXIT_FAILURE, "fts_read");
    503 		/* NOTREACHED */
    504 	}
    505 	return (rval);
    506 }
    507 
    508 /*
    509  * mastercmp --
    510  *	The comparison function for the copy order.  The order is to copy
    511  *	non-directory files before directory files.  The reason for this
    512  *	is because files tend to be in the same cylinder group as their
    513  *	parent directory, whereas directories tend not to be.  Copying the
    514  *	files first reduces seeking.
    515  */
    516 int
    517 mastercmp(const FTSENT **a, const FTSENT **b)
    518 {
    519 	int a_info, b_info;
    520 
    521 	a_info = (*a)->fts_info;
    522 	if (a_info == FTS_ERR || a_info == FTS_NS || a_info == FTS_DNR)
    523 		return (0);
    524 	b_info = (*b)->fts_info;
    525 	if (b_info == FTS_ERR || b_info == FTS_NS || b_info == FTS_DNR)
    526 		return (0);
    527 	if (a_info == FTS_D)
    528 		return (-1);
    529 	if (b_info == FTS_D)
    530 		return (1);
    531 	return (0);
    532 }
    533