VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/cp_utils.c@ 1442

Last change on this file since 1442 was 1356, checked in by bird, 17 years ago

Fixed busted --changed implementation. It leaked a handle if the files matched and didn't rewind on mismatch.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 KB
Line 
1/*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 * 4. 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.
16 *
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
27 * SUCH DAMAGE.
28 */
29
30#ifndef lint
31#if 0
32static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: src/bin/cp/utils.c,v 1.43 2004/04/06 20:06:44 markm Exp $");
35#endif
36#endif /* not lint */
37
38#ifndef _MSC_VER
39# include <sys/param.h>
40#endif
41#include <sys/stat.h>
42#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
43# include <sys/mman.h>
44#endif
45
46#include "err.h"
47#include <errno.h>
48#include <fcntl.h>
49#include <fts.h>
50#include <limits.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <signal.h>
54#include <sysexits.h>
55#include <unistd.h>
56#ifdef __sun__
57# include "solfakes.h"
58#endif
59#ifdef _MSC_VER
60# define MSC_DO_64_BIT_IO
61# include "mscfakes.h"
62#endif
63#include "cp_extern.h"
64#include "cmp_extern.h"
65
66#define cp_pct(x,y) (int)(100.0 * (double)(x) / (double)(y))
67
68#ifndef MAXBSIZE
69# define MAXBSIZE 0x20000
70#endif
71#ifndef O_BINARY
72# define O_BINARY 0
73#endif
74
75#ifndef S_ISVTX
76# define S_ISVTX 0
77#endif
78
79
80int
81copy_file(const FTSENT *entp, int dne, int changed_only, int *pcopied)
82{
83 static char buf[MAXBSIZE];
84 struct stat *fs;
85 int ch, checkch, from_fd, rcount, rval, to_fd;
86 ssize_t wcount;
87 size_t wresid;
88 size_t wtotal;
89 char *bufp;
90#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
91 char *p;
92#endif
93
94 *pcopied = 0;
95
96 if ((from_fd = open(entp->fts_path, O_RDONLY | O_BINARY, 0)) == -1) {
97 warn("%s", entp->fts_path);
98 return (1);
99 }
100
101 fs = entp->fts_statp;
102
103 /*
104 * If the file exists and we're interactive, verify with the user.
105 * If the file DNE, set the mode to be the from file, minus setuid
106 * bits, modified by the umask; arguably wrong, but it makes copying
107 * executables work right and it's been that way forever. (The
108 * other choice is 666 or'ed with the execute bits on the from file
109 * modified by the umask.)
110 */
111 if (!dne) {
112 /* compare the files first if requested */
113 if (changed_only) {
114 if (cmp_fd_and_file(from_fd, entp->fts_path, to.p_path,
115 1 /* silent */, 0 /* lflag */,
116 0 /* special */) == OK_EXIT) {
117 close(from_fd);
118 return (0);
119 }
120 if (lseek(from_fd, 0, SEEK_SET) != 0) {
121 close(from_fd);
122 if ((from_fd = open(entp->fts_path, O_RDONLY | O_BINARY, 0)) == -1) {
123 warn("%s", entp->fts_path);
124 return (1);
125 }
126 }
127 }
128
129#define YESNO "(y/n [n]) "
130 if (nflag) {
131 if (vflag)
132 printf("%s not overwritten\n", to.p_path);
133 return (0);
134 } else if (iflag) {
135 (void)fprintf(stderr, "overwrite %s? %s",
136 to.p_path, YESNO);
137 checkch = ch = getchar();
138 while (ch != '\n' && ch != EOF)
139 ch = getchar();
140 if (checkch != 'y' && checkch != 'Y') {
141 (void)close(from_fd);
142 (void)fprintf(stderr, "not overwritten\n");
143 return (1);
144 }
145 }
146
147 if (fflag) {
148 /* remove existing destination file name,
149 * create a new file */
150 (void)unlink(to.p_path);
151 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY,
152 fs->st_mode & ~(S_ISUID | S_ISGID));
153 } else
154 /* overwrite existing destination file name */
155 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_BINARY, 0);
156 } else
157 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY,
158 fs->st_mode & ~(S_ISUID | S_ISGID));
159
160 if (to_fd == -1) {
161 warn("%s", to.p_path);
162 (void)close(from_fd);
163 return (1);
164 }
165
166 rval = 0;
167 *pcopied = 1;
168
169 /*
170 * Mmap and write if less than 8M (the limit is so we don't totally
171 * trash memory on big files. This is really a minor hack, but it
172 * wins some CPU back.
173 */
174#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
175 if (S_ISREG(fs->st_mode) && fs->st_size > 0 &&
176 fs->st_size <= 8 * 1048576) {
177 if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
178 MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) {
179 warn("%s", entp->fts_path);
180 rval = 1;
181 } else {
182 wtotal = 0;
183 for (bufp = p, wresid = fs->st_size; ;
184 bufp += wcount, wresid -= (size_t)wcount) {
185 wcount = write(to_fd, bufp, wresid);
186 wtotal += wcount;
187 if (info) {
188 info = 0;
189 (void)fprintf(stderr,
190 "%s -> %s %3d%%\n",
191 entp->fts_path, to.p_path,
192 cp_pct(wtotal, fs->st_size));
193
194 }
195 if (wcount >= (ssize_t)wresid || wcount <= 0)
196 break;
197 }
198 if (wcount != (ssize_t)wresid) {
199 warn("%s", to.p_path);
200 rval = 1;
201 }
202 /* Some systems don't unmap on close(2). */
203 if (munmap(p, fs->st_size) < 0) {
204 warn("%s", entp->fts_path);
205 rval = 1;
206 }
207 }
208 } else
209#endif
210 {
211 wtotal = 0;
212 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
213 for (bufp = buf, wresid = rcount; ;
214 bufp += wcount, wresid -= wcount) {
215 wcount = write(to_fd, bufp, wresid);
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 }
225 if (wcount >= (ssize_t)wresid || wcount <= 0)
226 break;
227 }
228 if (wcount != (ssize_t)wresid) {
229 warn("%s", to.p_path);
230 rval = 1;
231 break;
232 }
233 }
234 if (rcount < 0) {
235 warn("%s", entp->fts_path);
236 rval = 1;
237 }
238 }
239
240 /*
241 * Don't remove the target even after an error. The target might
242 * not be a regular file, or its attributes might be important,
243 * or its contents might be irreplaceable. It would only be safe
244 * to remove it if we created it and its length is 0.
245 */
246
247 if (pflag && setfile(fs, to_fd))
248 rval = 1;
249 (void)close(from_fd);
250 if (close(to_fd)) {
251 warn("%s", to.p_path);
252 rval = 1;
253 }
254 return (rval);
255}
256
257int
258copy_link(const FTSENT *p, int exists)
259{
260 int len;
261 char llink[PATH_MAX];
262
263 if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) {
264 warn("readlink: %s", p->fts_path);
265 return (1);
266 }
267 llink[len] = '\0';
268 if (exists && unlink(to.p_path)) {
269 warn("unlink: %s", to.p_path);
270 return (1);
271 }
272 if (symlink(llink, to.p_path)) {
273 warn("symlink: %s", llink);
274 return (1);
275 }
276 return (pflag ? setfile(p->fts_statp, -1) : 0);
277}
278
279int
280copy_fifo(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 (mkfifo(to.p_path, from_stat->st_mode)) {
287 warn("mkfifo: %s", to.p_path);
288 return (1);
289 }
290 return (pflag ? setfile(from_stat, -1) : 0);
291}
292
293int
294copy_special(struct stat *from_stat, int exists)
295{
296 if (exists && unlink(to.p_path)) {
297 warn("unlink: %s", to.p_path);
298 return (1);
299 }
300 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
301 warn("mknod: %s", to.p_path);
302 return (1);
303 }
304 return (pflag ? setfile(from_stat, -1) : 0);
305}
306
307int
308setfile(struct stat *fs, int fd)
309{
310 static struct timeval tv[2];
311 struct stat ts;
312 int rval, gotstat, islink, fdval;
313
314 rval = 0;
315 fdval = fd != -1;
316 islink = !fdval && S_ISLNK(fs->st_mode);
317 fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX |
318 S_IRWXU | S_IRWXG | S_IRWXO;
319
320#ifdef HAVE_ST_TIMESPEC
321 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
322 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
323#else
324 tv[0].tv_sec = fs->st_atime;
325 tv[1].tv_sec = fs->st_mtime;
326 tv[0].tv_usec = tv[1].tv_usec = 0;
327#endif
328 if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) {
329 warn("%sutimes: %s", islink ? "l" : "", to.p_path);
330 rval = 1;
331 }
332 if (fdval ? fstat(fd, &ts) :
333 (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts)))
334 gotstat = 0;
335 else {
336 gotstat = 1;
337 ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
338 S_IRWXU | S_IRWXG | S_IRWXO;
339 }
340 /*
341 * Changing the ownership probably won't succeed, unless we're root
342 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
343 * the mode; current BSD behavior is to remove all setuid bits on
344 * chown. If chown fails, lose setuid/setgid bits.
345 */
346 if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
347 if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
348 (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
349 chown(to.p_path, fs->st_uid, fs->st_gid))) {
350 if (errno != EPERM) {
351 warn("chown: %s", to.p_path);
352 rval = 1;
353 }
354 fs->st_mode &= ~(S_ISUID | S_ISGID);
355 }
356
357 if (!gotstat || fs->st_mode != ts.st_mode)
358 if (fdval ? fchmod(fd, fs->st_mode) :
359 (islink ? lchmod(to.p_path, fs->st_mode) :
360 chmod(to.p_path, fs->st_mode))) {
361 warn("chmod: %s", to.p_path);
362 rval = 1;
363 }
364
365#ifdef HAVE_ST_FLAGS
366 if (!gotstat || fs->st_flags != ts.st_flags)
367 if (fdval ?
368 fchflags(fd, fs->st_flags) :
369 (islink ? (errno = ENOSYS) :
370 chflags(to.p_path, fs->st_flags))) {
371 warn("chflags: %s", to.p_path);
372 rval = 1;
373 }
374#endif
375
376 return (rval);
377}
378
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette