]>
Commit | Line | Data |
---|---|---|
b6fdc44c | 1 | #include "git-compat-util.h" |
cf78ae4f | 2 | #include "repository.h" |
41771fa4 | 3 | #include "hex.h" |
30ae764b | 4 | #include "walker.h" |
29508e1e | 5 | #include "http.h" |
94e99012 | 6 | #include "list.h" |
abcbdc03 | 7 | #include "transport.h" |
d6fe0036 | 8 | #include "packfile.h" |
a034e910 | 9 | #include "object-store-ll.h" |
7baa3e86 | 10 | |
9cba13ca | 11 | struct alt_base { |
2afea3bc | 12 | char *base; |
b3661567 DB |
13 | int got_indices; |
14 | struct packed_git *packs; | |
15 | struct alt_base *next; | |
16 | }; | |
17 | ||
e388ab74 | 18 | enum object_request_state { |
1d389ab6 NH |
19 | WAITING, |
20 | ABORTED, | |
21 | ACTIVE, | |
4b05548f | 22 | COMPLETE |
1d389ab6 | 23 | }; |
6eb7ed54 | 24 | |
9cba13ca | 25 | struct object_request { |
30ae764b | 26 | struct walker *walker; |
16f0705d | 27 | struct object_id oid; |
1d389ab6 | 28 | struct alt_base *repo; |
e388ab74 | 29 | enum object_request_state state; |
5424bc55 | 30 | struct http_object_request *req; |
94e99012 | 31 | struct list_head node; |
1d389ab6 NH |
32 | }; |
33 | ||
e388ab74 | 34 | struct alternates_request { |
30ae764b | 35 | struct walker *walker; |
8e29f6a0 | 36 | const char *base; |
54ba4c5f | 37 | struct strbuf *url; |
028c2976 | 38 | struct strbuf *buffer; |
acc075a8 NH |
39 | struct active_request_slot *slot; |
40 | int http_specific; | |
41 | }; | |
42 | ||
30ae764b DB |
43 | struct walker_data { |
44 | const char *url; | |
45 | int got_alternates; | |
46 | struct alt_base *alt; | |
30ae764b DB |
47 | }; |
48 | ||
94e99012 | 49 | static LIST_HEAD(object_queue_head); |
bc8f2652 | 50 | |
30ae764b | 51 | static void fetch_alternates(struct walker *walker, const char *base); |
1d389ab6 | 52 | |
29508e1e | 53 | static void process_object_response(void *callback_data); |
1d389ab6 | 54 | |
647edf79 | 55 | static void start_object_request(struct object_request *obj_req) |
1d389ab6 | 56 | { |
1d389ab6 | 57 | struct active_request_slot *slot; |
5424bc55 | 58 | struct http_object_request *req; |
1d389ab6 | 59 | |
f0be0db1 | 60 | req = new_http_object_request(obj_req->repo->base, &obj_req->oid); |
afe8a907 | 61 | if (!req) { |
e388ab74 | 62 | obj_req->state = ABORTED; |
1d389ab6 NH |
63 | return; |
64 | } | |
5424bc55 | 65 | obj_req->req = req; |
1d389ab6 | 66 | |
5424bc55 | 67 | slot = req->slot; |
29508e1e | 68 | slot->callback_func = process_object_response; |
e388ab74 | 69 | slot->callback_data = obj_req; |
1d389ab6 | 70 | |
a7a8d378 | 71 | /* Try to get the request started, abort the request on error */ |
e388ab74 | 72 | obj_req->state = ACTIVE; |
1d389ab6 | 73 | if (!start_active_slot(slot)) { |
e388ab74 | 74 | obj_req->state = ABORTED; |
5424bc55 | 75 | release_http_object_request(req); |
e388ab74 | 76 | return; |
1d389ab6 | 77 | } |
1d389ab6 NH |
78 | } |
79 | ||
e388ab74 | 80 | static void finish_object_request(struct object_request *obj_req) |
1d389ab6 | 81 | { |
5424bc55 | 82 | if (finish_http_object_request(obj_req->req)) |
1d389ab6 | 83 | return; |
1d389ab6 | 84 | |
5424bc55 | 85 | if (obj_req->req->rename == 0) |
16f0705d | 86 | walker_say(obj_req->walker, "got %s\n", oid_to_hex(&obj_req->oid)); |
1d389ab6 NH |
87 | } |
88 | ||
29508e1e NH |
89 | static void process_object_response(void *callback_data) |
90 | { | |
e388ab74 NH |
91 | struct object_request *obj_req = |
92 | (struct object_request *)callback_data; | |
30ae764b DB |
93 | struct walker *walker = obj_req->walker; |
94 | struct walker_data *data = walker->data; | |
95 | struct alt_base *alt = data->alt; | |
29508e1e | 96 | |
5424bc55 | 97 | process_http_object_request(obj_req->req); |
e388ab74 | 98 | obj_req->state = COMPLETE; |
29508e1e | 99 | |
ccbbd8bf JK |
100 | normalize_curl_result(&obj_req->req->curl_result, |
101 | obj_req->req->http_code, | |
102 | obj_req->req->errorstr, | |
103 | sizeof(obj_req->req->errorstr)); | |
104 | ||
29508e1e | 105 | /* Use alternates if necessary */ |
5424bc55 | 106 | if (missing_target(obj_req->req)) { |
30ae764b | 107 | fetch_alternates(walker, alt->base); |
afe8a907 | 108 | if (obj_req->repo->next) { |
e388ab74 NH |
109 | obj_req->repo = |
110 | obj_req->repo->next; | |
5424bc55 | 111 | release_http_object_request(obj_req->req); |
647edf79 | 112 | start_object_request(obj_req); |
29508e1e NH |
113 | return; |
114 | } | |
115 | } | |
116 | ||
e388ab74 | 117 | finish_object_request(obj_req); |
29508e1e NH |
118 | } |
119 | ||
e388ab74 | 120 | static void release_object_request(struct object_request *obj_req) |
1d389ab6 | 121 | { |
5424bc55 TRC |
122 | if (obj_req->req !=NULL && obj_req->req->localfile != -1) |
123 | error("fd leakage in release: %d", obj_req->req->localfile); | |
1d389ab6 | 124 | |
94e99012 | 125 | list_del(&obj_req->node); |
e388ab74 | 126 | free(obj_req); |
1d389ab6 NH |
127 | } |
128 | ||
1e5e0974 | 129 | static int fill_active_slot(void *data UNUSED) |
1d389ab6 | 130 | { |
45c17412 | 131 | struct object_request *obj_req; |
94e99012 | 132 | struct list_head *pos, *tmp, *head = &object_queue_head; |
1d389ab6 | 133 | |
94e99012 EW |
134 | list_for_each_safe(pos, tmp, head) { |
135 | obj_req = list_entry(pos, struct object_request, node); | |
e388ab74 | 136 | if (obj_req->state == WAITING) { |
bc726bd0 | 137 | if (repo_has_object_file(the_repository, &obj_req->oid)) |
09db444f | 138 | obj_req->state = COMPLETE; |
45c17412 | 139 | else { |
647edf79 | 140 | start_object_request(obj_req); |
45c17412 DB |
141 | return 1; |
142 | } | |
f1a906a3 | 143 | } |
8fcf7f9a | 144 | } |
45c17412 | 145 | return 0; |
1d389ab6 NH |
146 | } |
147 | ||
30ae764b | 148 | static void prefetch(struct walker *walker, unsigned char *sha1) |
1d389ab6 | 149 | { |
e388ab74 | 150 | struct object_request *newreq; |
30ae764b | 151 | struct walker_data *data = walker->data; |
1d389ab6 NH |
152 | |
153 | newreq = xmalloc(sizeof(*newreq)); | |
30ae764b | 154 | newreq->walker = walker; |
92e2cab9 | 155 | oidread(&newreq->oid, sha1); |
30ae764b | 156 | newreq->repo = data->alt; |
1d389ab6 | 157 | newreq->state = WAITING; |
5424bc55 | 158 | newreq->req = NULL; |
1d389ab6 | 159 | |
e9176745 | 160 | http_is_verbose = walker->get_verbosely; |
94e99012 | 161 | list_add_tail(&newreq->node, &object_queue_head); |
29508e1e | 162 | |
29508e1e NH |
163 | fill_active_slots(); |
164 | step_active_slots(); | |
1d389ab6 NH |
165 | } |
166 | ||
abcbdc03 JK |
167 | static int is_alternate_allowed(const char *url) |
168 | { | |
169 | const char *protocols[] = { | |
170 | "http", "https", "ftp", "ftps" | |
171 | }; | |
172 | int i; | |
173 | ||
de461388 EW |
174 | if (http_follow_config != HTTP_FOLLOW_ALWAYS) { |
175 | warning("alternate disabled by http.followRedirects: %s", url); | |
176 | return 0; | |
177 | } | |
178 | ||
abcbdc03 JK |
179 | for (i = 0; i < ARRAY_SIZE(protocols); i++) { |
180 | const char *end; | |
181 | if (skip_prefix(url, protocols[i], &end) && | |
182 | starts_with(end, "://")) | |
183 | break; | |
184 | } | |
185 | ||
186 | if (i >= ARRAY_SIZE(protocols)) { | |
187 | warning("ignoring alternate with unknown protocol: %s", url); | |
188 | return 0; | |
189 | } | |
190 | if (!is_transport_allowed(protocols[i], 0)) { | |
191 | warning("ignoring alternate with restricted protocol: %s", url); | |
192 | return 0; | |
193 | } | |
194 | ||
195 | return 1; | |
196 | } | |
197 | ||
e388ab74 | 198 | static void process_alternates_response(void *callback_data) |
b3661567 | 199 | { |
e388ab74 NH |
200 | struct alternates_request *alt_req = |
201 | (struct alternates_request *)callback_data; | |
30ae764b DB |
202 | struct walker *walker = alt_req->walker; |
203 | struct walker_data *cdata = walker->data; | |
acc075a8 | 204 | struct active_request_slot *slot = alt_req->slot; |
30ae764b | 205 | struct alt_base *tail = cdata->alt; |
8e29f6a0 | 206 | const char *base = alt_req->base; |
a04ff3ec | 207 | const char null_byte = '\0'; |
acc075a8 NH |
208 | char *data; |
209 | int i = 0; | |
1d389ab6 | 210 | |
ccbbd8bf JK |
211 | normalize_curl_result(&slot->curl_result, slot->http_code, |
212 | curl_errorstr, sizeof(curl_errorstr)); | |
213 | ||
acc075a8 NH |
214 | if (alt_req->http_specific) { |
215 | if (slot->curl_result != CURLE_OK || | |
028c2976 | 216 | !alt_req->buffer->len) { |
acc075a8 NH |
217 | |
218 | /* Try reusing the slot to get non-http alternates */ | |
219 | alt_req->http_specific = 0; | |
54ba4c5f JK |
220 | strbuf_reset(alt_req->url); |
221 | strbuf_addf(alt_req->url, "%s/objects/info/alternates", | |
222 | base); | |
acc075a8 | 223 | curl_easy_setopt(slot->curl, CURLOPT_URL, |
54ba4c5f | 224 | alt_req->url->buf); |
acc075a8 NH |
225 | active_requests++; |
226 | slot->in_use = 1; | |
afe8a907 | 227 | if (slot->finished) |
c9826473 | 228 | (*slot->finished) = 0; |
a3f583cb | 229 | if (!start_active_slot(slot)) { |
30ae764b | 230 | cdata->got_alternates = -1; |
29508e1e | 231 | slot->in_use = 0; |
afe8a907 | 232 | if (slot->finished) |
c9826473 | 233 | (*slot->finished) = 1; |
1d389ab6 | 234 | } |
a3f583cb | 235 | return; |
b3661567 | 236 | } |
acc075a8 | 237 | } else if (slot->curl_result != CURLE_OK) { |
be4a015b | 238 | if (!missing_target(slot)) { |
30ae764b | 239 | cdata->got_alternates = -1; |
acc075a8 NH |
240 | return; |
241 | } | |
b3661567 DB |
242 | } |
243 | ||
a04ff3ec | 244 | fwrite_buffer((char *)&null_byte, 1, 1, alt_req->buffer); |
028c2976 MH |
245 | alt_req->buffer->len--; |
246 | data = alt_req->buffer->buf; | |
1b0c1e67 | 247 | |
028c2976 | 248 | while (i < alt_req->buffer->len) { |
b3661567 | 249 | int posn = i; |
028c2976 | 250 | while (posn < alt_req->buffer->len && data[posn] != '\n') |
b3661567 DB |
251 | posn++; |
252 | if (data[posn] == '\n') { | |
1b0c1e67 DB |
253 | int okay = 0; |
254 | int serverlen = 0; | |
255 | struct alt_base *newalt; | |
b3661567 | 256 | if (data[i] == '/') { |
4c42aa1a TRC |
257 | /* |
258 | * This counts | |
5df1e0d0 JH |
259 | * http://git.host/pub/scm/linux.git/ |
260 | * -----------here^ | |
261 | * so memcpy(dst, base, serverlen) will | |
262 | * copy up to "...git.host". | |
263 | */ | |
264 | const char *colon_ss = strstr(base,"://"); | |
265 | if (colon_ss) { | |
266 | serverlen = (strchr(colon_ss + 3, '/') | |
267 | - base); | |
268 | okay = 1; | |
269 | } | |
1b0c1e67 | 270 | } else if (!memcmp(data + i, "../", 3)) { |
4c42aa1a TRC |
271 | /* |
272 | * Relative URL; chop the corresponding | |
5df1e0d0 JH |
273 | * number of subpath from base (and ../ |
274 | * from data), and concatenate the result. | |
275 | * | |
276 | * The code first drops ../ from data, and | |
277 | * then drops one ../ from data and one path | |
278 | * from base. IOW, one extra ../ is dropped | |
279 | * from data than path is dropped from base. | |
280 | * | |
281 | * This is not wrong. The alternate in | |
282 | * http://git.host/pub/scm/linux.git/ | |
283 | * to borrow from | |
284 | * http://git.host/pub/scm/linus.git/ | |
285 | * is ../../linus.git/objects/. You need | |
286 | * two ../../ to borrow from your direct | |
287 | * neighbour. | |
288 | */ | |
1b0c1e67 DB |
289 | i += 3; |
290 | serverlen = strlen(base); | |
8fcf7f9a | 291 | while (i + 2 < posn && |
1b0c1e67 DB |
292 | !memcmp(data + i, "../", 3)) { |
293 | do { | |
294 | serverlen--; | |
295 | } while (serverlen && | |
296 | base[serverlen - 1] != '/'); | |
297 | i += 3; | |
298 | } | |
a9486b02 | 299 | /* If the server got removed, give up. */ |
8fcf7f9a | 300 | okay = strchr(base, ':') - base + 3 < |
4c42aa1a | 301 | serverlen; |
acc075a8 | 302 | } else if (alt_req->http_specific) { |
1b0c1e67 DB |
303 | char *colon = strchr(data + i, ':'); |
304 | char *slash = strchr(data + i, '/'); | |
305 | if (colon && slash && colon < data + posn && | |
306 | slash < data + posn && colon < slash) { | |
307 | okay = 1; | |
308 | } | |
309 | } | |
1b0c1e67 | 310 | if (okay) { |
59b8263a RS |
311 | struct strbuf target = STRBUF_INIT; |
312 | strbuf_add(&target, base, serverlen); | |
d61434ae JK |
313 | strbuf_add(&target, data + i, posn - i); |
314 | if (!strbuf_strip_suffix(&target, "objects")) { | |
315 | warning("ignoring alternate that does" | |
316 | " not end in 'objects': %s", | |
317 | target.buf); | |
318 | strbuf_release(&target); | |
319 | } else if (is_alternate_allowed(target.buf)) { | |
abcbdc03 JK |
320 | warning("adding alternate object store: %s", |
321 | target.buf); | |
322 | newalt = xmalloc(sizeof(*newalt)); | |
323 | newalt->next = NULL; | |
324 | newalt->base = strbuf_detach(&target, NULL); | |
325 | newalt->got_indices = 0; | |
326 | newalt->packs = NULL; | |
327 | ||
328 | while (tail->next != NULL) | |
329 | tail = tail->next; | |
330 | tail->next = newalt; | |
5cae73d5 EW |
331 | } else { |
332 | strbuf_release(&target); | |
abcbdc03 | 333 | } |
b3661567 DB |
334 | } |
335 | } | |
336 | i = posn + 1; | |
337 | } | |
bc8f2652 | 338 | |
30ae764b | 339 | cdata->got_alternates = 1; |
acc075a8 NH |
340 | } |
341 | ||
30ae764b | 342 | static void fetch_alternates(struct walker *walker, const char *base) |
acc075a8 | 343 | { |
028c2976 | 344 | struct strbuf buffer = STRBUF_INIT; |
54ba4c5f | 345 | struct strbuf url = STRBUF_INIT; |
acc075a8 | 346 | struct active_request_slot *slot; |
cb754fdf | 347 | struct alternates_request alt_req; |
30ae764b | 348 | struct walker_data *cdata = walker->data; |
acc075a8 | 349 | |
4c42aa1a TRC |
350 | /* |
351 | * If another request has already started fetching alternates, | |
352 | * wait for them to arrive and return to processing this request's | |
353 | * curl message | |
354 | */ | |
30ae764b | 355 | while (cdata->got_alternates == 0) { |
29508e1e | 356 | step_active_slots(); |
acc075a8 NH |
357 | } |
358 | ||
359 | /* Nothing to do if they've already been fetched */ | |
30ae764b | 360 | if (cdata->got_alternates == 1) |
acc075a8 NH |
361 | return; |
362 | ||
363 | /* Start the fetch */ | |
30ae764b | 364 | cdata->got_alternates = 0; |
acc075a8 | 365 | |
30ae764b | 366 | if (walker->get_verbosely) |
acc075a8 | 367 | fprintf(stderr, "Getting alternates list for %s\n", base); |
8fcf7f9a | 368 | |
54ba4c5f | 369 | strbuf_addf(&url, "%s/objects/info/http-alternates", base); |
acc075a8 | 370 | |
4c42aa1a TRC |
371 | /* |
372 | * Use a callback to process the result, since another request | |
373 | * may fail and need to have alternates loaded before continuing | |
374 | */ | |
acc075a8 | 375 | slot = get_active_slot(); |
e388ab74 | 376 | slot->callback_func = process_alternates_response; |
30ae764b | 377 | alt_req.walker = walker; |
acc075a8 NH |
378 | slot->callback_data = &alt_req; |
379 | ||
8dda4cbd | 380 | curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &buffer); |
29508e1e | 381 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); |
54ba4c5f | 382 | curl_easy_setopt(slot->curl, CURLOPT_URL, url.buf); |
acc075a8 NH |
383 | |
384 | alt_req.base = base; | |
54ba4c5f | 385 | alt_req.url = &url; |
acc075a8 NH |
386 | alt_req.buffer = &buffer; |
387 | alt_req.http_specific = 1; | |
388 | alt_req.slot = slot; | |
389 | ||
390 | if (start_active_slot(slot)) | |
391 | run_active_slot(slot); | |
392 | else | |
30ae764b | 393 | cdata->got_alternates = -1; |
acc075a8 | 394 | |
028c2976 | 395 | strbuf_release(&buffer); |
54ba4c5f | 396 | strbuf_release(&url); |
b3661567 DB |
397 | } |
398 | ||
30ae764b | 399 | static int fetch_indices(struct walker *walker, struct alt_base *repo) |
182005b9 | 400 | { |
b8caac2b | 401 | int ret; |
1d389ab6 | 402 | |
b3661567 | 403 | if (repo->got_indices) |
182005b9 DB |
404 | return 0; |
405 | ||
30ae764b | 406 | if (walker->get_verbosely) |
6fd72e39 | 407 | fprintf(stderr, "Getting pack list for %s\n", repo->base); |
8fcf7f9a | 408 | |
b8caac2b TRC |
409 | switch (http_get_info_packs(repo->base, &repo->packs)) { |
410 | case HTTP_OK: | |
411 | case HTTP_MISSING_TARGET: | |
412 | repo->got_indices = 1; | |
413 | ret = 0; | |
414 | break; | |
415 | default: | |
5e3a7691 | 416 | repo->got_indices = 0; |
b8caac2b | 417 | ret = -1; |
b3661567 | 418 | } |
182005b9 | 419 | |
3a462bc9 | 420 | return ret; |
182005b9 DB |
421 | } |
422 | ||
07c19e72 | 423 | static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1) |
182005b9 | 424 | { |
182005b9 | 425 | struct packed_git *target; |
49a0f240 | 426 | int ret; |
cb754fdf | 427 | struct slot_results results; |
2264dfa5 | 428 | struct http_pack_request *preq; |
182005b9 | 429 | |
30ae764b | 430 | if (fetch_indices(walker, repo)) |
182005b9 | 431 | return -1; |
b3661567 | 432 | target = find_sha1_pack(sha1, repo->packs); |
182005b9 | 433 | if (!target) |
b3661567 | 434 | return -1; |
eb053492 | 435 | close_pack_index(target); |
182005b9 | 436 | |
30ae764b | 437 | if (walker->get_verbosely) { |
182005b9 | 438 | fprintf(stderr, "Getting pack %s\n", |
2bf1db78 | 439 | hash_to_hex(target->hash)); |
182005b9 | 440 | fprintf(stderr, " which contains %s\n", |
2bf1db78 | 441 | hash_to_hex(sha1)); |
182005b9 DB |
442 | } |
443 | ||
eb053492 | 444 | preq = new_http_pack_request(target->hash, repo->base); |
afe8a907 | 445 | if (!preq) |
2264dfa5 | 446 | goto abort; |
2264dfa5 | 447 | preq->slot->results = &results; |
182005b9 | 448 | |
2264dfa5 TRC |
449 | if (start_active_slot(preq->slot)) { |
450 | run_active_slot(preq->slot); | |
c8568e13 | 451 | if (results.curl_result != CURLE_OK) { |
2264dfa5 TRC |
452 | error("Unable to get pack file %s\n%s", preq->url, |
453 | curl_errorstr); | |
454 | goto abort; | |
1d389ab6 NH |
455 | } |
456 | } else { | |
2264dfa5 TRC |
457 | error("Unable to start request"); |
458 | goto abort; | |
182005b9 DB |
459 | } |
460 | ||
2264dfa5 TRC |
461 | ret = finish_http_pack_request(preq); |
462 | release_http_pack_request(preq); | |
49a0f240 | 463 | if (ret) |
b721e01f | 464 | return ret; |
eb053492 | 465 | http_install_packfile(target, &repo->packs); |
49a0f240 | 466 | |
182005b9 | 467 | return 0; |
2264dfa5 TRC |
468 | |
469 | abort: | |
470 | return -1; | |
182005b9 DB |
471 | } |
472 | ||
53f31389 MW |
473 | static void abort_object_request(struct object_request *obj_req) |
474 | { | |
53f31389 MW |
475 | release_object_request(obj_req); |
476 | } | |
477 | ||
2bf1db78 | 478 | static int fetch_object(struct walker *walker, unsigned char *hash) |
6eb7ed54 | 479 | { |
2bf1db78 | 480 | char *hex = hash_to_hex(hash); |
29508e1e | 481 | int ret = 0; |
94e99012 | 482 | struct object_request *obj_req = NULL; |
5424bc55 | 483 | struct http_object_request *req; |
94e99012 | 484 | struct list_head *pos, *head = &object_queue_head; |
1d389ab6 | 485 | |
94e99012 EW |
486 | list_for_each(pos, head) { |
487 | obj_req = list_entry(pos, struct object_request, node); | |
2bf1db78 | 488 | if (hasheq(obj_req->oid.hash, hash)) |
94e99012 EW |
489 | break; |
490 | } | |
afe8a907 | 491 | if (!obj_req) |
1d389ab6 NH |
492 | return error("Couldn't find request for %s in the queue", hex); |
493 | ||
bc726bd0 | 494 | if (repo_has_object_file(the_repository, &obj_req->oid)) { |
afe8a907 | 495 | if (obj_req->req) |
5424bc55 | 496 | abort_http_object_request(obj_req->req); |
53f31389 | 497 | abort_object_request(obj_req); |
11f0dafe NH |
498 | return 0; |
499 | } | |
500 | ||
4c42aa1a | 501 | while (obj_req->state == WAITING) |
29508e1e | 502 | step_active_slots(); |
6eb7ed54 | 503 | |
5424bc55 TRC |
504 | /* |
505 | * obj_req->req might change when fetching alternates in the callback | |
506 | * process_object_response; therefore, the "shortcut" variable, req, | |
507 | * is used only after we're done with slots. | |
508 | */ | |
4c42aa1a | 509 | while (obj_req->state == ACTIVE) |
5424bc55 TRC |
510 | run_active_slot(obj_req->req->slot); |
511 | ||
512 | req = obj_req->req; | |
4c42aa1a | 513 | |
5424bc55 TRC |
514 | if (req->localfile != -1) { |
515 | close(req->localfile); | |
516 | req->localfile = -1; | |
313c4714 | 517 | } |
6eb7ed54 | 518 | |
3d10f72e JK |
519 | normalize_curl_result(&req->curl_result, req->http_code, |
520 | req->errorstr, sizeof(req->errorstr)); | |
17966c0a | 521 | |
e388ab74 | 522 | if (obj_req->state == ABORTED) { |
29508e1e | 523 | ret = error("Request for %s aborted", hex); |
5424bc55 TRC |
524 | } else if (req->curl_result != CURLE_OK && |
525 | req->http_code != 416) { | |
526 | if (missing_target(req)) | |
e2029eb9 PB |
527 | ret = -1; /* Be silent, it is probably in a pack. */ |
528 | else | |
529 | ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)", | |
5424bc55 TRC |
530 | req->errorstr, req->curl_result, |
531 | req->http_code, hex); | |
532 | } else if (req->zret != Z_STREAM_END) { | |
30ae764b | 533 | walker->corrupt_object_found++; |
5424bc55 | 534 | ret = error("File %s (%s) corrupt", hex, req->url); |
f0be0db1 | 535 | } else if (!oideq(&obj_req->oid, &req->real_oid)) { |
bd2afde8 | 536 | ret = error("File %s has bad hash", hex); |
5424bc55 | 537 | } else if (req->rename < 0) { |
ea657730 | 538 | struct strbuf buf = STRBUF_INIT; |
514c5fdd | 539 | loose_object_path(the_repository, &buf, &req->oid); |
ea657730 CC |
540 | ret = error("unable to write sha1 filename %s", buf.buf); |
541 | strbuf_release(&buf); | |
6eb7ed54 | 542 | } |
49a0f240 | 543 | |
5424bc55 | 544 | release_http_object_request(req); |
e388ab74 | 545 | release_object_request(obj_req); |
29508e1e | 546 | return ret; |
6eb7ed54 DB |
547 | } |
548 | ||
2bf1db78 | 549 | static int fetch(struct walker *walker, unsigned char *hash) |
b3661567 | 550 | { |
30ae764b DB |
551 | struct walker_data *data = walker->data; |
552 | struct alt_base *altbase = data->alt; | |
1d389ab6 | 553 | |
2bf1db78 | 554 | if (!fetch_object(walker, hash)) |
1d389ab6 | 555 | return 0; |
b3661567 | 556 | while (altbase) { |
2bf1db78 | 557 | if (!http_fetch_pack(walker, altbase, hash)) |
b3661567 | 558 | return 0; |
30ae764b | 559 | fetch_alternates(walker, data->alt->base); |
b3661567 DB |
560 | altbase = altbase->next; |
561 | } | |
2bf1db78 | 562 | return error("Unable to find %s under %s", hash_to_hex(hash), |
30ae764b | 563 | data->alt->base); |
b3661567 DB |
564 | } |
565 | ||
c13b2633 | 566 | static int fetch_ref(struct walker *walker, struct ref *ref) |
cd541a68 | 567 | { |
30ae764b | 568 | struct walker_data *data = walker->data; |
c13b2633 | 569 | return http_fetch_ref(data->alt->base, ref); |
cd541a68 DB |
570 | } |
571 | ||
30ae764b DB |
572 | static void cleanup(struct walker *walker) |
573 | { | |
09ae9aca TRC |
574 | struct walker_data *data = walker->data; |
575 | struct alt_base *alt, *alt_next; | |
576 | ||
577 | if (data) { | |
578 | alt = data->alt; | |
579 | while (alt) { | |
580 | alt_next = alt->next; | |
581 | ||
582 | free(alt->base); | |
583 | free(alt); | |
584 | ||
585 | alt = alt_next; | |
586 | } | |
587 | free(data); | |
588 | walker->data = NULL; | |
589 | } | |
30ae764b DB |
590 | } |
591 | ||
888692b7 | 592 | struct walker *get_http_walker(const char *url) |
6eb7ed54 | 593 | { |
9c880b3e | 594 | char *s; |
30ae764b DB |
595 | struct walker_data *data = xmalloc(sizeof(struct walker_data)); |
596 | struct walker *walker = xmalloc(sizeof(struct walker)); | |
6eb7ed54 | 597 | |
30ae764b | 598 | data->alt = xmalloc(sizeof(*data->alt)); |
95244ae3 | 599 | data->alt->base = xstrdup(url); |
30ae764b | 600 | for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s) |
9c880b3e | 601 | *s = 0; |
6eb7ed54 | 602 | |
30ae764b DB |
603 | data->alt->got_indices = 0; |
604 | data->alt->packs = NULL; | |
605 | data->alt->next = NULL; | |
606 | data->got_alternates = -1; | |
fc57b6aa | 607 | |
30ae764b DB |
608 | walker->corrupt_object_found = 0; |
609 | walker->fetch = fetch; | |
610 | walker->fetch_ref = fetch_ref; | |
611 | walker->prefetch = prefetch; | |
612 | walker->cleanup = cleanup; | |
613 | walker->data = data; | |
6eb7ed54 | 614 | |
1e5e0974 | 615 | add_fill_function(NULL, fill_active_slot); |
8e29f6a0 | 616 | |
30ae764b | 617 | return walker; |
6eb7ed54 | 618 | } |