mv.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. ;
  33. #endif
  34. #define OPT_FILEUTILS_FORCE 1
  35. #define OPT_FILEUTILS_INTERACTIVE 2
  36. #define OPT_FILEUTILS_NOCLOBBER 4
  37. int mv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  38. int mv_main(int argc, char **argv)
  39. {
  40. struct stat dest_stat;
  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. #if ENABLE_FEATURE_MV_LONG_OPTIONS
  48. applet_long_options = mv_longopts;
  49. #endif
  50. /* Need at least two arguments.
  51. * If more than one of -f, -i, -n is specified , only the final one
  52. * takes effect (it unsets previous options). */
  53. opt_complementary = "-2:f-in:i-fn:n-fi";
  54. flags = getopt32(argv, "fin");
  55. argc -= optind;
  56. argv += optind;
  57. last = argv[argc - 1];
  58. if (argc == 2) {
  59. dest_exists = cp_mv_stat(last, &dest_stat);
  60. if (dest_exists < 0) {
  61. return EXIT_FAILURE;
  62. }
  63. if (!(dest_exists & 2)) { /* last is not a directory */
  64. dest = last;
  65. goto DO_MOVE;
  66. }
  67. }
  68. do {
  69. dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
  70. dest_exists = cp_mv_stat(dest, &dest_stat);
  71. if (dest_exists < 0) {
  72. goto RET_1;
  73. }
  74. DO_MOVE:
  75. if (dest_exists) {
  76. if (flags & OPT_FILEUTILS_NOCLOBBER)
  77. goto RET_0;
  78. if (!(flags & OPT_FILEUTILS_FORCE)
  79. && ((access(dest, W_OK) < 0 && isatty(0))
  80. || (flags & OPT_FILEUTILS_INTERACTIVE))
  81. ) {
  82. if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) {
  83. goto RET_1; /* Ouch! fprintf failed! */
  84. }
  85. if (!bb_ask_confirmation()) {
  86. goto RET_0;
  87. }
  88. }
  89. }
  90. if (rename(*argv, dest) < 0) {
  91. struct stat source_stat;
  92. int source_exists;
  93. if (errno != EXDEV
  94. || (source_exists = cp_mv_stat2(*argv, &source_stat, lstat)) < 1
  95. ) {
  96. bb_perror_msg("can't rename '%s'", *argv);
  97. } else {
  98. static const char fmt[] ALIGN1 =
  99. "can't overwrite %sdirectory with %sdirectory";
  100. if (dest_exists) {
  101. if (dest_exists == 3) {
  102. if (source_exists != 3) {
  103. bb_error_msg(fmt, "", "non-");
  104. goto RET_1;
  105. }
  106. } else {
  107. if (source_exists == 3) {
  108. bb_error_msg(fmt, "non-", "");
  109. goto RET_1;
  110. }
  111. }
  112. if (unlink(dest) < 0) {
  113. bb_perror_msg("can't remove '%s'", dest);
  114. goto RET_1;
  115. }
  116. }
  117. /* FILEUTILS_RECUR also prevents nasties like
  118. * "read from device and write contents to dst"
  119. * instead of "create same device node" */
  120. copy_flag = FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS;
  121. #if ENABLE_SELINUX
  122. copy_flag |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
  123. #endif
  124. if ((copy_file(*argv, dest, copy_flag) >= 0)
  125. && (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)
  126. ) {
  127. goto RET_0;
  128. }
  129. }
  130. RET_1:
  131. status = 1;
  132. }
  133. RET_0:
  134. if (dest != last) {
  135. free((void *) dest);
  136. }
  137. } while (*++argv != last);
  138. return status;
  139. }