VirtualBox

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

Last change on this file since 3107 was 3107, checked in by bird, 8 years ago

hurd workaround

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

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