xref: /dragonfly/bin/cp/utils.c (revision 6e285212)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)utils.c	8.3 (Berkeley) 4/1/94
34  * $FreeBSD: src/bin/cp/utils.c,v 1.27.2.6 2002/08/10 13:20:19 johan Exp $
35  * $DragonFly: src/bin/cp/utils.c,v 1.2 2003/06/17 04:22:49 dillon Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
42 #include <sys/mman.h>
43 #endif
44 
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <fts.h>
49 #include <limits.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <sysexits.h>
53 #include <unistd.h>
54 
55 #include "extern.h"
56 
57 int
58 copy_file(entp, dne)
59 	FTSENT *entp;
60 	int dne;
61 {
62 	static char buf[MAXBSIZE];
63 	struct stat *fs;
64 	int ch, checkch, from_fd, rcount, rval, to_fd, wcount, wresid;
65 	char *bufp;
66 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
67 	char *p;
68 #endif
69 
70 	if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
71 		warn("%s", entp->fts_path);
72 		return (1);
73 	}
74 
75 	fs = entp->fts_statp;
76 
77 	/*
78 	 * If the file exists and we're interactive, verify with the user.
79 	 * If the file DNE, set the mode to be the from file, minus setuid
80 	 * bits, modified by the umask; arguably wrong, but it makes copying
81 	 * executables work right and it's been that way forever.  (The
82 	 * other choice is 666 or'ed with the execute bits on the from file
83 	 * modified by the umask.)
84 	 */
85 	if (!dne) {
86 #define YESNO "(y/n [n]) "
87 		if (nflag) {
88 			if (vflag)
89 				printf("%s not overwritten\n", to.p_path);
90 			return (0);
91 		} else if (iflag) {
92 			(void)fprintf(stderr, "overwrite %s? %s",
93 					to.p_path, YESNO);
94 			checkch = ch = getchar();
95 			while (ch != '\n' && ch != EOF)
96 				ch = getchar();
97 			if (checkch != 'y' && checkch != 'Y') {
98 				(void)close(from_fd);
99 				(void)fprintf(stderr, "not overwritten\n");
100 				return (1);
101 			}
102 		}
103 
104 		if (fflag) {
105 		    /* remove existing destination file name,
106 		     * create a new file  */
107 		    (void)unlink(to.p_path);
108 		    to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
109 				 fs->st_mode & ~(S_ISUID | S_ISGID));
110 		} else
111 		    /* overwrite existing destination file name */
112 		    to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
113 	} else
114 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
115 		    fs->st_mode & ~(S_ISUID | S_ISGID));
116 
117 	if (to_fd == -1) {
118 		warn("%s", to.p_path);
119 		(void)close(from_fd);
120 		return (1);;
121 	}
122 
123 	rval = 0;
124 
125 	/*
126 	 * Mmap and write if less than 8M (the limit is so we don't totally
127 	 * trash memory on big files.  This is really a minor hack, but it
128 	 * wins some CPU back.
129 	 */
130 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
131 	if (S_ISREG(fs->st_mode) && fs->st_size <= 8 * 1048576) {
132 		if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
133 		    MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) {
134 			warn("%s", entp->fts_path);
135 			rval = 1;
136 		} else {
137 			for (bufp = p, wresid = fs->st_size; ;
138 			    bufp += wcount, wresid -= wcount) {
139 				wcount = write(to_fd, bufp, wresid);
140 				if (wcount >= wresid || wcount <= 0)
141 					break;
142 			}
143 			if (wcount != wresid) {
144 				warn("%s", to.p_path);
145 				rval = 1;
146 			}
147 			/* Some systems don't unmap on close(2). */
148 			if (munmap(p, fs->st_size) < 0) {
149 				warn("%s", entp->fts_path);
150 				rval = 1;
151 			}
152 		}
153 	} else
154 #endif
155 	{
156 		while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
157 			for (bufp = buf, wresid = rcount; ;
158 			    bufp += wcount, wresid -= wcount) {
159 				wcount = write(to_fd, bufp, wresid);
160 				if (wcount >= wresid || wcount <= 0)
161 					break;
162 			}
163 			if (wcount != wresid) {
164 				warn("%s", to.p_path);
165 				rval = 1;
166 				break;
167 			}
168 		}
169 		if (rcount < 0) {
170 			warn("%s", entp->fts_path);
171 			rval = 1;
172 		}
173 	}
174 
175 	/*
176 	 * Don't remove the target even after an error.  The target might
177 	 * not be a regular file, or its attributes might be important,
178 	 * or its contents might be irreplaceable.  It would only be safe
179 	 * to remove it if we created it and its length is 0.
180 	 */
181 
182 	if (pflag && setfile(fs, to_fd))
183 		rval = 1;
184 	(void)close(from_fd);
185 	if (close(to_fd)) {
186 		warn("%s", to.p_path);
187 		rval = 1;
188 	}
189 	return (rval);
190 }
191 
192 int
193 copy_link(p, exists)
194 	FTSENT *p;
195 	int exists;
196 {
197 	int len;
198 	char link[PATH_MAX];
199 
200 	if ((len = readlink(p->fts_path, link, sizeof(link) - 1)) == -1) {
201 		warn("readlink: %s", p->fts_path);
202 		return (1);
203 	}
204 	link[len] = '\0';
205 	if (exists && unlink(to.p_path)) {
206 		warn("unlink: %s", to.p_path);
207 		return (1);
208 	}
209 	if (symlink(link, to.p_path)) {
210 		warn("symlink: %s", link);
211 		return (1);
212 	}
213 	return (0);
214 }
215 
216 int
217 copy_fifo(from_stat, exists)
218 	struct stat *from_stat;
219 	int exists;
220 {
221 	if (exists && unlink(to.p_path)) {
222 		warn("unlink: %s", to.p_path);
223 		return (1);
224 	}
225 	if (mkfifo(to.p_path, from_stat->st_mode)) {
226 		warn("mkfifo: %s", to.p_path);
227 		return (1);
228 	}
229 	return (pflag ? setfile(from_stat, 0) : 0);
230 }
231 
232 int
233 copy_special(from_stat, exists)
234 	struct stat *from_stat;
235 	int exists;
236 {
237 	if (exists && unlink(to.p_path)) {
238 		warn("unlink: %s", to.p_path);
239 		return (1);
240 	}
241 	if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
242 		warn("mknod: %s", to.p_path);
243 		return (1);
244 	}
245 	return (pflag ? setfile(from_stat, 0) : 0);
246 }
247 
248 #define	RETAINBITS \
249 	(S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
250 
251 int
252 setfile(fs, fd)
253 	register struct stat *fs;
254 	int fd;
255 {
256 	static struct timeval tv[2];
257 	struct stat ts;
258 	int rval;
259 	int gotstat;
260 
261 	rval = 0;
262 	fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX |
263 		       S_IRWXU | S_IRWXG | S_IRWXO;
264 
265 	TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
266 	TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
267 	if (utimes(to.p_path, tv)) {
268 		warn("utimes: %s", to.p_path);
269 		rval = 1;
270 	}
271 	if (fd ? fstat(fd, &ts) : stat(to.p_path, &ts))
272 		gotstat = 0;
273 	else {
274 		gotstat = 1;
275 		ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
276 			      S_IRWXU | S_IRWXG | S_IRWXO;
277 	}
278 	/*
279 	 * Changing the ownership probably won't succeed, unless we're root
280 	 * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
281 	 * the mode; current BSD behavior is to remove all setuid bits on
282 	 * chown.  If chown fails, lose setuid/setgid bits.
283 	 */
284 	if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
285 		if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
286 		    chown(to.p_path, fs->st_uid, fs->st_gid)) {
287 			if (errno != EPERM) {
288 				warn("chown: %s", to.p_path);
289 				rval = 1;
290 			}
291 			fs->st_mode &= ~(S_ISUID | S_ISGID);
292 		}
293 
294 	if (!gotstat || fs->st_mode != ts.st_mode)
295 		if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
296 			warn("chmod: %s", to.p_path);
297 			rval = 1;
298 		}
299 
300 	if (!gotstat || fs->st_flags != ts.st_flags)
301 		if (fd ?
302 		    fchflags(fd, fs->st_flags) : chflags(to.p_path, fs->st_flags)) {
303 			warn("chflags: %s", to.p_path);
304 			rval = 1;
305 		}
306 
307 	return (rval);
308 }
309 
310 void
311 usage()
312 {
313 
314 	(void)fprintf(stderr, "%s\n%s\n",
315 "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-pv] src target",
316 "       cp [-R [-H | -L | -P]] [-f | -i | -n] [-pv] src1 ... srcN directory");
317 	exit(EX_USAGE);
318 }
319