source: trunk/src/gmake/kmkbuiltin/mv.c@ 627

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

rename isn't destructive in MSC.

File size: 11.7 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#ifdef _MSC_VER
267 if (errno == EEXIST) {
268 remove(to);
269 if (!rename(from, to)) {
270 if (vflg)
271 printf("%s -> %s\n", from, to);
272 return (0);
273 }
274 }
275#endif
276
277 if (errno == EXDEV) {
278#ifndef CROSS_DEVICE_MOVE
279 warnx("cannot move `%s' to a different device: `%s'", from, to);
280 return (1);
281#else
282 struct statfs sfs;
283 char path[PATH_MAX];
284
285 /*
286 * If the source is a symbolic link and is on another
287 * filesystem, it can be recreated at the destination.
288 */
289 if (lstat(from, &sb) == -1) {
290 warn("%s", from);
291 return (1);
292 }
293 if (!S_ISLNK(sb.st_mode)) {
294 /* Can't mv(1) a mount point. */
295 if (realpath(from, path) == NULL) {
296 warnx("cannot resolve %s: %s", from, path);
297 return (1);
298 }
299 if (!statfs(path, &sfs) &&
300 !strcmp(path, sfs.f_mntonname)) {
301 warnx("cannot rename a mount point");
302 return (1);
303 }
304 }
305#endif
306 } else {
307 warn("rename %s to %s", from, to);
308 return (1);
309 }
310
311#ifdef CROSS_DEVICE_MOVE
312 /*
313 * If rename fails because we're trying to cross devices, and
314 * it's a regular file, do the copy internally; otherwise, use
315 * cp and rm.
316 */
317 if (lstat(from, &sb)) {
318 warn("%s", from);
319 return (1);
320 }
321 return (S_ISREG(sb.st_mode) ?
322 fastcopy(from, to, &sb) : copy(from, to));
323#endif
324}
325
326#ifdef CROSS_DEVICE_MOVE
327int
328static fastcopy(char *from, char *to, struct stat *sbp)
329{
330 struct timeval tval[2];
331 static u_int blen;
332 static char *bp;
333 mode_t oldmode;
334 int nread, from_fd, to_fd;
335 acl_t acl;
336
337 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
338 warn("%s", from);
339 return (1);
340 }
341 if (blen < sbp->st_blksize) {
342 if (bp != NULL)
343 free(bp);
344 if ((bp = malloc((size_t)sbp->st_blksize)) == NULL) {
345 blen = 0;
346 warnx("malloc failed");
347 return (1);
348 }
349 blen = sbp->st_blksize;
350 }
351 while ((to_fd =
352 open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
353 if (errno == EEXIST && unlink(to) == 0)
354 continue;
355 warn("%s", to);
356 (void)close(from_fd);
357 return (1);
358 }
359 while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
360 if (write(to_fd, bp, (size_t)nread) != nread) {
361 warn("%s", to);
362 goto err;
363 }
364 if (nread < 0) {
365 warn("%s", from);
366err: if (unlink(to))
367 warn("%s: remove", to);
368 (void)close(from_fd);
369 (void)close(to_fd);
370 return (1);
371 }
372
373 oldmode = sbp->st_mode & ALLPERMS;
374 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
375 warn("%s: set owner/group (was: %lu/%lu)", to,
376 (u_long)sbp->st_uid, (u_long)sbp->st_gid);
377 if (oldmode & (S_ISUID | S_ISGID)) {
378 warnx(
379"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
380 to, oldmode);
381 sbp->st_mode &= ~(S_ISUID | S_ISGID);
382 }
383 }
384 /*
385 * POSIX 1003.2c states that if _POSIX_ACL_EXTENDED is in effect
386 * for dest_file, then it's ACLs shall reflect the ACLs of the
387 * source_file.
388 */
389 if (fpathconf(to_fd, _PC_ACL_EXTENDED) == 1 &&
390 fpathconf(from_fd, _PC_ACL_EXTENDED) == 1) {
391 acl = acl_get_fd(from_fd);
392 if (acl == NULL)
393 warn("failed to get acl entries while setting %s",
394 from);
395 else if (acl_set_fd(to_fd, acl) < 0)
396 warn("failed to set acl entries for %s", to);
397 }
398 (void)close(from_fd);
399 if (fchmod(to_fd, sbp->st_mode))
400 warn("%s: set mode (was: 0%03o)", to, oldmode);
401 /*
402 * XXX
403 * NFS doesn't support chflags; ignore errors unless there's reason
404 * to believe we're losing bits. (Note, this still won't be right
405 * if the server supports flags and we were trying to *remove* flags
406 * on a file that we copied, i.e., that we didn't create.)
407 */
408 errno = 0;
409 if (fchflags(to_fd, (u_long)sbp->st_flags))
410 if (errno != EOPNOTSUPP || sbp->st_flags != 0)
411 warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
412
413 tval[0].tv_sec = sbp->st_atime;
414 tval[1].tv_sec = sbp->st_mtime;
415 tval[0].tv_usec = tval[1].tv_usec = 0;
416 if (utimes(to, tval))
417 warn("%s: set times", to);
418
419 if (close(to_fd)) {
420 warn("%s", to);
421 return (1);
422 }
423
424 if (unlink(from)) {
425 warn("%s: remove", from);
426 return (1);
427 }
428 if (vflg)
429 printf("%s -> %s\n", from, to);
430 return (0);
431}
432
433int
434copy(char *from, char *to)
435{
436 int pid, status;
437
438 if ((pid = fork()) == 0) {
439 execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to,
440 (char *)NULL);
441 warn("%s", _PATH_CP);
442 _exit(1);
443 }
444 if (waitpid(pid, &status, 0) == -1) {
445 warn("%s: waitpid", _PATH_CP);
446 return (1);
447 }
448 if (!WIFEXITED(status)) {
449 warnx("%s: did not terminate normally", _PATH_CP);
450 return (1);
451 }
452 if (WEXITSTATUS(status)) {
453 warnx("%s: terminated with %d (non-zero) status",
454 _PATH_CP, WEXITSTATUS(status));
455 return (1);
456 }
457 if (!(pid = vfork())) {
458 execl(_PATH_RM, "mv", "-rf", "--", from, (char *)NULL);
459 warn("%s", _PATH_RM);
460 _exit(1);
461 }
462 if (waitpid(pid, &status, 0) == -1) {
463 warn("%s: waitpid", _PATH_RM);
464 return (1);
465 }
466 if (!WIFEXITED(status)) {
467 warnx("%s: did not terminate normally", _PATH_RM);
468 return (1);
469 }
470 if (WEXITSTATUS(status)) {
471 warnx("%s: terminated with %d (non-zero) status",
472 _PATH_RM, WEXITSTATUS(status));
473 return (1);
474 }
475 return (0);
476}
477#endif /* CROSS_DEVICE_MOVE */
478
479
480static int
481usage(void)
482{
483
484 (void)fprintf(stderr, "%s\n%s\n",
485 "usage: mv [-f | -i | -n] [-v] source target",
486 " mv [-f | -i | -n] [-v] source ... directory");
487 return EX_USAGE;
488}
Note: See TracBrowser for help on using the repository browser.