9 #if LIBCURL_VERSION_NUM >= 0x070908
10 #define USE_CURL_MULTI
11 #define DEFAULT_MAX_REQUESTS 5
14 #if LIBCURL_VERSION_NUM < 0x070704
15 #define curl_global_cleanup() do { /* nothing */ } while(0)
17 #if LIBCURL_VERSION_NUM < 0x070800
18 #define curl_global_init(a) do { /* nothing */ } while(0)
21 #define PREV_BUF_SIZE 4096
22 #define RANGE_HEADER_SIZE 30
24 static int active_requests
= 0;
25 static int data_received
;
28 static int max_requests
= DEFAULT_MAX_REQUESTS
;
31 static CURL
*curl_default
;
32 static struct curl_slist
*pragma_header
;
33 static struct curl_slist
*no_pragma_header
;
34 static struct curl_slist
*no_range_header
;
35 static char curl_errorstr
[CURL_ERROR_SIZE
];
41 struct packed_git
*packs
;
42 struct alt_base
*next
;
45 static struct alt_base
*alt
= NULL
;
54 struct transfer_request
56 unsigned char sha1
[20];
57 struct alt_base
*repo
;
59 char filename
[PATH_MAX
];
60 char tmpfile
[PATH_MAX
];
62 enum transfer_state state
;
64 char errorstr
[CURL_ERROR_SIZE
];
66 unsigned char real_sha1
[20];
71 struct active_request_slot
*slot
;
72 struct transfer_request
*next
;
75 struct active_request_slot
82 struct active_request_slot
*next
;
85 static struct transfer_request
*request_queue_head
= NULL
;
86 static struct active_request_slot
*active_queue_head
= NULL
;
88 static int curl_ssl_verify
;
89 static char *ssl_cert
;
91 static char *ssl_capath
;
92 static char *ssl_cainfo
;
101 static size_t fwrite_buffer(void *ptr
, size_t eltsize
, size_t nmemb
,
102 struct buffer
*buffer
)
104 size_t size
= eltsize
* nmemb
;
105 if (size
> buffer
->size
- buffer
->posn
)
106 size
= buffer
->size
- buffer
->posn
;
107 memcpy(buffer
->buffer
+ buffer
->posn
, ptr
, size
);
108 buffer
->posn
+= size
;
113 static size_t fwrite_sha1_file(void *ptr
, size_t eltsize
, size_t nmemb
,
116 unsigned char expn
[4096];
117 size_t size
= eltsize
* nmemb
;
119 struct transfer_request
*request
= (struct transfer_request
*)data
;
121 ssize_t retval
= write(request
->local
,
122 ptr
+ posn
, size
- posn
);
126 } while (posn
< size
);
128 request
->stream
.avail_in
= size
;
129 request
->stream
.next_in
= ptr
;
131 request
->stream
.next_out
= expn
;
132 request
->stream
.avail_out
= sizeof(expn
);
133 request
->zret
= inflate(&request
->stream
, Z_SYNC_FLUSH
);
134 SHA1_Update(&request
->c
, expn
,
135 sizeof(expn
) - request
->stream
.avail_out
);
136 } while (request
->stream
.avail_in
&& request
->zret
== Z_OK
);
141 #ifdef USE_CURL_MULTI
142 void process_curl_messages();
143 void process_request_queue();
146 struct active_request_slot
*get_active_slot()
148 struct active_request_slot
*slot
= active_queue_head
;
149 struct active_request_slot
*newslot
;
151 #ifdef USE_CURL_MULTI
154 /* Wait for a slot to open up if the queue is full */
155 while (active_requests
>= max_requests
) {
156 curl_multi_perform(curlm
, &num_transfers
);
157 if (num_transfers
< active_requests
) {
158 process_curl_messages();
163 while (slot
!= NULL
&& slot
->in_use
) {
167 newslot
= xmalloc(sizeof(*newslot
));
168 newslot
->curl
= curl_easy_duphandle(curl_default
);
170 newslot
->next
= NULL
;
172 slot
= active_queue_head
;
174 active_queue_head
= newslot
;
176 while (slot
->next
!= NULL
) {
179 slot
->next
= newslot
;
188 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
189 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_range_header
);
190 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
195 int start_active_slot(struct active_request_slot
*slot
)
197 #ifdef USE_CURL_MULTI
198 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
200 if (curlm_result
!= CURLM_OK
&&
201 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
210 void run_active_slot(struct active_request_slot
*slot
)
212 #ifdef USE_CURL_MULTI
220 struct timeval select_timeout
;
221 CURLMcode curlm_result
;
223 while (!slot
->done
) {
226 curlm_result
= curl_multi_perform(curlm
,
228 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
229 if (num_transfers
< active_requests
) {
230 process_curl_messages();
231 process_request_queue();
234 if (!data_received
&& slot
->local
!= NULL
) {
235 current_pos
= ftell(slot
->local
);
236 if (current_pos
> last_pos
)
238 last_pos
= current_pos
;
241 if (!slot
->done
&& !data_received
) {
246 select_timeout
.tv_sec
= 0;
247 select_timeout
.tv_usec
= 50000;
248 select(max_fd
, &readfds
, &writefds
,
249 &excfds
, &select_timeout
);
253 slot
->curl_result
= curl_easy_perform(slot
->curl
);
258 void start_request(struct transfer_request
*request
)
260 char *hex
= sha1_to_hex(request
->sha1
);
261 char prevfile
[PATH_MAX
];
265 unsigned char prev_buf
[PREV_BUF_SIZE
];
266 ssize_t prev_read
= 0;
268 char range
[RANGE_HEADER_SIZE
];
269 struct curl_slist
*range_header
= NULL
;
270 struct active_request_slot
*slot
;
272 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", request
->filename
);
274 rename(request
->tmpfile
, prevfile
);
275 unlink(request
->tmpfile
);
277 request
->local
= open(request
->tmpfile
,
278 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
279 /* This could have failed due to the "lazy directory creation";
280 * try to mkdir the last path component.
282 if (request
->local
< 0 && errno
== ENOENT
) {
283 char *dir
= strrchr(request
->tmpfile
, '/');
286 mkdir(request
->tmpfile
, 0777);
289 request
->local
= open(request
->tmpfile
,
290 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
293 if (request
->local
< 0) {
294 request
->state
= ABORTED
;
295 error("Couldn't create temporary file %s for %s: %s\n",
296 request
->tmpfile
, request
->filename
, strerror(errno
));
300 memset(&request
->stream
, 0, sizeof(request
->stream
));
302 inflateInit(&request
->stream
);
304 SHA1_Init(&request
->c
);
306 url
= xmalloc(strlen(request
->repo
->base
) + 50);
307 request
->url
= xmalloc(strlen(request
->repo
->base
) + 50);
308 strcpy(url
, request
->repo
->base
);
309 posn
= url
+ strlen(request
->repo
->base
);
310 strcpy(posn
, "objects/");
312 memcpy(posn
, hex
, 2);
315 strcpy(posn
, hex
+ 2);
316 strcpy(request
->url
, url
);
318 /* If a previous temp file is present, process what was already
320 prevlocal
= open(prevfile
, O_RDONLY
);
321 if (prevlocal
!= -1) {
323 prev_read
= read(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
325 if (fwrite_sha1_file(prev_buf
,
328 request
) == prev_read
) {
329 prev_posn
+= prev_read
;
334 } while (prev_read
> 0);
339 /* Reset inflate/SHA1 if there was an error reading the previous temp
340 file; also rewind to the beginning of the local file. */
341 if (prev_read
== -1) {
342 memset(&request
->stream
, 0, sizeof(request
->stream
));
343 inflateInit(&request
->stream
);
344 SHA1_Init(&request
->c
);
347 lseek(request
->local
, SEEK_SET
, 0);
348 ftruncate(request
->local
, 0);
352 slot
= get_active_slot();
353 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, request
);
354 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
355 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
356 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
357 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
359 /* If we have successfully processed data from a previous fetch
360 attempt, only fetch the data we don't already have. */
364 "Resuming fetch of object %s at byte %ld\n",
366 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
367 range_header
= curl_slist_append(range_header
, range
);
368 curl_easy_setopt(slot
->curl
,
369 CURLOPT_HTTPHEADER
, range_header
);
372 /* Try to get the request started, abort the request on error */
373 if (!start_active_slot(slot
)) {
374 request
->state
= ABORTED
;
375 close(request
->local
);
380 request
->slot
= slot
;
381 request
->state
= ACTIVE
;
384 void finish_request(struct transfer_request
*request
)
386 fchmod(request
->local
, 0444);
387 close(request
->local
);
389 if (request
->http_code
== 416) {
390 fprintf(stderr
, "Warning: requested range invalid; we may already have all the data.\n");
391 } else if (request
->curl_result
!= CURLE_OK
) {
395 inflateEnd(&request
->stream
);
396 SHA1_Final(request
->real_sha1
, &request
->c
);
397 if (request
->zret
!= Z_STREAM_END
) {
398 unlink(request
->tmpfile
);
401 if (memcmp(request
->sha1
, request
->real_sha1
, 20)) {
402 unlink(request
->tmpfile
);
406 move_temp_to_file(request
->tmpfile
, request
->filename
);
408 if (request
->rename
== 0)
409 pull_say("got %s\n", sha1_to_hex(request
->sha1
));
412 void release_request(struct transfer_request
*request
)
414 struct transfer_request
*entry
= request_queue_head
;
416 if (request
== request_queue_head
) {
417 request_queue_head
= request
->next
;
419 while (entry
->next
!= NULL
&& entry
->next
!= request
)
421 if (entry
->next
== request
)
422 entry
->next
= entry
->next
->next
;
429 #ifdef USE_CURL_MULTI
430 void process_curl_messages()
433 struct active_request_slot
*slot
;
434 struct transfer_request
*request
= NULL
;
435 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
437 while (curl_message
!= NULL
) {
438 if (curl_message
->msg
== CURLMSG_DONE
) {
439 slot
= active_queue_head
;
440 while (slot
!= NULL
&&
441 slot
->curl
!= curl_message
->easy_handle
)
444 curl_multi_remove_handle(curlm
, slot
->curl
);
448 slot
->curl_result
= curl_message
->data
.result
;
449 request
= request_queue_head
;
450 while (request
!= NULL
&&
451 request
->slot
!= slot
)
452 request
= request
->next
;
454 fprintf(stderr
, "Received DONE message for unknown request!\n");
456 if (request
!= NULL
) {
457 request
->curl_result
=
458 curl_message
->data
.result
;
459 curl_easy_getinfo(slot
->curl
,
461 &request
->http_code
);
462 request
->slot
= NULL
;
464 /* Use alternates if necessary */
465 if (request
->http_code
== 404 &&
466 request
->repo
->next
!= NULL
) {
467 request
->repo
= request
->repo
->next
;
468 start_request(request
);
470 finish_request(request
);
471 request
->state
= COMPLETE
;
475 fprintf(stderr
, "Unknown CURL message received: %d\n",
476 (int)curl_message
->msg
);
478 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
482 void process_request_queue()
484 struct transfer_request
*request
= request_queue_head
;
487 while (active_requests
< max_requests
&& request
!= NULL
) {
488 if (request
->state
== WAITING
) {
489 if (has_sha1_file(request
->sha1
))
490 release_request(request
);
492 start_request(request
);
493 curl_multi_perform(curlm
, &num_transfers
);
495 request
= request
->next
;
500 void prefetch(unsigned char *sha1
)
502 struct transfer_request
*newreq
;
503 struct transfer_request
*tail
;
504 char *filename
= sha1_file_name(sha1
);
506 newreq
= xmalloc(sizeof(*newreq
));
507 memcpy(newreq
->sha1
, sha1
, 20);
511 newreq
->state
= WAITING
;
512 snprintf(newreq
->filename
, sizeof(newreq
->filename
), "%s", filename
);
513 snprintf(newreq
->tmpfile
, sizeof(newreq
->tmpfile
),
514 "%s.temp", filename
);
517 if (request_queue_head
== NULL
) {
518 request_queue_head
= newreq
;
520 tail
= request_queue_head
;
521 while (tail
->next
!= NULL
) {
526 #ifdef USE_CURL_MULTI
527 process_request_queue();
528 process_curl_messages();
532 static int fetch_index(struct alt_base
*repo
, unsigned char *sha1
)
534 char *hex
= sha1_to_hex(sha1
);
537 char tmpfile
[PATH_MAX
];
539 char range
[RANGE_HEADER_SIZE
];
540 struct curl_slist
*range_header
= NULL
;
543 struct active_request_slot
*slot
;
545 if (has_pack_index(sha1
))
549 fprintf(stderr
, "Getting index for pack %s\n", hex
);
551 url
= xmalloc(strlen(repo
->base
) + 64);
552 sprintf(url
, "%s/objects/pack/pack-%s.idx", repo
->base
, hex
);
554 filename
= sha1_pack_index_name(sha1
);
555 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
556 indexfile
= fopen(tmpfile
, "a");
558 return error("Unable to open local file %s for pack index",
561 slot
= get_active_slot();
562 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
563 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
564 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
565 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
566 slot
->local
= indexfile
;
568 /* If there is data present from a previous transfer attempt,
569 resume where it left off */
570 prev_posn
= ftell(indexfile
);
574 "Resuming fetch of index for pack %s at byte %ld\n",
576 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
577 range_header
= curl_slist_append(range_header
, range
);
578 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
581 if (start_active_slot(slot
)) {
582 run_active_slot(slot
);
583 if (slot
->curl_result
!= CURLE_OK
) {
585 return error("Unable to get pack index %s\n%s", url
,
589 return error("Unable to start request");
594 return move_temp_to_file(tmpfile
, filename
);
597 static int setup_index(struct alt_base
*repo
, unsigned char *sha1
)
599 struct packed_git
*new_pack
;
600 if (has_pack_file(sha1
))
601 return 0; // don't list this as something we can get
603 if (fetch_index(repo
, sha1
))
606 new_pack
= parse_pack_index(sha1
);
607 new_pack
->next
= repo
->packs
;
608 repo
->packs
= new_pack
;
612 static int fetch_alternates(char *base
)
615 struct buffer buffer
;
619 int http_specific
= 1;
620 struct alt_base
*tail
= alt
;
622 struct active_request_slot
*slot
;
624 data
= xmalloc(4096);
627 buffer
.buffer
= data
;
630 fprintf(stderr
, "Getting alternates list\n");
632 url
= xmalloc(strlen(base
) + 31);
633 sprintf(url
, "%s/objects/info/http-alternates", base
);
635 slot
= get_active_slot();
636 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
637 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
638 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
639 if (start_active_slot(slot
)) {
640 run_active_slot(slot
);
641 if (slot
->curl_result
!= CURLE_OK
|| !buffer
.posn
) {
644 sprintf(url
, "%s/objects/info/alternates", base
);
646 slot
= get_active_slot();
647 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
648 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
650 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
651 if (start_active_slot(slot
)) {
652 run_active_slot(slot
);
653 if (slot
->curl_result
!= CURLE_OK
) {
662 data
[buffer
.posn
] = '\0';
664 while (i
< buffer
.posn
) {
666 while (posn
< buffer
.posn
&& data
[posn
] != '\n')
668 if (data
[posn
] == '\n') {
671 struct alt_base
*newalt
;
673 if (data
[i
] == '/') {
674 serverlen
= strchr(base
+ 8, '/') - base
;
676 } else if (!memcmp(data
+ i
, "../", 3)) {
678 serverlen
= strlen(base
);
679 while (i
+ 2 < posn
&&
680 !memcmp(data
+ i
, "../", 3)) {
683 } while (serverlen
&&
684 base
[serverlen
- 1] != '/');
687 // If the server got removed, give up.
688 okay
= strchr(base
, ':') - base
+ 3 <
690 } else if (http_specific
) {
691 char *colon
= strchr(data
+ i
, ':');
692 char *slash
= strchr(data
+ i
, '/');
693 if (colon
&& slash
&& colon
< data
+ posn
&&
694 slash
< data
+ posn
&& colon
< slash
) {
698 // skip 'objects' at end
700 target
= xmalloc(serverlen
+ posn
- i
- 6);
701 strncpy(target
, base
, serverlen
);
702 strncpy(target
+ serverlen
, data
+ i
,
704 target
[serverlen
+ posn
- i
- 7] = '\0';
707 "Also look at %s\n", target
);
708 newalt
= xmalloc(sizeof(*newalt
));
710 newalt
->base
= target
;
711 newalt
->got_indices
= 0;
712 newalt
->packs
= NULL
;
713 while (tail
->next
!= NULL
)
725 static int fetch_indices(struct alt_base
*repo
)
727 unsigned char sha1
[20];
729 struct buffer buffer
;
733 struct active_request_slot
*slot
;
735 if (repo
->got_indices
)
738 data
= xmalloc(4096);
741 buffer
.buffer
= data
;
744 fprintf(stderr
, "Getting pack list\n");
746 url
= xmalloc(strlen(repo
->base
) + 21);
747 sprintf(url
, "%s/objects/info/packs", repo
->base
);
749 slot
= get_active_slot();
750 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
751 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
752 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
753 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
754 if (start_active_slot(slot
)) {
755 run_active_slot(slot
);
756 if (slot
->curl_result
!= CURLE_OK
)
757 return error("%s", curl_errorstr
);
759 return error("Unable to start request");
762 while (i
< buffer
.posn
) {
766 if (i
+ 52 < buffer
.posn
&&
767 !strncmp(data
+ i
, " pack-", 6) &&
768 !strncmp(data
+ i
+ 46, ".pack\n", 6)) {
769 get_sha1_hex(data
+ i
+ 6, sha1
);
770 setup_index(repo
, sha1
);
775 while (data
[i
] != '\n')
781 repo
->got_indices
= 1;
785 static int fetch_pack(struct alt_base
*repo
, unsigned char *sha1
)
788 struct packed_git
*target
;
789 struct packed_git
**lst
;
792 char tmpfile
[PATH_MAX
];
795 char range
[RANGE_HEADER_SIZE
];
796 struct curl_slist
*range_header
= NULL
;
798 struct active_request_slot
*slot
;
800 if (fetch_indices(repo
))
802 target
= find_sha1_pack(sha1
, repo
->packs
);
807 fprintf(stderr
, "Getting pack %s\n",
808 sha1_to_hex(target
->sha1
));
809 fprintf(stderr
, " which contains %s\n",
813 url
= xmalloc(strlen(repo
->base
) + 65);
814 sprintf(url
, "%s/objects/pack/pack-%s.pack",
815 repo
->base
, sha1_to_hex(target
->sha1
));
817 filename
= sha1_pack_name(target
->sha1
);
818 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
819 packfile
= fopen(tmpfile
, "a");
821 return error("Unable to open local file %s for pack",
824 slot
= get_active_slot();
825 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, packfile
);
826 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
827 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
828 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
829 slot
->local
= packfile
;
831 /* If there is data present from a previous transfer attempt,
832 resume where it left off */
833 prev_posn
= ftell(packfile
);
837 "Resuming fetch of pack %s at byte %ld\n",
838 sha1_to_hex(target
->sha1
), prev_posn
);
839 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
840 range_header
= curl_slist_append(range_header
, range
);
841 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
844 if (start_active_slot(slot
)) {
845 run_active_slot(slot
);
846 if (slot
->curl_result
!= CURLE_OK
) {
848 return error("Unable to get pack file %s\n%s", url
,
852 return error("Unable to start request");
857 ret
= move_temp_to_file(tmpfile
, filename
);
862 while (*lst
!= target
)
863 lst
= &((*lst
)->next
);
866 if (verify_pack(target
, 0))
868 install_packed_git(target
);
873 static int fetch_object(struct alt_base
*repo
, unsigned char *sha1
)
875 char *hex
= sha1_to_hex(sha1
);
877 struct transfer_request
*request
= request_queue_head
;
879 while (request
!= NULL
&& memcmp(request
->sha1
, sha1
, 20))
880 request
= request
->next
;
882 return error("Couldn't find request for %s in the queue", hex
);
884 if (has_sha1_file(request
->sha1
)) {
885 release_request(request
);
889 #ifdef USE_CURL_MULTI
891 while (request
->state
== WAITING
) {
892 curl_multi_perform(curlm
, &num_transfers
);
893 if (num_transfers
< active_requests
) {
894 process_curl_messages();
895 process_request_queue();
899 start_request(request
);
902 while (request
->state
== ACTIVE
) {
903 run_active_slot(request
->slot
);
904 #ifndef USE_CURL_MULTI
905 request
->curl_result
= request
->slot
->curl_result
;
906 curl_easy_getinfo(request
->slot
->curl
,
908 &request
->http_code
);
909 request
->slot
= NULL
;
911 /* Use alternates if necessary */
912 if (request
->http_code
== 404 &&
913 request
->repo
->next
!= NULL
) {
914 request
->repo
= request
->repo
->next
;
915 start_request(request
);
917 finish_request(request
);
918 request
->state
= COMPLETE
;
923 if (request
->state
== ABORTED
) {
924 release_request(request
);
925 return error("Request for %s aborted", hex
);
928 if (request
->curl_result
!= CURLE_OK
&& request
->http_code
!= 416) {
929 ret
= error("%s", request
->errorstr
);
930 release_request(request
);
934 if (request
->zret
!= Z_STREAM_END
) {
935 ret
= error("File %s (%s) corrupt\n", hex
, request
->url
);
936 release_request(request
);
940 if (memcmp(request
->sha1
, request
->real_sha1
, 20)) {
941 release_request(request
);
942 return error("File %s has bad hash\n", hex
);
945 if (request
->rename
< 0) {
946 ret
= error("unable to write sha1 filename %s: %s",
948 strerror(request
->rename
));
949 release_request(request
);
953 release_request(request
);
957 int fetch(unsigned char *sha1
)
959 struct alt_base
*altbase
= alt
;
961 if (!fetch_object(altbase
, sha1
))
964 if (!fetch_pack(altbase
, sha1
))
966 altbase
= altbase
->next
;
968 return error("Unable to find %s under %s\n", sha1_to_hex(sha1
),
972 static inline int needs_quote(int ch
)
975 case '/': case '-': case '.':
976 case 'A'...'Z': case 'a'...'z': case '0'...'9':
983 static inline int hex(int v
)
985 if (v
< 10) return '0' + v
;
986 else return 'A' + v
- 10;
989 static char *quote_ref_url(const char *base
, const char *ref
)
993 int len
, baselen
, ch
;
995 baselen
= strlen(base
);
996 len
= baselen
+ 6; /* "refs/" + NUL */
997 for (cp
= ref
; (ch
= *cp
) != 0; cp
++, len
++)
999 len
+= 2; /* extra two hex plus replacement % */
1000 qref
= xmalloc(len
);
1001 memcpy(qref
, base
, baselen
);
1002 memcpy(qref
+ baselen
, "refs/", 5);
1003 for (cp
= ref
, dp
= qref
+ baselen
+ 5; (ch
= *cp
) != 0; cp
++) {
1004 if (needs_quote(ch
)) {
1006 *dp
++ = hex((ch
>> 4) & 0xF);
1007 *dp
++ = hex(ch
& 0xF);
1017 int fetch_ref(char *ref
, unsigned char *sha1
)
1021 struct buffer buffer
;
1022 char *base
= alt
->base
;
1023 struct active_request_slot
*slot
;
1026 buffer
.buffer
= hex
;
1029 url
= quote_ref_url(base
, ref
);
1030 slot
= get_active_slot();
1031 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
1032 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1033 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
1034 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1035 if (start_active_slot(slot
)) {
1036 run_active_slot(slot
);
1037 if (slot
->curl_result
!= CURLE_OK
)
1038 return error("Couldn't get %s for %s\n%s",
1039 url
, ref
, curl_errorstr
);
1041 return error("Unable to start request");
1045 get_sha1_hex(hex
, sha1
);
1049 int main(int argc
, char **argv
)
1054 struct active_request_slot
*slot
;
1056 while (arg
< argc
&& argv
[arg
][0] == '-') {
1057 if (argv
[arg
][1] == 't') {
1059 } else if (argv
[arg
][1] == 'c') {
1061 } else if (argv
[arg
][1] == 'a') {
1065 } else if (argv
[arg
][1] == 'v') {
1067 } else if (argv
[arg
][1] == 'w') {
1068 write_ref
= argv
[arg
+ 1];
1070 } else if (!strcmp(argv
[arg
], "--recover")) {
1075 if (argc
< arg
+ 2) {
1076 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
1079 commit_id
= argv
[arg
];
1080 url
= argv
[arg
+ 1];
1082 curl_global_init(CURL_GLOBAL_ALL
);
1084 #ifdef USE_CURL_MULTI
1085 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
1086 if (http_max_requests
!= NULL
)
1087 max_requests
= atoi(http_max_requests
);
1088 if (max_requests
< 1)
1089 max_requests
= DEFAULT_MAX_REQUESTS
;
1091 curlm
= curl_multi_init();
1092 if (curlm
== NULL
) {
1093 fprintf(stderr
, "Error creating curl multi handle.\n");
1097 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
1098 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
1099 no_range_header
= curl_slist_append(no_range_header
, "Range:");
1101 curl_default
= curl_easy_init();
1103 curl_ssl_verify
= getenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
1104 curl_easy_setopt(curl_default
, CURLOPT_SSL_VERIFYPEER
, curl_ssl_verify
);
1105 #if LIBCURL_VERSION_NUM >= 0x070907
1106 curl_easy_setopt(curl_default
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
1109 if ((ssl_cert
= getenv("GIT_SSL_CERT")) != NULL
) {
1110 curl_easy_setopt(curl_default
, CURLOPT_SSLCERT
, ssl_cert
);
1112 #if LIBCURL_VERSION_NUM >= 0x070902
1113 if ((ssl_key
= getenv("GIT_SSL_KEY")) != NULL
) {
1114 curl_easy_setopt(curl_default
, CURLOPT_SSLKEY
, ssl_key
);
1117 #if LIBCURL_VERSION_NUM >= 0x070908
1118 if ((ssl_capath
= getenv("GIT_SSL_CAPATH")) != NULL
) {
1119 curl_easy_setopt(curl_default
, CURLOPT_CAPATH
, ssl_capath
);
1122 if ((ssl_cainfo
= getenv("GIT_SSL_CAINFO")) != NULL
) {
1123 curl_easy_setopt(curl_default
, CURLOPT_CAINFO
, ssl_cainfo
);
1125 curl_easy_setopt(curl_default
, CURLOPT_FAILONERROR
, 1);
1127 alt
= xmalloc(sizeof(*alt
));
1129 alt
->got_indices
= 0;
1132 fetch_alternates(alt
->base
);
1134 if (pull(commit_id
))
1137 curl_slist_free_all(pragma_header
);
1138 curl_slist_free_all(no_pragma_header
);
1139 curl_slist_free_all(no_range_header
);
1140 curl_easy_cleanup(curl_default
);
1141 slot
= active_queue_head
;
1142 while (slot
!= NULL
) {
1143 curl_easy_cleanup(slot
->curl
);
1146 #ifdef USE_CURL_MULTI
1147 curl_multi_cleanup(curlm
);
1149 curl_global_cleanup();