blob: 553bb6ecb641beea3b754bfdf451039cb5580b6f [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 Vlasenko06af2162007-02-03 17:28:39 +000036int mv_main(int argc, char **argv);
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 Vlasenko67b23e62006-10-03 21:00:06 +000050 opt_complementary = "f-i:i-f";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000051 flags = getopt32(argv, "fi");
Glenn L McGrath96099d52004-02-21 07:49:54 +000052 if (optind + 2 > argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000053 bb_show_usage();
Glenn L McGrath96099d52004-02-21 07:49:54 +000054 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000055
56 last = argv[argc - 1];
57 argv += optind;
Matt Kraai91b28552001-04-23 18:53:07 +000058
59 if (optind + 2 == argc) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000060 dest_exists = cp_mv_stat(last, &dest_stat);
61 if (dest_exists < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 return 1;
Matt Kraai91b28552001-04-23 18:53:07 +000063 }
64
Manuel Novoa III cad53642003-03-19 09:13:01 +000065 if (!(dest_exists & 2)) {
66 dest = last;
67 goto DO_MOVE;
Matt Kraai91b28552001-04-23 18:53:07 +000068 }
69 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000070
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 do {
Eric Andersen8876fb22003-06-20 09:01:58 +000072 dest = concat_path_file(last, bb_get_last_path_component(*argv));
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000073 dest_exists = cp_mv_stat(dest, &dest_stat);
74 if (dest_exists < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 goto RET_1;
76 }
77
Glenn L McGrath96099d52004-02-21 07:49:54 +000078DO_MOVE:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000079
Eric Andersen8876fb22003-06-20 09:01:58 +000080 if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) &&
Manuel Novoa III cad53642003-03-19 09:13:01 +000081 ((access(dest, W_OK) < 0 && isatty(0)) ||
Glenn L McGrath96099d52004-02-21 07:49:54 +000082 (flags & OPT_FILEUTILS_INTERACTIVE))) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000083 if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) {
Glenn L McGrath96099d52004-02-21 07:49:54 +000084 goto RET_1; /* Ouch! fprintf failed! */
85 }
86 if (!bb_ask_confirmation()) {
87 goto RET_0;
88 }
89 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 if (rename(*argv, dest) < 0) {
Glenn L McGrath96099d52004-02-21 07:49:54 +000091 struct stat source_stat;
92 int source_exists;
93
Rob Landley3c12ff72005-07-20 00:45:40 +000094 if (errno != EXDEV ||
95 (source_exists = cp_mv_stat(*argv, &source_stat)) < 1) {
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 Vlasenko49622d72007-03-10 16:58:49 +0000119 if ((copy_file(*argv, dest, copy_flag) >= 0) &&
Glenn L McGrath96099d52004-02-21 07:49:54 +0000120 (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 goto RET_0;
122 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123 }
Glenn L McGrath96099d52004-02-21 07:49:54 +0000124RET_1:
Matt Kraai91b28552001-04-23 18:53:07 +0000125 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126 }
Glenn L McGrath96099d52004-02-21 07:49:54 +0000127RET_0:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000128 if (dest != last) {
129 free((void *) dest);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000130 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 } while (*++argv != last);
Glenn L McGrath96099d52004-02-21 07:49:54 +0000132
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000133 return status;
Matt Kraai91b28552001-04-23 18:53:07 +0000134}