mv.c revision 98280
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 98280 2002-06-16 04:06:58Z tjr $");
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>
6296806Sjmallett#include <paths.h>
6390644Simp#include <pwd.h>
641556Srgrimes#include <stdio.h>
651556Srgrimes#include <stdlib.h>
661556Srgrimes#include <string.h>
6750544Smharo#include <sysexits.h>
681556Srgrimes#include <unistd.h>
691556Srgrimes
701556Srgrimes#include "pathnames.h"
711556Srgrimes
7292935Sobrienint fflg, iflg, nflg, vflg;
731556Srgrimes
7490110Simpint	copy(char *, char *);
7590110Simpint	do_move(char *, char *);
7690110Simpint	fastcopy(char *, char *, struct stat *);
7790110Simpvoid	usage(void);
781556Srgrimes
791556Srgrimesint
8090110Simpmain(int argc, char *argv[])
811556Srgrimes{
8291085Smarkm	size_t baselen, len;
8391085Smarkm	int rval;
8490114Simp	char *p, *endp;
851556Srgrimes	struct stat sb;
861556Srgrimes	int ch;
8777409Simp	char path[PATH_MAX];
881556Srgrimes
8992935Sobrien	while ((ch = getopt(argc, argv, "finv")) != -1)
901556Srgrimes		switch (ch) {
911556Srgrimes		case 'i':
9214154Swosch			iflg = 1;
9392935Sobrien			fflg = nflg = 0;
941556Srgrimes			break;
951556Srgrimes		case 'f':
961556Srgrimes			fflg = 1;
9792935Sobrien			iflg = nflg = 0;
981556Srgrimes			break;
9992935Sobrien		case 'n':
10092935Sobrien			nflg = 1;
10192935Sobrien			fflg = iflg = 0;
10292935Sobrien			break;
10350544Smharo		case 'v':
10450544Smharo			vflg = 1;
10550544Smharo			break;
1061556Srgrimes		default:
1071556Srgrimes			usage();
1081556Srgrimes		}
10914305Swosch	argc -= optind;
1101556Srgrimes	argv += optind;
1111556Srgrimes
1121556Srgrimes	if (argc < 2)
1131556Srgrimes		usage();
1141556Srgrimes
1151556Srgrimes	/*
1161556Srgrimes	 * If the stat on the target fails or the target isn't a directory,
1171556Srgrimes	 * try the move.  More than 2 arguments is an error in this case.
1181556Srgrimes	 */
1191556Srgrimes	if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
1201556Srgrimes		if (argc > 2)
1211556Srgrimes			usage();
1221556Srgrimes		exit(do_move(argv[0], argv[1]));
1231556Srgrimes	}
1241556Srgrimes
1251556Srgrimes	/* It's a directory, move each file into it. */
12636785Simp	if (strlen(argv[argc - 1]) > sizeof(path) - 1)
12736785Simp		errx(1, "%s: destination pathname too long", *argv);
1281556Srgrimes	(void)strcpy(path, argv[argc - 1]);
1291556Srgrimes	baselen = strlen(path);
1301556Srgrimes	endp = &path[baselen];
13136383Ssteve	if (!baselen || *(endp - 1) != '/') {
13236383Ssteve		*endp++ = '/';
13336383Ssteve		++baselen;
13436383Ssteve	}
1351556Srgrimes	for (rval = 0; --argc; ++argv) {
13611298Sbde		/*
13711298Sbde		 * Find the last component of the source pathname.  It
13811298Sbde		 * may have trailing slashes.
13911298Sbde		 */
14011298Sbde		p = *argv + strlen(*argv);
14111298Sbde		while (p != *argv && p[-1] == '/')
14211298Sbde			--p;
14311298Sbde		while (p != *argv && p[-1] != '/')
14411298Sbde			--p;
14511298Sbde
14677409Simp		if ((baselen + (len = strlen(p))) >= PATH_MAX) {
1471556Srgrimes			warnx("%s: destination pathname too long", *argv);
1481556Srgrimes			rval = 1;
1491556Srgrimes		} else {
15076878Skris			memmove(endp, p, (size_t)len + 1);
1511556Srgrimes			if (do_move(*argv, path))
1521556Srgrimes				rval = 1;
1531556Srgrimes		}
1541556Srgrimes	}
1551556Srgrimes	exit(rval);
1561556Srgrimes}
1571556Srgrimes
1581556Srgrimesint
15990110Simpdo_move(char *from, char *to)
1601556Srgrimes{
1611556Srgrimes	struct stat sb;
16229933Swosch	int ask, ch, first;
1631556Srgrimes	char modep[15];
1641556Srgrimes
1651556Srgrimes	/*
1661556Srgrimes	 * Check access.  If interactive and file exists, ask user if it
1671556Srgrimes	 * should be replaced.  Otherwise if file exists but isn't writable
1681556Srgrimes	 * make sure the user wants to clobber it.
1691556Srgrimes	 */
1701556Srgrimes	if (!fflg && !access(to, F_OK)) {
17114166Swosch
17214166Swosch		/* prompt only if source exist */
17314166Swosch	        if (lstat(from, &sb) == -1) {
17414305Swosch			warn("%s", from);
17514305Swosch			return (1);
17614166Swosch		}
17730106Swosch
17830106Swosch#define YESNO "(y/n [n]) "
1791556Srgrimes		ask = 0;
18092935Sobrien		if (nflg) {
18192935Sobrien			if (vflg)
18292935Sobrien				printf("%s not overwritten\n", to);
18392935Sobrien			return (0);
18492935Sobrien		} else if (iflg) {
18530106Swosch			(void)fprintf(stderr, "overwrite %s? %s", to, YESNO);
1861556Srgrimes			ask = 1;
1871556Srgrimes		} else if (access(to, W_OK) && !stat(to, &sb)) {
1881556Srgrimes			strmode(sb.st_mode, modep);
18930106Swosch			(void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
1901556Srgrimes			    modep + 1, modep[9] == ' ' ? "" : " ",
19176878Skris			    user_from_uid((unsigned long)sb.st_uid, 0),
19276878Skris			    group_from_gid((unsigned long)sb.st_gid, 0), to, YESNO);
1931556Srgrimes			ask = 1;
1941556Srgrimes		}
1951556Srgrimes		if (ask) {
19629933Swosch			first = ch = getchar();
19729933Swosch			while (ch != '\n' && ch != EOF)
19829933Swosch				ch = getchar();
19930106Swosch			if (first != 'y' && first != 'Y') {
20030106Swosch				(void)fprintf(stderr, "not overwritten\n");
2011556Srgrimes				return (0);
20230106Swosch			}
2031556Srgrimes		}
2041556Srgrimes	}
20550544Smharo	if (!rename(from, to)) {
20650544Smharo		if (vflg)
20750544Smharo			printf("%s -> %s\n", from, to);
2081556Srgrimes		return (0);
20950544Smharo	}
2101556Srgrimes
21131664Seivind	if (errno == EXDEV) {
21231664Seivind		struct statfs sfs;
21377409Simp		char path[PATH_MAX];
21431664Seivind
21531664Seivind		/* Can't mv(1) a mount point. */
21631664Seivind		if (realpath(from, path) == NULL) {
21731664Seivind			warnx("cannot resolve %s: %s", from, path);
21831664Seivind			return (1);
21931664Seivind		}
22031664Seivind		if (!statfs(path, &sfs) && !strcmp(path, sfs.f_mntonname)) {
22131664Seivind			warnx("cannot rename a mount point");
22231664Seivind			return (1);
22331664Seivind		}
22431664Seivind	} else {
2251556Srgrimes		warn("rename %s to %s", from, to);
2261556Srgrimes		return (1);
2271556Srgrimes	}
2281556Srgrimes
2291556Srgrimes	/*
2301556Srgrimes	 * If rename fails because we're trying to cross devices, and
2311556Srgrimes	 * it's a regular file, do the copy internally; otherwise, use
2321556Srgrimes	 * cp and rm.
2331556Srgrimes	 */
23462963Sdwmalone	if (lstat(from, &sb)) {
2351556Srgrimes		warn("%s", from);
2361556Srgrimes		return (1);
2371556Srgrimes	}
2381556Srgrimes	return (S_ISREG(sb.st_mode) ?
2391556Srgrimes	    fastcopy(from, to, &sb) : copy(from, to));
2401556Srgrimes}
2411556Srgrimes
2421556Srgrimesint
24390110Simpfastcopy(char *from, char *to, struct stat *sbp)
2441556Srgrimes{
2451556Srgrimes	struct timeval tval[2];
2461556Srgrimes	static u_int blen;
2471556Srgrimes	static char *bp;
24823525Sguido	mode_t oldmode;
24990114Simp	int nread, from_fd, to_fd;
2501556Srgrimes
2511556Srgrimes	if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
2521556Srgrimes		warn("%s", from);
2531556Srgrimes		return (1);
2541556Srgrimes	}
25523525Sguido	if (blen < sbp->st_blksize) {
25623525Sguido		if (bp != NULL)
25723525Sguido			free(bp);
25876878Skris		if ((bp = malloc((size_t)sbp->st_blksize)) == NULL) {
25923525Sguido			blen = 0;
26023525Sguido			warnx("malloc failed");
26123525Sguido			return (1);
26223525Sguido		}
26323525Sguido		blen = sbp->st_blksize;
26423525Sguido	}
26523525Sguido	while ((to_fd =
26623525Sguido	    open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
26723525Sguido		if (errno == EEXIST && unlink(to) == 0)
26823525Sguido			continue;
2691556Srgrimes		warn("%s", to);
2701556Srgrimes		(void)close(from_fd);
2711556Srgrimes		return (1);
2721556Srgrimes	}
27376878Skris	while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
27476878Skris		if (write(to_fd, bp, (size_t)nread) != nread) {
2751556Srgrimes			warn("%s", to);
2761556Srgrimes			goto err;
2771556Srgrimes		}
2781556Srgrimes	if (nread < 0) {
2791556Srgrimes		warn("%s", from);
2801556Srgrimeserr:		if (unlink(to))
2811556Srgrimes			warn("%s: remove", to);
2821556Srgrimes		(void)close(from_fd);
2831556Srgrimes		(void)close(to_fd);
2841556Srgrimes		return (1);
2851556Srgrimes	}
2861556Srgrimes	(void)close(from_fd);
2871556Srgrimes
28823525Sguido	oldmode = sbp->st_mode & ALLPERMS;
28923525Sguido	if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
29037245Sbde		warn("%s: set owner/group (was: %lu/%lu)", to,
29137245Sbde		    (u_long)sbp->st_uid, (u_long)sbp->st_gid);
29223525Sguido		if (oldmode & (S_ISUID | S_ISGID)) {
29323525Sguido			warnx(
29423525Sguido"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
29523525Sguido			    to, oldmode);
29623525Sguido			sbp->st_mode &= ~(S_ISUID | S_ISGID);
29723525Sguido		}
29823525Sguido	}
2991556Srgrimes	if (fchmod(to_fd, sbp->st_mode))
30023525Sguido		warn("%s: set mode (was: 0%03o)", to, oldmode);
30163680Ssada	/*
30263680Ssada	 * XXX
30363680Ssada	 * NFS doesn't support chflags; ignore errors unless there's reason
30463680Ssada	 * to believe we're losing bits.  (Note, this still won't be right
30563680Ssada	 * if the server supports flags and we were trying to *remove* flags
30663680Ssada	 * on a file that we copied, i.e., that we didn't create.)
30763680Ssada	 */
30863680Ssada	errno = 0;
30976878Skris	if (fchflags(to_fd, (u_long)sbp->st_flags))
31063680Ssada		if (errno != EOPNOTSUPP || sbp->st_flags != 0)
31163680Ssada			warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
3121556Srgrimes
3131556Srgrimes	tval[0].tv_sec = sbp->st_atime;
3141556Srgrimes	tval[1].tv_sec = sbp->st_mtime;
3151556Srgrimes	tval[0].tv_usec = tval[1].tv_usec = 0;
3161556Srgrimes	if (utimes(to, tval))
3171556Srgrimes		warn("%s: set times", to);
3181556Srgrimes
3191556Srgrimes	if (close(to_fd)) {
3201556Srgrimes		warn("%s", to);
3211556Srgrimes		return (1);
3221556Srgrimes	}
3231556Srgrimes
3241556Srgrimes	if (unlink(from)) {
3251556Srgrimes		warn("%s: remove", from);
3261556Srgrimes		return (1);
3271556Srgrimes	}
32850544Smharo	if (vflg)
32950544Smharo		printf("%s -> %s\n", from, to);
3301556Srgrimes	return (0);
3311556Srgrimes}
3321556Srgrimes
3331556Srgrimesint
33490110Simpcopy(char *from, char *to)
3351556Srgrimes{
3361556Srgrimes	int pid, status;
3371556Srgrimes
33840301Sdes	if ((pid = fork()) == 0) {
33998280Stjr		execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to,
34079452Sbrian		    (char *)NULL);
3411556Srgrimes		warn("%s", _PATH_CP);
3421556Srgrimes		_exit(1);
3431556Srgrimes	}
3441556Srgrimes	if (waitpid(pid, &status, 0) == -1) {
3451556Srgrimes		warn("%s: waitpid", _PATH_CP);
3461556Srgrimes		return (1);
3471556Srgrimes	}
3481556Srgrimes	if (!WIFEXITED(status)) {
3491556Srgrimes		warn("%s: did not terminate normally", _PATH_CP);
3501556Srgrimes		return (1);
3511556Srgrimes	}
3521556Srgrimes	if (WEXITSTATUS(status)) {
3531556Srgrimes		warn("%s: terminated with %d (non-zero) status",
3541556Srgrimes		    _PATH_CP, WEXITSTATUS(status));
3551556Srgrimes		return (1);
3561556Srgrimes	}
3571556Srgrimes	if (!(pid = vfork())) {
35898280Stjr		execl(_PATH_RM, "mv", "-rf", "--", from, (char *)NULL);
3591556Srgrimes		warn("%s", _PATH_RM);
3601556Srgrimes		_exit(1);
3611556Srgrimes	}
3621556Srgrimes	if (waitpid(pid, &status, 0) == -1) {
3631556Srgrimes		warn("%s: waitpid", _PATH_RM);
3641556Srgrimes		return (1);
3651556Srgrimes	}
3661556Srgrimes	if (!WIFEXITED(status)) {
3671556Srgrimes		warn("%s: did not terminate normally", _PATH_RM);
3681556Srgrimes		return (1);
3691556Srgrimes	}
3701556Srgrimes	if (WEXITSTATUS(status)) {
3711556Srgrimes		warn("%s: terminated with %d (non-zero) status",
3721556Srgrimes		    _PATH_RM, WEXITSTATUS(status));
3731556Srgrimes		return (1);
3741556Srgrimes	}
3751556Srgrimes	return (0);
3761556Srgrimes}
3771556Srgrimes
3781556Srgrimesvoid
37990110Simpusage(void)
3801556Srgrimes{
38150544Smharo
38214305Swosch	(void)fprintf(stderr, "%s\n%s\n",
38350544Smharo		      "usage: mv [-f | -i] [-v] source target",
38450544Smharo		      "       mv [-f | -i] [-v] source ... directory");
38550544Smharo	exit(EX_USAGE);
3861556Srgrimes}
387