[PATCH] HTTP partial transfer support for object, pack, and index transfers
[git/jrn.git] / http-fetch.c
blob778d508243e78bc5c3dce2d71ab6c225c6a3159f
1 #include "cache.h"
2 #include "commit.h"
4 #include "fetch.h"
6 #include <curl/curl.h>
7 #include <curl/easy.h>
9 #if LIBCURL_VERSION_NUM < 0x070704
10 #define curl_global_cleanup() do { /* nothing */ } while(0)
11 #endif
12 #if LIBCURL_VERSION_NUM < 0x070800
13 #define curl_global_init(a) do { /* nothing */ } while(0)
14 #endif
16 #define PREV_BUF_SIZE 4096
17 #define RANGE_HEADER_SIZE 30
19 static CURL *curl;
20 static struct curl_slist *no_pragma_header;
21 static struct curl_slist *no_range_header;
22 static char curl_errorstr[CURL_ERROR_SIZE];
24 static char *initial_base;
26 struct alt_base
28 char *base;
29 int got_indices;
30 struct packed_git *packs;
31 struct alt_base *next;
34 static struct alt_base *alt = NULL;
36 static SHA_CTX c;
37 static z_stream stream;
39 static int local;
40 static int zret;
42 static int curl_ssl_verify;
43 static char *ssl_cert;
44 static char *ssl_key;
45 static char *ssl_capath;
46 static char *ssl_cainfo;
48 struct buffer
50 size_t posn;
51 size_t size;
52 void *buffer;
55 static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
56 struct buffer *buffer)
58 size_t size = eltsize * nmemb;
59 if (size > buffer->size - buffer->posn)
60 size = buffer->size - buffer->posn;
61 memcpy(buffer->buffer + buffer->posn, ptr, size);
62 buffer->posn += size;
63 return size;
66 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
67 void *data)
69 unsigned char expn[4096];
70 size_t size = eltsize * nmemb;
71 int posn = 0;
72 do {
73 ssize_t retval = write(local, ptr + posn, size - posn);
74 if (retval < 0)
75 return posn;
76 posn += retval;
77 } while (posn < size);
79 stream.avail_in = size;
80 stream.next_in = ptr;
81 do {
82 stream.next_out = expn;
83 stream.avail_out = sizeof(expn);
84 zret = inflate(&stream, Z_SYNC_FLUSH);
85 SHA1_Update(&c, expn, sizeof(expn) - stream.avail_out);
86 } while (stream.avail_in && zret == Z_OK);
87 return size;
90 void prefetch(unsigned char *sha1)
94 int relink_or_rename(char *old, char *new) {
95 int ret;
97 ret = link(old, new);
98 if (ret < 0) {
99 /* Same Coda hack as in write_sha1_file(sha1_file.c) */
100 ret = errno;
101 if (ret == EXDEV && !rename(old, new))
102 return 0;
104 unlink(old);
105 if (ret) {
106 if (ret != EEXIST)
107 return ret;
110 return 0;
113 static int got_alternates = 0;
115 static int fetch_index(struct alt_base *repo, unsigned char *sha1)
117 char *filename;
118 char *url;
119 char tmpfile[PATH_MAX];
120 int ret;
121 long prev_posn = 0;
122 char range[RANGE_HEADER_SIZE];
123 struct curl_slist *range_header = NULL;
124 CURLcode curl_result;
126 FILE *indexfile;
128 if (has_pack_index(sha1))
129 return 0;
131 if (get_verbosely)
132 fprintf(stderr, "Getting index for pack %s\n",
133 sha1_to_hex(sha1));
135 url = xmalloc(strlen(repo->base) + 64);
136 sprintf(url, "%s/objects/pack/pack-%s.idx",
137 repo->base, sha1_to_hex(sha1));
139 filename = sha1_pack_index_name(sha1);
140 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
141 indexfile = fopen(tmpfile, "a");
142 if (!indexfile)
143 return error("Unable to open local file %s for pack index",
144 filename);
146 curl_easy_setopt(curl, CURLOPT_FILE, indexfile);
147 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
148 curl_easy_setopt(curl, CURLOPT_URL, url);
149 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
150 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
152 /* If there is data present from a previous transfer attempt,
153 resume where it left off */
154 prev_posn = ftell(indexfile);
155 if (prev_posn>0) {
156 if (get_verbosely)
157 fprintf(stderr,
158 "Resuming fetch of index for pack %s at byte %ld\n",
159 sha1_to_hex(sha1), prev_posn);
160 sprintf(range, "Range: bytes=%ld-", prev_posn);
161 range_header = curl_slist_append(range_header, range);
162 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, range_header);
165 /* Clear out the Range: header after performing the request, so
166 other curl requests don't inherit inappropriate header data */
167 curl_result = curl_easy_perform(curl);
168 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_range_header);
169 if (curl_result != 0) {
170 fclose(indexfile);
171 return error("Unable to get pack index %s\n%s", url,
172 curl_errorstr);
175 fclose(indexfile);
177 ret = relink_or_rename(tmpfile, filename);
178 if (ret)
179 return error("unable to write index filename %s: %s",
180 filename, strerror(ret));
182 return 0;
185 static int setup_index(struct alt_base *repo, unsigned char *sha1)
187 struct packed_git *new_pack;
188 if (has_pack_file(sha1))
189 return 0; // don't list this as something we can get
191 if (fetch_index(repo, sha1))
192 return -1;
194 new_pack = parse_pack_index(sha1);
195 new_pack->next = repo->packs;
196 repo->packs = new_pack;
197 return 0;
200 static int fetch_alternates(char *base)
202 int ret = 0;
203 struct buffer buffer;
204 char *url;
205 char *data;
206 int i = 0;
207 int http_specific = 1;
208 if (got_alternates)
209 return 0;
210 data = xmalloc(4096);
211 buffer.size = 4095;
212 buffer.posn = 0;
213 buffer.buffer = data;
215 if (get_verbosely)
216 fprintf(stderr, "Getting alternates list\n");
218 url = xmalloc(strlen(base) + 31);
219 sprintf(url, "%s/objects/info/http-alternates", base);
221 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
222 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
223 curl_easy_setopt(curl, CURLOPT_URL, url);
225 if (curl_easy_perform(curl) || !buffer.posn) {
226 http_specific = 0;
228 sprintf(url, "%s/objects/info/alternates", base);
230 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
231 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
232 curl_easy_setopt(curl, CURLOPT_URL, url);
234 if (curl_easy_perform(curl)) {
235 return 0;
239 data[buffer.posn] = '\0';
241 while (i < buffer.posn) {
242 int posn = i;
243 while (posn < buffer.posn && data[posn] != '\n')
244 posn++;
245 if (data[posn] == '\n') {
246 int okay = 0;
247 int serverlen = 0;
248 struct alt_base *newalt;
249 char *target = NULL;
250 if (data[i] == '/') {
251 serverlen = strchr(base + 8, '/') - base;
252 okay = 1;
253 } else if (!memcmp(data + i, "../", 3)) {
254 i += 3;
255 serverlen = strlen(base);
256 while (i + 2 < posn &&
257 !memcmp(data + i, "../", 3)) {
258 do {
259 serverlen--;
260 } while (serverlen &&
261 base[serverlen - 1] != '/');
262 i += 3;
264 // If the server got removed, give up.
265 okay = strchr(base, ':') - base + 3 <
266 serverlen;
267 } else if (http_specific) {
268 char *colon = strchr(data + i, ':');
269 char *slash = strchr(data + i, '/');
270 if (colon && slash && colon < data + posn &&
271 slash < data + posn && colon < slash) {
272 okay = 1;
275 // skip 'objects' at end
276 if (okay) {
277 target = xmalloc(serverlen + posn - i - 6);
278 strncpy(target, base, serverlen);
279 strncpy(target + serverlen, data + i,
280 posn - i - 7);
281 target[serverlen + posn - i - 7] = '\0';
282 if (get_verbosely)
283 fprintf(stderr,
284 "Also look at %s\n", target);
285 newalt = xmalloc(sizeof(*newalt));
286 newalt->next = alt;
287 newalt->base = target;
288 newalt->got_indices = 0;
289 newalt->packs = NULL;
290 alt = newalt;
291 ret++;
294 i = posn + 1;
296 got_alternates = 1;
298 return ret;
301 static int fetch_indices(struct alt_base *repo)
303 unsigned char sha1[20];
304 char *url;
305 struct buffer buffer;
306 char *data;
307 int i = 0;
309 if (repo->got_indices)
310 return 0;
312 data = xmalloc(4096);
313 buffer.size = 4096;
314 buffer.posn = 0;
315 buffer.buffer = data;
317 if (get_verbosely)
318 fprintf(stderr, "Getting pack list\n");
320 url = xmalloc(strlen(repo->base) + 21);
321 sprintf(url, "%s/objects/info/packs", repo->base);
323 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
324 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
325 curl_easy_setopt(curl, CURLOPT_URL, url);
326 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
327 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
329 if (curl_easy_perform(curl))
330 return error("%s", curl_errorstr);
332 while (i < buffer.posn) {
333 switch (data[i]) {
334 case 'P':
335 i++;
336 if (i + 52 < buffer.posn &&
337 !strncmp(data + i, " pack-", 6) &&
338 !strncmp(data + i + 46, ".pack\n", 6)) {
339 get_sha1_hex(data + i + 6, sha1);
340 setup_index(repo, sha1);
341 i += 51;
342 break;
344 default:
345 while (data[i] != '\n')
346 i++;
348 i++;
351 repo->got_indices = 1;
352 return 0;
355 static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
357 char *url;
358 struct packed_git *target;
359 struct packed_git **lst;
360 FILE *packfile;
361 char *filename;
362 char tmpfile[PATH_MAX];
363 int ret;
364 long prev_posn = 0;
365 char range[RANGE_HEADER_SIZE];
366 struct curl_slist *range_header = NULL;
367 CURLcode curl_result;
369 if (fetch_indices(repo))
370 return -1;
371 target = find_sha1_pack(sha1, repo->packs);
372 if (!target)
373 return -1;
375 if (get_verbosely) {
376 fprintf(stderr, "Getting pack %s\n",
377 sha1_to_hex(target->sha1));
378 fprintf(stderr, " which contains %s\n",
379 sha1_to_hex(sha1));
382 url = xmalloc(strlen(repo->base) + 65);
383 sprintf(url, "%s/objects/pack/pack-%s.pack",
384 repo->base, sha1_to_hex(target->sha1));
386 filename = sha1_pack_name(target->sha1);
387 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
388 packfile = fopen(tmpfile, "a");
389 if (!packfile)
390 return error("Unable to open local file %s for pack",
391 filename);
393 curl_easy_setopt(curl, CURLOPT_FILE, packfile);
394 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
395 curl_easy_setopt(curl, CURLOPT_URL, url);
396 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
397 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
399 /* If there is data present from a previous transfer attempt,
400 resume where it left off */
401 prev_posn = ftell(packfile);
402 if (prev_posn>0) {
403 if (get_verbosely)
404 fprintf(stderr,
405 "Resuming fetch of pack %s at byte %ld\n",
406 sha1_to_hex(target->sha1), prev_posn);
407 sprintf(range, "Range: bytes=%ld-", prev_posn);
408 range_header = curl_slist_append(range_header, range);
409 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, range_header);
412 /* Clear out the Range: header after performing the request, so
413 other curl requests don't inherit inappropriate header data */
414 curl_result = curl_easy_perform(curl);
415 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_range_header);
416 if (curl_result != 0) {
417 fclose(packfile);
418 return error("Unable to get pack file %s\n%s", url,
419 curl_errorstr);
422 fclose(packfile);
424 ret = relink_or_rename(tmpfile, filename);
425 if (ret)
426 return error("unable to write pack filename %s: %s",
427 filename, strerror(ret));
429 lst = &repo->packs;
430 while (*lst != target)
431 lst = &((*lst)->next);
432 *lst = (*lst)->next;
434 install_packed_git(target);
436 return 0;
439 static int fetch_object(struct alt_base *repo, unsigned char *sha1)
441 char *hex = sha1_to_hex(sha1);
442 char *filename = sha1_file_name(sha1);
443 unsigned char real_sha1[20];
444 char tmpfile[PATH_MAX];
445 char prevfile[PATH_MAX];
446 int ret;
447 char *url;
448 char *posn;
449 int prevlocal;
450 unsigned char prev_buf[PREV_BUF_SIZE];
451 ssize_t prev_read = 0;
452 long prev_posn = 0;
453 char range[RANGE_HEADER_SIZE];
454 struct curl_slist *range_header = NULL;
455 CURLcode curl_result;
457 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
458 snprintf(prevfile, sizeof(prevfile), "%s.prev", filename);
459 unlink(prevfile);
460 rename(tmpfile, prevfile);
461 unlink(tmpfile);
463 local = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
465 /* Note: if another instance starts now, it will turn our new
466 tmpfile into its prevfile. */
468 if (local < 0)
469 return error("Couldn't create temporary file %s for %s: %s\n",
470 tmpfile, filename, strerror(errno));
472 memset(&stream, 0, sizeof(stream));
474 inflateInit(&stream);
476 SHA1_Init(&c);
478 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
479 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
480 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
481 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
482 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
484 url = xmalloc(strlen(repo->base) + 50);
485 strcpy(url, repo->base);
486 posn = url + strlen(repo->base);
487 strcpy(posn, "objects/");
488 posn += 8;
489 memcpy(posn, hex, 2);
490 posn += 2;
491 *(posn++) = '/';
492 strcpy(posn, hex + 2);
494 curl_easy_setopt(curl, CURLOPT_URL, url);
496 /* If a previous temp file is present, process what was already
497 fetched. */
498 prevlocal = open(prevfile, O_RDONLY);
499 if (prevlocal != -1) {
500 do {
501 prev_read = read(prevlocal, prev_buf, PREV_BUF_SIZE);
502 if (prev_read>0) {
503 if (fwrite_sha1_file(prev_buf,
505 prev_read,
506 NULL) == prev_read) {
507 prev_posn += prev_read;
508 } else {
509 prev_read = -1;
512 } while (prev_read > 0);
513 close(prevlocal);
515 unlink(prevfile);
517 /* Reset inflate/SHA1 if there was an error reading the previous temp
518 file; also rewind to the beginning of the local file. */
519 if (prev_read == -1) {
520 memset(&stream, 0, sizeof(stream));
521 inflateInit(&stream);
522 SHA1_Init(&c);
523 if (prev_posn>0) {
524 prev_posn = 0;
525 lseek(local, SEEK_SET, 0);
529 /* If we have successfully processed data from a previous fetch
530 attempt, only fetch the data we don't already have. */
531 if (prev_posn>0) {
532 if (get_verbosely)
533 fprintf(stderr,
534 "Resuming fetch of object %s at byte %ld\n",
535 hex, prev_posn);
536 sprintf(range, "Range: bytes=%ld-", prev_posn);
537 range_header = curl_slist_append(range_header, range);
538 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, range_header);
541 /* Clear out the Range: header after performing the request, so
542 other curl requests don't inherit inappropriate header data */
543 curl_result = curl_easy_perform(curl);
544 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_range_header);
545 if (curl_result != 0) {
546 unlink(tmpfile);
547 return error("%s", curl_errorstr);
550 fchmod(local, 0444);
551 close(local);
552 inflateEnd(&stream);
553 SHA1_Final(real_sha1, &c);
554 if (zret != Z_STREAM_END) {
555 unlink(tmpfile);
556 return error("File %s (%s) corrupt\n", hex, url);
558 if (memcmp(sha1, real_sha1, 20)) {
559 unlink(tmpfile);
560 return error("File %s has bad hash\n", hex);
562 ret = relink_or_rename(tmpfile, filename);
563 if (ret)
564 return error("unable to write sha1 filename %s: %s",
565 filename, strerror(ret));
567 pull_say("got %s\n", hex);
568 return 0;
571 int fetch(unsigned char *sha1)
573 struct alt_base *altbase = alt;
574 while (altbase) {
575 if (!fetch_object(altbase, sha1))
576 return 0;
577 if (!fetch_pack(altbase, sha1))
578 return 0;
579 if (fetch_alternates(altbase->base) > 0) {
580 altbase = alt;
581 continue;
583 altbase = altbase->next;
585 return error("Unable to find %s under %s\n", sha1_to_hex(sha1),
586 initial_base);
589 int fetch_ref(char *ref, unsigned char *sha1)
591 char *url, *posn;
592 char hex[42];
593 struct buffer buffer;
594 char *base = initial_base;
595 buffer.size = 41;
596 buffer.posn = 0;
597 buffer.buffer = hex;
598 hex[41] = '\0';
600 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
601 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
602 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
603 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
605 url = xmalloc(strlen(base) + 6 + strlen(ref));
606 strcpy(url, base);
607 posn = url + strlen(base);
608 strcpy(posn, "refs/");
609 posn += 5;
610 strcpy(posn, ref);
612 curl_easy_setopt(curl, CURLOPT_URL, url);
614 if (curl_easy_perform(curl))
615 return error("Couldn't get %s for %s\n%s",
616 url, ref, curl_errorstr);
618 hex[40] = '\0';
619 get_sha1_hex(hex, sha1);
620 return 0;
623 int main(int argc, char **argv)
625 char *commit_id;
626 char *url;
627 int arg = 1;
629 while (arg < argc && argv[arg][0] == '-') {
630 if (argv[arg][1] == 't') {
631 get_tree = 1;
632 } else if (argv[arg][1] == 'c') {
633 get_history = 1;
634 } else if (argv[arg][1] == 'a') {
635 get_all = 1;
636 get_tree = 1;
637 get_history = 1;
638 } else if (argv[arg][1] == 'v') {
639 get_verbosely = 1;
640 } else if (argv[arg][1] == 'w') {
641 write_ref = argv[arg + 1];
642 arg++;
643 } else if (!strcmp(argv[arg], "--recover")) {
644 get_recover = 1;
646 arg++;
648 if (argc < arg + 2) {
649 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
650 return 1;
652 commit_id = argv[arg];
653 url = argv[arg + 1];
655 curl_global_init(CURL_GLOBAL_ALL);
657 curl = curl_easy_init();
658 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
659 no_range_header = curl_slist_append(no_range_header, "Range:");
661 curl_ssl_verify = getenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
662 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
663 #if LIBCURL_VERSION_NUM >= 0x070907
664 curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
665 #endif
667 if ((ssl_cert = getenv("GIT_SSL_CERT")) != NULL) {
668 curl_easy_setopt(curl, CURLOPT_SSLCERT, ssl_cert);
670 #if LIBCURL_VERSION_NUM >= 0x070902
671 if ((ssl_key = getenv("GIT_SSL_KEY")) != NULL) {
672 curl_easy_setopt(curl, CURLOPT_SSLKEY, ssl_key);
674 #endif
675 #if LIBCURL_VERSION_NUM >= 0x070908
676 if ((ssl_capath = getenv("GIT_SSL_CAPATH")) != NULL) {
677 curl_easy_setopt(curl, CURLOPT_CAPATH, ssl_capath);
679 #endif
680 if ((ssl_cainfo = getenv("GIT_SSL_CAINFO")) != NULL) {
681 curl_easy_setopt(curl, CURLOPT_CAINFO, ssl_cainfo);
684 alt = xmalloc(sizeof(*alt));
685 alt->base = url;
686 alt->got_indices = 0;
687 alt->packs = NULL;
688 alt->next = NULL;
689 initial_base = url;
691 if (pull(commit_id))
692 return 1;
694 curl_slist_free_all(no_pragma_header);
695 curl_global_cleanup();
696 return 0;