blob: 02252c7ef114942c0d311f01081edacd1edf35c1 [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
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000038#if ENABLE_FEATURE_MV_LONG_OPTIONS
Eric Andersen8876fb22003-06-20 09:01:58 +000039static const struct option mv_long_options[] = {
40 { "interactive", 0, NULL, 'i' },
41 { "force", 0, NULL, 'f' },
42 { 0, 0, 0, 0 }
43};
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000044#endif
Eric Andersen8876fb22003-06-20 09:01:58 +000045
Eric Andersen8876fb22003-06-20 09:01:58 +000046#define OPT_FILEUTILS_FORCE 1
47#define OPT_FILEUTILS_INTERACTIVE 2
48
49static const char fmt[] = "cannot overwrite %sdirectory with %sdirectory";
Matt Kraai91b28552001-04-23 18:53:07 +000050
Rob Landleydfba7412006-03-06 20:47:33 +000051int mv_main(int argc, char **argv)
Matt Kraai91b28552001-04-23 18:53:07 +000052{
Manuel Novoa III cad53642003-03-19 09:13:01 +000053 struct stat dest_stat;
54 const char *last;
55 const char *dest;
Eric Andersen8876fb22003-06-20 09:01:58 +000056 unsigned long flags;
Glenn L McGrath96099d52004-02-21 07:49:54 +000057 int dest_exists;
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 int status = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000059
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000060#if ENABLE_FEATURE_MV_LONG_OPTIONS
Eric Andersen8876fb22003-06-20 09:01:58 +000061 bb_applet_long_options = mv_long_options;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000062#endif
"Vladimir N. Oleynik"27421a12005-09-05 14:46:07 +000063 bb_opt_complementally = "f-i:i-f";
Glenn L McGrath96099d52004-02-21 07:49:54 +000064 flags = bb_getopt_ulflags(argc, argv, "fi");
65 if (optind + 2 > argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 bb_show_usage();
Glenn L McGrath96099d52004-02-21 07:49:54 +000067 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000068
69 last = argv[argc - 1];
70 argv += optind;
Matt Kraai91b28552001-04-23 18:53:07 +000071
72 if (optind + 2 == argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 if ((dest_exists = cp_mv_stat(last, &dest_stat)) < 0) {
74 return 1;
Matt Kraai91b28552001-04-23 18:53:07 +000075 }
76
Manuel Novoa III cad53642003-03-19 09:13:01 +000077 if (!(dest_exists & 2)) {
78 dest = last;
79 goto DO_MOVE;
Matt Kraai91b28552001-04-23 18:53:07 +000080 }
81 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000082
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 do {
Eric Andersen8876fb22003-06-20 09:01:58 +000084 dest = concat_path_file(last, bb_get_last_path_component(*argv));
Matt Kraai91b28552001-04-23 18:53:07 +000085
Manuel Novoa III cad53642003-03-19 09:13:01 +000086 if ((dest_exists = cp_mv_stat(dest, &dest_stat)) < 0) {
87 goto RET_1;
88 }
89
Glenn L McGrath96099d52004-02-21 07:49:54 +000090DO_MOVE:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000091
Eric Andersen8876fb22003-06-20 09:01:58 +000092 if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) &&
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 ((access(dest, W_OK) < 0 && isatty(0)) ||
Glenn L McGrath96099d52004-02-21 07:49:54 +000094 (flags & OPT_FILEUTILS_INTERACTIVE))) {
95 if (fprintf(stderr, "mv: overwrite `%s'? ", dest) < 0) {
96 goto RET_1; /* Ouch! fprintf failed! */
97 }
98 if (!bb_ask_confirmation()) {
99 goto RET_0;
100 }
101 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 if (rename(*argv, dest) < 0) {
Glenn L McGrath96099d52004-02-21 07:49:54 +0000103 struct stat source_stat;
104 int source_exists;
105
Rob Landley3c12ff72005-07-20 00:45:40 +0000106 if (errno != EXDEV ||
107 (source_exists = cp_mv_stat(*argv, &source_stat)) < 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 bb_perror_msg("unable to rename `%s'", *argv);
Rob Landley3c12ff72005-07-20 00:45:40 +0000109 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110 if (dest_exists) {
Glenn L McGrath96099d52004-02-21 07:49:54 +0000111 if (dest_exists == 3) {
112 if (source_exists != 3) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000113 bb_error_msg(fmt, "", "non-");
114 goto RET_1;
115 }
116 } else {
Glenn L McGrath96099d52004-02-21 07:49:54 +0000117 if (source_exists == 3) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118 bb_error_msg(fmt, "non-", "");
119 goto RET_1;
120 }
121 }
122 if (unlink(dest) < 0) {
123 bb_perror_msg("cannot remove `%s'", dest);
124 goto RET_1;
125 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000126 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000127 if ((copy_file(*argv, dest,
Glenn L McGrath96099d52004-02-21 07:49:54 +0000128 FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS) >= 0) &&
129 (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000130 goto RET_0;
131 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000132 }
Glenn L McGrath96099d52004-02-21 07:49:54 +0000133RET_1:
Matt Kraai91b28552001-04-23 18:53:07 +0000134 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135 }
Glenn L McGrath96099d52004-02-21 07:49:54 +0000136RET_0:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000137 if (dest != last) {
138 free((void *) dest);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000139 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 } while (*++argv != last);
Glenn L McGrath96099d52004-02-21 07:49:54 +0000141
142 return (status);
Matt Kraai91b28552001-04-23 18:53:07 +0000143}