VirtualBox

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

Last change on this file since 3387 was 3221, checked in by bird, 6 years ago

kmk_cp: use getopt_r and stop using global and static variables.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use