3
0

mv.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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"
  16. //config: default y
  17. //config: help
  18. //config: mv is used to move or rename files or directories.
  19. //config:
  20. //config:config FEATURE_MV_LONG_OPTIONS
  21. //config: bool "Enable long options"
  22. //config: default y
  23. //config: depends on MV && LONG_OPTS
  24. //applet:IF_MV(APPLET(mv, BB_DIR_BIN, BB_SUID_DROP))
  25. //kbuild:lib-$(CONFIG_MV) += mv.o
  26. //usage:#define mv_trivial_usage
  27. //usage: "[-fin] SOURCE DEST\n"
  28. //usage: "or: mv [-fin] SOURCE... DIRECTORY"
  29. //usage:#define mv_full_usage "\n\n"
  30. //usage: "Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY\n"
  31. //usage: "\n -f Don't prompt before overwriting"
  32. //usage: "\n -i Interactive, prompt before overwrite"
  33. //usage: "\n -n Don't overwrite an existing file"
  34. //usage:
  35. //usage:#define mv_example_usage
  36. //usage: "$ mv /tmp/foo /bin/bar\n"
  37. #include "libbb.h"
  38. #include "libcoreutils/coreutils.h"
  39. #if ENABLE_FEATURE_MV_LONG_OPTIONS
  40. static const char mv_longopts[] ALIGN1 =
  41. "interactive\0" No_argument "i"
  42. "force\0" No_argument "f"
  43. "no-clobber\0" No_argument "n"
  44. IF_FEATURE_VERBOSE(
  45. "verbose\0" No_argument "v"
  46. )
  47. ;
  48. #endif
  49. #define OPT_FORCE (1 << 0)
  50. #define OPT_INTERACTIVE (1 << 1)
  51. #define OPT_NOCLOBBER (1 << 2)
  52. #define OPT_VERBOSE ((1 << 3) * ENABLE_FEATURE_VERBOSE)
  53. int mv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  54. int mv_main(int argc, char **argv)
  55. {
  56. struct stat dest_stat;
  57. const char *last;
  58. const char *dest;
  59. unsigned flags;
  60. int dest_exists;
  61. int status = 0;
  62. int copy_flag = 0;
  63. #if ENABLE_FEATURE_MV_LONG_OPTIONS
  64. applet_long_options = mv_longopts;
  65. #endif
  66. /* Need at least two arguments.
  67. * If more than one of -f, -i, -n is specified , only the final one
  68. * takes effect (it unsets previous options).
  69. */
  70. opt_complementary = "-2:f-in:i-fn:n-fi";
  71. flags = getopt32(argv, "finv");
  72. argc -= optind;
  73. argv += optind;
  74. last = argv[argc - 1];
  75. if (argc == 2) {
  76. dest_exists = cp_mv_stat(last, &dest_stat);
  77. if (dest_exists < 0) {
  78. return EXIT_FAILURE;
  79. }
  80. if (!(dest_exists & 2)) { /* last is not a directory */
  81. dest = last;
  82. goto DO_MOVE;
  83. }
  84. }
  85. do {
  86. dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
  87. dest_exists = cp_mv_stat(dest, &dest_stat);
  88. if (dest_exists < 0) {
  89. goto RET_1;
  90. }
  91. DO_MOVE:
  92. if (dest_exists) {
  93. if (flags & OPT_NOCLOBBER)
  94. goto RET_0;
  95. if (!(flags & OPT_FORCE)
  96. && ((access(dest, W_OK) < 0 && isatty(0))
  97. || (flags & OPT_INTERACTIVE))
  98. ) {
  99. if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) {
  100. goto RET_1; /* Ouch! fprintf failed! */
  101. }
  102. if (!bb_ask_confirmation()) {
  103. goto RET_0;
  104. }
  105. }
  106. }
  107. if (rename(*argv, dest) < 0) {
  108. struct stat source_stat;
  109. int source_exists;
  110. if (errno != EXDEV
  111. || (source_exists = cp_mv_stat2(*argv, &source_stat, lstat)) < 1
  112. ) {
  113. bb_perror_msg("can't rename '%s'", *argv);
  114. } else {
  115. static const char fmt[] ALIGN1 =
  116. "can't overwrite %sdirectory with %sdirectory";
  117. if (dest_exists) {
  118. if (dest_exists == 3) {
  119. if (source_exists != 3) {
  120. bb_error_msg(fmt, "", "non-");
  121. goto RET_1;
  122. }
  123. } else {
  124. if (source_exists == 3) {
  125. bb_error_msg(fmt, "non-", "");
  126. goto RET_1;
  127. }
  128. }
  129. if (unlink(dest) < 0) {
  130. bb_perror_msg("can't remove '%s'", dest);
  131. goto RET_1;
  132. }
  133. }
  134. /* FILEUTILS_RECUR also prevents nasties like
  135. * "read from device and write contents to dst"
  136. * instead of "create same device node" */
  137. copy_flag = FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS;
  138. #if ENABLE_SELINUX
  139. copy_flag |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
  140. #endif
  141. if ((copy_file(*argv, dest, copy_flag) >= 0)
  142. && (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)
  143. ) {
  144. goto RET_0;
  145. }
  146. }
  147. RET_1:
  148. status = 1;
  149. }
  150. RET_0:
  151. if (flags & OPT_VERBOSE) {
  152. printf("'%s' -> '%s'\n", *argv, dest);
  153. }
  154. if (dest != last) {
  155. free((void *) dest);
  156. }
  157. } while (*++argv != last);
  158. return status;
  159. }