mv.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini mv implementation for busybox
  4. *
  5. * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
  6. * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  11. *
  12. * Size reduction and improved error checking.
  13. */
  14. //config:config MV
  15. //config: bool "mv (10 kb)"
  16. //config: default y
  17. //config: help
  18. //config: mv is used to move or rename files or directories.
  19. //applet:IF_MV(APPLET_NOEXEC(mv, mv, BB_DIR_BIN, BB_SUID_DROP, mv))
  20. /* NOEXEC despite cases when it can be a "runner" (mv LARGE_DIR OTHER_FS) */
  21. //kbuild:lib-$(CONFIG_MV) += mv.o
  22. //usage:#define mv_trivial_usage
  23. //usage: "[-finT] SOURCE DEST\n"
  24. //usage: "or: mv [-fin] SOURCE... { -t DIRECTORY | DIRECTORY }"
  25. //usage:#define mv_full_usage "\n\n"
  26. //usage: "Rename SOURCE to DEST, or move SOURCEs to DIRECTORY\n"
  27. //usage: "\n -f Don't prompt before overwriting"
  28. //usage: "\n -i Interactive, prompt before overwrite"
  29. //usage: "\n -n Don't overwrite an existing file"
  30. //usage: "\n -T Refuse to move if DEST is a directory"
  31. //usage: "\n -t DIR Move all SOURCEs into DIR"
  32. //usage:
  33. //usage:#define mv_example_usage
  34. //usage: "$ mv /tmp/foo /bin/bar\n"
  35. #include "libbb.h"
  36. #include "libcoreutils/coreutils.h"
  37. int mv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  38. int mv_main(int argc, char **argv)
  39. {
  40. struct stat statbuf;
  41. const char *last;
  42. const char *dest;
  43. unsigned flags;
  44. int dest_exists;
  45. int status = 0;
  46. int copy_flag = 0;
  47. #define OPT_FORCE (1 << 0)
  48. #define OPT_INTERACTIVE (1 << 1)
  49. #define OPT_NOCLOBBER (1 << 2)
  50. #define OPT_DESTNOTDIR (1 << 3)
  51. #define OPT_DESTDIR (1 << 4)
  52. #define OPT_VERBOSE ((1 << 5) * ENABLE_FEATURE_VERBOSE)
  53. flags = getopt32long(argv, "^"
  54. "finTt:v"
  55. "\0"
  56. /* At least one argument. (Usually two+, but -t DIR can have only one) */
  57. "-1"
  58. /* only the final one of -f, -i, -n takes effect */
  59. ":f-in:i-fn:n-fi"
  60. /* -t and -T don't mix */
  61. ":t--T:T--t",
  62. "interactive\0" No_argument "i"
  63. "force\0" No_argument "f"
  64. "no-clobber\0" No_argument "n"
  65. "no-target-directory\0" No_argument "T"
  66. "target-directory\0" Required_argument "t"
  67. IF_FEATURE_VERBOSE(
  68. "verbose\0" No_argument "v",
  69. &last
  70. )
  71. );
  72. argc -= optind;
  73. argv += optind;
  74. if (!(flags & OPT_DESTDIR)) {
  75. last = argv[argc - 1];
  76. if (argc < 2)
  77. bb_show_usage();
  78. if (argc != 2) {
  79. if (flags & OPT_DESTNOTDIR)
  80. bb_show_usage();
  81. /* "mv A B C... DIR" - target must be dir */
  82. } else /* argc == 2 */ {
  83. /* "mv A B" - only case where target can be not a dir */
  84. dest_exists = cp_mv_stat(last, &statbuf);
  85. if (dest_exists < 0) { /* error other than ENOENT */
  86. return EXIT_FAILURE;
  87. }
  88. if (!(dest_exists & 2)) {
  89. /* last is not a directory */
  90. dest = last;
  91. goto DO_MOVE;
  92. }
  93. /* last is a directory */
  94. if (flags & OPT_DESTNOTDIR) {
  95. if (stat(argv[0], &statbuf) == 0 && !S_ISDIR(statbuf.st_mode))
  96. bb_error_msg_and_die("'%s' is a directory", last);
  97. /* "mv -T DIR1 DIR2" is allowed (renames a dir) */
  98. dest = last;
  99. goto DO_MOVE;
  100. }
  101. /* else: fall through into "do { move SRC to DIR/SRC } while" loop */
  102. }
  103. }
  104. /* else: last is DIR from "-t DIR" */
  105. do {
  106. dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
  107. dest_exists = cp_mv_stat(dest, &statbuf);
  108. if (dest_exists < 0) {
  109. goto RET_1;
  110. }
  111. DO_MOVE:
  112. if (dest_exists) {
  113. if (flags & OPT_NOCLOBBER)
  114. goto RET_0;
  115. if (!(flags & OPT_FORCE)
  116. && ((access(dest, W_OK) < 0 && isatty(0))
  117. || (flags & OPT_INTERACTIVE))
  118. ) {
  119. if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) {
  120. goto RET_1; /* Ouch! fprintf failed! */
  121. }
  122. if (!bb_ask_y_confirmation()) {
  123. goto RET_0;
  124. }
  125. }
  126. }
  127. if (rename(*argv, dest) < 0) {
  128. int source_exists;
  129. if (errno != EXDEV
  130. || (source_exists = cp_mv_stat2(*argv, &statbuf, lstat)) < 1
  131. ) {
  132. bb_perror_msg("can't rename '%s'", *argv);
  133. } else {
  134. static const char fmt[] ALIGN1 =
  135. "can't overwrite %sdirectory with %sdirectory";
  136. if (dest_exists) {
  137. if (dest_exists == 3) {
  138. if (source_exists != 3) {
  139. bb_error_msg(fmt, "", "non-");
  140. goto RET_1;
  141. }
  142. } else {
  143. if (source_exists == 3) {
  144. bb_error_msg(fmt, "non-", "");
  145. goto RET_1;
  146. }
  147. }
  148. if (unlink(dest) < 0) {
  149. bb_perror_msg("can't remove '%s'", dest);
  150. goto RET_1;
  151. }
  152. }
  153. /* FILEUTILS_RECUR also prevents nasties like
  154. * "read from device and write contents to dst"
  155. * instead of "create same device node" */
  156. copy_flag = FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS;
  157. #if ENABLE_SELINUX
  158. copy_flag |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
  159. #endif
  160. if ((copy_file(*argv, dest, copy_flag) >= 0)
  161. && (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)
  162. ) {
  163. goto RET_0;
  164. }
  165. }
  166. RET_1:
  167. status = 1;
  168. }
  169. RET_0:
  170. if (flags & OPT_VERBOSE) {
  171. printf("'%s' -> '%s'\n", *argv, dest);
  172. }
  173. if (dest != last) {
  174. free((void *) dest);
  175. }
  176. } while (*++argv && *argv != last);
  177. return status;
  178. }