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