7 #define PREV_BUF_SIZE 4096
8 #define RANGE_HEADER_SIZE 30
14 struct packed_git
*packs
;
15 struct alt_base
*next
;
18 enum object_request_state
{
27 struct walker
*walker
;
28 unsigned char sha1
[20];
29 struct alt_base
*repo
;
31 char filename
[PATH_MAX
];
32 char tmpfile
[PATH_MAX
];
34 enum object_request_state state
;
36 char errorstr
[CURL_ERROR_SIZE
];
38 unsigned char real_sha1
[20];
43 struct active_request_slot
*slot
;
44 struct object_request
*next
;
47 struct alternates_request
{
48 struct walker
*walker
;
51 struct strbuf
*buffer
;
52 struct active_request_slot
*slot
;
60 struct curl_slist
*no_pragma_header
;
63 static struct object_request
*object_queue_head
;
65 static size_t fwrite_sha1_file(void *ptr
, size_t eltsize
, size_t nmemb
,
68 unsigned char expn
[4096];
69 size_t size
= eltsize
* nmemb
;
71 struct object_request
*obj_req
= (struct object_request
*)data
;
73 ssize_t retval
= xwrite(obj_req
->local
,
74 (char *) ptr
+ posn
, size
- posn
);
78 } while (posn
< size
);
80 obj_req
->stream
.avail_in
= size
;
81 obj_req
->stream
.next_in
= ptr
;
83 obj_req
->stream
.next_out
= expn
;
84 obj_req
->stream
.avail_out
= sizeof(expn
);
85 obj_req
->zret
= inflate(&obj_req
->stream
, Z_SYNC_FLUSH
);
86 SHA1_Update(&obj_req
->c
, expn
,
87 sizeof(expn
) - obj_req
->stream
.avail_out
);
88 } while (obj_req
->stream
.avail_in
&& obj_req
->zret
== Z_OK
);
93 static void fetch_alternates(struct walker
*walker
, const char *base
);
95 static void process_object_response(void *callback_data
);
97 static void start_object_request(struct walker
*walker
,
98 struct object_request
*obj_req
)
100 char *hex
= sha1_to_hex(obj_req
->sha1
);
101 char prevfile
[PATH_MAX
];
105 unsigned char prev_buf
[PREV_BUF_SIZE
];
106 ssize_t prev_read
= 0;
108 char range
[RANGE_HEADER_SIZE
];
109 struct curl_slist
*range_header
= NULL
;
110 struct active_request_slot
*slot
;
111 struct walker_data
*data
= walker
->data
;
113 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", obj_req
->filename
);
115 rename(obj_req
->tmpfile
, prevfile
);
116 unlink(obj_req
->tmpfile
);
118 if (obj_req
->local
!= -1)
119 error("fd leakage in start: %d", obj_req
->local
);
120 obj_req
->local
= open(obj_req
->tmpfile
,
121 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
122 /* This could have failed due to the "lazy directory creation";
123 * try to mkdir the last path component.
125 if (obj_req
->local
< 0 && errno
== ENOENT
) {
126 char *dir
= strrchr(obj_req
->tmpfile
, '/');
129 mkdir(obj_req
->tmpfile
, 0777);
132 obj_req
->local
= open(obj_req
->tmpfile
,
133 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
136 if (obj_req
->local
< 0) {
137 obj_req
->state
= ABORTED
;
138 error("Couldn't create temporary file %s for %s: %s",
139 obj_req
->tmpfile
, obj_req
->filename
, strerror(errno
));
143 memset(&obj_req
->stream
, 0, sizeof(obj_req
->stream
));
145 inflateInit(&obj_req
->stream
);
147 SHA1_Init(&obj_req
->c
);
149 url
= xmalloc(strlen(obj_req
->repo
->base
) + 51);
150 obj_req
->url
= xmalloc(strlen(obj_req
->repo
->base
) + 51);
151 strcpy(url
, obj_req
->repo
->base
);
152 posn
= url
+ strlen(obj_req
->repo
->base
);
153 strcpy(posn
, "/objects/");
155 memcpy(posn
, hex
, 2);
158 strcpy(posn
, hex
+ 2);
159 strcpy(obj_req
->url
, url
);
161 /* If a previous temp file is present, process what was already
163 prevlocal
= open(prevfile
, O_RDONLY
);
164 if (prevlocal
!= -1) {
166 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
168 if (fwrite_sha1_file(prev_buf
,
171 obj_req
) == prev_read
) {
172 prev_posn
+= prev_read
;
177 } while (prev_read
> 0);
182 /* Reset inflate/SHA1 if there was an error reading the previous temp
183 file; also rewind to the beginning of the local file. */
184 if (prev_read
== -1) {
185 memset(&obj_req
->stream
, 0, sizeof(obj_req
->stream
));
186 inflateInit(&obj_req
->stream
);
187 SHA1_Init(&obj_req
->c
);
190 lseek(obj_req
->local
, 0, SEEK_SET
);
191 ftruncate(obj_req
->local
, 0);
195 slot
= get_active_slot();
196 slot
->callback_func
= process_object_response
;
197 slot
->callback_data
= obj_req
;
198 obj_req
->slot
= slot
;
200 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, obj_req
);
201 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
202 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, obj_req
->errorstr
);
203 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
204 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, data
->no_pragma_header
);
206 /* If we have successfully processed data from a previous fetch
207 attempt, only fetch the data we don't already have. */
209 if (walker
->get_verbosely
)
211 "Resuming fetch of object %s at byte %ld\n",
213 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
214 range_header
= curl_slist_append(range_header
, range
);
215 curl_easy_setopt(slot
->curl
,
216 CURLOPT_HTTPHEADER
, range_header
);
219 /* Try to get the request started, abort the request on error */
220 obj_req
->state
= ACTIVE
;
221 if (!start_active_slot(slot
)) {
222 obj_req
->state
= ABORTED
;
223 obj_req
->slot
= NULL
;
224 close(obj_req
->local
); obj_req
->local
= -1;
230 static void finish_object_request(struct object_request
*obj_req
)
234 fchmod(obj_req
->local
, 0444);
235 close(obj_req
->local
); obj_req
->local
= -1;
237 if (obj_req
->http_code
== 416) {
238 fprintf(stderr
, "Warning: requested range invalid; we may already have all the data.\n");
239 } else if (obj_req
->curl_result
!= CURLE_OK
) {
240 if (stat(obj_req
->tmpfile
, &st
) == 0)
242 unlink(obj_req
->tmpfile
);
246 inflateEnd(&obj_req
->stream
);
247 SHA1_Final(obj_req
->real_sha1
, &obj_req
->c
);
248 if (obj_req
->zret
!= Z_STREAM_END
) {
249 unlink(obj_req
->tmpfile
);
252 if (hashcmp(obj_req
->sha1
, obj_req
->real_sha1
)) {
253 unlink(obj_req
->tmpfile
);
257 move_temp_to_file(obj_req
->tmpfile
, obj_req
->filename
);
259 if (obj_req
->rename
== 0)
260 walker_say(obj_req
->walker
, "got %s\n", sha1_to_hex(obj_req
->sha1
));
263 static void process_object_response(void *callback_data
)
265 struct object_request
*obj_req
=
266 (struct object_request
*)callback_data
;
267 struct walker
*walker
= obj_req
->walker
;
268 struct walker_data
*data
= walker
->data
;
269 struct alt_base
*alt
= data
->alt
;
271 obj_req
->curl_result
= obj_req
->slot
->curl_result
;
272 obj_req
->http_code
= obj_req
->slot
->http_code
;
273 obj_req
->slot
= NULL
;
274 obj_req
->state
= COMPLETE
;
276 /* Use alternates if necessary */
277 if (missing_target(obj_req
)) {
278 fetch_alternates(walker
, alt
->base
);
279 if (obj_req
->repo
->next
!= NULL
) {
282 close(obj_req
->local
);
284 start_object_request(walker
, obj_req
);
289 finish_object_request(obj_req
);
292 static void release_object_request(struct object_request
*obj_req
)
294 struct object_request
*entry
= object_queue_head
;
296 if (obj_req
->local
!= -1)
297 error("fd leakage in release: %d", obj_req
->local
);
298 if (obj_req
== object_queue_head
) {
299 object_queue_head
= obj_req
->next
;
301 while (entry
->next
!= NULL
&& entry
->next
!= obj_req
)
303 if (entry
->next
== obj_req
)
304 entry
->next
= entry
->next
->next
;
311 #ifdef USE_CURL_MULTI
312 static int fill_active_slot(struct walker
*walker
)
314 struct object_request
*obj_req
;
316 for (obj_req
= object_queue_head
; obj_req
; obj_req
= obj_req
->next
) {
317 if (obj_req
->state
== WAITING
) {
318 if (has_sha1_file(obj_req
->sha1
))
319 obj_req
->state
= COMPLETE
;
321 start_object_request(walker
, obj_req
);
330 static void prefetch(struct walker
*walker
, unsigned char *sha1
)
332 struct object_request
*newreq
;
333 struct object_request
*tail
;
334 struct walker_data
*data
= walker
->data
;
335 char *filename
= sha1_file_name(sha1
);
337 newreq
= xmalloc(sizeof(*newreq
));
338 newreq
->walker
= walker
;
339 hashcpy(newreq
->sha1
, sha1
);
340 newreq
->repo
= data
->alt
;
343 newreq
->state
= WAITING
;
344 snprintf(newreq
->filename
, sizeof(newreq
->filename
), "%s", filename
);
345 snprintf(newreq
->tmpfile
, sizeof(newreq
->tmpfile
),
346 "%s.temp", filename
);
350 if (object_queue_head
== NULL
) {
351 object_queue_head
= newreq
;
353 tail
= object_queue_head
;
354 while (tail
->next
!= NULL
) {
360 #ifdef USE_CURL_MULTI
366 static int fetch_index(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
368 char *hex
= sha1_to_hex(sha1
);
371 char tmpfile
[PATH_MAX
];
373 char range
[RANGE_HEADER_SIZE
];
374 struct curl_slist
*range_header
= NULL
;
375 struct walker_data
*data
= walker
->data
;
378 struct active_request_slot
*slot
;
379 struct slot_results results
;
381 if (has_pack_index(sha1
))
384 if (walker
->get_verbosely
)
385 fprintf(stderr
, "Getting index for pack %s\n", hex
);
387 url
= xmalloc(strlen(repo
->base
) + 64);
388 sprintf(url
, "%s/objects/pack/pack-%s.idx", repo
->base
, hex
);
390 filename
= sha1_pack_index_name(sha1
);
391 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
392 indexfile
= fopen(tmpfile
, "a");
394 return error("Unable to open local file %s for pack index",
397 slot
= get_active_slot();
398 slot
->results
= &results
;
399 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
400 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
401 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
402 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, data
->no_pragma_header
);
403 slot
->local
= indexfile
;
405 /* If there is data present from a previous transfer attempt,
406 resume where it left off */
407 prev_posn
= ftell(indexfile
);
409 if (walker
->get_verbosely
)
411 "Resuming fetch of index for pack %s at byte %ld\n",
413 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
414 range_header
= curl_slist_append(range_header
, range
);
415 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
418 if (start_active_slot(slot
)) {
419 run_active_slot(slot
);
420 if (results
.curl_result
!= CURLE_OK
) {
422 return error("Unable to get pack index %s\n%s", url
,
427 return error("Unable to start request");
432 return move_temp_to_file(tmpfile
, filename
);
435 static int setup_index(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
437 struct packed_git
*new_pack
;
438 if (has_pack_file(sha1
))
439 return 0; /* don't list this as something we can get */
441 if (fetch_index(walker
, repo
, sha1
))
444 new_pack
= parse_pack_index(sha1
);
446 return -1; /* parse_pack_index() already issued error message */
447 new_pack
->next
= repo
->packs
;
448 repo
->packs
= new_pack
;
452 static void process_alternates_response(void *callback_data
)
454 struct alternates_request
*alt_req
=
455 (struct alternates_request
*)callback_data
;
456 struct walker
*walker
= alt_req
->walker
;
457 struct walker_data
*cdata
= walker
->data
;
458 struct active_request_slot
*slot
= alt_req
->slot
;
459 struct alt_base
*tail
= cdata
->alt
;
460 const char *base
= alt_req
->base
;
461 static const char null_byte
= '\0';
465 if (alt_req
->http_specific
) {
466 if (slot
->curl_result
!= CURLE_OK
||
467 !alt_req
->buffer
->len
) {
469 /* Try reusing the slot to get non-http alternates */
470 alt_req
->http_specific
= 0;
471 sprintf(alt_req
->url
, "%s/objects/info/alternates",
473 curl_easy_setopt(slot
->curl
, CURLOPT_URL
,
477 if (slot
->finished
!= NULL
)
478 (*slot
->finished
) = 0;
479 if (!start_active_slot(slot
)) {
480 cdata
->got_alternates
= -1;
482 if (slot
->finished
!= NULL
)
483 (*slot
->finished
) = 1;
487 } else if (slot
->curl_result
!= CURLE_OK
) {
488 if (!missing_target(slot
)) {
489 cdata
->got_alternates
= -1;
494 fwrite_buffer(&null_byte
, 1, 1, alt_req
->buffer
);
495 alt_req
->buffer
->len
--;
496 data
= alt_req
->buffer
->buf
;
498 while (i
< alt_req
->buffer
->len
) {
500 while (posn
< alt_req
->buffer
->len
&& data
[posn
] != '\n')
502 if (data
[posn
] == '\n') {
505 struct alt_base
*newalt
;
507 if (data
[i
] == '/') {
509 * http://git.host/pub/scm/linux.git/
511 * so memcpy(dst, base, serverlen) will
512 * copy up to "...git.host".
514 const char *colon_ss
= strstr(base
,"://");
516 serverlen
= (strchr(colon_ss
+ 3, '/')
520 } else if (!memcmp(data
+ i
, "../", 3)) {
521 /* Relative URL; chop the corresponding
522 * number of subpath from base (and ../
523 * from data), and concatenate the result.
525 * The code first drops ../ from data, and
526 * then drops one ../ from data and one path
527 * from base. IOW, one extra ../ is dropped
528 * from data than path is dropped from base.
530 * This is not wrong. The alternate in
531 * http://git.host/pub/scm/linux.git/
533 * http://git.host/pub/scm/linus.git/
534 * is ../../linus.git/objects/. You need
535 * two ../../ to borrow from your direct
539 serverlen
= strlen(base
);
540 while (i
+ 2 < posn
&&
541 !memcmp(data
+ i
, "../", 3)) {
544 } while (serverlen
&&
545 base
[serverlen
- 1] != '/');
548 /* If the server got removed, give up. */
549 okay
= strchr(base
, ':') - base
+ 3 <
551 } else if (alt_req
->http_specific
) {
552 char *colon
= strchr(data
+ i
, ':');
553 char *slash
= strchr(data
+ i
, '/');
554 if (colon
&& slash
&& colon
< data
+ posn
&&
555 slash
< data
+ posn
&& colon
< slash
) {
559 /* skip "objects\n" at end */
561 target
= xmalloc(serverlen
+ posn
- i
- 6);
562 memcpy(target
, base
, serverlen
);
563 memcpy(target
+ serverlen
, data
+ i
,
565 target
[serverlen
+ posn
- i
- 7] = 0;
566 if (walker
->get_verbosely
)
568 "Also look at %s\n", target
);
569 newalt
= xmalloc(sizeof(*newalt
));
571 newalt
->base
= target
;
572 newalt
->got_indices
= 0;
573 newalt
->packs
= NULL
;
575 while (tail
->next
!= NULL
)
583 cdata
->got_alternates
= 1;
586 static void fetch_alternates(struct walker
*walker
, const char *base
)
588 struct strbuf buffer
= STRBUF_INIT
;
590 struct active_request_slot
*slot
;
591 struct alternates_request alt_req
;
592 struct walker_data
*cdata
= walker
->data
;
594 /* If another request has already started fetching alternates,
595 wait for them to arrive and return to processing this request's
597 #ifdef USE_CURL_MULTI
598 while (cdata
->got_alternates
== 0) {
603 /* Nothing to do if they've already been fetched */
604 if (cdata
->got_alternates
== 1)
607 /* Start the fetch */
608 cdata
->got_alternates
= 0;
610 if (walker
->get_verbosely
)
611 fprintf(stderr
, "Getting alternates list for %s\n", base
);
613 url
= xmalloc(strlen(base
) + 31);
614 sprintf(url
, "%s/objects/info/http-alternates", base
);
616 /* Use a callback to process the result, since another request
617 may fail and need to have alternates loaded before continuing */
618 slot
= get_active_slot();
619 slot
->callback_func
= process_alternates_response
;
620 alt_req
.walker
= walker
;
621 slot
->callback_data
= &alt_req
;
623 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
624 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
625 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
629 alt_req
.buffer
= &buffer
;
630 alt_req
.http_specific
= 1;
633 if (start_active_slot(slot
))
634 run_active_slot(slot
);
636 cdata
->got_alternates
= -1;
638 strbuf_release(&buffer
);
642 static int fetch_indices(struct walker
*walker
, struct alt_base
*repo
)
644 unsigned char sha1
[20];
646 struct strbuf buffer
= STRBUF_INIT
;
651 struct active_request_slot
*slot
;
652 struct slot_results results
;
654 if (repo
->got_indices
)
657 if (walker
->get_verbosely
)
658 fprintf(stderr
, "Getting pack list for %s\n", repo
->base
);
660 url
= xmalloc(strlen(repo
->base
) + 21);
661 sprintf(url
, "%s/objects/info/packs", repo
->base
);
663 slot
= get_active_slot();
664 slot
->results
= &results
;
665 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
666 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
667 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
668 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
669 if (start_active_slot(slot
)) {
670 run_active_slot(slot
);
671 if (results
.curl_result
!= CURLE_OK
) {
672 if (missing_target(&results
)) {
673 repo
->got_indices
= 1;
676 repo
->got_indices
= 0;
677 ret
= error("%s", curl_errorstr
);
682 repo
->got_indices
= 0;
683 ret
= error("Unable to start request");
688 while (i
< buffer
.len
) {
692 if (i
+ 52 <= buffer
.len
&&
693 !prefixcmp(data
+ i
, " pack-") &&
694 !prefixcmp(data
+ i
+ 46, ".pack\n")) {
695 get_sha1_hex(data
+ i
+ 6, sha1
);
696 setup_index(walker
, repo
, sha1
);
701 while (i
< buffer
.len
&& data
[i
] != '\n')
707 repo
->got_indices
= 1;
709 strbuf_release(&buffer
);
714 static int fetch_pack(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
717 struct packed_git
*target
;
718 struct packed_git
**lst
;
721 char tmpfile
[PATH_MAX
];
724 char range
[RANGE_HEADER_SIZE
];
725 struct curl_slist
*range_header
= NULL
;
726 struct walker_data
*data
= walker
->data
;
728 struct active_request_slot
*slot
;
729 struct slot_results results
;
731 if (fetch_indices(walker
, repo
))
733 target
= find_sha1_pack(sha1
, repo
->packs
);
737 if (walker
->get_verbosely
) {
738 fprintf(stderr
, "Getting pack %s\n",
739 sha1_to_hex(target
->sha1
));
740 fprintf(stderr
, " which contains %s\n",
744 url
= xmalloc(strlen(repo
->base
) + 65);
745 sprintf(url
, "%s/objects/pack/pack-%s.pack",
746 repo
->base
, sha1_to_hex(target
->sha1
));
748 filename
= sha1_pack_name(target
->sha1
);
749 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
750 packfile
= fopen(tmpfile
, "a");
752 return error("Unable to open local file %s for pack",
755 slot
= get_active_slot();
756 slot
->results
= &results
;
757 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, packfile
);
758 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
759 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
760 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, data
->no_pragma_header
);
761 slot
->local
= packfile
;
763 /* If there is data present from a previous transfer attempt,
764 resume where it left off */
765 prev_posn
= ftell(packfile
);
767 if (walker
->get_verbosely
)
769 "Resuming fetch of pack %s at byte %ld\n",
770 sha1_to_hex(target
->sha1
), prev_posn
);
771 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
772 range_header
= curl_slist_append(range_header
, range
);
773 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
776 if (start_active_slot(slot
)) {
777 run_active_slot(slot
);
778 if (results
.curl_result
!= CURLE_OK
) {
780 return error("Unable to get pack file %s\n%s", url
,
785 return error("Unable to start request");
788 target
->pack_size
= ftell(packfile
);
791 ret
= move_temp_to_file(tmpfile
, filename
);
796 while (*lst
!= target
)
797 lst
= &((*lst
)->next
);
800 if (verify_pack(target
))
802 install_packed_git(target
);
807 static void abort_object_request(struct object_request
*obj_req
)
809 if (obj_req
->local
>= 0) {
810 close(obj_req
->local
);
813 unlink(obj_req
->tmpfile
);
815 release_active_slot(obj_req
->slot
);
816 obj_req
->slot
= NULL
;
818 release_object_request(obj_req
);
821 static int fetch_object(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
823 char *hex
= sha1_to_hex(sha1
);
825 struct object_request
*obj_req
= object_queue_head
;
827 while (obj_req
!= NULL
&& hashcmp(obj_req
->sha1
, sha1
))
828 obj_req
= obj_req
->next
;
830 return error("Couldn't find request for %s in the queue", hex
);
832 if (has_sha1_file(obj_req
->sha1
)) {
833 abort_object_request(obj_req
);
837 #ifdef USE_CURL_MULTI
838 while (obj_req
->state
== WAITING
) {
842 start_object_request(walker
, obj_req
);
845 while (obj_req
->state
== ACTIVE
) {
846 run_active_slot(obj_req
->slot
);
848 if (obj_req
->local
!= -1) {
849 close(obj_req
->local
); obj_req
->local
= -1;
852 if (obj_req
->state
== ABORTED
) {
853 ret
= error("Request for %s aborted", hex
);
854 } else if (obj_req
->curl_result
!= CURLE_OK
&&
855 obj_req
->http_code
!= 416) {
856 if (missing_target(obj_req
))
857 ret
= -1; /* Be silent, it is probably in a pack. */
859 ret
= error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
860 obj_req
->errorstr
, obj_req
->curl_result
,
861 obj_req
->http_code
, hex
);
862 } else if (obj_req
->zret
!= Z_STREAM_END
) {
863 walker
->corrupt_object_found
++;
864 ret
= error("File %s (%s) corrupt", hex
, obj_req
->url
);
865 } else if (hashcmp(obj_req
->sha1
, obj_req
->real_sha1
)) {
866 ret
= error("File %s has bad hash", hex
);
867 } else if (obj_req
->rename
< 0) {
868 ret
= error("unable to write sha1 filename %s",
872 release_object_request(obj_req
);
876 static int fetch(struct walker
*walker
, unsigned char *sha1
)
878 struct walker_data
*data
= walker
->data
;
879 struct alt_base
*altbase
= data
->alt
;
881 if (!fetch_object(walker
, altbase
, sha1
))
884 if (!fetch_pack(walker
, altbase
, sha1
))
886 fetch_alternates(walker
, data
->alt
->base
);
887 altbase
= altbase
->next
;
889 return error("Unable to find %s under %s", sha1_to_hex(sha1
),
893 static int fetch_ref(struct walker
*walker
, struct ref
*ref
)
895 struct walker_data
*data
= walker
->data
;
896 return http_fetch_ref(data
->alt
->base
, ref
);
899 static void cleanup(struct walker
*walker
)
901 struct walker_data
*data
= walker
->data
;
904 curl_slist_free_all(data
->no_pragma_header
);
907 struct walker
*get_http_walker(const char *url
, struct remote
*remote
)
910 struct walker_data
*data
= xmalloc(sizeof(struct walker_data
));
911 struct walker
*walker
= xmalloc(sizeof(struct walker
));
915 data
->no_pragma_header
= curl_slist_append(NULL
, "Pragma:");
917 data
->alt
= xmalloc(sizeof(*data
->alt
));
918 data
->alt
->base
= xmalloc(strlen(url
) + 1);
919 strcpy(data
->alt
->base
, url
);
920 for (s
= data
->alt
->base
+ strlen(data
->alt
->base
) - 1; *s
== '/'; --s
)
923 data
->alt
->got_indices
= 0;
924 data
->alt
->packs
= NULL
;
925 data
->alt
->next
= NULL
;
926 data
->got_alternates
= -1;
928 walker
->corrupt_object_found
= 0;
929 walker
->fetch
= fetch
;
930 walker
->fetch_ref
= fetch_ref
;
931 walker
->prefetch
= prefetch
;
932 walker
->cleanup
= cleanup
;
935 #ifdef USE_CURL_MULTI
936 add_fill_function(walker
, (int (*)(void *)) fill_active_slot
);