1 1.46 riastrad /* $NetBSD: mv.c,v 1.46 2020/06/24 16:58:12 riastradh Exp $ */ 2 1.9 cgd 3 1.1 cgd /* 4 1.8 mycroft * Copyright (c) 1989, 1993, 1994 5 1.8 mycroft * The Regents of the University of California. All rights reserved. 6 1.1 cgd * 7 1.1 cgd * This code is derived from software contributed to Berkeley by 8 1.1 cgd * Ken Smith of The State University of New York at Buffalo. 9 1.1 cgd * 10 1.1 cgd * Redistribution and use in source and binary forms, with or without 11 1.1 cgd * modification, are permitted provided that the following conditions 12 1.1 cgd * are met: 13 1.1 cgd * 1. Redistributions of source code must retain the above copyright 14 1.1 cgd * notice, this list of conditions and the following disclaimer. 15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 cgd * notice, this list of conditions and the following disclaimer in the 17 1.1 cgd * documentation and/or other materials provided with the distribution. 18 1.31 agc * 3. Neither the name of the University nor the names of its contributors 19 1.1 cgd * may be used to endorse or promote products derived from this software 20 1.1 cgd * without specific prior written permission. 21 1.1 cgd * 22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 1.1 cgd * SUCH DAMAGE. 33 1.1 cgd */ 34 1.1 cgd 35 1.11 christos #include <sys/cdefs.h> 36 1.1 cgd #ifndef lint 37 1.41 lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\ 38 1.41 lukem The Regents of the University of California. All rights reserved."); 39 1.1 cgd #endif /* not lint */ 40 1.1 cgd 41 1.1 cgd #ifndef lint 42 1.9 cgd #if 0 43 1.9 cgd static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94"; 44 1.9 cgd #else 45 1.46 riastrad __RCSID("$NetBSD: mv.c,v 1.46 2020/06/24 16:58:12 riastradh Exp $"); 46 1.9 cgd #endif 47 1.1 cgd #endif /* not lint */ 48 1.1 cgd 49 1.1 cgd #include <sys/param.h> 50 1.1 cgd #include <sys/time.h> 51 1.1 cgd #include <sys/wait.h> 52 1.1 cgd #include <sys/stat.h> 53 1.42 manu #include <sys/extattr.h> 54 1.8 mycroft 55 1.8 mycroft #include <err.h> 56 1.8 mycroft #include <errno.h> 57 1.1 cgd #include <fcntl.h> 58 1.27 wiz #include <grp.h> 59 1.14 kleink #include <locale.h> 60 1.27 wiz #include <pwd.h> 61 1.1 cgd #include <stdio.h> 62 1.1 cgd #include <stdlib.h> 63 1.1 cgd #include <string.h> 64 1.8 mycroft #include <unistd.h> 65 1.8 mycroft 66 1.1 cgd #include "pathnames.h" 67 1.1 cgd 68 1.46 riastrad static int fflg, hflg, iflg, vflg; 69 1.43 joerg static int stdin_ok; 70 1.45 mrg static sig_atomic_t pinfo; 71 1.1 cgd 72 1.43 joerg static int copy(char *, char *); 73 1.43 joerg static int do_move(char *, char *); 74 1.43 joerg static int fastcopy(char *, char *, struct stat *); 75 1.43 joerg __dead static void usage(void); 76 1.8 mycroft 77 1.45 mrg static void 78 1.45 mrg progress(int sig __unused) 79 1.45 mrg { 80 1.45 mrg 81 1.45 mrg pinfo++; 82 1.45 mrg } 83 1.45 mrg 84 1.8 mycroft int 85 1.27 wiz main(int argc, char *argv[]) 86 1.1 cgd { 87 1.38 rillig int ch, len, rval; 88 1.10 tls char *p, *endp; 89 1.1 cgd struct stat sb; 90 1.1 cgd char path[MAXPATHLEN + 1]; 91 1.38 rillig size_t baselen; 92 1.1 cgd 93 1.27 wiz setprogname(argv[0]); 94 1.17 mycroft (void)setlocale(LC_ALL, ""); 95 1.14 kleink 96 1.46 riastrad while ((ch = getopt(argc, argv, "hifv")) != -1) 97 1.8 mycroft switch (ch) { 98 1.46 riastrad case 'h': 99 1.46 riastrad hflg = 1; 100 1.46 riastrad break; 101 1.1 cgd case 'i': 102 1.5 jtc fflg = 0; 103 1.1 cgd iflg = 1; 104 1.1 cgd break; 105 1.1 cgd case 'f': 106 1.5 jtc iflg = 0; 107 1.1 cgd fflg = 1; 108 1.1 cgd break; 109 1.28 jrf case 'v': 110 1.28 jrf vflg = 1; 111 1.28 jrf break; 112 1.1 cgd default: 113 1.1 cgd usage(); 114 1.1 cgd } 115 1.5 jtc argc -= optind; 116 1.1 cgd argv += optind; 117 1.1 cgd 118 1.1 cgd if (argc < 2) 119 1.1 cgd usage(); 120 1.1 cgd 121 1.6 jtc stdin_ok = isatty(STDIN_FILENO); 122 1.6 jtc 123 1.45 mrg (void)signal(SIGINFO, progress); 124 1.45 mrg 125 1.1 cgd /* 126 1.1 cgd * If the stat on the target fails or the target isn't a directory, 127 1.1 cgd * try the move. More than 2 arguments is an error in this case. 128 1.1 cgd */ 129 1.46 riastrad if (hflg || stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) { 130 1.1 cgd if (argc > 2) 131 1.1 cgd usage(); 132 1.1 cgd exit(do_move(argv[0], argv[1])); 133 1.1 cgd } 134 1.1 cgd 135 1.1 cgd /* It's a directory, move each file into it. */ 136 1.36 christos baselen = strlcpy(path, argv[argc - 1], sizeof(path)); 137 1.36 christos if (baselen >= sizeof(path)) 138 1.36 christos errx(1, "%s: destination pathname too long", argv[argc - 1]); 139 1.1 cgd endp = &path[baselen]; 140 1.36 christos if (!baselen || *(endp - 1) != '/') { 141 1.36 christos *endp++ = '/'; 142 1.36 christos ++baselen; 143 1.36 christos } 144 1.8 mycroft for (rval = 0; --argc; ++argv) { 145 1.14 kleink p = *argv + strlen(*argv) - 1; 146 1.14 kleink while (*p == '/' && p != *argv) 147 1.14 kleink *p-- = '\0'; 148 1.8 mycroft if ((p = strrchr(*argv, '/')) == NULL) 149 1.1 cgd p = *argv; 150 1.1 cgd else 151 1.1 cgd ++p; 152 1.14 kleink 153 1.8 mycroft if ((baselen + (len = strlen(p))) >= MAXPATHLEN) { 154 1.33 jschauma warnx("%s: destination pathname too long", *argv); 155 1.8 mycroft rval = 1; 156 1.8 mycroft } else { 157 1.8 mycroft memmove(endp, p, len + 1); 158 1.8 mycroft if (do_move(*argv, path)) 159 1.8 mycroft rval = 1; 160 1.1 cgd } 161 1.1 cgd } 162 1.8 mycroft exit(rval); 163 1.18 mycroft /* NOTREACHED */ 164 1.1 cgd } 165 1.1 cgd 166 1.43 joerg static int 167 1.27 wiz do_move(char *from, char *to) 168 1.1 cgd { 169 1.1 cgd struct stat sb; 170 1.8 mycroft char modep[15]; 171 1.1 cgd 172 1.8 mycroft /* 173 1.8 mycroft * (1) If the destination path exists, the -f option is not specified 174 1.6 jtc * and either of the following conditions are true: 175 1.6 jtc * 176 1.24 jdolecek * (a) The permissions of the destination path do not permit 177 1.6 jtc * writing and the standard input is a terminal. 178 1.6 jtc * (b) The -i option is specified. 179 1.6 jtc * 180 1.8 mycroft * the mv utility shall write a prompt to standard error and 181 1.6 jtc * read a line from standard input. If the response is not 182 1.6 jtc * affirmative, mv shall do nothing more with the current 183 1.6 jtc * source file... 184 1.1 cgd */ 185 1.1 cgd if (!fflg && !access(to, F_OK)) { 186 1.8 mycroft int ask = 1; 187 1.6 jtc int ch; 188 1.6 jtc 189 1.1 cgd if (iflg) { 190 1.21 sommerfe if (access(from, F_OK)) { 191 1.33 jschauma warn("rename %s", from); 192 1.21 sommerfe return (1); 193 1.21 sommerfe } 194 1.33 jschauma (void)fprintf(stderr, "overwrite %s? ", to); 195 1.6 jtc } else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) { 196 1.21 sommerfe if (access(from, F_OK)) { 197 1.33 jschauma warn("rename %s", from); 198 1.21 sommerfe return (1); 199 1.21 sommerfe } 200 1.8 mycroft strmode(sb.st_mode, modep); 201 1.8 mycroft (void)fprintf(stderr, "override %s%s%s/%s for %s? ", 202 1.8 mycroft modep + 1, modep[9] == ' ' ? "" : " ", 203 1.8 mycroft user_from_uid(sb.st_uid, 0), 204 1.33 jschauma group_from_gid(sb.st_gid, 0), to); 205 1.8 mycroft } else 206 1.8 mycroft ask = 0; 207 1.1 cgd if (ask) { 208 1.37 elad if ((ch = getchar()) != EOF && ch != '\n') { 209 1.37 elad int ch2; 210 1.37 elad while ((ch2 = getchar()) != EOF && ch2 != '\n') 211 1.37 elad continue; 212 1.37 elad } 213 1.33 jschauma if (ch != 'y' && ch != 'Y') 214 1.8 mycroft return (0); 215 1.1 cgd } 216 1.1 cgd } 217 1.6 jtc 218 1.8 mycroft /* 219 1.8 mycroft * (2) If rename() succeeds, mv shall do nothing more with the 220 1.6 jtc * current source file. If it fails for any other reason than 221 1.6 jtc * EXDEV, mv shall write a diagnostic message to the standard 222 1.6 jtc * error and do nothing more with the current source file. 223 1.6 jtc * 224 1.6 jtc * (3) If the destination path exists, and it is a file of type 225 1.6 jtc * directory and source_file is not a file of type directory, 226 1.6 jtc * or it is a file not of type directory, and source file is 227 1.8 mycroft * a file of type directory, mv shall write a diagnostic 228 1.6 jtc * message to standard error, and do nothing more with the 229 1.6 jtc * current source file... 230 1.6 jtc */ 231 1.28 jrf if (!rename(from, to)) { 232 1.28 jrf if (vflg) 233 1.33 jschauma printf("%s -> %s\n", from, to); 234 1.8 mycroft return (0); 235 1.28 jrf } 236 1.1 cgd 237 1.1 cgd if (errno != EXDEV) { 238 1.33 jschauma warn("rename %s to %s", from, to); 239 1.8 mycroft return (1); 240 1.1 cgd } 241 1.1 cgd 242 1.8 mycroft /* 243 1.8 mycroft * (4) If the destination path exists, mv shall attempt to remove it. 244 1.8 mycroft * If this fails for any reason, mv shall write a diagnostic 245 1.6 jtc * message to the standard error and do nothing more with the 246 1.6 jtc * current source file... 247 1.6 jtc */ 248 1.15 mikel if (!lstat(to, &sb)) { 249 1.6 jtc if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) { 250 1.33 jschauma warn("can't remove %s", to); 251 1.6 jtc return (1); 252 1.6 jtc } 253 1.6 jtc } 254 1.6 jtc 255 1.8 mycroft /* 256 1.8 mycroft * (5) The file hierarchy rooted in source_file shall be duplicated 257 1.15 mikel * as a file hierarchy rooted in the destination path... 258 1.1 cgd */ 259 1.15 mikel if (lstat(from, &sb)) { 260 1.33 jschauma warn("%s", from); 261 1.8 mycroft return (1); 262 1.1 cgd } 263 1.30 jschauma 264 1.8 mycroft return (S_ISREG(sb.st_mode) ? 265 1.1 cgd fastcopy(from, to, &sb) : copy(from, to)); 266 1.1 cgd } 267 1.1 cgd 268 1.43 joerg static int 269 1.27 wiz fastcopy(char *from, char *to, struct stat *sbp) 270 1.1 cgd { 271 1.44 enami #if defined(__NetBSD__) 272 1.44 enami struct timespec ts[2]; 273 1.44 enami #else 274 1.1 cgd struct timeval tval[2]; 275 1.44 enami #endif 276 1.40 christos static blksize_t blen; 277 1.1 cgd static char *bp; 278 1.45 mrg int from_fd, to_fd; 279 1.45 mrg ssize_t nread; 280 1.45 mrg off_t total = 0; 281 1.1 cgd 282 1.1 cgd if ((from_fd = open(from, O_RDONLY, 0)) < 0) { 283 1.33 jschauma warn("%s", from); 284 1.8 mycroft return (1); 285 1.1 cgd } 286 1.8 mycroft if ((to_fd = 287 1.8 mycroft open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) { 288 1.33 jschauma warn("%s", to); 289 1.1 cgd (void)close(from_fd); 290 1.8 mycroft return (1); 291 1.1 cgd } 292 1.1 cgd if (!blen && !(bp = malloc(blen = sbp->st_blksize))) { 293 1.23 drochner warn(NULL); 294 1.40 christos blen = 0; 295 1.39 hubertf (void)close(from_fd); 296 1.39 hubertf (void)close(to_fd); 297 1.8 mycroft return (1); 298 1.1 cgd } 299 1.45 mrg while ((nread = read(from_fd, bp, blen)) > 0) { 300 1.1 cgd if (write(to_fd, bp, nread) != nread) { 301 1.33 jschauma warn("%s", to); 302 1.1 cgd goto err; 303 1.1 cgd } 304 1.45 mrg total += nread; 305 1.45 mrg if (pinfo) { 306 1.45 mrg int pcent = (int)((100.0 * total) / sbp->st_size); 307 1.45 mrg 308 1.45 mrg pinfo = 0; 309 1.45 mrg (void)fprintf(stderr, "%s => %s %llu/%llu bytes %d%% " 310 1.45 mrg "written\n", from, to, (unsigned long long)total, 311 1.45 mrg (unsigned long long)sbp->st_size, pcent); 312 1.45 mrg } 313 1.45 mrg } 314 1.1 cgd if (nread < 0) { 315 1.33 jschauma warn("%s", from); 316 1.8 mycroft err: if (unlink(to)) 317 1.33 jschauma warn("%s: remove", to); 318 1.1 cgd (void)close(from_fd); 319 1.1 cgd (void)close(to_fd); 320 1.8 mycroft return (1); 321 1.1 cgd } 322 1.42 manu 323 1.42 manu if (fcpxattr(from_fd, to_fd) == -1) 324 1.42 manu warn("%s: error copying extended attributes", to); 325 1.42 manu 326 1.8 mycroft (void)close(from_fd); 327 1.22 christos #ifdef BSD4_4 328 1.44 enami #if defined(__NetBSD__) 329 1.44 enami ts[0] = sbp->st_atimespec; 330 1.44 enami ts[1] = sbp->st_mtimespec; 331 1.44 enami #else 332 1.16 mycroft TIMESPEC_TO_TIMEVAL(&tval[0], &sbp->st_atimespec); 333 1.16 mycroft TIMESPEC_TO_TIMEVAL(&tval[1], &sbp->st_mtimespec); 334 1.44 enami #endif 335 1.22 christos #else 336 1.22 christos tval[0].tv_sec = sbp->st_atime; 337 1.22 christos tval[1].tv_sec = sbp->st_mtime; 338 1.22 christos tval[0].tv_usec = 0; 339 1.22 christos tval[1].tv_usec = 0; 340 1.22 christos #endif 341 1.20 christos #ifdef __SVR4 342 1.20 christos if (utimes(to, tval)) 343 1.20 christos #else 344 1.44 enami #if defined(__NetBSD__) 345 1.44 enami if (futimens(to_fd, ts)) 346 1.44 enami #else 347 1.16 mycroft if (futimes(to_fd, tval)) 348 1.20 christos #endif 349 1.44 enami #endif 350 1.33 jschauma warn("%s: set times", to); 351 1.16 mycroft if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) { 352 1.13 hubertf if (errno != EPERM) 353 1.33 jschauma warn("%s: set owner/group", to); 354 1.16 mycroft sbp->st_mode &= ~(S_ISUID | S_ISGID); 355 1.16 mycroft } 356 1.8 mycroft if (fchmod(to_fd, sbp->st_mode)) 357 1.33 jschauma warn("%s: set mode", to); 358 1.26 darrenr if (fchflags(to_fd, sbp->st_flags) && (errno != EOPNOTSUPP)) 359 1.33 jschauma warn("%s: set flags (was: 0%07o)", to, sbp->st_flags); 360 1.8 mycroft 361 1.8 mycroft if (close(to_fd)) { 362 1.33 jschauma warn("%s", to); 363 1.8 mycroft return (1); 364 1.8 mycroft } 365 1.8 mycroft 366 1.8 mycroft if (unlink(from)) { 367 1.33 jschauma warn("%s: remove", from); 368 1.8 mycroft return (1); 369 1.8 mycroft } 370 1.28 jrf 371 1.28 jrf if (vflg) 372 1.33 jschauma printf("%s -> %s\n", from, to); 373 1.28 jrf 374 1.8 mycroft return (0); 375 1.1 cgd } 376 1.1 cgd 377 1.43 joerg static int 378 1.27 wiz copy(char *from, char *to) 379 1.1 cgd { 380 1.39 hubertf pid_t pid; 381 1.39 hubertf int status; 382 1.1 cgd 383 1.8 mycroft if ((pid = vfork()) == 0) { 384 1.38 rillig execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to, NULL); 385 1.8 mycroft warn("%s", _PATH_CP); 386 1.1 cgd _exit(1); 387 1.1 cgd } 388 1.8 mycroft if (waitpid(pid, &status, 0) == -1) { 389 1.8 mycroft warn("%s: waitpid", _PATH_CP); 390 1.8 mycroft return (1); 391 1.8 mycroft } 392 1.8 mycroft if (!WIFEXITED(status)) { 393 1.35 hubertf warnx("%s: did not terminate normally", _PATH_CP); 394 1.8 mycroft return (1); 395 1.8 mycroft } 396 1.8 mycroft if (WEXITSTATUS(status)) { 397 1.35 hubertf warnx("%s: terminated with %d (non-zero) status", 398 1.8 mycroft _PATH_CP, WEXITSTATUS(status)); 399 1.8 mycroft return (1); 400 1.8 mycroft } 401 1.1 cgd if (!(pid = vfork())) { 402 1.38 rillig execl(_PATH_RM, "mv", "-rf", "--", from, NULL); 403 1.8 mycroft warn("%s", _PATH_RM); 404 1.1 cgd _exit(1); 405 1.1 cgd } 406 1.8 mycroft if (waitpid(pid, &status, 0) == -1) { 407 1.8 mycroft warn("%s: waitpid", _PATH_RM); 408 1.8 mycroft return (1); 409 1.8 mycroft } 410 1.8 mycroft if (!WIFEXITED(status)) { 411 1.35 hubertf warnx("%s: did not terminate normally", _PATH_RM); 412 1.8 mycroft return (1); 413 1.8 mycroft } 414 1.8 mycroft if (WEXITSTATUS(status)) { 415 1.35 hubertf warnx("%s: terminated with %d (non-zero) status", 416 1.8 mycroft _PATH_RM, WEXITSTATUS(status)); 417 1.8 mycroft return (1); 418 1.8 mycroft } 419 1.8 mycroft return (0); 420 1.1 cgd } 421 1.1 cgd 422 1.43 joerg static void 423 1.27 wiz usage(void) 424 1.1 cgd { 425 1.46 riastrad (void)fprintf(stderr, "usage: %s [-fhiv] source target\n" 426 1.28 jrf " %s [-fiv] source ... directory\n", getprogname(), 427 1.27 wiz getprogname()); 428 1.1 cgd exit(1); 429 1.18 mycroft /* NOTREACHED */ 430 1.1 cgd } 431