mv.c revision 92974
11556Srgrimes/*
21556Srgrimes * Copyright (c) 1989, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Ken Smith of The State University of New York at Buffalo.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 3. All advertising materials mentioning features or use of this software
171556Srgrimes *    must display the following acknowledgement:
181556Srgrimes *	This product includes software developed by the University of
191556Srgrimes *	California, Berkeley and its contributors.
201556Srgrimes * 4. Neither the name of the University nor the names of its contributors
211556Srgrimes *    may be used to endorse or promote products derived from this software
221556Srgrimes *    without specific prior written permission.
231556Srgrimes *
241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341556Srgrimes * SUCH DAMAGE.
351556Srgrimes */
361556Srgrimes
371556Srgrimes#ifndef lint
3820420Sstevestatic char const copyright[] =
391556Srgrimes"@(#) Copyright (c) 1989, 1993, 1994\n\
401556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
411556Srgrimes#endif /* not lint */
421556Srgrimes
431556Srgrimes#ifndef lint
4436049Scharnier#if 0
4536049Scharnierstatic char sccsid[] = "@(#)mv.c	8.2 (Berkeley) 4/2/94";
4636049Scharnier#endif
471556Srgrimes#endif /* not lint */
4892974Sobrien#include <sys/cdefs.h>
4992974Sobrien__FBSDID("$FreeBSD: head/bin/mv/mv.c 92974 2002-03-22 19:52:59Z obrien $");
501556Srgrimes
511556Srgrimes#include <sys/param.h>
521556Srgrimes#include <sys/time.h>
531556Srgrimes#include <sys/wait.h>
541556Srgrimes#include <sys/stat.h>
5531664Seivind#include <sys/mount.h>
561556Srgrimes
571556Srgrimes#include <err.h>
581556Srgrimes#include <errno.h>
591556Srgrimes#include <fcntl.h>
6090644Simp#include <grp.h>
6177409Simp#include <limits.h>
6290644Simp#include <pwd.h>
631556Srgrimes#include <stdio.h>
641556Srgrimes#include <stdlib.h>
651556Srgrimes#include <string.h>
6650544Smharo#include <sysexits.h>
671556Srgrimes#include <unistd.h>
681556Srgrimes
691556Srgrimes#include "pathnames.h"
701556Srgrimes
7192935Sobrienint fflg, iflg, nflg, vflg;
721556Srgrimes
7390110Simpint	copy(char *, char *);
7490110Simpint	do_move(char *, char *);
7590110Simpint	fastcopy(char *, char *, struct stat *);
7690110Simpvoid	usage(void);
771556Srgrimes
781556Srgrimesint
7990110Simpmain(int argc, char *argv[])
801556Srgrimes{
8191085Smarkm	size_t baselen, len;
8291085Smarkm	int rval;
8390114Simp	char *p, *endp;
841556Srgrimes	struct stat sb;
851556Srgrimes	int ch;
8677409Simp	char path[PATH_MAX];
871556Srgrimes
8892935Sobrien	while ((ch = getopt(argc, argv, "finv")) != -1)
891556Srgrimes		switch (ch) {
901556Srgrimes		case 'i':
9114154Swosch			iflg = 1;
9292935Sobrien			fflg = nflg = 0;
931556Srgrimes			break;
941556Srgrimes		case 'f':
951556Srgrimes			fflg = 1;
9692935Sobrien			iflg = nflg = 0;
971556Srgrimes			break;
9892935Sobrien		case 'n':
9992935Sobrien			nflg = 1;
10092935Sobrien			fflg = iflg = 0;
10192935Sobrien			break;
10250544Smharo		case 'v':
10350544Smharo			vflg = 1;
10450544Smharo			break;
1051556Srgrimes		default:
1061556Srgrimes			usage();
1071556Srgrimes		}
10814305Swosch	argc -= optind;
1091556Srgrimes	argv += optind;
1101556Srgrimes
1111556Srgrimes	if (argc < 2)
1121556Srgrimes		usage();
1131556Srgrimes
1141556Srgrimes	/*
1151556Srgrimes	 * If the stat on the target fails or the target isn't a directory,
1161556Srgrimes	 * try the move.  More than 2 arguments is an error in this case.
1171556Srgrimes	 */
1181556Srgrimes	if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
1191556Srgrimes		if (argc > 2)
1201556Srgrimes			usage();
1211556Srgrimes		exit(do_move(argv[0], argv[1]));
1221556Srgrimes	}
1231556Srgrimes
1241556Srgrimes	/* It's a directory, move each file into it. */
12536785Simp	if (strlen(argv[argc - 1]) > sizeof(path) - 1)
12636785Simp		errx(1, "%s: destination pathname too long", *argv);
1271556Srgrimes	(void)strcpy(path, argv[argc - 1]);
1281556Srgrimes	baselen = strlen(path);
1291556Srgrimes	endp = &path[baselen];
13036383Ssteve	if (!baselen || *(endp - 1) != '/') {
13136383Ssteve		*endp++ = '/';
13236383Ssteve		++baselen;
13336383Ssteve	}
1341556Srgrimes	for (rval = 0; --argc; ++argv) {
13511298Sbde		/*
13611298Sbde		 * Find the last component of the source pathname.  It
13711298Sbde		 * may have trailing slashes.
13811298Sbde		 */
13911298Sbde		p = *argv + strlen(*argv);
14011298Sbde		while (p != *argv && p[-1] == '/')
14111298Sbde			--p;
14211298Sbde		while (p != *argv && p[-1] != '/')
14311298Sbde			--p;
14411298Sbde
14577409Simp		if ((baselen + (len = strlen(p))) >= PATH_MAX) {
1461556Srgrimes			warnx("%s: destination pathname too long", *argv);
1471556Srgrimes			rval = 1;
1481556Srgrimes		} else {
14976878Skris			memmove(endp, p, (size_t)len + 1);
1501556Srgrimes			if (do_move(*argv, path))
1511556Srgrimes				rval = 1;
1521556Srgrimes		}
1531556Srgrimes	}
1541556Srgrimes	exit(rval);
1551556Srgrimes}
1561556Srgrimes
1571556Srgrimesint
15890110Simpdo_move(char *from, char *to)
1591556Srgrimes{
1601556Srgrimes	struct stat sb;
16129933Swosch	int ask, ch, first;
1621556Srgrimes	char modep[15];
1631556Srgrimes
1641556Srgrimes	/*
1651556Srgrimes	 * Check access.  If interactive and file exists, ask user if it
1661556Srgrimes	 * should be replaced.  Otherwise if file exists but isn't writable
1671556Srgrimes	 * make sure the user wants to clobber it.
1681556Srgrimes	 */
1691556Srgrimes	if (!fflg && !access(to, F_OK)) {
17014166Swosch
17114166Swosch		/* prompt only if source exist */
17214166Swosch	        if (lstat(from, &sb) == -1) {
17314305Swosch			warn("%s", from);
17414305Swosch			return (1);
17514166Swosch		}
17630106Swosch
17730106Swosch#define YESNO "(y/n [n]) "
1781556Srgrimes		ask = 0;
17992935Sobrien		if (nflg) {
18092935Sobrien			if (vflg)
18192935Sobrien				printf("%s not overwritten\n", to);
18292935Sobrien			return (0);
18392935Sobrien		} else if (iflg) {
18430106Swosch			(void)fprintf(stderr, "overwrite %s? %s", to, YESNO);
1851556Srgrimes			ask = 1;
1861556Srgrimes		} else if (access(to, W_OK) && !stat(to, &sb)) {
1871556Srgrimes			strmode(sb.st_mode, modep);
18830106Swosch			(void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
1891556Srgrimes			    modep + 1, modep[9] == ' ' ? "" : " ",
19076878Skris			    user_from_uid((unsigned long)sb.st_uid, 0),
19176878Skris			    group_from_gid((unsigned long)sb.st_gid, 0), to, YESNO);
1921556Srgrimes			ask = 1;
1931556Srgrimes		}
1941556Srgrimes		if (ask) {
19529933Swosch			first = ch = getchar();
19629933Swosch			while (ch != '\n' && ch != EOF)
19729933Swosch				ch = getchar();
19830106Swosch			if (first != 'y' && first != 'Y') {
19930106Swosch				(void)fprintf(stderr, "not overwritten\n");
2001556Srgrimes				return (0);
20130106Swosch			}
2021556Srgrimes		}
2031556Srgrimes	}
20450544Smharo	if (!rename(from, to)) {
20550544Smharo		if (vflg)
20650544Smharo			printf("%s -> %s\n", from, to);
2071556Srgrimes		return (0);
20850544Smharo	}
2091556Srgrimes
21031664Seivind	if (errno == EXDEV) {
21131664Seivind		struct statfs sfs;
21277409Simp		char path[PATH_MAX];
21331664Seivind
21431664Seivind		/* Can't mv(1) a mount point. */
21531664Seivind		if (realpath(from, path) == NULL) {
21631664Seivind			warnx("cannot resolve %s: %s", from, path);
21731664Seivind			return (1);
21831664Seivind		}
21931664Seivind		if (!statfs(path, &sfs) && !strcmp(path, sfs.f_mntonname)) {
22031664Seivind			warnx("cannot rename a mount point");
22131664Seivind			return (1);
22231664Seivind		}
22331664Seivind	} else {
2241556Srgrimes		warn("rename %s to %s", from, to);
2251556Srgrimes		return (1);
2261556Srgrimes	}
2271556Srgrimes
2281556Srgrimes	/*
2291556Srgrimes	 * If rename fails because we're trying to cross devices, and
2301556Srgrimes	 * it's a regular file, do the copy internally; otherwise, use
2311556Srgrimes	 * cp and rm.
2321556Srgrimes	 */
23362963Sdwmalone	if (lstat(from, &sb)) {
2341556Srgrimes		warn("%s", from);
2351556Srgrimes		return (1);
2361556Srgrimes	}
2371556Srgrimes	return (S_ISREG(sb.st_mode) ?
2381556Srgrimes	    fastcopy(from, to, &sb) : copy(from, to));
2391556Srgrimes}
2401556Srgrimes
2411556Srgrimesint
24290110Simpfastcopy(char *from, char *to, struct stat *sbp)
2431556Srgrimes{
2441556Srgrimes	struct timeval tval[2];
2451556Srgrimes	static u_int blen;
2461556Srgrimes	static char *bp;
24723525Sguido	mode_t oldmode;
24890114Simp	int nread, from_fd, to_fd;
2491556Srgrimes
2501556Srgrimes	if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
2511556Srgrimes		warn("%s", from);
2521556Srgrimes		return (1);
2531556Srgrimes	}
25423525Sguido	if (blen < sbp->st_blksize) {
25523525Sguido		if (bp != NULL)
25623525Sguido			free(bp);
25776878Skris		if ((bp = malloc((size_t)sbp->st_blksize)) == NULL) {
25823525Sguido			blen = 0;
25923525Sguido			warnx("malloc failed");
26023525Sguido			return (1);
26123525Sguido		}
26223525Sguido		blen = sbp->st_blksize;
26323525Sguido	}
26423525Sguido	while ((to_fd =
26523525Sguido	    open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
26623525Sguido		if (errno == EEXIST && unlink(to) == 0)
26723525Sguido			continue;
2681556Srgrimes		warn("%s", to);
2691556Srgrimes		(void)close(from_fd);
2701556Srgrimes		return (1);
2711556Srgrimes	}
27276878Skris	while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
27376878Skris		if (write(to_fd, bp, (size_t)nread) != nread) {
2741556Srgrimes			warn("%s", to);
2751556Srgrimes			goto err;
2761556Srgrimes		}
2771556Srgrimes	if (nread < 0) {
2781556Srgrimes		warn("%s", from);
2791556Srgrimeserr:		if (unlink(to))
2801556Srgrimes			warn("%s: remove", to);
2811556Srgrimes		(void)close(from_fd);
2821556Srgrimes		(void)close(to_fd);
2831556Srgrimes		return (1);
2841556Srgrimes	}
2851556Srgrimes	(void)close(from_fd);
2861556Srgrimes
28723525Sguido	oldmode = sbp->st_mode & ALLPERMS;
28823525Sguido	if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
28937245Sbde		warn("%s: set owner/group (was: %lu/%lu)", to,
29037245Sbde		    (u_long)sbp->st_uid, (u_long)sbp->st_gid);
29123525Sguido		if (oldmode & (S_ISUID | S_ISGID)) {
29223525Sguido			warnx(
29323525Sguido"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
29423525Sguido			    to, oldmode);
29523525Sguido			sbp->st_mode &= ~(S_ISUID | S_ISGID);
29623525Sguido		}
29723525Sguido	}
2981556Srgrimes	if (fchmod(to_fd, sbp->st_mode))
29923525Sguido		warn("%s: set mode (was: 0%03o)", to, oldmode);
30063680Ssada	/*
30163680Ssada	 * XXX
30263680Ssada	 * NFS doesn't support chflags; ignore errors unless there's reason
30363680Ssada	 * to believe we're losing bits.  (Note, this still won't be right
30463680Ssada	 * if the server supports flags and we were trying to *remove* flags
30563680Ssada	 * on a file that we copied, i.e., that we didn't create.)
30663680Ssada	 */
30763680Ssada	errno = 0;
30876878Skris	if (fchflags(to_fd, (u_long)sbp->st_flags))
30963680Ssada		if (errno != EOPNOTSUPP || sbp->st_flags != 0)
31063680Ssada			warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
3111556Srgrimes
3121556Srgrimes	tval[0].tv_sec = sbp->st_atime;
3131556Srgrimes	tval[1].tv_sec = sbp->st_mtime;
3141556Srgrimes	tval[0].tv_usec = tval[1].tv_usec = 0;
3151556Srgrimes	if (utimes(to, tval))
3161556Srgrimes		warn("%s: set times", to);
3171556Srgrimes
3181556Srgrimes	if (close(to_fd)) {
3191556Srgrimes		warn("%s", to);
3201556Srgrimes		return (1);
3211556Srgrimes	}
3221556Srgrimes
3231556Srgrimes	if (unlink(from)) {
3241556Srgrimes		warn("%s: remove", from);
3251556Srgrimes		return (1);
3261556Srgrimes	}
32750544Smharo	if (vflg)
32850544Smharo		printf("%s -> %s\n", from, to);
3291556Srgrimes	return (0);
3301556Srgrimes}
3311556Srgrimes
3321556Srgrimesint
33390110Simpcopy(char *from, char *to)
3341556Srgrimes{
3351556Srgrimes	int pid, status;
3361556Srgrimes
33740301Sdes	if ((pid = fork()) == 0) {
33879452Sbrian		execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", from, to,
33979452Sbrian		    (char *)NULL);
3401556Srgrimes		warn("%s", _PATH_CP);
3411556Srgrimes		_exit(1);
3421556Srgrimes	}
3431556Srgrimes	if (waitpid(pid, &status, 0) == -1) {
3441556Srgrimes		warn("%s: waitpid", _PATH_CP);
3451556Srgrimes		return (1);
3461556Srgrimes	}
3471556Srgrimes	if (!WIFEXITED(status)) {
3481556Srgrimes		warn("%s: did not terminate normally", _PATH_CP);
3491556Srgrimes		return (1);
3501556Srgrimes	}
3511556Srgrimes	if (WEXITSTATUS(status)) {
3521556Srgrimes		warn("%s: terminated with %d (non-zero) status",
3531556Srgrimes		    _PATH_CP, WEXITSTATUS(status));
3541556Srgrimes		return (1);
3551556Srgrimes	}
3561556Srgrimes	if (!(pid = vfork())) {
35779452Sbrian		execl(_PATH_RM, "mv", "-rf", from, (char *)NULL);
3581556Srgrimes		warn("%s", _PATH_RM);
3591556Srgrimes		_exit(1);
3601556Srgrimes	}
3611556Srgrimes	if (waitpid(pid, &status, 0) == -1) {
3621556Srgrimes		warn("%s: waitpid", _PATH_RM);
3631556Srgrimes		return (1);
3641556Srgrimes	}
3651556Srgrimes	if (!WIFEXITED(status)) {
3661556Srgrimes		warn("%s: did not terminate normally", _PATH_RM);
3671556Srgrimes		return (1);
3681556Srgrimes	}
3691556Srgrimes	if (WEXITSTATUS(status)) {
3701556Srgrimes		warn("%s: terminated with %d (non-zero) status",
3711556Srgrimes		    _PATH_RM, WEXITSTATUS(status));
3721556Srgrimes		return (1);
3731556Srgrimes	}
3741556Srgrimes	return (0);
3751556Srgrimes}
3761556Srgrimes
3771556Srgrimesvoid
37890110Simpusage(void)
3791556Srgrimes{
38050544Smharo
38114305Swosch	(void)fprintf(stderr, "%s\n%s\n",
38250544Smharo		      "usage: mv [-f | -i] [-v] source target",
38350544Smharo		      "       mv [-f | -i] [-v] source ... directory");
38450544Smharo	exit(EX_USAGE);
3851556Srgrimes}
386