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