mv.c revision 36049
1/* 2 * Copyright (c) 1989, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Ken Smith of The State University of New York at Buffalo. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint 38static char const copyright[] = 39"@(#) Copyright (c) 1989, 1993, 1994\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41#endif /* not lint */ 42 43#ifndef lint 44#if 0 45static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94"; 46#endif 47static const char rcsid[] = 48 "$Id$"; 49#endif /* not lint */ 50 51#include <sys/param.h> 52#include <sys/time.h> 53#include <sys/wait.h> 54#include <sys/stat.h> 55#include <sys/mount.h> 56 57#include <err.h> 58#include <errno.h> 59#include <fcntl.h> 60#include <stdio.h> 61#include <stdlib.h> 62#include <string.h> 63#include <unistd.h> 64 65#include "pathnames.h" 66 67int fflg, iflg; 68 69int copy __P((char *, char *)); 70int do_move __P((char *, char *)); 71int fastcopy __P((char *, char *, struct stat *)); 72void usage __P((void)); 73 74int 75main(argc, argv) 76 int argc; 77 char *argv[]; 78{ 79 register int baselen, len, rval; 80 register char *p, *endp; 81 struct stat sb; 82 int ch; 83 char path[MAXPATHLEN + 1]; 84 85 while ((ch = getopt(argc, argv, "fi")) != -1) 86 switch (ch) { 87 case 'i': 88 iflg = 1; 89 fflg = 0; 90 break; 91 case 'f': 92 fflg = 1; 93 iflg = 0; 94 break; 95 default: 96 usage(); 97 } 98 argc -= optind; 99 argv += optind; 100 101 if (argc < 2) 102 usage(); 103 104 /* 105 * If the stat on the target fails or the target isn't a directory, 106 * try the move. More than 2 arguments is an error in this case. 107 */ 108 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) { 109 if (argc > 2) 110 usage(); 111 exit(do_move(argv[0], argv[1])); 112 } 113 114 /* It's a directory, move each file into it. */ 115 (void)strcpy(path, argv[argc - 1]); 116 baselen = strlen(path); 117 endp = &path[baselen]; 118 *endp++ = '/'; 119 ++baselen; 120 for (rval = 0; --argc; ++argv) { 121 /* 122 * Find the last component of the source pathname. It 123 * may have trailing slashes. 124 */ 125 p = *argv + strlen(*argv); 126 while (p != *argv && p[-1] == '/') 127 --p; 128 while (p != *argv && p[-1] != '/') 129 --p; 130 131 if ((baselen + (len = strlen(p))) >= MAXPATHLEN) { 132 warnx("%s: destination pathname too long", *argv); 133 rval = 1; 134 } else { 135 memmove(endp, p, len + 1); 136 if (do_move(*argv, path)) 137 rval = 1; 138 } 139 } 140 exit(rval); 141} 142 143int 144do_move(from, to) 145 char *from, *to; 146{ 147 struct stat sb; 148 int ask, ch, first; 149 char modep[15]; 150 151 /* 152 * Check access. If interactive and file exists, ask user if it 153 * should be replaced. Otherwise if file exists but isn't writable 154 * make sure the user wants to clobber it. 155 */ 156 if (!fflg && !access(to, F_OK)) { 157 158 /* prompt only if source exist */ 159 if (lstat(from, &sb) == -1) { 160 warn("%s", from); 161 return (1); 162 } 163 164#define YESNO "(y/n [n]) " 165 ask = 0; 166 if (iflg) { 167 (void)fprintf(stderr, "overwrite %s? %s", to, YESNO); 168 ask = 1; 169 } else if (access(to, W_OK) && !stat(to, &sb)) { 170 strmode(sb.st_mode, modep); 171 (void)fprintf(stderr, "override %s%s%s/%s for %s? %s", 172 modep + 1, modep[9] == ' ' ? "" : " ", 173 user_from_uid(sb.st_uid, 0), 174 group_from_gid(sb.st_gid, 0), to, YESNO); 175 ask = 1; 176 } 177 if (ask) { 178 first = ch = getchar(); 179 while (ch != '\n' && ch != EOF) 180 ch = getchar(); 181 if (first != 'y' && first != 'Y') { 182 (void)fprintf(stderr, "not overwritten\n"); 183 return (0); 184 } 185 } 186 } 187 if (!rename(from, to)) 188 return (0); 189 190 if (errno == EXDEV) { 191 struct statfs sfs; 192 char path[MAXPATHLEN]; 193 194 /* Can't mv(1) a mount point. */ 195 if (realpath(from, path) == NULL) { 196 warnx("cannot resolve %s: %s", from, path); 197 return (1); 198 } 199 if (!statfs(path, &sfs) && !strcmp(path, sfs.f_mntonname)) { 200 warnx("cannot rename a mount point"); 201 return (1); 202 } 203 } else { 204 warn("rename %s to %s", from, to); 205 return (1); 206 } 207 208 /* 209 * If rename fails because we're trying to cross devices, and 210 * it's a regular file, do the copy internally; otherwise, use 211 * cp and rm. 212 */ 213 if (stat(from, &sb)) { 214 warn("%s", from); 215 return (1); 216 } 217 return (S_ISREG(sb.st_mode) ? 218 fastcopy(from, to, &sb) : copy(from, to)); 219} 220 221int 222fastcopy(from, to, sbp) 223 char *from, *to; 224 struct stat *sbp; 225{ 226 struct timeval tval[2]; 227 static u_int blen; 228 static char *bp; 229 mode_t oldmode; 230 register int nread, from_fd, to_fd; 231 232 if ((from_fd = open(from, O_RDONLY, 0)) < 0) { 233 warn("%s", from); 234 return (1); 235 } 236 if (blen < sbp->st_blksize) { 237 if (bp != NULL) 238 free(bp); 239 if ((bp = malloc(sbp->st_blksize)) == NULL) { 240 blen = 0; 241 warnx("malloc failed"); 242 return (1); 243 } 244 blen = sbp->st_blksize; 245 } 246 while ((to_fd = 247 open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) { 248 if (errno == EEXIST && unlink(to) == 0) 249 continue; 250 warn("%s", to); 251 (void)close(from_fd); 252 return (1); 253 } 254 while ((nread = read(from_fd, bp, blen)) > 0) 255 if (write(to_fd, bp, nread) != nread) { 256 warn("%s", to); 257 goto err; 258 } 259 if (nread < 0) { 260 warn("%s", from); 261err: if (unlink(to)) 262 warn("%s: remove", to); 263 (void)close(from_fd); 264 (void)close(to_fd); 265 return (1); 266 } 267 (void)close(from_fd); 268 269 oldmode = sbp->st_mode & ALLPERMS; 270 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) { 271 warn("%s: set owner/group (was: %u/%u)", to, sbp->st_uid, 272 sbp->st_gid); 273 if (oldmode & (S_ISUID | S_ISGID)) { 274 warnx( 275"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)", 276 to, oldmode); 277 sbp->st_mode &= ~(S_ISUID | S_ISGID); 278 } 279 } 280 if (fchmod(to_fd, sbp->st_mode)) 281 warn("%s: set mode (was: 0%03o)", to, oldmode); 282 283 tval[0].tv_sec = sbp->st_atime; 284 tval[1].tv_sec = sbp->st_mtime; 285 tval[0].tv_usec = tval[1].tv_usec = 0; 286 if (utimes(to, tval)) 287 warn("%s: set times", to); 288 289 if (close(to_fd)) { 290 warn("%s", to); 291 return (1); 292 } 293 294 if (unlink(from)) { 295 warn("%s: remove", from); 296 return (1); 297 } 298 return (0); 299} 300 301int 302copy(from, to) 303 char *from, *to; 304{ 305 int pid, status; 306 307 if ((pid = vfork()) == 0) { 308 execl(_PATH_CP, "mv", "-PRp", from, to, NULL); 309 warn("%s", _PATH_CP); 310 _exit(1); 311 } 312 if (waitpid(pid, &status, 0) == -1) { 313 warn("%s: waitpid", _PATH_CP); 314 return (1); 315 } 316 if (!WIFEXITED(status)) { 317 warn("%s: did not terminate normally", _PATH_CP); 318 return (1); 319 } 320 if (WEXITSTATUS(status)) { 321 warn("%s: terminated with %d (non-zero) status", 322 _PATH_CP, WEXITSTATUS(status)); 323 return (1); 324 } 325 if (!(pid = vfork())) { 326 execl(_PATH_RM, "mv", "-rf", from, NULL); 327 warn("%s", _PATH_RM); 328 _exit(1); 329 } 330 if (waitpid(pid, &status, 0) == -1) { 331 warn("%s: waitpid", _PATH_RM); 332 return (1); 333 } 334 if (!WIFEXITED(status)) { 335 warn("%s: did not terminate normally", _PATH_RM); 336 return (1); 337 } 338 if (WEXITSTATUS(status)) { 339 warn("%s: terminated with %d (non-zero) status", 340 _PATH_RM, WEXITSTATUS(status)); 341 return (1); 342 } 343 return (0); 344} 345 346void 347usage() 348{ 349 (void)fprintf(stderr, "%s\n%s\n", 350 "usage: mv [-f | -i] source target", 351 " mv [-f | -i] source ... directory"); 352 exit(1); 353} 354