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