utils.c revision 1.35 1 /* $NetBSD: utils.c,v 1.35 2010/10/22 17:56:06 pooka 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
36 #else
37 __RCSID("$NetBSD: utils.c,v 1.35 2010/10/22 17:56:06 pooka Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <sys/mman.h>
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/time.h>
45
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <fts.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #include "extern.h"
56
57 int
58 set_utimes(const char *file, struct stat *fs)
59 {
60 static struct timeval tv[2];
61
62 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
63 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
64
65 if (lutimes(file, tv)) {
66 warn("lutimes: %s", file);
67 return (1);
68 }
69 return (0);
70 }
71
72 int
73 copy_file(FTSENT *entp, int dne)
74 {
75 static char buf[MAXBSIZE];
76 struct stat to_stat, *fs;
77 int ch, checkch, from_fd, rcount, rval, to_fd, tolnk, wcount;
78
79 if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
80 warn("%s", entp->fts_path);
81 return (1);
82 }
83
84 to_fd = -1;
85 fs = entp->fts_statp;
86 tolnk = ((Rflag && !(Lflag || Hflag)) || Pflag);
87
88 /*
89 * If the file exists and we're interactive, verify with the user.
90 * If the file DNE, set the mode to be the from file, minus setuid
91 * bits, modified by the umask; arguably wrong, but it makes copying
92 * executables work right and it's been that way forever. (The
93 * other choice is 666 or'ed with the execute bits on the from file
94 * modified by the umask.)
95 */
96 if (!dne) {
97 struct stat sb;
98 int sval;
99
100 if (iflag) {
101 (void)fprintf(stderr, "overwrite %s? ", to.p_path);
102 checkch = ch = getchar();
103 while (ch != '\n' && ch != EOF)
104 ch = getchar();
105 if (checkch != 'y' && checkch != 'Y') {
106 (void)close(from_fd);
107 return (0);
108 }
109 }
110
111 sval = tolnk ?
112 lstat(to.p_path, &sb) : stat(to.p_path, &sb);
113 if (sval == -1) {
114 warn("stat: %s", to.p_path);
115 return (1);
116 }
117
118 if (!(tolnk && S_ISLNK(sb.st_mode)))
119 to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
120 } else
121 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
122 fs->st_mode & ~(S_ISUID | S_ISGID));
123
124 if (to_fd == -1 && (fflag || tolnk)) {
125 /*
126 * attempt to remove existing destination file name and
127 * create a new file
128 */
129 (void)unlink(to.p_path);
130 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
131 fs->st_mode & ~(S_ISUID | S_ISGID));
132 }
133
134 if (to_fd == -1) {
135 warn("%s", to.p_path);
136 (void)close(from_fd);
137 return (1);
138 }
139
140 rval = 0;
141
142 /*
143 * There's no reason to do anything other than close the file
144 * now if it's empty, so let's not bother.
145 */
146
147 if (fs->st_size > 0) {
148
149 /*
150 * Disable use of mmap. With the current vnode locking
151 * scheme it has a very annoying property: if the source
152 * media is slow (like a slow network), the target file
153 * will be locked for the duration of the entire max 8MB
154 * write and cause processes attempting to e.g. stat() it
155 * to "tstile". Revisit when vnode locking gets a little
156 * smarter.
157 */
158 #ifdef VNODE_LOCKING_IS_SMARTER_NOW
159 /*
160 * Mmap and write if less than 8M (the limit is so
161 * we don't totally trash memory on big files).
162 * This is really a minor hack, but it wins some CPU back.
163 */
164
165 if (fs->st_size <= 8 * 1048576) {
166 size_t fsize = (size_t)fs->st_size;
167 char *p;
168
169 p = mmap(NULL, fsize, PROT_READ, MAP_FILE|MAP_SHARED,
170 from_fd, (off_t)0);
171 if (p == MAP_FAILED) {
172 goto mmap_failed;
173 } else {
174 (void) madvise(p, (size_t)fs->st_size,
175 MADV_SEQUENTIAL);
176 if (write(to_fd, p, fsize) !=
177 fs->st_size) {
178 warn("%s", to.p_path);
179 rval = 1;
180 }
181 if (munmap(p, fsize) < 0) {
182 warn("%s", entp->fts_path);
183 rval = 1;
184 }
185 }
186 } else {
187 mmap_failed:
188 #endif
189 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
190 wcount = write(to_fd, buf, (size_t)rcount);
191 if (rcount != wcount || wcount == -1) {
192 warn("%s", to.p_path);
193 rval = 1;
194 break;
195 }
196 }
197 if (rcount < 0) {
198 warn("%s", entp->fts_path);
199 rval = 1;
200 }
201 #ifdef VNODE_LOCKING_IS_SMARTER_NOW
202 }
203 #endif
204 }
205
206 if (rval == 1) {
207 (void)close(from_fd);
208 (void)close(to_fd);
209 return (1);
210 }
211
212 if (pflag && setfile(fs, to_fd))
213 rval = 1;
214 /*
215 * If the source was setuid or setgid, lose the bits unless the
216 * copy is owned by the same user and group.
217 */
218 #define RETAINBITS \
219 (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
220 if (!pflag && dne
221 && fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) {
222 if (fstat(to_fd, &to_stat)) {
223 warn("%s", to.p_path);
224 rval = 1;
225 } else if (fs->st_gid == to_stat.st_gid &&
226 fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
227 warn("%s", to.p_path);
228 rval = 1;
229 }
230 }
231 (void)close(from_fd);
232 if (close(to_fd)) {
233 warn("%s", to.p_path);
234 rval = 1;
235 }
236 /* set the mod/access times now after close of the fd */
237 if (pflag && set_utimes(to.p_path, fs)) {
238 rval = 1;
239 }
240 return (rval);
241 }
242
243 int
244 copy_link(FTSENT *p, int exists)
245 {
246 int len;
247 char target[MAXPATHLEN];
248
249 if ((len = readlink(p->fts_path, target, sizeof(target)-1)) == -1) {
250 warn("readlink: %s", p->fts_path);
251 return (1);
252 }
253 target[len] = '\0';
254 if (exists && unlink(to.p_path)) {
255 warn("unlink: %s", to.p_path);
256 return (1);
257 }
258 if (symlink(target, to.p_path)) {
259 warn("symlink: %s", target);
260 return (1);
261 }
262 return (pflag ? setfile(p->fts_statp, 0) : 0);
263 }
264
265 int
266 copy_fifo(struct stat *from_stat, int exists)
267 {
268 if (exists && unlink(to.p_path)) {
269 warn("unlink: %s", to.p_path);
270 return (1);
271 }
272 if (mkfifo(to.p_path, from_stat->st_mode)) {
273 warn("mkfifo: %s", to.p_path);
274 return (1);
275 }
276 return (pflag ? setfile(from_stat, 0) : 0);
277 }
278
279 int
280 copy_special(struct stat *from_stat, int exists)
281 {
282 if (exists && unlink(to.p_path)) {
283 warn("unlink: %s", to.p_path);
284 return (1);
285 }
286 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
287 warn("mknod: %s", to.p_path);
288 return (1);
289 }
290 return (pflag ? setfile(from_stat, 0) : 0);
291 }
292
293
294 /*
295 * Function: setfile
296 *
297 * Purpose:
298 * Set the owner/group/permissions for the "to" file to the information
299 * in the stat structure. If fd is zero, also call set_utimes() to set
300 * the mod/access times. If fd is non-zero, the caller must do a utimes
301 * itself after close(fd).
302 */
303 int
304 setfile(struct stat *fs, int fd)
305 {
306 int rval, islink;
307
308 rval = 0;
309 islink = S_ISLNK(fs->st_mode);
310 fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
311
312 /*
313 * Changing the ownership probably won't succeed, unless we're root
314 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
315 * the mode; current BSD behavior is to remove all setuid bits on
316 * chown. If chown fails, lose setuid/setgid bits.
317 */
318 if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
319 lchown(to.p_path, fs->st_uid, fs->st_gid)) {
320 if (errno != EPERM) {
321 warn("chown: %s", to.p_path);
322 rval = 1;
323 }
324 fs->st_mode &= ~(S_ISUID | S_ISGID);
325 }
326 if (fd ? fchmod(fd, fs->st_mode) : lchmod(to.p_path, fs->st_mode)) {
327 warn("chmod: %s", to.p_path);
328 rval = 1;
329 }
330
331 if (!islink && !Nflag) {
332 unsigned long fflags = fs->st_flags;
333 /*
334 * XXX
335 * NFS doesn't support chflags; ignore errors unless
336 * there's reason to believe we're losing bits.
337 * (Note, this still won't be right if the server
338 * supports flags and we were trying to *remove* flags
339 * on a file that we copied, i.e., that we didn't create.)
340 */
341 errno = 0;
342 if ((fd ? fchflags(fd, fflags) :
343 chflags(to.p_path, fflags)) == -1)
344 if (errno != EOPNOTSUPP || fs->st_flags != 0) {
345 warn("chflags: %s", to.p_path);
346 rval = 1;
347 }
348 }
349 /* if fd is non-zero, caller must call set_utimes() after close() */
350 if (fd == 0 && set_utimes(to.p_path, fs))
351 rval = 1;
352 return (rval);
353 }
354
355 void
356 usage(void)
357 {
358 (void)fprintf(stderr,
359 "usage: %s [-R [-H | -L | -P]] [-f | -i] [-Npv] src target\n"
360 " %s [-R [-H | -L | -P]] [-f | -i] [-Npv] src1 ... srcN directory\n",
361 getprogname(), getprogname());
362 exit(1);
363 /* NOTREACHED */
364 }
365