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