/
fetch
executable file
·229 lines (127 loc) · 4.05 KB
/
fetch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env bash
rvm_base_except="selector"
source "$rvm_path/scripts/base"
result=0
# Set it to cleanup the download on interruption.
trap 'cleanup_download' 1 2 3 15
cleanup_download()
{
[[ -f "$archive" ]] && rm -f "$archive"
}
record_md5()
{
if [[ "Darwin" = "$(uname)" ]] || [[ "FreeBSD" = "$(uname)" ]]; then
archive_md5="$(/sbin/md5 -q "${archive}")"
else
archive_md5="$(md5sum "${archive}" | awk '{print $1}')"
fi
"$rvm_path/scripts/db" "$rvm_path/config/md5" "$archive" "$archive_md5"
}
builtin cd "${rvm_archives_path:-"$rvm_path/archives"}"
# args=($*) # Reserved for future use
if [[ -z "$1" ]] ; then
"$rvm_path/scripts/log" "fail" \
"BUG: $0 called without an argument :/"
exit 1
fi
url="$1"; download=1 ; package_name="$2"
if ! command -v curl > /dev/null ; then
"$rvm_path/scripts/log" "fail" \
"rvm requires curl. curl was not found in your active path."
exit 1
elif [[ ! -z ${rvm_proxy} ]] ; then
fetch_command="curl -x${rvm_proxy} -L --create-dirs -C - " # -s for silent
else
fetch_command="curl -L --create-dirs -C - " # -s for silent
fi
if [[ ! -z "$package_name" ]] ; then
fetch_command="${fetch_command} -o ${package_name} "
archive="$package_name"
else
fetch_command="${fetch_command} -O "
archive=$(basename "$url")
fi
if [[ ${rvm_debug_flag:-0} -gt 0 ]] ; then
"$rvm_path/scripts/log" "debug" \
"Fetching $archive"
fi
# Check first if we have the correct archive
archive_md5="$("$rvm_path/scripts/db" "$rvm_path/config/md5" "$archive" | head -n1)"
if [[ -e "$archive" && ! -z "$archive_md5" ]] ; then
if [[ ${rvm_debug_flag:-0} -gt 0 ]] ; then
"$rvm_path/scripts/log" "debug" \
"Found archive and its md5, testing correctness"
fi
if ! "$rvm_path/scripts"/md5 "${rvm_archives_path:-"$rvm_path/archives"}/${archive}" "$archive_md5" ; then
if [[ ${rvm_debug_flag:-0} -gt 0 ]] ; then
"$rvm_path/scripts/log" "debug" \
"Archive md5 did not match, downloading"
fi
download=1
else
if [[ ${rvm_debug_flag:-0} -gt 0 ]] ; then
"$rvm_path/scripts/log" "debug" \
"Archive md5 matched, not downloading"
fi
download=0
result=0
fi
else
if [[ ${rvm_debug_flag:-0} -gt 0 ]] ; then
"$rvm_path/scripts/log" "debug" \
"No archive or no MD5, downloading"
fi
download=1
fi
# try to convert the ftp url to a http url
http_url="$(echo "$url" | sed -e 's/ftp:/http:/' -e 's/git:/http:/')"
if [[ $download -gt 0 ]] ; then
rm -f $archive
eval $fetch_command "$url" ; result=$?
if [[ $result -gt 0 ]] ; then
retry=0
try_http=0
if [[ $result -eq 78 ]] ; then
"$rvm_path/scripts/log" "error" \
"The requested url does not exist: '$url'"
try_http=1
elif [[ $result -eq 18 ]] ; then
"$rvm_path/scripts/log" "error" \
"Partial file. Only a part of the file was transferred. Removing partial and re-trying."
rm -f "$archive"
retry=1
elif [[ $result -eq 33 ]] ; then
if [[ ${rvm_debug_flag:-0} -gt 0 ]] ; then
"$rvm_path/scripts/log" "debug" \
"Server does not support 'range' command, removing '$archive'"
fi
rm -f "$archive"
retry=1
else
"$rvm_path/scripts/log" "error" \
"There was an error, please check ${rvm_log_path:-"$rvm_path/log"}/$rvm_ruby_string/*.log. Next we'll try to fetch via http."
try_http=1
fi
if [[ $retry -eq 1 ]] ; then
eval $fetch_command "$url" ; result=$?
if [[ $result -gt 0 ]] ; then
"$rvm_path/scripts/log" "error" \
"There was an error, please check ${rvm_log_path:-"$rvm_path/log"}/$rvm_ruby_string/*.log"
else
record_md5
fi
fi
if [[ $try_http -eq 1 ]] ; then
eval $fetch_command "$http_url" ; result=$?
if [[ $result -gt 0 ]] ; then
"$rvm_path/scripts/log" "error" \
"There was an error, please check ${rvm_log_path:-"$rvm_path/log"}/$rvm_ruby_string/*.log"
else
record_md5
fi
fi
else
record_md5
fi
fi
exit $result