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 * 3. 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
30 #include <sys/param.h>
33 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
49 #define cp_pct(x, y) ((y == 0) ? 0 : (int)(100.0 * (x) / (y)))
53 copy_file(const FTSENT
*entp
, int dne
)
55 static char buf
[MAXBSIZE
];
57 ssize_t wcount
, rcount
;
60 int ch
, checkch
, from_fd
= 0, rval
, to_fd
= 0;
62 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
66 if ((from_fd
= open(entp
->fts_path
, O_RDONLY
, 0)) == -1) {
67 warn("%s", entp
->fts_path
);
74 * If the file exists and we're interactive, verify with the user.
75 * If the file DNE, set the mode to be the from file, minus setuid
76 * bits, modified by the umask; arguably wrong, but it makes copying
77 * executables work right and it's been that way forever. (The
78 * other choice is 666 or'ed with the execute bits on the from file
79 * modified by the umask.)
82 #define YESNO "(y/n [n]) "
85 printf("%s not overwritten\n", to
.p_path
);
89 fprintf(stderr
, "overwrite %s? %s",
91 checkch
= ch
= getchar();
92 while (ch
!= '\n' && ch
!= EOF
)
94 if (checkch
!= 'y' && checkch
!= 'Y') {
96 fprintf(stderr
, "not overwritten\n");
102 /* remove existing destination file name,
103 * create a new file */
106 to_fd
= open(to
.p_path
, O_WRONLY
| O_TRUNC
| O_CREAT
,
107 fs
->st_mode
& ~(S_ISUID
| S_ISGID
));
110 /* overwrite existing destination file name */
111 to_fd
= open(to
.p_path
, O_WRONLY
| O_TRUNC
, 0);
115 to_fd
= open(to
.p_path
, O_WRONLY
| O_TRUNC
| O_CREAT
,
116 fs
->st_mode
& ~(S_ISUID
| S_ISGID
));
120 warn("%s", to
.p_path
);
129 * Mmap and write if less than 8M (the limit is so we don't totally
130 * trash memory on big files. This is really a minor hack, but it
131 * wins some CPU back.
132 * Some filesystems, such as smbnetfs, don't support mmap,
133 * so this is a best-effort attempt.
135 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
136 if (S_ISREG(fs
->st_mode
) && fs
->st_size
> 0 &&
137 fs
->st_size
<= 8 * 1024 * 1024 &&
138 (p
= mmap(NULL
, (size_t)fs
->st_size
, PROT_READ
,
139 MAP_SHARED
, from_fd
, (off_t
)0)) != MAP_FAILED
) {
141 for (bufp
= p
, wresid
= fs
->st_size
; ;
142 bufp
+= wcount
, wresid
-= (size_t)wcount
) {
143 wcount
= write(to_fd
, bufp
, wresid
);
151 entp
->fts_path
, to
.p_path
,
152 cp_pct(wtotal
, fs
->st_size
));
154 if (wcount
>= (ssize_t
)wresid
)
157 if (wcount
!= (ssize_t
)wresid
) {
158 warn("%s", to
.p_path
);
161 /* Some systems don't unmap on close(2). */
162 if (munmap(p
, fs
->st_size
) < 0) {
163 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
);
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
);
194 warn("%s", entp
->fts_path
);
199 if (link(entp
->fts_path
, to
.p_path
)) {
200 warn("%s", to
.p_path
);
206 * Don't remove the target even after an error. The target might
207 * not be a regular file, or its attributes might be important,
208 * or its contents might be irreplaceable. It would only be safe
209 * to remove it if we created it and its length is 0.
213 if (pflag
&& setfile(fs
, to_fd
))
216 warn("%s", to
.p_path
);
227 copy_link(const FTSENT
*p
, int exists
)
230 char llink
[PATH_MAX
];
232 if ((len
= readlink(p
->fts_path
, llink
, sizeof(llink
) - 1)) == -1) {
233 warn("readlink: %s", p
->fts_path
);
237 if (exists
&& unlink(to
.p_path
)) {
238 warn("unlink: %s", to
.p_path
);
241 if (symlink(llink
, to
.p_path
)) {
242 warn("symlink: %s", llink
);
245 return (pflag
? setfile(p
->fts_statp
, -1) : 0);
249 copy_fifo(struct stat
*from_stat
, int exists
)
251 if (exists
&& unlink(to
.p_path
)) {
252 warn("unlink: %s", to
.p_path
);
255 if (mkfifo(to
.p_path
, from_stat
->st_mode
)) {
256 warn("mkfifo: %s", to
.p_path
);
259 return (pflag
? setfile(from_stat
, -1) : 0);
263 copy_special(struct stat
*from_stat
, int exists
)
265 if (exists
&& unlink(to
.p_path
)) {
266 warn("unlink: %s", to
.p_path
);
269 if (mknod(to
.p_path
, from_stat
->st_mode
, from_stat
->st_rdev
)) {
270 warn("mknod: %s", to
.p_path
);
273 return (pflag
? setfile(from_stat
, -1) : 0);
277 setfile(struct stat
*fs
, int fd
)
279 static struct timeval tv
[2];
281 int rval
, gotstat
, islink
, fdval
;
285 islink
= !fdval
&& S_ISLNK(fs
->st_mode
);
286 fs
->st_mode
&= S_ISUID
| S_ISGID
| S_ISVTX
|
287 S_IRWXU
| S_IRWXG
| S_IRWXO
;
289 TIMESPEC_TO_TIMEVAL(&tv
[0], &fs
->st_atimespec
);
290 TIMESPEC_TO_TIMEVAL(&tv
[1], &fs
->st_mtimespec
);
291 if (islink
? lutimes(to
.p_path
, tv
) : utimes(to
.p_path
, tv
)) {
292 warn("%sutimes: %s", islink
? "l" : "", to
.p_path
);
295 if (fdval
? fstat(fd
, &ts
) :
296 (islink
? lstat(to
.p_path
, &ts
) : stat(to
.p_path
, &ts
)))
300 ts
.st_mode
&= S_ISUID
| S_ISGID
| S_ISVTX
|
301 S_IRWXU
| S_IRWXG
| S_IRWXO
;
304 * Changing the ownership probably won't succeed, unless we're root
305 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
306 * the mode; current BSD behavior is to remove all setuid bits on
307 * chown. If chown fails, lose setuid/setgid bits.
309 if (!gotstat
|| fs
->st_uid
!= ts
.st_uid
|| fs
->st_gid
!= ts
.st_gid
)
310 if (fdval
? fchown(fd
, fs
->st_uid
, fs
->st_gid
) :
311 (islink
? lchown(to
.p_path
, fs
->st_uid
, fs
->st_gid
) :
312 chown(to
.p_path
, fs
->st_uid
, fs
->st_gid
))) {
313 if (errno
!= EPERM
) {
314 warn("chown: %s", to
.p_path
);
317 fs
->st_mode
&= ~(S_ISUID
| S_ISGID
);
320 if (!gotstat
|| fs
->st_mode
!= ts
.st_mode
)
321 if (fdval
? fchmod(fd
, fs
->st_mode
) :
322 (islink
? lchmod(to
.p_path
, fs
->st_mode
) :
323 chmod(to
.p_path
, fs
->st_mode
))) {
324 warn("chmod: %s", to
.p_path
);
328 if (!gotstat
|| fs
->st_flags
!= ts
.st_flags
)
330 fchflags(fd
, fs
->st_flags
) :
331 (islink
? lchflags(to
.p_path
, fs
->st_flags
) :
332 chflags(to
.p_path
, fs
->st_flags
))) {
333 warn("chflags: %s", to
.p_path
);
344 fprintf(stderr
, "%s\n%s\n",
345 "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpvx] source_file target_file",
346 " cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpvx] source_file ... "