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. int mv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  25. int mv_main(int argc, char **argv)
  26. {
  27. struct stat dest_stat;
  28. const char *last;
  29. const char *dest;
  30. unsigned flags;
  31. int dest_exists;
  32. int status = 0;
  33. int copy_flag = 0;
  34. #if ENABLE_FEATURE_MV_LONG_OPTIONS
  35. applet_long_options = mv_longopts;
  36. #endif
  37. // Need at least two arguments
  38. // -f unsets -i, -i unsets -f
  39. opt_complementary = "-2:f-i:i-f";
  40. flags = getopt32(argv, "fi");
  41. argc -= optind;
  42. argv += optind;
  43. last = argv[argc - 1];
  44. if (argc == 2) {
  45. dest_exists = cp_mv_stat(last, &dest_stat);
  46. if (dest_exists < 0) {
  47. return EXIT_FAILURE;
  48. }
  49. if (!(dest_exists & 2)) { /* last is not a directory */
  50. dest = last;
  51. goto DO_MOVE;
  52. }
  53. }
  54. do {
  55. dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
  56. dest_exists = cp_mv_stat(dest, &dest_stat);
  57. if (dest_exists < 0) {
  58. goto RET_1;
  59. }
  60. DO_MOVE:
  61. if (dest_exists
  62. && !(flags & OPT_FILEUTILS_FORCE)
  63. && ((access(dest, W_OK) < 0 && isatty(0))
  64. || (flags & OPT_FILEUTILS_INTERACTIVE))
  65. ) {
  66. if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) {
  67. goto RET_1; /* Ouch! fprintf failed! */
  68. }
  69. if (!bb_ask_confirmation()) {
  70. goto RET_0;
  71. }
  72. }
  73. if (rename(*argv, dest) < 0) {
  74. struct stat source_stat;
  75. int source_exists;
  76. if (errno != EXDEV
  77. || (source_exists = cp_mv_stat2(*argv, &source_stat, lstat)) < 1
  78. ) {
  79. bb_perror_msg("cannot rename '%s'", *argv);
  80. } else {
  81. static const char fmt[] ALIGN1 =
  82. "cannot overwrite %sdirectory with %sdirectory";
  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. }