VirtualBox

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

Last change on this file since 2113 was 2113, checked in by bird, 16 years ago

kmkbuiltin: include config.h

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