9 struct packed_git
*packs
;
10 struct alt_base
*next
;
13 enum object_request_state
{
20 struct object_request
{
21 struct walker
*walker
;
22 unsigned char sha1
[20];
23 struct alt_base
*repo
;
24 enum object_request_state state
;
25 struct http_object_request
*req
;
26 struct object_request
*next
;
29 struct alternates_request
{
30 struct walker
*walker
;
33 struct strbuf
*buffer
;
34 struct active_request_slot
*slot
;
44 static struct object_request
*object_queue_head
;
46 static void fetch_alternates(struct walker
*walker
, const char *base
);
48 static void process_object_response(void *callback_data
);
50 static void start_object_request(struct walker
*walker
,
51 struct object_request
*obj_req
)
53 struct active_request_slot
*slot
;
54 struct http_object_request
*req
;
56 req
= new_http_object_request(obj_req
->repo
->base
, obj_req
->sha1
);
58 obj_req
->state
= ABORTED
;
64 slot
->callback_func
= process_object_response
;
65 slot
->callback_data
= obj_req
;
67 /* Try to get the request started, abort the request on error */
68 obj_req
->state
= ACTIVE
;
69 if (!start_active_slot(slot
)) {
70 obj_req
->state
= ABORTED
;
71 release_http_object_request(req
);
76 static void finish_object_request(struct object_request
*obj_req
)
78 if (finish_http_object_request(obj_req
->req
))
81 if (obj_req
->req
->rename
== 0)
82 walker_say(obj_req
->walker
, "got %s\n", sha1_to_hex(obj_req
->sha1
));
85 static void process_object_response(void *callback_data
)
87 struct object_request
*obj_req
=
88 (struct object_request
*)callback_data
;
89 struct walker
*walker
= obj_req
->walker
;
90 struct walker_data
*data
= walker
->data
;
91 struct alt_base
*alt
= data
->alt
;
93 process_http_object_request(obj_req
->req
);
94 obj_req
->state
= COMPLETE
;
96 /* Use alternates if necessary */
97 if (missing_target(obj_req
->req
)) {
98 fetch_alternates(walker
, alt
->base
);
99 if (obj_req
->repo
->next
!= NULL
) {
102 release_http_object_request(obj_req
->req
);
103 start_object_request(walker
, obj_req
);
108 finish_object_request(obj_req
);
111 static void release_object_request(struct object_request
*obj_req
)
113 struct object_request
*entry
= object_queue_head
;
115 if (obj_req
->req
!=NULL
&& obj_req
->req
->localfile
!= -1)
116 error("fd leakage in release: %d", obj_req
->req
->localfile
);
117 if (obj_req
== object_queue_head
) {
118 object_queue_head
= obj_req
->next
;
120 while (entry
->next
!= NULL
&& entry
->next
!= obj_req
)
122 if (entry
->next
== obj_req
)
123 entry
->next
= entry
->next
->next
;
129 #ifdef USE_CURL_MULTI
130 static int fill_active_slot(struct walker
*walker
)
132 struct object_request
*obj_req
;
134 for (obj_req
= object_queue_head
; obj_req
; obj_req
= obj_req
->next
) {
135 if (obj_req
->state
== WAITING
) {
136 if (has_sha1_file(obj_req
->sha1
))
137 obj_req
->state
= COMPLETE
;
139 start_object_request(walker
, obj_req
);
148 static void prefetch(struct walker
*walker
, unsigned char *sha1
)
150 struct object_request
*newreq
;
151 struct object_request
*tail
;
152 struct walker_data
*data
= walker
->data
;
154 newreq
= xmalloc(sizeof(*newreq
));
155 newreq
->walker
= walker
;
156 hashcpy(newreq
->sha1
, sha1
);
157 newreq
->repo
= data
->alt
;
158 newreq
->state
= WAITING
;
162 http_is_verbose
= walker
->get_verbosely
;
164 if (object_queue_head
== NULL
) {
165 object_queue_head
= newreq
;
167 tail
= object_queue_head
;
168 while (tail
->next
!= NULL
)
173 #ifdef USE_CURL_MULTI
179 static void process_alternates_response(void *callback_data
)
181 struct alternates_request
*alt_req
=
182 (struct alternates_request
*)callback_data
;
183 struct walker
*walker
= alt_req
->walker
;
184 struct walker_data
*cdata
= walker
->data
;
185 struct active_request_slot
*slot
= alt_req
->slot
;
186 struct alt_base
*tail
= cdata
->alt
;
187 const char *base
= alt_req
->base
;
188 const char null_byte
= '\0';
192 if (alt_req
->http_specific
) {
193 if (slot
->curl_result
!= CURLE_OK
||
194 !alt_req
->buffer
->len
) {
196 /* Try reusing the slot to get non-http alternates */
197 alt_req
->http_specific
= 0;
198 sprintf(alt_req
->url
, "%s/objects/info/alternates",
200 curl_easy_setopt(slot
->curl
, CURLOPT_URL
,
204 if (slot
->finished
!= NULL
)
205 (*slot
->finished
) = 0;
206 if (!start_active_slot(slot
)) {
207 cdata
->got_alternates
= -1;
209 if (slot
->finished
!= NULL
)
210 (*slot
->finished
) = 1;
214 } else if (slot
->curl_result
!= CURLE_OK
) {
215 if (!missing_target(slot
)) {
216 cdata
->got_alternates
= -1;
221 fwrite_buffer((char *)&null_byte
, 1, 1, alt_req
->buffer
);
222 alt_req
->buffer
->len
--;
223 data
= alt_req
->buffer
->buf
;
225 while (i
< alt_req
->buffer
->len
) {
227 while (posn
< alt_req
->buffer
->len
&& data
[posn
] != '\n')
229 if (data
[posn
] == '\n') {
232 struct alt_base
*newalt
;
234 if (data
[i
] == '/') {
237 * http://git.host/pub/scm/linux.git/
239 * so memcpy(dst, base, serverlen) will
240 * copy up to "...git.host".
242 const char *colon_ss
= strstr(base
,"://");
244 serverlen
= (strchr(colon_ss
+ 3, '/')
248 } else if (!memcmp(data
+ i
, "../", 3)) {
250 * Relative URL; chop the corresponding
251 * number of subpath from base (and ../
252 * from data), and concatenate the result.
254 * The code first drops ../ from data, and
255 * then drops one ../ from data and one path
256 * from base. IOW, one extra ../ is dropped
257 * from data than path is dropped from base.
259 * This is not wrong. The alternate in
260 * http://git.host/pub/scm/linux.git/
262 * http://git.host/pub/scm/linus.git/
263 * is ../../linus.git/objects/. You need
264 * two ../../ to borrow from your direct
268 serverlen
= strlen(base
);
269 while (i
+ 2 < posn
&&
270 !memcmp(data
+ i
, "../", 3)) {
273 } while (serverlen
&&
274 base
[serverlen
- 1] != '/');
277 /* If the server got removed, give up. */
278 okay
= strchr(base
, ':') - base
+ 3 <
280 } else if (alt_req
->http_specific
) {
281 char *colon
= strchr(data
+ i
, ':');
282 char *slash
= strchr(data
+ i
, '/');
283 if (colon
&& slash
&& colon
< data
+ posn
&&
284 slash
< data
+ posn
&& colon
< slash
) {
288 /* skip "objects\n" at end */
290 target
= xmalloc(serverlen
+ posn
- i
- 6);
291 memcpy(target
, base
, serverlen
);
292 memcpy(target
+ serverlen
, data
+ i
,
294 target
[serverlen
+ posn
- i
- 7] = 0;
295 if (walker
->get_verbosely
)
297 "Also look at %s\n", target
);
298 newalt
= xmalloc(sizeof(*newalt
));
300 newalt
->base
= target
;
301 newalt
->got_indices
= 0;
302 newalt
->packs
= NULL
;
304 while (tail
->next
!= NULL
)
312 cdata
->got_alternates
= 1;
315 static void fetch_alternates(struct walker
*walker
, const char *base
)
317 struct strbuf buffer
= STRBUF_INIT
;
319 struct active_request_slot
*slot
;
320 struct alternates_request alt_req
;
321 struct walker_data
*cdata
= walker
->data
;
324 * If another request has already started fetching alternates,
325 * wait for them to arrive and return to processing this request's
328 #ifdef USE_CURL_MULTI
329 while (cdata
->got_alternates
== 0) {
334 /* Nothing to do if they've already been fetched */
335 if (cdata
->got_alternates
== 1)
338 /* Start the fetch */
339 cdata
->got_alternates
= 0;
341 if (walker
->get_verbosely
)
342 fprintf(stderr
, "Getting alternates list for %s\n", base
);
344 url
= xmalloc(strlen(base
) + 31);
345 sprintf(url
, "%s/objects/info/http-alternates", base
);
348 * Use a callback to process the result, since another request
349 * may fail and need to have alternates loaded before continuing
351 slot
= get_active_slot(url
);
352 slot
->callback_func
= process_alternates_response
;
353 alt_req
.walker
= walker
;
354 slot
->callback_data
= &alt_req
;
356 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
357 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
358 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
362 alt_req
.buffer
= &buffer
;
363 alt_req
.http_specific
= 1;
366 if (start_active_slot(slot
))
367 run_active_slot(slot
);
369 cdata
->got_alternates
= -1;
371 strbuf_release(&buffer
);
375 static int fetch_indices(struct walker
*walker
, struct alt_base
*repo
)
379 if (repo
->got_indices
)
382 if (walker
->get_verbosely
)
383 fprintf(stderr
, "Getting pack list for %s\n", repo
->base
);
385 switch (http_get_info_packs(repo
->base
, &repo
->packs
)) {
387 case HTTP_MISSING_TARGET
:
388 repo
->got_indices
= 1;
392 repo
->got_indices
= 0;
399 static int fetch_pack(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
401 struct packed_git
*target
;
403 struct slot_results results
;
404 struct http_pack_request
*preq
;
406 if (fetch_indices(walker
, repo
))
408 target
= find_sha1_pack(sha1
, repo
->packs
);
412 if (walker
->get_verbosely
) {
413 fprintf(stderr
, "Getting pack %s\n",
414 sha1_to_hex(target
->sha1
));
415 fprintf(stderr
, " which contains %s\n",
419 preq
= new_http_pack_request(target
, repo
->base
);
422 preq
->lst
= &repo
->packs
;
423 preq
->slot
->results
= &results
;
425 if (start_active_slot(preq
->slot
)) {
426 run_active_slot(preq
->slot
);
427 if (results
.curl_result
!= CURLE_OK
) {
428 error("Unable to get pack file %s\n%s", preq
->url
,
433 error("Unable to start request");
437 ret
= finish_http_pack_request(preq
);
438 release_http_pack_request(preq
);
448 static void abort_object_request(struct object_request
*obj_req
)
450 release_object_request(obj_req
);
453 static int fetch_object(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
455 char *hex
= sha1_to_hex(sha1
);
457 struct object_request
*obj_req
= object_queue_head
;
458 struct http_object_request
*req
;
460 while (obj_req
!= NULL
&& hashcmp(obj_req
->sha1
, sha1
))
461 obj_req
= obj_req
->next
;
463 return error("Couldn't find request for %s in the queue", hex
);
465 if (has_sha1_file(obj_req
->sha1
)) {
466 if (obj_req
->req
!= NULL
)
467 abort_http_object_request(obj_req
->req
);
468 abort_object_request(obj_req
);
472 #ifdef USE_CURL_MULTI
473 while (obj_req
->state
== WAITING
)
476 start_object_request(walker
, obj_req
);
480 * obj_req->req might change when fetching alternates in the callback
481 * process_object_response; therefore, the "shortcut" variable, req,
482 * is used only after we're done with slots.
484 while (obj_req
->state
== ACTIVE
)
485 run_active_slot(obj_req
->req
->slot
);
489 if (req
->localfile
!= -1) {
490 close(req
->localfile
);
494 if (obj_req
->state
== ABORTED
) {
495 ret
= error("Request for %s aborted", hex
);
496 } else if (req
->curl_result
!= CURLE_OK
&&
497 req
->http_code
!= 416) {
498 if (missing_target(req
))
499 ret
= -1; /* Be silent, it is probably in a pack. */
501 ret
= error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
502 req
->errorstr
, req
->curl_result
,
503 req
->http_code
, hex
);
504 } else if (req
->zret
!= Z_STREAM_END
) {
505 walker
->corrupt_object_found
++;
506 ret
= error("File %s (%s) corrupt", hex
, req
->url
);
507 } else if (hashcmp(obj_req
->sha1
, req
->real_sha1
)) {
508 ret
= error("File %s has bad hash", hex
);
509 } else if (req
->rename
< 0) {
510 ret
= error("unable to write sha1 filename %s",
511 sha1_file_name(req
->sha1
));
514 release_http_object_request(req
);
515 release_object_request(obj_req
);
519 static int fetch(struct walker
*walker
, unsigned char *sha1
)
521 struct walker_data
*data
= walker
->data
;
522 struct alt_base
*altbase
= data
->alt
;
524 if (!fetch_object(walker
, altbase
, sha1
))
527 if (!fetch_pack(walker
, altbase
, sha1
))
529 fetch_alternates(walker
, data
->alt
->base
);
530 altbase
= altbase
->next
;
532 return error("Unable to find %s under %s", sha1_to_hex(sha1
),
536 static int fetch_ref(struct walker
*walker
, struct ref
*ref
)
538 struct walker_data
*data
= walker
->data
;
539 return http_fetch_ref(data
->alt
->base
, ref
);
542 static void cleanup(struct walker
*walker
)
544 struct walker_data
*data
= walker
->data
;
545 struct alt_base
*alt
, *alt_next
;
550 alt_next
= alt
->next
;
562 struct walker
*get_http_walker(const char *url
)
565 struct walker_data
*data
= xmalloc(sizeof(struct walker_data
));
566 struct walker
*walker
= xmalloc(sizeof(struct walker
));
568 data
->alt
= xmalloc(sizeof(*data
->alt
));
569 data
->alt
->base
= xmalloc(strlen(url
) + 1);
570 strcpy(data
->alt
->base
, url
);
571 for (s
= data
->alt
->base
+ strlen(data
->alt
->base
) - 1; *s
== '/'; --s
)
574 data
->alt
->got_indices
= 0;
575 data
->alt
->packs
= NULL
;
576 data
->alt
->next
= NULL
;
577 data
->got_alternates
= -1;
579 walker
->corrupt_object_found
= 0;
580 walker
->fetch
= fetch
;
581 walker
->fetch_ref
= fetch_ref
;
582 walker
->prefetch
= prefetch
;
583 walker
->cleanup
= cleanup
;
586 #ifdef USE_CURL_MULTI
587 add_fill_function(walker
, (int (*)(void *)) fill_active_slot
);