VirtualBox

source: kBuild/trunk/src/gmake/kmkbuiltin/mv.c@ 621

Last change on this file since 621 was 614, checked in by bird, 18 years ago

user_from_uid/group_from_gid is BSD stuff.

File size: 11.5 KB
Line 
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 <sys/types.h>
50#ifndef _MSC_VER
51#include <sys/acl.h>
52#include <sys/param.h>
53#include <sys/time.h>
54#include <sys/wait.h>
55#include <sys/mount.h>
56#endif
57#include <sys/stat.h>
58
59#include "err.h"
60#include <errno.h>
61#include <fcntl.h>
62#ifndef _MSC_VER
63#include <grp.h>
64#endif
65#include <limits.h>
66#ifndef _MSC_VER
67#include <paths.h>
68#include <pwd.h>
69#endif
70#include <stdio.h>
71#include <stdlib.h>
72#include <string.h>
73#ifndef _MSC_VER
74#include <sysexits.h>
75#include <unistd.h>
76#else
77#include "mscfakes.h"
78#endif
79
80#if !defined(__FreeBSD__) && !defined(__APPLE__)
81extern void strmode(mode_t mode, char *p);
82#endif
83
84static int fflg, iflg, nflg, vflg;
85
86static int do_move(char *, char *);
87#ifdef CROSS_DEVICE_MOVE
88static int fastcopy(char *, char *, struct stat *);
89static int copy(char *, char *);
90#endif
91static int usage(void);
92
93#if !defined(__FreeBSD__) && !defined(__APPLE__)
94static const char *user_from_uid(unsigned long id, int x)
95{
96 static char s_buf[64];
97 sprintf(s_buf, "%ld", id);
98 return s_buf;
99}
100static const char *group_from_gid(unsigned long id, int x)
101{
102 static char s_buf[64];
103 sprintf(s_buf, "%ld", id);
104 return s_buf;
105}
106#endif
107
108
109int
110kmk_builtin_mv(int argc, char *argv[])
111{
112 size_t baselen, len;
113 int rval;
114 char *p, *endp;
115 struct stat sb;
116 int ch;
117 char path[PATH_MAX];
118
119 /* kmk: reinitialize globals */
120 fflg = iflg = nflg = vflg = 0;
121
122 /* kmk: reset getopt and set progname */
123 g_progname = argv[0];
124 opterr = 1;
125 optarg = NULL;
126 optopt = 0;
127#if defined(__FreeBSD__) || defined(__EMX__) || defined(__APPLE__)
128 optreset = 1;
129 optind = 1;
130#else
131 optind = 0; /* init */
132#endif
133
134 while ((ch = getopt(argc, argv, "finv")) != -1)
135 switch (ch) {
136 case 'i':
137 iflg = 1;
138 fflg = nflg = 0;
139 break;
140 case 'f':
141 fflg = 1;
142 iflg = nflg = 0;
143 break;
144 case 'n':
145 nflg = 1;
146 fflg = iflg = 0;
147 break;
148 case 'v':
149 vflg = 1;
150 break;
151 default:
152 return usage();
153 }
154 argc -= optind;
155 argv += optind;
156
157 if (argc < 2)
158 return usage();
159
160 /*
161 * If the stat on the target fails or the target isn't a directory,
162 * try the move. More than 2 arguments is an error in this case.
163 */
164 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
165 if (argc > 2)
166 return usage();
167 return do_move(argv[0], argv[1]);
168 }
169
170 /* It's a directory, move each file into it. */
171 if (strlen(argv[argc - 1]) > sizeof(path) - 1)
172 return errx(1, "%s: destination pathname too long", *argv);
173 (void)strcpy(path, argv[argc - 1]);
174 baselen = strlen(path);
175 endp = &path[baselen];
176#if defined(_MSC_VER) || defined(__EMX__)
177 if (!baselen || (*(endp - 1) != '/' && *(endp - 1) != '\\' && *(endp - 1) != ':')) {
178#else
179 if (!baselen || *(endp - 1) != '/') {
180#endif
181 *endp++ = '/';
182 ++baselen;
183 }
184 for (rval = 0; --argc; ++argv) {
185 /*
186 * Find the last component of the source pathname. It
187 * may have trailing slashes.
188 */
189 p = *argv + strlen(*argv);
190#if defined(_MSC_VER) || defined(__EMX__)
191 while (p != *argv && (p[-1] == '/' || p[-1] == '\\'))
192 --p;
193 while (p != *argv && p[-1] != '/' && p[-1] != '/' && p[-1] != ':')
194 --p;
195#else
196 while (p != *argv && p[-1] == '/')
197 --p;
198 while (p != *argv && p[-1] != '/')
199 --p;
200#endif
201
202 if ((baselen + (len = strlen(p))) >= PATH_MAX) {
203 warnx("%s: destination pathname too long", *argv);
204 rval = 1;
205 } else {
206 memmove(endp, p, (size_t)len + 1);
207 if (do_move(*argv, path))
208 rval = 1;
209 }
210 }
211 return rval;
212}
213
214static int
215do_move(char *from, char *to)
216{
217 struct stat sb;
218 int ask, ch, first;
219 char modep[15];
220
221 /*
222 * Check access. If interactive and file exists, ask user if it
223 * should be replaced. Otherwise if file exists but isn't writable
224 * make sure the user wants to clobber it.
225 */
226 if (!fflg && !access(to, F_OK)) {
227
228 /* prompt only if source exist */
229 if (lstat(from, &sb) == -1) {
230 warn("%s", from);
231 return (1);
232 }
233
234#define YESNO "(y/n [n]) "
235 ask = 0;
236 if (nflg) {
237 if (vflg)
238 printf("%s not overwritten\n", to);
239 return (0);
240 } else if (iflg) {
241 (void)fprintf(stderr, "overwrite %s? %s", to, YESNO);
242 ask = 1;
243 } else if (access(to, W_OK) && !stat(to, &sb)) {
244 strmode(sb.st_mode, modep);
245 (void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
246 modep + 1, modep[9] == ' ' ? "" : " ",
247 user_from_uid((unsigned long)sb.st_uid, 0),
248 group_from_gid((unsigned long)sb.st_gid, 0), to, YESNO);
249 ask = 1;
250 }
251 if (ask) {
252 first = ch = getchar();
253 while (ch != '\n' && ch != EOF)
254 ch = getchar();
255 if (first != 'y' && first != 'Y') {
256 (void)fprintf(stderr, "not overwritten\n");
257 return (0);
258 }
259 }
260 }
261 if (!rename(from, to)) {
262 if (vflg)
263 printf("%s -> %s\n", from, to);
264 return (0);
265 }
266
267 if (errno == EXDEV) {
268#ifndef CROSS_DEVICE_MOVE
269 warnx("cannot move `%s' to a different device: `%s'", from, to);
270 return (1);
271#else
272 struct statfs sfs;
273 char path[PATH_MAX];
274
275 /*
276 * If the source is a symbolic link and is on another
277 * filesystem, it can be recreated at the destination.
278 */
279 if (lstat(from, &sb) == -1) {
280 warn("%s", from);
281 return (1);
282 }
283 if (!S_ISLNK(sb.st_mode)) {
284 /* Can't mv(1) a mount point. */
285 if (realpath(from, path) == NULL) {
286 warnx("cannot resolve %s: %s", from, path);
287 return (1);
288 }
289 if (!statfs(path, &sfs) &&
290 !strcmp(path, sfs.f_mntonname)) {
291 warnx("cannot rename a mount point");
292 return (1);
293 }
294 }
295#endif
296 } else {
297 warn("rename %s to %s", from, to);
298 return (1);
299 }
300
301#ifdef CROSS_DEVICE_MOVE
302 /*
303 * If rename fails because we're trying to cross devices, and
304 * it's a regular file, do the copy internally; otherwise, use
305 * cp and rm.
306 */
307 if (lstat(from, &sb)) {
308 warn("%s", from);
309 return (1);
310 }
311 return (S_ISREG(sb.st_mode) ?
312 fastcopy(from, to, &sb) : copy(from, to));
313#endif
314}
315
316#ifdef CROSS_DEVICE_MOVE
317int
318static fastcopy(char *from, char *to, struct stat *sbp)
319{
320 struct timeval tval[2];
321 static u_int blen;
322 static char *bp;
323 mode_t oldmode;
324 int nread, from_fd, to_fd;
325 acl_t acl;
326
327 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
328 warn("%s", from);
329 return (1);
330 }
331 if (blen < sbp->st_blksize) {
332 if (bp != NULL)
333 free(bp);
334 if ((bp = malloc((size_t)sbp->st_blksize)) == NULL) {
335 blen = 0;
336 warnx("malloc failed");
337 return (1);
338 }
339 blen = sbp->st_blksize;
340 }
341 while ((to_fd =
342 open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
343 if (errno == EEXIST && unlink(to) == 0)
344 continue;
345 warn("%s", to);
346 (void)close(from_fd);
347 return (1);
348 }
349 while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
350 if (write(to_fd, bp, (size_t)nread) != nread) {
351 warn("%s", to);
352 goto err;
353 }
354 if (nread < 0) {
355 warn("%s", from);
356err: if (unlink(to))
357 warn("%s: remove", to);
358 (void)close(from_fd);
359 (void)close(to_fd);
360 return (1);
361 }
362
363 oldmode = sbp->st_mode & ALLPERMS;
364 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
365 warn("%s: set owner/group (was: %lu/%lu)", to,
366 (u_long)sbp->st_uid, (u_long)sbp->st_gid);
367 if (oldmode & (S_ISUID | S_ISGID)) {
368 warnx(
369"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
370 to, oldmode);
371 sbp->st_mode &= ~(S_ISUID | S_ISGID);
372 }
373 }
374 /*
375 * POSIX 1003.2c states that if _POSIX_ACL_EXTENDED is in effect
376 * for dest_file, then it's ACLs shall reflect the ACLs of the
377 * source_file.
378 */
379 if (fpathconf(to_fd, _PC_ACL_EXTENDED) == 1 &&
380 fpathconf(from_fd, _PC_ACL_EXTENDED) == 1) {
381 acl = acl_get_fd(from_fd);
382 if (acl == NULL)
383 warn("failed to get acl entries while setting %s",
384 from);
385 else if (acl_set_fd(to_fd, acl) < 0)
386 warn("failed to set acl entries for %s", to);
387 }
388 (void)close(from_fd);
389 if (fchmod(to_fd, sbp->st_mode))
390 warn("%s: set mode (was: 0%03o)", to, oldmode);
391 /*
392 * XXX
393 * NFS doesn't support chflags; ignore errors unless there's reason
394 * to believe we're losing bits. (Note, this still won't be right
395 * if the server supports flags and we were trying to *remove* flags
396 * on a file that we copied, i.e., that we didn't create.)
397 */
398 errno = 0;
399 if (fchflags(to_fd, (u_long)sbp->st_flags))
400 if (errno != EOPNOTSUPP || sbp->st_flags != 0)
401 warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
402
403 tval[0].tv_sec = sbp->st_atime;
404 tval[1].tv_sec = sbp->st_mtime;
405 tval[0].tv_usec = tval[1].tv_usec = 0;
406 if (utimes(to, tval))
407 warn("%s: set times", to);
408
409 if (close(to_fd)) {
410 warn("%s", to);
411 return (1);
412 }
413
414 if (unlink(from)) {
415 warn("%s: remove", from);
416 return (1);
417 }
418 if (vflg)
419 printf("%s -> %s\n", from, to);
420 return (0);
421}
422
423int
424copy(char *from, char *to)
425{
426 int pid, status;
427
428 if ((pid = fork()) == 0) {
429 execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to,
430 (char *)NULL);
431 warn("%s", _PATH_CP);
432 _exit(1);
433 }
434 if (waitpid(pid, &status, 0) == -1) {
435 warn("%s: waitpid", _PATH_CP);
436 return (1);
437 }
438 if (!WIFEXITED(status)) {
439 warnx("%s: did not terminate normally", _PATH_CP);
440 return (1);
441 }
442 if (WEXITSTATUS(status)) {
443 warnx("%s: terminated with %d (non-zero) status",
444 _PATH_CP, WEXITSTATUS(status));
445 return (1);
446 }
447 if (!(pid = vfork())) {
448 execl(_PATH_RM, "mv", "-rf", "--", from, (char *)NULL);
449 warn("%s", _PATH_RM);
450 _exit(1);
451 }
452 if (waitpid(pid, &status, 0) == -1) {
453 warn("%s: waitpid", _PATH_RM);
454 return (1);
455 }
456 if (!WIFEXITED(status)) {
457 warnx("%s: did not terminate normally", _PATH_RM);
458 return (1);
459 }
460 if (WEXITSTATUS(status)) {
461 warnx("%s: terminated with %d (non-zero) status",
462 _PATH_RM, WEXITSTATUS(status));
463 return (1);
464 }
465 return (0);
466}
467#endif /* CROSS_DEVICE_MOVE */
468
469
470static int
471usage(void)
472{
473
474 (void)fprintf(stderr, "%s\n%s\n",
475 "usage: mv [-f | -i | -n] [-v] source target",
476 " mv [-f | -i | -n] [-v] source ... directory");
477 return EX_USAGE;
478}
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