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