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)))
60 /* Memory strategy threshold, in pages: if physmem is larger then this, use a
62 #define PHYSPAGES_THRESHOLD (32*1024)
64 /* Maximum buffer size in bytes - do not allow it to grow larger than this */
65 #define BUFSIZE_MAX (2*1024*1024)
67 /* Small (default) buffer size in bytes. It's inefficient for this to be
68 * smaller than MAXPHYS */
69 #define BUFSIZE_SMALL (MAXPHYS)
72 copy_file(const FTSENT *entp, int dne)
74 static char *buf = NULL;
75 static size_t bufsize;
80 int ch, checkch, from_fd = 0, rcount, rval, to_fd = 0;
82 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
86 if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
87 warn("%s", entp->fts_path);
94 * If the file exists and we're interactive, verify with the user.
95 * If the file DNE, set the mode to be the from file, minus setuid
96 * bits, modified by the umask; arguably wrong, but it makes copying
97 * executables work right and it's been that way forever. (The
98 * other choice is 666 or'ed with the execute bits on the from file
99 * modified by the umask.)
102 #define YESNO "(y/n [n]) "
105 printf("%s not overwritten\n", to.p_path);
106 (void)close(from_fd);
109 (void)fprintf(stderr, "overwrite %s? %s",
111 checkch = ch = getchar();
112 while (ch != '\n' && ch != EOF)
114 if (checkch != 'y' && checkch != 'Y') {
115 (void)close(from_fd);
116 (void)fprintf(stderr, "not overwritten\n");
122 /* remove existing destination file name,
123 * create a new file */
124 (void)unlink(to.p_path);
126 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
127 fs->st_mode & ~(S_ISUID | S_ISGID));
130 /* overwrite existing destination file name */
131 to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
135 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
136 fs->st_mode & ~(S_ISUID | S_ISGID));
140 warn("%s", to.p_path);
141 (void)close(from_fd);
149 * Mmap and write if less than 8M (the limit is so we don't totally
150 * trash memory on big files. This is really a minor hack, but it
151 * wins some CPU back.
152 * Some filesystems, such as smbnetfs, don't support mmap,
153 * so this is a best-effort attempt.
155 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
156 if (S_ISREG(fs->st_mode) && fs->st_size > 0 &&
157 fs->st_size <= 8 * 1024 * 1024 &&
158 (p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
159 MAP_SHARED, from_fd, (off_t)0)) != MAP_FAILED) {
161 for (bufp = p, wresid = fs->st_size; ;
162 bufp += wcount, wresid -= (size_t)wcount) {
163 wcount = write(to_fd, bufp, wresid);
169 (void)fprintf(stderr,
171 entp->fts_path, to.p_path,
172 cp_pct(wtotal, fs->st_size));
174 if (wcount >= (ssize_t)wresid)
177 if (wcount != (ssize_t)wresid) {
178 warn("%s", to.p_path);
181 /* Some systems don't unmap on close(2). */
182 if (munmap(p, fs->st_size) < 0) {
183 warn("%s", entp->fts_path);
191 * Note that buf and bufsize are static. If
192 * malloc() fails, it will fail at the start
193 * and not copy only some files.
195 if (sysconf(_SC_PHYS_PAGES) >
197 bufsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
199 bufsize = BUFSIZE_SMALL;
200 buf = malloc(bufsize);
202 err(1, "Not enough memory");
205 while ((rcount = read(from_fd, buf, bufsize)) > 0) {
206 for (bufp = buf, wresid = rcount; ;
207 bufp += wcount, wresid -= wcount) {
208 wcount = write(to_fd, bufp, wresid);
214 (void)fprintf(stderr,
216 entp->fts_path, to.p_path,
217 cp_pct(wtotal, fs->st_size));
219 if (wcount >= (ssize_t)wresid)
222 if (wcount != (ssize_t)wresid) {
223 warn("%s", to.p_path);
229 warn("%s", entp->fts_path);
234 if (link(entp->fts_path, to.p_path)) {
235 warn("%s", to.p_path);
241 * Don't remove the target even after an error. The target might
242 * not be a regular file, or its attributes might be important,
243 * or its contents might be irreplaceable. It would only be safe
244 * to remove it if we created it and its length is 0.
248 if (pflag && setfile(fs, to_fd))
250 if (pflag && preserve_fd_acls(from_fd, to_fd) != 0)
253 warn("%s", to.p_path);
258 (void)close(from_fd);
264 copy_link(const FTSENT *p, int exists)
267 char llink[PATH_MAX];
269 if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) {
270 warn("readlink: %s", p->fts_path);
274 if (exists && unlink(to.p_path)) {
275 warn("unlink: %s", to.p_path);
278 if (symlink(llink, to.p_path)) {
279 warn("symlink: %s", llink);
282 return (pflag ? setfile(p->fts_statp, -1) : 0);
286 copy_fifo(struct stat *from_stat, int exists)
288 if (exists && unlink(to.p_path)) {
289 warn("unlink: %s", to.p_path);
292 if (mkfifo(to.p_path, from_stat->st_mode)) {
293 warn("mkfifo: %s", to.p_path);
296 return (pflag ? setfile(from_stat, -1) : 0);
300 copy_special(struct stat *from_stat, int exists)
302 if (exists && unlink(to.p_path)) {
303 warn("unlink: %s", to.p_path);
306 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
307 warn("mknod: %s", to.p_path);
310 return (pflag ? setfile(from_stat, -1) : 0);
314 setfile(struct stat *fs, int fd)
316 static struct timeval tv[2];
318 int rval, gotstat, islink, fdval;
322 islink = !fdval && S_ISLNK(fs->st_mode);
323 fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX |
324 S_IRWXU | S_IRWXG | S_IRWXO;
326 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
327 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
328 if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) {
329 warn("%sutimes: %s", islink ? "l" : "", to.p_path);
332 if (fdval ? fstat(fd, &ts) :
333 (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts)))
337 ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
338 S_IRWXU | S_IRWXG | S_IRWXO;
341 * Changing the ownership probably won't succeed, unless we're root
342 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
343 * the mode; current BSD behavior is to remove all setuid bits on
344 * chown. If chown fails, lose setuid/setgid bits.
346 if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
347 if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
348 (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
349 chown(to.p_path, fs->st_uid, fs->st_gid))) {
350 if (errno != EPERM) {
351 warn("chown: %s", to.p_path);
354 fs->st_mode &= ~(S_ISUID | S_ISGID);
357 if (!gotstat || fs->st_mode != ts.st_mode)
358 if (fdval ? fchmod(fd, fs->st_mode) :
359 (islink ? lchmod(to.p_path, fs->st_mode) :
360 chmod(to.p_path, fs->st_mode))) {
361 warn("chmod: %s", to.p_path);
365 if (!gotstat || fs->st_flags != ts.st_flags)
367 fchflags(fd, fs->st_flags) :
368 (islink ? lchflags(to.p_path, fs->st_flags) :
369 chflags(to.p_path, fs->st_flags))) {
370 warn("chflags: %s", to.p_path);
378 preserve_fd_acls(int source_fd, int dest_fd)
382 int acl_supported = 0, ret, trivial;
384 ret = fpathconf(source_fd, _PC_ACL_NFS4);
387 acl_type = ACL_TYPE_NFS4;
388 } else if (ret < 0 && errno != EINVAL) {
389 warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", to.p_path);
392 if (acl_supported == 0) {
393 ret = fpathconf(source_fd, _PC_ACL_EXTENDED);
396 acl_type = ACL_TYPE_ACCESS;
397 } else if (ret < 0 && errno != EINVAL) {
398 warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
403 if (acl_supported == 0)
406 acl = acl_get_fd_np(source_fd, acl_type);
408 warn("failed to get acl entries while setting %s", to.p_path);
411 if (acl_is_trivial_np(acl, &trivial)) {
412 warn("acl_is_trivial() failed for %s", to.p_path);
420 if (acl_set_fd_np(dest_fd, acl, acl_type) < 0) {
421 warn("failed to set acl entries for %s", to.p_path);
430 preserve_dir_acls(struct stat *fs, char *source_dir, char *dest_dir)
432 acl_t (*aclgetf)(const char *, acl_type_t);
433 int (*aclsetf)(const char *, acl_type_t, acl_t);
437 int acl_supported = 0, ret, trivial;
439 ret = pathconf(source_dir, _PC_ACL_NFS4);
442 acl_type = ACL_TYPE_NFS4;
443 } else if (ret < 0 && errno != EINVAL) {
444 warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", source_dir);
447 if (acl_supported == 0) {
448 ret = pathconf(source_dir, _PC_ACL_EXTENDED);
451 acl_type = ACL_TYPE_ACCESS;
452 } else if (ret < 0 && errno != EINVAL) {
453 warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
458 if (acl_supported == 0)
462 * If the file is a link we will not follow it
464 if (S_ISLNK(fs->st_mode)) {
465 aclgetf = acl_get_link_np;
466 aclsetf = acl_set_link_np;
468 aclgetf = acl_get_file;
469 aclsetf = acl_set_file;
471 if (acl_type == ACL_TYPE_ACCESS) {
473 * Even if there is no ACL_TYPE_DEFAULT entry here, a zero
474 * size ACL will be returned. So it is not safe to simply
475 * check the pointer to see if the default ACL is present.
477 acl = aclgetf(source_dir, ACL_TYPE_DEFAULT);
479 warn("failed to get default acl entries on %s",
483 aclp = &acl->ats_acl;
484 if (aclp->acl_cnt != 0 && aclsetf(dest_dir,
485 ACL_TYPE_DEFAULT, acl) < 0) {
486 warn("failed to set default acl entries on %s",
493 acl = aclgetf(source_dir, acl_type);
495 warn("failed to get acl entries on %s", source_dir);
498 if (acl_is_trivial_np(acl, &trivial)) {
499 warn("acl_is_trivial() failed on %s", source_dir);
507 if (aclsetf(dest_dir, acl_type, acl) < 0) {
508 warn("failed to set acl entries on %s", dest_dir);
520 (void)fprintf(stderr, "%s\n%s\n",
521 "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpvx] source_file target_file",
522 " cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpvx] source_file ... "