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 copy_file(const FTSENT *entp, int dne)
63 static char buf[MAXBSIZE];
68 int ch, checkch, from_fd = 0, rcount, rval, to_fd = 0;
70 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
74 if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
75 warn("%s", entp->fts_path);
82 * If the file exists and we're interactive, verify with the user.
83 * If the file DNE, set the mode to be the from file, minus setuid
84 * bits, modified by the umask; arguably wrong, but it makes copying
85 * executables work right and it's been that way forever. (The
86 * other choice is 666 or'ed with the execute bits on the from file
87 * modified by the umask.)
90 #define YESNO "(y/n [n]) "
93 printf("%s not overwritten\n", to.p_path);
97 (void)fprintf(stderr, "overwrite %s? %s",
99 checkch = ch = getchar();
100 while (ch != '\n' && ch != EOF)
102 if (checkch != 'y' && checkch != 'Y') {
103 (void)close(from_fd);
104 (void)fprintf(stderr, "not overwritten\n");
110 /* remove existing destination file name,
111 * create a new file */
112 (void)unlink(to.p_path);
114 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
115 fs->st_mode & ~(S_ISUID | S_ISGID));
118 /* overwrite existing destination file name */
119 to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
123 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
124 fs->st_mode & ~(S_ISUID | S_ISGID));
128 warn("%s", to.p_path);
129 (void)close(from_fd);
137 * Mmap and write if less than 8M (the limit is so we don't totally
138 * trash memory on big files. This is really a minor hack, but it
139 * wins some CPU back.
140 * Some filesystems, such as smbnetfs, don't support mmap,
141 * so this is a best-effort attempt.
143 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
144 if (S_ISREG(fs->st_mode) && fs->st_size > 0 &&
145 fs->st_size <= 8 * 1024 * 1024 &&
146 (p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
147 MAP_SHARED, from_fd, (off_t)0)) != MAP_FAILED) {
149 for (bufp = p, wresid = fs->st_size; ;
150 bufp += wcount, wresid -= (size_t)wcount) {
151 wcount = write(to_fd, bufp, wresid);
157 (void)fprintf(stderr,
159 entp->fts_path, to.p_path,
160 cp_pct(wtotal, fs->st_size));
162 if (wcount >= (ssize_t)wresid)
165 if (wcount != (ssize_t)wresid) {
166 warn("%s", to.p_path);
169 /* Some systems don't unmap on close(2). */
170 if (munmap(p, fs->st_size) < 0) {
171 warn("%s", entp->fts_path);
178 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
179 for (bufp = buf, wresid = rcount; ;
180 bufp += wcount, wresid -= wcount) {
181 wcount = write(to_fd, bufp, wresid);
187 (void)fprintf(stderr,
189 entp->fts_path, to.p_path,
190 cp_pct(wtotal, fs->st_size));
192 if (wcount >= (ssize_t)wresid)
195 if (wcount != (ssize_t)wresid) {
196 warn("%s", to.p_path);
202 warn("%s", entp->fts_path);
207 if (link(entp->fts_path, to.p_path)) {
208 warn("%s", to.p_path);
214 * Don't remove the target even after an error. The target might
215 * not be a regular file, or its attributes might be important,
216 * or its contents might be irreplaceable. It would only be safe
217 * to remove it if we created it and its length is 0.
221 if (pflag && setfile(fs, to_fd))
223 if (pflag && preserve_fd_acls(from_fd, to_fd) != 0)
225 (void)close(from_fd);
227 warn("%s", to.p_path);
232 (void)close(from_fd);
238 copy_link(const FTSENT *p, int exists)
241 char llink[PATH_MAX];
243 if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) {
244 warn("readlink: %s", p->fts_path);
248 if (exists && unlink(to.p_path)) {
249 warn("unlink: %s", to.p_path);
252 if (symlink(llink, to.p_path)) {
253 warn("symlink: %s", llink);
256 return (pflag ? setfile(p->fts_statp, -1) : 0);
260 copy_fifo(struct stat *from_stat, int exists)
262 if (exists && unlink(to.p_path)) {
263 warn("unlink: %s", to.p_path);
266 if (mkfifo(to.p_path, from_stat->st_mode)) {
267 warn("mkfifo: %s", to.p_path);
270 return (pflag ? setfile(from_stat, -1) : 0);
274 copy_special(struct stat *from_stat, int exists)
276 if (exists && unlink(to.p_path)) {
277 warn("unlink: %s", to.p_path);
280 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
281 warn("mknod: %s", to.p_path);
284 return (pflag ? setfile(from_stat, -1) : 0);
288 setfile(struct stat *fs, int fd)
290 static struct timeval tv[2];
292 int rval, gotstat, islink, fdval;
296 islink = !fdval && S_ISLNK(fs->st_mode);
297 fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX |
298 S_IRWXU | S_IRWXG | S_IRWXO;
300 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
301 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
302 if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) {
303 warn("%sutimes: %s", islink ? "l" : "", to.p_path);
306 if (fdval ? fstat(fd, &ts) :
307 (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts)))
311 ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
312 S_IRWXU | S_IRWXG | S_IRWXO;
315 * Changing the ownership probably won't succeed, unless we're root
316 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
317 * the mode; current BSD behavior is to remove all setuid bits on
318 * chown. If chown fails, lose setuid/setgid bits.
320 if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
321 if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
322 (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
323 chown(to.p_path, fs->st_uid, fs->st_gid))) {
324 if (errno != EPERM) {
325 warn("chown: %s", to.p_path);
328 fs->st_mode &= ~(S_ISUID | S_ISGID);
331 if (!gotstat || fs->st_mode != ts.st_mode)
332 if (fdval ? fchmod(fd, fs->st_mode) :
333 (islink ? lchmod(to.p_path, fs->st_mode) :
334 chmod(to.p_path, fs->st_mode))) {
335 warn("chmod: %s", to.p_path);
339 if (!gotstat || fs->st_flags != ts.st_flags)
341 fchflags(fd, fs->st_flags) :
342 (islink ? (errno = ENOSYS) :
343 chflags(to.p_path, fs->st_flags))) {
344 warn("chflags: %s", to.p_path);
352 preserve_fd_acls(int source_fd, int dest_fd)
357 if (fpathconf(source_fd, _PC_ACL_EXTENDED) != 1 ||
358 fpathconf(dest_fd, _PC_ACL_EXTENDED) != 1)
360 acl = acl_get_fd(source_fd);
362 warn("failed to get acl entries while setting %s", to.p_path);
365 aclp = &acl->ats_acl;
366 if (aclp->acl_cnt == 3)
368 if (acl_set_fd(dest_fd, acl) < 0) {
369 warn("failed to set acl entries for %s", to.p_path);
376 preserve_dir_acls(struct stat *fs, char *source_dir, char *dest_dir)
378 acl_t (*aclgetf)(const char *, acl_type_t);
379 int (*aclsetf)(const char *, acl_type_t, acl_t);
383 if (pathconf(source_dir, _PC_ACL_EXTENDED) != 1 ||
384 pathconf(dest_dir, _PC_ACL_EXTENDED) != 1)
387 * If the file is a link we will not follow it
389 if (S_ISLNK(fs->st_mode)) {
390 aclgetf = acl_get_link_np;
391 aclsetf = acl_set_link_np;
393 aclgetf = acl_get_file;
394 aclsetf = acl_set_file;
397 * Even if there is no ACL_TYPE_DEFAULT entry here, a zero
398 * size ACL will be returned. So it is not safe to simply
399 * check the pointer to see if the default ACL is present.
401 acl = aclgetf(source_dir, ACL_TYPE_DEFAULT);
403 warn("failed to get default acl entries on %s",
407 aclp = &acl->ats_acl;
408 if (aclp->acl_cnt != 0 && aclsetf(dest_dir,
409 ACL_TYPE_DEFAULT, acl) < 0) {
410 warn("failed to set default acl entries on %s",
414 acl = aclgetf(source_dir, ACL_TYPE_ACCESS);
416 warn("failed to get acl entries on %s", source_dir);
419 aclp = &acl->ats_acl;
420 if (aclsetf(dest_dir, ACL_TYPE_ACCESS, acl) < 0) {
421 warn("failed to set acl entries on %s", dest_dir);
431 (void)fprintf(stderr, "%s\n%s\n",
432 "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpv] source_file target_file",
433 " cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpv] source_file ... "