blob: 7d4905fc921c8af5357adf633937d7fcfe613ff7 [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 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Matt Kraai91b28552001-04-23 18:53:07 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
11 *
12 * Size reduction and improved error checking.
13 */
14
Matt Kraai91b28552001-04-23 18:53:07 +000015#include <sys/types.h>
16#include <sys/stat.h>
17#include <unistd.h>
18#include <dirent.h>
19#include <errno.h>
20#include <stdlib.h>
Rob Landleyb7128c62005-09-11 01:05:30 +000021#include <getopt.h> /* struct option */
Matt Kraai91b28552001-04-23 18:53:07 +000022#include "busybox.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000023#include "libcoreutils/coreutils.h"
Matt Kraai91b28552001-04-23 18:53:07 +000024
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000025#if ENABLE_FEATURE_MV_LONG_OPTIONS
Eric Andersen8876fb22003-06-20 09:01:58 +000026static const struct option mv_long_options[] = {
27 { "interactive", 0, NULL, 'i' },
28 { "force", 0, NULL, 'f' },
29 { 0, 0, 0, 0 }
30};
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000031#endif
Eric Andersen8876fb22003-06-20 09:01:58 +000032
Eric Andersen8876fb22003-06-20 09:01:58 +000033#define OPT_FILEUTILS_FORCE 1
34#define OPT_FILEUTILS_INTERACTIVE 2
35
36static const char fmt[] = "cannot overwrite %sdirectory with %sdirectory";
Matt Kraai91b28552001-04-23 18:53:07 +000037
Denis Vlasenko06af2162007-02-03 17:28:39 +000038int mv_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000039int mv_main(int argc, char **argv)
Matt Kraai91b28552001-04-23 18:53:07 +000040{
Manuel Novoa III cad53642003-03-19 09:13:01 +000041 struct stat dest_stat;
42 const char *last;
43 const char *dest;
Eric Andersen8876fb22003-06-20 09:01:58 +000044 unsigned long flags;
Glenn L McGrath96099d52004-02-21 07:49:54 +000045 int dest_exists;
Manuel Novoa III cad53642003-03-19 09:13:01 +000046 int status = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000047
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000048#if ENABLE_FEATURE_MV_LONG_OPTIONS
Denis Vlasenko67b23e62006-10-03 21:00:06 +000049 applet_long_options = mv_long_options;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000050#endif
Denis Vlasenko67b23e62006-10-03 21:00:06 +000051 opt_complementary = "f-i:i-f";
52 flags = getopt32(argc, argv, "fi");
Glenn L McGrath96099d52004-02-21 07:49:54 +000053 if (optind + 2 > argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 bb_show_usage();
Glenn L McGrath96099d52004-02-21 07:49:54 +000055 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000056
57 last = argv[argc - 1];
58 argv += optind;
Matt Kraai91b28552001-04-23 18:53:07 +000059
60 if (optind + 2 == argc) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000061 dest_exists = cp_mv_stat(last, &dest_stat);
62 if (dest_exists < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 return 1;
Matt Kraai91b28552001-04-23 18:53:07 +000064 }
65
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 if (!(dest_exists & 2)) {
67 dest = last;
68 goto DO_MOVE;
Matt Kraai91b28552001-04-23 18:53:07 +000069 }
70 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000071
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 do {
Eric Andersen8876fb22003-06-20 09:01:58 +000073 dest = concat_path_file(last, bb_get_last_path_component(*argv));
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000074 dest_exists = cp_mv_stat(dest, &dest_stat);
75 if (dest_exists < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000076 goto RET_1;
77 }
78
Glenn L McGrath96099d52004-02-21 07:49:54 +000079DO_MOVE:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000080
Eric Andersen8876fb22003-06-20 09:01:58 +000081 if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) &&
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 ((access(dest, W_OK) < 0 && isatty(0)) ||
Glenn L McGrath96099d52004-02-21 07:49:54 +000083 (flags & OPT_FILEUTILS_INTERACTIVE))) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000084 if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) {
Glenn L McGrath96099d52004-02-21 07:49:54 +000085 goto RET_1; /* Ouch! fprintf failed! */
86 }
87 if (!bb_ask_confirmation()) {
88 goto RET_0;
89 }
90 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 if (rename(*argv, dest) < 0) {
Glenn L McGrath96099d52004-02-21 07:49:54 +000092 struct stat source_stat;
93 int source_exists;
94
Rob Landley3c12ff72005-07-20 00:45:40 +000095 if (errno != EXDEV ||
96 (source_exists = cp_mv_stat(*argv, &source_stat)) < 1) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000097 bb_perror_msg("cannot rename '%s'", *argv);
Rob Landley3c12ff72005-07-20 00:45:40 +000098 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +000099 if (dest_exists) {
Glenn L McGrath96099d52004-02-21 07:49:54 +0000100 if (dest_exists == 3) {
101 if (source_exists != 3) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 bb_error_msg(fmt, "", "non-");
103 goto RET_1;
104 }
105 } else {
Glenn L McGrath96099d52004-02-21 07:49:54 +0000106 if (source_exists == 3) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000107 bb_error_msg(fmt, "non-", "");
108 goto RET_1;
109 }
110 }
111 if (unlink(dest) < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000112 bb_perror_msg("cannot remove '%s'", dest);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000113 goto RET_1;
114 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000115 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000116 if ((copy_file(*argv, dest,
Glenn L McGrath96099d52004-02-21 07:49:54 +0000117 FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS) >= 0) &&
118 (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 goto RET_0;
120 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 }
Glenn L McGrath96099d52004-02-21 07:49:54 +0000122RET_1:
Matt Kraai91b28552001-04-23 18:53:07 +0000123 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000124 }
Glenn L McGrath96099d52004-02-21 07:49:54 +0000125RET_0:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126 if (dest != last) {
127 free((void *) dest);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000128 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 } while (*++argv != last);
Glenn L McGrath96099d52004-02-21 07:49:54 +0000130
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000131 return status;
Matt Kraai91b28552001-04-23 18:53:07 +0000132}