2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Ken Smith of The State University of New York at Buffalo.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
35 static char const copyright[] =
36 "@(#) Copyright (c) 1989, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/types.h>
49 #include <sys/param.h>
53 #include <sys/mount.h>
68 /* Exit code for a failed exec. */
69 #define EXEC_FAILED 127
71 int fflg, iflg, nflg, vflg;
73 static int copy(const char *, const char *);
74 static int do_move(const char *, const char *);
75 static int fastcopy(const char *, const char *, struct stat *);
76 static void usage(void);
77 static void preserve_fd_acls(int source_fd, int dest_fd, const char *source_path,
78 const char *dest_path);
81 main(int argc, char *argv[])
90 while ((ch = getopt(argc, argv, "finv")) != -1)
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.
120 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
123 exit(do_move(argv[0], argv[1]));
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) != '/') {
136 for (rval = 0; --argc; ++argv) {
138 * Find the last component of the source pathname. It
139 * may have trailing slashes.
141 p = *argv + strlen(*argv);
142 while (p != *argv && p[-1] == '/')
144 while (p != *argv && p[-1] != '/')
147 if ((baselen + (len = strlen(p))) >= PATH_MAX) {
148 warnx("%s: destination pathname too long", *argv);
151 memmove(endp, p, (size_t)len + 1);
152 if (do_move(*argv, path))
160 do_move(const char *from, const char *to)
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.
171 if (!fflg && !access(to, F_OK)) {
173 /* prompt only if source exist */
174 if (lstat(from, &sb) == -1) {
179 #define YESNO "(y/n [n]) "
183 printf("%s not overwritten\n", to);
186 (void)fprintf(stderr, "overwrite %s? %s", to, YESNO);
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);
197 first = ch = getchar();
198 while (ch != '\n' && ch != EOF)
200 if (first != 'y' && first != 'Y') {
201 (void)fprintf(stderr, "not overwritten\n");
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.
211 if (!rename(from, to)) {
213 printf("%s -> %s\n", from, to);
217 if (errno == EXDEV) {
222 * If the source is a symbolic link and is on another
223 * filesystem, it can be recreated at the destination.
225 if (lstat(from, &sb) == -1) {
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);
235 if (!statfs(path, &sfs) &&
236 !strcmp(path, sfs.f_mntonname)) {
237 warnx("cannot rename a mount point");
242 warn("rename %s to %s", from, to);
247 * If rename fails because we're trying to cross devices, and
248 * it's a regular file, do the copy internally; otherwise, use
251 if (lstat(from, &sb)) {
255 return (S_ISREG(sb.st_mode) ?
256 fastcopy(from, to, &sb) : copy(from, to));
260 fastcopy(const char *from, const char *to, struct stat *sbp)
262 struct timeval tval[2];
263 static u_int blen = MAXPHYS;
264 static char *bp = NULL;
266 int nread, from_fd, to_fd;
268 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
269 warn("fastcopy: open() failed (from): %s", from);
272 if (bp == NULL && (bp = malloc((size_t)blen)) == NULL) {
273 warnx("malloc(%u) failed", blen);
277 open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
278 if (errno == EEXIST && unlink(to) == 0)
280 warn("fastcopy: open() failed (to): %s", to);
281 (void)close(from_fd);
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);
290 warn("fastcopy: read() failed: %s", from);
292 warn("%s: remove", to);
293 (void)close(from_fd);
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)) {
304 "%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
306 sbp->st_mode &= ~(S_ISUID | S_ISGID);
309 if (fchmod(to_fd, sbp->st_mode))
310 warn("%s: set mode (was: 0%03o)", to, oldmode);
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
316 preserve_fd_acls(from_fd, to_fd, from, to);
317 (void)close(from_fd);
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.)
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);
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);
342 warn("%s: remove", from);
346 printf("%s -> %s\n", from, to);
351 copy(const char *from, const char *to)
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);
364 if (unlink(to) != 0) {
365 warn("unlink %s", to);
369 } else if (errno != ENOENT) {
374 /* Copy source to destination. */
375 if (!(pid = vfork())) {
376 execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to,
380 if (waitpid(pid, &status, 0) == -1) {
381 warn("%s %s %s: waitpid", _PATH_CP, from, to);
384 if (!WIFEXITED(status)) {
385 warnx("%s %s %s: did not terminate normally",
389 switch (WEXITSTATUS(status)) {
393 warnx("%s %s %s: exec failed", _PATH_CP, from, to);
396 warnx("%s %s %s: terminated with %d (non-zero) status",
397 _PATH_CP, from, to, WEXITSTATUS(status));
401 /* Delete the source. */
402 if (!(pid = vfork())) {
403 execl(_PATH_RM, "mv", "-rf", "--", from, (char *)NULL);
406 if (waitpid(pid, &status, 0) == -1) {
407 warn("%s %s: waitpid", _PATH_RM, from);
410 if (!WIFEXITED(status)) {
411 warnx("%s %s: did not terminate normally", _PATH_RM, from);
414 switch (WEXITSTATUS(status)) {
418 warnx("%s %s: exec failed", _PATH_RM, from);
421 warnx("%s %s: terminated with %d (non-zero) status",
422 _PATH_RM, from, WEXITSTATUS(status));
429 preserve_fd_acls(int source_fd, int dest_fd, const char *source_path,
430 const char *dest_path)
434 int acl_supported = 0, ret, trivial;
436 ret = fpathconf(source_fd, _PC_ACL_NFS4);
439 acl_type = ACL_TYPE_NFS4;
440 } else if (ret < 0 && errno != EINVAL) {
441 warn("fpathconf(..., _PC_ACL_NFS4) failed for %s",
445 if (acl_supported == 0) {
446 ret = fpathconf(source_fd, _PC_ACL_EXTENDED);
449 acl_type = ACL_TYPE_ACCESS;
450 } else if (ret < 0 && errno != EINVAL) {
451 warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
456 if (acl_supported == 0)
459 acl = acl_get_fd_np(source_fd, acl_type);
461 warn("failed to get acl entries for %s", source_path);
464 if (acl_is_trivial_np(acl, &trivial)) {
465 warn("acl_is_trivial() failed for %s", source_path);
473 if (acl_set_fd_np(dest_fd, acl, acl_type) < 0) {
474 warn("failed to set acl entries for %s", dest_path);
485 (void)fprintf(stderr, "%s\n%s\n",
486 "usage: mv [-f | -i | -n] [-v] source target",
487 " mv [-f | -i | -n] [-v] source ... directory");