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