VirtualBox

source: kBuild/trunk/src/gmake/kmkbuiltin/cp_utils.c@ 347

Last change on this file since 347 was 347, checked in by bird, 19 years ago

Open files in binary mode before copying!

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