file_cmds-116.9.tar.gz
[apple/file_cmds.git] / cp / utils.c
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
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
37 #endif
38 #endif /* not lint */
39 #include <sys/cdefs.h>
40 __RCSID("$FreeBSD: src/bin/cp/utils.c,v 1.38 2002/07/31 16:52:16 markm Exp $");
41
42 #include <sys/param.h>
43 #include <sys/time.h>
44 #include <sys/stat.h>
45 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
46 #include <sys/mman.h>
47 #endif
48
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <fts.h>
53 #include <limits.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <sysexits.h>
57 #include <unistd.h>
58
59 #ifdef __APPLE__
60 #include <copyfile.h>
61 #endif
62
63 #include "extern.h"
64
65 int
66 copy_file(FTSENT *entp, int dne)
67 {
68 static char buf[MAXBSIZE];
69 struct stat *fs;
70 int ch, checkch, from_fd, rcount, rval, to_fd;
71 ssize_t wcount;
72 size_t wresid;
73 char *bufp;
74 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
75 char *p;
76 #endif
77
78 if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
79 warn("%s", entp->fts_path);
80 return (1);
81 }
82
83 fs = entp->fts_statp;
84
85 /*
86 * If the file exists and we're interactive, verify with the user.
87 * If the file DNE, set the mode to be the from file, minus setuid
88 * bits, modified by the umask; arguably wrong, but it makes copying
89 * executables work right and it's been that way forever. (The
90 * other choice is 666 or'ed with the execute bits on the from file
91 * modified by the umask.)
92 */
93 if (!dne) {
94 #define YESNO "(y/n [n]) "
95 if (nflag) {
96 if (vflag)
97 printf("%s not overwritten\n", to.p_path);
98 close(from_fd);
99 return (0);
100 } else if (iflag) {
101 (void)fprintf(stderr, "overwrite %s? %s",
102 to.p_path, YESNO);
103 checkch = ch = getchar();
104 while (ch != '\n' && ch != EOF)
105 ch = getchar();
106 if (checkch != 'y' && checkch != 'Y') {
107 (void)close(from_fd);
108 (void)fprintf(stderr, "not overwritten\n");
109 return (1);
110 }
111 }
112
113 if (fflag) {
114 /* remove existing destination file name,
115 * create a new file */
116 (void)unlink(to.p_path);
117 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
118 fs->st_mode & ~(S_ISUID | S_ISGID));
119 } else
120 /* overwrite existing destination file name */
121 to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
122 } else
123 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
124 fs->st_mode & ~(S_ISUID | S_ISGID));
125
126 if (to_fd == -1) {
127 warn("%s", to.p_path);
128 (void)close(from_fd);
129 return (1);
130 }
131
132 rval = 0;
133
134 /*
135 * Mmap and write if less than 8M (the limit is so we don't totally
136 * trash memory on big files. This is really a minor hack, but it
137 * wins some CPU back.
138 */
139 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
140 if (S_ISREG(fs->st_mode) && fs->st_size <= 8 * 1048576) {
141 if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
142 MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) {
143 warn("%s", entp->fts_path);
144 rval = 1;
145 } else {
146 for (bufp = p, wresid = fs->st_size; ;
147 bufp += wcount, wresid -= (size_t)wcount) {
148 wcount = write(to_fd, bufp, wresid);
149 if (wcount >= (ssize_t)wresid || wcount <= 0)
150 break;
151 }
152 if (wcount != (ssize_t)wresid) {
153 warn("%s", to.p_path);
154 rval = 1;
155 }
156 /* Some systems don't unmap on close(2). */
157 if (munmap(p, fs->st_size) < 0) {
158 warn("%s", entp->fts_path);
159 rval = 1;
160 }
161 }
162 } else
163 #endif
164 {
165 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
166 for (bufp = buf, wresid = rcount; ;
167 bufp += wcount, wresid -= wcount) {
168 wcount = write(to_fd, bufp, wresid);
169 if (wcount >= (ssize_t)wresid || wcount <= 0)
170 break;
171 }
172 if (wcount != (ssize_t)wresid) {
173 warn("%s", to.p_path);
174 rval = 1;
175 break;
176 }
177 }
178 if (rcount < 0) {
179 warn("%s", entp->fts_path);
180 rval = 1;
181 }
182 }
183
184 #ifdef __APPLE__
185 if (pflag)
186 copyfile(entp->fts_path, to.p_path, 0, COPYFILE_XATTR | COPYFILE_ACL);
187 else
188 copyfile(entp->fts_path, to.p_path, 0, COPYFILE_XATTR);
189 #endif
190 /*
191 * Don't remove the target even after an error. The target might
192 * not be a regular file, or its attributes might be important,
193 * or its contents might be irreplaceable. It would only be safe
194 * to remove it if we created it and its length is 0.
195 */
196
197 if (pflag && setfile(fs, to_fd))
198 rval = 1;
199 (void)close(from_fd);
200 if (close(to_fd)) {
201 warn("%s", to.p_path);
202 rval = 1;
203 }
204 return (rval);
205 }
206
207 int
208 copy_link(FTSENT *p, int exists)
209 {
210 int len;
211 char llink[PATH_MAX];
212
213 if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) {
214 warn("readlink: %s", p->fts_path);
215 return (1);
216 }
217 llink[len] = '\0';
218 if (exists && unlink(to.p_path)) {
219 warn("unlink: %s", to.p_path);
220 return (1);
221 }
222 if (symlink(llink, to.p_path)) {
223 warn("symlink: %s", llink);
224 return (1);
225 }
226 #ifdef __APPLE__
227 copyfile(p->fts_path, to.p_path, 0, COPYFILE_XATTR | COPYFILE_NOFOLLOW_SRC);
228 #endif
229 return (0);
230 }
231
232 int
233 copy_fifo(struct stat *from_stat, int exists)
234 {
235 if (exists && unlink(to.p_path)) {
236 warn("unlink: %s", to.p_path);
237 return (1);
238 }
239 if (mkfifo(to.p_path, from_stat->st_mode)) {
240 warn("mkfifo: %s", to.p_path);
241 return (1);
242 }
243 return (pflag ? setfile(from_stat, 0) : 0);
244 }
245
246 int
247 copy_special(struct stat *from_stat, int exists)
248 {
249 if (exists && unlink(to.p_path)) {
250 warn("unlink: %s", to.p_path);
251 return (1);
252 }
253 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
254 warn("mknod: %s", to.p_path);
255 return (1);
256 }
257 return (pflag ? setfile(from_stat, 0) : 0);
258 }
259
260 int
261 setfile(struct stat *fs, int fd)
262 {
263 static struct timeval tv[2];
264 struct stat ts;
265 int rval;
266 int gotstat;
267
268 rval = 0;
269 fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX |
270 S_IRWXU | S_IRWXG | S_IRWXO;
271
272 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
273 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
274 if (utimes(to.p_path, tv)) {
275 warn("utimes: %s", to.p_path);
276 rval = 1;
277 }
278 if (fd ? fstat(fd, &ts) : stat(to.p_path, &ts))
279 gotstat = 0;
280 else {
281 gotstat = 1;
282 ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
283 S_IRWXU | S_IRWXG | S_IRWXO;
284 }
285 /*
286 * Changing the ownership probably won't succeed, unless we're root
287 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
288 * the mode; current BSD behavior is to remove all setuid bits on
289 * chown. If chown fails, lose setuid/setgid bits.
290 */
291 if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
292 if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
293 chown(to.p_path, fs->st_uid, fs->st_gid)) {
294 if (errno != EPERM) {
295 warn("chown: %s", to.p_path);
296 rval = 1;
297 }
298 fs->st_mode &= ~(S_ISUID | S_ISGID);
299 }
300
301 if (!gotstat || fs->st_mode != ts.st_mode)
302 if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
303 warn("chmod: %s", to.p_path);
304 rval = 1;
305 }
306
307 if (!gotstat || fs->st_flags != ts.st_flags)
308 if (fd ?
309 fchflags(fd, fs->st_flags) : chflags(to.p_path, fs->st_flags)) {
310 warn("chflags: %s", to.p_path);
311 rval = 1;
312 }
313
314 return (rval);
315 }
316
317 void
318 usage(void)
319 {
320
321 (void)fprintf(stderr, "%s\n%s\n",
322 "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-pv] src target",
323 " cp [-R [-H | -L | -P]] [-f | -i | -n] [-pv] src1 ... srcN directory");
324 exit(EX_USAGE);
325 }