utils.c revision 1.15 1 /* $NetBSD: utils.c,v 1.15 1998/08/19 01:29:11 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
40 #else
41 __RCSID("$NetBSD: utils.c,v 1.15 1998/08/19 01:29:11 thorpej Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 #include <sys/mman.h>
48 #include <sys/time.h>
49
50 #include <err.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <fts.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 #include "extern.h"
60
61 int
62 copy_file(entp, dne)
63 FTSENT *entp;
64 int dne;
65 {
66 static char buf[MAXBSIZE];
67 struct stat to_stat, *fs;
68 int ch, checkch, from_fd, rcount, rval, to_fd, wcount;
69 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
70 char *p;
71 #endif
72
73 if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
74 warn("%s", entp->fts_path);
75 return (1);
76 }
77
78 fs = entp->fts_statp;
79
80 /*
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.)
87 */
88 if (!dne) {
89 if (iflag) {
90 (void)fprintf(stderr, "overwrite %s? ", to.p_path);
91 checkch = ch = getchar();
92 while (ch != '\n' && ch != EOF)
93 ch = getchar();
94 if (checkch != 'y' && checkch != 'Y') {
95 (void)close(from_fd);
96 return (0);
97 }
98 }
99 to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
100 } else
101 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
102 fs->st_mode & ~(S_ISUID | S_ISGID));
103
104 if (to_fd == -1) {
105 warn("%s", to.p_path);
106 (void)close(from_fd);
107 return (1);;
108 }
109
110 rval = 0;
111
112 /*
113 * Mmap and write if less than 8M (the limit is so we don't totally
114 * trash memory on big files. This is really a minor hack, but it
115 * wins some CPU back.
116 */
117 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
118 if (fs->st_size <= 8 * 1048576) {
119 if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
120 MAP_FILE|MAP_SHARED, from_fd, (off_t)0)) == (char *)-1) {
121 warn("%s", entp->fts_path);
122 rval = 1;
123 } else {
124 if (write(to_fd, p, fs->st_size) != fs->st_size) {
125 warn("%s", to.p_path);
126 rval = 1;
127 }
128 /* Some systems don't unmap on close(2). */
129 if (munmap(p, fs->st_size) < 0) {
130 warn("%s", entp->fts_path);
131 rval = 1;
132 }
133 }
134 } else
135 #endif
136 {
137 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
138 wcount = write(to_fd, buf, rcount);
139 if (rcount != wcount || wcount == -1) {
140 warn("%s", to.p_path);
141 rval = 1;
142 break;
143 }
144 }
145 if (rcount < 0) {
146 warn("%s", entp->fts_path);
147 rval = 1;
148 }
149 }
150
151 if (rval == 1) {
152 (void)close(from_fd);
153 (void)close(to_fd);
154 return (1);
155 }
156
157 if (pflag && setfile(fs, to_fd))
158 rval = 1;
159 /*
160 * If the source was setuid or setgid, lose the bits unless the
161 * copy is owned by the same user and group.
162 */
163 #define RETAINBITS \
164 (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
165 else if (fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) {
166 if (fstat(to_fd, &to_stat)) {
167 warn("%s", to.p_path);
168 rval = 1;
169 } else if (fs->st_gid == to_stat.st_gid &&
170 fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
171 warn("%s", to.p_path);
172 rval = 1;
173 }
174 }
175 (void)close(from_fd);
176 if (close(to_fd)) {
177 warn("%s", to.p_path);
178 rval = 1;
179 }
180 return (rval);
181 }
182
183 int
184 copy_link(p, exists)
185 FTSENT *p;
186 int exists;
187 {
188 int len;
189 char target[MAXPATHLEN];
190
191 if ((len = readlink(p->fts_path, target, sizeof(target))) == -1) {
192 warn("readlink: %s", p->fts_path);
193 return (1);
194 }
195 target[len] = '\0';
196 if (exists && unlink(to.p_path)) {
197 warn("unlink: %s", to.p_path);
198 return (1);
199 }
200 if (symlink(target, to.p_path)) {
201 warn("symlink: %s", target);
202 return (1);
203 }
204 return (pflag ? setfile(p->fts_statp, 0) : 0);
205 }
206
207 int
208 copy_fifo(from_stat, exists)
209 struct stat *from_stat;
210 int exists;
211 {
212 if (exists && unlink(to.p_path)) {
213 warn("unlink: %s", to.p_path);
214 return (1);
215 }
216 if (mkfifo(to.p_path, from_stat->st_mode)) {
217 warn("mkfifo: %s", to.p_path);
218 return (1);
219 }
220 return (pflag ? setfile(from_stat, 0) : 0);
221 }
222
223 int
224 copy_special(from_stat, exists)
225 struct stat *from_stat;
226 int exists;
227 {
228 if (exists && unlink(to.p_path)) {
229 warn("unlink: %s", to.p_path);
230 return (1);
231 }
232 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
233 warn("mknod: %s", to.p_path);
234 return (1);
235 }
236 return (pflag ? setfile(from_stat, 0) : 0);
237 }
238
239
240 int
241 setfile(fs, fd)
242 struct stat *fs;
243 int fd;
244 {
245 static struct timeval tv[2];
246 int rval, islink;
247
248 rval = 0;
249 islink = S_ISLNK(fs->st_mode);
250 fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
251
252 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
253 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
254 if (fd ? futimes(fd, tv) : lutimes(to.p_path, tv)) {
255 warn("utimes: %s", to.p_path);
256 rval = 1;
257 }
258 /*
259 * Changing the ownership probably won't succeed, unless we're root
260 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
261 * the mode; current BSD behavior is to remove all setuid bits on
262 * chown. If chown fails, lose setuid/setgid bits.
263 */
264 if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
265 lchown(to.p_path, fs->st_uid, fs->st_gid)) {
266 if (errno != EPERM) {
267 warn("chown: %s", to.p_path);
268 rval = 1;
269 }
270 fs->st_mode &= ~(S_ISUID | S_ISGID);
271 }
272 if (fd ? fchmod(fd, fs->st_mode) : lchmod(to.p_path, fs->st_mode)) {
273 warn("chmod: %s", to.p_path);
274 rval = 1;
275 }
276
277 if (!islink) {
278 /*
279 * XXX
280 * NFS doesn't support chflags; ignore errors unless
281 * there's reason to believe we're losing bits.
282 * (Note, this still won't be right if the server
283 * supports flags and we were trying to *remove* flags
284 * on a file that we copied, i.e., that we didn't create.)
285 */
286 errno = 0;
287 if (fd ? fchflags(fd, fs->st_flags) :
288 chflags(to.p_path, fs->st_flags))
289 if (errno != EOPNOTSUPP || fs->st_flags != 0) {
290 warn("chflags: %s", to.p_path);
291 rval = 1;
292 }
293 }
294 return (rval);
295 }
296
297 void
298 usage()
299 {
300 (void)fprintf(stderr, "%s\n%s\n",
301 "usage: cp [-R [-H | -L | -P]] [-fip] src target",
302 " cp [-R [-H | -L | -P]] [-fip] src1 ... srcN directory");
303 exit(1);
304 /* NOTREACHED */
305 }
306