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