blob: d8dc6c06b8dcea0ee24941f27b95310d5349543a [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>
Denis Vlasenko49622d72007-03-10 16:58:49 +00006 * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
Matt Kraai91b28552001-04-23 18:53:07 +00007 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Matt Kraai91b28552001-04-23 18:53:07 +00009 */
10
Manuel Novoa III cad53642003-03-19 09:13:01 +000011/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
12 *
13 * Size reduction and improved error checking.
14 */
15
Matt Kraai91b28552001-04-23 18:53:07 +000016#include <sys/types.h>
17#include <sys/stat.h>
Matt Kraai91b28552001-04-23 18:53:07 +000018#include <dirent.h>
Rob Landleyb7128c62005-09-11 01:05:30 +000019#include <getopt.h> /* struct option */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000020#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000021#include "libcoreutils/coreutils.h"
Matt Kraai91b28552001-04-23 18:53:07 +000022
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000023#if ENABLE_FEATURE_MV_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000024static const char mv_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000025 "interactive\0" No_argument "i"
26 "force\0" No_argument "f"
Denis Vlasenko990d0f62007-07-24 15:54:42 +000027 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000028#endif
Eric Andersen8876fb22003-06-20 09:01:58 +000029
Eric Andersen8876fb22003-06-20 09:01:58 +000030#define OPT_FILEUTILS_FORCE 1
31#define OPT_FILEUTILS_INTERACTIVE 2
32
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000033static const char fmt[] ALIGN1 =
34 "cannot overwrite %sdirectory with %sdirectory";
Matt Kraai91b28552001-04-23 18:53:07 +000035
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000036int mv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000037int mv_main(int argc, char **argv)
Matt Kraai91b28552001-04-23 18:53:07 +000038{
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 struct stat dest_stat;
40 const char *last;
41 const char *dest;
Eric Andersen8876fb22003-06-20 09:01:58 +000042 unsigned long flags;
Glenn L McGrath96099d52004-02-21 07:49:54 +000043 int dest_exists;
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 int status = 0;
Denis Vlasenko49622d72007-03-10 16:58:49 +000045 int copy_flag = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000046
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000047#if ENABLE_FEATURE_MV_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000048 applet_long_options = mv_longopts;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000049#endif
Denis Vlasenko6666ac42007-08-24 21:46:24 +000050 // Need at least two arguments
51 // -f unsets -i, -i unsets -f
52 opt_complementary = "-2:f-i:i-f";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000053 flags = getopt32(argv, "fi");
Denis Vlasenko6666ac42007-08-24 21:46:24 +000054 argc -= optind;
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 argv += optind;
Denis Vlasenko6666ac42007-08-24 21:46:24 +000056 last = argv[argc - 1];
Matt Kraai91b28552001-04-23 18:53:07 +000057
Denis Vlasenko6666ac42007-08-24 21:46:24 +000058 if (argc == 2) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000059 dest_exists = cp_mv_stat(last, &dest_stat);
60 if (dest_exists < 0) {
Bernhard Reutner-Fischer7c2db5c2007-11-16 12:39:16 +000061 return EXIT_FAILURE;
Matt Kraai91b28552001-04-23 18:53:07 +000062 }
63
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 if (!(dest_exists & 2)) {
65 dest = last;
66 goto DO_MOVE;
Matt Kraai91b28552001-04-23 18:53:07 +000067 }
68 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000069
Manuel Novoa III cad53642003-03-19 09:13:01 +000070 do {
Denis Vlasenko818322b2007-09-24 18:27:04 +000071 dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000072 dest_exists = cp_mv_stat(dest, &dest_stat);
73 if (dest_exists < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 goto RET_1;
75 }
76
Denis Vlasenko6666ac42007-08-24 21:46:24 +000077 DO_MOVE:
78 if (dest_exists && !(flags & OPT_FILEUTILS_FORCE)
79 && ((access(dest, W_OK) < 0 && isatty(0))
80 || (flags & OPT_FILEUTILS_INTERACTIVE))
81 ) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000082 if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) {
Glenn L McGrath96099d52004-02-21 07:49:54 +000083 goto RET_1; /* Ouch! fprintf failed! */
84 }
85 if (!bb_ask_confirmation()) {
86 goto RET_0;
87 }
88 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000089 if (rename(*argv, dest) < 0) {
Glenn L McGrath96099d52004-02-21 07:49:54 +000090 struct stat source_stat;
91 int source_exists;
92
Denis Vlasenko6666ac42007-08-24 21:46:24 +000093 if (errno != EXDEV
94 || (source_exists = cp_mv_stat(*argv, &source_stat)) < 1
95 ) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000096 bb_perror_msg("cannot rename '%s'", *argv);
Rob Landley3c12ff72005-07-20 00:45:40 +000097 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +000098 if (dest_exists) {
Glenn L McGrath96099d52004-02-21 07:49:54 +000099 if (dest_exists == 3) {
100 if (source_exists != 3) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101 bb_error_msg(fmt, "", "non-");
102 goto RET_1;
103 }
104 } else {
Glenn L McGrath96099d52004-02-21 07:49:54 +0000105 if (source_exists == 3) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000106 bb_error_msg(fmt, "non-", "");
107 goto RET_1;
108 }
109 }
110 if (unlink(dest) < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000111 bb_perror_msg("cannot remove '%s'", dest);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000112 goto RET_1;
113 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000114 }
Denis Vlasenko49622d72007-03-10 16:58:49 +0000115 copy_flag = FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS;
116#if ENABLE_SELINUX
117 copy_flag |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000118#endif
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000119 if ((copy_file(*argv, dest, copy_flag) >= 0)
120 && (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)
121 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000122 goto RET_0;
123 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000124 }
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000125 RET_1:
Matt Kraai91b28552001-04-23 18:53:07 +0000126 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000127 }
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000128 RET_0:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 if (dest != last) {
130 free((void *) dest);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000131 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000132 } while (*++argv != last);
Glenn L McGrath96099d52004-02-21 07:49:54 +0000133
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000134 return status;
Matt Kraai91b28552001-04-23 18:53:07 +0000135}