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
= git_inflate(&obj_req
->stream
, Z_SYNC_FLUSH
);
86 git_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
);
114 unlink_or_warn(prevfile
);
115 rename(obj_req
->tmpfile
, prevfile
);
116 unlink_or_warn(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 git_inflate_init(&obj_req
->stream
);
147 git_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);
180 unlink_or_warn(prevfile
);
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 git_inflate_init(&obj_req
->stream
);
187 git_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 close(obj_req
->local
); obj_req
->local
= -1;
236 if (obj_req
->http_code
== 416) {
237 fprintf(stderr
, "Warning: requested range invalid; we may already have all the data.\n");
238 } else if (obj_req
->curl_result
!= CURLE_OK
) {
239 if (stat(obj_req
->tmpfile
, &st
) == 0)
241 unlink_or_warn(obj_req
->tmpfile
);
245 git_inflate_end(&obj_req
->stream
);
246 git_SHA1_Final(obj_req
->real_sha1
, &obj_req
->c
);
247 if (obj_req
->zret
!= Z_STREAM_END
) {
248 unlink_or_warn(obj_req
->tmpfile
);
251 if (hashcmp(obj_req
->sha1
, obj_req
->real_sha1
)) {
252 unlink_or_warn(obj_req
->tmpfile
);
256 move_temp_to_file(obj_req
->tmpfile
, obj_req
->filename
);
258 if (obj_req
->rename
== 0)
259 walker_say(obj_req
->walker
, "got %s\n", sha1_to_hex(obj_req
->sha1
));
262 static void process_object_response(void *callback_data
)
264 struct object_request
*obj_req
=
265 (struct object_request
*)callback_data
;
266 struct walker
*walker
= obj_req
->walker
;
267 struct walker_data
*data
= walker
->data
;
268 struct alt_base
*alt
= data
->alt
;
270 obj_req
->curl_result
= obj_req
->slot
->curl_result
;
271 obj_req
->http_code
= obj_req
->slot
->http_code
;
272 obj_req
->slot
= NULL
;
273 obj_req
->state
= COMPLETE
;
275 /* Use alternates if necessary */
276 if (missing_target(obj_req
)) {
277 fetch_alternates(walker
, alt
->base
);
278 if (obj_req
->repo
->next
!= NULL
) {
281 close(obj_req
->local
);
283 start_object_request(walker
, obj_req
);
288 finish_object_request(obj_req
);
291 static void release_object_request(struct object_request
*obj_req
)
293 struct object_request
*entry
= object_queue_head
;
295 if (obj_req
->local
!= -1)
296 error("fd leakage in release: %d", obj_req
->local
);
297 if (obj_req
== object_queue_head
) {
298 object_queue_head
= obj_req
->next
;
300 while (entry
->next
!= NULL
&& entry
->next
!= obj_req
)
302 if (entry
->next
== obj_req
)
303 entry
->next
= entry
->next
->next
;
310 #ifdef USE_CURL_MULTI
311 static int fill_active_slot(struct walker
*walker
)
313 struct object_request
*obj_req
;
315 for (obj_req
= object_queue_head
; obj_req
; obj_req
= obj_req
->next
) {
316 if (obj_req
->state
== WAITING
) {
317 if (has_sha1_file(obj_req
->sha1
))
318 obj_req
->state
= COMPLETE
;
320 start_object_request(walker
, obj_req
);
329 static void prefetch(struct walker
*walker
, unsigned char *sha1
)
331 struct object_request
*newreq
;
332 struct object_request
*tail
;
333 struct walker_data
*data
= walker
->data
;
334 char *filename
= sha1_file_name(sha1
);
336 newreq
= xmalloc(sizeof(*newreq
));
337 newreq
->walker
= walker
;
338 hashcpy(newreq
->sha1
, sha1
);
339 newreq
->repo
= data
->alt
;
342 newreq
->state
= WAITING
;
343 snprintf(newreq
->filename
, sizeof(newreq
->filename
), "%s", filename
);
344 snprintf(newreq
->tmpfile
, sizeof(newreq
->tmpfile
),
345 "%s.temp", filename
);
349 if (object_queue_head
== NULL
) {
350 object_queue_head
= newreq
;
352 tail
= object_queue_head
;
353 while (tail
->next
!= NULL
) {
359 #ifdef USE_CURL_MULTI
365 static int fetch_index(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
367 char *hex
= sha1_to_hex(sha1
);
370 char tmpfile
[PATH_MAX
];
372 char range
[RANGE_HEADER_SIZE
];
373 struct curl_slist
*range_header
= NULL
;
374 struct walker_data
*data
= walker
->data
;
377 struct active_request_slot
*slot
;
378 struct slot_results results
;
380 if (has_pack_index(sha1
))
383 if (walker
->get_verbosely
)
384 fprintf(stderr
, "Getting index for pack %s\n", hex
);
386 url
= xmalloc(strlen(repo
->base
) + 64);
387 sprintf(url
, "%s/objects/pack/pack-%s.idx", repo
->base
, hex
);
389 filename
= sha1_pack_index_name(sha1
);
390 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
391 indexfile
= fopen(tmpfile
, "a");
393 return error("Unable to open local file %s for pack index",
396 slot
= get_active_slot();
397 slot
->results
= &results
;
398 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
399 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
400 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
401 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, data
->no_pragma_header
);
402 slot
->local
= indexfile
;
404 /* If there is data present from a previous transfer attempt,
405 resume where it left off */
406 prev_posn
= ftell(indexfile
);
408 if (walker
->get_verbosely
)
410 "Resuming fetch of index for pack %s at byte %ld\n",
412 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
413 range_header
= curl_slist_append(range_header
, range
);
414 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
417 if (start_active_slot(slot
)) {
418 run_active_slot(slot
);
419 if (results
.curl_result
!= CURLE_OK
) {
422 return error("Unable to get pack index %s\n%s", url
,
428 return error("Unable to start request");
434 return move_temp_to_file(tmpfile
, filename
);
437 static int setup_index(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
439 struct packed_git
*new_pack
;
440 if (has_pack_file(sha1
))
441 return 0; /* don't list this as something we can get */
443 if (fetch_index(walker
, repo
, sha1
))
446 new_pack
= parse_pack_index(sha1
);
448 return -1; /* parse_pack_index() already issued error message */
449 new_pack
->next
= repo
->packs
;
450 repo
->packs
= new_pack
;
454 static void process_alternates_response(void *callback_data
)
456 struct alternates_request
*alt_req
=
457 (struct alternates_request
*)callback_data
;
458 struct walker
*walker
= alt_req
->walker
;
459 struct walker_data
*cdata
= walker
->data
;
460 struct active_request_slot
*slot
= alt_req
->slot
;
461 struct alt_base
*tail
= cdata
->alt
;
462 const char *base
= alt_req
->base
;
463 static const char null_byte
= '\0';
467 if (alt_req
->http_specific
) {
468 if (slot
->curl_result
!= CURLE_OK
||
469 !alt_req
->buffer
->len
) {
471 /* Try reusing the slot to get non-http alternates */
472 alt_req
->http_specific
= 0;
473 sprintf(alt_req
->url
, "%s/objects/info/alternates",
475 curl_easy_setopt(slot
->curl
, CURLOPT_URL
,
479 if (slot
->finished
!= NULL
)
480 (*slot
->finished
) = 0;
481 if (!start_active_slot(slot
)) {
482 cdata
->got_alternates
= -1;
484 if (slot
->finished
!= NULL
)
485 (*slot
->finished
) = 1;
489 } else if (slot
->curl_result
!= CURLE_OK
) {
490 if (!missing_target(slot
)) {
491 cdata
->got_alternates
= -1;
496 fwrite_buffer(&null_byte
, 1, 1, alt_req
->buffer
);
497 alt_req
->buffer
->len
--;
498 data
= alt_req
->buffer
->buf
;
500 while (i
< alt_req
->buffer
->len
) {
502 while (posn
< alt_req
->buffer
->len
&& data
[posn
] != '\n')
504 if (data
[posn
] == '\n') {
507 struct alt_base
*newalt
;
509 if (data
[i
] == '/') {
511 * http://git.host/pub/scm/linux.git/
513 * so memcpy(dst, base, serverlen) will
514 * copy up to "...git.host".
516 const char *colon_ss
= strstr(base
,"://");
518 serverlen
= (strchr(colon_ss
+ 3, '/')
522 } else if (!memcmp(data
+ i
, "../", 3)) {
523 /* Relative URL; chop the corresponding
524 * number of subpath from base (and ../
525 * from data), and concatenate the result.
527 * The code first drops ../ from data, and
528 * then drops one ../ from data and one path
529 * from base. IOW, one extra ../ is dropped
530 * from data than path is dropped from base.
532 * This is not wrong. The alternate in
533 * http://git.host/pub/scm/linux.git/
535 * http://git.host/pub/scm/linus.git/
536 * is ../../linus.git/objects/. You need
537 * two ../../ to borrow from your direct
541 serverlen
= strlen(base
);
542 while (i
+ 2 < posn
&&
543 !memcmp(data
+ i
, "../", 3)) {
546 } while (serverlen
&&
547 base
[serverlen
- 1] != '/');
550 /* If the server got removed, give up. */
551 okay
= strchr(base
, ':') - base
+ 3 <
553 } else if (alt_req
->http_specific
) {
554 char *colon
= strchr(data
+ i
, ':');
555 char *slash
= strchr(data
+ i
, '/');
556 if (colon
&& slash
&& colon
< data
+ posn
&&
557 slash
< data
+ posn
&& colon
< slash
) {
561 /* skip "objects\n" at end */
563 target
= xmalloc(serverlen
+ posn
- i
- 6);
564 memcpy(target
, base
, serverlen
);
565 memcpy(target
+ serverlen
, data
+ i
,
567 target
[serverlen
+ posn
- i
- 7] = 0;
568 if (walker
->get_verbosely
)
570 "Also look at %s\n", target
);
571 newalt
= xmalloc(sizeof(*newalt
));
573 newalt
->base
= target
;
574 newalt
->got_indices
= 0;
575 newalt
->packs
= NULL
;
577 while (tail
->next
!= NULL
)
585 cdata
->got_alternates
= 1;
588 static void fetch_alternates(struct walker
*walker
, const char *base
)
590 struct strbuf buffer
= STRBUF_INIT
;
592 struct active_request_slot
*slot
;
593 struct alternates_request alt_req
;
594 struct walker_data
*cdata
= walker
->data
;
596 /* If another request has already started fetching alternates,
597 wait for them to arrive and return to processing this request's
599 #ifdef USE_CURL_MULTI
600 while (cdata
->got_alternates
== 0) {
605 /* Nothing to do if they've already been fetched */
606 if (cdata
->got_alternates
== 1)
609 /* Start the fetch */
610 cdata
->got_alternates
= 0;
612 if (walker
->get_verbosely
)
613 fprintf(stderr
, "Getting alternates list for %s\n", base
);
615 url
= xmalloc(strlen(base
) + 31);
616 sprintf(url
, "%s/objects/info/http-alternates", base
);
618 /* Use a callback to process the result, since another request
619 may fail and need to have alternates loaded before continuing */
620 slot
= get_active_slot();
621 slot
->callback_func
= process_alternates_response
;
622 alt_req
.walker
= walker
;
623 slot
->callback_data
= &alt_req
;
625 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
626 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
627 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
631 alt_req
.buffer
= &buffer
;
632 alt_req
.http_specific
= 1;
635 if (start_active_slot(slot
))
636 run_active_slot(slot
);
638 cdata
->got_alternates
= -1;
640 strbuf_release(&buffer
);
644 static int fetch_indices(struct walker
*walker
, struct alt_base
*repo
)
646 unsigned char sha1
[20];
648 struct strbuf buffer
= STRBUF_INIT
;
653 struct active_request_slot
*slot
;
654 struct slot_results results
;
656 if (repo
->got_indices
)
659 if (walker
->get_verbosely
)
660 fprintf(stderr
, "Getting pack list for %s\n", repo
->base
);
662 url
= xmalloc(strlen(repo
->base
) + 21);
663 sprintf(url
, "%s/objects/info/packs", repo
->base
);
665 slot
= get_active_slot();
666 slot
->results
= &results
;
667 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
668 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
669 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
670 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
671 if (start_active_slot(slot
)) {
672 run_active_slot(slot
);
673 if (results
.curl_result
!= CURLE_OK
) {
674 if (missing_target(&results
)) {
675 repo
->got_indices
= 1;
678 repo
->got_indices
= 0;
679 ret
= error("%s", curl_errorstr
);
684 repo
->got_indices
= 0;
685 ret
= error("Unable to start request");
690 while (i
< buffer
.len
) {
694 if (i
+ 52 <= buffer
.len
&&
695 !prefixcmp(data
+ i
, " pack-") &&
696 !prefixcmp(data
+ i
+ 46, ".pack\n")) {
697 get_sha1_hex(data
+ i
+ 6, sha1
);
698 setup_index(walker
, repo
, sha1
);
703 while (i
< buffer
.len
&& data
[i
] != '\n')
709 repo
->got_indices
= 1;
711 strbuf_release(&buffer
);
716 static int fetch_pack(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
719 struct packed_git
*target
;
720 struct packed_git
**lst
;
723 char tmpfile
[PATH_MAX
];
726 char range
[RANGE_HEADER_SIZE
];
727 struct curl_slist
*range_header
= NULL
;
728 struct walker_data
*data
= walker
->data
;
730 struct active_request_slot
*slot
;
731 struct slot_results results
;
733 if (fetch_indices(walker
, repo
))
735 target
= find_sha1_pack(sha1
, repo
->packs
);
739 if (walker
->get_verbosely
) {
740 fprintf(stderr
, "Getting pack %s\n",
741 sha1_to_hex(target
->sha1
));
742 fprintf(stderr
, " which contains %s\n",
746 url
= xmalloc(strlen(repo
->base
) + 65);
747 sprintf(url
, "%s/objects/pack/pack-%s.pack",
748 repo
->base
, sha1_to_hex(target
->sha1
));
750 filename
= sha1_pack_name(target
->sha1
);
751 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
752 packfile
= fopen(tmpfile
, "a");
754 return error("Unable to open local file %s for pack",
757 slot
= get_active_slot();
758 slot
->results
= &results
;
759 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, packfile
);
760 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
761 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
762 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, data
->no_pragma_header
);
763 slot
->local
= packfile
;
765 /* If there is data present from a previous transfer attempt,
766 resume where it left off */
767 prev_posn
= ftell(packfile
);
769 if (walker
->get_verbosely
)
771 "Resuming fetch of pack %s at byte %ld\n",
772 sha1_to_hex(target
->sha1
), prev_posn
);
773 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
774 range_header
= curl_slist_append(range_header
, range
);
775 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
778 if (start_active_slot(slot
)) {
779 run_active_slot(slot
);
780 if (results
.curl_result
!= CURLE_OK
) {
783 return error("Unable to get pack file %s\n%s", url
,
789 return error("Unable to start request");
792 target
->pack_size
= ftell(packfile
);
796 ret
= move_temp_to_file(tmpfile
, filename
);
801 while (*lst
!= target
)
802 lst
= &((*lst
)->next
);
805 if (verify_pack(target
))
807 install_packed_git(target
);
812 static void abort_object_request(struct object_request
*obj_req
)
814 if (obj_req
->local
>= 0) {
815 close(obj_req
->local
);
818 unlink_or_warn(obj_req
->tmpfile
);
820 release_active_slot(obj_req
->slot
);
821 obj_req
->slot
= NULL
;
823 release_object_request(obj_req
);
826 static int fetch_object(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
828 char *hex
= sha1_to_hex(sha1
);
830 struct object_request
*obj_req
= object_queue_head
;
832 while (obj_req
!= NULL
&& hashcmp(obj_req
->sha1
, sha1
))
833 obj_req
= obj_req
->next
;
835 return error("Couldn't find request for %s in the queue", hex
);
837 if (has_sha1_file(obj_req
->sha1
)) {
838 abort_object_request(obj_req
);
842 #ifdef USE_CURL_MULTI
843 while (obj_req
->state
== WAITING
) {
847 start_object_request(walker
, obj_req
);
850 while (obj_req
->state
== ACTIVE
) {
851 run_active_slot(obj_req
->slot
);
853 if (obj_req
->local
!= -1) {
854 close(obj_req
->local
); obj_req
->local
= -1;
857 if (obj_req
->state
== ABORTED
) {
858 ret
= error("Request for %s aborted", hex
);
859 } else if (obj_req
->curl_result
!= CURLE_OK
&&
860 obj_req
->http_code
!= 416) {
861 if (missing_target(obj_req
))
862 ret
= -1; /* Be silent, it is probably in a pack. */
864 ret
= error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
865 obj_req
->errorstr
, obj_req
->curl_result
,
866 obj_req
->http_code
, hex
);
867 } else if (obj_req
->zret
!= Z_STREAM_END
) {
868 walker
->corrupt_object_found
++;
869 ret
= error("File %s (%s) corrupt", hex
, obj_req
->url
);
870 } else if (hashcmp(obj_req
->sha1
, obj_req
->real_sha1
)) {
871 ret
= error("File %s has bad hash", hex
);
872 } else if (obj_req
->rename
< 0) {
873 ret
= error("unable to write sha1 filename %s",
877 release_object_request(obj_req
);
881 static int fetch(struct walker
*walker
, unsigned char *sha1
)
883 struct walker_data
*data
= walker
->data
;
884 struct alt_base
*altbase
= data
->alt
;
886 if (!fetch_object(walker
, altbase
, sha1
))
889 if (!fetch_pack(walker
, altbase
, sha1
))
891 fetch_alternates(walker
, data
->alt
->base
);
892 altbase
= altbase
->next
;
894 return error("Unable to find %s under %s", sha1_to_hex(sha1
),
898 static int fetch_ref(struct walker
*walker
, struct ref
*ref
)
900 struct walker_data
*data
= walker
->data
;
901 return http_fetch_ref(data
->alt
->base
, ref
);
904 static void cleanup(struct walker
*walker
)
906 struct walker_data
*data
= walker
->data
;
909 curl_slist_free_all(data
->no_pragma_header
);
912 struct walker
*get_http_walker(const char *url
, struct remote
*remote
)
915 struct walker_data
*data
= xmalloc(sizeof(struct walker_data
));
916 struct walker
*walker
= xmalloc(sizeof(struct walker
));
920 data
->no_pragma_header
= curl_slist_append(NULL
, "Pragma:");
922 data
->alt
= xmalloc(sizeof(*data
->alt
));
923 data
->alt
->base
= xmalloc(strlen(url
) + 1);
924 strcpy(data
->alt
->base
, url
);
925 for (s
= data
->alt
->base
+ strlen(data
->alt
->base
) - 1; *s
== '/'; --s
)
928 data
->alt
->got_indices
= 0;
929 data
->alt
->packs
= NULL
;
930 data
->alt
->next
= NULL
;
931 data
->got_alternates
= -1;
933 walker
->corrupt_object_found
= 0;
934 walker
->fetch
= fetch
;
935 walker
->fetch_ref
= fetch_ref
;
936 walker
->prefetch
= prefetch
;
937 walker
->cleanup
= cleanup
;
940 #ifdef USE_CURL_MULTI
941 add_fill_function(walker
, (int (*)(void *)) fill_active_slot
);