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