]>
git.saurik.com Git - apple/file_cmds.git/blob - cp/utils.c
75d53d67d83bcfcce8d266d65b517a752e426d12
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 static char sccsid
[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/bin/cp/utils.c,v 1.46 2005/09/05 04:36:08 csjp Exp $");
38 #include <sys/types.h>
40 #include <sys/param.h>
42 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
60 #include <sys/mount.h>
61 #include <get_compat.h>
63 #define COMPAT_MODE(a,b) (1)
64 #endif /* __APPLE__ */
67 #define cp_pct(x,y) (int)(100.0 * (double)(x) / (double)(y))
70 copy_file(const FTSENT
*entp
, int dne
)
72 static char buf
[MAXBSIZE
];
74 int ch
, checkch
, from_fd
, rcount
, rval
, to_fd
;
79 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
85 if ((from_fd
= open(entp
->fts_path
, O_RDONLY
, 0)) == -1) {
86 warn("%s", entp
->fts_path
);
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.)
101 #define YESNO "(y/n [n]) "
104 printf("%s not overwritten\n", to
.p_path
);
105 (void)close(from_fd
);
108 (void)fprintf(stderr
, "overwrite %s? %s",
110 checkch
= ch
= getchar();
111 while (ch
!= '\n' && ch
!= EOF
)
113 if (checkch
!= 'y' && checkch
!= 'Y') {
114 (void)close(from_fd
);
115 (void)fprintf(stderr
, "not overwritten\n");
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);
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
));
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
));
139 /* overwrite existing destination file name */
140 to_fd
= open(to
.p_path
, O_WRONLY
| O_TRUNC
, 0);
143 to_fd
= open(to
.p_path
, O_WRONLY
| O_TRUNC
| O_CREAT
,
144 fs
->st_mode
& ~(S_ISUID
| S_ISGID
));
147 warn("%s", to
.p_path
);
148 (void)close(from_fd
);
155 if (S_ISREG(fs
->st_mode
)) {
159 * Pre-allocate blocks for the destination file if it
162 if (fstatfs(to_fd
, &sfs
) == 0 &&
163 strcmp(sfs
.f_fstypename
, "acfs") == 0) {
167 fst
.fst_posmode
= F_PEOFPOSMODE
;
169 fst
.fst_length
= fs
->st_size
;
171 (void) fcntl(to_fd
, F_PREALLOCATE
, &fst
);
174 #endif /* __APPLE__ */
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 warn("%s: fchmod failed", to
.p_path
);
184 warn("%s", to
.p_path
);
187 * Mmap and write if less than 8M (the limit is so we don't totally
188 * trash memory on big files. This is really a minor hack, but it
189 * wins some CPU back.
191 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
192 if (S_ISREG(fs
->st_mode
) && fs
->st_size
> 0 &&
193 fs
->st_size
<= 8 * 1048576) {
194 if ((p
= mmap(NULL
, (size_t)fs
->st_size
, PROT_READ
,
195 MAP_SHARED
, from_fd
, (off_t
)0)) == MAP_FAILED
) {
196 warn("%s", entp
->fts_path
);
200 for (bufp
= p
, wresid
= fs
->st_size
; ;
201 bufp
+= wcount
, wresid
-= (size_t)wcount
) {
202 wcount
= write(to_fd
, bufp
, wresid
);
206 (void)fprintf(stderr
,
208 entp
->fts_path
, to
.p_path
,
209 cp_pct(wtotal
, fs
->st_size
));
212 if (wcount
>= (ssize_t
)wresid
|| wcount
<= 0)
215 if (wcount
!= (ssize_t
)wresid
) {
216 warn("%s", to
.p_path
);
219 /* Some systems don't unmap on close(2). */
220 if (munmap(p
, fs
->st_size
) < 0) {
221 warn("%s", entp
->fts_path
);
229 while ((rcount
= read(from_fd
, buf
, MAXBSIZE
)) > 0) {
230 for (bufp
= buf
, wresid
= rcount
; ;
231 bufp
+= wcount
, wresid
-= wcount
) {
232 wcount
= write(to_fd
, bufp
, wresid
);
236 (void)fprintf(stderr
,
238 entp
->fts_path
, to
.p_path
,
239 cp_pct(wtotal
, fs
->st_size
));
242 if (wcount
>= (ssize_t
)wresid
|| wcount
<= 0)
245 if (wcount
!= (ssize_t
)wresid
) {
246 warn("%s", to
.p_path
);
252 warn("%s", entp
->fts_path
);
258 * Don't remove the target even after an error. The target might
259 * not be a regular file, or its attributes might be important,
260 * or its contents might be irreplaceable. It would only be safe
261 * to remove it if we created it and its length is 0.
264 if (fchmod(to_fd
, mode
))
265 warn("%s: fchmod failed", to
.p_path
);
267 /* do these before setfile in case copyfile changes mtime */
268 if (!Xflag
&& S_ISREG(fs
->st_mode
)) { /* skip devices, etc */
269 if (fcopyfile(from_fd
, to_fd
, NULL
, COPYFILE_XATTR
) < 0)
270 warn("%s: could not copy extended attributes to %s", entp
->fts_path
, to
.p_path
);
272 if (pflag
&& setfile(fs
, to_fd
))
275 /* If this ACL denies writeattr then setfile will fail... */
276 if (fcopyfile(from_fd
, to_fd
, NULL
, COPYFILE_ACL
) < 0)
277 warn("%s: could not copy ACL to %s", entp
->fts_path
, to
.p_path
);
279 #else /* !__APPLE__ */
280 if (pflag
&& setfile(fs
, to_fd
))
282 if (pflag
&& preserve_fd_acls(from_fd
, to_fd
) != 0)
284 #endif /* __APPLE__ */
285 (void)close(from_fd
);
287 warn("%s", to
.p_path
);
294 copy_link(const FTSENT
*p
, int exists
)
297 char llink
[PATH_MAX
];
299 if ((len
= readlink(p
->fts_path
, llink
, sizeof(llink
) - 1)) == -1) {
300 warn("readlink: %s", p
->fts_path
);
304 if (exists
&& unlink(to
.p_path
)) {
305 warn("unlink: %s", to
.p_path
);
308 if (symlink(llink
, to
.p_path
)) {
309 warn("symlink: %s", llink
);
314 if (copyfile(p
->fts_path
, to
.p_path
, NULL
, COPYFILE_XATTR
| COPYFILE_NOFOLLOW_SRC
) <0)
315 warn("%s: could not copy extended attributes to %s",
316 p
->fts_path
, to
.p_path
);
318 return (pflag
? setfile(p
->fts_statp
, -1) : 0);
322 copy_fifo(struct stat
*from_stat
, int exists
)
324 if (exists
&& unlink(to
.p_path
)) {
325 warn("unlink: %s", to
.p_path
);
328 if (mkfifo(to
.p_path
, from_stat
->st_mode
)) {
329 warn("mkfifo: %s", to
.p_path
);
332 return (pflag
? setfile(from_stat
, -1) : 0);
336 copy_special(struct stat
*from_stat
, int exists
)
338 if (exists
&& unlink(to
.p_path
)) {
339 warn("unlink: %s", to
.p_path
);
342 if (mknod(to
.p_path
, from_stat
->st_mode
, from_stat
->st_rdev
)) {
343 warn("mknod: %s", to
.p_path
);
346 return (pflag
? setfile(from_stat
, -1) : 0);
350 setfile(struct stat
*fs
, int fd
)
352 static struct timeval tv
[2];
354 int rval
, gotstat
, islink
, fdval
;
358 islink
= !fdval
&& S_ISLNK(fs
->st_mode
);
359 fs
->st_mode
&= S_ISUID
| S_ISGID
| S_ISVTX
|
360 S_IRWXU
| S_IRWXG
| S_IRWXO
;
362 TIMESPEC_TO_TIMEVAL(&tv
[0], &fs
->st_atimespec
);
363 TIMESPEC_TO_TIMEVAL(&tv
[1], &fs
->st_mtimespec
);
365 if (islink
? 0 : utimes(to
.p_path
, tv
)) {
367 if (islink
? lutimes(to
.p_path
, tv
) : utimes(to
.p_path
, tv
)) {
368 #endif /* __APPLE__ */
369 warn("%sutimes: %s", islink
? "l" : "", to
.p_path
);
372 if (fdval
? fstat(fd
, &ts
) :
373 (islink
? lstat(to
.p_path
, &ts
) : stat(to
.p_path
, &ts
)))
377 ts
.st_mode
&= S_ISUID
| S_ISGID
| S_ISVTX
|
378 S_IRWXU
| S_IRWXG
| S_IRWXO
;
381 * Changing the ownership probably won't succeed, unless we're root
382 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
383 * the mode; current BSD behavior is to remove all setuid bits on
384 * chown. If chown fails, lose setuid/setgid bits.
386 if (!gotstat
|| fs
->st_uid
!= ts
.st_uid
|| fs
->st_gid
!= ts
.st_gid
)
387 if (fdval
? fchown(fd
, fs
->st_uid
, fs
->st_gid
) :
388 (islink
? lchown(to
.p_path
, fs
->st_uid
, fs
->st_gid
) :
389 chown(to
.p_path
, fs
->st_uid
, fs
->st_gid
))) {
390 if (errno
!= EPERM
) {
391 warn("%schown: %s", islink
? "l" : "", to
.p_path
);
394 fs
->st_mode
&= ~(S_ISUID
| S_ISGID
);
397 if (!gotstat
|| fs
->st_mode
!= ts
.st_mode
)
398 if (fdval
? fchmod(fd
, fs
->st_mode
) :
399 (islink
? lchmod(to
.p_path
, fs
->st_mode
) :
400 chmod(to
.p_path
, fs
->st_mode
))) {
401 warn("%schmod: %s", islink
? "l" : "", to
.p_path
);
405 if (!gotstat
|| fs
->st_flags
!= ts
.st_flags
)
407 fchflags(fd
, fs
->st_flags
) :
408 (islink
? lchflags(to
.p_path
, fs
->st_flags
) :
409 chflags(to
.p_path
, fs
->st_flags
))) {
410 warn("%schflags: %s", islink
? "l" : "", to
.p_path
);
419 preserve_fd_acls(int source_fd
, int dest_fd
)
424 if (fpathconf(source_fd
, _PC_ACL_EXTENDED
) != 1 ||
425 fpathconf(dest_fd
, _PC_ACL_EXTENDED
) != 1)
427 acl
= acl_get_fd(source_fd
);
429 warn("failed to get acl entries while setting %s", to
.p_path
);
432 aclp
= &acl
->ats_acl
;
433 if (aclp
->acl_cnt
== 3)
435 if (acl_set_fd(dest_fd
, acl
) < 0) {
436 warn("failed to set acl entries for %s", to
.p_path
);
443 preserve_dir_acls(struct stat
*fs
, char *source_dir
, char *dest_dir
)
445 acl_t (*aclgetf
)(const char *, acl_type_t
);
446 int (*aclsetf
)(const char *, acl_type_t
, acl_t
);
450 if (pathconf(source_dir
, _PC_ACL_EXTENDED
) != 1 ||
451 pathconf(dest_dir
, _PC_ACL_EXTENDED
) != 1)
454 * If the file is a link we will not follow it
456 if (S_ISLNK(fs
->st_mode
)) {
457 aclgetf
= acl_get_link_np
;
458 aclsetf
= acl_set_link_np
;
460 aclgetf
= acl_get_file
;
461 aclsetf
= acl_set_file
;
464 * Even if there is no ACL_TYPE_DEFAULT entry here, a zero
465 * size ACL will be returned. So it is not safe to simply
466 * check the pointer to see if the default ACL is present.
468 acl
= aclgetf(source_dir
, ACL_TYPE_DEFAULT
);
470 warn("failed to get default acl entries on %s",
474 aclp
= &acl
->ats_acl
;
475 if (aclp
->acl_cnt
!= 0 && aclsetf(dest_dir
,
476 ACL_TYPE_DEFAULT
, acl
) < 0) {
477 warn("failed to set default acl entries on %s",
481 acl
= aclgetf(source_dir
, ACL_TYPE_ACCESS
);
483 warn("failed to get acl entries on %s", source_dir
);
486 aclp
= &acl
->ats_acl
;
487 if (aclsetf(dest_dir
, ACL_TYPE_ACCESS
, acl
) < 0) {
488 warn("failed to set acl entries on %s", dest_dir
);
493 #endif /* !__APPLE__ */
499 if (COMPAT_MODE("bin/cp", "unix2003")) {
500 (void)fprintf(stderr
, "%s\n%s\n",
501 "usage: cp [-R [-H | -L | -P]] [-fi | -n] [-pvX] source_file target_file",
502 " cp [-R [-H | -L | -P]] [-fi | -n] [-pvX] source_file ... "
505 (void)fprintf(stderr
, "%s\n%s\n",
506 "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-pvX] source_file target_file",
507 " cp [-R [-H | -L | -P]] [-f | -i | -n] [-pvX] source_file ... "