mv.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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: "\nOptions:"
  22. //usage: "\n -f Don't prompt before overwriting"
  23. //usage: "\n -i Interactive, prompt before overwrite"
  24. //usage: "\n -n Don't overwrite an existing file"
  25. //usage:
  26. //usage:#define mv_example_usage
  27. //usage: "$ mv /tmp/foo /bin/bar\n"
  28. #if ENABLE_FEATURE_MV_LONG_OPTIONS
  29. static const char mv_longopts[] ALIGN1 =
  30. "interactive\0" No_argument "i"
  31. "force\0" No_argument "f"
  32. "no-clobber\0" No_argument "n"
  33. ;
  34. #endif
  35. #define OPT_FILEUTILS_FORCE 1
  36. #define OPT_FILEUTILS_INTERACTIVE 2
  37. #define OPT_FILEUTILS_NOCLOBBER 4
  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. opt_complementary = "-2:f-in:i-fn:n-fi";
  55. flags = getopt32(argv, "fin");
  56. argc -= optind;
  57. argv += optind;
  58. last = argv[argc - 1];
  59. if (argc == 2) {
  60. dest_exists = cp_mv_stat(last, &dest_stat);
  61. if (dest_exists < 0) {
  62. return EXIT_FAILURE;
  63. }
  64. if (!(dest_exists & 2)) { /* last is not a directory */
  65. dest = last;
  66. goto DO_MOVE;
  67. }
  68. }
  69. do {
  70. dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
  71. dest_exists = cp_mv_stat(dest, &dest_stat);
  72. if (dest_exists < 0) {
  73. goto RET_1;
  74. }
  75. DO_MOVE:
  76. if (dest_exists) {
  77. if (flags & OPT_FILEUTILS_NOCLOBBER)
  78. goto RET_0;
  79. if (!(flags & OPT_FILEUTILS_FORCE)
  80. && ((access(dest, W_OK) < 0 && isatty(0))
  81. || (flags & OPT_FILEUTILS_INTERACTIVE))
  82. ) {
  83. if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) {
  84. goto RET_1; /* Ouch! fprintf failed! */
  85. }
  86. if (!bb_ask_confirmation()) {
  87. goto RET_0;
  88. }
  89. }
  90. }
  91. if (rename(*argv, dest) < 0) {
  92. struct stat source_stat;
  93. int source_exists;
  94. if (errno != EXDEV
  95. || (source_exists = cp_mv_stat2(*argv, &source_stat, lstat)) < 1
  96. ) {
  97. bb_perror_msg("can't rename '%s'", *argv);
  98. } else {
  99. static const char fmt[] ALIGN1 =
  100. "can't overwrite %sdirectory with %sdirectory";
  101. if (dest_exists) {
  102. if (dest_exists == 3) {
  103. if (source_exists != 3) {
  104. bb_error_msg(fmt, "", "non-");
  105. goto RET_1;
  106. }
  107. } else {
  108. if (source_exists == 3) {
  109. bb_error_msg(fmt, "non-", "");
  110. goto RET_1;
  111. }
  112. }
  113. if (unlink(dest) < 0) {
  114. bb_perror_msg("can't remove '%s'", dest);
  115. goto RET_1;
  116. }
  117. }
  118. /* FILEUTILS_RECUR also prevents nasties like
  119. * "read from device and write contents to dst"
  120. * instead of "create same device node" */
  121. copy_flag = FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS;
  122. #if ENABLE_SELINUX
  123. copy_flag |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
  124. #endif
  125. if ((copy_file(*argv, dest, copy_flag) >= 0)
  126. && (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)
  127. ) {
  128. goto RET_0;
  129. }
  130. }
  131. RET_1:
  132. status = 1;
  133. }
  134. RET_0:
  135. if (dest != last) {
  136. free((void *) dest);
  137. }
  138. } while (*++argv != last);
  139. return status;
  140. }