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$");
38 #include <sys/types.h>
40 #include <sys/param.h>
42 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
58 #define cp_pct(x, y) ((y == 0) ? 0 : (int)(100.0 * (x) / (y)))
61 * Memory strategy threshold, in pages: if physmem is larger then this, use a
64 #define PHYSPAGES_THRESHOLD (32*1024)
66 /* Maximum buffer size in bytes - do not allow it to grow larger than this. */
67 #define BUFSIZE_MAX (2*1024*1024)
70 * Small (default) buffer size in bytes. It's inefficient for this to be
71 * smaller than MAXPHYS.
73 #define BUFSIZE_SMALL (MAXPHYS)
76 copy_file(const FTSENT *entp, int dne)
78 static char *buf = NULL;
79 static size_t bufsize;
84 int ch, checkch, from_fd, rcount, rval, to_fd;
86 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
91 if (!lflag && !sflag &&
92 (from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
93 warn("%s", entp->fts_path);
100 * If the file exists and we're interactive, verify with the user.
101 * If the file DNE, set the mode to be the from file, minus setuid
102 * bits, modified by the umask; arguably wrong, but it makes copying
103 * executables work right and it's been that way forever. (The
104 * other choice is 666 or'ed with the execute bits on the from file
105 * modified by the umask.)
108 #define YESNO "(y/n [n]) "
111 printf("%s not overwritten\n", to.p_path);
115 (void)fprintf(stderr, "overwrite %s? %s",
117 checkch = ch = getchar();
118 while (ch != '\n' && ch != EOF)
120 if (checkch != 'y' && checkch != 'Y') {
121 (void)fprintf(stderr, "not overwritten\n");
129 * Remove existing destination file name create a new
132 (void)unlink(to.p_path);
133 if (!lflag && !sflag) {
134 to_fd = open(to.p_path,
135 O_WRONLY | O_TRUNC | O_CREAT,
136 fs->st_mode & ~(S_ISUID | S_ISGID));
138 } else if (!lflag && !sflag) {
139 /* Overwrite existing destination file name. */
140 to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
142 } else if (!lflag && !sflag) {
143 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
144 fs->st_mode & ~(S_ISUID | S_ISGID));
147 if (!lflag && !sflag && to_fd == -1) {
148 warn("%s", to.p_path);
155 if (!lflag && !sflag) {
157 * Mmap and write if less than 8M (the limit is so we don't
158 * totally trash memory on big files. This is really a minor
159 * hack, but it wins some CPU back.
160 * Some filesystems, such as smbnetfs, don't support mmap,
161 * so this is a best-effort attempt.
163 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
164 if (S_ISREG(fs->st_mode) && fs->st_size > 0 &&
165 fs->st_size <= 8 * 1024 * 1024 &&
166 (p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
167 MAP_SHARED, from_fd, (off_t)0)) != MAP_FAILED) {
169 for (bufp = p, wresid = fs->st_size; ;
170 bufp += wcount, wresid -= (size_t)wcount) {
171 wcount = write(to_fd, bufp, wresid);
177 (void)fprintf(stderr,
179 entp->fts_path, to.p_path,
180 cp_pct(wtotal, fs->st_size));
182 if (wcount >= (ssize_t)wresid)
185 if (wcount != (ssize_t)wresid) {
186 warn("%s", to.p_path);
189 /* Some systems don't unmap on close(2). */
190 if (munmap(p, fs->st_size) < 0) {
191 warn("%s", entp->fts_path);
199 * Note that buf and bufsize are static. If
200 * malloc() fails, it will fail at the start
201 * and not copy only some files.
203 if (sysconf(_SC_PHYS_PAGES) >
205 bufsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
207 bufsize = BUFSIZE_SMALL;
208 buf = malloc(bufsize);
210 err(1, "Not enough memory");
213 while ((rcount = read(from_fd, buf, bufsize)) > 0) {
214 for (bufp = buf, wresid = rcount; ;
215 bufp += wcount, wresid -= wcount) {
216 wcount = write(to_fd, bufp, wresid);
222 (void)fprintf(stderr,
224 entp->fts_path, to.p_path,
225 cp_pct(wtotal, fs->st_size));
227 if (wcount >= (ssize_t)wresid)
230 if (wcount != (ssize_t)wresid) {
231 warn("%s", to.p_path);
237 warn("%s", entp->fts_path);
242 if (link(entp->fts_path, to.p_path)) {
243 warn("%s", to.p_path);
247 if (symlink(entp->fts_path, to.p_path)) {
248 warn("%s", to.p_path);
254 * Don't remove the target even after an error. The target might
255 * not be a regular file, or its attributes might be important,
256 * or its contents might be irreplaceable. It would only be safe
257 * to remove it if we created it and its length is 0.
260 if (!lflag && !sflag) {
261 if (pflag && setfile(fs, to_fd))
263 if (pflag && preserve_fd_acls(from_fd, to_fd) != 0)
266 warn("%s", to.p_path);
273 (void)close(from_fd);
278 copy_link(const FTSENT *p, int exists)
281 char llink[PATH_MAX];
283 if (exists && nflag) {
285 printf("%s not overwritten\n", to.p_path);
288 if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) {
289 warn("readlink: %s", p->fts_path);
293 if (exists && unlink(to.p_path)) {
294 warn("unlink: %s", to.p_path);
297 if (symlink(llink, to.p_path)) {
298 warn("symlink: %s", llink);
301 return (pflag ? setfile(p->fts_statp, -1) : 0);
305 copy_fifo(struct stat *from_stat, int exists)
308 if (exists && nflag) {
310 printf("%s not overwritten\n", to.p_path);
313 if (exists && unlink(to.p_path)) {
314 warn("unlink: %s", to.p_path);
317 if (mkfifo(to.p_path, from_stat->st_mode)) {
318 warn("mkfifo: %s", to.p_path);
321 return (pflag ? setfile(from_stat, -1) : 0);
325 copy_special(struct stat *from_stat, int exists)
328 if (exists && nflag) {
330 printf("%s not overwritten\n", to.p_path);
333 if (exists && unlink(to.p_path)) {
334 warn("unlink: %s", to.p_path);
337 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
338 warn("mknod: %s", to.p_path);
341 return (pflag ? setfile(from_stat, -1) : 0);
345 setfile(struct stat *fs, int fd)
347 static struct timespec tspec[2];
349 int rval, gotstat, islink, fdval;
353 islink = !fdval && S_ISLNK(fs->st_mode);
354 fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX |
355 S_IRWXU | S_IRWXG | S_IRWXO;
357 tspec[0] = fs->st_atim;
358 tspec[1] = fs->st_mtim;
359 if (fdval ? futimens(fd, tspec) : utimensat(AT_FDCWD, to.p_path, tspec,
360 islink ? AT_SYMLINK_NOFOLLOW : 0)) {
361 warn("utimensat: %s", to.p_path);
364 if (fdval ? fstat(fd, &ts) :
365 (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts)))
369 ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
370 S_IRWXU | S_IRWXG | S_IRWXO;
373 * Changing the ownership probably won't succeed, unless we're root
374 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
375 * the mode; current BSD behavior is to remove all setuid bits on
376 * chown. If chown fails, lose setuid/setgid bits.
378 if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
379 if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
380 (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
381 chown(to.p_path, fs->st_uid, fs->st_gid))) {
382 if (errno != EPERM) {
383 warn("chown: %s", to.p_path);
386 fs->st_mode &= ~(S_ISUID | S_ISGID);
389 if (!gotstat || fs->st_mode != ts.st_mode)
390 if (fdval ? fchmod(fd, fs->st_mode) :
391 (islink ? lchmod(to.p_path, fs->st_mode) :
392 chmod(to.p_path, fs->st_mode))) {
393 warn("chmod: %s", to.p_path);
397 if (!gotstat || fs->st_flags != ts.st_flags)
399 fchflags(fd, fs->st_flags) :
400 (islink ? lchflags(to.p_path, fs->st_flags) :
401 chflags(to.p_path, fs->st_flags))) {
402 warn("chflags: %s", to.p_path);
410 preserve_fd_acls(int source_fd, int dest_fd)
414 int acl_supported = 0, ret, trivial;
416 ret = fpathconf(source_fd, _PC_ACL_NFS4);
419 acl_type = ACL_TYPE_NFS4;
420 } else if (ret < 0 && errno != EINVAL) {
421 warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", to.p_path);
424 if (acl_supported == 0) {
425 ret = fpathconf(source_fd, _PC_ACL_EXTENDED);
428 acl_type = ACL_TYPE_ACCESS;
429 } else if (ret < 0 && errno != EINVAL) {
430 warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
435 if (acl_supported == 0)
438 acl = acl_get_fd_np(source_fd, acl_type);
440 warn("failed to get acl entries while setting %s", to.p_path);
443 if (acl_is_trivial_np(acl, &trivial)) {
444 warn("acl_is_trivial() failed for %s", to.p_path);
452 if (acl_set_fd_np(dest_fd, acl, acl_type) < 0) {
453 warn("failed to set acl entries for %s", to.p_path);
462 preserve_dir_acls(struct stat *fs, char *source_dir, char *dest_dir)
464 acl_t (*aclgetf)(const char *, acl_type_t);
465 int (*aclsetf)(const char *, acl_type_t, acl_t);
469 int acl_supported = 0, ret, trivial;
471 ret = pathconf(source_dir, _PC_ACL_NFS4);
474 acl_type = ACL_TYPE_NFS4;
475 } else if (ret < 0 && errno != EINVAL) {
476 warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", source_dir);
479 if (acl_supported == 0) {
480 ret = pathconf(source_dir, _PC_ACL_EXTENDED);
483 acl_type = ACL_TYPE_ACCESS;
484 } else if (ret < 0 && errno != EINVAL) {
485 warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
490 if (acl_supported == 0)
494 * If the file is a link we will not follow it.
496 if (S_ISLNK(fs->st_mode)) {
497 aclgetf = acl_get_link_np;
498 aclsetf = acl_set_link_np;
500 aclgetf = acl_get_file;
501 aclsetf = acl_set_file;
503 if (acl_type == ACL_TYPE_ACCESS) {
505 * Even if there is no ACL_TYPE_DEFAULT entry here, a zero
506 * size ACL will be returned. So it is not safe to simply
507 * check the pointer to see if the default ACL is present.
509 acl = aclgetf(source_dir, ACL_TYPE_DEFAULT);
511 warn("failed to get default acl entries on %s",
515 aclp = &acl->ats_acl;
516 if (aclp->acl_cnt != 0 && aclsetf(dest_dir,
517 ACL_TYPE_DEFAULT, acl) < 0) {
518 warn("failed to set default acl entries on %s",
525 acl = aclgetf(source_dir, acl_type);
527 warn("failed to get acl entries on %s", source_dir);
530 if (acl_is_trivial_np(acl, &trivial)) {
531 warn("acl_is_trivial() failed on %s", source_dir);
539 if (aclsetf(dest_dir, acl_type, acl) < 0) {
540 warn("failed to set acl entries on %s", dest_dir);
552 (void)fprintf(stderr, "%s\n%s\n",
553 "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpsvx] "
554 "source_file target_file",
555 " cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpsvx] "