repair 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/env bash
  2. source "$rvm_scripts_path/base"
  3. usage()
  4. {
  5. printf "%b" "
  6. Usage:
  7. rvm repair [option]
  8. Options:
  9. wrappers - Repair wrappers
  10. symlinks - Repair symlinks
  11. environments - Repair environments
  12. archives - Repair archives
  13. gemsets - Repair gemsets
  14. all - Repair all of the above
  15. "
  16. }
  17. repair_gemsets()
  18. {
  19. \typeset directory directories
  20. rvm_log "Removing gemsets missing names or interpreters."
  21. (
  22. __rvm_cd "${rvm_gems_path:-"rvm_path/gems"}"
  23. directories=(
  24. $( __rvm_find . -mindepth 1 -maxdepth 1 -type d | __rvm_grep '@$' )
  25. $( __rvm_find . -mindepth 1 -maxdepth 1 -type d | __rvm_grep '^./@')
  26. )
  27. for directory in "${directories[@]//.\/}"
  28. do
  29. __rvm_rm_rf "./$directory/"
  30. done
  31. )
  32. rvm_log "Gemsets repaired."
  33. return 0
  34. }
  35. repair_wrappers()
  36. {
  37. rvm_log "Regenerating all wrappers..."
  38. __rvm_read_lines ruby_names <(__rvm_list_gemset_strings)
  39. for ruby_name in "${ruby_names[@]}"
  40. do
  41. __rvm_log_command \
  42. "wrappers.regenerate.$ruby_name" \
  43. "Regenerating wrappers for $ruby_name" \
  44. __rvm_with $ruby_name run_gem_wrappers regenerate ||
  45. failed_wrappers+=( "$wrapper_ruby_name" )
  46. done
  47. if (( ${#failed_wrappers[@]} ))
  48. then rvm_error "Wrappers regeneration failed for: ${failed_wrappers[*]}"
  49. else rvm_log "Wrappers regenerated"
  50. fi
  51. return ${#failed_wrappers[@]}
  52. }
  53. # Removes stale symlinks in $rvm_bin_path, likely related to wrappers.
  54. repair_symlinks()
  55. {
  56. rvm_log "Repairing symlinks..."
  57. for executable_name in "${rvm_bin_path}"/*
  58. do
  59. if
  60. [[ -L "$executable_name" && ! -e "$executable_name" && "$executable_name" != "${rvm_bin_path}/*" ]]
  61. then
  62. rvm_log "removing stale symlink from $(basename "$executable_name")"
  63. \command \rm -f "$executable_name"
  64. fi
  65. done
  66. rvm_log "Symlinks repaired"
  67. }
  68. # Regenerates each symlink file.
  69. repair_environments()
  70. {
  71. \typeset environment_name environments
  72. rvm_log "Regenerating environments..."
  73. environments=($(__rvm_cd "$rvm_environments_path" ; __rvm_find . -maxdepth 1 -mindepth 1 -type f))
  74. for environment_name in "${environments[@]//.\/}"
  75. do
  76. [[ -L "$rvm_environments_path/$environment_name" ]] && continue
  77. rvm_log "Regenerating environment file for '$environment_name'"
  78. [[ -f "$rvm_environments_path/$environment_name" ]] && \command \rm -f "$rvm_environments_path/$environment_name"
  79. (
  80. source "$rvm_scripts_path/base"
  81. __rvm_become "$environment_name"
  82. __rvm_ensure_has_environment_files
  83. )
  84. done
  85. rvm_log "Environments regenerated"
  86. }
  87. # Removes archives that have incorrect md5 sums.
  88. repair_archives()
  89. {
  90. \typeset archive_file archives stored_md5sum
  91. rvm_log "Repairing archives..."
  92. __rvm_read_lines archives <(
  93. __rvm_cd "${rvm_archives_path}"
  94. __rvm_find . -maxdepth 1 -mindepth 1 -type f
  95. )
  96. for archive_file in "${archives[@]//.\/}"
  97. do
  98. if
  99. __rvm_checksum_read "$archive_file" &&
  100. __rvm_checksum_any
  101. then
  102. __rvm_checksum_validate_file "${rvm_archives_path}/$archive_file" ||
  103. {
  104. rvm_log "Removing archive for '$archive_file' - Incorrect md5 checksum."
  105. __rvm_rm_rf "${rvm_archives_path}/$archive_file"
  106. }
  107. fi
  108. done
  109. rvm_log "Archives repaired"
  110. return 0
  111. }
  112. repair_all()
  113. {
  114. repair_symlinks
  115. repair_archives
  116. repair_environments
  117. repair_wrappers
  118. return 0
  119. }
  120. args=($*)
  121. action="${args[$__array_start]}"
  122. args[$__array_start]=""
  123. args=(${args[@]})
  124. if [[ -z "$action" ]]
  125. then
  126. usage
  127. exit $?
  128. fi
  129. case "$action" in
  130. all) repair_all ;;
  131. symlinks) repair_symlinks ;;
  132. gemsets) repair_gemsets ;;
  133. environments) repair_environments ;;
  134. archives) repair_archives ;;
  135. wrappers) repair_wrappers ;;
  136. help) usage ;;
  137. *) usage >&2 ; exit 1 ;;
  138. esac
  139. exit $?