blob: c08d2327c6c5af106cc9b82752ccd3a2e1980788 [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>
18#include <unistd.h>
19#include <dirent.h>
20#include <errno.h>
21#include <stdlib.h>
Rob Landleyb7128c62005-09-11 01:05:30 +000022#include <getopt.h> /* struct option */
Matt Kraai91b28552001-04-23 18:53:07 +000023#include "busybox.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000024#include "libcoreutils/coreutils.h"
Matt Kraai91b28552001-04-23 18:53:07 +000025
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000026#if ENABLE_FEATURE_MV_LONG_OPTIONS
Eric Andersen8876fb22003-06-20 09:01:58 +000027static const struct option mv_long_options[] = {
28 { "interactive", 0, NULL, 'i' },
29 { "force", 0, NULL, 'f' },
30 { 0, 0, 0, 0 }
31};
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000032#endif
Eric Andersen8876fb22003-06-20 09:01:58 +000033
Eric Andersen8876fb22003-06-20 09:01:58 +000034#define OPT_FILEUTILS_FORCE 1
35#define OPT_FILEUTILS_INTERACTIVE 2
36
37static const char fmt[] = "cannot overwrite %sdirectory with %sdirectory";
Matt Kraai91b28552001-04-23 18:53:07 +000038
Denis Vlasenko06af2162007-02-03 17:28:39 +000039int mv_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000040int mv_main(int argc, char **argv)
Matt Kraai91b28552001-04-23 18:53:07 +000041{
Manuel Novoa III cad53642003-03-19 09:13:01 +000042 struct stat dest_stat;
43 const char *last;
44 const char *dest;
Eric Andersen8876fb22003-06-20 09:01:58 +000045 unsigned long flags;
Glenn L McGrath96099d52004-02-21 07:49:54 +000046 int dest_exists;
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 int status = 0;
Denis Vlasenko49622d72007-03-10 16:58:49 +000048 int copy_flag = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000049
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000050#if ENABLE_FEATURE_MV_LONG_OPTIONS
Denis Vlasenko67b23e62006-10-03 21:00:06 +000051 applet_long_options = mv_long_options;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000052#endif
Denis Vlasenko67b23e62006-10-03 21:00:06 +000053 opt_complementary = "f-i:i-f";
54 flags = getopt32(argc, argv, "fi");
Glenn L McGrath96099d52004-02-21 07:49:54 +000055 if (optind + 2 > argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 bb_show_usage();
Glenn L McGrath96099d52004-02-21 07:49:54 +000057 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000058
59 last = argv[argc - 1];
60 argv += optind;
Matt Kraai91b28552001-04-23 18:53:07 +000061
62 if (optind + 2 == argc) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000063 dest_exists = cp_mv_stat(last, &dest_stat);
64 if (dest_exists < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000065 return 1;
Matt Kraai91b28552001-04-23 18:53:07 +000066 }
67
Manuel Novoa III cad53642003-03-19 09:13:01 +000068 if (!(dest_exists & 2)) {
69 dest = last;
70 goto DO_MOVE;
Matt Kraai91b28552001-04-23 18:53:07 +000071 }
72 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000073
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 do {
Eric Andersen8876fb22003-06-20 09:01:58 +000075 dest = concat_path_file(last, bb_get_last_path_component(*argv));
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000076 dest_exists = cp_mv_stat(dest, &dest_stat);
77 if (dest_exists < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000078 goto RET_1;
79 }
80
Glenn L McGrath96099d52004-02-21 07:49:54 +000081DO_MOVE:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000082
Eric Andersen8876fb22003-06-20 09:01:58 +000083 if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) &&
Manuel Novoa III cad53642003-03-19 09:13:01 +000084 ((access(dest, W_OK) < 0 && isatty(0)) ||
Glenn L McGrath96099d52004-02-21 07:49:54 +000085 (flags & OPT_FILEUTILS_INTERACTIVE))) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000086 if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) {
Glenn L McGrath96099d52004-02-21 07:49:54 +000087 goto RET_1; /* Ouch! fprintf failed! */
88 }
89 if (!bb_ask_confirmation()) {
90 goto RET_0;
91 }
92 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 if (rename(*argv, dest) < 0) {
Glenn L McGrath96099d52004-02-21 07:49:54 +000094 struct stat source_stat;
95 int source_exists;
96
Rob Landley3c12ff72005-07-20 00:45:40 +000097 if (errno != EXDEV ||
98 (source_exists = cp_mv_stat(*argv, &source_stat)) < 1) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000099 bb_perror_msg("cannot rename '%s'", *argv);
Rob Landley3c12ff72005-07-20 00:45:40 +0000100 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101 if (dest_exists) {
Glenn L McGrath96099d52004-02-21 07:49:54 +0000102 if (dest_exists == 3) {
103 if (source_exists != 3) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104 bb_error_msg(fmt, "", "non-");
105 goto RET_1;
106 }
107 } else {
Glenn L McGrath96099d52004-02-21 07:49:54 +0000108 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 }
113 if (unlink(dest) < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000114 bb_perror_msg("cannot remove '%s'", dest);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000115 goto RET_1;
116 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000117 }
Denis Vlasenko49622d72007-03-10 16:58:49 +0000118 copy_flag = FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS;
119#if ENABLE_SELINUX
120 copy_flag |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000121#endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000122 if ((copy_file(*argv, dest, copy_flag) >= 0) &&
Glenn L McGrath96099d52004-02-21 07:49:54 +0000123 (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000124 goto RET_0;
125 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126 }
Glenn L McGrath96099d52004-02-21 07:49:54 +0000127RET_1:
Matt Kraai91b28552001-04-23 18:53:07 +0000128 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 }
Glenn L McGrath96099d52004-02-21 07:49:54 +0000130RET_0:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 if (dest != last) {
132 free((void *) dest);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000133 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000134 } while (*++argv != last);
Glenn L McGrath96099d52004-02-21 07:49:54 +0000135
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000136 return status;
Matt Kraai91b28552001-04-23 18:53:07 +0000137}