VirtualBox

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

Last change on this file since 1321 was 1309, checked in by bird, 17 years ago

combined the bulk of the cmp stuff into cmp_util.c. implemented cp --changed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 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 && cmp_fd_and_file(from_fd, entp->fts_path, to.p_path,
115 1 /* silent */, 0 /* lflag */,
116 0 /* special */)
117 == OK_EXIT) {
118 return (0);
119 }
120
121#define YESNO "(y/n [n]) "
122 if (nflag) {
123 if (vflag)
124 printf("%s not overwritten\n", to.p_path);
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 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY,
144 fs->st_mode & ~(S_ISUID | S_ISGID));
145 } else
146 /* overwrite existing destination file name */
147 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_BINARY, 0);
148 } else
149 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY,
150 fs->st_mode & ~(S_ISUID | S_ISGID));
151
152 if (to_fd == -1) {
153 warn("%s", to.p_path);
154 (void)close(from_fd);
155 return (1);
156 }
157
158 rval = 0;
159 *pcopied = 1;
160
161 /*
162 * Mmap and write if less than 8M (the limit is so we don't totally
163 * trash memory on big files. This is really a minor hack, but it
164 * wins some CPU back.
165 */
166#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
167 if (S_ISREG(fs->st_mode) && fs->st_size > 0 &&
168 fs->st_size <= 8 * 1048576) {
169 if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
170 MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) {
171 warn("%s", entp->fts_path);
172 rval = 1;
173 } else {
174 wtotal = 0;
175 for (bufp = p, wresid = fs->st_size; ;
176 bufp += wcount, wresid -= (size_t)wcount) {
177 wcount = write(to_fd, bufp, wresid);
178 wtotal += wcount;
179 if (info) {
180 info = 0;
181 (void)fprintf(stderr,
182 "%s -> %s %3d%%\n",
183 entp->fts_path, to.p_path,
184 cp_pct(wtotal, fs->st_size));
185
186 }
187 if (wcount >= (ssize_t)wresid || wcount <= 0)
188 break;
189 }
190 if (wcount != (ssize_t)wresid) {
191 warn("%s", to.p_path);
192 rval = 1;
193 }
194 /* Some systems don't unmap on close(2). */
195 if (munmap(p, fs->st_size) < 0) {
196 warn("%s", entp->fts_path);
197 rval = 1;
198 }
199 }
200 } else
201#endif
202 {
203 wtotal = 0;
204 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
205 for (bufp = buf, wresid = rcount; ;
206 bufp += wcount, wresid -= wcount) {
207 wcount = write(to_fd, bufp, wresid);
208 wtotal += wcount;
209 if (info) {
210 info = 0;
211 (void)fprintf(stderr,
212 "%s -> %s %3d%%\n",
213 entp->fts_path, to.p_path,
214 cp_pct(wtotal, fs->st_size));
215
216 }
217 if (wcount >= (ssize_t)wresid || wcount <= 0)
218 break;
219 }
220 if (wcount != (ssize_t)wresid) {
221 warn("%s", to.p_path);
222 rval = 1;
223 break;
224 }
225 }
226 if (rcount < 0) {
227 warn("%s", entp->fts_path);
228 rval = 1;
229 }
230 }
231
232 /*
233 * Don't remove the target even after an error. The target might
234 * not be a regular file, or its attributes might be important,
235 * or its contents might be irreplaceable. It would only be safe
236 * to remove it if we created it and its length is 0.
237 */
238
239 if (pflag && setfile(fs, to_fd))
240 rval = 1;
241 (void)close(from_fd);
242 if (close(to_fd)) {
243 warn("%s", to.p_path);
244 rval = 1;
245 }
246 return (rval);
247}
248
249int
250copy_link(const FTSENT *p, int exists)
251{
252 int len;
253 char llink[PATH_MAX];
254
255 if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) {
256 warn("readlink: %s", p->fts_path);
257 return (1);
258 }
259 llink[len] = '\0';
260 if (exists && unlink(to.p_path)) {
261 warn("unlink: %s", to.p_path);
262 return (1);
263 }
264 if (symlink(llink, to.p_path)) {
265 warn("symlink: %s", llink);
266 return (1);
267 }
268 return (pflag ? setfile(p->fts_statp, -1) : 0);
269}
270
271int
272copy_fifo(struct stat *from_stat, int exists)
273{
274 if (exists && unlink(to.p_path)) {
275 warn("unlink: %s", to.p_path);
276 return (1);
277 }
278 if (mkfifo(to.p_path, from_stat->st_mode)) {
279 warn("mkfifo: %s", to.p_path);
280 return (1);
281 }
282 return (pflag ? setfile(from_stat, -1) : 0);
283}
284
285int
286copy_special(struct stat *from_stat, int exists)
287{
288 if (exists && unlink(to.p_path)) {
289 warn("unlink: %s", to.p_path);
290 return (1);
291 }
292 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
293 warn("mknod: %s", to.p_path);
294 return (1);
295 }
296 return (pflag ? setfile(from_stat, -1) : 0);
297}
298
299int
300setfile(struct stat *fs, int fd)
301{
302 static struct timeval tv[2];
303 struct stat ts;
304 int rval, gotstat, islink, fdval;
305
306 rval = 0;
307 fdval = fd != -1;
308 islink = !fdval && S_ISLNK(fs->st_mode);
309 fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX |
310 S_IRWXU | S_IRWXG | S_IRWXO;
311
312#ifdef HAVE_ST_TIMESPEC
313 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
314 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
315#else
316 tv[0].tv_sec = fs->st_atime;
317 tv[1].tv_sec = fs->st_mtime;
318 tv[0].tv_usec = tv[1].tv_usec = 0;
319#endif
320 if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) {
321 warn("%sutimes: %s", islink ? "l" : "", to.p_path);
322 rval = 1;
323 }
324 if (fdval ? fstat(fd, &ts) :
325 (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts)))
326 gotstat = 0;
327 else {
328 gotstat = 1;
329 ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
330 S_IRWXU | S_IRWXG | S_IRWXO;
331 }
332 /*
333 * Changing the ownership probably won't succeed, unless we're root
334 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
335 * the mode; current BSD behavior is to remove all setuid bits on
336 * chown. If chown fails, lose setuid/setgid bits.
337 */
338 if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
339 if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
340 (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
341 chown(to.p_path, fs->st_uid, fs->st_gid))) {
342 if (errno != EPERM) {
343 warn("chown: %s", to.p_path);
344 rval = 1;
345 }
346 fs->st_mode &= ~(S_ISUID | S_ISGID);
347 }
348
349 if (!gotstat || fs->st_mode != ts.st_mode)
350 if (fdval ? fchmod(fd, fs->st_mode) :
351 (islink ? lchmod(to.p_path, fs->st_mode) :
352 chmod(to.p_path, fs->st_mode))) {
353 warn("chmod: %s", to.p_path);
354 rval = 1;
355 }
356
357#ifdef HAVE_ST_FLAGS
358 if (!gotstat || fs->st_flags != ts.st_flags)
359 if (fdval ?
360 fchflags(fd, fs->st_flags) :
361 (islink ? (errno = ENOSYS) :
362 chflags(to.p_path, fs->st_flags))) {
363 warn("chflags: %s", to.p_path);
364 rval = 1;
365 }
366#endif
367
368 return (rval);
369}
370
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