utility_system 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #!/usr/bin/env bash
  2. # params: [printf_if=-] [suffix]
  3. __rvm_system_path()
  4. {
  5. rvm_remote_server_path="$(__rvm_db "rvm_remote_server_path${2:-}")"
  6. [[ -n "${rvm_remote_server_path}" ]] ||
  7. rvm_remote_server_path="${_system_name_lowercase}/${_system_version}/${_system_arch}"
  8. if [[ "${1:-}" == "-" ]]
  9. then printf "%b" "${rvm_remote_server_path}\n"
  10. fi
  11. }
  12. __rvm_remote_extension()
  13. {
  14. case "$1" in
  15. *.tar.*)
  16. rvm_remote_extension="tar${1##*tar}"
  17. ;;
  18. jruby-*)
  19. rvm_remote_extension="tar.gz"
  20. ;;
  21. *)
  22. rvm_remote_extension="tar.bz2"
  23. ;;
  24. esac
  25. [[ "$2" != "-" ]] || printf "%b" "${rvm_remote_extension}"
  26. }
  27. # params: ruby-string-to-transform
  28. __rvm_ruby_package_file()
  29. {
  30. case "$1" in
  31. *.tar.*)
  32. rvm_ruby_package_file="/$1"
  33. ;;
  34. rbx*|rubinius*)
  35. rvm_ruby_package_file="/${1//rbx/rubinius}.$(__rvm_remote_extension "$1" -)"
  36. ;;
  37. jruby-head)
  38. rvm_ruby_package_file="/jruby-head.$(__rvm_remote_extension "$1" -)"
  39. ;;
  40. jruby*)
  41. \typeset __version
  42. __version="$(
  43. rvm_ruby_string="$1"
  44. rvm_remote_flag=0 __rvm_ruby_string
  45. echo "$rvm_ruby_version"
  46. )"
  47. rvm_ruby_package_file="/${__version}/jruby-dist-${__version}-bin.$(__rvm_remote_extension "$1" -)"
  48. ;;
  49. "")
  50. rvm_ruby_package_file=""
  51. ;;
  52. ruby*|mruby*)
  53. rvm_ruby_package_file="/$1.$(__rvm_remote_extension "$1" -)"
  54. ;;
  55. *)
  56. rvm_ruby_package_file="/ruby-$1.$(__rvm_remote_extension "$1" -)"
  57. ;;
  58. esac
  59. }
  60. __rvm_include_travis_binaries()
  61. {
  62. if [[ $rvm_remote_server_url == *"travis"* && $TRAVIS != true && $_system_name_lowercase == "osx" ]]
  63. then
  64. rvm_debug "Travis binaries for OSX are not movable and can't be used outside of Travis environment. \
  65. Skip that source."
  66. return 1
  67. fi
  68. return 0
  69. }
  70. __rvm_calculate_remote_file()
  71. {
  72. rvm_remote_server_url="$( __rvm_db "rvm_remote_server_url${3:-}" )"
  73. [[ -n "$rvm_remote_server_url" ]] || {
  74. rvm_debug "rvm_remote_server_url${3:-} not found"
  75. return $1
  76. }
  77. __rvm_include_travis_binaries || return $1
  78. __rvm_system_path "" "${3:-}"
  79. __rvm_ruby_package_file "${4:-}"
  80. __remote_file="${rvm_remote_server_url}/${rvm_remote_server_path}${rvm_ruby_package_file}"
  81. }
  82. # params: return_on_end return_on_failed_link [suffix] [file]
  83. __rvm_remote_server_path_single()
  84. {
  85. \typeset __remote_file
  86. __rvm_calculate_remote_file "$@" || return $?
  87. if
  88. [[ -z "${__remote_file:-}" ]]
  89. then
  90. rvm_debug "No remote file name found"
  91. return $1
  92. elif
  93. file_exists_at_url "${__remote_file}"
  94. then
  95. rvm_debug "Remote file exists ${__remote_file}"
  96. printf "%b" "$( __rvm_db "rvm_remote_server_verify_downloads${3:-}" ):${__remote_file}"
  97. elif
  98. [[ -f "${rvm_archives_path}/${rvm_ruby_package_file##*/}" && "${rvm_ruby_package_file##*/}" == *bin-* ]]
  99. then
  100. rvm_debug "Cached file exists ${__remote_file}"
  101. printf "%b" "$( __rvm_db "rvm_remote_server_verify_downloads${3:-}" ):${rvm_archives_path}/${rvm_ruby_package_file##*/}"
  102. else
  103. rvm_debug "Remote file does not exist ${__remote_file}"
  104. return $2
  105. fi
  106. }
  107. # params: [file_name]
  108. __rvm_remote_server_path()
  109. {
  110. \typeset _iterator
  111. _iterator=""
  112. while ! __rvm_remote_server_path_single 0 1 "${_iterator}" "${1:-}"
  113. do : $(( _iterator+=1 ))
  114. done
  115. }
  116. __list_remote_all()
  117. {
  118. \typeset _iterator rvm_remote_server_url rvm_remote_server_path
  119. _iterator=""
  120. while
  121. __rvm_db "rvm_remote_server_url${_iterator:-}" rvm_remote_server_url
  122. do
  123. if
  124. __rvm_include_travis_binaries
  125. then
  126. __rvm_system_path "" "${_iterator}"
  127. rvm_debug "__list_remote_all${_iterator:-} $rvm_remote_server_url $rvm_remote_server_path"
  128. __list_remote_for "${rvm_remote_server_url}" "$rvm_remote_server_path"
  129. fi
  130. : $(( _iterator+=1 ))
  131. done | \command \sort -u | __rvm_version_sort
  132. }
  133. __list_remote_for()
  134. {
  135. __list_remote_for_local "$@" ||
  136. __list_remote_for_index "$@" ||
  137. __list_remote_for_s3 "$@" ||
  138. return $?
  139. }
  140. __list_remote_for_local()
  141. {
  142. \typeset __status1 __status2
  143. __status1=0
  144. __status2=0
  145. if [[ -f $rvm_user_path/remote ]]
  146. then __rvm_grep "${1}/${2}" < $rvm_user_path/remote || __status1=$?
  147. fi
  148. __rvm_grep "${1}/${2}" < $rvm_path/config/remote || __status2=$?
  149. if (( __status1 || __status2 ))
  150. then return 1
  151. else rvm_debug "__list_remote_for_local found"
  152. fi
  153. true # for OSX
  154. }
  155. __list_remote_for_index()
  156. {
  157. if
  158. file_exists_at_url "${1}/index.txt"
  159. then
  160. rvm_debug "__list_remote_for_index ${1}/index.txt"
  161. __rvm_curl -s "${1}/index.txt" | GREP_OPTIONS="" \command \grep -E "${1}/${2}/.*\.tar\.(gz|bz2)$"
  162. elif
  163. file_exists_at_url "${1}/${2}/index.txt"
  164. then
  165. rvm_debug "__list_remote_for_index ${1}/${2}/index.txt"
  166. __rvm_curl -s "${1}/${2}/index.txt" | GREP_OPTIONS="" \command \grep -E "${1}/${2}/.*\.tar\.(gz|bz2)$"
  167. else
  168. return 1
  169. fi
  170. true # for OSX
  171. }
  172. __list_remote_for_s3()
  173. {
  174. curl -ILfs "${1}" | __rvm_grep "Server: AmazonS3" >/dev/null || return $?
  175. \typeset __tmp_name __iterator __next __local_url
  176. __iterator=0
  177. __next=""
  178. __tmp_name="$(
  179. : ${TMPDIR:=${rvm_tmp_path:-/tmp}}
  180. mktemp "${TMPDIR}/tmp.XXXXXXXXXXXXXXXXXX"
  181. )"
  182. while
  183. [[ __iterator -eq 0 || -n "${__next}" ]]
  184. do
  185. __local_url="${1}?prefix=${2}/${__next:+&marker=${__next}}"
  186. rvm_debug "__list_remote_for_s3-${__iterator} ${__local_url}"
  187. __rvm_curl -s "${__local_url}" > "${__tmp_name}${__iterator}"
  188. GREP_OPTIONS="" \command \grep -oE "<Key>[^<]*</Key>" < "${__tmp_name}${__iterator}" |
  189. __rvm_awk -F"[<>]" '{print $3}' > "${__tmp_name}"
  190. if __rvm_grep "<IsTruncated>true</IsTruncated>" < "${__tmp_name}${__iterator}"
  191. then __next="$(__rvm_tail -n 1 "${__tmp_name}")"
  192. else __next=""
  193. fi
  194. rm "${__tmp_name}${__iterator}"
  195. : $(( __iterator+=1 ))
  196. done
  197. GREP_OPTIONS="" \command \grep -E "${2}/.*\.tar\.(gz|bz2)$" < "${__tmp_name}" |
  198. GREP_OPTIONS="" \command \grep -v -- "-src-" |
  199. __rvm_awk "{ print "'"'$1/'"'"\$1 }"
  200. rm "${__tmp_name}"*
  201. }
  202. __rvm_get_user_shell()
  203. {
  204. case "${_system_type}:${_system_name}" in
  205. (Linux:*|SunOS:*|BSD:*|Windows:Cygwin)
  206. __shell="$( getent passwd $USER )" ||
  207. {
  208. rvm_error "Error checking user shell via getent ... something went wrong, report a bug."
  209. return 2
  210. }
  211. echo "${__shell##*:}"
  212. ;;
  213. (Windows:Mingw)
  214. __shell="$( echo $SHELL )" ||
  215. {
  216. rvm_error "Error checking user shell from echo $SHELL ... something went wrong, report a bug."
  217. return 2
  218. }
  219. echo "${__shell##*:}"
  220. ;;
  221. (Darwin:*)
  222. \typeset __version
  223. __version="$(dscl localhost -read "/Search/Users/$USER" UserShell)" ||
  224. {
  225. rvm_error "Error checking user shell via dscl ... something went wrong, report a bug."
  226. return 3
  227. }
  228. echo ${__version#*: }
  229. ;;
  230. (*)
  231. rvm_error "Do not know how to check user shell on '$(command uname)'."
  232. return 1
  233. ;;
  234. esac
  235. }