Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini mv implementation for busybox |
| 4 | * |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 5 | * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 23 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
| 24 | * |
| 25 | * Size reduction and improved error checking. |
| 26 | */ |
| 27 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <unistd.h> |
| 31 | #include <dirent.h> |
| 32 | #include <errno.h> |
| 33 | #include <stdlib.h> |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 34 | #include <getopt.h> /* struct option */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 35 | #include "busybox.h" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 36 | #include "libcoreutils/coreutils.h" |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 37 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 38 | #if ENABLE_FEATURE_MV_LONG_OPTIONS |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 39 | static const struct option mv_long_options[] = { |
| 40 | { "interactive", 0, NULL, 'i' }, |
| 41 | { "force", 0, NULL, 'f' }, |
| 42 | { 0, 0, 0, 0 } |
| 43 | }; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 44 | #endif |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 45 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 46 | #define OPT_FILEUTILS_FORCE 1 |
| 47 | #define OPT_FILEUTILS_INTERACTIVE 2 |
| 48 | |
| 49 | static const char fmt[] = "cannot overwrite %sdirectory with %sdirectory"; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 50 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 51 | int mv_main(int argc, char **argv) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 52 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 53 | struct stat dest_stat; |
| 54 | const char *last; |
| 55 | const char *dest; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 56 | unsigned long flags; |
Glenn L McGrath | 96099d5 | 2004-02-21 07:49:54 +0000 | [diff] [blame] | 57 | int dest_exists; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | int status = 0; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 59 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 60 | #if ENABLE_FEATURE_MV_LONG_OPTIONS |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 61 | bb_applet_long_options = mv_long_options; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 62 | #endif |
"Vladimir N. Oleynik" | 27421a1 | 2005-09-05 14:46:07 +0000 | [diff] [blame] | 63 | bb_opt_complementally = "f-i:i-f"; |
Glenn L McGrath | 96099d5 | 2004-02-21 07:49:54 +0000 | [diff] [blame] | 64 | flags = bb_getopt_ulflags(argc, argv, "fi"); |
| 65 | if (optind + 2 > argc) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 66 | bb_show_usage(); |
Glenn L McGrath | 96099d5 | 2004-02-21 07:49:54 +0000 | [diff] [blame] | 67 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 68 | |
| 69 | last = argv[argc - 1]; |
| 70 | argv += optind; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 71 | |
| 72 | if (optind + 2 == argc) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 73 | if ((dest_exists = cp_mv_stat(last, &dest_stat)) < 0) { |
| 74 | return 1; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 77 | if (!(dest_exists & 2)) { |
| 78 | dest = last; |
| 79 | goto DO_MOVE; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 82 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 83 | do { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 84 | dest = concat_path_file(last, bb_get_last_path_component(*argv)); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 85 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 86 | if ((dest_exists = cp_mv_stat(dest, &dest_stat)) < 0) { |
| 87 | goto RET_1; |
| 88 | } |
| 89 | |
Glenn L McGrath | 96099d5 | 2004-02-21 07:49:54 +0000 | [diff] [blame] | 90 | DO_MOVE: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 91 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 92 | if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) && |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 93 | ((access(dest, W_OK) < 0 && isatty(0)) || |
Glenn L McGrath | 96099d5 | 2004-02-21 07:49:54 +0000 | [diff] [blame] | 94 | (flags & OPT_FILEUTILS_INTERACTIVE))) { |
| 95 | if (fprintf(stderr, "mv: overwrite `%s'? ", dest) < 0) { |
| 96 | goto RET_1; /* Ouch! fprintf failed! */ |
| 97 | } |
| 98 | if (!bb_ask_confirmation()) { |
| 99 | goto RET_0; |
| 100 | } |
| 101 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | if (rename(*argv, dest) < 0) { |
Glenn L McGrath | 96099d5 | 2004-02-21 07:49:54 +0000 | [diff] [blame] | 103 | struct stat source_stat; |
| 104 | int source_exists; |
| 105 | |
Rob Landley | 3c12ff7 | 2005-07-20 00:45:40 +0000 | [diff] [blame] | 106 | if (errno != EXDEV || |
| 107 | (source_exists = cp_mv_stat(*argv, &source_stat)) < 1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 108 | bb_perror_msg("unable to rename `%s'", *argv); |
Rob Landley | 3c12ff7 | 2005-07-20 00:45:40 +0000 | [diff] [blame] | 109 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 110 | if (dest_exists) { |
Glenn L McGrath | 96099d5 | 2004-02-21 07:49:54 +0000 | [diff] [blame] | 111 | if (dest_exists == 3) { |
| 112 | if (source_exists != 3) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 113 | bb_error_msg(fmt, "", "non-"); |
| 114 | goto RET_1; |
| 115 | } |
| 116 | } else { |
Glenn L McGrath | 96099d5 | 2004-02-21 07:49:54 +0000 | [diff] [blame] | 117 | if (source_exists == 3) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 118 | bb_error_msg(fmt, "non-", ""); |
| 119 | goto RET_1; |
| 120 | } |
| 121 | } |
| 122 | if (unlink(dest) < 0) { |
| 123 | bb_perror_msg("cannot remove `%s'", dest); |
| 124 | goto RET_1; |
| 125 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 126 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 127 | if ((copy_file(*argv, dest, |
Glenn L McGrath | 96099d5 | 2004-02-21 07:49:54 +0000 | [diff] [blame] | 128 | FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS) >= 0) && |
| 129 | (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 130 | goto RET_0; |
| 131 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 132 | } |
Glenn L McGrath | 96099d5 | 2004-02-21 07:49:54 +0000 | [diff] [blame] | 133 | RET_1: |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 134 | status = 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 135 | } |
Glenn L McGrath | 96099d5 | 2004-02-21 07:49:54 +0000 | [diff] [blame] | 136 | RET_0: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 137 | if (dest != last) { |
| 138 | free((void *) dest); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 139 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 140 | } while (*++argv != last); |
Glenn L McGrath | 96099d5 | 2004-02-21 07:49:54 +0000 | [diff] [blame] | 141 | |
| 142 | return (status); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 143 | } |