2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Ken Smith of The State University of New York at Buffalo.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char const copyright[] =
38 "@(#) Copyright (c) 1989, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/types.h>
51 #include <sys/param.h>
55 #include <sys/mount.h>
70 /* Exit code for a failed exec. */
71 #define EXEC_FAILED 127
73 static int fflg, hflg, iflg, nflg, vflg;
75 static int copy(const char *, const char *);
76 static int do_move(const char *, const char *);
77 static int fastcopy(const char *, const char *, struct stat *);
78 static void usage(void);
79 static void preserve_fd_acls(int source_fd, int dest_fd, const char *source_path,
80 const char *dest_path);
83 main(int argc, char *argv[])
92 while ((ch = getopt(argc, argv, "fhinv")) != -1)
122 * If the stat on the target fails or the target isn't a directory,
123 * try the move. More than 2 arguments is an error in this case.
125 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
127 errx(1, "%s is not a directory", argv[argc - 1]);
128 exit(do_move(argv[0], argv[1]));
132 * If -h was specified, treat the target as a symlink instead of
138 if (lstat(argv[1], &sb) == 0 && S_ISLNK(sb.st_mode))
139 exit(do_move(argv[0], argv[1]));
142 /* It's a directory, move each file into it. */
143 if (strlen(argv[argc - 1]) > sizeof(path) - 1)
144 errx(1, "%s: destination pathname too long", *argv);
145 (void)strcpy(path, argv[argc - 1]);
146 baselen = strlen(path);
147 endp = &path[baselen];
148 if (!baselen || *(endp - 1) != '/') {
152 for (rval = 0; --argc; ++argv) {
154 * Find the last component of the source pathname. It
155 * may have trailing slashes.
157 p = *argv + strlen(*argv);
158 while (p != *argv && p[-1] == '/')
160 while (p != *argv && p[-1] != '/')
163 if ((baselen + (len = strlen(p))) >= PATH_MAX) {
164 warnx("%s: destination pathname too long", *argv);
167 memmove(endp, p, (size_t)len + 1);
168 if (do_move(*argv, path))
176 do_move(const char *from, const char *to)
183 * Check access. If interactive and file exists, ask user if it
184 * should be replaced. Otherwise if file exists but isn't writable
185 * make sure the user wants to clobber it.
187 if (!fflg && !access(to, F_OK)) {
189 /* prompt only if source exist */
190 if (lstat(from, &sb) == -1) {
195 #define YESNO "(y/n [n]) "
199 printf("%s not overwritten\n", to);
202 (void)fprintf(stderr, "overwrite %s? %s", to, YESNO);
204 } else if (access(to, W_OK) && !stat(to, &sb) && isatty(STDIN_FILENO)) {
205 strmode(sb.st_mode, modep);
206 (void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
207 modep + 1, modep[9] == ' ' ? "" : " ",
208 user_from_uid((unsigned long)sb.st_uid, 0),
209 group_from_gid((unsigned long)sb.st_gid, 0), to, YESNO);
213 first = ch = getchar();
214 while (ch != '\n' && ch != EOF)
216 if (first != 'y' && first != 'Y') {
217 (void)fprintf(stderr, "not overwritten\n");
223 * Rename on FreeBSD will fail with EISDIR and ENOTDIR, before failing
224 * with EXDEV. Therefore, copy() doesn't have to perform the checks
225 * specified in the Step 3 of the POSIX mv specification.
227 if (!rename(from, to)) {
229 printf("%s -> %s\n", from, to);
233 if (errno == EXDEV) {
238 * If the source is a symbolic link and is on another
239 * filesystem, it can be recreated at the destination.
241 if (lstat(from, &sb) == -1) {
245 if (!S_ISLNK(sb.st_mode)) {
246 /* Can't mv(1) a mount point. */
247 if (realpath(from, path) == NULL) {
248 warn("cannot resolve %s: %s", from, path);
251 if (!statfs(path, &sfs) &&
252 !strcmp(path, sfs.f_mntonname)) {
253 warnx("cannot rename a mount point");
258 warn("rename %s to %s", from, to);
263 * If rename fails because we're trying to cross devices, and
264 * it's a regular file, do the copy internally; otherwise, use
267 if (lstat(from, &sb)) {
271 return (S_ISREG(sb.st_mode) ?
272 fastcopy(from, to, &sb) : copy(from, to));
276 fastcopy(const char *from, const char *to, struct stat *sbp)
278 struct timespec ts[2];
279 static u_int blen = MAXPHYS;
280 static char *bp = NULL;
282 int nread, from_fd, to_fd;
285 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
286 warn("fastcopy: open() failed (from): %s", from);
289 if (bp == NULL && (bp = malloc((size_t)blen)) == NULL) {
290 warnx("malloc(%u) failed", blen);
291 (void)close(from_fd);
295 open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
296 if (errno == EEXIST && unlink(to) == 0)
298 warn("fastcopy: open() failed (to): %s", to);
299 (void)close(from_fd);
302 while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
303 if (write(to_fd, bp, (size_t)nread) != nread) {
304 warn("fastcopy: write() failed: %s", to);
308 warn("fastcopy: read() failed: %s", from);
310 warn("%s: remove", to);
311 (void)close(from_fd);
316 oldmode = sbp->st_mode & ALLPERMS;
317 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
318 warn("%s: set owner/group (was: %lu/%lu)", to,
319 (u_long)sbp->st_uid, (u_long)sbp->st_gid);
320 if (oldmode & (S_ISUID | S_ISGID)) {
322 "%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
324 sbp->st_mode &= ~(S_ISUID | S_ISGID);
327 if (fchmod(to_fd, sbp->st_mode))
328 warn("%s: set mode (was: 0%03o)", to, oldmode);
330 * POSIX 1003.2c states that if _POSIX_ACL_EXTENDED is in effect
331 * for dest_file, then its ACLs shall reflect the ACLs of the
334 preserve_fd_acls(from_fd, to_fd, from, to);
335 (void)close(from_fd);
338 * NFS doesn't support chflags; ignore errors unless there's reason
339 * to believe we're losing bits. (Note, this still won't be right
340 * if the server supports flags and we were trying to *remove* flags
341 * on a file that we copied, i.e., that we didn't create.)
343 if (fstat(to_fd, &tsb) == 0) {
344 if ((sbp->st_flags & ~UF_ARCHIVE) !=
345 (tsb.st_flags & ~UF_ARCHIVE)) {
347 sbp->st_flags | (tsb.st_flags & UF_ARCHIVE)))
348 if (errno != EOPNOTSUPP ||
349 ((sbp->st_flags & ~UF_ARCHIVE) != 0))
350 warn("%s: set flags (was: 0%07o)",
354 warn("%s: cannot stat", to);
356 ts[0] = sbp->st_atim;
357 ts[1] = sbp->st_mtim;
358 if (futimens(to_fd, ts))
359 warn("%s: set times", to);
367 warn("%s: remove", from);
371 printf("%s -> %s\n", from, to);
376 copy(const char *from, const char *to)
381 if (lstat(to, &sb) == 0) {
382 /* Destination path exists. */
383 if (S_ISDIR(sb.st_mode)) {
384 if (rmdir(to) != 0) {
385 warn("rmdir %s", to);
389 if (unlink(to) != 0) {
390 warn("unlink %s", to);
394 } else if (errno != ENOENT) {
399 /* Copy source to destination. */
400 if (!(pid = vfork())) {
401 execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to,
405 if (waitpid(pid, &status, 0) == -1) {
406 warn("%s %s %s: waitpid", _PATH_CP, from, to);
409 if (!WIFEXITED(status)) {
410 warnx("%s %s %s: did not terminate normally",
414 switch (WEXITSTATUS(status)) {
418 warnx("%s %s %s: exec failed", _PATH_CP, from, to);
421 warnx("%s %s %s: terminated with %d (non-zero) status",
422 _PATH_CP, from, to, WEXITSTATUS(status));
426 /* Delete the source. */
427 if (!(pid = vfork())) {
428 execl(_PATH_RM, "mv", "-rf", "--", from, (char *)NULL);
431 if (waitpid(pid, &status, 0) == -1) {
432 warn("%s %s: waitpid", _PATH_RM, from);
435 if (!WIFEXITED(status)) {
436 warnx("%s %s: did not terminate normally", _PATH_RM, from);
439 switch (WEXITSTATUS(status)) {
443 warnx("%s %s: exec failed", _PATH_RM, from);
446 warnx("%s %s: terminated with %d (non-zero) status",
447 _PATH_RM, from, WEXITSTATUS(status));
454 preserve_fd_acls(int source_fd, int dest_fd, const char *source_path,
455 const char *dest_path)
459 int acl_supported = 0, ret, trivial;
461 ret = fpathconf(source_fd, _PC_ACL_NFS4);
464 acl_type = ACL_TYPE_NFS4;
465 } else if (ret < 0 && errno != EINVAL) {
466 warn("fpathconf(..., _PC_ACL_NFS4) failed for %s",
470 if (acl_supported == 0) {
471 ret = fpathconf(source_fd, _PC_ACL_EXTENDED);
474 acl_type = ACL_TYPE_ACCESS;
475 } else if (ret < 0 && errno != EINVAL) {
476 warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
481 if (acl_supported == 0)
484 acl = acl_get_fd_np(source_fd, acl_type);
486 warn("failed to get acl entries for %s", source_path);
489 if (acl_is_trivial_np(acl, &trivial)) {
490 warn("acl_is_trivial() failed for %s", source_path);
498 if (acl_set_fd_np(dest_fd, acl, acl_type) < 0) {
499 warn("failed to set acl entries for %s", dest_path);
510 (void)fprintf(stderr, "%s\n%s\n",
511 "usage: mv [-f | -i | -n] [-hv] source target",
512 " mv [-f | -i | -n] [-v] source ... directory");