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/param.h>
40 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
55 #define cp_pct(x,y) (int)(100.0 * (double)(x) / (double)(y))
58 copy_file(const FTSENT *entp, int dne)
60 static char buf[MAXBSIZE];
62 int ch, checkch, from_fd, rcount, rval, to_fd;
67 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
71 if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
72 warn("%s", entp->fts_path);
79 * If the file exists and we're interactive, verify with the user.
80 * If the file DNE, set the mode to be the from file, minus setuid
81 * bits, modified by the umask; arguably wrong, but it makes copying
82 * executables work right and it's been that way forever. (The
83 * other choice is 666 or'ed with the execute bits on the from file
84 * modified by the umask.)
87 #define YESNO "(y/n [n]) "
90 printf("%s not overwritten\n", to.p_path);
94 (void)fprintf(stderr, "overwrite %s? %s",
96 checkch = ch = getchar();
97 while (ch != '\n' && ch != EOF)
99 if (checkch != 'y' && checkch != 'Y') {
100 (void)close(from_fd);
101 (void)fprintf(stderr, "not overwritten\n");
107 /* remove existing destination file name,
108 * create a new file */
109 (void)unlink(to.p_path);
110 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
111 fs->st_mode & ~(S_ISUID | S_ISGID));
113 /* overwrite existing destination file name */
114 to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
116 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
117 fs->st_mode & ~(S_ISUID | S_ISGID));
120 warn("%s", to.p_path);
121 (void)close(from_fd);
128 * Mmap and write if less than 8M (the limit is so we don't totally
129 * trash memory on big files. This is really a minor hack, but it
130 * wins some CPU back.
132 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
133 if (S_ISREG(fs->st_mode) && fs->st_size > 0 &&
134 fs->st_size <= 8 * 1048576) {
135 if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
136 MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) {
137 warn("%s", entp->fts_path);
141 for (bufp = p, wresid = fs->st_size; ;
142 bufp += wcount, wresid -= (size_t)wcount) {
143 wcount = write(to_fd, bufp, wresid);
147 (void)fprintf(stderr,
149 entp->fts_path, to.p_path,
150 cp_pct(wtotal, fs->st_size));
153 if (wcount >= (ssize_t)wresid || wcount <= 0)
156 if (wcount != (ssize_t)wresid) {
157 warn("%s", to.p_path);
160 /* Some systems don't unmap on close(2). */
161 if (munmap(p, fs->st_size) < 0) {
162 warn("%s", entp->fts_path);
170 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
171 for (bufp = buf, wresid = rcount; ;
172 bufp += wcount, wresid -= wcount) {
173 wcount = write(to_fd, bufp, wresid);
177 (void)fprintf(stderr,
179 entp->fts_path, to.p_path,
180 cp_pct(wtotal, fs->st_size));
183 if (wcount >= (ssize_t)wresid || wcount <= 0)
186 if (wcount != (ssize_t)wresid) {
187 warn("%s", to.p_path);
193 warn("%s", entp->fts_path);
199 * Don't remove the target even after an error. The target might
200 * not be a regular file, or its attributes might be important,
201 * or its contents might be irreplaceable. It would only be safe
202 * to remove it if we created it and its length is 0.
205 if (pflag && setfile(fs, to_fd))
207 (void)close(from_fd);
209 warn("%s", to.p_path);
216 copy_link(const FTSENT *p, int exists)
219 char llink[PATH_MAX];
221 if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) {
222 warn("readlink: %s", p->fts_path);
226 if (exists && unlink(to.p_path)) {
227 warn("unlink: %s", to.p_path);
230 if (symlink(llink, to.p_path)) {
231 warn("symlink: %s", llink);
234 return (pflag ? setfile(p->fts_statp, -1) : 0);
238 copy_fifo(struct stat *from_stat, int exists)
240 if (exists && unlink(to.p_path)) {
241 warn("unlink: %s", to.p_path);
244 if (mkfifo(to.p_path, from_stat->st_mode)) {
245 warn("mkfifo: %s", to.p_path);
248 return (pflag ? setfile(from_stat, -1) : 0);
252 copy_special(struct stat *from_stat, int exists)
254 if (exists && unlink(to.p_path)) {
255 warn("unlink: %s", to.p_path);
258 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
259 warn("mknod: %s", to.p_path);
262 return (pflag ? setfile(from_stat, -1) : 0);
266 setfile(struct stat *fs, int fd)
268 static struct timeval tv[2];
270 int rval, gotstat, islink, fdval;
274 islink = !fdval && S_ISLNK(fs->st_mode);
275 fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX |
276 S_IRWXU | S_IRWXG | S_IRWXO;
278 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
279 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
280 if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) {
281 warn("%sutimes: %s", islink ? "l" : "", to.p_path);
284 if (fdval ? fstat(fd, &ts) :
285 (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts)))
289 ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
290 S_IRWXU | S_IRWXG | S_IRWXO;
293 * Changing the ownership probably won't succeed, unless we're root
294 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
295 * the mode; current BSD behavior is to remove all setuid bits on
296 * chown. If chown fails, lose setuid/setgid bits.
298 if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
299 if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
300 (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
301 chown(to.p_path, fs->st_uid, fs->st_gid))) {
302 if (errno != EPERM) {
303 warn("chown: %s", to.p_path);
306 fs->st_mode &= ~(S_ISUID | S_ISGID);
309 if (!gotstat || fs->st_mode != ts.st_mode)
310 if (fdval ? fchmod(fd, fs->st_mode) :
311 (islink ? lchmod(to.p_path, fs->st_mode) :
312 chmod(to.p_path, fs->st_mode))) {
313 warn("chmod: %s", to.p_path);
317 if (!gotstat || fs->st_flags != ts.st_flags)
319 fchflags(fd, fs->st_flags) :
320 (islink ? (errno = ENOSYS) :
321 chflags(to.p_path, fs->st_flags))) {
322 warn("chflags: %s", to.p_path);
333 (void)fprintf(stderr, "%s\n%s\n",
334 "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-pv] src target",
335 " cp [-R [-H | -L | -P]] [-f | -i | -n] [-pv] src1 ... srcN directory");