mv.c 4.0 KB

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