blob: 274aecb45d6db965e0bb5ad1a2662217a2ba6572 [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
Rob Landleydfba7412006-03-06 20:47:33 +000038int mv_main(int argc, char **argv)
Matt Kraai91b28552001-04-23 18:53:07 +000039{
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 struct stat dest_stat;
41 const char *last;
42 const char *dest;
Eric Andersen8876fb22003-06-20 09:01:58 +000043 unsigned long flags;
Glenn L McGrath96099d52004-02-21 07:49:54 +000044 int dest_exists;
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 int status = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000046
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000047#if ENABLE_FEATURE_MV_LONG_OPTIONS
Eric Andersen8876fb22003-06-20 09:01:58 +000048 bb_applet_long_options = mv_long_options;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000049#endif
"Vladimir N. Oleynik"27421a12005-09-05 14:46:07 +000050 bb_opt_complementally = "f-i:i-f";
Glenn L McGrath96099d52004-02-21 07:49:54 +000051 flags = bb_getopt_ulflags(argc, argv, "fi");
52 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) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 if ((dest_exists = cp_mv_stat(last, &dest_stat)) < 0) {
61 return 1;
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 {
Eric Andersen8876fb22003-06-20 09:01:58 +000071 dest = concat_path_file(last, bb_get_last_path_component(*argv));
Matt Kraai91b28552001-04-23 18:53:07 +000072
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 if ((dest_exists = cp_mv_stat(dest, &dest_stat)) < 0) {
74 goto RET_1;
75 }
76
Glenn L McGrath96099d52004-02-21 07:49:54 +000077DO_MOVE:
Eric Andersenc7bda1c2004-03-15 08:29:22 +000078
Eric Andersen8876fb22003-06-20 09:01:58 +000079 if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) &&
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 ((access(dest, W_OK) < 0 && isatty(0)) ||
Glenn L McGrath96099d52004-02-21 07:49:54 +000081 (flags & OPT_FILEUTILS_INTERACTIVE))) {
82 if (fprintf(stderr, "mv: overwrite `%s'? ", dest) < 0) {
83 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
Rob Landley3c12ff72005-07-20 00:45:40 +000093 if (errno != EXDEV ||
94 (source_exists = cp_mv_stat(*argv, &source_stat)) < 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 bb_perror_msg("unable to rename `%s'", *argv);
Rob Landley3c12ff72005-07-20 00:45:40 +000096 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 if (dest_exists) {
Glenn L McGrath96099d52004-02-21 07:49:54 +000098 if (dest_exists == 3) {
99 if (source_exists != 3) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 bb_error_msg(fmt, "", "non-");
101 goto RET_1;
102 }
103 } else {
Glenn L McGrath96099d52004-02-21 07:49:54 +0000104 if (source_exists == 3) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 bb_error_msg(fmt, "non-", "");
106 goto RET_1;
107 }
108 }
109 if (unlink(dest) < 0) {
110 bb_perror_msg("cannot remove `%s'", dest);
111 goto RET_1;
112 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000113 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 if ((copy_file(*argv, dest,
Glenn L McGrath96099d52004-02-21 07:49:54 +0000115 FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS) >= 0) &&
116 (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000117 goto RET_0;
118 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 }
Glenn L McGrath96099d52004-02-21 07:49:54 +0000120RET_1:
Matt Kraai91b28552001-04-23 18:53:07 +0000121 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000122 }
Glenn L McGrath96099d52004-02-21 07:49:54 +0000123RET_0:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000124 if (dest != last) {
125 free((void *) dest);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000126 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000127 } while (*++argv != last);
Glenn L McGrath96099d52004-02-21 07:49:54 +0000128
129 return (status);
Matt Kraai91b28552001-04-23 18:53:07 +0000130}