mv.c revision 76878
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
4736049Scharnierstatic const char rcsid[] =
4850471Speter  "$FreeBSD: head/bin/mv/mv.c 76878 2001-05-20 05:00:16Z kris $";
491556Srgrimes#endif /* not lint */
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>
601556Srgrimes#include <stdio.h>
611556Srgrimes#include <stdlib.h>
621556Srgrimes#include <string.h>
6350544Smharo#include <sysexits.h>
641556Srgrimes#include <unistd.h>
651556Srgrimes
661556Srgrimes#include "pathnames.h"
671556Srgrimes
6850544Smharoint fflg, iflg, vflg;
691556Srgrimes
701556Srgrimesint	copy __P((char *, char *));
711556Srgrimesint	do_move __P((char *, char *));
721556Srgrimesint	fastcopy __P((char *, char *, struct stat *));
7376878Skrisint	main __P((int, char *[]));
741556Srgrimesvoid	usage __P((void));
751556Srgrimes
761556Srgrimesint
771556Srgrimesmain(argc, argv)
781556Srgrimes	int argc;
791556Srgrimes	char *argv[];
801556Srgrimes{
811556Srgrimes	register int baselen, len, rval;
821556Srgrimes	register char *p, *endp;
831556Srgrimes	struct stat sb;
841556Srgrimes	int ch;
8536785Simp	char path[MAXPATHLEN];
861556Srgrimes
8750544Smharo	while ((ch = getopt(argc, argv, "fiv")) != -1)
881556Srgrimes		switch (ch) {
891556Srgrimes		case 'i':
9014154Swosch			iflg = 1;
9114166Swosch			fflg = 0;
921556Srgrimes			break;
931556Srgrimes		case 'f':
941556Srgrimes			fflg = 1;
9514166Swosch			iflg = 0;
961556Srgrimes			break;
9750544Smharo		case 'v':
9850544Smharo			vflg = 1;
9950544Smharo			break;
1001556Srgrimes		default:
1011556Srgrimes			usage();
1021556Srgrimes		}
10314305Swosch	argc -= optind;
1041556Srgrimes	argv += optind;
1051556Srgrimes
1061556Srgrimes	if (argc < 2)
1071556Srgrimes		usage();
1081556Srgrimes
1091556Srgrimes	/*
1101556Srgrimes	 * If the stat on the target fails or the target isn't a directory,
1111556Srgrimes	 * try the move.  More than 2 arguments is an error in this case.
1121556Srgrimes	 */
1131556Srgrimes	if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
1141556Srgrimes		if (argc > 2)
1151556Srgrimes			usage();
1161556Srgrimes		exit(do_move(argv[0], argv[1]));
1171556Srgrimes	}
1181556Srgrimes
1191556Srgrimes	/* It's a directory, move each file into it. */
12036785Simp	if (strlen(argv[argc - 1]) > sizeof(path) - 1)
12136785Simp		errx(1, "%s: destination pathname too long", *argv);
1221556Srgrimes	(void)strcpy(path, argv[argc - 1]);
1231556Srgrimes	baselen = strlen(path);
1241556Srgrimes	endp = &path[baselen];
12536383Ssteve	if (!baselen || *(endp - 1) != '/') {
12636383Ssteve		*endp++ = '/';
12736383Ssteve		++baselen;
12836383Ssteve	}
1291556Srgrimes	for (rval = 0; --argc; ++argv) {
13011298Sbde		/*
13111298Sbde		 * Find the last component of the source pathname.  It
13211298Sbde		 * may have trailing slashes.
13311298Sbde		 */
13411298Sbde		p = *argv + strlen(*argv);
13511298Sbde		while (p != *argv && p[-1] == '/')
13611298Sbde			--p;
13711298Sbde		while (p != *argv && p[-1] != '/')
13811298Sbde			--p;
13911298Sbde
1401556Srgrimes		if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
1411556Srgrimes			warnx("%s: destination pathname too long", *argv);
1421556Srgrimes			rval = 1;
1431556Srgrimes		} else {
14476878Skris			memmove(endp, p, (size_t)len + 1);
1451556Srgrimes			if (do_move(*argv, path))
1461556Srgrimes				rval = 1;
1471556Srgrimes		}
1481556Srgrimes	}
1491556Srgrimes	exit(rval);
1501556Srgrimes}
1511556Srgrimes
1521556Srgrimesint
1531556Srgrimesdo_move(from, to)
1541556Srgrimes	char *from, *to;
1551556Srgrimes{
1561556Srgrimes	struct stat sb;
15729933Swosch	int ask, ch, first;
1581556Srgrimes	char modep[15];
1591556Srgrimes
1601556Srgrimes	/*
1611556Srgrimes	 * Check access.  If interactive and file exists, ask user if it
1621556Srgrimes	 * should be replaced.  Otherwise if file exists but isn't writable
1631556Srgrimes	 * make sure the user wants to clobber it.
1641556Srgrimes	 */
1651556Srgrimes	if (!fflg && !access(to, F_OK)) {
16614166Swosch
16714166Swosch		/* prompt only if source exist */
16814166Swosch	        if (lstat(from, &sb) == -1) {
16914305Swosch			warn("%s", from);
17014305Swosch			return (1);
17114166Swosch		}
17230106Swosch
17330106Swosch#define YESNO "(y/n [n]) "
1741556Srgrimes		ask = 0;
1751556Srgrimes		if (iflg) {
17630106Swosch			(void)fprintf(stderr, "overwrite %s? %s", to, YESNO);
1771556Srgrimes			ask = 1;
1781556Srgrimes		} else if (access(to, W_OK) && !stat(to, &sb)) {
1791556Srgrimes			strmode(sb.st_mode, modep);
18030106Swosch			(void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
1811556Srgrimes			    modep + 1, modep[9] == ' ' ? "" : " ",
18276878Skris			    user_from_uid((unsigned long)sb.st_uid, 0),
18376878Skris			    group_from_gid((unsigned long)sb.st_gid, 0), to, YESNO);
1841556Srgrimes			ask = 1;
1851556Srgrimes		}
1861556Srgrimes		if (ask) {
18729933Swosch			first = ch = getchar();
18829933Swosch			while (ch != '\n' && ch != EOF)
18929933Swosch				ch = getchar();
19030106Swosch			if (first != 'y' && first != 'Y') {
19130106Swosch				(void)fprintf(stderr, "not overwritten\n");
1921556Srgrimes				return (0);
19330106Swosch			}
1941556Srgrimes		}
1951556Srgrimes	}
19650544Smharo	if (!rename(from, to)) {
19750544Smharo		if (vflg)
19850544Smharo			printf("%s -> %s\n", from, to);
1991556Srgrimes		return (0);
20050544Smharo	}
2011556Srgrimes
20231664Seivind	if (errno == EXDEV) {
20331664Seivind		struct statfs sfs;
20431664Seivind		char path[MAXPATHLEN];
20531664Seivind
20631664Seivind		/* Can't mv(1) a mount point. */
20731664Seivind		if (realpath(from, path) == NULL) {
20831664Seivind			warnx("cannot resolve %s: %s", from, path);
20931664Seivind			return (1);
21031664Seivind		}
21131664Seivind		if (!statfs(path, &sfs) && !strcmp(path, sfs.f_mntonname)) {
21231664Seivind			warnx("cannot rename a mount point");
21331664Seivind			return (1);
21431664Seivind		}
21531664Seivind	} else {
2161556Srgrimes		warn("rename %s to %s", from, to);
2171556Srgrimes		return (1);
2181556Srgrimes	}
2191556Srgrimes
2201556Srgrimes	/*
2211556Srgrimes	 * If rename fails because we're trying to cross devices, and
2221556Srgrimes	 * it's a regular file, do the copy internally; otherwise, use
2231556Srgrimes	 * cp and rm.
2241556Srgrimes	 */
22562963Sdwmalone	if (lstat(from, &sb)) {
2261556Srgrimes		warn("%s", from);
2271556Srgrimes		return (1);
2281556Srgrimes	}
2291556Srgrimes	return (S_ISREG(sb.st_mode) ?
2301556Srgrimes	    fastcopy(from, to, &sb) : copy(from, to));
2311556Srgrimes}
2321556Srgrimes
2331556Srgrimesint
2341556Srgrimesfastcopy(from, to, sbp)
2351556Srgrimes	char *from, *to;
2361556Srgrimes	struct stat *sbp;
2371556Srgrimes{
2381556Srgrimes	struct timeval tval[2];
2391556Srgrimes	static u_int blen;
2401556Srgrimes	static char *bp;
24123525Sguido	mode_t oldmode;
2421556Srgrimes	register int nread, from_fd, to_fd;
2431556Srgrimes
2441556Srgrimes	if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
2451556Srgrimes		warn("%s", from);
2461556Srgrimes		return (1);
2471556Srgrimes	}
24823525Sguido	if (blen < sbp->st_blksize) {
24923525Sguido		if (bp != NULL)
25023525Sguido			free(bp);
25176878Skris		if ((bp = malloc((size_t)sbp->st_blksize)) == NULL) {
25223525Sguido			blen = 0;
25323525Sguido			warnx("malloc failed");
25423525Sguido			return (1);
25523525Sguido		}
25623525Sguido		blen = sbp->st_blksize;
25723525Sguido	}
25823525Sguido	while ((to_fd =
25923525Sguido	    open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
26023525Sguido		if (errno == EEXIST && unlink(to) == 0)
26123525Sguido			continue;
2621556Srgrimes		warn("%s", to);
2631556Srgrimes		(void)close(from_fd);
2641556Srgrimes		return (1);
2651556Srgrimes	}
26676878Skris	while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
26776878Skris		if (write(to_fd, bp, (size_t)nread) != nread) {
2681556Srgrimes			warn("%s", to);
2691556Srgrimes			goto err;
2701556Srgrimes		}
2711556Srgrimes	if (nread < 0) {
2721556Srgrimes		warn("%s", from);
2731556Srgrimeserr:		if (unlink(to))
2741556Srgrimes			warn("%s: remove", to);
2751556Srgrimes		(void)close(from_fd);
2761556Srgrimes		(void)close(to_fd);
2771556Srgrimes		return (1);
2781556Srgrimes	}
2791556Srgrimes	(void)close(from_fd);
2801556Srgrimes
28123525Sguido	oldmode = sbp->st_mode & ALLPERMS;
28223525Sguido	if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
28337245Sbde		warn("%s: set owner/group (was: %lu/%lu)", to,
28437245Sbde		    (u_long)sbp->st_uid, (u_long)sbp->st_gid);
28523525Sguido		if (oldmode & (S_ISUID | S_ISGID)) {
28623525Sguido			warnx(
28723525Sguido"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
28823525Sguido			    to, oldmode);
28923525Sguido			sbp->st_mode &= ~(S_ISUID | S_ISGID);
29023525Sguido		}
29123525Sguido	}
2921556Srgrimes	if (fchmod(to_fd, sbp->st_mode))
29323525Sguido		warn("%s: set mode (was: 0%03o)", to, oldmode);
29463680Ssada	/*
29563680Ssada	 * XXX
29663680Ssada	 * NFS doesn't support chflags; ignore errors unless there's reason
29763680Ssada	 * to believe we're losing bits.  (Note, this still won't be right
29863680Ssada	 * if the server supports flags and we were trying to *remove* flags
29963680Ssada	 * on a file that we copied, i.e., that we didn't create.)
30063680Ssada	 */
30163680Ssada	errno = 0;
30276878Skris	if (fchflags(to_fd, (u_long)sbp->st_flags))
30363680Ssada		if (errno != EOPNOTSUPP || sbp->st_flags != 0)
30463680Ssada			warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
3051556Srgrimes
3061556Srgrimes	tval[0].tv_sec = sbp->st_atime;
3071556Srgrimes	tval[1].tv_sec = sbp->st_mtime;
3081556Srgrimes	tval[0].tv_usec = tval[1].tv_usec = 0;
3091556Srgrimes	if (utimes(to, tval))
3101556Srgrimes		warn("%s: set times", to);
3111556Srgrimes
3121556Srgrimes	if (close(to_fd)) {
3131556Srgrimes		warn("%s", to);
3141556Srgrimes		return (1);
3151556Srgrimes	}
3161556Srgrimes
3171556Srgrimes	if (unlink(from)) {
3181556Srgrimes		warn("%s: remove", from);
3191556Srgrimes		return (1);
3201556Srgrimes	}
32150544Smharo	if (vflg)
32250544Smharo		printf("%s -> %s\n", from, to);
3231556Srgrimes	return (0);
3241556Srgrimes}
3251556Srgrimes
3261556Srgrimesint
3271556Srgrimescopy(from, to)
3281556Srgrimes	char *from, *to;
3291556Srgrimes{
3301556Srgrimes	int pid, status;
3311556Srgrimes
33240301Sdes	if ((pid = fork()) == 0) {
33350544Smharo		execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", from, to, NULL);
3341556Srgrimes		warn("%s", _PATH_CP);
3351556Srgrimes		_exit(1);
3361556Srgrimes	}
3371556Srgrimes	if (waitpid(pid, &status, 0) == -1) {
3381556Srgrimes		warn("%s: waitpid", _PATH_CP);
3391556Srgrimes		return (1);
3401556Srgrimes	}
3411556Srgrimes	if (!WIFEXITED(status)) {
3421556Srgrimes		warn("%s: did not terminate normally", _PATH_CP);
3431556Srgrimes		return (1);
3441556Srgrimes	}
3451556Srgrimes	if (WEXITSTATUS(status)) {
3461556Srgrimes		warn("%s: terminated with %d (non-zero) status",
3471556Srgrimes		    _PATH_CP, WEXITSTATUS(status));
3481556Srgrimes		return (1);
3491556Srgrimes	}
3501556Srgrimes	if (!(pid = vfork())) {
3511556Srgrimes		execl(_PATH_RM, "mv", "-rf", from, NULL);
3521556Srgrimes		warn("%s", _PATH_RM);
3531556Srgrimes		_exit(1);
3541556Srgrimes	}
3551556Srgrimes	if (waitpid(pid, &status, 0) == -1) {
3561556Srgrimes		warn("%s: waitpid", _PATH_RM);
3571556Srgrimes		return (1);
3581556Srgrimes	}
3591556Srgrimes	if (!WIFEXITED(status)) {
3601556Srgrimes		warn("%s: did not terminate normally", _PATH_RM);
3611556Srgrimes		return (1);
3621556Srgrimes	}
3631556Srgrimes	if (WEXITSTATUS(status)) {
3641556Srgrimes		warn("%s: terminated with %d (non-zero) status",
3651556Srgrimes		    _PATH_RM, WEXITSTATUS(status));
3661556Srgrimes		return (1);
3671556Srgrimes	}
3681556Srgrimes	return (0);
3691556Srgrimes}
3701556Srgrimes
3711556Srgrimesvoid
3721556Srgrimesusage()
3731556Srgrimes{
37450544Smharo
37514305Swosch	(void)fprintf(stderr, "%s\n%s\n",
37650544Smharo		      "usage: mv [-f | -i] [-v] source target",
37750544Smharo		      "       mv [-f | -i] [-v] source ... directory");
37850544Smharo	exit(EX_USAGE);
3791556Srgrimes}
380