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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid
[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
39 #include <sys/cdefs.h>
40 __RCSID("$FreeBSD: src/bin/cp/utils.c,v 1.38 2002/07/31 16:52:16 markm Exp $");
42 #include <sys/param.h>
44 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
61 copy_file(FTSENT
*entp
, int dne
)
63 static char buf
[MAXBSIZE
];
65 int ch
, checkch
, from_fd
, rcount
, rval
, to_fd
;
69 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
73 if ((from_fd
= open(entp
->fts_path
, O_RDONLY
, 0)) == -1) {
74 warn("%s", entp
->fts_path
);
81 * If the file exists and we're interactive, verify with the user.
82 * If the file DNE, set the mode to be the from file, minus setuid
83 * bits, modified by the umask; arguably wrong, but it makes copying
84 * executables work right and it's been that way forever. (The
85 * other choice is 666 or'ed with the execute bits on the from file
86 * modified by the umask.)
89 #define YESNO "(y/n [n]) "
92 printf("%s not overwritten\n", to
.p_path
);
96 (void)fprintf(stderr
, "overwrite %s? %s",
98 checkch
= ch
= getchar();
99 while (ch
!= '\n' && ch
!= EOF
)
101 if (checkch
!= 'y' && checkch
!= 'Y') {
102 (void)close(from_fd
);
103 (void)fprintf(stderr
, "not overwritten\n");
109 /* remove existing destination file name,
110 * create a new file */
111 (void)unlink(to
.p_path
);
112 to_fd
= open(to
.p_path
, O_WRONLY
| O_TRUNC
| O_CREAT
,
113 fs
->st_mode
& ~(S_ISUID
| S_ISGID
));
115 /* overwrite existing destination file name */
116 to_fd
= open(to
.p_path
, O_WRONLY
| O_TRUNC
, 0);
118 to_fd
= open(to
.p_path
, O_WRONLY
| O_TRUNC
| O_CREAT
,
119 fs
->st_mode
& ~(S_ISUID
| S_ISGID
));
122 warn("%s", to
.p_path
);
123 (void)close(from_fd
);
130 * Mmap and write if less than 8M (the limit is so we don't totally
131 * trash memory on big files. This is really a minor hack, but it
132 * wins some CPU back.
134 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
135 if (S_ISREG(fs
->st_mode
) && fs
->st_size
<= 8 * 1048576) {
136 if ((p
= mmap(NULL
, (size_t)fs
->st_size
, PROT_READ
,
137 MAP_SHARED
, from_fd
, (off_t
)0)) == MAP_FAILED
) {
138 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
);
144 if (wcount
>= (ssize_t
)wresid
|| wcount
<= 0)
147 if (wcount
!= (ssize_t
)wresid
) {
148 warn("%s", to
.p_path
);
151 /* Some systems don't unmap on close(2). */
152 if (munmap(p
, fs
->st_size
) < 0) {
153 warn("%s", entp
->fts_path
);
160 while ((rcount
= read(from_fd
, buf
, MAXBSIZE
)) > 0) {
161 for (bufp
= buf
, wresid
= rcount
; ;
162 bufp
+= wcount
, wresid
-= wcount
) {
163 wcount
= write(to_fd
, bufp
, wresid
);
164 if (wcount
>= (ssize_t
)wresid
|| wcount
<= 0)
167 if (wcount
!= (ssize_t
)wresid
) {
168 warn("%s", to
.p_path
);
174 warn("%s", entp
->fts_path
);
180 * Don't remove the target even after an error. The target might
181 * not be a regular file, or its attributes might be important,
182 * or its contents might be irreplaceable. It would only be safe
183 * to remove it if we created it and its length is 0.
186 if (pflag
&& setfile(fs
, to_fd
))
188 (void)close(from_fd
);
190 warn("%s", to
.p_path
);
197 copy_link(FTSENT
*p
, int exists
)
200 char llink
[PATH_MAX
];
202 if ((len
= readlink(p
->fts_path
, llink
, sizeof(llink
) - 1)) == -1) {
203 warn("readlink: %s", p
->fts_path
);
207 if (exists
&& unlink(to
.p_path
)) {
208 warn("unlink: %s", to
.p_path
);
211 if (symlink(llink
, to
.p_path
)) {
212 warn("symlink: %s", llink
);
219 copy_fifo(struct stat
*from_stat
, int exists
)
221 if (exists
&& unlink(to
.p_path
)) {
222 warn("unlink: %s", to
.p_path
);
225 if (mkfifo(to
.p_path
, from_stat
->st_mode
)) {
226 warn("mkfifo: %s", to
.p_path
);
229 return (pflag ?
setfile(from_stat
, 0) : 0);
233 copy_special(struct stat
*from_stat
, int exists
)
235 if (exists
&& unlink(to
.p_path
)) {
236 warn("unlink: %s", to
.p_path
);
239 if (mknod(to
.p_path
, from_stat
->st_mode
, from_stat
->st_rdev
)) {
240 warn("mknod: %s", to
.p_path
);
243 return (pflag ?
setfile(from_stat
, 0) : 0);
247 setfile(struct stat
*fs
, int fd
)
249 static struct timeval tv
[2];
255 fs
->st_mode
&= S_ISUID
| S_ISGID
| S_ISVTX
|
256 S_IRWXU
| S_IRWXG
| S_IRWXO
;
258 TIMESPEC_TO_TIMEVAL(&tv
[0], &fs
->st_atimespec
);
259 TIMESPEC_TO_TIMEVAL(&tv
[1], &fs
->st_mtimespec
);
260 if (utimes(to
.p_path
, tv
)) {
261 warn("utimes: %s", to
.p_path
);
264 if (fd ?
fstat(fd
, &ts
) : stat(to
.p_path
, &ts
))
268 ts
.st_mode
&= S_ISUID
| S_ISGID
| S_ISVTX
|
269 S_IRWXU
| S_IRWXG
| S_IRWXO
;
272 * Changing the ownership probably won't succeed, unless we're root
273 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
274 * the mode; current BSD behavior is to remove all setuid bits on
275 * chown. If chown fails, lose setuid/setgid bits.
277 if (!gotstat
|| fs
->st_uid
!= ts
.st_uid
|| fs
->st_gid
!= ts
.st_gid
)
278 if (fd ?
fchown(fd
, fs
->st_uid
, fs
->st_gid
) :
279 chown(to
.p_path
, fs
->st_uid
, fs
->st_gid
)) {
280 if (errno
!= EPERM
) {
281 warn("chown: %s", to
.p_path
);
284 fs
->st_mode
&= ~(S_ISUID
| S_ISGID
);
287 if (!gotstat
|| fs
->st_mode
!= ts
.st_mode
)
288 if (fd ?
fchmod(fd
, fs
->st_mode
) : chmod(to
.p_path
, fs
->st_mode
)) {
289 warn("chmod: %s", to
.p_path
);
293 if (!gotstat
|| fs
->st_flags
!= ts
.st_flags
)
295 fchflags(fd
, fs
->st_flags
) : chflags(to
.p_path
, fs
->st_flags
)) {
296 warn("chflags: %s", to
.p_path
);
307 (void)fprintf(stderr
, "%s\n%s\n",
308 "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-pv] src target",
309 " cp [-R [-H | -L | -P]] [-f | -i | -n] [-pv] src1 ... srcN directory");