mv.c 3.8 KB

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