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