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