3
0

mv.c 3.1 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 "libbb.h"
  15. #include "libcoreutils/coreutils.h"
  16. #if ENABLE_FEATURE_MV_LONG_OPTIONS
  17. static const char mv_longopts[] ALIGN1 =
  18. "interactive\0" No_argument "i"
  19. "force\0" No_argument "f"
  20. ;
  21. #endif
  22. #define OPT_FILEUTILS_FORCE 1
  23. #define OPT_FILEUTILS_INTERACTIVE 2
  24. static const char fmt[] ALIGN1 =
  25. "cannot overwrite %sdirectory with %sdirectory";
  26. int mv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  27. int mv_main(int argc, char **argv)
  28. {
  29. struct stat dest_stat;
  30. const char *last;
  31. const char *dest;
  32. unsigned long flags;
  33. int dest_exists;
  34. int status = 0;
  35. int copy_flag = 0;
  36. #if ENABLE_FEATURE_MV_LONG_OPTIONS
  37. applet_long_options = mv_longopts;
  38. #endif
  39. // Need at least two arguments
  40. // -f unsets -i, -i unsets -f
  41. opt_complementary = "-2:f-i:i-f";
  42. flags = getopt32(argv, "fi");
  43. argc -= optind;
  44. argv += optind;
  45. last = argv[argc - 1];
  46. if (argc == 2) {
  47. dest_exists = cp_mv_stat(last, &dest_stat);
  48. if (dest_exists < 0) {
  49. return EXIT_FAILURE;
  50. }
  51. if (!(dest_exists & 2)) {
  52. dest = last;
  53. goto DO_MOVE;
  54. }
  55. }
  56. do {
  57. dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
  58. dest_exists = cp_mv_stat(dest, &dest_stat);
  59. if (dest_exists < 0) {
  60. goto RET_1;
  61. }
  62. DO_MOVE:
  63. if (dest_exists
  64. && !(flags & OPT_FILEUTILS_FORCE)
  65. && ((access(dest, W_OK) < 0 && isatty(0))
  66. || (flags & OPT_FILEUTILS_INTERACTIVE))
  67. ) {
  68. if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) {
  69. goto RET_1; /* Ouch! fprintf failed! */
  70. }
  71. if (!bb_ask_confirmation()) {
  72. goto RET_0;
  73. }
  74. }
  75. if (rename(*argv, dest) < 0) {
  76. struct stat source_stat;
  77. int source_exists;
  78. if (errno != EXDEV
  79. || (source_exists = cp_mv_stat(*argv, &source_stat)) < 1
  80. ) {
  81. bb_perror_msg("cannot rename '%s'", *argv);
  82. } else {
  83. if (dest_exists) {
  84. if (dest_exists == 3) {
  85. if (source_exists != 3) {
  86. bb_error_msg(fmt, "", "non-");
  87. goto RET_1;
  88. }
  89. } else {
  90. if (source_exists == 3) {
  91. bb_error_msg(fmt, "non-", "");
  92. goto RET_1;
  93. }
  94. }
  95. if (unlink(dest) < 0) {
  96. bb_perror_msg("cannot remove '%s'", dest);
  97. goto RET_1;
  98. }
  99. }
  100. /* FILEUTILS_RECUR also prevents nasties like
  101. * "read from device and write contents to dst"
  102. * instead of "create same device node" */
  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. }