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