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 #if LIBCURL_VERSION_NUM < 0x070c04
22 #define NO_CURL_EASY_DUPHANDLE
25 #define PREV_BUF_SIZE 4096
26 #define RANGE_HEADER_SIZE 30
28 static int active_requests
= 0;
29 static int data_received
;
32 static int max_requests
= -1;
35 #ifndef NO_CURL_EASY_DUPHANDLE
36 static CURL
*curl_default
;
38 static struct curl_slist
*pragma_header
;
39 static struct curl_slist
*no_pragma_header
;
40 static struct curl_slist
*no_range_header
;
41 static char curl_errorstr
[CURL_ERROR_SIZE
];
47 struct packed_git
*packs
;
48 struct alt_base
*next
;
51 static struct alt_base
*alt
= NULL
;
60 struct transfer_request
62 unsigned char sha1
[20];
63 struct alt_base
*repo
;
65 char filename
[PATH_MAX
];
66 char tmpfile
[PATH_MAX
];
68 enum transfer_state state
;
70 char errorstr
[CURL_ERROR_SIZE
];
72 unsigned char real_sha1
[20];
77 struct active_request_slot
*slot
;
78 struct transfer_request
*next
;
81 struct active_request_slot
88 struct active_request_slot
*next
;
91 static struct transfer_request
*request_queue_head
= NULL
;
92 static struct active_request_slot
*active_queue_head
= NULL
;
94 static int curl_ssl_verify
= -1;
95 static char *ssl_cert
= NULL
;
96 #if LIBCURL_VERSION_NUM >= 0x070902
97 static char *ssl_key
= NULL
;
99 #if LIBCURL_VERSION_NUM >= 0x070908
100 static char *ssl_capath
= NULL
;
102 static char *ssl_cainfo
= NULL
;
111 static int http_options(const char *var
, const char *value
)
113 if (!strcmp("http.sslverify", var
)) {
114 if (curl_ssl_verify
== -1) {
115 curl_ssl_verify
= git_config_bool(var
, value
);
120 if (!strcmp("http.sslcert", var
)) {
121 if (ssl_cert
== NULL
) {
122 ssl_cert
= xmalloc(strlen(value
)+1);
123 strcpy(ssl_cert
, value
);
127 #if LIBCURL_VERSION_NUM >= 0x070902
128 if (!strcmp("http.sslkey", var
)) {
129 if (ssl_key
== NULL
) {
130 ssl_key
= xmalloc(strlen(value
)+1);
131 strcpy(ssl_key
, value
);
136 #if LIBCURL_VERSION_NUM >= 0x070908
137 if (!strcmp("http.sslcapath", var
)) {
138 if (ssl_capath
== NULL
) {
139 ssl_capath
= xmalloc(strlen(value
)+1);
140 strcpy(ssl_capath
, value
);
145 if (!strcmp("http.sslcainfo", var
)) {
146 if (ssl_cainfo
== NULL
) {
147 ssl_cainfo
= xmalloc(strlen(value
)+1);
148 strcpy(ssl_cainfo
, value
);
153 #ifdef USE_CURL_MULTI
154 if (!strcmp("http.maxrequests", var
)) {
155 if (max_requests
== -1)
156 max_requests
= git_config_int(var
, value
);
161 /* Fall back on the default ones */
162 return git_default_config(var
, value
);
165 static size_t fwrite_buffer(void *ptr
, size_t eltsize
, size_t nmemb
,
166 struct buffer
*buffer
)
168 size_t size
= eltsize
* nmemb
;
169 if (size
> buffer
->size
- buffer
->posn
)
170 size
= buffer
->size
- buffer
->posn
;
171 memcpy(buffer
->buffer
+ buffer
->posn
, ptr
, size
);
172 buffer
->posn
+= size
;
177 static size_t fwrite_buffer_dynamic(const void *ptr
, size_t eltsize
,
178 size_t nmemb
, struct buffer
*buffer
)
180 size_t size
= eltsize
* nmemb
;
181 if (size
> buffer
->size
- buffer
->posn
) {
182 buffer
->size
= buffer
->size
* 3 / 2;
183 if (buffer
->size
< buffer
->posn
+ size
)
184 buffer
->size
= buffer
->posn
+ size
;
185 buffer
->buffer
= xrealloc(buffer
->buffer
, buffer
->size
);
187 memcpy(buffer
->buffer
+ buffer
->posn
, ptr
, size
);
188 buffer
->posn
+= size
;
193 static size_t fwrite_sha1_file(void *ptr
, size_t eltsize
, size_t nmemb
,
196 unsigned char expn
[4096];
197 size_t size
= eltsize
* nmemb
;
199 struct transfer_request
*request
= (struct transfer_request
*)data
;
201 ssize_t retval
= write(request
->local
,
202 ptr
+ posn
, size
- posn
);
206 } while (posn
< size
);
208 request
->stream
.avail_in
= size
;
209 request
->stream
.next_in
= ptr
;
211 request
->stream
.next_out
= expn
;
212 request
->stream
.avail_out
= sizeof(expn
);
213 request
->zret
= inflate(&request
->stream
, Z_SYNC_FLUSH
);
214 SHA1_Update(&request
->c
, expn
,
215 sizeof(expn
) - request
->stream
.avail_out
);
216 } while (request
->stream
.avail_in
&& request
->zret
== Z_OK
);
221 #ifdef USE_CURL_MULTI
222 static void process_curl_messages(void);
223 static void process_request_queue(void);
226 static CURL
* get_curl_handle(void)
228 CURL
* result
= curl_easy_init();
230 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, curl_ssl_verify
);
231 #if LIBCURL_VERSION_NUM >= 0x070907
232 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
235 if (ssl_cert
!= NULL
)
236 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
237 #if LIBCURL_VERSION_NUM >= 0x070902
239 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
241 #if LIBCURL_VERSION_NUM >= 0x070908
242 if (ssl_capath
!= NULL
)
243 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
245 if (ssl_cainfo
!= NULL
)
246 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
247 curl_easy_setopt(result
, CURLOPT_FAILONERROR
, 1);
252 static struct active_request_slot
*get_active_slot(void)
254 struct active_request_slot
*slot
= active_queue_head
;
255 struct active_request_slot
*newslot
;
257 #ifdef USE_CURL_MULTI
260 /* Wait for a slot to open up if the queue is full */
261 while (active_requests
>= max_requests
) {
262 curl_multi_perform(curlm
, &num_transfers
);
263 if (num_transfers
< active_requests
) {
264 process_curl_messages();
269 while (slot
!= NULL
&& slot
->in_use
) {
273 newslot
= xmalloc(sizeof(*newslot
));
274 #ifdef NO_CURL_EASY_DUPHANDLE
275 newslot
->curl
= get_curl_handle();
277 newslot
->curl
= curl_easy_duphandle(curl_default
);
280 newslot
->next
= NULL
;
282 slot
= active_queue_head
;
284 active_queue_head
= newslot
;
286 while (slot
->next
!= NULL
) {
289 slot
->next
= newslot
;
298 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
299 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_range_header
);
300 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
305 static int start_active_slot(struct active_request_slot
*slot
)
307 #ifdef USE_CURL_MULTI
308 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
310 if (curlm_result
!= CURLM_OK
&&
311 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
320 static void run_active_slot(struct active_request_slot
*slot
)
322 #ifdef USE_CURL_MULTI
330 struct timeval select_timeout
;
331 CURLMcode curlm_result
;
333 while (!slot
->done
) {
336 curlm_result
= curl_multi_perform(curlm
,
338 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
339 if (num_transfers
< active_requests
) {
340 process_curl_messages();
341 process_request_queue();
344 if (!data_received
&& slot
->local
!= NULL
) {
345 current_pos
= ftell(slot
->local
);
346 if (current_pos
> last_pos
)
348 last_pos
= current_pos
;
351 if (!slot
->done
&& !data_received
) {
356 select_timeout
.tv_sec
= 0;
357 select_timeout
.tv_usec
= 50000;
358 select(max_fd
, &readfds
, &writefds
,
359 &excfds
, &select_timeout
);
363 slot
->curl_result
= curl_easy_perform(slot
->curl
);
368 static void start_request(struct transfer_request
*request
)
370 char *hex
= sha1_to_hex(request
->sha1
);
371 char prevfile
[PATH_MAX
];
375 unsigned char prev_buf
[PREV_BUF_SIZE
];
376 ssize_t prev_read
= 0;
378 char range
[RANGE_HEADER_SIZE
];
379 struct curl_slist
*range_header
= NULL
;
380 struct active_request_slot
*slot
;
382 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", request
->filename
);
384 rename(request
->tmpfile
, prevfile
);
385 unlink(request
->tmpfile
);
387 request
->local
= open(request
->tmpfile
,
388 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
389 /* This could have failed due to the "lazy directory creation";
390 * try to mkdir the last path component.
392 if (request
->local
< 0 && errno
== ENOENT
) {
393 char *dir
= strrchr(request
->tmpfile
, '/');
396 mkdir(request
->tmpfile
, 0777);
399 request
->local
= open(request
->tmpfile
,
400 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
403 if (request
->local
< 0) {
404 request
->state
= ABORTED
;
405 error("Couldn't create temporary file %s for %s: %s\n",
406 request
->tmpfile
, request
->filename
, strerror(errno
));
410 memset(&request
->stream
, 0, sizeof(request
->stream
));
412 inflateInit(&request
->stream
);
414 SHA1_Init(&request
->c
);
416 url
= xmalloc(strlen(request
->repo
->base
) + 50);
417 request
->url
= xmalloc(strlen(request
->repo
->base
) + 50);
418 strcpy(url
, request
->repo
->base
);
419 posn
= url
+ strlen(request
->repo
->base
);
420 strcpy(posn
, "objects/");
422 memcpy(posn
, hex
, 2);
425 strcpy(posn
, hex
+ 2);
426 strcpy(request
->url
, url
);
428 /* If a previous temp file is present, process what was already
430 prevlocal
= open(prevfile
, O_RDONLY
);
431 if (prevlocal
!= -1) {
433 prev_read
= read(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
435 if (fwrite_sha1_file(prev_buf
,
438 request
) == prev_read
) {
439 prev_posn
+= prev_read
;
444 } while (prev_read
> 0);
449 /* Reset inflate/SHA1 if there was an error reading the previous temp
450 file; also rewind to the beginning of the local file. */
451 if (prev_read
== -1) {
452 memset(&request
->stream
, 0, sizeof(request
->stream
));
453 inflateInit(&request
->stream
);
454 SHA1_Init(&request
->c
);
457 lseek(request
->local
, SEEK_SET
, 0);
458 ftruncate(request
->local
, 0);
462 slot
= get_active_slot();
463 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, request
);
464 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
465 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
466 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
467 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
469 /* If we have successfully processed data from a previous fetch
470 attempt, only fetch the data we don't already have. */
474 "Resuming fetch of object %s at byte %ld\n",
476 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
477 range_header
= curl_slist_append(range_header
, range
);
478 curl_easy_setopt(slot
->curl
,
479 CURLOPT_HTTPHEADER
, range_header
);
482 /* Try to get the request started, abort the request on error */
483 if (!start_active_slot(slot
)) {
484 request
->state
= ABORTED
;
485 close(request
->local
);
490 request
->slot
= slot
;
491 request
->state
= ACTIVE
;
494 static void finish_request(struct transfer_request
*request
)
496 fchmod(request
->local
, 0444);
497 close(request
->local
);
499 if (request
->http_code
== 416) {
500 fprintf(stderr
, "Warning: requested range invalid; we may already have all the data.\n");
501 } else if (request
->curl_result
!= CURLE_OK
) {
505 inflateEnd(&request
->stream
);
506 SHA1_Final(request
->real_sha1
, &request
->c
);
507 if (request
->zret
!= Z_STREAM_END
) {
508 unlink(request
->tmpfile
);
511 if (memcmp(request
->sha1
, request
->real_sha1
, 20)) {
512 unlink(request
->tmpfile
);
516 move_temp_to_file(request
->tmpfile
, request
->filename
);
518 if (request
->rename
== 0)
519 pull_say("got %s\n", sha1_to_hex(request
->sha1
));
522 static void release_request(struct transfer_request
*request
)
524 struct transfer_request
*entry
= request_queue_head
;
526 if (request
== request_queue_head
) {
527 request_queue_head
= request
->next
;
529 while (entry
->next
!= NULL
&& entry
->next
!= request
)
531 if (entry
->next
== request
)
532 entry
->next
= entry
->next
->next
;
539 #ifdef USE_CURL_MULTI
540 void process_curl_messages(void)
543 struct active_request_slot
*slot
;
544 struct transfer_request
*request
= NULL
;
545 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
547 while (curl_message
!= NULL
) {
548 if (curl_message
->msg
== CURLMSG_DONE
) {
549 slot
= active_queue_head
;
550 while (slot
!= NULL
&&
551 slot
->curl
!= curl_message
->easy_handle
)
554 curl_multi_remove_handle(curlm
, slot
->curl
);
558 slot
->curl_result
= curl_message
->data
.result
;
559 request
= request_queue_head
;
560 while (request
!= NULL
&&
561 request
->slot
!= slot
)
562 request
= request
->next
;
564 fprintf(stderr
, "Received DONE message for unknown request!\n");
566 if (request
!= NULL
) {
567 request
->curl_result
=
568 curl_message
->data
.result
;
569 curl_easy_getinfo(slot
->curl
,
571 &request
->http_code
);
572 request
->slot
= NULL
;
574 /* Use alternates if necessary */
575 if (request
->http_code
== 404 &&
576 request
->repo
->next
!= NULL
) {
577 request
->repo
= request
->repo
->next
;
578 start_request(request
);
580 finish_request(request
);
581 request
->state
= COMPLETE
;
585 fprintf(stderr
, "Unknown CURL message received: %d\n",
586 (int)curl_message
->msg
);
588 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
592 void process_request_queue(void)
594 struct transfer_request
*request
= request_queue_head
;
597 while (active_requests
< max_requests
&& request
!= NULL
) {
598 if (request
->state
== WAITING
) {
599 if (has_sha1_file(request
->sha1
))
600 release_request(request
);
602 start_request(request
);
603 curl_multi_perform(curlm
, &num_transfers
);
605 request
= request
->next
;
610 void prefetch(unsigned char *sha1
)
612 struct transfer_request
*newreq
;
613 struct transfer_request
*tail
;
614 char *filename
= sha1_file_name(sha1
);
616 newreq
= xmalloc(sizeof(*newreq
));
617 memcpy(newreq
->sha1
, sha1
, 20);
621 newreq
->state
= WAITING
;
622 snprintf(newreq
->filename
, sizeof(newreq
->filename
), "%s", filename
);
623 snprintf(newreq
->tmpfile
, sizeof(newreq
->tmpfile
),
624 "%s.temp", filename
);
627 if (request_queue_head
== NULL
) {
628 request_queue_head
= newreq
;
630 tail
= request_queue_head
;
631 while (tail
->next
!= NULL
) {
636 #ifdef USE_CURL_MULTI
637 process_request_queue();
638 process_curl_messages();
642 static int fetch_index(struct alt_base
*repo
, unsigned char *sha1
)
644 char *hex
= sha1_to_hex(sha1
);
647 char tmpfile
[PATH_MAX
];
649 char range
[RANGE_HEADER_SIZE
];
650 struct curl_slist
*range_header
= NULL
;
653 struct active_request_slot
*slot
;
655 if (has_pack_index(sha1
))
659 fprintf(stderr
, "Getting index for pack %s\n", hex
);
661 url
= xmalloc(strlen(repo
->base
) + 64);
662 sprintf(url
, "%s/objects/pack/pack-%s.idx", repo
->base
, hex
);
664 filename
= sha1_pack_index_name(sha1
);
665 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
666 indexfile
= fopen(tmpfile
, "a");
668 return error("Unable to open local file %s for pack index",
671 slot
= get_active_slot();
672 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
673 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
674 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
675 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
676 slot
->local
= indexfile
;
678 /* If there is data present from a previous transfer attempt,
679 resume where it left off */
680 prev_posn
= ftell(indexfile
);
684 "Resuming fetch of index for pack %s at byte %ld\n",
686 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
687 range_header
= curl_slist_append(range_header
, range
);
688 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
691 if (start_active_slot(slot
)) {
692 run_active_slot(slot
);
693 if (slot
->curl_result
!= CURLE_OK
) {
695 return error("Unable to get pack index %s\n%s", url
,
699 return error("Unable to start request");
704 return move_temp_to_file(tmpfile
, filename
);
707 static int setup_index(struct alt_base
*repo
, unsigned char *sha1
)
709 struct packed_git
*new_pack
;
710 if (has_pack_file(sha1
))
711 return 0; // don't list this as something we can get
713 if (fetch_index(repo
, sha1
))
716 new_pack
= parse_pack_index(sha1
);
717 new_pack
->next
= repo
->packs
;
718 repo
->packs
= new_pack
;
722 static int fetch_alternates(char *base
)
725 struct buffer buffer
;
729 int http_specific
= 1;
730 struct alt_base
*tail
= alt
;
731 static const char null_byte
= '\0';
733 struct active_request_slot
*slot
;
735 data
= xmalloc(4096);
738 buffer
.buffer
= data
;
741 fprintf(stderr
, "Getting alternates list\n");
743 url
= xmalloc(strlen(base
) + 31);
744 sprintf(url
, "%s/objects/info/http-alternates", base
);
746 slot
= get_active_slot();
747 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
748 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
749 fwrite_buffer_dynamic
);
750 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
751 if (start_active_slot(slot
)) {
752 run_active_slot(slot
);
753 if (slot
->curl_result
!= CURLE_OK
|| !buffer
.posn
) {
756 sprintf(url
, "%s/objects/info/alternates", base
);
758 slot
= get_active_slot();
759 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
760 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
761 fwrite_buffer_dynamic
);
762 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
763 if (start_active_slot(slot
)) {
764 run_active_slot(slot
);
765 if (slot
->curl_result
!= CURLE_OK
) {
776 fwrite_buffer_dynamic(&null_byte
, 1, 1, &buffer
);
778 data
= buffer
.buffer
;
780 while (i
< buffer
.posn
) {
782 while (posn
< buffer
.posn
&& data
[posn
] != '\n')
784 if (data
[posn
] == '\n') {
787 struct alt_base
*newalt
;
789 if (data
[i
] == '/') {
790 serverlen
= strchr(base
+ 8, '/') - base
;
792 } else if (!memcmp(data
+ i
, "../", 3)) {
794 serverlen
= strlen(base
);
795 while (i
+ 2 < posn
&&
796 !memcmp(data
+ i
, "../", 3)) {
799 } while (serverlen
&&
800 base
[serverlen
- 1] != '/');
803 // If the server got removed, give up.
804 okay
= strchr(base
, ':') - base
+ 3 <
806 } else if (http_specific
) {
807 char *colon
= strchr(data
+ i
, ':');
808 char *slash
= strchr(data
+ i
, '/');
809 if (colon
&& slash
&& colon
< data
+ posn
&&
810 slash
< data
+ posn
&& colon
< slash
) {
814 // skip 'objects' at end
816 target
= xmalloc(serverlen
+ posn
- i
- 6);
817 strncpy(target
, base
, serverlen
);
818 strncpy(target
+ serverlen
, data
+ i
,
820 target
[serverlen
+ posn
- i
- 7] = '\0';
823 "Also look at %s\n", target
);
824 newalt
= xmalloc(sizeof(*newalt
));
826 newalt
->base
= target
;
827 newalt
->got_indices
= 0;
828 newalt
->packs
= NULL
;
829 while (tail
->next
!= NULL
)
842 static int fetch_indices(struct alt_base
*repo
)
844 unsigned char sha1
[20];
846 struct buffer buffer
;
850 struct active_request_slot
*slot
;
852 if (repo
->got_indices
)
855 data
= xmalloc(4096);
858 buffer
.buffer
= data
;
861 fprintf(stderr
, "Getting pack list\n");
863 url
= xmalloc(strlen(repo
->base
) + 21);
864 sprintf(url
, "%s/objects/info/packs", repo
->base
);
866 slot
= get_active_slot();
867 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
868 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
869 fwrite_buffer_dynamic
);
870 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
871 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
872 if (start_active_slot(slot
)) {
873 run_active_slot(slot
);
874 if (slot
->curl_result
!= CURLE_OK
) {
876 return error("%s", curl_errorstr
);
880 return error("Unable to start request");
883 data
= buffer
.buffer
;
884 while (i
< buffer
.posn
) {
888 if (i
+ 52 < buffer
.posn
&&
889 !strncmp(data
+ i
, " pack-", 6) &&
890 !strncmp(data
+ i
+ 46, ".pack\n", 6)) {
891 get_sha1_hex(data
+ i
+ 6, sha1
);
892 setup_index(repo
, sha1
);
897 while (data
[i
] != '\n')
904 repo
->got_indices
= 1;
908 static int fetch_pack(struct alt_base
*repo
, unsigned char *sha1
)
911 struct packed_git
*target
;
912 struct packed_git
**lst
;
915 char tmpfile
[PATH_MAX
];
918 char range
[RANGE_HEADER_SIZE
];
919 struct curl_slist
*range_header
= NULL
;
921 struct active_request_slot
*slot
;
923 if (fetch_indices(repo
))
925 target
= find_sha1_pack(sha1
, repo
->packs
);
930 fprintf(stderr
, "Getting pack %s\n",
931 sha1_to_hex(target
->sha1
));
932 fprintf(stderr
, " which contains %s\n",
936 url
= xmalloc(strlen(repo
->base
) + 65);
937 sprintf(url
, "%s/objects/pack/pack-%s.pack",
938 repo
->base
, sha1_to_hex(target
->sha1
));
940 filename
= sha1_pack_name(target
->sha1
);
941 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
942 packfile
= fopen(tmpfile
, "a");
944 return error("Unable to open local file %s for pack",
947 slot
= get_active_slot();
948 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, packfile
);
949 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
950 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
951 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
952 slot
->local
= packfile
;
954 /* If there is data present from a previous transfer attempt,
955 resume where it left off */
956 prev_posn
= ftell(packfile
);
960 "Resuming fetch of pack %s at byte %ld\n",
961 sha1_to_hex(target
->sha1
), prev_posn
);
962 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
963 range_header
= curl_slist_append(range_header
, range
);
964 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
967 if (start_active_slot(slot
)) {
968 run_active_slot(slot
);
969 if (slot
->curl_result
!= CURLE_OK
) {
971 return error("Unable to get pack file %s\n%s", url
,
975 return error("Unable to start request");
980 ret
= move_temp_to_file(tmpfile
, filename
);
985 while (*lst
!= target
)
986 lst
= &((*lst
)->next
);
989 if (verify_pack(target
, 0))
991 install_packed_git(target
);
996 static int fetch_object(struct alt_base
*repo
, unsigned char *sha1
)
998 char *hex
= sha1_to_hex(sha1
);
1000 struct transfer_request
*request
= request_queue_head
;
1002 while (request
!= NULL
&& memcmp(request
->sha1
, sha1
, 20))
1003 request
= request
->next
;
1004 if (request
== NULL
)
1005 return error("Couldn't find request for %s in the queue", hex
);
1007 if (has_sha1_file(request
->sha1
)) {
1008 release_request(request
);
1012 #ifdef USE_CURL_MULTI
1013 while (request
->state
== WAITING
) {
1015 curl_multi_perform(curlm
, &num_transfers
);
1016 if (num_transfers
< active_requests
) {
1017 process_curl_messages();
1018 process_request_queue();
1022 start_request(request
);
1025 while (request
->state
== ACTIVE
) {
1026 run_active_slot(request
->slot
);
1027 #ifndef USE_CURL_MULTI
1028 request
->curl_result
= request
->slot
->curl_result
;
1029 curl_easy_getinfo(request
->slot
->curl
,
1031 &request
->http_code
);
1032 request
->slot
= NULL
;
1034 /* Use alternates if necessary */
1035 if (request
->http_code
== 404 &&
1036 request
->repo
->next
!= NULL
) {
1037 request
->repo
= request
->repo
->next
;
1038 start_request(request
);
1040 finish_request(request
);
1041 request
->state
= COMPLETE
;
1046 if (request
->state
== ABORTED
) {
1047 release_request(request
);
1048 return error("Request for %s aborted", hex
);
1051 if (request
->curl_result
!= CURLE_OK
&& request
->http_code
!= 416) {
1052 ret
= error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
1053 request
->errorstr
, request
->curl_result
,
1054 request
->http_code
, hex
);
1055 release_request(request
);
1059 if (request
->zret
!= Z_STREAM_END
) {
1060 ret
= error("File %s (%s) corrupt\n", hex
, request
->url
);
1061 release_request(request
);
1065 if (memcmp(request
->sha1
, request
->real_sha1
, 20)) {
1066 release_request(request
);
1067 return error("File %s has bad hash\n", hex
);
1070 if (request
->rename
< 0) {
1071 ret
= error("unable to write sha1 filename %s: %s",
1073 strerror(request
->rename
));
1074 release_request(request
);
1078 release_request(request
);
1082 int fetch(unsigned char *sha1
)
1084 struct alt_base
*altbase
= alt
;
1086 if (!fetch_object(altbase
, sha1
))
1089 if (!fetch_pack(altbase
, sha1
))
1091 altbase
= altbase
->next
;
1093 return error("Unable to find %s under %s\n", sha1_to_hex(sha1
),
1097 static inline int needs_quote(int ch
)
1100 case '/': case '-': case '.':
1101 case 'A'...'Z': case 'a'...'z': case '0'...'9':
1108 static inline int hex(int v
)
1110 if (v
< 10) return '0' + v
;
1111 else return 'A' + v
- 10;
1114 static char *quote_ref_url(const char *base
, const char *ref
)
1118 int len
, baselen
, ch
;
1120 baselen
= strlen(base
);
1121 len
= baselen
+ 6; /* "refs/" + NUL */
1122 for (cp
= ref
; (ch
= *cp
) != 0; cp
++, len
++)
1123 if (needs_quote(ch
))
1124 len
+= 2; /* extra two hex plus replacement % */
1125 qref
= xmalloc(len
);
1126 memcpy(qref
, base
, baselen
);
1127 memcpy(qref
+ baselen
, "refs/", 5);
1128 for (cp
= ref
, dp
= qref
+ baselen
+ 5; (ch
= *cp
) != 0; cp
++) {
1129 if (needs_quote(ch
)) {
1131 *dp
++ = hex((ch
>> 4) & 0xF);
1132 *dp
++ = hex(ch
& 0xF);
1142 int fetch_ref(char *ref
, unsigned char *sha1
)
1146 struct buffer buffer
;
1147 char *base
= alt
->base
;
1148 struct active_request_slot
*slot
;
1151 buffer
.buffer
= hex
;
1154 url
= quote_ref_url(base
, ref
);
1155 slot
= get_active_slot();
1156 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
1157 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1158 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
1159 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1160 if (start_active_slot(slot
)) {
1161 run_active_slot(slot
);
1162 if (slot
->curl_result
!= CURLE_OK
)
1163 return error("Couldn't get %s for %s\n%s",
1164 url
, ref
, curl_errorstr
);
1166 return error("Unable to start request");
1170 get_sha1_hex(hex
, sha1
);
1174 int main(int argc
, char **argv
)
1179 struct active_request_slot
*slot
;
1181 while (arg
< argc
&& argv
[arg
][0] == '-') {
1182 if (argv
[arg
][1] == 't') {
1184 } else if (argv
[arg
][1] == 'c') {
1186 } else if (argv
[arg
][1] == 'a') {
1190 } else if (argv
[arg
][1] == 'v') {
1192 } else if (argv
[arg
][1] == 'w') {
1193 write_ref
= argv
[arg
+ 1];
1195 } else if (!strcmp(argv
[arg
], "--recover")) {
1200 if (argc
< arg
+ 2) {
1201 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
1204 commit_id
= argv
[arg
];
1205 url
= argv
[arg
+ 1];
1207 curl_global_init(CURL_GLOBAL_ALL
);
1209 #ifdef USE_CURL_MULTI
1211 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
1212 if (http_max_requests
!= NULL
)
1213 max_requests
= atoi(http_max_requests
);
1216 curlm
= curl_multi_init();
1217 if (curlm
== NULL
) {
1218 fprintf(stderr
, "Error creating curl multi handle.\n");
1223 if (getenv("GIT_SSL_NO_VERIFY"))
1224 curl_ssl_verify
= 0;
1226 ssl_cert
= getenv("GIT_SSL_CERT");
1227 #if LIBCURL_VERSION_NUM >= 0x070902
1228 ssl_key
= getenv("GIT_SSL_KEY");
1230 #if LIBCURL_VERSION_NUM >= 0x070908
1231 ssl_capath
= getenv("GIT_SSL_CAPATH");
1233 ssl_cainfo
= getenv("GIT_SSL_CAINFO");
1235 git_config(http_options
);
1237 if (curl_ssl_verify
== -1)
1238 curl_ssl_verify
= 1;
1240 #ifdef USE_CURL_MULTI
1241 if (max_requests
< 1)
1242 max_requests
= DEFAULT_MAX_REQUESTS
;
1245 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
1246 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
1247 no_range_header
= curl_slist_append(no_range_header
, "Range:");
1249 #ifndef NO_CURL_EASY_DUPHANDLE
1250 curl_default
= get_curl_handle();
1253 alt
= xmalloc(sizeof(*alt
));
1255 alt
->got_indices
= 0;
1258 fetch_alternates(alt
->base
);
1260 if (pull(commit_id
))
1263 curl_slist_free_all(pragma_header
);
1264 curl_slist_free_all(no_pragma_header
);
1265 curl_slist_free_all(no_range_header
);
1266 #ifndef NO_CURL_EASY_DUPHANDLE
1267 curl_easy_cleanup(curl_default
);
1269 slot
= active_queue_head
;
1270 while (slot
!= NULL
) {
1271 curl_easy_cleanup(slot
->curl
);
1274 #ifdef USE_CURL_MULTI
1275 curl_multi_cleanup(curlm
);
1277 curl_global_cleanup();