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 buffer
*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 int missing__target(int code
, int result
)
95 return /* file:// URL -- do we ever use one??? */
96 (result
== CURLE_FILE_COULDNT_READ_FILE
) ||
97 /* http:// and https:// URL */
98 (code
== 404 && result
== CURLE_HTTP_RETURNED_ERROR
) ||
100 (code
== 550 && result
== CURLE_FTP_COULDNT_RETR_FILE
)
104 #define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
106 static void fetch_alternates(struct walker
*walker
, const char *base
);
108 static void process_object_response(void *callback_data
);
110 static void start_object_request(struct walker
*walker
,
111 struct object_request
*obj_req
)
113 char *hex
= sha1_to_hex(obj_req
->sha1
);
114 char prevfile
[PATH_MAX
];
118 unsigned char prev_buf
[PREV_BUF_SIZE
];
119 ssize_t prev_read
= 0;
121 char range
[RANGE_HEADER_SIZE
];
122 struct curl_slist
*range_header
= NULL
;
123 struct active_request_slot
*slot
;
124 struct walker_data
*data
= walker
->data
;
126 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", obj_req
->filename
);
128 rename(obj_req
->tmpfile
, prevfile
);
129 unlink(obj_req
->tmpfile
);
131 if (obj_req
->local
!= -1)
132 error("fd leakage in start: %d", obj_req
->local
);
133 obj_req
->local
= open(obj_req
->tmpfile
,
134 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
135 /* This could have failed due to the "lazy directory creation";
136 * try to mkdir the last path component.
138 if (obj_req
->local
< 0 && errno
== ENOENT
) {
139 char *dir
= strrchr(obj_req
->tmpfile
, '/');
142 mkdir(obj_req
->tmpfile
, 0777);
145 obj_req
->local
= open(obj_req
->tmpfile
,
146 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
149 if (obj_req
->local
< 0) {
150 obj_req
->state
= ABORTED
;
151 error("Couldn't create temporary file %s for %s: %s",
152 obj_req
->tmpfile
, obj_req
->filename
, strerror(errno
));
156 memset(&obj_req
->stream
, 0, sizeof(obj_req
->stream
));
158 inflateInit(&obj_req
->stream
);
160 SHA1_Init(&obj_req
->c
);
162 url
= xmalloc(strlen(obj_req
->repo
->base
) + 51);
163 obj_req
->url
= xmalloc(strlen(obj_req
->repo
->base
) + 51);
164 strcpy(url
, obj_req
->repo
->base
);
165 posn
= url
+ strlen(obj_req
->repo
->base
);
166 strcpy(posn
, "/objects/");
168 memcpy(posn
, hex
, 2);
171 strcpy(posn
, hex
+ 2);
172 strcpy(obj_req
->url
, url
);
174 /* If a previous temp file is present, process what was already
176 prevlocal
= open(prevfile
, O_RDONLY
);
177 if (prevlocal
!= -1) {
179 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
181 if (fwrite_sha1_file(prev_buf
,
184 obj_req
) == prev_read
) {
185 prev_posn
+= prev_read
;
190 } while (prev_read
> 0);
195 /* Reset inflate/SHA1 if there was an error reading the previous temp
196 file; also rewind to the beginning of the local file. */
197 if (prev_read
== -1) {
198 memset(&obj_req
->stream
, 0, sizeof(obj_req
->stream
));
199 inflateInit(&obj_req
->stream
);
200 SHA1_Init(&obj_req
->c
);
203 lseek(obj_req
->local
, 0, SEEK_SET
);
204 ftruncate(obj_req
->local
, 0);
208 slot
= get_active_slot();
209 slot
->callback_func
= process_object_response
;
210 slot
->callback_data
= obj_req
;
211 obj_req
->slot
= slot
;
213 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, obj_req
);
214 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
215 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, obj_req
->errorstr
);
216 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
217 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, data
->no_pragma_header
);
219 /* If we have successfully processed data from a previous fetch
220 attempt, only fetch the data we don't already have. */
222 if (walker
->get_verbosely
)
224 "Resuming fetch of object %s at byte %ld\n",
226 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
227 range_header
= curl_slist_append(range_header
, range
);
228 curl_easy_setopt(slot
->curl
,
229 CURLOPT_HTTPHEADER
, range_header
);
232 /* Try to get the request started, abort the request on error */
233 obj_req
->state
= ACTIVE
;
234 if (!start_active_slot(slot
)) {
235 obj_req
->state
= ABORTED
;
236 obj_req
->slot
= NULL
;
237 close(obj_req
->local
); obj_req
->local
= -1;
243 static void finish_object_request(struct object_request
*obj_req
)
247 fchmod(obj_req
->local
, 0444);
248 close(obj_req
->local
); obj_req
->local
= -1;
250 if (obj_req
->http_code
== 416) {
251 fprintf(stderr
, "Warning: requested range invalid; we may already have all the data.\n");
252 } else if (obj_req
->curl_result
!= CURLE_OK
) {
253 if (stat(obj_req
->tmpfile
, &st
) == 0)
255 unlink(obj_req
->tmpfile
);
259 inflateEnd(&obj_req
->stream
);
260 SHA1_Final(obj_req
->real_sha1
, &obj_req
->c
);
261 if (obj_req
->zret
!= Z_STREAM_END
) {
262 unlink(obj_req
->tmpfile
);
265 if (hashcmp(obj_req
->sha1
, obj_req
->real_sha1
)) {
266 unlink(obj_req
->tmpfile
);
270 move_temp_to_file(obj_req
->tmpfile
, obj_req
->filename
);
272 if (obj_req
->rename
== 0)
273 walker_say(obj_req
->walker
, "got %s\n", sha1_to_hex(obj_req
->sha1
));
276 static void process_object_response(void *callback_data
)
278 struct object_request
*obj_req
=
279 (struct object_request
*)callback_data
;
280 struct walker
*walker
= obj_req
->walker
;
281 struct walker_data
*data
= walker
->data
;
282 struct alt_base
*alt
= data
->alt
;
284 obj_req
->curl_result
= obj_req
->slot
->curl_result
;
285 obj_req
->http_code
= obj_req
->slot
->http_code
;
286 obj_req
->slot
= NULL
;
287 obj_req
->state
= COMPLETE
;
289 /* Use alternates if necessary */
290 if (missing_target(obj_req
)) {
291 fetch_alternates(walker
, alt
->base
);
292 if (obj_req
->repo
->next
!= NULL
) {
295 close(obj_req
->local
);
297 start_object_request(walker
, obj_req
);
302 finish_object_request(obj_req
);
305 static void release_object_request(struct object_request
*obj_req
)
307 struct object_request
*entry
= object_queue_head
;
309 if (obj_req
->local
!= -1)
310 error("fd leakage in release: %d", obj_req
->local
);
311 if (obj_req
== object_queue_head
) {
312 object_queue_head
= obj_req
->next
;
314 while (entry
->next
!= NULL
&& entry
->next
!= obj_req
)
316 if (entry
->next
== obj_req
)
317 entry
->next
= entry
->next
->next
;
324 #ifdef USE_CURL_MULTI
325 static int fill_active_slot(struct walker
*walker
)
327 struct object_request
*obj_req
;
329 for (obj_req
= object_queue_head
; obj_req
; obj_req
= obj_req
->next
) {
330 if (obj_req
->state
== WAITING
) {
331 if (has_sha1_file(obj_req
->sha1
))
332 obj_req
->state
= COMPLETE
;
334 start_object_request(walker
, obj_req
);
343 static void prefetch(struct walker
*walker
, unsigned char *sha1
)
345 struct object_request
*newreq
;
346 struct object_request
*tail
;
347 struct walker_data
*data
= walker
->data
;
348 char *filename
= sha1_file_name(sha1
);
350 newreq
= xmalloc(sizeof(*newreq
));
351 newreq
->walker
= walker
;
352 hashcpy(newreq
->sha1
, sha1
);
353 newreq
->repo
= data
->alt
;
356 newreq
->state
= WAITING
;
357 snprintf(newreq
->filename
, sizeof(newreq
->filename
), "%s", filename
);
358 snprintf(newreq
->tmpfile
, sizeof(newreq
->tmpfile
),
359 "%s.temp", filename
);
363 if (object_queue_head
== NULL
) {
364 object_queue_head
= newreq
;
366 tail
= object_queue_head
;
367 while (tail
->next
!= NULL
) {
373 #ifdef USE_CURL_MULTI
379 static int fetch_index(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
381 char *hex
= sha1_to_hex(sha1
);
384 char tmpfile
[PATH_MAX
];
386 char range
[RANGE_HEADER_SIZE
];
387 struct curl_slist
*range_header
= NULL
;
388 struct walker_data
*data
= walker
->data
;
391 struct active_request_slot
*slot
;
392 struct slot_results results
;
394 if (has_pack_index(sha1
))
397 if (walker
->get_verbosely
)
398 fprintf(stderr
, "Getting index for pack %s\n", hex
);
400 url
= xmalloc(strlen(repo
->base
) + 64);
401 sprintf(url
, "%s/objects/pack/pack-%s.idx", repo
->base
, hex
);
403 filename
= sha1_pack_index_name(sha1
);
404 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
405 indexfile
= fopen(tmpfile
, "a");
407 return error("Unable to open local file %s for pack index",
410 slot
= get_active_slot();
411 slot
->results
= &results
;
412 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
413 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
414 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
415 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, data
->no_pragma_header
);
416 slot
->local
= indexfile
;
418 /* If there is data present from a previous transfer attempt,
419 resume where it left off */
420 prev_posn
= ftell(indexfile
);
422 if (walker
->get_verbosely
)
424 "Resuming fetch of index for pack %s at byte %ld\n",
426 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
427 range_header
= curl_slist_append(range_header
, range
);
428 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
431 if (start_active_slot(slot
)) {
432 run_active_slot(slot
);
433 if (results
.curl_result
!= CURLE_OK
) {
435 return error("Unable to get pack index %s\n%s", url
,
440 return error("Unable to start request");
445 return move_temp_to_file(tmpfile
, filename
);
448 static int setup_index(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
450 struct packed_git
*new_pack
;
451 if (has_pack_file(sha1
))
452 return 0; /* don't list this as something we can get */
454 if (fetch_index(walker
, repo
, sha1
))
457 new_pack
= parse_pack_index(sha1
);
458 new_pack
->next
= repo
->packs
;
459 repo
->packs
= new_pack
;
463 static void process_alternates_response(void *callback_data
)
465 struct alternates_request
*alt_req
=
466 (struct alternates_request
*)callback_data
;
467 struct walker
*walker
= alt_req
->walker
;
468 struct walker_data
*cdata
= walker
->data
;
469 struct active_request_slot
*slot
= alt_req
->slot
;
470 struct alt_base
*tail
= cdata
->alt
;
471 const char *base
= alt_req
->base
;
472 static const char null_byte
= '\0';
476 if (alt_req
->http_specific
) {
477 if (slot
->curl_result
!= CURLE_OK
||
478 !alt_req
->buffer
->posn
) {
480 /* Try reusing the slot to get non-http alternates */
481 alt_req
->http_specific
= 0;
482 sprintf(alt_req
->url
, "%s/objects/info/alternates",
484 curl_easy_setopt(slot
->curl
, CURLOPT_URL
,
488 if (slot
->finished
!= NULL
)
489 (*slot
->finished
) = 0;
490 if (!start_active_slot(slot
)) {
491 cdata
->got_alternates
= -1;
493 if (slot
->finished
!= NULL
)
494 (*slot
->finished
) = 1;
498 } else if (slot
->curl_result
!= CURLE_OK
) {
499 if (!missing_target(slot
)) {
500 cdata
->got_alternates
= -1;
505 fwrite_buffer(&null_byte
, 1, 1, alt_req
->buffer
);
506 alt_req
->buffer
->posn
--;
507 data
= alt_req
->buffer
->buffer
;
509 while (i
< alt_req
->buffer
->posn
) {
511 while (posn
< alt_req
->buffer
->posn
&& data
[posn
] != '\n')
513 if (data
[posn
] == '\n') {
516 struct alt_base
*newalt
;
518 if (data
[i
] == '/') {
520 * http://git.host/pub/scm/linux.git/
522 * so memcpy(dst, base, serverlen) will
523 * copy up to "...git.host".
525 const char *colon_ss
= strstr(base
,"://");
527 serverlen
= (strchr(colon_ss
+ 3, '/')
531 } else if (!memcmp(data
+ i
, "../", 3)) {
532 /* Relative URL; chop the corresponding
533 * number of subpath from base (and ../
534 * from data), and concatenate the result.
536 * The code first drops ../ from data, and
537 * then drops one ../ from data and one path
538 * from base. IOW, one extra ../ is dropped
539 * from data than path is dropped from base.
541 * This is not wrong. The alternate in
542 * http://git.host/pub/scm/linux.git/
544 * http://git.host/pub/scm/linus.git/
545 * is ../../linus.git/objects/. You need
546 * two ../../ to borrow from your direct
550 serverlen
= strlen(base
);
551 while (i
+ 2 < posn
&&
552 !memcmp(data
+ i
, "../", 3)) {
555 } while (serverlen
&&
556 base
[serverlen
- 1] != '/');
559 /* If the server got removed, give up. */
560 okay
= strchr(base
, ':') - base
+ 3 <
562 } else if (alt_req
->http_specific
) {
563 char *colon
= strchr(data
+ i
, ':');
564 char *slash
= strchr(data
+ i
, '/');
565 if (colon
&& slash
&& colon
< data
+ posn
&&
566 slash
< data
+ posn
&& colon
< slash
) {
570 /* skip "objects\n" at end */
572 target
= xmalloc(serverlen
+ posn
- i
- 6);
573 memcpy(target
, base
, serverlen
);
574 memcpy(target
+ serverlen
, data
+ i
,
576 target
[serverlen
+ posn
- i
- 7] = 0;
577 if (walker
->get_verbosely
)
579 "Also look at %s\n", target
);
580 newalt
= xmalloc(sizeof(*newalt
));
582 newalt
->base
= target
;
583 newalt
->got_indices
= 0;
584 newalt
->packs
= NULL
;
586 while (tail
->next
!= NULL
)
594 cdata
->got_alternates
= 1;
597 static void fetch_alternates(struct walker
*walker
, const char *base
)
599 struct buffer buffer
;
602 struct active_request_slot
*slot
;
603 struct alternates_request alt_req
;
604 struct walker_data
*cdata
= walker
->data
;
606 /* If another request has already started fetching alternates,
607 wait for them to arrive and return to processing this request's
609 #ifdef USE_CURL_MULTI
610 while (cdata
->got_alternates
== 0) {
615 /* Nothing to do if they've already been fetched */
616 if (cdata
->got_alternates
== 1)
619 /* Start the fetch */
620 cdata
->got_alternates
= 0;
622 data
= xmalloc(4096);
625 buffer
.buffer
= data
;
627 if (walker
->get_verbosely
)
628 fprintf(stderr
, "Getting alternates list for %s\n", base
);
630 url
= xmalloc(strlen(base
) + 31);
631 sprintf(url
, "%s/objects/info/http-alternates", base
);
633 /* Use a callback to process the result, since another request
634 may fail and need to have alternates loaded before continuing */
635 slot
= get_active_slot();
636 slot
->callback_func
= process_alternates_response
;
637 alt_req
.walker
= walker
;
638 slot
->callback_data
= &alt_req
;
640 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
641 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
642 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
646 alt_req
.buffer
= &buffer
;
647 alt_req
.http_specific
= 1;
650 if (start_active_slot(slot
))
651 run_active_slot(slot
);
653 cdata
->got_alternates
= -1;
659 static int fetch_indices(struct walker
*walker
, struct alt_base
*repo
)
661 unsigned char sha1
[20];
663 struct buffer buffer
;
667 struct active_request_slot
*slot
;
668 struct slot_results results
;
670 if (repo
->got_indices
)
673 data
= xmalloc(4096);
676 buffer
.buffer
= data
;
678 if (walker
->get_verbosely
)
679 fprintf(stderr
, "Getting pack list for %s\n", repo
->base
);
681 url
= xmalloc(strlen(repo
->base
) + 21);
682 sprintf(url
, "%s/objects/info/packs", repo
->base
);
684 slot
= get_active_slot();
685 slot
->results
= &results
;
686 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
687 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
688 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
689 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
690 if (start_active_slot(slot
)) {
691 run_active_slot(slot
);
692 if (results
.curl_result
!= CURLE_OK
) {
693 if (missing_target(&results
)) {
694 repo
->got_indices
= 1;
698 repo
->got_indices
= 0;
700 return error("%s", curl_errorstr
);
704 repo
->got_indices
= 0;
706 return error("Unable to start request");
709 data
= buffer
.buffer
;
710 while (i
< buffer
.posn
) {
714 if (i
+ 52 <= buffer
.posn
&&
715 !prefixcmp(data
+ i
, " pack-") &&
716 !prefixcmp(data
+ i
+ 46, ".pack\n")) {
717 get_sha1_hex(data
+ i
+ 6, sha1
);
718 setup_index(walker
, repo
, sha1
);
723 while (i
< buffer
.posn
&& data
[i
] != '\n')
730 repo
->got_indices
= 1;
734 static int fetch_pack(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
737 struct packed_git
*target
;
738 struct packed_git
**lst
;
741 char tmpfile
[PATH_MAX
];
744 char range
[RANGE_HEADER_SIZE
];
745 struct curl_slist
*range_header
= NULL
;
746 struct walker_data
*data
= walker
->data
;
748 struct active_request_slot
*slot
;
749 struct slot_results results
;
751 if (fetch_indices(walker
, repo
))
753 target
= find_sha1_pack(sha1
, repo
->packs
);
757 if (walker
->get_verbosely
) {
758 fprintf(stderr
, "Getting pack %s\n",
759 sha1_to_hex(target
->sha1
));
760 fprintf(stderr
, " which contains %s\n",
764 url
= xmalloc(strlen(repo
->base
) + 65);
765 sprintf(url
, "%s/objects/pack/pack-%s.pack",
766 repo
->base
, sha1_to_hex(target
->sha1
));
768 filename
= sha1_pack_name(target
->sha1
);
769 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
770 packfile
= fopen(tmpfile
, "a");
772 return error("Unable to open local file %s for pack",
775 slot
= get_active_slot();
776 slot
->results
= &results
;
777 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, packfile
);
778 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
779 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
780 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, data
->no_pragma_header
);
781 slot
->local
= packfile
;
783 /* If there is data present from a previous transfer attempt,
784 resume where it left off */
785 prev_posn
= ftell(packfile
);
787 if (walker
->get_verbosely
)
789 "Resuming fetch of pack %s at byte %ld\n",
790 sha1_to_hex(target
->sha1
), prev_posn
);
791 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
792 range_header
= curl_slist_append(range_header
, range
);
793 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
796 if (start_active_slot(slot
)) {
797 run_active_slot(slot
);
798 if (results
.curl_result
!= CURLE_OK
) {
800 return error("Unable to get pack file %s\n%s", url
,
805 return error("Unable to start request");
808 target
->pack_size
= ftell(packfile
);
811 ret
= move_temp_to_file(tmpfile
, filename
);
816 while (*lst
!= target
)
817 lst
= &((*lst
)->next
);
820 if (verify_pack(target
, 0))
822 install_packed_git(target
);
827 static void abort_object_request(struct object_request
*obj_req
)
829 if (obj_req
->local
>= 0) {
830 close(obj_req
->local
);
833 unlink(obj_req
->tmpfile
);
835 release_active_slot(obj_req
->slot
);
836 obj_req
->slot
= NULL
;
838 release_object_request(obj_req
);
841 static int fetch_object(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
843 char *hex
= sha1_to_hex(sha1
);
845 struct object_request
*obj_req
= object_queue_head
;
847 while (obj_req
!= NULL
&& hashcmp(obj_req
->sha1
, sha1
))
848 obj_req
= obj_req
->next
;
850 return error("Couldn't find request for %s in the queue", hex
);
852 if (has_sha1_file(obj_req
->sha1
)) {
853 abort_object_request(obj_req
);
857 #ifdef USE_CURL_MULTI
858 while (obj_req
->state
== WAITING
) {
862 start_object_request(walker
, obj_req
);
865 while (obj_req
->state
== ACTIVE
) {
866 run_active_slot(obj_req
->slot
);
868 if (obj_req
->local
!= -1) {
869 close(obj_req
->local
); obj_req
->local
= -1;
872 if (obj_req
->state
== ABORTED
) {
873 ret
= error("Request for %s aborted", hex
);
874 } else if (obj_req
->curl_result
!= CURLE_OK
&&
875 obj_req
->http_code
!= 416) {
876 if (missing_target(obj_req
))
877 ret
= -1; /* Be silent, it is probably in a pack. */
879 ret
= error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
880 obj_req
->errorstr
, obj_req
->curl_result
,
881 obj_req
->http_code
, hex
);
882 } else if (obj_req
->zret
!= Z_STREAM_END
) {
883 walker
->corrupt_object_found
++;
884 ret
= error("File %s (%s) corrupt", hex
, obj_req
->url
);
885 } else if (hashcmp(obj_req
->sha1
, obj_req
->real_sha1
)) {
886 ret
= error("File %s has bad hash", hex
);
887 } else if (obj_req
->rename
< 0) {
888 ret
= error("unable to write sha1 filename %s",
892 release_object_request(obj_req
);
896 static int fetch(struct walker
*walker
, unsigned char *sha1
)
898 struct walker_data
*data
= walker
->data
;
899 struct alt_base
*altbase
= data
->alt
;
901 if (!fetch_object(walker
, altbase
, sha1
))
904 if (!fetch_pack(walker
, altbase
, sha1
))
906 fetch_alternates(walker
, data
->alt
->base
);
907 altbase
= altbase
->next
;
909 return error("Unable to find %s under %s", sha1_to_hex(sha1
),
913 static inline int needs_quote(int ch
)
915 if (((ch
>= 'A') && (ch
<= 'Z'))
916 || ((ch
>= 'a') && (ch
<= 'z'))
917 || ((ch
>= '0') && (ch
<= '9'))
925 static inline int hex(int v
)
927 if (v
< 10) return '0' + v
;
928 else return 'A' + v
- 10;
931 static char *quote_ref_url(const char *base
, const char *ref
)
935 int len
, baselen
, ch
;
937 baselen
= strlen(base
);
938 len
= baselen
+ 7; /* "/refs/" + NUL */
939 for (cp
= ref
; (ch
= *cp
) != 0; cp
++, len
++)
941 len
+= 2; /* extra two hex plus replacement % */
943 memcpy(qref
, base
, baselen
);
944 memcpy(qref
+ baselen
, "/refs/", 6);
945 for (cp
= ref
, dp
= qref
+ baselen
+ 6; (ch
= *cp
) != 0; cp
++) {
946 if (needs_quote(ch
)) {
948 *dp
++ = hex((ch
>> 4) & 0xF);
949 *dp
++ = hex(ch
& 0xF);
959 static int fetch_ref(struct walker
*walker
, char *ref
, unsigned char *sha1
)
963 struct buffer buffer
;
964 struct walker_data
*data
= walker
->data
;
965 const char *base
= data
->alt
->base
;
966 struct active_request_slot
*slot
;
967 struct slot_results results
;
973 url
= quote_ref_url(base
, ref
);
974 slot
= get_active_slot();
975 slot
->results
= &results
;
976 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
977 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
978 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
979 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
980 if (start_active_slot(slot
)) {
981 run_active_slot(slot
);
982 if (results
.curl_result
!= CURLE_OK
)
983 return error("Couldn't get %s for %s\n%s",
984 url
, ref
, curl_errorstr
);
986 return error("Unable to start request");
990 get_sha1_hex(hex
, sha1
);
994 static void cleanup(struct walker
*walker
)
996 struct walker_data
*data
= walker
->data
;
999 curl_slist_free_all(data
->no_pragma_header
);
1002 struct walker
*get_http_walker(const char *url
)
1005 struct walker_data
*data
= xmalloc(sizeof(struct walker_data
));
1006 struct walker
*walker
= xmalloc(sizeof(struct walker
));
1010 data
->no_pragma_header
= curl_slist_append(NULL
, "Pragma:");
1012 data
->alt
= xmalloc(sizeof(*data
->alt
));
1013 data
->alt
->base
= xmalloc(strlen(url
) + 1);
1014 strcpy(data
->alt
->base
, url
);
1015 for (s
= data
->alt
->base
+ strlen(data
->alt
->base
) - 1; *s
== '/'; --s
)
1018 data
->alt
->got_indices
= 0;
1019 data
->alt
->packs
= NULL
;
1020 data
->alt
->next
= NULL
;
1021 data
->got_alternates
= -1;
1023 walker
->corrupt_object_found
= 0;
1024 walker
->fetch
= fetch
;
1025 walker
->fetch_ref
= fetch_ref
;
1026 walker
->prefetch
= prefetch
;
1027 walker
->cleanup
= cleanup
;
1028 walker
->data
= data
;
1030 #ifdef USE_CURL_MULTI
1031 add_fill_function(walker
, (int (*)(void *)) fill_active_slot
);