2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/param.h>
48 #define cp_pct(x, y) ((y == 0) ? 0 : (int)(100.0 * (x) / (y)))
51 * Memory strategy threshold, in pages: if physmem is larger then this, use a
54 #define PHYSPAGES_THRESHOLD (32*1024)
56 /* Maximum buffer size in bytes - do not allow it to grow larger than this. */
57 #define BUFSIZE_MAX (2*1024*1024)
60 * Small (default) buffer size in bytes. It's inefficient for this to be
61 * smaller than MAXPHYS.
63 #define BUFSIZE_SMALL (MAXPHYS)
66 * Prompt used in -i case.
68 #define YESNO "(y/n [n]) "
71 copy_fallback(int from_fd
, int to_fd
)
73 static char *buf
= NULL
;
74 static size_t bufsize
;
75 ssize_t rcount
, wresid
, wcount
= 0;
79 if (sysconf(_SC_PHYS_PAGES
) > PHYSPAGES_THRESHOLD
)
80 bufsize
= MIN(BUFSIZE_MAX
, MAXPHYS
* 8);
82 bufsize
= BUFSIZE_SMALL
;
83 buf
= malloc(bufsize
);
85 err(1, "Not enough memory");
87 rcount
= read(from_fd
, buf
, bufsize
);
90 for (bufp
= buf
, wresid
= rcount
; ; bufp
+= wcount
, wresid
-= wcount
) {
91 wcount
= write(to_fd
, bufp
, wresid
);
97 return (wcount
< 0 ? wcount
: rcount
);
101 copy_file(const FTSENT
*entp
, int dne
)
106 int ch
, checkch
, from_fd
, rval
, to_fd
;
107 int use_copy_file_range
= 1;
109 fs
= entp
->fts_statp
;
110 from_fd
= to_fd
= -1;
111 if (!lflag
&& !sflag
) {
112 if ((from_fd
= open(entp
->fts_path
, O_RDONLY
, 0)) < 0 ||
113 fstat(from_fd
, &sb
) != 0) {
114 warn("%s", entp
->fts_path
);
116 (void)close(from_fd
);
120 * Check that the file hasn't been replaced with one of a
121 * different type. This can happen if we've been asked to
122 * copy something which is actively being modified and
123 * lost the race, or if we've been asked to copy something
124 * like /proc/X/fd/Y which stat(2) reports as S_IFREG but
125 * is actually something else once you open it.
127 if ((sb
.st_mode
& S_IFMT
) != (fs
->st_mode
& S_IFMT
)) {
128 warnx("%s: File changed", entp
->fts_path
);
129 (void)close(from_fd
);
135 * If the file exists and we're interactive, verify with the user.
136 * If the file DNE, set the mode to be the from file, minus setuid
137 * bits, modified by the umask; arguably wrong, but it makes copying
138 * executables work right and it's been that way forever. (The
139 * other choice is 666 or'ed with the execute bits on the from file
140 * modified by the umask.)
145 printf("%s not overwritten\n", to
.p_path
);
149 (void)fprintf(stderr
, "overwrite %s? %s",
151 checkch
= ch
= getchar();
152 while (ch
!= '\n' && ch
!= EOF
)
154 if (checkch
!= 'y' && checkch
!= 'Y') {
155 (void)fprintf(stderr
, "not overwritten\n");
162 /* remove existing destination file */
163 (void)unlink(to
.p_path
);
171 if (link(entp
->fts_path
, to
.p_path
) != 0) {
172 warn("%s", to
.p_path
);
179 if (symlink(entp
->fts_path
, to
.p_path
) != 0) {
180 warn("%s", to
.p_path
);
187 /* overwrite existing destination file */
188 to_fd
= open(to
.p_path
, O_WRONLY
| O_TRUNC
, 0);
190 /* create new destination file */
191 to_fd
= open(to
.p_path
, O_WRONLY
| O_TRUNC
| O_CREAT
,
192 fs
->st_mode
& ~(S_ISUID
| S_ISGID
));
195 warn("%s", to
.p_path
);
202 if (use_copy_file_range
) {
203 wcount
= copy_file_range(from_fd
, NULL
,
204 to_fd
, NULL
, SSIZE_MAX
, 0);
205 if (wcount
< 0 && errno
== EINVAL
) {
206 /* probably a non-seekable descriptor */
207 use_copy_file_range
= 0;
210 if (!use_copy_file_range
) {
211 wcount
= copy_fallback(from_fd
, to_fd
);
216 (void)fprintf(stderr
,
218 entp
->fts_path
, to
.p_path
,
219 cp_pct(wtotal
, fs
->st_size
));
221 } while (wcount
> 0);
223 warn("%s", entp
->fts_path
);
228 * Don't remove the target even after an error. The target might
229 * not be a regular file, or its attributes might be important,
230 * or its contents might be irreplaceable. It would only be safe
231 * to remove it if we created it and its length is 0.
233 if (pflag
&& setfile(fs
, to_fd
))
235 if (pflag
&& preserve_fd_acls(from_fd
, to_fd
) != 0)
238 warn("%s", to
.p_path
);
244 (void)close(from_fd
);
249 copy_link(const FTSENT
*p
, int exists
)
252 char llink
[PATH_MAX
];
254 if (exists
&& nflag
) {
256 printf("%s not overwritten\n", to
.p_path
);
259 if ((len
= readlink(p
->fts_path
, llink
, sizeof(llink
) - 1)) == -1) {
260 warn("readlink: %s", p
->fts_path
);
264 if (exists
&& unlink(to
.p_path
)) {
265 warn("unlink: %s", to
.p_path
);
268 if (symlink(llink
, to
.p_path
)) {
269 warn("symlink: %s", llink
);
272 return (pflag
? setfile(p
->fts_statp
, -1) : 0);
276 copy_fifo(struct stat
*from_stat
, int exists
)
279 if (exists
&& nflag
) {
281 printf("%s not overwritten\n", to
.p_path
);
284 if (exists
&& unlink(to
.p_path
)) {
285 warn("unlink: %s", to
.p_path
);
288 if (mkfifo(to
.p_path
, from_stat
->st_mode
)) {
289 warn("mkfifo: %s", to
.p_path
);
292 return (pflag
? setfile(from_stat
, -1) : 0);
296 copy_special(struct stat
*from_stat
, int exists
)
299 if (exists
&& nflag
) {
301 printf("%s not overwritten\n", to
.p_path
);
304 if (exists
&& unlink(to
.p_path
)) {
305 warn("unlink: %s", to
.p_path
);
308 if (mknod(to
.p_path
, from_stat
->st_mode
, from_stat
->st_rdev
)) {
309 warn("mknod: %s", to
.p_path
);
312 return (pflag
? setfile(from_stat
, -1) : 0);
316 setfile(struct stat
*fs
, int fd
)
318 static struct timespec tspec
[2];
320 int rval
, gotstat
, islink
, fdval
;
324 islink
= !fdval
&& S_ISLNK(fs
->st_mode
);
325 fs
->st_mode
&= S_ISUID
| S_ISGID
| S_ISVTX
|
326 S_IRWXU
| S_IRWXG
| S_IRWXO
;
328 tspec
[0] = fs
->st_atim
;
329 tspec
[1] = fs
->st_mtim
;
330 if (fdval
? futimens(fd
, tspec
) : utimensat(AT_FDCWD
, to
.p_path
, tspec
,
331 islink
? AT_SYMLINK_NOFOLLOW
: 0)) {
332 warn("utimensat: %s", to
.p_path
);
335 if (fdval
? fstat(fd
, &ts
) :
336 (islink
? lstat(to
.p_path
, &ts
) : stat(to
.p_path
, &ts
)))
340 ts
.st_mode
&= S_ISUID
| S_ISGID
| S_ISVTX
|
341 S_IRWXU
| S_IRWXG
| S_IRWXO
;
344 * Changing the ownership probably won't succeed, unless we're root
345 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
346 * the mode; current BSD behavior is to remove all setuid bits on
347 * chown. If chown fails, lose setuid/setgid bits.
349 if (!gotstat
|| fs
->st_uid
!= ts
.st_uid
|| fs
->st_gid
!= ts
.st_gid
)
350 if (fdval
? fchown(fd
, fs
->st_uid
, fs
->st_gid
) :
351 (islink
? lchown(to
.p_path
, fs
->st_uid
, fs
->st_gid
) :
352 chown(to
.p_path
, fs
->st_uid
, fs
->st_gid
))) {
353 if (errno
!= EPERM
) {
354 warn("chown: %s", to
.p_path
);
357 fs
->st_mode
&= ~(S_ISUID
| S_ISGID
);
360 if (!gotstat
|| fs
->st_mode
!= ts
.st_mode
)
361 if (fdval
? fchmod(fd
, fs
->st_mode
) :
362 (islink
? lchmod(to
.p_path
, fs
->st_mode
) :
363 chmod(to
.p_path
, fs
->st_mode
))) {
364 warn("chmod: %s", to
.p_path
);
368 if (!Nflag
&& (!gotstat
|| fs
->st_flags
!= ts
.st_flags
))
370 fchflags(fd
, fs
->st_flags
) :
371 (islink
? lchflags(to
.p_path
, fs
->st_flags
) :
372 chflags(to
.p_path
, fs
->st_flags
))) {
374 * NFS doesn't support chflags; ignore errors unless
375 * there's reason to believe we're losing bits. (Note,
376 * this still won't be right if the server supports
377 * flags and we were trying to *remove* flags on a file
378 * that we copied, i.e., that we didn't create.)
380 if (errno
!= EOPNOTSUPP
|| fs
->st_flags
!= 0) {
381 warn("chflags: %s", to
.p_path
);
390 preserve_fd_acls(int source_fd
, int dest_fd
)
394 int acl_supported
= 0, ret
, trivial
;
396 ret
= fpathconf(source_fd
, _PC_ACL_NFS4
);
399 acl_type
= ACL_TYPE_NFS4
;
400 } else if (ret
< 0 && errno
!= EINVAL
) {
401 warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", to
.p_path
);
404 if (acl_supported
== 0) {
405 ret
= fpathconf(source_fd
, _PC_ACL_EXTENDED
);
408 acl_type
= ACL_TYPE_ACCESS
;
409 } else if (ret
< 0 && errno
!= EINVAL
) {
410 warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
415 if (acl_supported
== 0)
418 acl
= acl_get_fd_np(source_fd
, acl_type
);
420 warn("failed to get acl entries while setting %s", to
.p_path
);
423 if (acl_is_trivial_np(acl
, &trivial
)) {
424 warn("acl_is_trivial() failed for %s", to
.p_path
);
432 if (acl_set_fd_np(dest_fd
, acl
, acl_type
) < 0) {
433 warn("failed to set acl entries for %s", to
.p_path
);
442 preserve_dir_acls(struct stat
*fs
, char *source_dir
, char *dest_dir
)
444 acl_t (*aclgetf
)(const char *, acl_type_t
);
445 int (*aclsetf
)(const char *, acl_type_t
, acl_t
);
449 int acl_supported
= 0, ret
, trivial
;
451 ret
= pathconf(source_dir
, _PC_ACL_NFS4
);
454 acl_type
= ACL_TYPE_NFS4
;
455 } else if (ret
< 0 && errno
!= EINVAL
) {
456 warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", source_dir
);
459 if (acl_supported
== 0) {
460 ret
= pathconf(source_dir
, _PC_ACL_EXTENDED
);
463 acl_type
= ACL_TYPE_ACCESS
;
464 } else if (ret
< 0 && errno
!= EINVAL
) {
465 warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
470 if (acl_supported
== 0)
474 * If the file is a link we will not follow it.
476 if (S_ISLNK(fs
->st_mode
)) {
477 aclgetf
= acl_get_link_np
;
478 aclsetf
= acl_set_link_np
;
480 aclgetf
= acl_get_file
;
481 aclsetf
= acl_set_file
;
483 if (acl_type
== ACL_TYPE_ACCESS
) {
485 * Even if there is no ACL_TYPE_DEFAULT entry here, a zero
486 * size ACL will be returned. So it is not safe to simply
487 * check the pointer to see if the default ACL is present.
489 acl
= aclgetf(source_dir
, ACL_TYPE_DEFAULT
);
491 warn("failed to get default acl entries on %s",
495 aclp
= &acl
->ats_acl
;
496 if (aclp
->acl_cnt
!= 0 && aclsetf(dest_dir
,
497 ACL_TYPE_DEFAULT
, acl
) < 0) {
498 warn("failed to set default acl entries on %s",
505 acl
= aclgetf(source_dir
, acl_type
);
507 warn("failed to get acl entries on %s", source_dir
);
510 if (acl_is_trivial_np(acl
, &trivial
)) {
511 warn("acl_is_trivial() failed on %s", source_dir
);
519 if (aclsetf(dest_dir
, acl_type
, acl
) < 0) {
520 warn("failed to set acl entries on %s", dest_dir
);
532 (void)fprintf(stderr
, "%s\n%s\n",
533 "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpsvx] "
534 "source_file target_file",
535 " cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpsvx] "