mv.c 4.0 KB

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