mv.c revision 90114
190075Sobrien/* 290075Sobrien * Copyright (c) 1989, 1993, 1994 390075Sobrien * The Regents of the University of California. All rights reserved. 490075Sobrien * 590075Sobrien * This code is derived from software contributed to Berkeley by 690075Sobrien * Ken Smith of The State University of New York at Buffalo. 790075Sobrien * 890075Sobrien * Redistribution and use in source and binary forms, with or without 990075Sobrien * modification, are permitted provided that the following conditions 1090075Sobrien * are met: 1190075Sobrien * 1. Redistributions of source code must retain the above copyright 1290075Sobrien * notice, this list of conditions and the following disclaimer. 1390075Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1490075Sobrien * notice, this list of conditions and the following disclaimer in the 1590075Sobrien * documentation and/or other materials provided with the distribution. 1690075Sobrien * 3. All advertising materials mentioning features or use of this software 1790075Sobrien * must display the following acknowledgement: 1890075Sobrien * This product includes software developed by the University of 1990075Sobrien * California, Berkeley and its contributors. 2090075Sobrien * 4. Neither the name of the University nor the names of its contributors 2190075Sobrien * may be used to endorse or promote products derived from this software 2290075Sobrien * without specific prior written permission. 2390075Sobrien * 2490075Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2590075Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2690075Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2790075Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2896263Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2996263Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3090075Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3190075Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3296263Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3390075Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3490075Sobrien * SUCH DAMAGE. 3590075Sobrien */ 3690075Sobrien 3790075Sobrien#ifndef lint 3890075Sobrienstatic char const copyright[] = 3990075Sobrien"@(#) Copyright (c) 1989, 1993, 1994\n\ 4090075Sobrien The Regents of the University of California. All rights reserved.\n"; 4190075Sobrien#endif /* not lint */ 4290075Sobrien 4390075Sobrien#ifndef lint 4490075Sobrien#if 0 4590075Sobrienstatic char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94"; 4690075Sobrien#endif 4790075Sobrienstatic const char rcsid[] = 4890075Sobrien "$FreeBSD: head/bin/mv/mv.c 90114 2002-02-02 07:09:30Z imp $"; 4990075Sobrien#endif /* not lint */ 5090075Sobrien 5190075Sobrien#include <sys/param.h> 5290075Sobrien#include <sys/time.h> 5390075Sobrien#include <sys/wait.h> 5490075Sobrien#include <sys/stat.h> 5590075Sobrien#include <sys/mount.h> 5690075Sobrien 5790075Sobrien#include <err.h> 5896263Sobrien#include <errno.h> 5990075Sobrien#include <fcntl.h> 6090075Sobrien#include <limits.h> 6190075Sobrien#include <stdio.h> 6290075Sobrien#include <stdlib.h> 6390075Sobrien#include <string.h> 6490075Sobrien#include <sysexits.h> 6590075Sobrien#include <unistd.h> 6690075Sobrien 6790075Sobrien#include "pathnames.h" 6890075Sobrien 6990075Sobrienint fflg, iflg, vflg; 7090075Sobrien 7190075Sobrienint copy(char *, char *); 7290075Sobrienint do_move(char *, char *); 7390075Sobrienint fastcopy(char *, char *, struct stat *); 7490075Sobrienvoid usage(void); 7590075Sobrien 7690075Sobrienint 7790075Sobrienmain(int argc, char *argv[]) 7890075Sobrien{ 7990075Sobrien int baselen, len, rval; 8090075Sobrien char *p, *endp; 8190075Sobrien struct stat sb; 8290075Sobrien int ch; 8390075Sobrien char path[PATH_MAX]; 8490075Sobrien 8590075Sobrien while ((ch = getopt(argc, argv, "fiv")) != -1) 8690075Sobrien switch (ch) { 8790075Sobrien case 'i': 8890075Sobrien iflg = 1; 8990075Sobrien fflg = 0; 9090075Sobrien break; 9190075Sobrien case 'f': 9290075Sobrien fflg = 1; 9390075Sobrien iflg = 0; 9490075Sobrien break; 9590075Sobrien case 'v': 9690075Sobrien vflg = 1; 9790075Sobrien break; 9890075Sobrien default: 9990075Sobrien usage(); 10090075Sobrien } 10190075Sobrien argc -= optind; 10290075Sobrien argv += optind; 10390075Sobrien 10490075Sobrien if (argc < 2) 10590075Sobrien usage(); 10690075Sobrien 10790075Sobrien /* 10890075Sobrien * If the stat on the target fails or the target isn't a directory, 10990075Sobrien * try the move. More than 2 arguments is an error in this case. 11090075Sobrien */ 11190075Sobrien if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) { 11290075Sobrien if (argc > 2) 11390075Sobrien usage(); 11490075Sobrien exit(do_move(argv[0], argv[1])); 11590075Sobrien } 11690075Sobrien 11790075Sobrien /* It's a directory, move each file into it. */ 11890075Sobrien if (strlen(argv[argc - 1]) > sizeof(path) - 1) 11990075Sobrien errx(1, "%s: destination pathname too long", *argv); 12090075Sobrien (void)strcpy(path, argv[argc - 1]); 12190075Sobrien baselen = strlen(path); 12290075Sobrien endp = &path[baselen]; 12390075Sobrien if (!baselen || *(endp - 1) != '/') { 12490075Sobrien *endp++ = '/'; 12590075Sobrien ++baselen; 12690075Sobrien } 12790075Sobrien for (rval = 0; --argc; ++argv) { 12890075Sobrien /* 12990075Sobrien * Find the last component of the source pathname. It 13090075Sobrien * may have trailing slashes. 13190075Sobrien */ 13290075Sobrien p = *argv + strlen(*argv); 13390075Sobrien while (p != *argv && p[-1] == '/') 13490075Sobrien --p; 13590075Sobrien while (p != *argv && p[-1] != '/') 13690075Sobrien --p; 13790075Sobrien 13890075Sobrien if ((baselen + (len = strlen(p))) >= PATH_MAX) { 13990075Sobrien warnx("%s: destination pathname too long", *argv); 14090075Sobrien rval = 1; 14190075Sobrien } else { 14290075Sobrien memmove(endp, p, (size_t)len + 1); 14390075Sobrien if (do_move(*argv, path)) 14490075Sobrien rval = 1; 14590075Sobrien } 14690075Sobrien } 14790075Sobrien exit(rval); 14890075Sobrien} 14990075Sobrien 15090075Sobrienint 15190075Sobriendo_move(char *from, char *to) 15290075Sobrien{ 15390075Sobrien struct stat sb; 15490075Sobrien int ask, ch, first; 15590075Sobrien char modep[15]; 15690075Sobrien 15790075Sobrien /* 15890075Sobrien * Check access. If interactive and file exists, ask user if it 15990075Sobrien * should be replaced. Otherwise if file exists but isn't writable 16090075Sobrien * make sure the user wants to clobber it. 16190075Sobrien */ 16290075Sobrien if (!fflg && !access(to, F_OK)) { 16390075Sobrien 16490075Sobrien /* prompt only if source exist */ 16590075Sobrien if (lstat(from, &sb) == -1) { 16690075Sobrien warn("%s", from); 16790075Sobrien return (1); 16890075Sobrien } 16990075Sobrien 17090075Sobrien#define YESNO "(y/n [n]) " 17190075Sobrien ask = 0; 17290075Sobrien if (iflg) { 17390075Sobrien (void)fprintf(stderr, "overwrite %s? %s", to, YESNO); 17490075Sobrien ask = 1; 17590075Sobrien } else if (access(to, W_OK) && !stat(to, &sb)) { 17690075Sobrien strmode(sb.st_mode, modep); 17790075Sobrien (void)fprintf(stderr, "override %s%s%s/%s for %s? %s", 17890075Sobrien modep + 1, modep[9] == ' ' ? "" : " ", 17990075Sobrien user_from_uid((unsigned long)sb.st_uid, 0), 18090075Sobrien group_from_gid((unsigned long)sb.st_gid, 0), to, YESNO); 18190075Sobrien ask = 1; 18290075Sobrien } 18390075Sobrien if (ask) { 18490075Sobrien first = ch = getchar(); 18590075Sobrien while (ch != '\n' && ch != EOF) 18690075Sobrien ch = getchar(); 18790075Sobrien if (first != 'y' && first != 'Y') { 18890075Sobrien (void)fprintf(stderr, "not overwritten\n"); 18990075Sobrien return (0); 19090075Sobrien } 19190075Sobrien } 19290075Sobrien } 19390075Sobrien if (!rename(from, to)) { 19490075Sobrien if (vflg) 19590075Sobrien printf("%s -> %s\n", from, to); 19690075Sobrien return (0); 19790075Sobrien } 19890075Sobrien 19990075Sobrien if (errno == EXDEV) { 20090075Sobrien struct statfs sfs; 20190075Sobrien char path[PATH_MAX]; 20290075Sobrien 20390075Sobrien /* Can't mv(1) a mount point. */ 20490075Sobrien if (realpath(from, path) == NULL) { 20590075Sobrien warnx("cannot resolve %s: %s", from, path); 20690075Sobrien return (1); 20790075Sobrien } 20890075Sobrien if (!statfs(path, &sfs) && !strcmp(path, sfs.f_mntonname)) { 20990075Sobrien warnx("cannot rename a mount point"); 21090075Sobrien return (1); 21190075Sobrien } 21290075Sobrien } else { 21390075Sobrien warn("rename %s to %s", from, to); 21490075Sobrien return (1); 21596263Sobrien } 21696263Sobrien 21796263Sobrien /* 21896263Sobrien * If rename fails because we're trying to cross devices, and 21996263Sobrien * it's a regular file, do the copy internally; otherwise, use 22096263Sobrien * cp and rm. 22196263Sobrien */ 22296263Sobrien if (lstat(from, &sb)) { 22396263Sobrien warn("%s", from); 22496263Sobrien return (1); 22596263Sobrien } 22696263Sobrien return (S_ISREG(sb.st_mode) ? 22796263Sobrien fastcopy(from, to, &sb) : copy(from, to)); 22896263Sobrien} 22996263Sobrien 23096263Sobrienint 23196263Sobrienfastcopy(char *from, char *to, struct stat *sbp) 23296263Sobrien{ 23396263Sobrien struct timeval tval[2]; 23496263Sobrien static u_int blen; 23596263Sobrien static char *bp; 23696263Sobrien mode_t oldmode; 23796263Sobrien int nread, from_fd, to_fd; 23896263Sobrien 23996263Sobrien if ((from_fd = open(from, O_RDONLY, 0)) < 0) { 24096263Sobrien warn("%s", from); 24190075Sobrien return (1); 24290075Sobrien } 24390075Sobrien if (blen < sbp->st_blksize) { 24490075Sobrien if (bp != NULL) 24590075Sobrien free(bp); 24690075Sobrien if ((bp = malloc((size_t)sbp->st_blksize)) == NULL) { 24790075Sobrien blen = 0; 24890075Sobrien warnx("malloc failed"); 24990075Sobrien return (1); 25090075Sobrien } 25190075Sobrien blen = sbp->st_blksize; 25290075Sobrien } 25390075Sobrien while ((to_fd = 25490075Sobrien open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) { 25590075Sobrien if (errno == EEXIST && unlink(to) == 0) 25690075Sobrien continue; 25790075Sobrien warn("%s", to); 25890075Sobrien (void)close(from_fd); 25990075Sobrien return (1); 26090075Sobrien } 26190075Sobrien while ((nread = read(from_fd, bp, (size_t)blen)) > 0) 26290075Sobrien if (write(to_fd, bp, (size_t)nread) != nread) { 26390075Sobrien warn("%s", to); 26490075Sobrien goto err; 26590075Sobrien } 26690075Sobrien if (nread < 0) { 26790075Sobrien warn("%s", from); 26890075Sobrienerr: if (unlink(to)) 26990075Sobrien warn("%s: remove", to); 27090075Sobrien (void)close(from_fd); 27190075Sobrien (void)close(to_fd); 27290075Sobrien return (1); 27390075Sobrien } 27490075Sobrien (void)close(from_fd); 27590075Sobrien 27690075Sobrien oldmode = sbp->st_mode & ALLPERMS; 27790075Sobrien if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) { 27890075Sobrien warn("%s: set owner/group (was: %lu/%lu)", to, 27990075Sobrien (u_long)sbp->st_uid, (u_long)sbp->st_gid); 28090075Sobrien if (oldmode & (S_ISUID | S_ISGID)) { 28190075Sobrien warnx( 28290075Sobrien"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)", 28390075Sobrien to, oldmode); 28490075Sobrien sbp->st_mode &= ~(S_ISUID | S_ISGID); 28590075Sobrien } 28690075Sobrien } 28790075Sobrien if (fchmod(to_fd, sbp->st_mode)) 28890075Sobrien warn("%s: set mode (was: 0%03o)", to, oldmode); 28990075Sobrien /* 29090075Sobrien * XXX 29196263Sobrien * NFS doesn't support chflags; ignore errors unless there's reason 29290075Sobrien * to believe we're losing bits. (Note, this still won't be right 29396263Sobrien * if the server supports flags and we were trying to *remove* flags 29496263Sobrien * on a file that we copied, i.e., that we didn't create.) 29590075Sobrien */ 29696263Sobrien errno = 0; 29796263Sobrien if (fchflags(to_fd, (u_long)sbp->st_flags)) 29896263Sobrien if (errno != EOPNOTSUPP || sbp->st_flags != 0) 29996263Sobrien warn("%s: set flags (was: 0%07o)", to, sbp->st_flags); 30090075Sobrien 30196263Sobrien tval[0].tv_sec = sbp->st_atime; 30296263Sobrien tval[1].tv_sec = sbp->st_mtime; 30396263Sobrien tval[0].tv_usec = tval[1].tv_usec = 0; 30496263Sobrien if (utimes(to, tval)) 30596263Sobrien warn("%s: set times", to); 30696263Sobrien 30796263Sobrien if (close(to_fd)) { 30896263Sobrien warn("%s", to); 30996263Sobrien return (1); 31096263Sobrien } 31196263Sobrien 31290075Sobrien if (unlink(from)) { 31390075Sobrien warn("%s: remove", from); 31490075Sobrien return (1); 31590075Sobrien } 31690075Sobrien if (vflg) 31790075Sobrien printf("%s -> %s\n", from, to); 31890075Sobrien return (0); 31990075Sobrien} 32090075Sobrien 32190075Sobrienint 32290075Sobriencopy(char *from, char *to) 32390075Sobrien{ 32490075Sobrien int pid, status; 32590075Sobrien 32690075Sobrien if ((pid = fork()) == 0) { 32790075Sobrien execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", from, to, 32890075Sobrien (char *)NULL); 32990075Sobrien warn("%s", _PATH_CP); 33096263Sobrien _exit(1); 33190075Sobrien } 33296263Sobrien if (waitpid(pid, &status, 0) == -1) { 33396263Sobrien warn("%s: waitpid", _PATH_CP); 33496263Sobrien return (1); 33596263Sobrien } 33696263Sobrien if (!WIFEXITED(status)) { 33796263Sobrien warn("%s: did not terminate normally", _PATH_CP); 33890075Sobrien return (1); 33996263Sobrien } 34096263Sobrien if (WEXITSTATUS(status)) { 34196263Sobrien warn("%s: terminated with %d (non-zero) status", 34296263Sobrien _PATH_CP, WEXITSTATUS(status)); 34396263Sobrien return (1); 34490075Sobrien } 34596263Sobrien if (!(pid = vfork())) { 34690075Sobrien execl(_PATH_RM, "mv", "-rf", from, (char *)NULL); 34790075Sobrien warn("%s", _PATH_RM); 34890075Sobrien _exit(1); 34990075Sobrien } 35090075Sobrien if (waitpid(pid, &status, 0) == -1) { 35190075Sobrien warn("%s: waitpid", _PATH_RM); 35290075Sobrien return (1); 35396263Sobrien } 35490075Sobrien if (!WIFEXITED(status)) { 35590075Sobrien warn("%s: did not terminate normally", _PATH_RM); 35690075Sobrien return (1); 35790075Sobrien } 35890075Sobrien if (WEXITSTATUS(status)) { 35990075Sobrien warn("%s: terminated with %d (non-zero) status", 36090075Sobrien _PATH_RM, WEXITSTATUS(status)); 36190075Sobrien return (1); 36290075Sobrien } 36390075Sobrien return (0); 36490075Sobrien} 36590075Sobrien 36690075Sobrienvoid 36790075Sobrienusage(void) 36890075Sobrien{ 36990075Sobrien 37090075Sobrien (void)fprintf(stderr, "%s\n%s\n", 37190075Sobrien "usage: mv [-f | -i] [-v] source target", 37290075Sobrien " mv [-f | -i] [-v] source ... directory"); 37390075Sobrien exit(EX_USAGE); 37490075Sobrien} 37590075Sobrien