1 | /* $NetBSD: utils.c,v 1.29 2005/10/15 18:22:18 christos 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 | #ifdef HAVE_CONFIG_H |
---|
33 | #include "config.h" |
---|
34 | #endif |
---|
35 | |
---|
36 | #include <sys/cdefs.h> |
---|
37 | #if 0 |
---|
38 | #ifndef lint |
---|
39 | #if 0 |
---|
40 | static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94"; |
---|
41 | #else |
---|
42 | __RCSID("$NetBSD: utils.c,v 1.29 2005/10/15 18:22:18 christos Exp $"); |
---|
43 | #endif |
---|
44 | #endif /* not lint */ |
---|
45 | #endif |
---|
46 | |
---|
47 | #if 0 |
---|
48 | #include <sys/mman.h> |
---|
49 | #endif |
---|
50 | #include <sys/param.h> |
---|
51 | #include <sys/stat.h> |
---|
52 | #include <sys/time.h> |
---|
53 | #include <sys/utime.h> |
---|
54 | |
---|
55 | #include <err.h> |
---|
56 | #include <errno.h> |
---|
57 | #include <fcntl.h> |
---|
58 | #include <fts.h> |
---|
59 | #include <limits.h> |
---|
60 | #include <stdio.h> |
---|
61 | #include <stdlib.h> |
---|
62 | #include <string.h> |
---|
63 | #include <unistd.h> |
---|
64 | |
---|
65 | #include "extern-cp.h" |
---|
66 | |
---|
67 | #define lchmod chmod |
---|
68 | #define lchown chown |
---|
69 | |
---|
70 | #define cp_pct(x, y) ((y == 0) ? 0 : (int)(100.0 * (x) / (y))) |
---|
71 | |
---|
72 | /* original was MAXBSIZE which results in 64K on the stack */ |
---|
73 | #define MAX_READ 1024 |
---|
74 | |
---|
75 | int |
---|
76 | set_utimes(const char *file, struct stat *fs) |
---|
77 | { |
---|
78 | struct utimbuf tv; |
---|
79 | |
---|
80 | tv.actime = fs->st_atime; |
---|
81 | tv.modtime = fs->st_mtime; |
---|
82 | |
---|
83 | if (utime(file, &tv)) { |
---|
84 | warn("lutimes: %s", file); |
---|
85 | return (1); |
---|
86 | } |
---|
87 | return (0); |
---|
88 | } |
---|
89 | |
---|
90 | int |
---|
91 | copy_file(rtems_shell_cp_globals* cp_globals __attribute__((unused)), FTSENT *entp, int dne) |
---|
92 | { |
---|
93 | static char buf[MAX_READ]; |
---|
94 | struct stat *fs; |
---|
95 | ssize_t wcount; |
---|
96 | size_t wresid; |
---|
97 | off_t wtotal; |
---|
98 | int ch, checkch, from_fd = 0, rcount, rval, to_fd = 0; |
---|
99 | char *bufp; |
---|
100 | #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED |
---|
101 | char *p; |
---|
102 | #endif |
---|
103 | |
---|
104 | if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) { |
---|
105 | warn("%s", entp->fts_path); |
---|
106 | return (1); |
---|
107 | } |
---|
108 | |
---|
109 | fs = entp->fts_statp; |
---|
110 | |
---|
111 | /* |
---|
112 | * If the file exists and we're interactive, verify with the user. |
---|
113 | * If the file DNE, set the mode to be the from file, minus setuid |
---|
114 | * bits, modified by the umask; arguably wrong, but it makes copying |
---|
115 | * executables work right and it's been that way forever. (The |
---|
116 | * other choice is 666 or'ed with the execute bits on the from file |
---|
117 | * modified by the umask.) |
---|
118 | */ |
---|
119 | if (!dne) { |
---|
120 | #define YESNO "(y/n [n]) " |
---|
121 | if (nflag) { |
---|
122 | if (vflag) |
---|
123 | printf("%s not overwritten\n", to.p_path); |
---|
124 | (void)close(from_fd); |
---|
125 | return (0); |
---|
126 | } else if (iflag) { |
---|
127 | (void)fprintf(stderr, "overwrite %s? %s", |
---|
128 | to.p_path, YESNO); |
---|
129 | checkch = ch = getchar(); |
---|
130 | while (ch != '\n' && ch != EOF) |
---|
131 | ch = getchar(); |
---|
132 | if (checkch != 'y' && checkch != 'Y') { |
---|
133 | (void)close(from_fd); |
---|
134 | (void)fprintf(stderr, "not overwritten\n"); |
---|
135 | return (1); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | if (fflag) { |
---|
140 | /* remove existing destination file name, |
---|
141 | * create a new file */ |
---|
142 | (void)unlink(to.p_path); |
---|
143 | if (!lflag) |
---|
144 | to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT, |
---|
145 | fs->st_mode & ~(S_ISUID | S_ISGID)); |
---|
146 | } else { |
---|
147 | if (!lflag) |
---|
148 | /* overwrite existing destination file name */ |
---|
149 | to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0); |
---|
150 | } |
---|
151 | } else { |
---|
152 | if (!lflag) |
---|
153 | to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT, |
---|
154 | fs->st_mode & ~(S_ISUID | S_ISGID)); |
---|
155 | } |
---|
156 | |
---|
157 | if (to_fd == -1) { |
---|
158 | warn("%s", to.p_path); |
---|
159 | (void)close(from_fd); |
---|
160 | return (1); |
---|
161 | } |
---|
162 | |
---|
163 | rval = 0; |
---|
164 | |
---|
165 | if (!lflag) { |
---|
166 | /* |
---|
167 | * Mmap and write if less than 8M (the limit is so we don't totally |
---|
168 | * trash memory on big files. This is really a minor hack, but it |
---|
169 | * wins some CPU back. |
---|
170 | */ |
---|
171 | #ifdef CCJ_REMOVED_VM_AND_BUFFER_CACHE_SYNCHRONIZED |
---|
172 | if (S_ISREG(fs->st_mode) && fs->st_size > 0 && |
---|
173 | fs->st_size <= 8 * 1048576) { |
---|
174 | if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ, |
---|
175 | MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) { |
---|
176 | warn("%s", entp->fts_path); |
---|
177 | rval = 1; |
---|
178 | } else { |
---|
179 | wtotal = 0; |
---|
180 | for (bufp = p, wresid = fs->st_size; ; |
---|
181 | bufp += wcount, wresid -= (size_t)wcount) { |
---|
182 | wcount = write(to_fd, bufp, wresid); |
---|
183 | if (wcount <= 0) |
---|
184 | break; |
---|
185 | wtotal += wcount; |
---|
186 | if (info) { |
---|
187 | info = 0; |
---|
188 | (void)fprintf(stderr, |
---|
189 | "%s -> %s %3d%%\n", |
---|
190 | entp->fts_path, to.p_path, |
---|
191 | cp_pct(wtotal, fs->st_size)); |
---|
192 | } |
---|
193 | if (wcount >= (ssize_t)wresid) |
---|
194 | break; |
---|
195 | } |
---|
196 | if (wcount != (ssize_t)wresid) { |
---|
197 | warn("%s", to.p_path); |
---|
198 | rval = 1; |
---|
199 | } |
---|
200 | /* Some systems don't unmap on close(2). */ |
---|
201 | if (munmap(p, fs->st_size) < 0) { |
---|
202 | warn("%s", entp->fts_path); |
---|
203 | rval = 1; |
---|
204 | } |
---|
205 | } |
---|
206 | } else |
---|
207 | #endif |
---|
208 | { |
---|
209 | wtotal = 0; |
---|
210 | while ((rcount = read(from_fd, buf, MAX_READ)) > 0) { |
---|
211 | for (bufp = buf, wresid = rcount; ; |
---|
212 | bufp += wcount, wresid -= wcount) { |
---|
213 | wcount = write(to_fd, bufp, wresid); |
---|
214 | if (wcount <= 0) |
---|
215 | break; |
---|
216 | wtotal += wcount; |
---|
217 | if (info) { |
---|
218 | info = 0; |
---|
219 | (void)fprintf(stderr, |
---|
220 | "%s -> %s %3d%%\n", |
---|
221 | entp->fts_path, to.p_path, |
---|
222 | cp_pct(wtotal, fs->st_size)); |
---|
223 | } |
---|
224 | if (wcount >= (ssize_t)wresid) |
---|
225 | break; |
---|
226 | } |
---|
227 | if (wcount != (ssize_t)wresid) { |
---|
228 | warn("%s", to.p_path); |
---|
229 | rval = 1; |
---|
230 | break; |
---|
231 | } |
---|
232 | } |
---|
233 | if (rcount < 0) { |
---|
234 | warn("%s", entp->fts_path); |
---|
235 | rval = 1; |
---|
236 | } |
---|
237 | } |
---|
238 | } else { |
---|
239 | if (link(entp->fts_path, to.p_path)) { |
---|
240 | warn("%s", to.p_path); |
---|
241 | rval = 1; |
---|
242 | } |
---|
243 | } |
---|
244 | (void)close(from_fd); |
---|
245 | |
---|
246 | /* |
---|
247 | * Don't remove the target even after an error. The target might |
---|
248 | * not be a regular file, or its attributes might be important, |
---|
249 | * or its contents might be irreplaceable. It would only be safe |
---|
250 | * to remove it if we created it and its length is 0. |
---|
251 | */ |
---|
252 | |
---|
253 | if (!lflag) { |
---|
254 | if (pflag && setfile(cp_globals, fs, to_fd)) |
---|
255 | rval = 1; |
---|
256 | if (pflag && preserve_fd_acls(from_fd, to_fd) != 0) |
---|
257 | rval = 1; |
---|
258 | (void)close(from_fd); |
---|
259 | if (close(to_fd)) { |
---|
260 | warn("%s", to.p_path); |
---|
261 | rval = 1; |
---|
262 | } |
---|
263 | } |
---|
264 | return (rval); |
---|
265 | } |
---|
266 | |
---|
267 | int |
---|
268 | copy_link(rtems_shell_cp_globals* cp_globals, FTSENT *p, int exists) |
---|
269 | { |
---|
270 | ssize_t len; |
---|
271 | char llink[PATH_MAX]; |
---|
272 | |
---|
273 | if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) { |
---|
274 | warn("readlink: %s", p->fts_path); |
---|
275 | return (1); |
---|
276 | } |
---|
277 | llink[len] = '\0'; |
---|
278 | if (exists && unlink(to.p_path)) { |
---|
279 | warn("unlink: %s", to.p_path); |
---|
280 | return (1); |
---|
281 | } |
---|
282 | if (symlink(llink, to.p_path)) { |
---|
283 | warn("symlink: %s", llink); |
---|
284 | return (1); |
---|
285 | } |
---|
286 | return (pflag ? setfile(cp_globals, p->fts_statp, -1) : 0); |
---|
287 | } |
---|
288 | |
---|
289 | int |
---|
290 | copy_fifo(rtems_shell_cp_globals* cp_globals, struct stat *from_stat, int exists) |
---|
291 | { |
---|
292 | if (exists && unlink(to.p_path)) { |
---|
293 | warn("unlink: %s", to.p_path); |
---|
294 | return (1); |
---|
295 | } |
---|
296 | if (mkfifo(to.p_path, from_stat->st_mode)) { |
---|
297 | warn("mkfifo: %s", to.p_path); |
---|
298 | return (1); |
---|
299 | } |
---|
300 | return (pflag ? setfile(cp_globals, from_stat, -1) : 0); |
---|
301 | } |
---|
302 | |
---|
303 | int |
---|
304 | copy_special(rtems_shell_cp_globals* cp_globals, struct stat *from_stat, int exists) |
---|
305 | { |
---|
306 | if (exists && unlink(to.p_path)) { |
---|
307 | warn("unlink: %s", to.p_path); |
---|
308 | return (1); |
---|
309 | } |
---|
310 | if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) { |
---|
311 | warn("mknod: %s", to.p_path); |
---|
312 | return (1); |
---|
313 | } |
---|
314 | return (pflag ? setfile(cp_globals, from_stat, -1) : 0); |
---|
315 | } |
---|
316 | |
---|
317 | #define TIMESPEC_TO_TIMEVAL(tv, ts) { \ |
---|
318 | (tv)->tv_sec = *(ts); \ |
---|
319 | (tv)->tv_usec = 0; \ |
---|
320 | } |
---|
321 | |
---|
322 | #define st_atimespec st_atime |
---|
323 | #define st_mtimespec st_mtime |
---|
324 | #define lutimes utimes |
---|
325 | |
---|
326 | int |
---|
327 | setfile(rtems_shell_cp_globals* cp_globals, struct stat *fs, int fd) |
---|
328 | { |
---|
329 | static struct timeval tv[2]; |
---|
330 | struct stat ts; |
---|
331 | int rval, gotstat, islink, fdval; |
---|
332 | |
---|
333 | rval = 0; |
---|
334 | fdval = fd != -1; |
---|
335 | islink = !fdval && S_ISLNK(fs->st_mode); |
---|
336 | fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX | |
---|
337 | S_IRWXU | S_IRWXG | S_IRWXO; |
---|
338 | |
---|
339 | TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec); |
---|
340 | TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec); |
---|
341 | #if 0 |
---|
342 | if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) { |
---|
343 | warn("%sutimes: %s", islink ? "l" : "", to.p_path); |
---|
344 | rval = 1; |
---|
345 | } |
---|
346 | #endif |
---|
347 | if (fdval ? fstat(fd, &ts) : |
---|
348 | (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts))) |
---|
349 | gotstat = 0; |
---|
350 | else { |
---|
351 | gotstat = 1; |
---|
352 | ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX | |
---|
353 | S_IRWXU | S_IRWXG | S_IRWXO; |
---|
354 | } |
---|
355 | /* |
---|
356 | * Changing the ownership probably won't succeed, unless we're root |
---|
357 | * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting |
---|
358 | * the mode; current BSD behavior is to remove all setuid bits on |
---|
359 | * chown. If chown fails, lose setuid/setgid bits. |
---|
360 | */ |
---|
361 | if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid) |
---|
362 | if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) : |
---|
363 | (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) : |
---|
364 | chown(to.p_path, fs->st_uid, fs->st_gid))) { |
---|
365 | if (errno != EPERM) { |
---|
366 | warn("chown: %s", to.p_path); |
---|
367 | rval = 1; |
---|
368 | } |
---|
369 | fs->st_mode &= ~(S_ISUID | S_ISGID); |
---|
370 | } |
---|
371 | |
---|
372 | if (!gotstat || fs->st_mode != ts.st_mode) |
---|
373 | if (fdval ? fchmod(fd, fs->st_mode) : |
---|
374 | (islink ? lchmod(to.p_path, fs->st_mode) : |
---|
375 | chmod(to.p_path, fs->st_mode))) { |
---|
376 | warn("chmod: %s", to.p_path); |
---|
377 | rval = 1; |
---|
378 | } |
---|
379 | |
---|
380 | #if 0 |
---|
381 | if (!gotstat || fs->st_flags != ts.st_flags) |
---|
382 | if (fdval ? |
---|
383 | fchflags(fd, fs->st_flags) : |
---|
384 | (islink ? (errno = ENOSYS) : |
---|
385 | chflags(to.p_path, fs->st_flags))) { |
---|
386 | warn("chflags: %s", to.p_path); |
---|
387 | rval = 1; |
---|
388 | } |
---|
389 | #endif |
---|
390 | |
---|
391 | return (rval); |
---|
392 | } |
---|
393 | |
---|
394 | int |
---|
395 | preserve_fd_acls(int source_fd __attribute__((unused)), int dest_fd __attribute__((unused))) |
---|
396 | { |
---|
397 | #if 0 |
---|
398 | struct acl *aclp; |
---|
399 | acl_t acl; |
---|
400 | |
---|
401 | if (fpathconf(source_fd, _PC_ACL_EXTENDED) != 1 || |
---|
402 | fpathconf(dest_fd, _PC_ACL_EXTENDED) != 1) |
---|
403 | return (0); |
---|
404 | acl = acl_get_fd(source_fd); |
---|
405 | if (acl == NULL) { |
---|
406 | warn("failed to get acl entries while setting %s", to.p_path); |
---|
407 | return (1); |
---|
408 | } |
---|
409 | aclp = &acl->ats_acl; |
---|
410 | if (aclp->acl_cnt == 3) |
---|
411 | return (0); |
---|
412 | if (acl_set_fd(dest_fd, acl) < 0) { |
---|
413 | warn("failed to set acl entries for %s", to.p_path); |
---|
414 | return (1); |
---|
415 | } |
---|
416 | #endif |
---|
417 | return (0); |
---|
418 | } |
---|
419 | |
---|
420 | int |
---|
421 | preserve_dir_acls(struct stat *fs __attribute__((unused)), char *source_dir __attribute__((unused)), char *dest_dir __attribute__((unused))) |
---|
422 | { |
---|
423 | #if 0 |
---|
424 | acl_t (*aclgetf)(const char *, acl_type_t); |
---|
425 | int (*aclsetf)(const char *, acl_type_t, acl_t); |
---|
426 | struct acl *aclp; |
---|
427 | acl_t acl; |
---|
428 | |
---|
429 | if (pathconf(source_dir, _PC_ACL_EXTENDED) != 1 || |
---|
430 | pathconf(dest_dir, _PC_ACL_EXTENDED) != 1) |
---|
431 | return (0); |
---|
432 | /* |
---|
433 | * If the file is a link we will not follow it |
---|
434 | */ |
---|
435 | if (S_ISLNK(fs->st_mode)) { |
---|
436 | aclgetf = acl_get_link_np; |
---|
437 | aclsetf = acl_set_link_np; |
---|
438 | } else { |
---|
439 | aclgetf = acl_get_file; |
---|
440 | aclsetf = acl_set_file; |
---|
441 | } |
---|
442 | /* |
---|
443 | * Even if there is no ACL_TYPE_DEFAULT entry here, a zero |
---|
444 | * size ACL will be returned. So it is not safe to simply |
---|
445 | * check the pointer to see if the default ACL is present. |
---|
446 | */ |
---|
447 | acl = aclgetf(source_dir, ACL_TYPE_DEFAULT); |
---|
448 | if (acl == NULL) { |
---|
449 | warn("failed to get default acl entries on %s", |
---|
450 | source_dir); |
---|
451 | return (1); |
---|
452 | } |
---|
453 | aclp = &acl->ats_acl; |
---|
454 | if (aclp->acl_cnt != 0 && aclsetf(dest_dir, |
---|
455 | ACL_TYPE_DEFAULT, acl) < 0) { |
---|
456 | warn("failed to set default acl entries on %s", |
---|
457 | dest_dir); |
---|
458 | return (1); |
---|
459 | } |
---|
460 | acl = aclgetf(source_dir, ACL_TYPE_ACCESS); |
---|
461 | if (acl == NULL) { |
---|
462 | warn("failed to get acl entries on %s", source_dir); |
---|
463 | return (1); |
---|
464 | } |
---|
465 | aclp = &acl->ats_acl; |
---|
466 | if (aclsetf(dest_dir, ACL_TYPE_ACCESS, acl) < 0) { |
---|
467 | warn("failed to set acl entries on %s", dest_dir); |
---|
468 | return (1); |
---|
469 | } |
---|
470 | #endif |
---|
471 | return (0); |
---|
472 | } |
---|
473 | |
---|
474 | void |
---|
475 | usage(rtems_shell_cp_globals* cp_globals) |
---|
476 | { |
---|
477 | (void)fprintf(stderr, "%s\n%s\n", |
---|
478 | "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpv] source_file target_file", |
---|
479 | " cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpv] source_file ... " |
---|
480 | "target_directory"); |
---|
481 | longjmp (cp_globals->exit_jmp, 1); |
---|
482 | } |
---|