7 #define PREV_BUF_SIZE 4096
8 #define RANGE_HEADER_SIZE 30
10 static int got_alternates = -1;
12 static struct curl_slist *no_pragma_header;
18 struct packed_git *packs;
19 struct alt_base *next;
22 static struct alt_base *alt = NULL;
24 enum object_request_state {
33 unsigned char sha1[20];
34 struct alt_base *repo;
36 char filename[PATH_MAX];
37 char tmpfile[PATH_MAX];
39 enum object_request_state state;
41 char errorstr[CURL_ERROR_SIZE];
43 unsigned char real_sha1[20];
48 struct active_request_slot *slot;
49 struct object_request *next;
52 struct alternates_request {
55 struct buffer *buffer;
56 struct active_request_slot *slot;
60 static struct object_request *object_queue_head = NULL;
62 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
65 unsigned char expn[4096];
66 size_t size = eltsize * nmemb;
68 struct object_request *obj_req = (struct object_request *)data;
70 ssize_t retval = write(obj_req->local,
71 ptr + posn, size - posn);
75 } while (posn < size);
77 obj_req->stream.avail_in = size;
78 obj_req->stream.next_in = ptr;
80 obj_req->stream.next_out = expn;
81 obj_req->stream.avail_out = sizeof(expn);
82 obj_req->zret = inflate(&obj_req->stream, Z_SYNC_FLUSH);
83 SHA1_Update(&obj_req->c, expn,
84 sizeof(expn) - obj_req->stream.avail_out);
85 } while (obj_req->stream.avail_in && obj_req->zret == Z_OK);
90 static void fetch_alternates(char *base);
92 static void process_object_response(void *callback_data);
94 static void start_object_request(struct object_request *obj_req)
96 char *hex = sha1_to_hex(obj_req->sha1);
97 char prevfile[PATH_MAX];
101 unsigned char prev_buf[PREV_BUF_SIZE];
102 ssize_t prev_read = 0;
104 char range[RANGE_HEADER_SIZE];
105 struct curl_slist *range_header = NULL;
106 struct active_request_slot *slot;
108 snprintf(prevfile, sizeof(prevfile), "%s.prev", obj_req->filename);
110 rename(obj_req->tmpfile, prevfile);
111 unlink(obj_req->tmpfile);
113 if (obj_req->local != -1)
114 error("fd leakage in start: %d", obj_req->local);
115 obj_req->local = open(obj_req->tmpfile,
116 O_WRONLY | O_CREAT | O_EXCL, 0666);
117 /* This could have failed due to the "lazy directory creation";
118 * try to mkdir the last path component.
120 if (obj_req->local < 0 && errno == ENOENT) {
121 char *dir = strrchr(obj_req->tmpfile, '/');
124 mkdir(obj_req->tmpfile, 0777);
127 obj_req->local = open(obj_req->tmpfile,
128 O_WRONLY | O_CREAT | O_EXCL, 0666);
131 if (obj_req->local < 0) {
132 obj_req->state = ABORTED;
133 error("Couldn't create temporary file %s for %s: %s\n",
134 obj_req->tmpfile, obj_req->filename, strerror(errno));
138 memset(&obj_req->stream, 0, sizeof(obj_req->stream));
140 inflateInit(&obj_req->stream);
142 SHA1_Init(&obj_req->c);
144 url = xmalloc(strlen(obj_req->repo->base) + 50);
145 obj_req->url = xmalloc(strlen(obj_req->repo->base) + 50);
146 strcpy(url, obj_req->repo->base);
147 posn = url + strlen(obj_req->repo->base);
148 strcpy(posn, "objects/");
150 memcpy(posn, hex, 2);
153 strcpy(posn, hex + 2);
154 strcpy(obj_req->url, url);
156 /* If a previous temp file is present, process what was already
158 prevlocal = open(prevfile, O_RDONLY);
159 if (prevlocal != -1) {
161 prev_read = read(prevlocal, prev_buf, PREV_BUF_SIZE);
163 if (fwrite_sha1_file(prev_buf,
166 obj_req) == prev_read) {
167 prev_posn += prev_read;
172 } while (prev_read > 0);
177 /* Reset inflate/SHA1 if there was an error reading the previous temp
178 file; also rewind to the beginning of the local file. */
179 if (prev_read == -1) {
180 memset(&obj_req->stream, 0, sizeof(obj_req->stream));
181 inflateInit(&obj_req->stream);
182 SHA1_Init(&obj_req->c);
185 lseek(obj_req->local, SEEK_SET, 0);
186 ftruncate(obj_req->local, 0);
190 slot = get_active_slot();
191 slot->callback_func = process_object_response;
192 slot->callback_data = obj_req;
193 obj_req->slot = slot;
195 curl_easy_setopt(slot->curl, CURLOPT_FILE, obj_req);
196 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
197 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, obj_req->errorstr);
198 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
199 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
201 /* If we have successfully processed data from a previous fetch
202 attempt, only fetch the data we don't already have. */
206 "Resuming fetch of object %s at byte %ld\n",
208 sprintf(range, "Range: bytes=%ld-", prev_posn);
209 range_header = curl_slist_append(range_header, range);
210 curl_easy_setopt(slot->curl,
211 CURLOPT_HTTPHEADER, range_header);
214 /* Try to get the request started, abort the request on error */
215 obj_req->state = ACTIVE;
216 if (!start_active_slot(slot)) {
217 obj_req->state = ABORTED;
218 obj_req->slot = NULL;
219 close(obj_req->local); obj_req->local = -1;
226 static void finish_object_request(struct object_request *obj_req)
230 fchmod(obj_req->local, 0444);
231 close(obj_req->local); obj_req->local = -1;
233 if (obj_req->http_code == 416) {
234 fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
235 } else if (obj_req->curl_result != CURLE_OK) {
236 if (stat(obj_req->tmpfile, &st) == 0)
238 unlink(obj_req->tmpfile);
242 inflateEnd(&obj_req->stream);
243 SHA1_Final(obj_req->real_sha1, &obj_req->c);
244 if (obj_req->zret != Z_STREAM_END) {
245 unlink(obj_req->tmpfile);
248 if (memcmp(obj_req->sha1, obj_req->real_sha1, 20)) {
249 unlink(obj_req->tmpfile);
253 move_temp_to_file(obj_req->tmpfile, obj_req->filename);
255 if (obj_req->rename == 0)
256 pull_say("got %s\n", sha1_to_hex(obj_req->sha1));
259 static void process_object_response(void *callback_data)
261 struct object_request *obj_req =
262 (struct object_request *)callback_data;
264 obj_req->curl_result = obj_req->slot->curl_result;
265 obj_req->http_code = obj_req->slot->http_code;
266 obj_req->slot = NULL;
267 obj_req->state = COMPLETE;
269 /* Use alternates if necessary */
270 if (obj_req->http_code == 404 ||
271 obj_req->curl_result == CURLE_FILE_COULDNT_READ_FILE) {
272 fetch_alternates(alt->base);
273 if (obj_req->repo->next != NULL) {
276 close(obj_req->local);
278 start_object_request(obj_req);
283 finish_object_request(obj_req);
286 static void release_object_request(struct object_request *obj_req)
288 struct object_request *entry = object_queue_head;
290 if (obj_req->local != -1)
291 error("fd leakage in release: %d", obj_req->local);
292 if (obj_req == object_queue_head) {
293 object_queue_head = obj_req->next;
295 while (entry->next != NULL && entry->next != obj_req)
297 if (entry->next == obj_req)
298 entry->next = entry->next->next;
305 #ifdef USE_CURL_MULTI
306 void fill_active_slots(void)
308 struct object_request *obj_req = object_queue_head;
309 struct active_request_slot *slot = active_queue_head;
312 while (active_requests < max_requests && obj_req != NULL) {
313 if (obj_req->state == WAITING) {
314 if (has_sha1_file(obj_req->sha1))
315 release_object_request(obj_req);
317 start_object_request(obj_req);
318 curl_multi_perform(curlm, &num_transfers);
320 obj_req = obj_req->next;
323 while (slot != NULL) {
324 if (!slot->in_use && slot->curl != NULL) {
325 curl_easy_cleanup(slot->curl);
333 void prefetch(unsigned char *sha1)
335 struct object_request *newreq;
336 struct object_request *tail;
337 char *filename = sha1_file_name(sha1);
339 newreq = xmalloc(sizeof(*newreq));
340 memcpy(newreq->sha1, sha1, 20);
344 newreq->state = WAITING;
345 snprintf(newreq->filename, sizeof(newreq->filename), "%s", filename);
346 snprintf(newreq->tmpfile, sizeof(newreq->tmpfile),
347 "%s.temp", filename);
350 if (object_queue_head == NULL) {
351 object_queue_head = newreq;
353 tail = object_queue_head;
354 while (tail->next != NULL) {
360 #ifdef USE_CURL_MULTI
366 static int fetch_index(struct alt_base *repo, unsigned char *sha1)
368 char *hex = sha1_to_hex(sha1);
371 char tmpfile[PATH_MAX];
373 char range[RANGE_HEADER_SIZE];
374 struct curl_slist *range_header = NULL;
377 struct active_request_slot *slot;
379 if (has_pack_index(sha1))
383 fprintf(stderr, "Getting index for pack %s\n", hex);
385 url = xmalloc(strlen(repo->base) + 64);
386 sprintf(url, "%s/objects/pack/pack-%s.idx", repo->base, hex);
388 filename = sha1_pack_index_name(sha1);
389 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
390 indexfile = fopen(tmpfile, "a");
392 return error("Unable to open local file %s for pack index",
395 slot = get_active_slot();
396 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
397 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
398 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
399 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
400 slot->local = indexfile;
402 /* If there is data present from a previous transfer attempt,
403 resume where it left off */
404 prev_posn = ftell(indexfile);
408 "Resuming fetch of index for pack %s at byte %ld\n",
410 sprintf(range, "Range: bytes=%ld-", prev_posn);
411 range_header = curl_slist_append(range_header, range);
412 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
415 if (start_active_slot(slot)) {
416 run_active_slot(slot);
417 if (slot->curl_result != CURLE_OK) {
419 return error("Unable to get pack index %s\n%s", url,
424 return error("Unable to start request");
429 return move_temp_to_file(tmpfile, filename);
432 static int setup_index(struct alt_base *repo, unsigned char *sha1)
434 struct packed_git *new_pack;
435 if (has_pack_file(sha1))
436 return 0; // don't list this as something we can get
438 if (fetch_index(repo, sha1))
441 new_pack = parse_pack_index(sha1);
442 new_pack->next = repo->packs;
443 repo->packs = new_pack;
447 static void process_alternates_response(void *callback_data)
449 struct alternates_request *alt_req =
450 (struct alternates_request *)callback_data;
451 struct active_request_slot *slot = alt_req->slot;
452 struct alt_base *tail = alt;
453 char *base = alt_req->base;
454 static const char null_byte = '\0';
458 if (alt_req->http_specific) {
459 if (slot->curl_result != CURLE_OK ||
460 !alt_req->buffer->posn) {
462 /* Try reusing the slot to get non-http alternates */
463 alt_req->http_specific = 0;
464 sprintf(alt_req->url, "%s/objects/info/alternates",
466 curl_easy_setopt(slot->curl, CURLOPT_URL,
470 if (start_active_slot(slot)) {
478 } else if (slot->curl_result != CURLE_OK) {
479 if (slot->http_code != 404 &&
480 slot->curl_result != CURLE_FILE_COULDNT_READ_FILE) {
486 fwrite_buffer(&null_byte, 1, 1, alt_req->buffer);
487 alt_req->buffer->posn--;
488 data = alt_req->buffer->buffer;
490 while (i < alt_req->buffer->posn) {
492 while (posn < alt_req->buffer->posn && data[posn] != '\n')
494 if (data[posn] == '\n') {
497 struct alt_base *newalt;
499 if (data[i] == '/') {
500 serverlen = strchr(base + 8, '/') - base;
502 } else if (!memcmp(data + i, "../", 3)) {
504 serverlen = strlen(base);
505 while (i + 2 < posn &&
506 !memcmp(data + i, "../", 3)) {
509 } while (serverlen &&
510 base[serverlen - 1] != '/');
513 // If the server got removed, give up.
514 okay = strchr(base, ':') - base + 3 <
516 } else if (alt_req->http_specific) {
517 char *colon = strchr(data + i, ':');
518 char *slash = strchr(data + i, '/');
519 if (colon && slash && colon < data + posn &&
520 slash < data + posn && colon < slash) {
524 // skip 'objects' at end
526 target = xmalloc(serverlen + posn - i - 6);
527 strncpy(target, base, serverlen);
528 strncpy(target + serverlen, data + i,
530 target[serverlen + posn - i - 7] = '\0';
533 "Also look at %s\n", target);
534 newalt = xmalloc(sizeof(*newalt));
536 newalt->base = target;
537 newalt->got_indices = 0;
538 newalt->packs = NULL;
539 while (tail->next != NULL)
550 static void fetch_alternates(char *base)
552 struct buffer buffer;
555 struct active_request_slot *slot;
556 static struct alternates_request alt_req;
558 /* If another request has already started fetching alternates,
559 wait for them to arrive and return to processing this request's
561 #ifdef USE_CURL_MULTI
562 while (got_alternates == 0) {
567 /* Nothing to do if they've already been fetched */
568 if (got_alternates == 1)
571 /* Start the fetch */
574 data = xmalloc(4096);
577 buffer.buffer = data;
580 fprintf(stderr, "Getting alternates list for %s\n", base);
582 url = xmalloc(strlen(base) + 31);
583 sprintf(url, "%s/objects/info/http-alternates", base);
585 /* Use a callback to process the result, since another request
586 may fail and need to have alternates loaded before continuing */
587 slot = get_active_slot();
588 slot->callback_func = process_alternates_response;
589 slot->callback_data = &alt_req;
591 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
592 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
593 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
597 alt_req.buffer = &buffer;
598 alt_req.http_specific = 1;
601 if (start_active_slot(slot))
602 run_active_slot(slot);
610 static int fetch_indices(struct alt_base *repo)
612 unsigned char sha1[20];
614 struct buffer buffer;
618 struct active_request_slot *slot;
620 if (repo->got_indices)
623 data = xmalloc(4096);
626 buffer.buffer = data;
629 fprintf(stderr, "Getting pack list for %s\n", repo->base);
631 url = xmalloc(strlen(repo->base) + 21);
632 sprintf(url, "%s/objects/info/packs", repo->base);
634 slot = get_active_slot();
635 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
636 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
637 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
638 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
639 if (start_active_slot(slot)) {
640 run_active_slot(slot);
641 if (slot->curl_result != CURLE_OK) {
642 if (slot->http_code == 404 ||
643 slot->curl_result == CURLE_FILE_COULDNT_READ_FILE) {
644 repo->got_indices = 1;
648 repo->got_indices = 0;
650 return error("%s", curl_errorstr);
654 repo->got_indices = 0;
656 return error("Unable to start request");
659 data = buffer.buffer;
660 while (i < buffer.posn) {
664 if (i + 52 <= buffer.posn &&
665 !strncmp(data + i, " pack-", 6) &&
666 !strncmp(data + i + 46, ".pack\n", 6)) {
667 get_sha1_hex(data + i + 6, sha1);
668 setup_index(repo, sha1);
673 while (i < buffer.posn && data[i] != '\n')
680 repo->got_indices = 1;
684 static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
687 struct packed_git *target;
688 struct packed_git **lst;
691 char tmpfile[PATH_MAX];
694 char range[RANGE_HEADER_SIZE];
695 struct curl_slist *range_header = NULL;
697 struct active_request_slot *slot;
699 if (fetch_indices(repo))
701 target = find_sha1_pack(sha1, repo->packs);
706 fprintf(stderr, "Getting pack %s\n",
707 sha1_to_hex(target->sha1));
708 fprintf(stderr, " which contains %s\n",
712 url = xmalloc(strlen(repo->base) + 65);
713 sprintf(url, "%s/objects/pack/pack-%s.pack",
714 repo->base, sha1_to_hex(target->sha1));
716 filename = sha1_pack_name(target->sha1);
717 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
718 packfile = fopen(tmpfile, "a");
720 return error("Unable to open local file %s for pack",
723 slot = get_active_slot();
724 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
725 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
726 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
727 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
728 slot->local = packfile;
730 /* If there is data present from a previous transfer attempt,
731 resume where it left off */
732 prev_posn = ftell(packfile);
736 "Resuming fetch of pack %s at byte %ld\n",
737 sha1_to_hex(target->sha1), prev_posn);
738 sprintf(range, "Range: bytes=%ld-", prev_posn);
739 range_header = curl_slist_append(range_header, range);
740 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
743 if (start_active_slot(slot)) {
744 run_active_slot(slot);
745 if (slot->curl_result != CURLE_OK) {
747 return error("Unable to get pack file %s\n%s", url,
752 return error("Unable to start request");
757 ret = move_temp_to_file(tmpfile, filename);
762 while (*lst != target)
763 lst = &((*lst)->next);
766 if (verify_pack(target, 0))
768 install_packed_git(target);
773 static int fetch_object(struct alt_base *repo, unsigned char *sha1)
775 char *hex = sha1_to_hex(sha1);
777 struct object_request *obj_req = object_queue_head;
779 while (obj_req != NULL && memcmp(obj_req->sha1, sha1, 20))
780 obj_req = obj_req->next;
782 return error("Couldn't find request for %s in the queue", hex);
784 if (has_sha1_file(obj_req->sha1)) {
785 release_object_request(obj_req);
789 #ifdef USE_CURL_MULTI
790 while (obj_req->state == WAITING) {
794 start_object_request(obj_req);
797 while (obj_req->state == ACTIVE) {
798 run_active_slot(obj_req->slot);
800 if (obj_req->local != -1) {
801 close(obj_req->local); obj_req->local = -1;
804 if (obj_req->state == ABORTED) {
805 ret = error("Request for %s aborted", hex);
806 } else if (obj_req->curl_result != CURLE_OK &&
807 obj_req->http_code != 416) {
808 if (obj_req->http_code == 404 ||
809 obj_req->curl_result == CURLE_FILE_COULDNT_READ_FILE)
810 ret = -1; /* Be silent, it is probably in a pack. */
812 ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
813 obj_req->errorstr, obj_req->curl_result,
814 obj_req->http_code, hex);
815 } else if (obj_req->zret != Z_STREAM_END) {
816 ret = error("File %s (%s) corrupt\n", hex, obj_req->url);
817 } else if (memcmp(obj_req->sha1, obj_req->real_sha1, 20)) {
818 ret = error("File %s has bad hash\n", hex);
819 } else if (obj_req->rename < 0) {
820 ret = error("unable to write sha1 filename %s: %s",
822 strerror(obj_req->rename));
825 release_object_request(obj_req);
829 int fetch(unsigned char *sha1)
831 struct alt_base *altbase = alt;
833 if (!fetch_object(altbase, sha1))
836 if (!fetch_pack(altbase, sha1))
838 fetch_alternates(alt->base);
839 altbase = altbase->next;
841 return error("Unable to find %s under %s\n", sha1_to_hex(sha1),
845 static inline int needs_quote(int ch)
848 case '/': case '-': case '.':
849 case 'A'...'Z': case 'a'...'z': case '0'...'9':
856 static inline int hex(int v)
858 if (v < 10) return '0' + v;
859 else return 'A' + v - 10;
862 static char *quote_ref_url(const char *base, const char *ref)
866 int len, baselen, ch;
868 baselen = strlen(base);
869 len = baselen + 6; /* "refs/" + NUL */
870 for (cp = ref; (ch = *cp) != 0; cp++, len++)
872 len += 2; /* extra two hex plus replacement % */
874 memcpy(qref, base, baselen);
875 memcpy(qref + baselen, "refs/", 5);
876 for (cp = ref, dp = qref + baselen + 5; (ch = *cp) != 0; cp++) {
877 if (needs_quote(ch)) {
879 *dp++ = hex((ch >> 4) & 0xF);
880 *dp++ = hex(ch & 0xF);
890 int fetch_ref(char *ref, unsigned char *sha1)
894 struct buffer buffer;
895 char *base = alt->base;
896 struct active_request_slot *slot;
902 url = quote_ref_url(base, ref);
903 slot = get_active_slot();
904 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
905 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
906 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
907 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
908 if (start_active_slot(slot)) {
909 run_active_slot(slot);
910 if (slot->curl_result != CURLE_OK)
911 return error("Couldn't get %s for %s\n%s",
912 url, ref, curl_errorstr);
914 return error("Unable to start request");
918 get_sha1_hex(hex, sha1);
922 int main(int argc, char **argv)
929 setup_git_directory();
931 while (arg < argc && argv[arg][0] == '-') {
932 if (argv[arg][1] == 't') {
934 } else if (argv[arg][1] == 'c') {
936 } else if (argv[arg][1] == 'a') {
940 } else if (argv[arg][1] == 'v') {
942 } else if (argv[arg][1] == 'w') {
943 write_ref = argv[arg + 1];
945 } else if (!strcmp(argv[arg], "--recover")) {
950 if (argc < arg + 2) {
951 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
954 commit_id = argv[arg];
959 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
961 alt = xmalloc(sizeof(*alt));
963 alt->got_indices = 0;
970 curl_slist_free_all(no_pragma_header);