mv.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 tarball for details.
  9. */
  10. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  11. *
  12. * Size reduction and improved error checking.
  13. */
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <dirent.h>
  17. #include <getopt.h> /* struct option */
  18. #include "libbb.h"
  19. #include "libcoreutils/coreutils.h"
  20. #if ENABLE_FEATURE_MV_LONG_OPTIONS
  21. static const char mv_longopts[] ALIGN1 =
  22. "interactive\0" No_argument "i"
  23. "force\0" No_argument "f"
  24. ;
  25. #endif
  26. #define OPT_FILEUTILS_FORCE 1
  27. #define OPT_FILEUTILS_INTERACTIVE 2
  28. static const char fmt[] ALIGN1 =
  29. "cannot overwrite %sdirectory with %sdirectory";
  30. int mv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  31. int mv_main(int argc, char **argv)
  32. {
  33. struct stat dest_stat;
  34. const char *last;
  35. const char *dest;
  36. unsigned long flags;
  37. int dest_exists;
  38. int status = 0;
  39. int copy_flag = 0;
  40. #if ENABLE_FEATURE_MV_LONG_OPTIONS
  41. applet_long_options = mv_longopts;
  42. #endif
  43. // Need at least two arguments
  44. // -f unsets -i, -i unsets -f
  45. opt_complementary = "-2:f-i:i-f";
  46. flags = getopt32(argv, "fi");
  47. argc -= optind;
  48. argv += optind;
  49. last = argv[argc - 1];
  50. if (argc == 2) {
  51. dest_exists = cp_mv_stat(last, &dest_stat);
  52. if (dest_exists < 0) {
  53. return EXIT_FAILURE;
  54. }
  55. if (!(dest_exists & 2)) {
  56. dest = last;
  57. goto DO_MOVE;
  58. }
  59. }
  60. do {
  61. dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
  62. dest_exists = cp_mv_stat(dest, &dest_stat);
  63. if (dest_exists < 0) {
  64. goto RET_1;
  65. }
  66. DO_MOVE:
  67. if (dest_exists && !(flags & OPT_FILEUTILS_FORCE)
  68. && ((access(dest, W_OK) < 0 && isatty(0))
  69. || (flags & OPT_FILEUTILS_INTERACTIVE))
  70. ) {
  71. if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) {
  72. goto RET_1; /* Ouch! fprintf failed! */
  73. }
  74. if (!bb_ask_confirmation()) {
  75. goto RET_0;
  76. }
  77. }
  78. if (rename(*argv, dest) < 0) {
  79. struct stat source_stat;
  80. int source_exists;
  81. if (errno != EXDEV
  82. || (source_exists = cp_mv_stat(*argv, &source_stat)) < 1
  83. ) {
  84. bb_perror_msg("cannot rename '%s'", *argv);
  85. } else {
  86. if (dest_exists) {
  87. if (dest_exists == 3) {
  88. if (source_exists != 3) {
  89. bb_error_msg(fmt, "", "non-");
  90. goto RET_1;
  91. }
  92. } else {
  93. if (source_exists == 3) {
  94. bb_error_msg(fmt, "non-", "");
  95. goto RET_1;
  96. }
  97. }
  98. if (unlink(dest) < 0) {
  99. bb_perror_msg("cannot remove '%s'", dest);
  100. goto RET_1;
  101. }
  102. }
  103. copy_flag = FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS;
  104. #if ENABLE_SELINUX
  105. copy_flag |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
  106. #endif
  107. if ((copy_file(*argv, dest, copy_flag) >= 0)
  108. && (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)
  109. ) {
  110. goto RET_0;
  111. }
  112. }
  113. RET_1:
  114. status = 1;
  115. }
  116. RET_0:
  117. if (dest != last) {
  118. free((void *) dest);
  119. }
  120. } while (*++argv != last);
  121. return status;
  122. }