Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini `cp' and `mv' implementation for BusyBox. |
| 4 | * |
| 5 | * |
| 6 | * Copyright (C) 1999 by Lineo, inc. |
| 7 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
| 8 | * |
| 9 | * Copyright (C) 2000 by BitterSweet Enterprises, LLC. (GPL) |
| 10 | * Extensively modified and rewritten by Karl M. Hegbloom <karlheg@debian.org> |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | #include "internal.h" |
| 29 | #define BB_DECLARE_EXTERN |
| 30 | #define bb_need_name_too_long |
| 31 | #define bb_need_omitting_directory |
| 32 | #define bb_need_not_a_directory |
| 33 | #include "messages.c" |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <time.h> |
| 37 | #include <utime.h> |
| 38 | #include <dirent.h> |
| 39 | #include <sys/param.h> |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 40 | #include <setjmp.h> |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 41 | #include <string.h> |
| 42 | #include <unistd.h> |
| 43 | #include <errno.h> |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 44 | |
| 45 | #define is_cp 0 |
| 46 | #define is_mv 1 |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 47 | static int dz_i; /* index into cp_mv_usage */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 48 | static const char *dz; /* dollar zero, .bss */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 49 | static const char *cp_mv_usage[] = /* .rodata */ |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 50 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 51 | "cp [OPTION]... SOURCE DEST\n" |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 52 | " or: cp [OPTION]... SOURCE... DIRECTORY\n" |
| 53 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 54 | "\nCopies SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n" |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | "\n" |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 56 | "\t-a\tSame as -dpR\n" |
| 57 | "\t-d\tPreserves links\n" |
| 58 | "\t-p\tPreserves file attributes if possible\n" |
| 59 | "\t-R\tCopies directories recursively\n" |
| 60 | #endif |
| 61 | , |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 62 | "mv SOURCE DEST\n" |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 63 | " or: mv SOURCE... DIRECTORY\n" |
| 64 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 65 | "\nRename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n" |
| 66 | #endif |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 69 | static int recursiveFlag; |
| 70 | static int followLinks; |
| 71 | static int preserveFlag; |
| 72 | |
| 73 | static const char *baseSrcName; |
| 74 | static int srcDirFlag; |
| 75 | static struct stat srcStatBuf; |
| 76 | |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame^] | 77 | static char baseDestName[BUFSIZ + 1]; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 78 | static size_t baseDestLen; |
| 79 | static int destDirFlag; |
| 80 | static struct stat destStatBuf; |
| 81 | |
| 82 | static jmp_buf catch; |
| 83 | static volatile int mv_Action_first_time; |
| 84 | |
| 85 | static void name_too_long__exit (void) __attribute__((noreturn)); |
| 86 | |
| 87 | static |
| 88 | void name_too_long__exit (void) |
| 89 | { |
| 90 | fprintf(stderr, name_too_long, dz); |
| 91 | exit FALSE; |
| 92 | } |
| 93 | |
| 94 | static void |
| 95 | fill_baseDest_buf(char *_buf, size_t * _buflen) { |
| 96 | const char *srcBasename; |
| 97 | if ((srcBasename = strrchr(baseSrcName, '/')) == NULL) { |
| 98 | srcBasename = baseSrcName; |
| 99 | if (_buf[*_buflen - 1] != '/') { |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame^] | 100 | if (++(*_buflen) > BUFSIZ) |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 101 | name_too_long__exit(); |
| 102 | strcat(_buf, "/"); |
| 103 | } |
| 104 | } |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame^] | 105 | if (*_buflen + strlen(srcBasename) > BUFSIZ) |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 106 | name_too_long__exit(); |
| 107 | strcat(_buf, srcBasename); |
| 108 | return; |
| 109 | |
| 110 | } |
| 111 | |
| 112 | static int |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 113 | cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk) |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 114 | { |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame^] | 115 | char destName[BUFSIZ + 1]; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 116 | size_t destLen; |
| 117 | const char *srcBasename; |
| 118 | char *name; |
| 119 | |
| 120 | strcpy(destName, baseDestName); |
| 121 | destLen = strlen(destName); |
| 122 | |
| 123 | if (srcDirFlag == TRUE) { |
| 124 | if (recursiveFlag == FALSE) { |
| 125 | fprintf(stderr, omitting_directory, dz, baseSrcName); |
| 126 | return TRUE; |
| 127 | } |
| 128 | srcBasename = (strstr(fileName, baseSrcName) |
| 129 | + strlen(baseSrcName)); |
| 130 | |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame^] | 131 | if (destLen + strlen(srcBasename) > BUFSIZ) { |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 132 | fprintf(stderr, name_too_long, dz); |
| 133 | return FALSE; |
| 134 | } |
| 135 | strcat(destName, srcBasename); |
| 136 | } |
| 137 | else if (destDirFlag == TRUE) { |
| 138 | fill_baseDest_buf(&destName[0], &destLen); |
| 139 | } |
| 140 | else { |
| 141 | srcBasename = baseSrcName; |
| 142 | } |
| 143 | if (mv_Action_first_time && (dz_i == is_mv)) { |
| 144 | mv_Action_first_time = errno = 0; |
| 145 | if (rename(fileName, destName) < 0 && errno != EXDEV) { |
| 146 | fprintf(stderr, "%s: rename(%s, %s): %s\n", |
| 147 | dz, fileName, destName, strerror(errno)); |
| 148 | goto do_copyFile; /* Try anyway... */ |
| 149 | } |
| 150 | else if (errno == EXDEV) |
| 151 | goto do_copyFile; |
| 152 | else |
| 153 | longjmp(catch, 1); /* succeeded with rename() */ |
| 154 | } |
| 155 | do_copyFile: |
| 156 | if (preserveFlag == TRUE && statbuf->st_nlink > 1) { |
| 157 | if (is_in_ino_dev_hashtable(statbuf, &name)) { |
| 158 | if (link(name, destName) < 0) { |
| 159 | fprintf(stderr, "%s: link(%s, %s): %s\n", |
| 160 | dz, name, destName, strerror(errno)); |
| 161 | return FALSE; |
| 162 | } |
| 163 | return TRUE; |
| 164 | } |
| 165 | else { |
| 166 | add_to_ino_dev_hashtable(statbuf, destName); |
| 167 | } |
| 168 | } |
| 169 | return copyFile(fileName, destName, preserveFlag, followLinks); |
| 170 | } |
| 171 | |
| 172 | static int |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 173 | rm_Action(const char *fileName, struct stat *statbuf, void* junk) |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 174 | { |
| 175 | int status = TRUE; |
| 176 | |
| 177 | if (S_ISDIR(statbuf->st_mode)) { |
| 178 | if (rmdir(fileName) < 0) { |
| 179 | fprintf(stderr, "%s: rmdir(%s): %s\n", dz, fileName, strerror(errno)); |
| 180 | status = FALSE; |
| 181 | } |
| 182 | } else if (unlink(fileName) < 0) { |
| 183 | fprintf(stderr, "%s: unlink(%s): %s\n", dz, fileName, strerror(errno)); |
| 184 | status = FALSE; |
| 185 | } |
| 186 | return status; |
| 187 | } |
| 188 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 189 | extern int cp_mv_main(int argc, char **argv) |
| 190 | { |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 191 | dz = *argv; /* already basename'd by busybox.c:main */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 192 | if (*dz == 'c' && *(dz + 1) == 'p') |
| 193 | dz_i = is_cp; |
| 194 | else |
| 195 | dz_i = is_mv; |
| 196 | if (argc < 3) |
| 197 | usage(cp_mv_usage[dz_i]); |
| 198 | argc--; |
| 199 | argv++; |
| 200 | |
| 201 | if (dz_i == is_cp) { |
| 202 | recursiveFlag = preserveFlag = FALSE; |
| 203 | followLinks = TRUE; |
| 204 | while (**argv == '-') { |
| 205 | while (*++(*argv)) { |
| 206 | switch (**argv) { |
| 207 | case 'a': |
| 208 | followLinks = FALSE; |
| 209 | preserveFlag = TRUE; |
| 210 | recursiveFlag = TRUE; |
| 211 | break; |
| 212 | case 'd': |
| 213 | followLinks = FALSE; |
| 214 | break; |
| 215 | case 'p': |
| 216 | preserveFlag = TRUE; |
| 217 | break; |
| 218 | case 'R': |
| 219 | recursiveFlag = TRUE; |
| 220 | break; |
| 221 | default: |
| 222 | usage(cp_mv_usage[is_cp]); |
| 223 | } |
| 224 | } |
| 225 | argc--; |
| 226 | argv++; |
| 227 | } |
| 228 | } else { /* (dz_i == is_mv) */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 229 | recursiveFlag = preserveFlag = TRUE; |
| 230 | followLinks = FALSE; |
| 231 | } |
| 232 | |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame^] | 233 | if (strlen(argv[argc - 1]) > BUFSIZ) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 234 | fprintf(stderr, name_too_long, "cp"); |
| 235 | goto exit_false; |
| 236 | } |
| 237 | strcpy(baseDestName, argv[argc - 1]); |
| 238 | baseDestLen = strlen(baseDestName); |
| 239 | if (baseDestLen == 0) |
| 240 | goto exit_false; |
| 241 | |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 242 | destDirFlag = isDirectory(baseDestName, TRUE, &destStatBuf); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 243 | if ((argc > 3) && destDirFlag == FALSE) { |
| 244 | fprintf(stderr, not_a_directory, "cp", baseDestName); |
| 245 | goto exit_false; |
| 246 | } |
| 247 | |
| 248 | while (argc-- > 1) { |
| 249 | size_t srcLen; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 250 | volatile int flags_memo; |
| 251 | int status; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 252 | |
| 253 | baseSrcName = *(argv++); |
| 254 | |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame^] | 255 | if ((srcLen = strlen(baseSrcName)) > BUFSIZ) |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 256 | name_too_long__exit(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 257 | |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 258 | if (srcLen == 0) continue; /* "" */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 259 | |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 260 | srcDirFlag = isDirectory(baseSrcName, followLinks, &srcStatBuf); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 261 | |
| 262 | if ((flags_memo = (recursiveFlag == TRUE && |
| 263 | srcDirFlag == TRUE && destDirFlag == TRUE))) { |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 264 | |
| 265 | struct stat sb; |
| 266 | int state = 0; |
| 267 | char *pushd, *d, *p; |
| 268 | |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame^] | 269 | if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) { |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 270 | fprintf(stderr, "%s: getcwd(): %s\n", dz, strerror(errno)); |
| 271 | continue; |
| 272 | } |
| 273 | if (chdir(baseDestName) < 0) { |
| 274 | fprintf(stderr, "%s: chdir(%s): %s\n", dz, baseSrcName, strerror(errno)); |
| 275 | continue; |
| 276 | } |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame^] | 277 | if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) { |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 278 | fprintf(stderr, "%s: getcwd(): %s\n", dz, strerror(errno)); |
| 279 | continue; |
| 280 | } |
| 281 | while (!state && *d != '\0') { |
| 282 | if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */ |
| 283 | fprintf(stderr, "%s: stat(%s) :%s\n", dz, d, strerror(errno)); |
| 284 | state = -1; |
| 285 | continue; |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 286 | } |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 287 | if ((sb.st_ino == srcStatBuf.st_ino) && |
| 288 | (sb.st_dev == srcStatBuf.st_dev)) { |
| 289 | fprintf(stderr, |
| 290 | "%s: Cannot %s `%s' " |
| 291 | "into a subdirectory of itself, `%s/%s'\n", |
| 292 | dz, dz, baseSrcName, baseDestName, baseSrcName); |
| 293 | state = -1; |
| 294 | continue; |
| 295 | } |
| 296 | if ((p = strrchr(d, '/')) != NULL) { |
| 297 | *p = '\0'; |
| 298 | } |
| 299 | } |
| 300 | if (chdir(pushd) < 0) { |
| 301 | fprintf(stderr, "%s: chdir(%s): %s\n", dz, pushd, strerror(errno)); |
| 302 | free(pushd); |
| 303 | free(d); |
| 304 | continue; |
| 305 | } |
| 306 | free(pushd); |
| 307 | free(d); |
| 308 | if (state < 0) |
| 309 | continue; |
| 310 | else |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 311 | fill_baseDest_buf(baseDestName, &baseDestLen); |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 312 | } |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 313 | status = setjmp(catch); |
| 314 | if (status == 0) { |
| 315 | mv_Action_first_time = 1; |
| 316 | if (recursiveAction(baseSrcName, |
| 317 | recursiveFlag, followLinks, FALSE, |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 318 | cp_mv_Action, cp_mv_Action, NULL) == FALSE) goto exit_false; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 319 | if (dz_i == is_mv && |
| 320 | recursiveAction(baseSrcName, |
| 321 | recursiveFlag, followLinks, TRUE, |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 322 | rm_Action, rm_Action, NULL) == FALSE) goto exit_false; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 323 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 324 | if (flags_memo) |
| 325 | *(baseDestName + baseDestLen) = '\0'; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 326 | } |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 327 | // exit_true: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 328 | exit TRUE; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 329 | exit_false: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 330 | exit FALSE; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 333 | /* |
| 334 | Local Variables: |
| 335 | c-file-style: "linux" |
| 336 | c-basic-offset: 4 |
| 337 | tab-width: 4 |
| 338 | End: |
| 339 | */ |