file_cmds-184.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 * 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
32 static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
33 #endif
34 #endif /* not lint */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/bin/cp/utils.c,v 1.46 2005/09/05 04:36:08 csjp Exp $");
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 #ifdef __APPLE__
57 #include <sys/time.h>
58 #include <copyfile.h>
59 #include <string.h>
60 #include <sys/mount.h>
61 #include <get_compat.h>
62 #else
63 #define COMPAT_MODE(a,b) (1)
64 #endif /* __APPLE__ */
65
66 #include "extern.h"
67 #define cp_pct(x,y) (int)(100.0 * (double)(x) / (double)(y))
68
69 int
70 copy_file(const FTSENT *entp, int dne)
71 {
72 static char buf[MAXBSIZE];
73 struct stat *fs;
74 int ch, checkch, from_fd, rcount, rval, to_fd;
75 ssize_t wcount;
76 size_t wresid;
77 off_t wtotal;
78 char *bufp;
79 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
80 char *p;
81 #endif
82
83 if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
84 warn("%s", entp->fts_path);
85 return (1);
86 }
87
88 fs = entp->fts_statp;
89
90 /*
91 * If the file exists and we're interactive, verify with the user.
92 * If the file DNE, set the mode to be the from file, minus setuid
93 * bits, modified by the umask; arguably wrong, but it makes copying
94 * executables work right and it's been that way forever. (The
95 * other choice is 666 or'ed with the execute bits on the from file
96 * modified by the umask.)
97 */
98 if (!dne) {
99 #define YESNO "(y/n [n]) "
100 if (nflag) {
101 if (vflag)
102 printf("%s not overwritten\n", to.p_path);
103 (void)close(from_fd);
104 return (1);
105 } else if (iflag) {
106 (void)fprintf(stderr, "overwrite %s? %s",
107 to.p_path, YESNO);
108 checkch = ch = getchar();
109 while (ch != '\n' && ch != EOF)
110 ch = getchar();
111 if (checkch != 'y' && checkch != 'Y') {
112 (void)close(from_fd);
113 (void)fprintf(stderr, "not overwritten\n");
114 return (1);
115 }
116 }
117
118 if (COMPAT_MODE("bin/cp", "unix2003")) {
119 /* first try to overwrite existing destination file name */
120 to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
121 if (to_fd == -1) {
122 if (fflag) {
123 /* Only if it fails remove file and create a new one */
124 (void)unlink(to.p_path);
125 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
126 fs->st_mode & ~(S_ISUID | S_ISGID));
127 }
128 }
129 } else {
130 if (fflag) {
131 /* remove existing destination file name,
132 * create a new file */
133 (void)unlink(to.p_path);
134 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
135 fs->st_mode & ~(S_ISUID | S_ISGID));
136 } else
137 /* overwrite existing destination file name */
138 to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
139 }
140 } else
141 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
142 fs->st_mode & ~(S_ISUID | S_ISGID));
143
144 if (to_fd == -1) {
145 warn("%s", to.p_path);
146 (void)close(from_fd);
147 return (1);
148 }
149
150 rval = 0;
151
152 #ifdef __APPLE__
153 if (S_ISREG(fs->st_mode)) {
154 struct statfs sfs;
155
156 /*
157 * Pre-allocate blocks for the destination file if it
158 * resides on Xsan.
159 */
160 if (fstatfs(to_fd, &sfs) == 0 &&
161 strcmp(sfs.f_fstypename, "acfs") == 0) {
162 fstore_t fst;
163
164 fst.fst_flags = 0;
165 fst.fst_posmode = F_PEOFPOSMODE;
166 fst.fst_offset = 0;
167 fst.fst_length = fs->st_size;
168
169 (void) fcntl(to_fd, F_PREALLOCATE, &fst);
170 }
171 }
172 #endif /* __APPLE__ */
173
174 /*
175 * Mmap and write if less than 8M (the limit is so we don't totally
176 * trash memory on big files. This is really a minor hack, but it
177 * wins some CPU back.
178 */
179 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
180 if (S_ISREG(fs->st_mode) && fs->st_size > 0 &&
181 fs->st_size <= 8 * 1048576) {
182 if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
183 MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) {
184 warn("%s", entp->fts_path);
185 rval = 1;
186 } else {
187 wtotal = 0;
188 for (bufp = p, wresid = fs->st_size; ;
189 bufp += wcount, wresid -= (size_t)wcount) {
190 wcount = write(to_fd, bufp, wresid);
191 wtotal += wcount;
192 if (info) {
193 info = 0;
194 (void)fprintf(stderr,
195 "%s -> %s %3d%%\n",
196 entp->fts_path, to.p_path,
197 cp_pct(wtotal, fs->st_size));
198
199 }
200 if (wcount >= (ssize_t)wresid || wcount <= 0)
201 break;
202 }
203 if (wcount != (ssize_t)wresid) {
204 warn("%s", to.p_path);
205 rval = 1;
206 }
207 /* Some systems don't unmap on close(2). */
208 if (munmap(p, fs->st_size) < 0) {
209 warn("%s", entp->fts_path);
210 rval = 1;
211 }
212 }
213 } else
214 #endif
215 {
216 wtotal = 0;
217 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
218 for (bufp = buf, wresid = rcount; ;
219 bufp += wcount, wresid -= wcount) {
220 wcount = write(to_fd, bufp, wresid);
221 wtotal += wcount;
222 if (info) {
223 info = 0;
224 (void)fprintf(stderr,
225 "%s -> %s %3d%%\n",
226 entp->fts_path, to.p_path,
227 cp_pct(wtotal, fs->st_size));
228
229 }
230 if (wcount >= (ssize_t)wresid || wcount <= 0)
231 break;
232 }
233 if (wcount != (ssize_t)wresid) {
234 warn("%s", to.p_path);
235 rval = 1;
236 break;
237 }
238 }
239 if (rcount < 0) {
240 warn("%s", entp->fts_path);
241 rval = 1;
242 }
243 }
244
245 /*
246 * Don't remove the target even after an error. The target might
247 * not be a regular file, or its attributes might be important,
248 * or its contents might be irreplaceable. It would only be safe
249 * to remove it if we created it and its length is 0.
250 */
251
252 #ifdef __APPLE__
253 /* do these before setfile in case copyfile changes mtime */
254 if (!Xflag && S_ISREG(fs->st_mode)) { /* skip devices, etc */
255 if (fcopyfile(from_fd, to_fd, NULL, COPYFILE_XATTR) < 0)
256 warn("%s: could not copy extended attributes to %s", entp->fts_path, to.p_path);
257 }
258 if (pflag && setfile(fs, to_fd))
259 rval = 1;
260 if (pflag) {
261 /* If this ACL denies writeattr then setfile will fail... */
262 if (fcopyfile(from_fd, to_fd, NULL, COPYFILE_ACL) < 0)
263 warn("%s: could not copy ACL to %s", entp->fts_path, to.p_path);
264 }
265 #else /* !__APPLE__ */
266 if (pflag && setfile(fs, to_fd))
267 rval = 1;
268 if (pflag && preserve_fd_acls(from_fd, to_fd) != 0)
269 rval = 1;
270 #endif /* __APPLE__ */
271 (void)close(from_fd);
272 if (close(to_fd)) {
273 warn("%s", to.p_path);
274 rval = 1;
275 }
276 return (rval);
277 }
278
279 int
280 copy_link(const FTSENT *p, int exists)
281 {
282 int len;
283 char llink[PATH_MAX];
284
285 if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) {
286 warn("readlink: %s", p->fts_path);
287 return (1);
288 }
289 llink[len] = '\0';
290 if (exists && unlink(to.p_path)) {
291 warn("unlink: %s", to.p_path);
292 return (1);
293 }
294 if (symlink(llink, to.p_path)) {
295 warn("symlink: %s", llink);
296 return (1);
297 }
298 #ifdef __APPLE__
299 if (!Xflag)
300 if (copyfile(p->fts_path, to.p_path, NULL, COPYFILE_XATTR | COPYFILE_NOFOLLOW_SRC) <0)
301 warn("%s: could not copy extended attributes to %s",
302 p->fts_path, to.p_path);
303 #endif
304 return (pflag ? setfile(p->fts_statp, -1) : 0);
305 }
306
307 int
308 copy_fifo(struct stat *from_stat, int exists)
309 {
310 if (exists && unlink(to.p_path)) {
311 warn("unlink: %s", to.p_path);
312 return (1);
313 }
314 if (mkfifo(to.p_path, from_stat->st_mode)) {
315 warn("mkfifo: %s", to.p_path);
316 return (1);
317 }
318 return (pflag ? setfile(from_stat, -1) : 0);
319 }
320
321 int
322 copy_special(struct stat *from_stat, int exists)
323 {
324 if (exists && unlink(to.p_path)) {
325 warn("unlink: %s", to.p_path);
326 return (1);
327 }
328 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
329 warn("mknod: %s", to.p_path);
330 return (1);
331 }
332 return (pflag ? setfile(from_stat, -1) : 0);
333 }
334
335 int
336 setfile(struct stat *fs, int fd)
337 {
338 static struct timeval tv[2];
339 struct stat ts;
340 int rval, gotstat, islink, fdval;
341
342 rval = 0;
343 fdval = fd != -1;
344 islink = !fdval && S_ISLNK(fs->st_mode);
345 fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX |
346 S_IRWXU | S_IRWXG | S_IRWXO;
347
348 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
349 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
350 #ifdef __APPLE__
351 if (islink ? 0 : utimes(to.p_path, tv)) {
352 #else
353 if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) {
354 #endif /* __APPLE__ */
355 warn("%sutimes: %s", islink ? "l" : "", to.p_path);
356 rval = 1;
357 }
358 if (fdval ? fstat(fd, &ts) :
359 (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts)))
360 gotstat = 0;
361 else {
362 gotstat = 1;
363 ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
364 S_IRWXU | S_IRWXG | S_IRWXO;
365 }
366 /*
367 * Changing the ownership probably won't succeed, unless we're root
368 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
369 * the mode; current BSD behavior is to remove all setuid bits on
370 * chown. If chown fails, lose setuid/setgid bits.
371 */
372 if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
373 if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
374 (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
375 chown(to.p_path, fs->st_uid, fs->st_gid))) {
376 if (errno != EPERM) {
377 warn("%schown: %s", islink ? "l" : "", to.p_path);
378 rval = 1;
379 }
380 fs->st_mode &= ~(S_ISUID | S_ISGID);
381 }
382
383 if (!gotstat || fs->st_mode != ts.st_mode)
384 if (fdval ? fchmod(fd, fs->st_mode) :
385 (islink ? lchmod(to.p_path, fs->st_mode) :
386 chmod(to.p_path, fs->st_mode))) {
387 warn("%schmod: %s", islink ? "l" : "", to.p_path);
388 rval = 1;
389 }
390
391 if (!gotstat || fs->st_flags != ts.st_flags)
392 if (fdval ?
393 fchflags(fd, fs->st_flags) :
394 (islink ? lchflags(to.p_path, fs->st_flags) :
395 chflags(to.p_path, fs->st_flags))) {
396 warn("%schflags: %s", islink ? "l" : "", to.p_path);
397 rval = 1;
398 }
399
400 return (rval);
401 }
402
403 #ifndef __APPLE__
404 int
405 preserve_fd_acls(int source_fd, int dest_fd)
406 {
407 struct acl *aclp;
408 acl_t acl;
409
410 if (fpathconf(source_fd, _PC_ACL_EXTENDED) != 1 ||
411 fpathconf(dest_fd, _PC_ACL_EXTENDED) != 1)
412 return (0);
413 acl = acl_get_fd(source_fd);
414 if (acl == NULL) {
415 warn("failed to get acl entries while setting %s", to.p_path);
416 return (1);
417 }
418 aclp = &acl->ats_acl;
419 if (aclp->acl_cnt == 3)
420 return (0);
421 if (acl_set_fd(dest_fd, acl) < 0) {
422 warn("failed to set acl entries for %s", to.p_path);
423 return (1);
424 }
425 return (0);
426 }
427
428 int
429 preserve_dir_acls(struct stat *fs, char *source_dir, char *dest_dir)
430 {
431 acl_t (*aclgetf)(const char *, acl_type_t);
432 int (*aclsetf)(const char *, acl_type_t, acl_t);
433 struct acl *aclp;
434 acl_t acl;
435
436 if (pathconf(source_dir, _PC_ACL_EXTENDED) != 1 ||
437 pathconf(dest_dir, _PC_ACL_EXTENDED) != 1)
438 return (0);
439 /*
440 * If the file is a link we will not follow it
441 */
442 if (S_ISLNK(fs->st_mode)) {
443 aclgetf = acl_get_link_np;
444 aclsetf = acl_set_link_np;
445 } else {
446 aclgetf = acl_get_file;
447 aclsetf = acl_set_file;
448 }
449 /*
450 * Even if there is no ACL_TYPE_DEFAULT entry here, a zero
451 * size ACL will be returned. So it is not safe to simply
452 * check the pointer to see if the default ACL is present.
453 */
454 acl = aclgetf(source_dir, ACL_TYPE_DEFAULT);
455 if (acl == NULL) {
456 warn("failed to get default acl entries on %s",
457 source_dir);
458 return (1);
459 }
460 aclp = &acl->ats_acl;
461 if (aclp->acl_cnt != 0 && aclsetf(dest_dir,
462 ACL_TYPE_DEFAULT, acl) < 0) {
463 warn("failed to set default acl entries on %s",
464 dest_dir);
465 return (1);
466 }
467 acl = aclgetf(source_dir, ACL_TYPE_ACCESS);
468 if (acl == NULL) {
469 warn("failed to get acl entries on %s", source_dir);
470 return (1);
471 }
472 aclp = &acl->ats_acl;
473 if (aclsetf(dest_dir, ACL_TYPE_ACCESS, acl) < 0) {
474 warn("failed to set acl entries on %s", dest_dir);
475 return (1);
476 }
477 return (0);
478 }
479 #endif /* !__APPLE__ */
480
481 void
482 usage(void)
483 {
484
485 if (COMPAT_MODE("bin/cp", "unix2003")) {
486 (void)fprintf(stderr, "%s\n%s\n",
487 "usage: cp [-R [-H | -L | -P]] [-fi | -n] [-pvX] source_file target_file",
488 " cp [-R [-H | -L | -P]] [-fi | -n] [-pvX] source_file ... "
489 "target_directory");
490 } else {
491 (void)fprintf(stderr, "%s\n%s\n",
492 "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-pvX] source_file target_file",
493 " cp [-R [-H | -L | -P]] [-f | -i | -n] [-pvX] source_file ... "
494 "target_directory");
495 }
496 exit(EX_USAGE);
497 }