cleanup 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/env bash
  2. rvm_base_except="selector"
  3. source "$rvm_scripts_path/base"
  4. chmod_unless_link()
  5. {
  6. if [[ -e "$1" && ! -L "$1" ]]
  7. then chmod -R u+w "$1" || return $?
  8. fi
  9. }
  10. remove_or_log()
  11. {
  12. rvm_verbose_log " - removing - $1"
  13. chmod_unless_link "$1" &&
  14. __rvm_rm_rf "$1" ||
  15. {
  16. : $(( failed_counter+=1 ))
  17. rvm_error " - failed removing $1 - please check ownership and permissions"
  18. }
  19. }
  20. empty_or_log()
  21. {
  22. rvm_verbose_log " - emptying - $1"
  23. chmod_unless_link "$1" &&
  24. echo "" > "$1" ||
  25. {
  26. : $(( failed_counter+=1 ))
  27. rvm_error " - failed emptying $1 - please check ownership and permissions"
  28. }
  29. }
  30. cleanup_single()
  31. {
  32. \typeset current_path entry
  33. current_path="${rvm_path}/${1}"
  34. if
  35. [[ -n "$current_path" && -d "$current_path" && "$current_path" != "/" ]]
  36. then
  37. for entry in "$current_path"/*
  38. do
  39. case $entry in
  40. (*\*) continue ;; # skip empty dirs
  41. esac
  42. remove_or_log "$entry"
  43. done
  44. fi
  45. }
  46. cleanup_checksums()
  47. {
  48. empty_or_log "${rvm_user_path:-${rvm_path}/user}/md5"
  49. empty_or_log "${rvm_user_path:-${rvm_path}/user}/sha512"
  50. }
  51. cleanup_gemsets()
  52. {
  53. \typeset current_path ruby_path
  54. for current_path in "${rvm_path}"/gems/*
  55. do
  56. case "${current_path##*/}" in
  57. (\*) continue ;; # skip no gemsets
  58. (cache) continue ;; # skip cache dir
  59. esac
  60. ruby_path="${current_path%@*}"
  61. ruby_path="${ruby_path//\/gems\///rubies/}"
  62. [[ -d "${ruby_path}" ]] ||
  63. remove_or_log "$current_path"
  64. done
  65. \typeset __found __search_path_gems __search_path_rubies gemset_name __need_a_cleanup=0
  66. for __found in "$rvm_path"/environments/* "$rvm_path"/wrappers/*
  67. do
  68. gemset_name="${__found##*/}"
  69. __search_path_gems="$rvm_path/gems/${gemset_name}"
  70. __search_path_rubies="$rvm_path/rubies/${gemset_name}"
  71. if
  72. [[ -e "$__found" &&
  73. "$gemset_name" != default &&
  74. ! -e "$__search_path_gems" &&
  75. ! -e "$__search_path_rubies"
  76. ]]
  77. then
  78. remove_or_log "$__found"
  79. fi
  80. done
  81. (( __need_a_cleanup )) ||
  82. for __found in "$rvm_path"/gems/*
  83. do
  84. gemset_name="${__found##*/}"
  85. gemset_name="${gemset_name%@*}"
  86. __search_path_rubies="$rvm_path/rubies/${gemset_name}"
  87. if
  88. [[ -e "$__found" &&
  89. "$gemset_name" != cache &&
  90. ! -e "$__search_path_rubies"
  91. ]]
  92. then
  93. remove_or_log "$__found"
  94. fi
  95. done
  96. }
  97. cleanup_links()
  98. {
  99. \typeset current_link
  100. \typeset -a all_links
  101. __rvm_read_lines all_links <(
  102. __rvm_find "${rvm_path}"/{environments,wrappers,gems,bin} -type l
  103. )
  104. for current_link in "${all_links[@]}"
  105. do
  106. if
  107. [[ -L "${current_link}" && ! -s "${current_link}" ]]
  108. then
  109. remove_or_log "${current_link}"
  110. fi
  111. done
  112. }
  113. cleanup()
  114. {
  115. while (( $# ))
  116. do
  117. rvm_log "Cleaning up rvm $1"
  118. case "$1" in
  119. (archives)
  120. cleanup_checksums
  121. cleanup_single $1
  122. ;;
  123. (checksums|gemsets|links)
  124. cleanup_$1
  125. ;;
  126. (logs)
  127. cleanup_single log
  128. ;;
  129. (sources)
  130. cleanup_single src
  131. ;;
  132. (*)
  133. cleanup_single $1
  134. ;;
  135. esac
  136. shift
  137. done
  138. }
  139. failed_counter=0
  140. case "$1" in
  141. archives|checksums|repos|sources|tmp|gemsets|logs|links)
  142. cleanup "$1"
  143. ;;
  144. all)
  145. cleanup archives repos src log tmp gemsets links
  146. ;;
  147. help)
  148. rvm_help cleanup
  149. ;;
  150. *)
  151. rvm_help cleanup
  152. exit 1
  153. ;;
  154. esac
  155. if (( failed_counter ))
  156. then rvm_warn "Cleanup failed for $failed_counter"
  157. else rvm_log "Cleanup done."
  158. fi