xref: /freebsd-src/bin/cp/utils.c (revision 3d7c8f088796ec6ceb1d54005657dee78fd01fbe)
14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993, 1994
54b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes  *
74b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
84b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
94b88c807SRodney W. Grimes  * are met:
104b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
114b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
124b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
134b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
144b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
164b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
174b88c807SRodney W. Grimes  *    without specific prior written permission.
184b88c807SRodney W. Grimes  *
194b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
204b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
214b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
224b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
234b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
244b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
254b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
264b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
274b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
284b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
294b88c807SRodney W. Grimes  * SUCH DAMAGE.
304b88c807SRodney W. Grimes  */
314b88c807SRodney W. Grimes 
324b88c807SRodney W. Grimes #include <sys/param.h>
33cb96a0efSDag-Erling Smørgrav #include <sys/acl.h>
344b88c807SRodney W. Grimes #include <sys/stat.h>
354b88c807SRodney W. Grimes 
364b88c807SRodney W. Grimes #include <err.h>
374b88c807SRodney W. Grimes #include <errno.h>
384b88c807SRodney W. Grimes #include <fcntl.h>
394b88c807SRodney W. Grimes #include <fts.h>
4034f9c106SWarner Losh #include <limits.h>
414b88c807SRodney W. Grimes #include <stdio.h>
4226f6b0fbSDag-Erling Smørgrav #include <stdlib.h>
43127432d7SMichael Haro #include <sysexits.h>
444b88c807SRodney W. Grimes #include <unistd.h>
454b88c807SRodney W. Grimes 
464b88c807SRodney W. Grimes #include "extern.h"
47eb82e1a1SMaxim Konovalov 
48eb82e1a1SMaxim Konovalov #define	cp_pct(x, y)	((y == 0) ? 0 : (int)(100.0 * (x) / (y)))
494b88c807SRodney W. Grimes 
50724fd448SBryan Drewery /*
51724fd448SBryan Drewery  * Memory strategy threshold, in pages: if physmem is larger then this, use a
52724fd448SBryan Drewery  * large buffer.
53724fd448SBryan Drewery  */
54e9cbc9a7SIvan Voras #define PHYSPAGES_THRESHOLD (32*1024)
55e9cbc9a7SIvan Voras 
56724fd448SBryan Drewery /* Maximum buffer size in bytes - do not allow it to grow larger than this. */
57e9cbc9a7SIvan Voras #define BUFSIZE_MAX (2*1024*1024)
58e9cbc9a7SIvan Voras 
59724fd448SBryan Drewery /*
60724fd448SBryan Drewery  * Small (default) buffer size in bytes. It's inefficient for this to be
61724fd448SBryan Drewery  * smaller than MAXPHYS.
62724fd448SBryan Drewery  */
63e9cbc9a7SIvan Voras #define BUFSIZE_SMALL (MAXPHYS)
64e9cbc9a7SIvan Voras 
65d002316fSDag-Erling Smørgrav /*
66d002316fSDag-Erling Smørgrav  * Prompt used in -i case.
67d002316fSDag-Erling Smørgrav  */
68d002316fSDag-Erling Smørgrav #define YESNO "(y/n [n]) "
69d002316fSDag-Erling Smørgrav 
70c98a764cSRick Macklem static ssize_t
copy_fallback(int from_fd,int to_fd)716c85042aSDag-Erling Smørgrav copy_fallback(int from_fd, int to_fd)
721ea95ba2SAlan Somers {
736c85042aSDag-Erling Smørgrav 	static char *buf = NULL;
746c85042aSDag-Erling Smørgrav 	static size_t bufsize;
75c98a764cSRick Macklem 	ssize_t rcount, wresid, wcount = 0;
761ea95ba2SAlan Somers 	char *bufp;
771ea95ba2SAlan Somers 
786c85042aSDag-Erling Smørgrav 	if (buf == NULL) {
796c85042aSDag-Erling Smørgrav 		if (sysconf(_SC_PHYS_PAGES) > PHYSPAGES_THRESHOLD)
806c85042aSDag-Erling Smørgrav 			bufsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
816c85042aSDag-Erling Smørgrav 		else
826c85042aSDag-Erling Smørgrav 			bufsize = BUFSIZE_SMALL;
836c85042aSDag-Erling Smørgrav 		buf = malloc(bufsize);
846c85042aSDag-Erling Smørgrav 		if (buf == NULL)
856c85042aSDag-Erling Smørgrav 			err(1, "Not enough memory");
866c85042aSDag-Erling Smørgrav 	}
871ea95ba2SAlan Somers 	rcount = read(from_fd, buf, bufsize);
881ea95ba2SAlan Somers 	if (rcount <= 0)
891ea95ba2SAlan Somers 		return (rcount);
901ea95ba2SAlan Somers 	for (bufp = buf, wresid = rcount; ; bufp += wcount, wresid -= wcount) {
911ea95ba2SAlan Somers 		wcount = write(to_fd, bufp, wresid);
921ea95ba2SAlan Somers 		if (wcount <= 0)
931ea95ba2SAlan Somers 			break;
948b864151SCollin Funk 		if (wcount >= wresid)
951ea95ba2SAlan Somers 			break;
961ea95ba2SAlan Somers 	}
971ea95ba2SAlan Somers 	return (wcount < 0 ? wcount : rcount);
981ea95ba2SAlan Somers }
991ea95ba2SAlan Somers 
1004b88c807SRodney W. Grimes int
copy_file(const FTSENT * entp,int dne)101ba8acd9dSMark Murray copy_file(const FTSENT *entp, int dne)
1024b88c807SRodney W. Grimes {
1039075d4cfSDag-Erling Smørgrav 	struct stat sb, *fs;
10435b7759cSAlexander Motin 	ssize_t wcount;
1056a861e9bSMaxim Konovalov 	off_t wtotal;
106c98a764cSRick Macklem 	int ch, checkch, from_fd, rval, to_fd;
1071ea95ba2SAlan Somers 	int use_copy_file_range = 1;
1084b88c807SRodney W. Grimes 
1099075d4cfSDag-Erling Smørgrav 	fs = entp->fts_statp;
1107ea2450fSBryan Drewery 	from_fd = to_fd = -1;
1119075d4cfSDag-Erling Smørgrav 	if (!lflag && !sflag) {
1129075d4cfSDag-Erling Smørgrav 		if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) < 0 ||
1139075d4cfSDag-Erling Smørgrav 		    fstat(from_fd, &sb) != 0) {
1144b88c807SRodney W. Grimes 			warn("%s", entp->fts_path);
115*3d7c8f08SPierre Pronchery 			if (from_fd >= 0)
116*3d7c8f08SPierre Pronchery 				(void)close(from_fd);
1174b88c807SRodney W. Grimes 			return (1);
1184b88c807SRodney W. Grimes 		}
1199075d4cfSDag-Erling Smørgrav 		/*
1209075d4cfSDag-Erling Smørgrav 		 * Check that the file hasn't been replaced with one of a
1219075d4cfSDag-Erling Smørgrav 		 * different type.  This can happen if we've been asked to
1229075d4cfSDag-Erling Smørgrav 		 * copy something which is actively being modified and
1239075d4cfSDag-Erling Smørgrav 		 * lost the race, or if we've been asked to copy something
1249075d4cfSDag-Erling Smørgrav 		 * like /proc/X/fd/Y which stat(2) reports as S_IFREG but
1259075d4cfSDag-Erling Smørgrav 		 * is actually something else once you open it.
1269075d4cfSDag-Erling Smørgrav 		 */
1279075d4cfSDag-Erling Smørgrav 		if ((sb.st_mode & S_IFMT) != (fs->st_mode & S_IFMT)) {
1289075d4cfSDag-Erling Smørgrav 			warnx("%s: File changed", entp->fts_path);
129*3d7c8f08SPierre Pronchery 			(void)close(from_fd);
1309075d4cfSDag-Erling Smørgrav 			return (1);
1319075d4cfSDag-Erling Smørgrav 		}
1329075d4cfSDag-Erling Smørgrav 	}
1334b88c807SRodney W. Grimes 
1344b88c807SRodney W. Grimes 	/*
1354b88c807SRodney W. Grimes 	 * If the file exists and we're interactive, verify with the user.
1364b88c807SRodney W. Grimes 	 * If the file DNE, set the mode to be the from file, minus setuid
1374b88c807SRodney W. Grimes 	 * bits, modified by the umask; arguably wrong, but it makes copying
1384b88c807SRodney W. Grimes 	 * executables work right and it's been that way forever.  (The
1394b88c807SRodney W. Grimes 	 * other choice is 666 or'ed with the execute bits on the from file
1404b88c807SRodney W. Grimes 	 * modified by the umask.)
1414b88c807SRodney W. Grimes 	 */
1424b88c807SRodney W. Grimes 	if (!dne) {
143786c276fSJohan Karlsson 		if (nflag) {
144786c276fSJohan Karlsson 			if (vflag)
145786c276fSJohan Karlsson 				printf("%s not overwritten\n", to.p_path);
1467ea2450fSBryan Drewery 			rval = 1;
1477ea2450fSBryan Drewery 			goto done;
148786c276fSJohan Karlsson 		} else if (iflag) {
149ab215c67SWolfram Schneider 			(void)fprintf(stderr, "overwrite %s? %s",
150ab215c67SWolfram Schneider 			    to.p_path, YESNO);
1514b88c807SRodney W. Grimes 			checkch = ch = getchar();
1524b88c807SRodney W. Grimes 			while (ch != '\n' && ch != EOF)
1534b88c807SRodney W. Grimes 				ch = getchar();
1546add522fSWolfram Schneider 			if (checkch != 'y' && checkch != 'Y') {
155ab215c67SWolfram Schneider 				(void)fprintf(stderr, "not overwritten\n");
1567ea2450fSBryan Drewery 				rval = 1;
1577ea2450fSBryan Drewery 				goto done;
1584b88c807SRodney W. Grimes 			}
1594b88c807SRodney W. Grimes 		}
1606add522fSWolfram Schneider 
1616add522fSWolfram Schneider 		if (fflag) {
162d002316fSDag-Erling Smørgrav 			/* remove existing destination file */
1636add522fSWolfram Schneider 			(void)unlink(to.p_path);
16438509270SDag-Erling Smørgrav 			dne = 1;
16514376e1fSBryan Drewery 		}
166d002316fSDag-Erling Smørgrav 	}
167d002316fSDag-Erling Smørgrav 
168d002316fSDag-Erling Smørgrav 	rval = 0;
169d002316fSDag-Erling Smørgrav 
170d002316fSDag-Erling Smørgrav 	if (lflag) {
171d002316fSDag-Erling Smørgrav 		if (link(entp->fts_path, to.p_path) != 0) {
172d002316fSDag-Erling Smørgrav 			warn("%s", to.p_path);
173d002316fSDag-Erling Smørgrav 			rval = 1;
174d002316fSDag-Erling Smørgrav 		}
175d002316fSDag-Erling Smørgrav 		goto done;
176d002316fSDag-Erling Smørgrav 	}
177d002316fSDag-Erling Smørgrav 
178d002316fSDag-Erling Smørgrav 	if (sflag) {
179d002316fSDag-Erling Smørgrav 		if (symlink(entp->fts_path, to.p_path) != 0) {
180d002316fSDag-Erling Smørgrav 			warn("%s", to.p_path);
181d002316fSDag-Erling Smørgrav 			rval = 1;
182d002316fSDag-Erling Smørgrav 		}
183d002316fSDag-Erling Smørgrav 		goto done;
184d002316fSDag-Erling Smørgrav 	}
185d002316fSDag-Erling Smørgrav 
18638509270SDag-Erling Smørgrav 	if (!dne) {
187d002316fSDag-Erling Smørgrav 		/* overwrite existing destination file */
1884b88c807SRodney W. Grimes 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
189d002316fSDag-Erling Smørgrav 	} else {
190d002316fSDag-Erling Smørgrav 		/* create new destination file */
1914b88c807SRodney W. Grimes 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
1924b88c807SRodney W. Grimes 		    fs->st_mode & ~(S_ISUID | S_ISGID));
1936fa36377SJulian Elischer 	}
194d002316fSDag-Erling Smørgrav 	if (to_fd == -1) {
1954b88c807SRodney W. Grimes 		warn("%s", to.p_path);
1967ea2450fSBryan Drewery 		rval = 1;
1977ea2450fSBryan Drewery 		goto done;
1984b88c807SRodney W. Grimes 	}
1994b88c807SRodney W. Grimes 
20000d321a2SMatthew N. Dodd 	wtotal = 0;
2011ea95ba2SAlan Somers 	do {
2021ea95ba2SAlan Somers 		if (use_copy_file_range) {
20335b7759cSAlexander Motin 			wcount = copy_file_range(from_fd, NULL,
204c98a764cSRick Macklem 			    to_fd, NULL, SSIZE_MAX, 0);
205c5b5f2d8SMateusz Guzik 			if (wcount < 0 && errno == EINVAL) {
206d002316fSDag-Erling Smørgrav 				/* probably a non-seekable descriptor */
2071ea95ba2SAlan Somers 				use_copy_file_range = 0;
2081ea95ba2SAlan Somers 			}
2091ea95ba2SAlan Somers 		}
2101ea95ba2SAlan Somers 		if (!use_copy_file_range) {
2116c85042aSDag-Erling Smørgrav 			wcount = copy_fallback(from_fd, to_fd);
2121ea95ba2SAlan Somers 		}
21335b7759cSAlexander Motin 		wtotal += wcount;
21400d321a2SMatthew N. Dodd 		if (info) {
21500d321a2SMatthew N. Dodd 			info = 0;
21600d321a2SMatthew N. Dodd 			(void)fprintf(stderr,
21700d321a2SMatthew N. Dodd 			    "%s -> %s %3d%%\n",
21800d321a2SMatthew N. Dodd 			    entp->fts_path, to.p_path,
21900d321a2SMatthew N. Dodd 			    cp_pct(wtotal, fs->st_size));
22000d321a2SMatthew N. Dodd 		}
22135b7759cSAlexander Motin 	} while (wcount > 0);
22235b7759cSAlexander Motin 	if (wcount < 0) {
2234b88c807SRodney W. Grimes 		warn("%s", entp->fts_path);
2244b88c807SRodney W. Grimes 		rval = 1;
2254b88c807SRodney W. Grimes 	}
2264b88c807SRodney W. Grimes 
2276a834fc3SBruce Evans 	/*
2286a834fc3SBruce Evans 	 * Don't remove the target even after an error.  The target might
2296a834fc3SBruce Evans 	 * not be a regular file, or its attributes might be important,
23046be34b9SKris Kennaway 	 * or its contents might be irreplaceable.  It would only be safe
2316a834fc3SBruce Evans 	 * to remove it if we created it and its length is 0.
2326a834fc3SBruce Evans 	 */
2334b88c807SRodney W. Grimes 	if (pflag && setfile(fs, to_fd))
2344b88c807SRodney W. Grimes 		rval = 1;
2359b4261c9SChristian S.J. Peron 	if (pflag && preserve_fd_acls(from_fd, to_fd) != 0)
2369b4261c9SChristian S.J. Peron 		rval = 1;
2374b88c807SRodney W. Grimes 	if (close(to_fd)) {
2384b88c807SRodney W. Grimes 		warn("%s", to.p_path);
2394b88c807SRodney W. Grimes 		rval = 1;
2404b88c807SRodney W. Grimes 	}
2412aafc4d4SEdward Tomasz Napierala 
2427ea2450fSBryan Drewery done:
2437ea2450fSBryan Drewery 	if (from_fd != -1)
2442aafc4d4SEdward Tomasz Napierala 		(void)close(from_fd);
2454b88c807SRodney W. Grimes 	return (rval);
2464b88c807SRodney W. Grimes }
2474b88c807SRodney W. Grimes 
2484b88c807SRodney W. Grimes int
copy_link(const FTSENT * p,int exists)249ba8acd9dSMark Murray copy_link(const FTSENT *p, int exists)
2504b88c807SRodney W. Grimes {
251cb96a0efSDag-Erling Smørgrav 	ssize_t len;
2528bd08b5fSMark Murray 	char llink[PATH_MAX];
2534b88c807SRodney W. Grimes 
254111a5d7cSMark Johnston 	if (exists && nflag) {
255111a5d7cSMark Johnston 		if (vflag)
256111a5d7cSMark Johnston 			printf("%s not overwritten\n", to.p_path);
257111a5d7cSMark Johnston 		return (1);
258111a5d7cSMark Johnston 	}
2598bd08b5fSMark Murray 	if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) {
2604b88c807SRodney W. Grimes 		warn("readlink: %s", p->fts_path);
2614b88c807SRodney W. Grimes 		return (1);
2624b88c807SRodney W. Grimes 	}
2638bd08b5fSMark Murray 	llink[len] = '\0';
2644b88c807SRodney W. Grimes 	if (exists && unlink(to.p_path)) {
2654b88c807SRodney W. Grimes 		warn("unlink: %s", to.p_path);
2664b88c807SRodney W. Grimes 		return (1);
2674b88c807SRodney W. Grimes 	}
2688bd08b5fSMark Murray 	if (symlink(llink, to.p_path)) {
2698bd08b5fSMark Murray 		warn("symlink: %s", llink);
2704b88c807SRodney W. Grimes 		return (1);
2714b88c807SRodney W. Grimes 	}
272a6b05ab6SJohn-Mark Gurney 	return (pflag ? setfile(p->fts_statp, -1) : 0);
2734b88c807SRodney W. Grimes }
2744b88c807SRodney W. Grimes 
2754b88c807SRodney W. Grimes int
copy_fifo(struct stat * from_stat,int exists)2765dce647cSWarner Losh copy_fifo(struct stat *from_stat, int exists)
2774b88c807SRodney W. Grimes {
278111a5d7cSMark Johnston 
279111a5d7cSMark Johnston 	if (exists && nflag) {
280111a5d7cSMark Johnston 		if (vflag)
281111a5d7cSMark Johnston 			printf("%s not overwritten\n", to.p_path);
282111a5d7cSMark Johnston 		return (1);
283111a5d7cSMark Johnston 	}
2844b88c807SRodney W. Grimes 	if (exists && unlink(to.p_path)) {
2854b88c807SRodney W. Grimes 		warn("unlink: %s", to.p_path);
2864b88c807SRodney W. Grimes 		return (1);
2874b88c807SRodney W. Grimes 	}
2884b88c807SRodney W. Grimes 	if (mkfifo(to.p_path, from_stat->st_mode)) {
2894b88c807SRodney W. Grimes 		warn("mkfifo: %s", to.p_path);
2904b88c807SRodney W. Grimes 		return (1);
2914b88c807SRodney W. Grimes 	}
292a6b05ab6SJohn-Mark Gurney 	return (pflag ? setfile(from_stat, -1) : 0);
2934b88c807SRodney W. Grimes }
2944b88c807SRodney W. Grimes 
2954b88c807SRodney W. Grimes int
copy_special(struct stat * from_stat,int exists)2965dce647cSWarner Losh copy_special(struct stat *from_stat, int exists)
2974b88c807SRodney W. Grimes {
298111a5d7cSMark Johnston 
299111a5d7cSMark Johnston 	if (exists && nflag) {
300111a5d7cSMark Johnston 		if (vflag)
301111a5d7cSMark Johnston 			printf("%s not overwritten\n", to.p_path);
302111a5d7cSMark Johnston 		return (1);
303111a5d7cSMark Johnston 	}
3044b88c807SRodney W. Grimes 	if (exists && unlink(to.p_path)) {
3054b88c807SRodney W. Grimes 		warn("unlink: %s", to.p_path);
3064b88c807SRodney W. Grimes 		return (1);
3074b88c807SRodney W. Grimes 	}
3084b88c807SRodney W. Grimes 	if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
3094b88c807SRodney W. Grimes 		warn("mknod: %s", to.p_path);
3104b88c807SRodney W. Grimes 		return (1);
3114b88c807SRodney W. Grimes 	}
312a6b05ab6SJohn-Mark Gurney 	return (pflag ? setfile(from_stat, -1) : 0);
3134b88c807SRodney W. Grimes }
3144b88c807SRodney W. Grimes 
3154b88c807SRodney W. Grimes int
setfile(struct stat * fs,int fd)3165dce647cSWarner Losh setfile(struct stat *fs, int fd)
3174b88c807SRodney W. Grimes {
3185faf2ae1SJilles Tjoelker 	static struct timespec tspec[2];
3194faaa037SPeter Wemm 	struct stat ts;
320a6b05ab6SJohn-Mark Gurney 	int rval, gotstat, islink, fdval;
3214b88c807SRodney W. Grimes 
3224b88c807SRodney W. Grimes 	rval = 0;
323a6b05ab6SJohn-Mark Gurney 	fdval = fd != -1;
324a6b05ab6SJohn-Mark Gurney 	islink = !fdval && S_ISLNK(fs->st_mode);
325eca0f509SBruce Evans 	fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX |
326eca0f509SBruce Evans 	    S_IRWXU | S_IRWXG | S_IRWXO;
3274b88c807SRodney W. Grimes 
3285faf2ae1SJilles Tjoelker 	tspec[0] = fs->st_atim;
3295faf2ae1SJilles Tjoelker 	tspec[1] = fs->st_mtim;
330347b9f6dSJilles Tjoelker 	if (fdval ? futimens(fd, tspec) : utimensat(AT_FDCWD, to.p_path, tspec,
3315faf2ae1SJilles Tjoelker 	    islink ? AT_SYMLINK_NOFOLLOW : 0)) {
3325faf2ae1SJilles Tjoelker 		warn("utimensat: %s", to.p_path);
3334b88c807SRodney W. Grimes 		rval = 1;
3344b88c807SRodney W. Grimes 	}
335a6b05ab6SJohn-Mark Gurney 	if (fdval ? fstat(fd, &ts) :
336a6b05ab6SJohn-Mark Gurney 	    (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts)))
3374faaa037SPeter Wemm 		gotstat = 0;
3384faaa037SPeter Wemm 	else {
3394faaa037SPeter Wemm 		gotstat = 1;
3404faaa037SPeter Wemm 		ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
3414faaa037SPeter Wemm 		    S_IRWXU | S_IRWXG | S_IRWXO;
3424faaa037SPeter Wemm 	}
3434b88c807SRodney W. Grimes 	/*
3444b88c807SRodney W. Grimes 	 * Changing the ownership probably won't succeed, unless we're root
3454b88c807SRodney W. Grimes 	 * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
3464b88c807SRodney W. Grimes 	 * the mode; current BSD behavior is to remove all setuid bits on
3474b88c807SRodney W. Grimes 	 * chown.  If chown fails, lose setuid/setgid bits.
3484b88c807SRodney W. Grimes 	 */
3494faaa037SPeter Wemm 	if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
350a6b05ab6SJohn-Mark Gurney 		if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
351a6b05ab6SJohn-Mark Gurney 		    (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
352a6b05ab6SJohn-Mark Gurney 		    chown(to.p_path, fs->st_uid, fs->st_gid))) {
3534b88c807SRodney W. Grimes 			if (errno != EPERM) {
3544b88c807SRodney W. Grimes 				warn("chown: %s", to.p_path);
3554b88c807SRodney W. Grimes 				rval = 1;
3564b88c807SRodney W. Grimes 			}
3574b88c807SRodney W. Grimes 			fs->st_mode &= ~(S_ISUID | S_ISGID);
3584b88c807SRodney W. Grimes 		}
3594faaa037SPeter Wemm 
3604faaa037SPeter Wemm 	if (!gotstat || fs->st_mode != ts.st_mode)
361a6b05ab6SJohn-Mark Gurney 		if (fdval ? fchmod(fd, fs->st_mode) :
362a6b05ab6SJohn-Mark Gurney 		    (islink ? lchmod(to.p_path, fs->st_mode) :
363a6b05ab6SJohn-Mark Gurney 		    chmod(to.p_path, fs->st_mode))) {
364048e4976SStephen McKay 			warn("chmod: %s", to.p_path);
3654b88c807SRodney W. Grimes 			rval = 1;
3664b88c807SRodney W. Grimes 		}
3674b88c807SRodney W. Grimes 
3685a52e3d0SWarner Losh 	if (!Nflag && (!gotstat || fs->st_flags != ts.st_flags))
369a6b05ab6SJohn-Mark Gurney 		if (fdval ?
370a6b05ab6SJohn-Mark Gurney 		    fchflags(fd, fs->st_flags) :
371f2db7573SJilles Tjoelker 		    (islink ? lchflags(to.p_path, fs->st_flags) :
372a6b05ab6SJohn-Mark Gurney 		    chflags(to.p_path, fs->st_flags))) {
3733e7e3b5bSWarner Losh 			/*
3743e7e3b5bSWarner Losh 			 * NFS doesn't support chflags; ignore errors unless
3753e7e3b5bSWarner Losh 			 * there's reason to believe we're losing bits.  (Note,
3763e7e3b5bSWarner Losh 			 * this still won't be right if the server supports
3773e7e3b5bSWarner Losh 			 * flags and we were trying to *remove* flags on a file
3783e7e3b5bSWarner Losh 			 * that we copied, i.e., that we didn't create.)
3793e7e3b5bSWarner Losh 			 */
3803e7e3b5bSWarner Losh 			if (errno != EOPNOTSUPP || fs->st_flags != 0) {
3814b88c807SRodney W. Grimes 				warn("chflags: %s", to.p_path);
3824b88c807SRodney W. Grimes 				rval = 1;
3834b88c807SRodney W. Grimes 			}
3843e7e3b5bSWarner Losh 		}
3854faaa037SPeter Wemm 
3864b88c807SRodney W. Grimes 	return (rval);
3874b88c807SRodney W. Grimes }
3884b88c807SRodney W. Grimes 
3899b4261c9SChristian S.J. Peron int
preserve_fd_acls(int source_fd,int dest_fd)3909b4261c9SChristian S.J. Peron preserve_fd_acls(int source_fd, int dest_fd)
3919b4261c9SChristian S.J. Peron {
3929b4261c9SChristian S.J. Peron 	acl_t acl;
3932689ba27SEdward Tomasz Napierala 	acl_type_t acl_type;
3942689ba27SEdward Tomasz Napierala 	int acl_supported = 0, ret, trivial;
3959b4261c9SChristian S.J. Peron 
3962689ba27SEdward Tomasz Napierala 	ret = fpathconf(source_fd, _PC_ACL_NFS4);
3972689ba27SEdward Tomasz Napierala 	if (ret > 0 ) {
3982689ba27SEdward Tomasz Napierala 		acl_supported = 1;
3992689ba27SEdward Tomasz Napierala 		acl_type = ACL_TYPE_NFS4;
4002689ba27SEdward Tomasz Napierala 	} else if (ret < 0 && errno != EINVAL) {
4012689ba27SEdward Tomasz Napierala 		warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", to.p_path);
4022689ba27SEdward Tomasz Napierala 		return (1);
4032689ba27SEdward Tomasz Napierala 	}
4042689ba27SEdward Tomasz Napierala 	if (acl_supported == 0) {
4052689ba27SEdward Tomasz Napierala 		ret = fpathconf(source_fd, _PC_ACL_EXTENDED);
4062689ba27SEdward Tomasz Napierala 		if (ret > 0 ) {
4072689ba27SEdward Tomasz Napierala 			acl_supported = 1;
4082689ba27SEdward Tomasz Napierala 			acl_type = ACL_TYPE_ACCESS;
4092689ba27SEdward Tomasz Napierala 		} else if (ret < 0 && errno != EINVAL) {
4102689ba27SEdward Tomasz Napierala 			warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
4112689ba27SEdward Tomasz Napierala 			    to.p_path);
4122689ba27SEdward Tomasz Napierala 			return (1);
4132689ba27SEdward Tomasz Napierala 		}
4142689ba27SEdward Tomasz Napierala 	}
4152689ba27SEdward Tomasz Napierala 	if (acl_supported == 0)
4169b4261c9SChristian S.J. Peron 		return (0);
4172689ba27SEdward Tomasz Napierala 
4182689ba27SEdward Tomasz Napierala 	acl = acl_get_fd_np(source_fd, acl_type);
4199b4261c9SChristian S.J. Peron 	if (acl == NULL) {
4209b4261c9SChristian S.J. Peron 		warn("failed to get acl entries while setting %s", to.p_path);
4219b4261c9SChristian S.J. Peron 		return (1);
4229b4261c9SChristian S.J. Peron 	}
4232689ba27SEdward Tomasz Napierala 	if (acl_is_trivial_np(acl, &trivial)) {
4242689ba27SEdward Tomasz Napierala 		warn("acl_is_trivial() failed for %s", to.p_path);
4252689ba27SEdward Tomasz Napierala 		acl_free(acl);
4269b4261c9SChristian S.J. Peron 		return (1);
4279b4261c9SChristian S.J. Peron 	}
4282689ba27SEdward Tomasz Napierala 	if (trivial) {
4292689ba27SEdward Tomasz Napierala 		acl_free(acl);
4302689ba27SEdward Tomasz Napierala 		return (0);
4312689ba27SEdward Tomasz Napierala 	}
4322689ba27SEdward Tomasz Napierala 	if (acl_set_fd_np(dest_fd, acl, acl_type) < 0) {
4332689ba27SEdward Tomasz Napierala 		warn("failed to set acl entries for %s", to.p_path);
4342689ba27SEdward Tomasz Napierala 		acl_free(acl);
4352689ba27SEdward Tomasz Napierala 		return (1);
4362689ba27SEdward Tomasz Napierala 	}
4372689ba27SEdward Tomasz Napierala 	acl_free(acl);
4389b4261c9SChristian S.J. Peron 	return (0);
4399b4261c9SChristian S.J. Peron }
4409b4261c9SChristian S.J. Peron 
4419b4261c9SChristian S.J. Peron int
preserve_dir_acls(struct stat * fs,char * source_dir,char * dest_dir)4429b4261c9SChristian S.J. Peron preserve_dir_acls(struct stat *fs, char *source_dir, char *dest_dir)
4439b4261c9SChristian S.J. Peron {
4449b4261c9SChristian S.J. Peron 	acl_t (*aclgetf)(const char *, acl_type_t);
4459b4261c9SChristian S.J. Peron 	int (*aclsetf)(const char *, acl_type_t, acl_t);
4469b4261c9SChristian S.J. Peron 	struct acl *aclp;
4479b4261c9SChristian S.J. Peron 	acl_t acl;
4482689ba27SEdward Tomasz Napierala 	acl_type_t acl_type;
4492689ba27SEdward Tomasz Napierala 	int acl_supported = 0, ret, trivial;
4509b4261c9SChristian S.J. Peron 
4512689ba27SEdward Tomasz Napierala 	ret = pathconf(source_dir, _PC_ACL_NFS4);
4522689ba27SEdward Tomasz Napierala 	if (ret > 0) {
4532689ba27SEdward Tomasz Napierala 		acl_supported = 1;
4542689ba27SEdward Tomasz Napierala 		acl_type = ACL_TYPE_NFS4;
4552689ba27SEdward Tomasz Napierala 	} else if (ret < 0 && errno != EINVAL) {
4562689ba27SEdward Tomasz Napierala 		warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", source_dir);
4572689ba27SEdward Tomasz Napierala 		return (1);
4582689ba27SEdward Tomasz Napierala 	}
4592689ba27SEdward Tomasz Napierala 	if (acl_supported == 0) {
4602689ba27SEdward Tomasz Napierala 		ret = pathconf(source_dir, _PC_ACL_EXTENDED);
4612689ba27SEdward Tomasz Napierala 		if (ret > 0) {
4622689ba27SEdward Tomasz Napierala 			acl_supported = 1;
4632689ba27SEdward Tomasz Napierala 			acl_type = ACL_TYPE_ACCESS;
4642689ba27SEdward Tomasz Napierala 		} else if (ret < 0 && errno != EINVAL) {
4652689ba27SEdward Tomasz Napierala 			warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
4662689ba27SEdward Tomasz Napierala 			    source_dir);
4672689ba27SEdward Tomasz Napierala 			return (1);
4682689ba27SEdward Tomasz Napierala 		}
4692689ba27SEdward Tomasz Napierala 	}
4702689ba27SEdward Tomasz Napierala 	if (acl_supported == 0)
4719b4261c9SChristian S.J. Peron 		return (0);
4722689ba27SEdward Tomasz Napierala 
4739b4261c9SChristian S.J. Peron 	/*
474724fd448SBryan Drewery 	 * If the file is a link we will not follow it.
4759b4261c9SChristian S.J. Peron 	 */
4769b4261c9SChristian S.J. Peron 	if (S_ISLNK(fs->st_mode)) {
4779b4261c9SChristian S.J. Peron 		aclgetf = acl_get_link_np;
4789b4261c9SChristian S.J. Peron 		aclsetf = acl_set_link_np;
4799b4261c9SChristian S.J. Peron 	} else {
4809b4261c9SChristian S.J. Peron 		aclgetf = acl_get_file;
4819b4261c9SChristian S.J. Peron 		aclsetf = acl_set_file;
4829b4261c9SChristian S.J. Peron 	}
4832689ba27SEdward Tomasz Napierala 	if (acl_type == ACL_TYPE_ACCESS) {
4849b4261c9SChristian S.J. Peron 		/*
4859b4261c9SChristian S.J. Peron 		 * Even if there is no ACL_TYPE_DEFAULT entry here, a zero
4869b4261c9SChristian S.J. Peron 		 * size ACL will be returned. So it is not safe to simply
4879b4261c9SChristian S.J. Peron 		 * check the pointer to see if the default ACL is present.
4889b4261c9SChristian S.J. Peron 		 */
4899b4261c9SChristian S.J. Peron 		acl = aclgetf(source_dir, ACL_TYPE_DEFAULT);
4909b4261c9SChristian S.J. Peron 		if (acl == NULL) {
4919b4261c9SChristian S.J. Peron 			warn("failed to get default acl entries on %s",
4929b4261c9SChristian S.J. Peron 			    source_dir);
4939b4261c9SChristian S.J. Peron 			return (1);
4949b4261c9SChristian S.J. Peron 		}
4959b4261c9SChristian S.J. Peron 		aclp = &acl->ats_acl;
4969b4261c9SChristian S.J. Peron 		if (aclp->acl_cnt != 0 && aclsetf(dest_dir,
4979b4261c9SChristian S.J. Peron 		    ACL_TYPE_DEFAULT, acl) < 0) {
4989b4261c9SChristian S.J. Peron 			warn("failed to set default acl entries on %s",
4999b4261c9SChristian S.J. Peron 			    dest_dir);
5002689ba27SEdward Tomasz Napierala 			acl_free(acl);
5019b4261c9SChristian S.J. Peron 			return (1);
5029b4261c9SChristian S.J. Peron 		}
5032689ba27SEdward Tomasz Napierala 		acl_free(acl);
5042689ba27SEdward Tomasz Napierala 	}
5052689ba27SEdward Tomasz Napierala 	acl = aclgetf(source_dir, acl_type);
5069b4261c9SChristian S.J. Peron 	if (acl == NULL) {
5079b4261c9SChristian S.J. Peron 		warn("failed to get acl entries on %s", source_dir);
5089b4261c9SChristian S.J. Peron 		return (1);
5099b4261c9SChristian S.J. Peron 	}
5102689ba27SEdward Tomasz Napierala 	if (acl_is_trivial_np(acl, &trivial)) {
5112689ba27SEdward Tomasz Napierala 		warn("acl_is_trivial() failed on %s", source_dir);
5122689ba27SEdward Tomasz Napierala 		acl_free(acl);
5139b4261c9SChristian S.J. Peron 		return (1);
5149b4261c9SChristian S.J. Peron 	}
5152689ba27SEdward Tomasz Napierala 	if (trivial) {
5162689ba27SEdward Tomasz Napierala 		acl_free(acl);
5172689ba27SEdward Tomasz Napierala 		return (0);
5182689ba27SEdward Tomasz Napierala 	}
5192689ba27SEdward Tomasz Napierala 	if (aclsetf(dest_dir, acl_type, acl) < 0) {
5202689ba27SEdward Tomasz Napierala 		warn("failed to set acl entries on %s", dest_dir);
5212689ba27SEdward Tomasz Napierala 		acl_free(acl);
5222689ba27SEdward Tomasz Napierala 		return (1);
5232689ba27SEdward Tomasz Napierala 	}
5242689ba27SEdward Tomasz Napierala 	acl_free(acl);
5259b4261c9SChristian S.J. Peron 	return (0);
5269b4261c9SChristian S.J. Peron }
5279b4261c9SChristian S.J. Peron 
5284b88c807SRodney W. Grimes void
usage(void)5295dce647cSWarner Losh usage(void)
5304b88c807SRodney W. Grimes {
531fcb2f1b3SMichael Haro 
5324b88c807SRodney W. Grimes 	(void)fprintf(stderr, "%s\n%s\n",
533724fd448SBryan Drewery 	    "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpsvx] "
534724fd448SBryan Drewery 	    "source_file target_file",
535724fd448SBryan Drewery 	    "       cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpsvx] "
536724fd448SBryan Drewery 	    "source_file ... "
537a89237aeSRuslan Ermilov 	    "target_directory");
538127432d7SMichael Haro 	exit(EX_USAGE);
5394b88c807SRodney W. Grimes }
540