utils.c revision 291774
1/*-
2 * Copyright (c) 1991, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef lint
31#if 0
32static char sccsid[] = "@(#)utils.c	8.3 (Berkeley) 4/1/94";
33#endif
34#endif /* not lint */
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: stable/10/bin/cp/utils.c 291774 2015-12-04 17:36:35Z bdrewery $");
37
38#include <sys/types.h>
39#include <sys/acl.h>
40#include <sys/param.h>
41#include <sys/stat.h>
42#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
43#include <sys/mman.h>
44#endif
45
46#include <err.h>
47#include <errno.h>
48#include <fcntl.h>
49#include <fts.h>
50#include <limits.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <sysexits.h>
54#include <unistd.h>
55
56#include "extern.h"
57
58#define	cp_pct(x, y)	((y == 0) ? 0 : (int)(100.0 * (x) / (y)))
59
60/*
61 * Memory strategy threshold, in pages: if physmem is larger then this, use a
62 * large buffer.
63 */
64#define PHYSPAGES_THRESHOLD (32*1024)
65
66/* Maximum buffer size in bytes - do not allow it to grow larger than this. */
67#define BUFSIZE_MAX (2*1024*1024)
68
69/*
70 * Small (default) buffer size in bytes. It's inefficient for this to be
71 * smaller than MAXPHYS.
72 */
73#define BUFSIZE_SMALL (MAXPHYS)
74
75int
76copy_file(const FTSENT *entp, int dne)
77{
78	static char *buf = NULL;
79	static size_t bufsize;
80	struct stat *fs;
81	ssize_t wcount;
82	size_t wresid;
83	off_t wtotal;
84	int ch, checkch, from_fd, rcount, rval, to_fd;
85	char *bufp;
86#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
87	char *p;
88#endif
89
90	from_fd = to_fd = -1;
91	if (!lflag && !sflag &&
92	    (from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
93		warn("%s", entp->fts_path);
94		return (1);
95	}
96
97	fs = entp->fts_statp;
98
99	/*
100	 * If the file exists and we're interactive, verify with the user.
101	 * If the file DNE, set the mode to be the from file, minus setuid
102	 * bits, modified by the umask; arguably wrong, but it makes copying
103	 * executables work right and it's been that way forever.  (The
104	 * other choice is 666 or'ed with the execute bits on the from file
105	 * modified by the umask.)
106	 */
107	if (!dne) {
108#define YESNO "(y/n [n]) "
109		if (nflag) {
110			if (vflag)
111				printf("%s not overwritten\n", to.p_path);
112			rval = 1;
113			goto done;
114		} else if (iflag) {
115			(void)fprintf(stderr, "overwrite %s? %s",
116			    to.p_path, YESNO);
117			checkch = ch = getchar();
118			while (ch != '\n' && ch != EOF)
119				ch = getchar();
120			if (checkch != 'y' && checkch != 'Y') {
121				(void)fprintf(stderr, "not overwritten\n");
122				rval = 1;
123				goto done;
124			}
125		}
126
127		if (fflag) {
128			/*
129			 * Remove existing destination file name create a new
130			 * file.
131			 */
132			(void)unlink(to.p_path);
133			if (!lflag && !sflag) {
134				to_fd = open(to.p_path,
135				    O_WRONLY | O_TRUNC | O_CREAT,
136				    fs->st_mode & ~(S_ISUID | S_ISGID));
137			}
138		} else if (!lflag && !sflag) {
139			/* Overwrite existing destination file name. */
140			to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
141		}
142	} else if (!lflag && !sflag) {
143		to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
144		    fs->st_mode & ~(S_ISUID | S_ISGID));
145	}
146
147	if (!lflag && !sflag && to_fd == -1) {
148		warn("%s", to.p_path);
149		rval = 1;
150		goto done;
151	}
152
153	rval = 0;
154
155	if (!lflag && !sflag) {
156		/*
157		 * Mmap and write if less than 8M (the limit is so we don't
158		 * totally trash memory on big files.  This is really a minor
159		 * hack, but it wins some CPU back.
160		 * Some filesystems, such as smbnetfs, don't support mmap,
161		 * so this is a best-effort attempt.
162		 */
163#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
164		if (S_ISREG(fs->st_mode) && fs->st_size > 0 &&
165		    fs->st_size <= 8 * 1024 * 1024 &&
166		    (p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
167		    MAP_SHARED, from_fd, (off_t)0)) != MAP_FAILED) {
168			wtotal = 0;
169			for (bufp = p, wresid = fs->st_size; ;
170			    bufp += wcount, wresid -= (size_t)wcount) {
171				wcount = write(to_fd, bufp, wresid);
172				if (wcount <= 0)
173					break;
174				wtotal += wcount;
175				if (info) {
176					info = 0;
177					(void)fprintf(stderr,
178					    "%s -> %s %3d%%\n",
179					    entp->fts_path, to.p_path,
180					    cp_pct(wtotal, fs->st_size));
181				}
182				if (wcount >= (ssize_t)wresid)
183					break;
184			}
185			if (wcount != (ssize_t)wresid) {
186				warn("%s", to.p_path);
187				rval = 1;
188			}
189			/* Some systems don't unmap on close(2). */
190			if (munmap(p, fs->st_size) < 0) {
191				warn("%s", entp->fts_path);
192				rval = 1;
193			}
194		} else
195#endif
196		{
197			if (buf == NULL) {
198				/*
199				 * Note that buf and bufsize are static. If
200				 * malloc() fails, it will fail at the start
201				 * and not copy only some files.
202				 */
203				if (sysconf(_SC_PHYS_PAGES) >
204				    PHYSPAGES_THRESHOLD)
205					bufsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
206				else
207					bufsize = BUFSIZE_SMALL;
208				buf = malloc(bufsize);
209				if (buf == NULL)
210					err(1, "Not enough memory");
211			}
212			wtotal = 0;
213			while ((rcount = read(from_fd, buf, bufsize)) > 0) {
214				for (bufp = buf, wresid = rcount; ;
215				    bufp += wcount, wresid -= wcount) {
216					wcount = write(to_fd, bufp, wresid);
217					if (wcount <= 0)
218						break;
219					wtotal += wcount;
220					if (info) {
221						info = 0;
222						(void)fprintf(stderr,
223						    "%s -> %s %3d%%\n",
224						    entp->fts_path, to.p_path,
225						    cp_pct(wtotal, fs->st_size));
226					}
227					if (wcount >= (ssize_t)wresid)
228						break;
229				}
230				if (wcount != (ssize_t)wresid) {
231					warn("%s", to.p_path);
232					rval = 1;
233					break;
234				}
235			}
236			if (rcount < 0) {
237				warn("%s", entp->fts_path);
238				rval = 1;
239			}
240		}
241	} else if (lflag) {
242		if (link(entp->fts_path, to.p_path)) {
243			warn("%s", to.p_path);
244			rval = 1;
245		}
246	} else if (sflag) {
247		if (symlink(entp->fts_path, to.p_path)) {
248			warn("%s", to.p_path);
249			rval = 1;
250		}
251	}
252
253	/*
254	 * Don't remove the target even after an error.  The target might
255	 * not be a regular file, or its attributes might be important,
256	 * or its contents might be irreplaceable.  It would only be safe
257	 * to remove it if we created it and its length is 0.
258	 */
259
260	if (!lflag && !sflag) {
261		if (pflag && setfile(fs, to_fd))
262			rval = 1;
263		if (pflag && preserve_fd_acls(from_fd, to_fd) != 0)
264			rval = 1;
265		if (close(to_fd)) {
266			warn("%s", to.p_path);
267			rval = 1;
268		}
269	}
270
271done:
272	if (from_fd != -1)
273		(void)close(from_fd);
274	return (rval);
275}
276
277int
278copy_link(const FTSENT *p, int exists)
279{
280	int len;
281	char llink[PATH_MAX];
282
283	if (exists && nflag) {
284		if (vflag)
285			printf("%s not overwritten\n", to.p_path);
286		return (1);
287	}
288	if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) {
289		warn("readlink: %s", p->fts_path);
290		return (1);
291	}
292	llink[len] = '\0';
293	if (exists && unlink(to.p_path)) {
294		warn("unlink: %s", to.p_path);
295		return (1);
296	}
297	if (symlink(llink, to.p_path)) {
298		warn("symlink: %s", llink);
299		return (1);
300	}
301	return (pflag ? setfile(p->fts_statp, -1) : 0);
302}
303
304int
305copy_fifo(struct stat *from_stat, int exists)
306{
307
308	if (exists && nflag) {
309		if (vflag)
310			printf("%s not overwritten\n", to.p_path);
311		return (1);
312	}
313	if (exists && unlink(to.p_path)) {
314		warn("unlink: %s", to.p_path);
315		return (1);
316	}
317	if (mkfifo(to.p_path, from_stat->st_mode)) {
318		warn("mkfifo: %s", to.p_path);
319		return (1);
320	}
321	return (pflag ? setfile(from_stat, -1) : 0);
322}
323
324int
325copy_special(struct stat *from_stat, int exists)
326{
327
328	if (exists && nflag) {
329		if (vflag)
330			printf("%s not overwritten\n", to.p_path);
331		return (1);
332	}
333	if (exists && unlink(to.p_path)) {
334		warn("unlink: %s", to.p_path);
335		return (1);
336	}
337	if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
338		warn("mknod: %s", to.p_path);
339		return (1);
340	}
341	return (pflag ? setfile(from_stat, -1) : 0);
342}
343
344int
345setfile(struct stat *fs, int fd)
346{
347	static struct timeval tv[2];
348	struct stat ts;
349	int rval, gotstat, islink, fdval;
350
351	rval = 0;
352	fdval = fd != -1;
353	islink = !fdval && S_ISLNK(fs->st_mode);
354	fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX |
355	    S_IRWXU | S_IRWXG | S_IRWXO;
356
357	TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atim);
358	TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtim);
359	if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) {
360		warn("%sutimes: %s", islink ? "l" : "", to.p_path);
361		rval = 1;
362	}
363	if (fdval ? fstat(fd, &ts) :
364	    (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts)))
365		gotstat = 0;
366	else {
367		gotstat = 1;
368		ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
369		    S_IRWXU | S_IRWXG | S_IRWXO;
370	}
371	/*
372	 * Changing the ownership probably won't succeed, unless we're root
373	 * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
374	 * the mode; current BSD behavior is to remove all setuid bits on
375	 * chown.  If chown fails, lose setuid/setgid bits.
376	 */
377	if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
378		if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
379		    (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
380		    chown(to.p_path, fs->st_uid, fs->st_gid))) {
381			if (errno != EPERM) {
382				warn("chown: %s", to.p_path);
383				rval = 1;
384			}
385			fs->st_mode &= ~(S_ISUID | S_ISGID);
386		}
387
388	if (!gotstat || fs->st_mode != ts.st_mode)
389		if (fdval ? fchmod(fd, fs->st_mode) :
390		    (islink ? lchmod(to.p_path, fs->st_mode) :
391		    chmod(to.p_path, fs->st_mode))) {
392			warn("chmod: %s", to.p_path);
393			rval = 1;
394		}
395
396	if (!gotstat || fs->st_flags != ts.st_flags)
397		if (fdval ?
398		    fchflags(fd, fs->st_flags) :
399		    (islink ? lchflags(to.p_path, fs->st_flags) :
400		    chflags(to.p_path, fs->st_flags))) {
401			warn("chflags: %s", to.p_path);
402			rval = 1;
403		}
404
405	return (rval);
406}
407
408int
409preserve_fd_acls(int source_fd, int dest_fd)
410{
411	acl_t acl;
412	acl_type_t acl_type;
413	int acl_supported = 0, ret, trivial;
414
415	ret = fpathconf(source_fd, _PC_ACL_NFS4);
416	if (ret > 0 ) {
417		acl_supported = 1;
418		acl_type = ACL_TYPE_NFS4;
419	} else if (ret < 0 && errno != EINVAL) {
420		warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", to.p_path);
421		return (1);
422	}
423	if (acl_supported == 0) {
424		ret = fpathconf(source_fd, _PC_ACL_EXTENDED);
425		if (ret > 0 ) {
426			acl_supported = 1;
427			acl_type = ACL_TYPE_ACCESS;
428		} else if (ret < 0 && errno != EINVAL) {
429			warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
430			    to.p_path);
431			return (1);
432		}
433	}
434	if (acl_supported == 0)
435		return (0);
436
437	acl = acl_get_fd_np(source_fd, acl_type);
438	if (acl == NULL) {
439		warn("failed to get acl entries while setting %s", to.p_path);
440		return (1);
441	}
442	if (acl_is_trivial_np(acl, &trivial)) {
443		warn("acl_is_trivial() failed for %s", to.p_path);
444		acl_free(acl);
445		return (1);
446	}
447	if (trivial) {
448		acl_free(acl);
449		return (0);
450	}
451	if (acl_set_fd_np(dest_fd, acl, acl_type) < 0) {
452		warn("failed to set acl entries for %s", to.p_path);
453		acl_free(acl);
454		return (1);
455	}
456	acl_free(acl);
457	return (0);
458}
459
460int
461preserve_dir_acls(struct stat *fs, char *source_dir, char *dest_dir)
462{
463	acl_t (*aclgetf)(const char *, acl_type_t);
464	int (*aclsetf)(const char *, acl_type_t, acl_t);
465	struct acl *aclp;
466	acl_t acl;
467	acl_type_t acl_type;
468	int acl_supported = 0, ret, trivial;
469
470	ret = pathconf(source_dir, _PC_ACL_NFS4);
471	if (ret > 0) {
472		acl_supported = 1;
473		acl_type = ACL_TYPE_NFS4;
474	} else if (ret < 0 && errno != EINVAL) {
475		warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", source_dir);
476		return (1);
477	}
478	if (acl_supported == 0) {
479		ret = pathconf(source_dir, _PC_ACL_EXTENDED);
480		if (ret > 0) {
481			acl_supported = 1;
482			acl_type = ACL_TYPE_ACCESS;
483		} else if (ret < 0 && errno != EINVAL) {
484			warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
485			    source_dir);
486			return (1);
487		}
488	}
489	if (acl_supported == 0)
490		return (0);
491
492	/*
493	 * If the file is a link we will not follow it.
494	 */
495	if (S_ISLNK(fs->st_mode)) {
496		aclgetf = acl_get_link_np;
497		aclsetf = acl_set_link_np;
498	} else {
499		aclgetf = acl_get_file;
500		aclsetf = acl_set_file;
501	}
502	if (acl_type == ACL_TYPE_ACCESS) {
503		/*
504		 * Even if there is no ACL_TYPE_DEFAULT entry here, a zero
505		 * size ACL will be returned. So it is not safe to simply
506		 * check the pointer to see if the default ACL is present.
507		 */
508		acl = aclgetf(source_dir, ACL_TYPE_DEFAULT);
509		if (acl == NULL) {
510			warn("failed to get default acl entries on %s",
511			    source_dir);
512			return (1);
513		}
514		aclp = &acl->ats_acl;
515		if (aclp->acl_cnt != 0 && aclsetf(dest_dir,
516		    ACL_TYPE_DEFAULT, acl) < 0) {
517			warn("failed to set default acl entries on %s",
518			    dest_dir);
519			acl_free(acl);
520			return (1);
521		}
522		acl_free(acl);
523	}
524	acl = aclgetf(source_dir, acl_type);
525	if (acl == NULL) {
526		warn("failed to get acl entries on %s", source_dir);
527		return (1);
528	}
529	if (acl_is_trivial_np(acl, &trivial)) {
530		warn("acl_is_trivial() failed on %s", source_dir);
531		acl_free(acl);
532		return (1);
533	}
534	if (trivial) {
535		acl_free(acl);
536		return (0);
537	}
538	if (aclsetf(dest_dir, acl_type, acl) < 0) {
539		warn("failed to set acl entries on %s", dest_dir);
540		acl_free(acl);
541		return (1);
542	}
543	acl_free(acl);
544	return (0);
545}
546
547void
548usage(void)
549{
550
551	(void)fprintf(stderr, "%s\n%s\n",
552	    "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpsvx] "
553	    "source_file target_file",
554	    "       cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpsvx] "
555	    "source_file ... "
556	    "target_directory");
557	exit(EX_USAGE);
558}
559