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_buffer_dynamic(const void *ptr
, size_t eltsize
,
114 size_t nmemb
, struct buffer
*buffer
)
116 size_t size
= eltsize
* nmemb
;
117 if (size
> buffer
->size
- buffer
->posn
) {
118 buffer
->size
= buffer
->size
* 3 / 2;
119 if (buffer
->size
< buffer
->posn
+ size
)
120 buffer
->size
= buffer
->posn
+ size
;
121 buffer
->buffer
= xrealloc(buffer
->buffer
, buffer
->size
);
123 memcpy(buffer
->buffer
+ buffer
->posn
, ptr
, size
);
124 buffer
->posn
+= size
;
129 static size_t fwrite_sha1_file(void *ptr
, size_t eltsize
, size_t nmemb
,
132 unsigned char expn
[4096];
133 size_t size
= eltsize
* nmemb
;
135 struct transfer_request
*request
= (struct transfer_request
*)data
;
137 ssize_t retval
= write(request
->local
,
138 ptr
+ posn
, size
- posn
);
142 } while (posn
< size
);
144 request
->stream
.avail_in
= size
;
145 request
->stream
.next_in
= ptr
;
147 request
->stream
.next_out
= expn
;
148 request
->stream
.avail_out
= sizeof(expn
);
149 request
->zret
= inflate(&request
->stream
, Z_SYNC_FLUSH
);
150 SHA1_Update(&request
->c
, expn
,
151 sizeof(expn
) - request
->stream
.avail_out
);
152 } while (request
->stream
.avail_in
&& request
->zret
== Z_OK
);
157 #ifdef USE_CURL_MULTI
158 void process_curl_messages();
159 void process_request_queue();
162 struct active_request_slot
*get_active_slot()
164 struct active_request_slot
*slot
= active_queue_head
;
165 struct active_request_slot
*newslot
;
167 #ifdef USE_CURL_MULTI
170 /* Wait for a slot to open up if the queue is full */
171 while (active_requests
>= max_requests
) {
172 curl_multi_perform(curlm
, &num_transfers
);
173 if (num_transfers
< active_requests
) {
174 process_curl_messages();
179 while (slot
!= NULL
&& slot
->in_use
) {
183 newslot
= xmalloc(sizeof(*newslot
));
184 newslot
->curl
= curl_easy_duphandle(curl_default
);
186 newslot
->next
= NULL
;
188 slot
= active_queue_head
;
190 active_queue_head
= newslot
;
192 while (slot
->next
!= NULL
) {
195 slot
->next
= newslot
;
204 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
205 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_range_header
);
206 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
211 int start_active_slot(struct active_request_slot
*slot
)
213 #ifdef USE_CURL_MULTI
214 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
216 if (curlm_result
!= CURLM_OK
&&
217 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
226 void run_active_slot(struct active_request_slot
*slot
)
228 #ifdef USE_CURL_MULTI
236 struct timeval select_timeout
;
237 CURLMcode curlm_result
;
239 while (!slot
->done
) {
242 curlm_result
= curl_multi_perform(curlm
,
244 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
245 if (num_transfers
< active_requests
) {
246 process_curl_messages();
247 process_request_queue();
250 if (!data_received
&& slot
->local
!= NULL
) {
251 current_pos
= ftell(slot
->local
);
252 if (current_pos
> last_pos
)
254 last_pos
= current_pos
;
257 if (!slot
->done
&& !data_received
) {
262 select_timeout
.tv_sec
= 0;
263 select_timeout
.tv_usec
= 50000;
264 select(max_fd
, &readfds
, &writefds
,
265 &excfds
, &select_timeout
);
269 slot
->curl_result
= curl_easy_perform(slot
->curl
);
274 void start_request(struct transfer_request
*request
)
276 char *hex
= sha1_to_hex(request
->sha1
);
277 char prevfile
[PATH_MAX
];
281 unsigned char prev_buf
[PREV_BUF_SIZE
];
282 ssize_t prev_read
= 0;
284 char range
[RANGE_HEADER_SIZE
];
285 struct curl_slist
*range_header
= NULL
;
286 struct active_request_slot
*slot
;
288 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", request
->filename
);
290 rename(request
->tmpfile
, prevfile
);
291 unlink(request
->tmpfile
);
293 request
->local
= open(request
->tmpfile
,
294 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
295 /* This could have failed due to the "lazy directory creation";
296 * try to mkdir the last path component.
298 if (request
->local
< 0 && errno
== ENOENT
) {
299 char *dir
= strrchr(request
->tmpfile
, '/');
302 mkdir(request
->tmpfile
, 0777);
305 request
->local
= open(request
->tmpfile
,
306 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
309 if (request
->local
< 0) {
310 request
->state
= ABORTED
;
311 error("Couldn't create temporary file %s for %s: %s\n",
312 request
->tmpfile
, request
->filename
, strerror(errno
));
316 memset(&request
->stream
, 0, sizeof(request
->stream
));
318 inflateInit(&request
->stream
);
320 SHA1_Init(&request
->c
);
322 url
= xmalloc(strlen(request
->repo
->base
) + 50);
323 request
->url
= xmalloc(strlen(request
->repo
->base
) + 50);
324 strcpy(url
, request
->repo
->base
);
325 posn
= url
+ strlen(request
->repo
->base
);
326 strcpy(posn
, "objects/");
328 memcpy(posn
, hex
, 2);
331 strcpy(posn
, hex
+ 2);
332 strcpy(request
->url
, url
);
334 /* If a previous temp file is present, process what was already
336 prevlocal
= open(prevfile
, O_RDONLY
);
337 if (prevlocal
!= -1) {
339 prev_read
= read(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
341 if (fwrite_sha1_file(prev_buf
,
344 request
) == prev_read
) {
345 prev_posn
+= prev_read
;
350 } while (prev_read
> 0);
355 /* Reset inflate/SHA1 if there was an error reading the previous temp
356 file; also rewind to the beginning of the local file. */
357 if (prev_read
== -1) {
358 memset(&request
->stream
, 0, sizeof(request
->stream
));
359 inflateInit(&request
->stream
);
360 SHA1_Init(&request
->c
);
363 lseek(request
->local
, SEEK_SET
, 0);
364 ftruncate(request
->local
, 0);
368 slot
= get_active_slot();
369 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, request
);
370 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
371 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
372 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
373 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
375 /* If we have successfully processed data from a previous fetch
376 attempt, only fetch the data we don't already have. */
380 "Resuming fetch of object %s at byte %ld\n",
382 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
383 range_header
= curl_slist_append(range_header
, range
);
384 curl_easy_setopt(slot
->curl
,
385 CURLOPT_HTTPHEADER
, range_header
);
388 /* Try to get the request started, abort the request on error */
389 if (!start_active_slot(slot
)) {
390 request
->state
= ABORTED
;
391 close(request
->local
);
396 request
->slot
= slot
;
397 request
->state
= ACTIVE
;
400 void finish_request(struct transfer_request
*request
)
402 fchmod(request
->local
, 0444);
403 close(request
->local
);
405 if (request
->http_code
== 416) {
406 fprintf(stderr
, "Warning: requested range invalid; we may already have all the data.\n");
407 } else if (request
->curl_result
!= CURLE_OK
) {
411 inflateEnd(&request
->stream
);
412 SHA1_Final(request
->real_sha1
, &request
->c
);
413 if (request
->zret
!= Z_STREAM_END
) {
414 unlink(request
->tmpfile
);
417 if (memcmp(request
->sha1
, request
->real_sha1
, 20)) {
418 unlink(request
->tmpfile
);
422 move_temp_to_file(request
->tmpfile
, request
->filename
);
424 if (request
->rename
== 0)
425 pull_say("got %s\n", sha1_to_hex(request
->sha1
));
428 void release_request(struct transfer_request
*request
)
430 struct transfer_request
*entry
= request_queue_head
;
432 if (request
== request_queue_head
) {
433 request_queue_head
= request
->next
;
435 while (entry
->next
!= NULL
&& entry
->next
!= request
)
437 if (entry
->next
== request
)
438 entry
->next
= entry
->next
->next
;
445 #ifdef USE_CURL_MULTI
446 void process_curl_messages()
449 struct active_request_slot
*slot
;
450 struct transfer_request
*request
= NULL
;
451 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
453 while (curl_message
!= NULL
) {
454 if (curl_message
->msg
== CURLMSG_DONE
) {
455 slot
= active_queue_head
;
456 while (slot
!= NULL
&&
457 slot
->curl
!= curl_message
->easy_handle
)
460 curl_multi_remove_handle(curlm
, slot
->curl
);
464 slot
->curl_result
= curl_message
->data
.result
;
465 request
= request_queue_head
;
466 while (request
!= NULL
&&
467 request
->slot
!= slot
)
468 request
= request
->next
;
470 fprintf(stderr
, "Received DONE message for unknown request!\n");
472 if (request
!= NULL
) {
473 request
->curl_result
=
474 curl_message
->data
.result
;
475 curl_easy_getinfo(slot
->curl
,
477 &request
->http_code
);
478 request
->slot
= NULL
;
480 /* Use alternates if necessary */
481 if (request
->http_code
== 404 &&
482 request
->repo
->next
!= NULL
) {
483 request
->repo
= request
->repo
->next
;
484 start_request(request
);
486 finish_request(request
);
487 request
->state
= COMPLETE
;
491 fprintf(stderr
, "Unknown CURL message received: %d\n",
492 (int)curl_message
->msg
);
494 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
498 void process_request_queue()
500 struct transfer_request
*request
= request_queue_head
;
503 while (active_requests
< max_requests
&& request
!= NULL
) {
504 if (request
->state
== WAITING
) {
505 if (has_sha1_file(request
->sha1
))
506 release_request(request
);
508 start_request(request
);
509 curl_multi_perform(curlm
, &num_transfers
);
511 request
= request
->next
;
516 void prefetch(unsigned char *sha1
)
518 struct transfer_request
*newreq
;
519 struct transfer_request
*tail
;
520 char *filename
= sha1_file_name(sha1
);
522 newreq
= xmalloc(sizeof(*newreq
));
523 memcpy(newreq
->sha1
, sha1
, 20);
527 newreq
->state
= WAITING
;
528 snprintf(newreq
->filename
, sizeof(newreq
->filename
), "%s", filename
);
529 snprintf(newreq
->tmpfile
, sizeof(newreq
->tmpfile
),
530 "%s.temp", filename
);
533 if (request_queue_head
== NULL
) {
534 request_queue_head
= newreq
;
536 tail
= request_queue_head
;
537 while (tail
->next
!= NULL
) {
542 #ifdef USE_CURL_MULTI
543 process_request_queue();
544 process_curl_messages();
548 static int fetch_index(struct alt_base
*repo
, unsigned char *sha1
)
550 char *hex
= sha1_to_hex(sha1
);
553 char tmpfile
[PATH_MAX
];
555 char range
[RANGE_HEADER_SIZE
];
556 struct curl_slist
*range_header
= NULL
;
559 struct active_request_slot
*slot
;
561 if (has_pack_index(sha1
))
565 fprintf(stderr
, "Getting index for pack %s\n", hex
);
567 url
= xmalloc(strlen(repo
->base
) + 64);
568 sprintf(url
, "%s/objects/pack/pack-%s.idx", repo
->base
, hex
);
570 filename
= sha1_pack_index_name(sha1
);
571 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
572 indexfile
= fopen(tmpfile
, "a");
574 return error("Unable to open local file %s for pack index",
577 slot
= get_active_slot();
578 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
579 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
580 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
581 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
582 slot
->local
= indexfile
;
584 /* If there is data present from a previous transfer attempt,
585 resume where it left off */
586 prev_posn
= ftell(indexfile
);
590 "Resuming fetch of index for pack %s at byte %ld\n",
592 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
593 range_header
= curl_slist_append(range_header
, range
);
594 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
597 if (start_active_slot(slot
)) {
598 run_active_slot(slot
);
599 if (slot
->curl_result
!= CURLE_OK
) {
601 return error("Unable to get pack index %s\n%s", url
,
605 return error("Unable to start request");
610 return move_temp_to_file(tmpfile
, filename
);
613 static int setup_index(struct alt_base
*repo
, unsigned char *sha1
)
615 struct packed_git
*new_pack
;
616 if (has_pack_file(sha1
))
617 return 0; // don't list this as something we can get
619 if (fetch_index(repo
, sha1
))
622 new_pack
= parse_pack_index(sha1
);
623 new_pack
->next
= repo
->packs
;
624 repo
->packs
= new_pack
;
628 static int fetch_alternates(char *base
)
631 struct buffer buffer
;
635 int http_specific
= 1;
636 struct alt_base
*tail
= alt
;
637 static const char null_byte
= '\0';
639 struct active_request_slot
*slot
;
641 data
= xmalloc(4096);
644 buffer
.buffer
= data
;
647 fprintf(stderr
, "Getting alternates list\n");
649 url
= xmalloc(strlen(base
) + 31);
650 sprintf(url
, "%s/objects/info/http-alternates", base
);
652 slot
= get_active_slot();
653 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
654 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
655 fwrite_buffer_dynamic
);
656 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
657 if (start_active_slot(slot
)) {
658 run_active_slot(slot
);
659 if (slot
->curl_result
!= CURLE_OK
|| !buffer
.posn
) {
662 sprintf(url
, "%s/objects/info/alternates", base
);
664 slot
= get_active_slot();
665 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
666 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
667 fwrite_buffer_dynamic
);
668 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
669 if (start_active_slot(slot
)) {
670 run_active_slot(slot
);
671 if (slot
->curl_result
!= CURLE_OK
) {
682 fwrite_buffer_dynamic(&null_byte
, 1, 1, &buffer
);
684 data
= buffer
.buffer
;
686 while (i
< buffer
.posn
) {
688 while (posn
< buffer
.posn
&& data
[posn
] != '\n')
690 if (data
[posn
] == '\n') {
693 struct alt_base
*newalt
;
695 if (data
[i
] == '/') {
696 serverlen
= strchr(base
+ 8, '/') - base
;
698 } else if (!memcmp(data
+ i
, "../", 3)) {
700 serverlen
= strlen(base
);
701 while (i
+ 2 < posn
&&
702 !memcmp(data
+ i
, "../", 3)) {
705 } while (serverlen
&&
706 base
[serverlen
- 1] != '/');
709 // If the server got removed, give up.
710 okay
= strchr(base
, ':') - base
+ 3 <
712 } else if (http_specific
) {
713 char *colon
= strchr(data
+ i
, ':');
714 char *slash
= strchr(data
+ i
, '/');
715 if (colon
&& slash
&& colon
< data
+ posn
&&
716 slash
< data
+ posn
&& colon
< slash
) {
720 // skip 'objects' at end
722 target
= xmalloc(serverlen
+ posn
- i
- 6);
723 strncpy(target
, base
, serverlen
);
724 strncpy(target
+ serverlen
, data
+ i
,
726 target
[serverlen
+ posn
- i
- 7] = '\0';
729 "Also look at %s\n", target
);
730 newalt
= xmalloc(sizeof(*newalt
));
732 newalt
->base
= target
;
733 newalt
->got_indices
= 0;
734 newalt
->packs
= NULL
;
735 while (tail
->next
!= NULL
)
748 static int fetch_indices(struct alt_base
*repo
)
750 unsigned char sha1
[20];
752 struct buffer buffer
;
756 struct active_request_slot
*slot
;
758 if (repo
->got_indices
)
761 data
= xmalloc(4096);
764 buffer
.buffer
= data
;
767 fprintf(stderr
, "Getting pack list\n");
769 url
= xmalloc(strlen(repo
->base
) + 21);
770 sprintf(url
, "%s/objects/info/packs", repo
->base
);
772 slot
= get_active_slot();
773 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
774 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
775 fwrite_buffer_dynamic
);
776 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
777 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
778 if (start_active_slot(slot
)) {
779 run_active_slot(slot
);
780 if (slot
->curl_result
!= CURLE_OK
) {
782 return error("%s", curl_errorstr
);
786 return error("Unable to start request");
789 data
= buffer
.buffer
;
790 while (i
< buffer
.posn
) {
794 if (i
+ 52 < buffer
.posn
&&
795 !strncmp(data
+ i
, " pack-", 6) &&
796 !strncmp(data
+ i
+ 46, ".pack\n", 6)) {
797 get_sha1_hex(data
+ i
+ 6, sha1
);
798 setup_index(repo
, sha1
);
803 while (data
[i
] != '\n')
810 repo
->got_indices
= 1;
814 static int fetch_pack(struct alt_base
*repo
, unsigned char *sha1
)
817 struct packed_git
*target
;
818 struct packed_git
**lst
;
821 char tmpfile
[PATH_MAX
];
824 char range
[RANGE_HEADER_SIZE
];
825 struct curl_slist
*range_header
= NULL
;
827 struct active_request_slot
*slot
;
829 if (fetch_indices(repo
))
831 target
= find_sha1_pack(sha1
, repo
->packs
);
836 fprintf(stderr
, "Getting pack %s\n",
837 sha1_to_hex(target
->sha1
));
838 fprintf(stderr
, " which contains %s\n",
842 url
= xmalloc(strlen(repo
->base
) + 65);
843 sprintf(url
, "%s/objects/pack/pack-%s.pack",
844 repo
->base
, sha1_to_hex(target
->sha1
));
846 filename
= sha1_pack_name(target
->sha1
);
847 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
848 packfile
= fopen(tmpfile
, "a");
850 return error("Unable to open local file %s for pack",
853 slot
= get_active_slot();
854 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, packfile
);
855 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
856 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
857 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
858 slot
->local
= packfile
;
860 /* If there is data present from a previous transfer attempt,
861 resume where it left off */
862 prev_posn
= ftell(packfile
);
866 "Resuming fetch of pack %s at byte %ld\n",
867 sha1_to_hex(target
->sha1
), prev_posn
);
868 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
869 range_header
= curl_slist_append(range_header
, range
);
870 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
873 if (start_active_slot(slot
)) {
874 run_active_slot(slot
);
875 if (slot
->curl_result
!= CURLE_OK
) {
877 return error("Unable to get pack file %s\n%s", url
,
881 return error("Unable to start request");
886 ret
= move_temp_to_file(tmpfile
, filename
);
891 while (*lst
!= target
)
892 lst
= &((*lst
)->next
);
895 if (verify_pack(target
, 0))
897 install_packed_git(target
);
902 static int fetch_object(struct alt_base
*repo
, unsigned char *sha1
)
904 char *hex
= sha1_to_hex(sha1
);
906 struct transfer_request
*request
= request_queue_head
;
908 while (request
!= NULL
&& memcmp(request
->sha1
, sha1
, 20))
909 request
= request
->next
;
911 return error("Couldn't find request for %s in the queue", hex
);
913 if (has_sha1_file(request
->sha1
)) {
914 release_request(request
);
918 #ifdef USE_CURL_MULTI
920 while (request
->state
== WAITING
) {
921 curl_multi_perform(curlm
, &num_transfers
);
922 if (num_transfers
< active_requests
) {
923 process_curl_messages();
924 process_request_queue();
928 start_request(request
);
931 while (request
->state
== ACTIVE
) {
932 run_active_slot(request
->slot
);
933 #ifndef USE_CURL_MULTI
934 request
->curl_result
= request
->slot
->curl_result
;
935 curl_easy_getinfo(request
->slot
->curl
,
937 &request
->http_code
);
938 request
->slot
= NULL
;
940 /* Use alternates if necessary */
941 if (request
->http_code
== 404 &&
942 request
->repo
->next
!= NULL
) {
943 request
->repo
= request
->repo
->next
;
944 start_request(request
);
946 finish_request(request
);
947 request
->state
= COMPLETE
;
952 if (request
->state
== ABORTED
) {
953 release_request(request
);
954 return error("Request for %s aborted", hex
);
957 if (request
->curl_result
!= CURLE_OK
&& request
->http_code
!= 416) {
958 ret
= error("%s", request
->errorstr
);
959 release_request(request
);
963 if (request
->zret
!= Z_STREAM_END
) {
964 ret
= error("File %s (%s) corrupt\n", hex
, request
->url
);
965 release_request(request
);
969 if (memcmp(request
->sha1
, request
->real_sha1
, 20)) {
970 release_request(request
);
971 return error("File %s has bad hash\n", hex
);
974 if (request
->rename
< 0) {
975 ret
= error("unable to write sha1 filename %s: %s",
977 strerror(request
->rename
));
978 release_request(request
);
982 release_request(request
);
986 int fetch(unsigned char *sha1
)
988 struct alt_base
*altbase
= alt
;
990 if (!fetch_object(altbase
, sha1
))
993 if (!fetch_pack(altbase
, sha1
))
995 altbase
= altbase
->next
;
997 return error("Unable to find %s under %s\n", sha1_to_hex(sha1
),
1001 static inline int needs_quote(int ch
)
1004 case '/': case '-': case '.':
1005 case 'A'...'Z': case 'a'...'z': case '0'...'9':
1012 static inline int hex(int v
)
1014 if (v
< 10) return '0' + v
;
1015 else return 'A' + v
- 10;
1018 static char *quote_ref_url(const char *base
, const char *ref
)
1022 int len
, baselen
, ch
;
1024 baselen
= strlen(base
);
1025 len
= baselen
+ 6; /* "refs/" + NUL */
1026 for (cp
= ref
; (ch
= *cp
) != 0; cp
++, len
++)
1027 if (needs_quote(ch
))
1028 len
+= 2; /* extra two hex plus replacement % */
1029 qref
= xmalloc(len
);
1030 memcpy(qref
, base
, baselen
);
1031 memcpy(qref
+ baselen
, "refs/", 5);
1032 for (cp
= ref
, dp
= qref
+ baselen
+ 5; (ch
= *cp
) != 0; cp
++) {
1033 if (needs_quote(ch
)) {
1035 *dp
++ = hex((ch
>> 4) & 0xF);
1036 *dp
++ = hex(ch
& 0xF);
1046 int fetch_ref(char *ref
, unsigned char *sha1
)
1050 struct buffer buffer
;
1051 char *base
= alt
->base
;
1052 struct active_request_slot
*slot
;
1055 buffer
.buffer
= hex
;
1058 url
= quote_ref_url(base
, ref
);
1059 slot
= get_active_slot();
1060 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
1061 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1062 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
1063 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1064 if (start_active_slot(slot
)) {
1065 run_active_slot(slot
);
1066 if (slot
->curl_result
!= CURLE_OK
)
1067 return error("Couldn't get %s for %s\n%s",
1068 url
, ref
, curl_errorstr
);
1070 return error("Unable to start request");
1074 get_sha1_hex(hex
, sha1
);
1078 int main(int argc
, char **argv
)
1083 struct active_request_slot
*slot
;
1085 while (arg
< argc
&& argv
[arg
][0] == '-') {
1086 if (argv
[arg
][1] == 't') {
1088 } else if (argv
[arg
][1] == 'c') {
1090 } else if (argv
[arg
][1] == 'a') {
1094 } else if (argv
[arg
][1] == 'v') {
1096 } else if (argv
[arg
][1] == 'w') {
1097 write_ref
= argv
[arg
+ 1];
1099 } else if (!strcmp(argv
[arg
], "--recover")) {
1104 if (argc
< arg
+ 2) {
1105 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
1108 commit_id
= argv
[arg
];
1109 url
= argv
[arg
+ 1];
1111 curl_global_init(CURL_GLOBAL_ALL
);
1113 #ifdef USE_CURL_MULTI
1114 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
1115 if (http_max_requests
!= NULL
)
1116 max_requests
= atoi(http_max_requests
);
1117 if (max_requests
< 1)
1118 max_requests
= DEFAULT_MAX_REQUESTS
;
1120 curlm
= curl_multi_init();
1121 if (curlm
== NULL
) {
1122 fprintf(stderr
, "Error creating curl multi handle.\n");
1126 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
1127 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
1128 no_range_header
= curl_slist_append(no_range_header
, "Range:");
1130 curl_default
= curl_easy_init();
1132 curl_ssl_verify
= getenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
1133 curl_easy_setopt(curl_default
, CURLOPT_SSL_VERIFYPEER
, curl_ssl_verify
);
1134 #if LIBCURL_VERSION_NUM >= 0x070907
1135 curl_easy_setopt(curl_default
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
1138 if ((ssl_cert
= getenv("GIT_SSL_CERT")) != NULL
) {
1139 curl_easy_setopt(curl_default
, CURLOPT_SSLCERT
, ssl_cert
);
1141 #if LIBCURL_VERSION_NUM >= 0x070902
1142 if ((ssl_key
= getenv("GIT_SSL_KEY")) != NULL
) {
1143 curl_easy_setopt(curl_default
, CURLOPT_SSLKEY
, ssl_key
);
1146 #if LIBCURL_VERSION_NUM >= 0x070908
1147 if ((ssl_capath
= getenv("GIT_SSL_CAPATH")) != NULL
) {
1148 curl_easy_setopt(curl_default
, CURLOPT_CAPATH
, ssl_capath
);
1151 if ((ssl_cainfo
= getenv("GIT_SSL_CAINFO")) != NULL
) {
1152 curl_easy_setopt(curl_default
, CURLOPT_CAINFO
, ssl_cainfo
);
1154 curl_easy_setopt(curl_default
, CURLOPT_FAILONERROR
, 1);
1156 alt
= xmalloc(sizeof(*alt
));
1158 alt
->got_indices
= 0;
1161 fetch_alternates(alt
->base
);
1163 if (pull(commit_id
))
1166 curl_slist_free_all(pragma_header
);
1167 curl_slist_free_all(no_pragma_header
);
1168 curl_slist_free_all(no_range_header
);
1169 curl_easy_cleanup(curl_default
);
1170 slot
= active_queue_head
;
1171 while (slot
!= NULL
) {
1172 curl_easy_cleanup(slot
->curl
);
1175 #ifdef USE_CURL_MULTI
1176 curl_multi_cleanup(curlm
);
1178 curl_global_cleanup();