fetch 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #!/usr/bin/env bash
  2. url="$1"
  3. archive="$2"
  4. [[ -n "$url" ]] || rvm_fail "BUG: no url given for fetch"
  5. shift || rvm_fail "BUG: $0 called without an argument :/"
  6. rvm_base_except="selector"
  7. source "$rvm_scripts_path/base"
  8. # handled by teardown - scripts/functions/environment:314
  9. __rvm_cleanup_download()
  10. {
  11. [[ -f "$archive" ]] && __rvm_rm_rf "$archive"
  12. }
  13. __rvm_cd "$rvm_archives_path"
  14. [[ -n "$archive" ]] || archive=$(basename "$url")
  15. fetch_command=( __rvm_curl --create-dirs -C - -o "${archive}.part" )
  16. download=1
  17. try_ftp=0
  18. result=0
  19. retry=0
  20. __rvm_checksum_read "$url" "$archive"
  21. __rvm_checksum_any || (( ${rvm_verify_downloads_flag_cli:-${rvm_verify_downloads_flag:-1}} > 0 )) ||
  22. rvm_fail "There is no checksum for '$url' or '$archive', it's not possible to validate it.
  23. This could be because your RVM install's list of versions is out of date. You may want to
  24. update your list of rubies by running 'rvm get stable' and try again.
  25. If that does not resolve the issue and you wish to continue with unverified download
  26. add '--verify-downloads 1' after the command.
  27. "
  28. function verification_check()
  29. {
  30. if
  31. (( ${rvm_verify_downloads_flag_cli:-${rvm_verify_downloads_flag:-1}}>=$1 ))
  32. then
  33. rvm_debug "Archive ${archive} checksum $2, continuing anyway."
  34. else
  35. rvm_warn "Archive ${archive} checksum $2, downloading again."
  36. download=1
  37. fi
  38. }
  39. rvm_debug "Fetch $url => $archive"
  40. download=0
  41. if
  42. [[ -e "$archive" ]]
  43. then
  44. # Check first if we have the correct archive
  45. if
  46. __rvm_checksum_validate_file "$rvm_archives_path/${archive}"
  47. then
  48. rvm_debug " ...archive checksum matched, not downloading"
  49. else
  50. case $? in
  51. (1) verification_check 1 "not found" ;;
  52. (*) verification_check 2 "did not match" ;;
  53. esac
  54. fi
  55. else
  56. rvm_debug " ...no archive, downloading"
  57. download=1
  58. fi
  59. function download_the_url()
  60. {
  61. \typeset counter=${1:-0}
  62. if
  63. (( counter >3 ))
  64. then
  65. rvm_error "ERROR: Retried 3 times and failed!"
  66. return 200
  67. fi
  68. "${fetch_command[@]}" "$url" &&
  69. \command \mv "${archive}.part" "${archive}" ||
  70. {
  71. result=$?
  72. case "$result" in
  73. (22|78)
  74. rvm_error "The requested url does not exist($result): '$url'"
  75. ;;
  76. (18)
  77. rvm_error "Partial file($result). Only a part of the file was transferred. Removing partial and re-trying."
  78. \command \rm -f "${archive}.part"
  79. download_the_url $counter
  80. ;;
  81. (33)
  82. rvm_debug "Server does not support 'range' command($result), removing '${archive}.part'"
  83. \command \rm -f "${archive}.part"
  84. download_the_url $counter
  85. ;;
  86. # duplication marker lfdgzkngdkjvnfjknkjvcnbjkncvjxbn
  87. (60)
  88. log "
  89. Could not download '${_url}', you can read more about it here:
  90. https://rvm.io/support/fixing-broken-ssl-certificates/
  91. To continue in insecure mode run 'echo insecure >> ~/.curlrc'.
  92. "
  93. ;;
  94. # duplication marker lfdgzkngdkjvnfjknkjvcnbjkncvjxbn
  95. (77)
  96. log "
  97. It looks like you have old certificates, you can read more about it here:
  98. https://rvm.io/support/fixing-broken-ssl-certificates/
  99. "
  100. ;;
  101. # duplication marker lfdgzkngdkjvnfjknkjvcnbjkncvjxbn
  102. (141)
  103. rvm_warn "Curl returned 141 - it is result of a segfault which means it's Curls fault.
  104. Try again and if it crashes more than a couple of times you either need to
  105. reinstall Curl or consult with your distribution manual and contact support."
  106. ;;
  107. (*)
  108. rvm_error "There was an error($result)."
  109. ;;
  110. esac
  111. return $result
  112. }
  113. }
  114. if
  115. (( download > 0 ))
  116. then
  117. download_the_url ||
  118. {
  119. \typeset __fallback __default_url __default __iterator result=$?
  120. \typeset -a __fallbacks
  121. __fallbacks=()
  122. for __default in ruby rubygems
  123. do
  124. __default_url="$(__rvm_db ${__default}_url)"
  125. if
  126. [[ "${url}" == "${__default_url}"* ]]
  127. then
  128. __iterator=1
  129. while
  130. __fallback="$(__rvm_db ${__default}_url_fallback_${__iterator})" &&
  131. [[ -n "${__fallback}" ]]
  132. do
  133. __fallback_patern="$(__rvm_db ${__default}_url_fallback_${__iterator}_pattern)"
  134. if
  135. [[ -n "${__fallback_patern}" ]]
  136. then
  137. __fallbacks+=( "$(__rvm_sed -E "s#${__fallback_patern}#${__fallback}#" <<<"${url}")" )
  138. else
  139. __fallbacks+=( "${url/${__default_url}/${__fallback}}" )
  140. fi
  141. : $(( __iterator+=1 ))
  142. done
  143. fi
  144. done
  145. if
  146. [[ -n "${__fallbacks[*]}" ]] # already detected something
  147. then
  148. true
  149. elif
  150. [[ "${url}" == "http://"* ]]
  151. then
  152. __fallbacks+=( "${url/http:/ftp:}" "${url/http:\/\//https://www.mirrorservice.org/sites/}" )
  153. elif
  154. [[ "${url}" == "https://"* ]]
  155. then
  156. __fallbacks+=( "${url/https:/ftp:}" "${url/https:\/\//https://www.mirrorservice.org/sites/}" )
  157. else
  158. rvm_fail "
  159. No fallback URL can be calculated for ${url}, open a ticket with full output here:
  160. https://github.com/rvm/rvm/issues
  161. " $result
  162. fi
  163. for __fallback in "${__fallbacks[@]}"
  164. do
  165. rvm_warn "Checking fallback: ${__fallback}"
  166. if
  167. file_exists_at_url "${__fallback}"
  168. then
  169. url="${__fallback}"
  170. download_the_url 1 || rvm_fail "Failed download" $?
  171. result=0
  172. break
  173. fi
  174. done
  175. if
  176. (( result ))
  177. then
  178. rvm_fail "\
  179. No fallback URL could be found, try increasing timeout with:
  180. echo \"export rvm_max_time_flag=20\" >> ~/.rvmrc
  181. " $result
  182. fi
  183. }
  184. fi
  185. # Check if we have downloaded the correct archive
  186. if
  187. __rvm_checksum_validate_file "$rvm_archives_path/${archive}"
  188. then
  189. rvm_debug "Downloaded archive checksum matched."
  190. else
  191. result=$?
  192. if
  193. (( result==1 && ${rvm_verify_downloads_flag_cli:-${rvm_verify_downloads_flag:-1}}>0 ))
  194. then
  195. rvm_warn "No checksum for downloaded archive, recording checksum in user configuration."
  196. __rvm_checksum_calculate_file "$archive"
  197. __rvm_checksum_write "$url"
  198. elif
  199. (( result>1 && ${rvm_verify_downloads_flag_cli:-${rvm_verify_downloads_flag:-1}}>1 ))
  200. then
  201. rvm_warn "Downloaded archive checksum did not match!"
  202. elif
  203. (( result == 1 ))
  204. then
  205. rvm_fail "Downloaded archive checksum could not be verified!
  206. If you wish to continue with unverified download add '--verify-downloads 1' after the command.
  207. "
  208. else
  209. \command \rm -f $archive
  210. rvm_fail "Downloaded archive checksum did not match, archive was removed!
  211. If you wish to continue with not matching download add '--verify-downloads 2' after the command.
  212. "
  213. fi
  214. fi