blob: aef1ed19a2b4b6c7afa1eb2b9b7cc0f2343e696b [file] [log] [blame]
Matt Kraai91b28552001-04-23 18:53:07 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini mv implementation for busybox
4 *
Matt Kraai91b28552001-04-23 18:53:07 +00005 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Manuel Novoa III cad53642003-03-19 09:13:01 +000023/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
24 *
25 * Size reduction and improved error checking.
26 */
27
Matt Kraai91b28552001-04-23 18:53:07 +000028#include <sys/types.h>
29#include <sys/stat.h>
30#include <unistd.h>
31#include <dirent.h>
32#include <errno.h>
33#include <stdlib.h>
Rob Landleyb7128c62005-09-11 01:05:30 +000034#include <getopt.h> /* struct option */
Matt Kraai91b28552001-04-23 18:53:07 +000035#include "busybox.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000036#include "libcoreutils/coreutils.h"
Matt Kraai91b28552001-04-23 18:53:07 +000037
Eric Andersen8876fb22003-06-20 09:01:58 +000038static const struct option mv_long_options[] = {
39 { "interactive", 0, NULL, 'i' },
40 { "force", 0, NULL, 'f' },
41 { 0, 0, 0, 0 }
42};
43
Eric Andersen8876fb22003-06-20 09:01:58 +000044#define OPT_FILEUTILS_FORCE 1
45#define OPT_FILEUTILS_INTERACTIVE 2
46
47static const char fmt[] = "cannot overwrite %sdirectory with %sdirectory";
Matt Kraai91b28552001-04-23 18:53:07 +000048
49extern int mv_main(int argc, char **argv)
50{
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 struct stat dest_stat;
52 const char *last;
53 const char *dest;
Eric Andersen8876fb22003-06-20 09:01:58 +000054 unsigned long flags;
Glenn L McGrath96099d52004-02-21 07:49:54 +000055 int dest_exists;
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 int status = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000057
Eric Andersen8876fb22003-06-20 09:01:58 +000058 bb_applet_long_options = mv_long_options;
"Vladimir N. Oleynik"27421a12005-09-05 14:46:07 +000059 bb_opt_complementally = "f-i:i-f";
Glenn L McGrath96099d52004-02-21 07:49:54 +000060 flags = bb_getopt_ulflags(argc, argv, "fi");
61 if (optind + 2 > argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 bb_show_usage();
Glenn L McGrath96099d52004-02-21 07:49:54 +000063 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000064
65 last = argv[argc - 1];
66 argv += optind;
Matt Kraai91b28552001-04-23 18:53:07 +000067
68 if (optind + 2 == argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 if ((dest_exists = cp_mv_stat(last, &dest_stat)) < 0) {
70 return 1;
Matt Kraai91b28552001-04-23 18:53:07 +000071 }
72
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 if (!(dest_exists & 2)) {
74 dest = last;
75 goto DO_MOVE;
Matt Kraai91b28552001-04-23 18:53:07 +000076 }
77 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000078
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 do {
Eric Andersen8876fb22003-06-20 09:01:58 +000080 dest = concat_path_file(last, bb_get_last_path_component(*argv));
Matt Kraai91b28552001-04-23 18:53:07 +000081
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 if ((dest_exists = cp_mv_stat(dest, &dest_stat)) < 0) {
83 goto RET_1;
84 }
85
Glenn L McGrath96099d52004-02-21 07:49:54 +000086DO_MOVE:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000087
Eric Andersen8876fb22003-06-20 09:01:58 +000088 if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) &&
Manuel Novoa III cad53642003-03-19 09:13:01 +000089 ((access(dest, W_OK) < 0 && isatty(0)) ||
Glenn L McGrath96099d52004-02-21 07:49:54 +000090 (flags & OPT_FILEUTILS_INTERACTIVE))) {
91 if (fprintf(stderr, "mv: overwrite `%s'? ", dest) < 0) {
92 goto RET_1; /* Ouch! fprintf failed! */
93 }
94 if (!bb_ask_confirmation()) {
95 goto RET_0;
96 }
97 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000098 if (rename(*argv, dest) < 0) {
Glenn L McGrath96099d52004-02-21 07:49:54 +000099 struct stat source_stat;
100 int source_exists;
101
Rob Landley3c12ff72005-07-20 00:45:40 +0000102 if (errno != EXDEV ||
103 (source_exists = cp_mv_stat(*argv, &source_stat)) < 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104 bb_perror_msg("unable to rename `%s'", *argv);
Rob Landley3c12ff72005-07-20 00:45:40 +0000105 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000106 if (dest_exists) {
Glenn L McGrath96099d52004-02-21 07:49:54 +0000107 if (dest_exists == 3) {
108 if (source_exists != 3) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000109 bb_error_msg(fmt, "", "non-");
110 goto RET_1;
111 }
112 } else {
Glenn L McGrath96099d52004-02-21 07:49:54 +0000113 if (source_exists == 3) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 bb_error_msg(fmt, "non-", "");
115 goto RET_1;
116 }
117 }
118 if (unlink(dest) < 0) {
119 bb_perror_msg("cannot remove `%s'", dest);
120 goto RET_1;
121 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000122 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123 if ((copy_file(*argv, dest,
Glenn L McGrath96099d52004-02-21 07:49:54 +0000124 FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS) >= 0) &&
125 (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126 goto RET_0;
127 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000128 }
Glenn L McGrath96099d52004-02-21 07:49:54 +0000129RET_1:
Matt Kraai91b28552001-04-23 18:53:07 +0000130 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 }
Glenn L McGrath96099d52004-02-21 07:49:54 +0000132RET_0:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000133 if (dest != last) {
134 free((void *) dest);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000135 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136 } while (*++argv != last);
Glenn L McGrath96099d52004-02-21 07:49:54 +0000137
138 return (status);
Matt Kraai91b28552001-04-23 18:53:07 +0000139}