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
34 static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/types.h>
42 #include <sys/param.h>
44 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
60 #define cp_pct(x, y) ((y == 0) ? 0 : (int)(100.0 * (x) / (y)))
63 * Memory strategy threshold, in pages: if physmem is larger then this, use a
66 #define PHYSPAGES_THRESHOLD (32*1024)
68 /* Maximum buffer size in bytes - do not allow it to grow larger than this. */
69 #define BUFSIZE_MAX (2*1024*1024)
72 * Small (default) buffer size in bytes. It's inefficient for this to be
73 * smaller than MAXPHYS.
75 #define BUFSIZE_SMALL (MAXPHYS)
78 copy_file(const FTSENT *entp, int dne)
80 static char *buf = NULL;
81 static size_t bufsize;
86 int ch, checkch, from_fd, rcount, rval, to_fd;
88 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
93 if (!lflag && !sflag &&
94 (from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
95 warn("%s", entp->fts_path);
102 * If the file exists and we're interactive, verify with the user.
103 * If the file DNE, set the mode to be the from file, minus setuid
104 * bits, modified by the umask; arguably wrong, but it makes copying
105 * executables work right and it's been that way forever. (The
106 * other choice is 666 or'ed with the execute bits on the from file
107 * modified by the umask.)
110 #define YESNO "(y/n [n]) "
113 printf("%s not overwritten\n", to.p_path);
117 (void)fprintf(stderr, "overwrite %s? %s",
119 checkch = ch = getchar();
120 while (ch != '\n' && ch != EOF)
122 if (checkch != 'y' && checkch != 'Y') {
123 (void)fprintf(stderr, "not overwritten\n");
131 * Remove existing destination file name create a new
134 (void)unlink(to.p_path);
135 if (!lflag && !sflag) {
136 to_fd = open(to.p_path,
137 O_WRONLY | O_TRUNC | O_CREAT,
138 fs->st_mode & ~(S_ISUID | S_ISGID));
140 } else if (!lflag && !sflag) {
141 /* Overwrite existing destination file name. */
142 to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
144 } else if (!lflag && !sflag) {
145 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
146 fs->st_mode & ~(S_ISUID | S_ISGID));
149 if (!lflag && !sflag && to_fd == -1) {
150 warn("%s", to.p_path);
157 if (!lflag && !sflag) {
159 * Mmap and write if less than 8M (the limit is so we don't
160 * totally trash memory on big files. This is really a minor
161 * hack, but it wins some CPU back.
162 * Some filesystems, such as smbnetfs, don't support mmap,
163 * so this is a best-effort attempt.
165 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
166 if (S_ISREG(fs->st_mode) && fs->st_size > 0 &&
167 fs->st_size <= 8 * 1024 * 1024 &&
168 (p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
169 MAP_SHARED, from_fd, (off_t)0)) != MAP_FAILED) {
171 for (bufp = p, wresid = fs->st_size; ;
172 bufp += wcount, wresid -= (size_t)wcount) {
173 wcount = write(to_fd, bufp, wresid);
179 (void)fprintf(stderr,
181 entp->fts_path, to.p_path,
182 cp_pct(wtotal, fs->st_size));
184 if (wcount >= (ssize_t)wresid)
187 if (wcount != (ssize_t)wresid) {
188 warn("%s", to.p_path);
191 /* Some systems don't unmap on close(2). */
192 if (munmap(p, fs->st_size) < 0) {
193 warn("%s", entp->fts_path);
201 * Note that buf and bufsize are static. If
202 * malloc() fails, it will fail at the start
203 * and not copy only some files.
205 if (sysconf(_SC_PHYS_PAGES) >
207 bufsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
209 bufsize = BUFSIZE_SMALL;
210 buf = malloc(bufsize);
212 err(1, "Not enough memory");
215 while ((rcount = read(from_fd, buf, bufsize)) > 0) {
216 for (bufp = buf, wresid = rcount; ;
217 bufp += wcount, wresid -= wcount) {
218 wcount = write(to_fd, bufp, wresid);
224 (void)fprintf(stderr,
226 entp->fts_path, to.p_path,
227 cp_pct(wtotal, fs->st_size));
229 if (wcount >= (ssize_t)wresid)
232 if (wcount != (ssize_t)wresid) {
233 warn("%s", to.p_path);
239 warn("%s", entp->fts_path);
244 if (link(entp->fts_path, to.p_path)) {
245 warn("%s", to.p_path);
249 if (symlink(entp->fts_path, to.p_path)) {
250 warn("%s", to.p_path);
256 * Don't remove the target even after an error. The target might
257 * not be a regular file, or its attributes might be important,
258 * or its contents might be irreplaceable. It would only be safe
259 * to remove it if we created it and its length is 0.
262 if (!lflag && !sflag) {
263 if (pflag && setfile(fs, to_fd))
265 if (pflag && preserve_fd_acls(from_fd, to_fd) != 0)
268 warn("%s", to.p_path);
275 (void)close(from_fd);
280 copy_link(const FTSENT *p, int exists)
283 char llink[PATH_MAX];
285 if (exists && nflag) {
287 printf("%s not overwritten\n", to.p_path);
290 if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) {
291 warn("readlink: %s", p->fts_path);
295 if (exists && unlink(to.p_path)) {
296 warn("unlink: %s", to.p_path);
299 if (symlink(llink, to.p_path)) {
300 warn("symlink: %s", llink);
303 return (pflag ? setfile(p->fts_statp, -1) : 0);
307 copy_fifo(struct stat *from_stat, int exists)
310 if (exists && nflag) {
312 printf("%s not overwritten\n", to.p_path);
315 if (exists && unlink(to.p_path)) {
316 warn("unlink: %s", to.p_path);
319 if (mkfifo(to.p_path, from_stat->st_mode)) {
320 warn("mkfifo: %s", to.p_path);
323 return (pflag ? setfile(from_stat, -1) : 0);
327 copy_special(struct stat *from_stat, int exists)
330 if (exists && nflag) {
332 printf("%s not overwritten\n", to.p_path);
335 if (exists && unlink(to.p_path)) {
336 warn("unlink: %s", to.p_path);
339 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
340 warn("mknod: %s", to.p_path);
343 return (pflag ? setfile(from_stat, -1) : 0);
347 setfile(struct stat *fs, int fd)
349 static struct timespec tspec[2];
351 int rval, gotstat, islink, fdval;
355 islink = !fdval && S_ISLNK(fs->st_mode);
356 fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX |
357 S_IRWXU | S_IRWXG | S_IRWXO;
359 tspec[0] = fs->st_atim;
360 tspec[1] = fs->st_mtim;
361 if (fdval ? futimens(fd, tspec) : utimensat(AT_FDCWD, to.p_path, tspec,
362 islink ? AT_SYMLINK_NOFOLLOW : 0)) {
363 warn("utimensat: %s", to.p_path);
366 if (fdval ? fstat(fd, &ts) :
367 (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts)))
371 ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
372 S_IRWXU | S_IRWXG | S_IRWXO;
375 * Changing the ownership probably won't succeed, unless we're root
376 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
377 * the mode; current BSD behavior is to remove all setuid bits on
378 * chown. If chown fails, lose setuid/setgid bits.
380 if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
381 if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
382 (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
383 chown(to.p_path, fs->st_uid, fs->st_gid))) {
384 if (errno != EPERM) {
385 warn("chown: %s", to.p_path);
388 fs->st_mode &= ~(S_ISUID | S_ISGID);
391 if (!gotstat || fs->st_mode != ts.st_mode)
392 if (fdval ? fchmod(fd, fs->st_mode) :
393 (islink ? lchmod(to.p_path, fs->st_mode) :
394 chmod(to.p_path, fs->st_mode))) {
395 warn("chmod: %s", to.p_path);
399 if (!gotstat || fs->st_flags != ts.st_flags)
401 fchflags(fd, fs->st_flags) :
402 (islink ? lchflags(to.p_path, fs->st_flags) :
403 chflags(to.p_path, fs->st_flags))) {
404 warn("chflags: %s", to.p_path);
412 preserve_fd_acls(int source_fd, int dest_fd)
416 int acl_supported = 0, ret, trivial;
418 ret = fpathconf(source_fd, _PC_ACL_NFS4);
421 acl_type = ACL_TYPE_NFS4;
422 } else if (ret < 0 && errno != EINVAL) {
423 warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", to.p_path);
426 if (acl_supported == 0) {
427 ret = fpathconf(source_fd, _PC_ACL_EXTENDED);
430 acl_type = ACL_TYPE_ACCESS;
431 } else if (ret < 0 && errno != EINVAL) {
432 warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
437 if (acl_supported == 0)
440 acl = acl_get_fd_np(source_fd, acl_type);
442 warn("failed to get acl entries while setting %s", to.p_path);
445 if (acl_is_trivial_np(acl, &trivial)) {
446 warn("acl_is_trivial() failed for %s", to.p_path);
454 if (acl_set_fd_np(dest_fd, acl, acl_type) < 0) {
455 warn("failed to set acl entries for %s", to.p_path);
464 preserve_dir_acls(struct stat *fs, char *source_dir, char *dest_dir)
466 acl_t (*aclgetf)(const char *, acl_type_t);
467 int (*aclsetf)(const char *, acl_type_t, acl_t);
471 int acl_supported = 0, ret, trivial;
473 ret = pathconf(source_dir, _PC_ACL_NFS4);
476 acl_type = ACL_TYPE_NFS4;
477 } else if (ret < 0 && errno != EINVAL) {
478 warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", source_dir);
481 if (acl_supported == 0) {
482 ret = pathconf(source_dir, _PC_ACL_EXTENDED);
485 acl_type = ACL_TYPE_ACCESS;
486 } else if (ret < 0 && errno != EINVAL) {
487 warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
492 if (acl_supported == 0)
496 * If the file is a link we will not follow it.
498 if (S_ISLNK(fs->st_mode)) {
499 aclgetf = acl_get_link_np;
500 aclsetf = acl_set_link_np;
502 aclgetf = acl_get_file;
503 aclsetf = acl_set_file;
505 if (acl_type == ACL_TYPE_ACCESS) {
507 * Even if there is no ACL_TYPE_DEFAULT entry here, a zero
508 * size ACL will be returned. So it is not safe to simply
509 * check the pointer to see if the default ACL is present.
511 acl = aclgetf(source_dir, ACL_TYPE_DEFAULT);
513 warn("failed to get default acl entries on %s",
517 aclp = &acl->ats_acl;
518 if (aclp->acl_cnt != 0 && aclsetf(dest_dir,
519 ACL_TYPE_DEFAULT, acl) < 0) {
520 warn("failed to set default acl entries on %s",
527 acl = aclgetf(source_dir, acl_type);
529 warn("failed to get acl entries on %s", source_dir);
532 if (acl_is_trivial_np(acl, &trivial)) {
533 warn("acl_is_trivial() failed on %s", source_dir);
541 if (aclsetf(dest_dir, acl_type, acl) < 0) {
542 warn("failed to set acl entries on %s", dest_dir);
554 (void)fprintf(stderr, "%s\n%s\n",
555 "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpsvx] "
556 "source_file target_file",
557 " cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpsvx] "