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 | * |
| 5 | * |
| 6 | * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <unistd.h> |
| 27 | #include <dirent.h> |
| 28 | #include <errno.h> |
| 29 | #include <stdlib.h> |
| 30 | |
| 31 | #include "busybox.h" |
| 32 | |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame^] | 33 | static int flags; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 34 | |
| 35 | static int remove_file(const char *path, struct stat *statbuf, void *junk) |
| 36 | { |
| 37 | if (unlink(path) < 0) |
| 38 | return FALSE; |
| 39 | return TRUE; |
| 40 | } |
| 41 | |
| 42 | static int remove_directory(const char *path, struct stat *statbuf, void *junk) |
| 43 | { |
| 44 | if (rmdir(path) < 0) |
| 45 | return FALSE; |
| 46 | return TRUE; |
| 47 | } |
| 48 | |
| 49 | static int manual_rename(const char *source, const char *dest) |
| 50 | { |
| 51 | struct stat source_stat; |
| 52 | struct stat dest_stat; |
| 53 | int source_exists = 1; |
| 54 | int dest_exists = 1; |
| 55 | |
| 56 | if (stat(source, &source_stat) < 0) { |
| 57 | if (errno != ENOENT) { |
| 58 | perror_msg("unable to stat `%s'", source); |
| 59 | return -1; |
| 60 | } |
| 61 | source_exists = 0; |
| 62 | } |
| 63 | |
| 64 | if (stat(dest, &dest_stat) < 0) { |
| 65 | if (errno != ENOENT) { |
| 66 | perror_msg("unable to stat `%s'", dest); |
| 67 | return -1; |
| 68 | } |
| 69 | dest_exists = 0; |
| 70 | } |
| 71 | |
| 72 | if (dest_exists) { |
| 73 | if (S_ISDIR(dest_stat.st_mode) && |
| 74 | (!source_exists || !S_ISDIR(source_stat.st_mode))) { |
| 75 | error_msg("cannot overwrite directory with non-directory"); |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | if (!S_ISDIR(dest_stat.st_mode) && source_exists && |
| 80 | S_ISDIR(source_stat.st_mode)) { |
| 81 | error_msg("cannot overwrite non-directory with directory"); |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | if (unlink(dest) < 0) { |
| 86 | perror_msg("cannot remove `%s'", dest); |
| 87 | return -1; |
| 88 | } |
| 89 | } |
| 90 | |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame^] | 91 | if (copy_file(source, dest, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS | |
| 92 | FILEUTILS_PRESERVE_SYMLINKS) < 0) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 93 | return -1; |
| 94 | |
| 95 | if (!recursive_action(source, TRUE, FALSE, TRUE, remove_file, |
| 96 | remove_directory, NULL)) |
| 97 | return -1; |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int move_file(const char *source, const char *dest) |
| 103 | { |
| 104 | struct stat dest_stat; |
| 105 | int dest_exists = 1; |
| 106 | |
| 107 | if (stat(dest, &dest_stat) < 0) { |
| 108 | if (errno != ENOENT) { |
| 109 | perror_msg("unable to stat `%s'", dest); |
| 110 | return -1; |
| 111 | } |
| 112 | dest_exists = 0; |
| 113 | } |
| 114 | |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame^] | 115 | if (dest_exists && !(flags & FILEUTILS_FORCE) && |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 116 | ((access(dest, W_OK) < 0 && isatty(0)) || |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame^] | 117 | (flags & FILEUTILS_INTERACTIVE))) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 118 | fprintf(stderr, "mv: overwrite `%s'? ", dest); |
| 119 | if (!ask_confirmation()) |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | if (rename(source, dest) < 0) { |
| 124 | if (errno == EXDEV) |
| 125 | return manual_rename(source, dest); |
| 126 | |
| 127 | perror_msg("unable to rename `%s'", source); |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | extern int mv_main(int argc, char **argv) |
| 135 | { |
| 136 | int status = 0; |
| 137 | int opt; |
| 138 | int i; |
| 139 | |
| 140 | while ((opt = getopt(argc, argv, "fi")) != -1) |
| 141 | switch (opt) { |
| 142 | case 'f': |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame^] | 143 | flags &= ~FILEUTILS_INTERACTIVE; |
| 144 | flags |= FILEUTILS_FORCE; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 145 | break; |
| 146 | case 'i': |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame^] | 147 | flags &= ~FILEUTILS_FORCE; |
| 148 | flags |= FILEUTILS_INTERACTIVE; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 149 | break; |
| 150 | default: |
| 151 | show_usage(); |
| 152 | } |
| 153 | |
| 154 | if (optind + 2 > argc) |
| 155 | show_usage(); |
| 156 | |
| 157 | if (optind + 2 == argc) { |
| 158 | struct stat dest_stat; |
| 159 | int dest_exists = 1; |
| 160 | |
| 161 | if (stat(argv[optind + 1], &dest_stat) < 0) { |
| 162 | if (errno != ENOENT) |
| 163 | perror_msg_and_die("unable to stat `%s'", argv[optind + 1]); |
| 164 | dest_exists = 0; |
| 165 | } |
| 166 | |
| 167 | if (!dest_exists || !S_ISDIR(dest_stat.st_mode)) { |
| 168 | if (move_file(argv[optind], argv[optind + 1]) < 0) |
| 169 | status = 1; |
| 170 | return status; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | for (i = optind; i < argc - 1; i++) { |
| 175 | char *dest = concat_path_file(argv[argc - 1], |
| 176 | get_last_path_component(argv[i])); |
| 177 | if (move_file(argv[i], dest) < 0) |
| 178 | status = 1; |
| 179 | free(dest); |
| 180 | } |
| 181 | |
| 182 | return status; |
| 183 | } |