VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/mv.c@ 2546

最後變更 在這個檔案從2546是 2546,由 bird 提交於 13 年 前

Applied modified patches for Haiku support from Mike Smith.

  • 屬性 svn:eol-style 設為 native
檔案大小: 12.2 KB
 
1/*-
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Ken Smith of The State University of New York at Buffalo.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if 0
34#ifndef lint
35static char const copyright[] =
36"@(#) Copyright (c) 1989, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94";
42#endif /* not lint */
43#endif
44#if 0
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD: src/bin/mv/mv.c,v 1.46 2005/09/05 04:36:08 csjp Exp $");
47#endif
48
49#include "config.h"
50#include <sys/types.h>
51#ifndef _MSC_VER
52# ifdef CROSS_DEVICE_MOVE
53# include <sys/acl.h>
54# endif
55# include <sys/param.h>
56# include <sys/time.h>
57# include <sys/wait.h>
58# ifndef __HAIKU__
59# include <sys/mount.h>
60# endif
61#endif
62#include <sys/stat.h>
63
64#include "err.h"
65#include <errno.h>
66#include <fcntl.h>
67#include <grp.h>
68#include <limits.h>
69#include <paths.h>
70#include <pwd.h>
71#include <stdio.h>
72#include <stdlib.h>
73#include <string.h>
74#ifndef __HAIKU__
75# include <sysexits.h>
76#endif
77#include <unistd.h>
78#include "getopt.h"
79#ifdef __sun__
80# include "solfakes.h"
81#endif
82#ifdef __HAIKU__
83# include "haikufakes.h"
84#endif
85#ifdef _MSC_VER
86# include "mscfakes.h"
87#endif
88#include "kmkbuiltin.h"
89
90
91static int fflg, iflg, nflg, vflg;
92static struct option long_options[] =
93{
94 { "help", no_argument, 0, 261 },
95 { "version", no_argument, 0, 262 },
96 { 0, 0, 0, 0 },
97};
98
99
100static int do_move(char *, char *);
101#ifdef CROSS_DEVICE_MOVE
102static int fastcopy(char *, char *, struct stat *);
103static int copy(char *, char *);
104#endif
105static int usage(FILE *);
106
107extern void bsd_strmode(mode_t mode, char *p);
108
109#if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(__DragonFly__) && !defined(__OpenBSD__)
110# ifdef __OS2__
111static
112# endif
113const char *user_from_uid(uid_t id, int x)
114{
115 static char s_buf[64];
116 sprintf(s_buf, "%ld", (long int)id);
117 (void)x;
118 return s_buf;
119}
120# ifdef __OS2__
121static
122# endif
123const char *group_from_gid(gid_t id, int x)
124{
125 static char s_buf[64];
126 sprintf(s_buf, "%ld", (long int)id);
127 (void)x;
128 return s_buf;
129}
130#endif /* 'not in libc' */
131
132
133int
134kmk_builtin_mv(int argc, char *argv[], char **envp)
135{
136 size_t baselen, len;
137 int rval;
138 char *p, *endp;
139 struct stat sb;
140 int ch;
141 char path[PATH_MAX];
142
143 /* kmk: reinitialize globals */
144 fflg = iflg = nflg = vflg = 0;
145
146 /* kmk: reset getopt and set progname */
147 g_progname = argv[0];
148 opterr = 1;
149 optarg = NULL;
150 optopt = 0;
151 optind = 0; /* init */
152
153 while ((ch = getopt_long(argc, argv, "finv", long_options, NULL)) != -1)
154 switch (ch) {
155 case 'i':
156 iflg = 1;
157 fflg = nflg = 0;
158 break;
159 case 'f':
160 fflg = 1;
161 iflg = nflg = 0;
162 break;
163 case 'n':
164 nflg = 1;
165 fflg = iflg = 0;
166 break;
167 case 'v':
168 vflg = 1;
169 break;
170 case 261:
171 usage(stdout);
172 return 0;
173 case 262:
174 return kbuild_version(argv[0]);
175 default:
176 return usage(stderr);
177 }
178 argc -= optind;
179 argv += optind;
180
181 if (argc < 2)
182 return usage(stderr);
183
184 /*
185 * If the stat on the target fails or the target isn't a directory,
186 * try the move. More than 2 arguments is an error in this case.
187 */
188 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
189 if (argc > 2)
190 return usage(stderr);
191 return do_move(argv[0], argv[1]);
192 }
193
194 /* It's a directory, move each file into it. */
195 if (strlen(argv[argc - 1]) > sizeof(path) - 1)
196 return errx(1, "%s: destination pathname too long", *argv);
197 (void)strcpy(path, argv[argc - 1]);
198 baselen = strlen(path);
199 endp = &path[baselen];
200#if defined(_MSC_VER) || defined(__EMX__)
201 if (!baselen || (*(endp - 1) != '/' && *(endp - 1) != '\\' && *(endp - 1) != ':')) {
202#else
203 if (!baselen || *(endp - 1) != '/') {
204#endif
205 *endp++ = '/';
206 ++baselen;
207 }
208 for (rval = 0; --argc; ++argv) {
209 /*
210 * Find the last component of the source pathname. It
211 * may have trailing slashes.
212 */
213 p = *argv + strlen(*argv);
214#if defined(_MSC_VER) || defined(__EMX__)
215 while (p != *argv && (p[-1] == '/' || p[-1] == '\\'))
216 --p;
217 while (p != *argv && p[-1] != '/' && p[-1] != '/' && p[-1] != ':')
218 --p;
219#else
220 while (p != *argv && p[-1] == '/')
221 --p;
222 while (p != *argv && p[-1] != '/')
223 --p;
224#endif
225
226 if ((baselen + (len = strlen(p))) >= PATH_MAX) {
227 warnx("%s: destination pathname too long", *argv);
228 rval = 1;
229 } else {
230 memmove(endp, p, (size_t)len + 1);
231 if (do_move(*argv, path))
232 rval = 1;
233 }
234 }
235 return rval;
236}
237
238static int
239do_move(char *from, char *to)
240{
241 struct stat sb;
242 int ask, ch, first;
243 char modep[15];
244
245 /*
246 * Check access. If interactive and file exists, ask user if it
247 * should be replaced. Otherwise if file exists but isn't writable
248 * make sure the user wants to clobber it.
249 */
250 if (!fflg && !access(to, F_OK)) {
251
252 /* prompt only if source exist */
253 if (lstat(from, &sb) == -1) {
254 warn("%s", from);
255 return (1);
256 }
257
258#define YESNO "(y/n [n]) "
259 ask = 0;
260 if (nflg) {
261 if (vflg)
262 printf("%s not overwritten\n", to);
263 return (0);
264 } else if (iflg) {
265 (void)fprintf(stderr, "overwrite %s? %s", to, YESNO);
266 ask = 1;
267 } else if (access(to, W_OK) && !stat(to, &sb)) {
268 bsd_strmode(sb.st_mode, modep);
269 (void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
270 modep + 1, modep[9] == ' ' ? "" : " ",
271 user_from_uid((unsigned long)sb.st_uid, 0),
272 group_from_gid((unsigned long)sb.st_gid, 0), to, YESNO);
273 ask = 1;
274 }
275 if (ask) {
276 first = ch = getchar();
277 while (ch != '\n' && ch != EOF)
278 ch = getchar();
279 if (first != 'y' && first != 'Y') {
280 (void)fprintf(stderr, "not overwritten\n");
281 return (0);
282 }
283 }
284 }
285 if (!rename(from, to)) {
286 if (vflg)
287 printf("%s -> %s\n", from, to);
288 return (0);
289 }
290#ifdef _MSC_VER
291 if (errno == EEXIST) {
292 remove(to);
293 if (!rename(from, to)) {
294 if (vflg)
295 printf("%s -> %s\n", from, to);
296 return (0);
297 }
298 }
299#endif
300
301 if (errno == EXDEV) {
302#ifndef CROSS_DEVICE_MOVE
303 warnx("cannot move `%s' to a different device: `%s'", from, to);
304 return (1);
305#else
306 struct statfs sfs;
307 char path[PATH_MAX];
308
309 /*
310 * If the source is a symbolic link and is on another
311 * filesystem, it can be recreated at the destination.
312 */
313 if (lstat(from, &sb) == -1) {
314 warn("%s", from);
315 return (1);
316 }
317 if (!S_ISLNK(sb.st_mode)) {
318 /* Can't mv(1) a mount point. */
319 if (realpath(from, path) == NULL) {
320 warnx("cannot resolve %s: %s", from, path);
321 return (1);
322 }
323 if (!statfs(path, &sfs) &&
324 !strcmp(path, sfs.f_mntonname)) {
325 warnx("cannot rename a mount point");
326 return (1);
327 }
328 }
329#endif
330 } else {
331 warn("rename %s to %s", from, to);
332 return (1);
333 }
334
335#ifdef CROSS_DEVICE_MOVE
336 /*
337 * If rename fails because we're trying to cross devices, and
338 * it's a regular file, do the copy internally; otherwise, use
339 * cp and rm.
340 */
341 if (lstat(from, &sb)) {
342 warn("%s", from);
343 return (1);
344 }
345 return (S_ISREG(sb.st_mode) ?
346 fastcopy(from, to, &sb) : copy(from, to));
347#endif
348}
349
350#ifdef CROSS_DEVICE_MOVE
351int
352static fastcopy(char *from, char *to, struct stat *sbp)
353{
354 struct timeval tval[2];
355 static u_int blen;
356 static char *bp;
357 mode_t oldmode;
358 int nread, from_fd, to_fd;
359 acl_t acl;
360
361 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
362 warn("%s", from);
363 return (1);
364 }
365 if (blen < sbp->st_blksize) {
366 if (bp != NULL)
367 free(bp);
368 if ((bp = malloc((size_t)sbp->st_blksize)) == NULL) {
369 blen = 0;
370 warnx("malloc failed");
371 return (1);
372 }
373 blen = sbp->st_blksize;
374 }
375 while ((to_fd =
376 open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
377 if (errno == EEXIST && unlink(to) == 0)
378 continue;
379 warn("%s", to);
380 (void)close(from_fd);
381 return (1);
382 }
383 while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
384 if (write(to_fd, bp, (size_t)nread) != nread) {
385 warn("%s", to);
386 goto err;
387 }
388 if (nread < 0) {
389 warn("%s", from);
390err: if (unlink(to))
391 warn("%s: remove", to);
392 (void)close(from_fd);
393 (void)close(to_fd);
394 return (1);
395 }
396
397 oldmode = sbp->st_mode & ALLPERMS;
398 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
399 warn("%s: set owner/group (was: %lu/%lu)", to,
400 (u_long)sbp->st_uid, (u_long)sbp->st_gid);
401 if (oldmode & (S_ISUID | S_ISGID)) {
402 warnx(
403"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
404 to, oldmode);
405 sbp->st_mode &= ~(S_ISUID | S_ISGID);
406 }
407 }
408 /*
409 * POSIX 1003.2c states that if _POSIX_ACL_EXTENDED is in effect
410 * for dest_file, then it's ACLs shall reflect the ACLs of the
411 * source_file.
412 */
413 if (fpathconf(to_fd, _PC_ACL_EXTENDED) == 1 &&
414 fpathconf(from_fd, _PC_ACL_EXTENDED) == 1) {
415 acl = acl_get_fd(from_fd);
416 if (acl == NULL)
417 warn("failed to get acl entries while setting %s",
418 from);
419 else if (acl_set_fd(to_fd, acl) < 0)
420 warn("failed to set acl entries for %s", to);
421 }
422 (void)close(from_fd);
423 if (fchmod(to_fd, sbp->st_mode))
424 warn("%s: set mode (was: 0%03o)", to, oldmode);
425 /*
426 * XXX
427 * NFS doesn't support chflags; ignore errors unless there's reason
428 * to believe we're losing bits. (Note, this still won't be right
429 * if the server supports flags and we were trying to *remove* flags
430 * on a file that we copied, i.e., that we didn't create.)
431 */
432 errno = 0;
433 if (fchflags(to_fd, (u_long)sbp->st_flags))
434 if (errno != EOPNOTSUPP || sbp->st_flags != 0)
435 warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
436
437 tval[0].tv_sec = sbp->st_atime;
438 tval[1].tv_sec = sbp->st_mtime;
439 tval[0].tv_usec = tval[1].tv_usec = 0;
440 if (utimes(to, tval))
441 warn("%s: set times", to);
442
443 if (close(to_fd)) {
444 warn("%s", to);
445 return (1);
446 }
447
448 if (unlink(from)) {
449 warn("%s: remove", from);
450 return (1);
451 }
452 if (vflg)
453 printf("%s -> %s\n", from, to);
454 return (0);
455}
456
457int
458copy(char *from, char *to)
459{
460 int pid, status;
461
462 if ((pid = fork()) == 0) {
463 execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to,
464 (char *)NULL);
465 warn("%s", _PATH_CP);
466 _exit(1);
467 }
468 if (waitpid(pid, &status, 0) == -1) {
469 warn("%s: waitpid", _PATH_CP);
470 return (1);
471 }
472 if (!WIFEXITED(status)) {
473 warnx("%s: did not terminate normally", _PATH_CP);
474 return (1);
475 }
476 if (WEXITSTATUS(status)) {
477 warnx("%s: terminated with %d (non-zero) status",
478 _PATH_CP, WEXITSTATUS(status));
479 return (1);
480 }
481 if (!(pid = vfork())) {
482 execl(_PATH_RM, "mv", "-rf", "--", from, (char *)NULL);
483 warn("%s", _PATH_RM);
484 _exit(1);
485 }
486 if (waitpid(pid, &status, 0) == -1) {
487 warn("%s: waitpid", _PATH_RM);
488 return (1);
489 }
490 if (!WIFEXITED(status)) {
491 warnx("%s: did not terminate normally", _PATH_RM);
492 return (1);
493 }
494 if (WEXITSTATUS(status)) {
495 warnx("%s: terminated with %d (non-zero) status",
496 _PATH_RM, WEXITSTATUS(status));
497 return (1);
498 }
499 return (0);
500}
501#endif /* CROSS_DEVICE_MOVE */
502
503
504static int
505usage(FILE *pf)
506{
507 fprintf(pf, "usage: %s [-f | -i | -n] [-v] source target\n"
508 " or: %s [-f | -i | -n] [-v] source ... directory\n"
509 " or: %s --help\n"
510 " or: %s --version\n",
511 g_progname, g_progname, g_progname, g_progname);
512 return EX_USAGE;
513}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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