1#include "cache.h"
2#include "commit.h"
3#include "pack.h"
4#include "fetch.h"
5
6#include <curl/curl.h>
7#include <curl/easy.h>
8
9#if LIBCURL_VERSION_NUM >= 0x070908
10#define USE_CURL_MULTI
11#define DEFAULT_MAX_REQUESTS 5
12#endif
13
14#if LIBCURL_VERSION_NUM < 0x070704
15#define curl_global_cleanup() do { /* nothing */ } while(0)
16#endif
17#if LIBCURL_VERSION_NUM < 0x070800
18#define curl_global_init(a) do { /* nothing */ } while(0)
19#endif
20
21#define PREV_BUF_SIZE 4096
22#define RANGE_HEADER_SIZE 30
23
24static int active_requests = 0;
25static int data_received;
26
27#ifdef USE_CURL_MULTI
28static int max_requests = -1;
29static CURLM *curlm;
30#endif
31static CURL *curl_default;
32static struct curl_slist *pragma_header;
33static struct curl_slist *no_pragma_header;
34static struct curl_slist *no_range_header;
35static char curl_errorstr[CURL_ERROR_SIZE];
36
37struct alt_base
38{
39 char *base;
40 int got_indices;
41 struct packed_git *packs;
42 struct alt_base *next;
43};
44
45static struct alt_base *alt = NULL;
46
47enum transfer_state {
48 WAITING,
49 ABORTED,
50 ACTIVE,
51 COMPLETE,
52};
53
54struct transfer_request
55{
56 unsigned char sha1[20];
57 struct alt_base *repo;
58 char *url;
59 char filename[PATH_MAX];
60 char tmpfile[PATH_MAX];
61 int local;
62 enum transfer_state state;
63 CURLcode curl_result;
64 char errorstr[CURL_ERROR_SIZE];
65 long http_code;
66 unsigned char real_sha1[20];
67 SHA_CTX c;
68 z_stream stream;
69 int zret;
70 int rename;
71 struct active_request_slot *slot;
72 struct transfer_request *next;
73};
74
75struct active_request_slot
76{
77 CURL *curl;
78 FILE *local;
79 int in_use;
80 int done;
81 CURLcode curl_result;
82 struct active_request_slot *next;
83};
84
85static struct transfer_request *request_queue_head = NULL;
86static struct active_request_slot *active_queue_head = NULL;
87
88static int curl_ssl_verify = -1;
89static char *ssl_cert = NULL;
90static char *ssl_key = NULL;
91static char *ssl_capath = NULL;
92static char *ssl_cainfo = NULL;
93
94struct buffer
95{
96 size_t posn;
97 size_t size;
98 void *buffer;
99};
100
101static int http_options(const char *var, const char *value)
102{
103 if (!strcmp("http.sslverify", var)) {
104 if (curl_ssl_verify == -1) {
105 curl_ssl_verify = git_config_bool(var, value);
106 }
107 return 0;
108 }
109
110 if (!strcmp("http.sslcert", var)) {
111 if (ssl_cert == NULL) {
112 ssl_cert = xmalloc(strlen(value)+1);
113 strcpy(ssl_cert, value);
114 }
115 return 0;
116 }
117#if LIBCURL_VERSION_NUM >= 0x070902
118 if (!strcmp("http.sslkey", var)) {
119 if (ssl_key == NULL) {
120 ssl_key = xmalloc(strlen(value)+1);
121 strcpy(ssl_key, value);
122 }
123 return 0;
124 }
125#endif
126#if LIBCURL_VERSION_NUM >= 0x070908
127 if (!strcmp("http.sslcapath", var)) {
128 if (ssl_capath == NULL) {
129 ssl_capath = xmalloc(strlen(value)+1);
130 strcpy(ssl_capath, value);
131 }
132 return 0;
133 }
134#endif
135 if (!strcmp("http.sslcainfo", var)) {
136 if (ssl_cainfo == NULL) {
137 ssl_cainfo = xmalloc(strlen(value)+1);
138 strcpy(ssl_cainfo, value);
139 }
140 return 0;
141 }
142
143#ifdef USE_CURL_MULTI
144 if (!strcmp("http.maxrequests", var)) {
145 if (max_requests == -1)
146 max_requests = git_config_int(var, value);
147 return 0;
148 }
149#endif
150
151 /* Fall back on the default ones */
152 return git_default_config(var, value);
153}
154
155static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
156 struct buffer *buffer)
157{
158 size_t size = eltsize * nmemb;
159 if (size > buffer->size - buffer->posn)
160 size = buffer->size - buffer->posn;
161 memcpy(buffer->buffer + buffer->posn, ptr, size);
162 buffer->posn += size;
163 data_received++;
164 return size;
165}
166
167static size_t fwrite_buffer_dynamic(const void *ptr, size_t eltsize,
168 size_t nmemb, struct buffer *buffer)
169{
170 size_t size = eltsize * nmemb;
171 if (size > buffer->size - buffer->posn) {
172 buffer->size = buffer->size * 3 / 2;
173 if (buffer->size < buffer->posn + size)
174 buffer->size = buffer->posn + size;
175 buffer->buffer = xrealloc(buffer->buffer, buffer->size);
176 }
177 memcpy(buffer->buffer + buffer->posn, ptr, size);
178 buffer->posn += size;
179 data_received++;
180 return size;
181}
182
183static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
184 void *data)
185{
186 unsigned char expn[4096];
187 size_t size = eltsize * nmemb;
188 int posn = 0;
189 struct transfer_request *request = (struct transfer_request *)data;
190 do {
191 ssize_t retval = write(request->local,
192 ptr + posn, size - posn);
193 if (retval < 0)
194 return posn;
195 posn += retval;
196 } while (posn < size);
197
198 request->stream.avail_in = size;
199 request->stream.next_in = ptr;
200 do {
201 request->stream.next_out = expn;
202 request->stream.avail_out = sizeof(expn);
203 request->zret = inflate(&request->stream, Z_SYNC_FLUSH);
204 SHA1_Update(&request->c, expn,
205 sizeof(expn) - request->stream.avail_out);
206 } while (request->stream.avail_in && request->zret == Z_OK);
207 data_received++;
208 return size;
209}
210
211#ifdef USE_CURL_MULTI
212void process_curl_messages();
213void process_request_queue();
214#endif
215
216struct active_request_slot *get_active_slot()
217{
218 struct active_request_slot *slot = active_queue_head;
219 struct active_request_slot *newslot;
220
221#ifdef USE_CURL_MULTI
222 int num_transfers;
223
224 /* Wait for a slot to open up if the queue is full */
225 while (active_requests >= max_requests) {
226 curl_multi_perform(curlm, &num_transfers);
227 if (num_transfers < active_requests) {
228 process_curl_messages();
229 }
230 }
231#endif
232
233 while (slot != NULL && slot->in_use) {
234 slot = slot->next;
235 }
236 if (slot == NULL) {
237 newslot = xmalloc(sizeof(*newslot));
238 newslot->curl = curl_easy_duphandle(curl_default);
239 newslot->in_use = 0;
240 newslot->next = NULL;
241
242 slot = active_queue_head;
243 if (slot == NULL) {
244 active_queue_head = newslot;
245 } else {
246 while (slot->next != NULL) {
247 slot = slot->next;
248 }
249 slot->next = newslot;
250 }
251 slot = newslot;
252 }
253
254 active_requests++;
255 slot->in_use = 1;
256 slot->done = 0;
257 slot->local = NULL;
258 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
259 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_range_header);
260 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
261
262 return slot;
263}
264
265int start_active_slot(struct active_request_slot *slot)
266{
267#ifdef USE_CURL_MULTI
268 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
269
270 if (curlm_result != CURLM_OK &&
271 curlm_result != CURLM_CALL_MULTI_PERFORM) {
272 active_requests--;
273 slot->in_use = 0;
274 return 0;
275 }
276#endif
277 return 1;
278}
279
280void run_active_slot(struct active_request_slot *slot)
281{
282#ifdef USE_CURL_MULTI
283 int num_transfers;
284 long last_pos = 0;
285 long current_pos;
286 fd_set readfds;
287 fd_set writefds;
288 fd_set excfds;
289 int max_fd;
290 struct timeval select_timeout;
291 CURLMcode curlm_result;
292
293 while (!slot->done) {
294 data_received = 0;
295 do {
296 curlm_result = curl_multi_perform(curlm,
297 &num_transfers);
298 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
299 if (num_transfers < active_requests) {
300 process_curl_messages();
301 process_request_queue();
302 }
303
304 if (!data_received && slot->local != NULL) {
305 current_pos = ftell(slot->local);
306 if (current_pos > last_pos)
307 data_received++;
308 last_pos = current_pos;
309 }
310
311 if (!slot->done && !data_received) {
312 max_fd = 0;
313 FD_ZERO(&readfds);
314 FD_ZERO(&writefds);
315 FD_ZERO(&excfds);
316 select_timeout.tv_sec = 0;
317 select_timeout.tv_usec = 50000;
318 select(max_fd, &readfds, &writefds,
319 &excfds, &select_timeout);
320 }
321 }
322#else
323 slot->curl_result = curl_easy_perform(slot->curl);
324 active_requests--;
325#endif
326}
327
328void start_request(struct transfer_request *request)
329{
330 char *hex = sha1_to_hex(request->sha1);
331 char prevfile[PATH_MAX];
332 char *url;
333 char *posn;
334 int prevlocal;
335 unsigned char prev_buf[PREV_BUF_SIZE];
336 ssize_t prev_read = 0;
337 long prev_posn = 0;
338 char range[RANGE_HEADER_SIZE];
339 struct curl_slist *range_header = NULL;
340 struct active_request_slot *slot;
341
342 snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
343 unlink(prevfile);
344 rename(request->tmpfile, prevfile);
345 unlink(request->tmpfile);
346
347 request->local = open(request->tmpfile,
348 O_WRONLY | O_CREAT | O_EXCL, 0666);
349 /* This could have failed due to the "lazy directory creation";
350 * try to mkdir the last path component.
351 */
352 if (request->local < 0 && errno == ENOENT) {
353 char *dir = strrchr(request->tmpfile, '/');
354 if (dir) {
355 *dir = 0;
356 mkdir(request->tmpfile, 0777);
357 *dir = '/';
358 }
359 request->local = open(request->tmpfile,
360 O_WRONLY | O_CREAT | O_EXCL, 0666);
361 }
362
363 if (request->local < 0) {
364 request->state = ABORTED;
365 error("Couldn't create temporary file %s for %s: %s\n",
366 request->tmpfile, request->filename, strerror(errno));
367 return;
368 }
369
370 memset(&request->stream, 0, sizeof(request->stream));
371
372 inflateInit(&request->stream);
373
374 SHA1_Init(&request->c);
375
376 url = xmalloc(strlen(request->repo->base) + 50);
377 request->url = xmalloc(strlen(request->repo->base) + 50);
378 strcpy(url, request->repo->base);
379 posn = url + strlen(request->repo->base);
380 strcpy(posn, "objects/");
381 posn += 8;
382 memcpy(posn, hex, 2);
383 posn += 2;
384 *(posn++) = '/';
385 strcpy(posn, hex + 2);
386 strcpy(request->url, url);
387
388 /* If a previous temp file is present, process what was already
389 fetched. */
390 prevlocal = open(prevfile, O_RDONLY);
391 if (prevlocal != -1) {
392 do {
393 prev_read = read(prevlocal, prev_buf, PREV_BUF_SIZE);
394 if (prev_read>0) {
395 if (fwrite_sha1_file(prev_buf,
396 1,
397 prev_read,
398 request) == prev_read) {
399 prev_posn += prev_read;
400 } else {
401 prev_read = -1;
402 }
403 }
404 } while (prev_read > 0);
405 close(prevlocal);
406 }
407 unlink(prevfile);
408
409 /* Reset inflate/SHA1 if there was an error reading the previous temp
410 file; also rewind to the beginning of the local file. */
411 if (prev_read == -1) {
412 memset(&request->stream, 0, sizeof(request->stream));
413 inflateInit(&request->stream);
414 SHA1_Init(&request->c);
415 if (prev_posn>0) {
416 prev_posn = 0;
417 lseek(request->local, SEEK_SET, 0);
418 ftruncate(request->local, 0);
419 }
420 }
421
422 slot = get_active_slot();
423 curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
424 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
425 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
426 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
427 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
428
429 /* If we have successfully processed data from a previous fetch
430 attempt, only fetch the data we don't already have. */
431 if (prev_posn>0) {
432 if (get_verbosely)
433 fprintf(stderr,
434 "Resuming fetch of object %s at byte %ld\n",
435 hex, prev_posn);
436 sprintf(range, "Range: bytes=%ld-", prev_posn);
437 range_header = curl_slist_append(range_header, range);
438 curl_easy_setopt(slot->curl,
439 CURLOPT_HTTPHEADER, range_header);
440 }
441
442 /* Try to get the request started, abort the request on error */
443 if (!start_active_slot(slot)) {
444 request->state = ABORTED;
445 close(request->local);
446 free(request->url);
447 return;
448 }
449
450 request->slot = slot;
451 request->state = ACTIVE;
452}
453
454void finish_request(struct transfer_request *request)
455{
456 fchmod(request->local, 0444);
457 close(request->local);
458
459 if (request->http_code == 416) {
460 fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
461 } else if (request->curl_result != CURLE_OK) {
462 return;
463 }
464
465 inflateEnd(&request->stream);
466 SHA1_Final(request->real_sha1, &request->c);
467 if (request->zret != Z_STREAM_END) {
468 unlink(request->tmpfile);
469 return;
470 }
471 if (memcmp(request->sha1, request->real_sha1, 20)) {
472 unlink(request->tmpfile);
473 return;
474 }
475 request->rename =
476 move_temp_to_file(request->tmpfile, request->filename);
477
478 if (request->rename == 0)
479 pull_say("got %s\n", sha1_to_hex(request->sha1));
480}
481
482void release_request(struct transfer_request *request)
483{
484 struct transfer_request *entry = request_queue_head;
485
486 if (request == request_queue_head) {
487 request_queue_head = request->next;
488 } else {
489 while (entry->next != NULL && entry->next != request)
490 entry = entry->next;
491 if (entry->next == request)
492 entry->next = entry->next->next;
493 }
494
495 free(request->url);
496 free(request);
497}
498
499#ifdef USE_CURL_MULTI
500void process_curl_messages()
501{
502 int num_messages;
503 struct active_request_slot *slot;
504 struct transfer_request *request = NULL;
505 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
506
507 while (curl_message != NULL) {
508 if (curl_message->msg == CURLMSG_DONE) {
509 slot = active_queue_head;
510 while (slot != NULL &&
511 slot->curl != curl_message->easy_handle)
512 slot = slot->next;
513 if (slot != NULL) {
514 curl_multi_remove_handle(curlm, slot->curl);
515 active_requests--;
516 slot->done = 1;
517 slot->in_use = 0;
518 slot->curl_result = curl_message->data.result;
519 request = request_queue_head;
520 while (request != NULL &&
521 request->slot != slot)
522 request = request->next;
523 } else {
524 fprintf(stderr, "Received DONE message for unknown request!\n");
525 }
526 if (request != NULL) {
527 request->curl_result =
528 curl_message->data.result;
529 curl_easy_getinfo(slot->curl,
530 CURLINFO_HTTP_CODE,
531 &request->http_code);
532 request->slot = NULL;
533
534 /* Use alternates if necessary */
535 if (request->http_code == 404 &&
536 request->repo->next != NULL) {
537 request->repo = request->repo->next;
538 start_request(request);
539 } else {
540 finish_request(request);
541 request->state = COMPLETE;
542 }
543 }
544 } else {
545 fprintf(stderr, "Unknown CURL message received: %d\n",
546 (int)curl_message->msg);
547 }
548 curl_message = curl_multi_info_read(curlm, &num_messages);
549 }
550}
551
552void process_request_queue()
553{
554 struct transfer_request *request = request_queue_head;
555 int num_transfers;
556
557 while (active_requests < max_requests && request != NULL) {
558 if (request->state == WAITING) {
559 if (has_sha1_file(request->sha1))
560 release_request(request);
561 else
562 start_request(request);
563 curl_multi_perform(curlm, &num_transfers);
564 }
565 request = request->next;
566 }
567}
568#endif
569
570void prefetch(unsigned char *sha1)
571{
572 struct transfer_request *newreq;
573 struct transfer_request *tail;
574 char *filename = sha1_file_name(sha1);
575
576 newreq = xmalloc(sizeof(*newreq));
577 memcpy(newreq->sha1, sha1, 20);
578 newreq->repo = alt;
579 newreq->url = NULL;
580 newreq->local = -1;
581 newreq->state = WAITING;
582 snprintf(newreq->filename, sizeof(newreq->filename), "%s", filename);
583 snprintf(newreq->tmpfile, sizeof(newreq->tmpfile),
584 "%s.temp", filename);
585 newreq->next = NULL;
586
587 if (request_queue_head == NULL) {
588 request_queue_head = newreq;
589 } else {
590 tail = request_queue_head;
591 while (tail->next != NULL) {
592 tail = tail->next;
593 }
594 tail->next = newreq;
595 }
596#ifdef USE_CURL_MULTI
597 process_request_queue();
598 process_curl_messages();
599#endif
600}
601
602static int fetch_index(struct alt_base *repo, unsigned char *sha1)
603{
604 char *hex = sha1_to_hex(sha1);
605 char *filename;
606 char *url;
607 char tmpfile[PATH_MAX];
608 long prev_posn = 0;
609 char range[RANGE_HEADER_SIZE];
610 struct curl_slist *range_header = NULL;
611
612 FILE *indexfile;
613 struct active_request_slot *slot;
614
615 if (has_pack_index(sha1))
616 return 0;
617
618 if (get_verbosely)
619 fprintf(stderr, "Getting index for pack %s\n", hex);
620
621 url = xmalloc(strlen(repo->base) + 64);
622 sprintf(url, "%s/objects/pack/pack-%s.idx", repo->base, hex);
623
624 filename = sha1_pack_index_name(sha1);
625 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
626 indexfile = fopen(tmpfile, "a");
627 if (!indexfile)
628 return error("Unable to open local file %s for pack index",
629 filename);
630
631 slot = get_active_slot();
632 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
633 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
634 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
635 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
636 slot->local = indexfile;
637
638 /* If there is data present from a previous transfer attempt,
639 resume where it left off */
640 prev_posn = ftell(indexfile);
641 if (prev_posn>0) {
642 if (get_verbosely)
643 fprintf(stderr,
644 "Resuming fetch of index for pack %s at byte %ld\n",
645 hex, prev_posn);
646 sprintf(range, "Range: bytes=%ld-", prev_posn);
647 range_header = curl_slist_append(range_header, range);
648 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
649 }
650
651 if (start_active_slot(slot)) {
652 run_active_slot(slot);
653 if (slot->curl_result != CURLE_OK) {
654 fclose(indexfile);
655 return error("Unable to get pack index %s\n%s", url,
656 curl_errorstr);
657 }
658 } else {
659 return error("Unable to start request");
660 }
661
662 fclose(indexfile);
663
664 return move_temp_to_file(tmpfile, filename);
665}
666
667static int setup_index(struct alt_base *repo, unsigned char *sha1)
668{
669 struct packed_git *new_pack;
670 if (has_pack_file(sha1))
671 return 0; // don't list this as something we can get
672
673 if (fetch_index(repo, sha1))
674 return -1;
675
676 new_pack = parse_pack_index(sha1);
677 new_pack->next = repo->packs;
678 repo->packs = new_pack;
679 return 0;
680}
681
682static int fetch_alternates(char *base)
683{
684 int ret = 0;
685 struct buffer buffer;
686 char *url;
687 char *data;
688 int i = 0;
689 int http_specific = 1;
690 struct alt_base *tail = alt;
691 static const char null_byte = '\0';
692
693 struct active_request_slot *slot;
694
695 data = xmalloc(4096);
696 buffer.size = 4096;
697 buffer.posn = 0;
698 buffer.buffer = data;
699
700 if (get_verbosely)
701 fprintf(stderr, "Getting alternates list\n");
702
703 url = xmalloc(strlen(base) + 31);
704 sprintf(url, "%s/objects/info/http-alternates", base);
705
706 slot = get_active_slot();
707 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
708 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
709 fwrite_buffer_dynamic);
710 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
711 if (start_active_slot(slot)) {
712 run_active_slot(slot);
713 if (slot->curl_result != CURLE_OK || !buffer.posn) {
714 http_specific = 0;
715
716 sprintf(url, "%s/objects/info/alternates", base);
717
718 slot = get_active_slot();
719 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
720 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
721 fwrite_buffer_dynamic);
722 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
723 if (start_active_slot(slot)) {
724 run_active_slot(slot);
725 if (slot->curl_result != CURLE_OK) {
726 free(buffer.buffer);
727 return 0;
728 }
729 }
730 }
731 } else {
732 free(buffer.buffer);
733 return 0;
734 }
735
736 fwrite_buffer_dynamic(&null_byte, 1, 1, &buffer);
737 buffer.posn--;
738 data = buffer.buffer;
739
740 while (i < buffer.posn) {
741 int posn = i;
742 while (posn < buffer.posn && data[posn] != '\n')
743 posn++;
744 if (data[posn] == '\n') {
745 int okay = 0;
746 int serverlen = 0;
747 struct alt_base *newalt;
748 char *target = NULL;
749 if (data[i] == '/') {
750 serverlen = strchr(base + 8, '/') - base;
751 okay = 1;
752 } else if (!memcmp(data + i, "../", 3)) {
753 i += 3;
754 serverlen = strlen(base);
755 while (i + 2 < posn &&
756 !memcmp(data + i, "../", 3)) {
757 do {
758 serverlen--;
759 } while (serverlen &&
760 base[serverlen - 1] != '/');
761 i += 3;
762 }
763 // If the server got removed, give up.
764 okay = strchr(base, ':') - base + 3 <
765 serverlen;
766 } else if (http_specific) {
767 char *colon = strchr(data + i, ':');
768 char *slash = strchr(data + i, '/');
769 if (colon && slash && colon < data + posn &&
770 slash < data + posn && colon < slash) {
771 okay = 1;
772 }
773 }
774 // skip 'objects' at end
775 if (okay) {
776 target = xmalloc(serverlen + posn - i - 6);
777 strncpy(target, base, serverlen);
778 strncpy(target + serverlen, data + i,
779 posn - i - 7);
780 target[serverlen + posn - i - 7] = '\0';
781 if (get_verbosely)
782 fprintf(stderr,
783 "Also look at %s\n", target);
784 newalt = xmalloc(sizeof(*newalt));
785 newalt->next = NULL;
786 newalt->base = target;
787 newalt->got_indices = 0;
788 newalt->packs = NULL;
789 while (tail->next != NULL)
790 tail = tail->next;
791 tail->next = newalt;
792 ret++;
793 }
794 }
795 i = posn + 1;
796 }
797
798 free(buffer.buffer);
799 return ret;
800}
801
802static int fetch_indices(struct alt_base *repo)
803{
804 unsigned char sha1[20];
805 char *url;
806 struct buffer buffer;
807 char *data;
808 int i = 0;
809
810 struct active_request_slot *slot;
811
812 if (repo->got_indices)
813 return 0;
814
815 data = xmalloc(4096);
816 buffer.size = 4096;
817 buffer.posn = 0;
818 buffer.buffer = data;
819
820 if (get_verbosely)
821 fprintf(stderr, "Getting pack list\n");
822
823 url = xmalloc(strlen(repo->base) + 21);
824 sprintf(url, "%s/objects/info/packs", repo->base);
825
826 slot = get_active_slot();
827 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
828 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
829 fwrite_buffer_dynamic);
830 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
831 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
832 if (start_active_slot(slot)) {
833 run_active_slot(slot);
834 if (slot->curl_result != CURLE_OK) {
835 free(buffer.buffer);
836 return error("%s", curl_errorstr);
837 }
838 } else {
839 free(buffer.buffer);
840 return error("Unable to start request");
841 }
842
843 data = buffer.buffer;
844 while (i < buffer.posn) {
845 switch (data[i]) {
846 case 'P':
847 i++;
848 if (i + 52 < buffer.posn &&
849 !strncmp(data + i, " pack-", 6) &&
850 !strncmp(data + i + 46, ".pack\n", 6)) {
851 get_sha1_hex(data + i + 6, sha1);
852 setup_index(repo, sha1);
853 i += 51;
854 break;
855 }
856 default:
857 while (data[i] != '\n')
858 i++;
859 }
860 i++;
861 }
862
863 free(buffer.buffer);
864 repo->got_indices = 1;
865 return 0;
866}
867
868static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
869{
870 char *url;
871 struct packed_git *target;
872 struct packed_git **lst;
873 FILE *packfile;
874 char *filename;
875 char tmpfile[PATH_MAX];
876 int ret;
877 long prev_posn = 0;
878 char range[RANGE_HEADER_SIZE];
879 struct curl_slist *range_header = NULL;
880
881 struct active_request_slot *slot;
882
883 if (fetch_indices(repo))
884 return -1;
885 target = find_sha1_pack(sha1, repo->packs);
886 if (!target)
887 return -1;
888
889 if (get_verbosely) {
890 fprintf(stderr, "Getting pack %s\n",
891 sha1_to_hex(target->sha1));
892 fprintf(stderr, " which contains %s\n",
893 sha1_to_hex(sha1));
894 }
895
896 url = xmalloc(strlen(repo->base) + 65);
897 sprintf(url, "%s/objects/pack/pack-%s.pack",
898 repo->base, sha1_to_hex(target->sha1));
899
900 filename = sha1_pack_name(target->sha1);
901 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
902 packfile = fopen(tmpfile, "a");
903 if (!packfile)
904 return error("Unable to open local file %s for pack",
905 filename);
906
907 slot = get_active_slot();
908 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
909 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
910 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
911 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
912 slot->local = packfile;
913
914 /* If there is data present from a previous transfer attempt,
915 resume where it left off */
916 prev_posn = ftell(packfile);
917 if (prev_posn>0) {
918 if (get_verbosely)
919 fprintf(stderr,
920 "Resuming fetch of pack %s at byte %ld\n",
921 sha1_to_hex(target->sha1), prev_posn);
922 sprintf(range, "Range: bytes=%ld-", prev_posn);
923 range_header = curl_slist_append(range_header, range);
924 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
925 }
926
927 if (start_active_slot(slot)) {
928 run_active_slot(slot);
929 if (slot->curl_result != CURLE_OK) {
930 fclose(packfile);
931 return error("Unable to get pack file %s\n%s", url,
932 curl_errorstr);
933 }
934 } else {
935 return error("Unable to start request");
936 }
937
938 fclose(packfile);
939
940 ret = move_temp_to_file(tmpfile, filename);
941 if (ret)
942 return ret;
943
944 lst = &repo->packs;
945 while (*lst != target)
946 lst = &((*lst)->next);
947 *lst = (*lst)->next;
948
949 if (verify_pack(target, 0))
950 return -1;
951 install_packed_git(target);
952
953 return 0;
954}
955
956static int fetch_object(struct alt_base *repo, unsigned char *sha1)
957{
958 char *hex = sha1_to_hex(sha1);
959 int ret;
960 struct transfer_request *request = request_queue_head;
961
962 while (request != NULL && memcmp(request->sha1, sha1, 20))
963 request = request->next;
964 if (request == NULL)
965 return error("Couldn't find request for %s in the queue", hex);
966
967 if (has_sha1_file(request->sha1)) {
968 release_request(request);
969 return 0;
970 }
971
972#ifdef USE_CURL_MULTI
973 int num_transfers;
974 while (request->state == WAITING) {
975 curl_multi_perform(curlm, &num_transfers);
976 if (num_transfers < active_requests) {
977 process_curl_messages();
978 process_request_queue();
979 }
980 }
981#else
982 start_request(request);
983#endif
984
985 while (request->state == ACTIVE) {
986 run_active_slot(request->slot);
987#ifndef USE_CURL_MULTI
988 request->curl_result = request->slot->curl_result;
989 curl_easy_getinfo(request->slot->curl,
990 CURLINFO_HTTP_CODE,
991 &request->http_code);
992 request->slot = NULL;
993
994 /* Use alternates if necessary */
995 if (request->http_code == 404 &&
996 request->repo->next != NULL) {
997 request->repo = request->repo->next;
998 start_request(request);
999 } else {
1000 finish_request(request);
1001 request->state = COMPLETE;
1002 }
1003#endif
1004 }
1005
1006 if (request->state == ABORTED) {
1007 release_request(request);
1008 return error("Request for %s aborted", hex);
1009 }
1010
1011 if (request->curl_result != CURLE_OK && request->http_code != 416) {
1012 ret = error("%s", request->errorstr);
1013 release_request(request);
1014 return ret;
1015 }
1016
1017 if (request->zret != Z_STREAM_END) {
1018 ret = error("File %s (%s) corrupt\n", hex, request->url);
1019 release_request(request);
1020 return ret;
1021 }
1022
1023 if (memcmp(request->sha1, request->real_sha1, 20)) {
1024 release_request(request);
1025 return error("File %s has bad hash\n", hex);
1026 }
1027
1028 if (request->rename < 0) {
1029 ret = error("unable to write sha1 filename %s: %s",
1030 request->filename,
1031 strerror(request->rename));
1032 release_request(request);
1033 return ret;
1034 }
1035
1036 release_request(request);
1037 return 0;
1038}
1039
1040int fetch(unsigned char *sha1)
1041{
1042 struct alt_base *altbase = alt;
1043
1044 if (!fetch_object(altbase, sha1))
1045 return 0;
1046 while (altbase) {
1047 if (!fetch_pack(altbase, sha1))
1048 return 0;
1049 altbase = altbase->next;
1050 }
1051 return error("Unable to find %s under %s\n", sha1_to_hex(sha1),
1052 alt->base);
1053}
1054
1055static inline int needs_quote(int ch)
1056{
1057 switch (ch) {
1058 case '/': case '-': case '.':
1059 case 'A'...'Z': case 'a'...'z': case '0'...'9':
1060 return 0;
1061 default:
1062 return 1;
1063 }
1064}
1065
1066static inline int hex(int v)
1067{
1068 if (v < 10) return '0' + v;
1069 else return 'A' + v - 10;
1070}
1071
1072static char *quote_ref_url(const char *base, const char *ref)
1073{
1074 const char *cp;
1075 char *dp, *qref;
1076 int len, baselen, ch;
1077
1078 baselen = strlen(base);
1079 len = baselen + 6; /* "refs/" + NUL */
1080 for (cp = ref; (ch = *cp) != 0; cp++, len++)
1081 if (needs_quote(ch))
1082 len += 2; /* extra two hex plus replacement % */
1083 qref = xmalloc(len);
1084 memcpy(qref, base, baselen);
1085 memcpy(qref + baselen, "refs/", 5);
1086 for (cp = ref, dp = qref + baselen + 5; (ch = *cp) != 0; cp++) {
1087 if (needs_quote(ch)) {
1088 *dp++ = '%';
1089 *dp++ = hex((ch >> 4) & 0xF);
1090 *dp++ = hex(ch & 0xF);
1091 }
1092 else
1093 *dp++ = ch;
1094 }
1095 *dp = 0;
1096
1097 return qref;
1098}
1099
1100int fetch_ref(char *ref, unsigned char *sha1)
1101{
1102 char *url;
1103 char hex[42];
1104 struct buffer buffer;
1105 char *base = alt->base;
1106 struct active_request_slot *slot;
1107 buffer.size = 41;
1108 buffer.posn = 0;
1109 buffer.buffer = hex;
1110 hex[41] = '\0';
1111
1112 url = quote_ref_url(base, ref);
1113 slot = get_active_slot();
1114 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
1115 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1116 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
1117 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1118 if (start_active_slot(slot)) {
1119 run_active_slot(slot);
1120 if (slot->curl_result != CURLE_OK)
1121 return error("Couldn't get %s for %s\n%s",
1122 url, ref, curl_errorstr);
1123 } else {
1124 return error("Unable to start request");
1125 }
1126
1127 hex[40] = '\0';
1128 get_sha1_hex(hex, sha1);
1129 return 0;
1130}
1131
1132int main(int argc, char **argv)
1133{
1134 char *commit_id;
1135 char *url;
1136 int arg = 1;
1137 struct active_request_slot *slot;
1138
1139 while (arg < argc && argv[arg][0] == '-') {
1140 if (argv[arg][1] == 't') {
1141 get_tree = 1;
1142 } else if (argv[arg][1] == 'c') {
1143 get_history = 1;
1144 } else if (argv[arg][1] == 'a') {
1145 get_all = 1;
1146 get_tree = 1;
1147 get_history = 1;
1148 } else if (argv[arg][1] == 'v') {
1149 get_verbosely = 1;
1150 } else if (argv[arg][1] == 'w') {
1151 write_ref = argv[arg + 1];
1152 arg++;
1153 } else if (!strcmp(argv[arg], "--recover")) {
1154 get_recover = 1;
1155 }
1156 arg++;
1157 }
1158 if (argc < arg + 2) {
1159 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
1160 return 1;
1161 }
1162 commit_id = argv[arg];
1163 url = argv[arg + 1];
1164
1165 curl_global_init(CURL_GLOBAL_ALL);
1166
1167#ifdef USE_CURL_MULTI
1168 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
1169 if (http_max_requests != NULL)
1170 max_requests = atoi(http_max_requests);
1171
1172 curlm = curl_multi_init();
1173 if (curlm == NULL) {
1174 fprintf(stderr, "Error creating curl multi handle.\n");
1175 return 1;
1176 }
1177#endif
1178
1179 if (getenv("GIT_SSL_NO_VERIFY"))
1180 curl_ssl_verify = 0;
1181
1182 ssl_cert = getenv("GIT_SSL_CERT");
1183#if LIBCURL_VERSION_NUM >= 0x070902
1184 ssl_key = getenv("GIT_SSL_KEY");
1185#endif
1186#if LIBCURL_VERSION_NUM >= 0x070908
1187 ssl_capath = getenv("GIT_SSL_CAPATH");
1188#endif
1189 ssl_cainfo = getenv("GIT_SSL_CAINFO");
1190
1191 git_config(http_options);
1192
1193 if (curl_ssl_verify == -1)
1194 curl_ssl_verify = 1;
1195
1196#ifdef USE_CURL_MULTI
1197 if (max_requests < 1)
1198 max_requests = DEFAULT_MAX_REQUESTS;
1199#endif
1200
1201 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
1202 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
1203 no_range_header = curl_slist_append(no_range_header, "Range:");
1204
1205 curl_default = curl_easy_init();
1206
1207 curl_easy_setopt(curl_default, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
1208#if LIBCURL_VERSION_NUM >= 0x070907
1209 curl_easy_setopt(curl_default, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
1210#endif
1211
1212 if (ssl_cert != NULL)
1213 curl_easy_setopt(curl_default, CURLOPT_SSLCERT, ssl_cert);
1214 if (ssl_key != NULL)
1215 curl_easy_setopt(curl_default, CURLOPT_SSLKEY, ssl_key);
1216 if (ssl_capath != NULL)
1217 curl_easy_setopt(curl_default, CURLOPT_CAPATH, ssl_capath);
1218 if (ssl_cainfo != NULL)
1219 curl_easy_setopt(curl_default, CURLOPT_CAINFO, ssl_cainfo);
1220
1221 curl_easy_setopt(curl_default, CURLOPT_FAILONERROR, 1);
1222
1223 alt = xmalloc(sizeof(*alt));
1224 alt->base = url;
1225 alt->got_indices = 0;
1226 alt->packs = NULL;
1227 alt->next = NULL;
1228 fetch_alternates(alt->base);
1229
1230 if (pull(commit_id))
1231 return 1;
1232
1233 curl_slist_free_all(pragma_header);
1234 curl_slist_free_all(no_pragma_header);
1235 curl_slist_free_all(no_range_header);
1236 curl_easy_cleanup(curl_default);
1237 slot = active_queue_head;
1238 while (slot != NULL) {
1239 curl_easy_cleanup(slot->curl);
1240 slot = slot->next;
1241 }
1242#ifdef USE_CURL_MULTI
1243 curl_multi_cleanup(curlm);
1244#endif
1245 curl_global_cleanup();
1246 return 0;
1247}