On Cygwin, use symbolic ref, not a symbolic link, to express .git/HEAD
[git/dscho.git] / http-fetch.c
blob71a8c60b56914ca579db3da1d2bef6795acc7bf9
1 #include "cache.h"
2 #include "commit.h"
3 #include "pack.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 if (verify_pack(target, 0))
435 return -1;
436 install_packed_git(target);
438 return 0;
441 static int fetch_object(struct alt_base *repo, unsigned char *sha1)
443 char *hex = sha1_to_hex(sha1);
444 char *filename = sha1_file_name(sha1);
445 unsigned char real_sha1[20];
446 char tmpfile[PATH_MAX];
447 char prevfile[PATH_MAX];
448 int ret;
449 char *url;
450 char *posn;
451 int prevlocal;
452 unsigned char prev_buf[PREV_BUF_SIZE];
453 ssize_t prev_read = 0;
454 long prev_posn = 0;
455 char range[RANGE_HEADER_SIZE];
456 struct curl_slist *range_header = NULL;
457 CURLcode curl_result;
459 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
460 snprintf(prevfile, sizeof(prevfile), "%s.prev", filename);
462 if (unlink(prevfile) && (errno != ENOENT))
463 return error("Failed to unlink %s (%s)",
464 prevfile, strerror(errno));
465 if (rename(tmpfile, prevfile) && (errno != ENOENT))
466 return error("Failed to rename %s to %s (%s)",
467 tmpfile, prevfile, strerror(errno));
469 local = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
471 /* Note: if another instance starts now, it will turn our new
472 tmpfile into its prevfile. */
474 if (local < 0)
475 return error("Couldn't create temporary file %s for %s: %s\n",
476 tmpfile, filename, strerror(errno));
478 memset(&stream, 0, sizeof(stream));
480 inflateInit(&stream);
482 SHA1_Init(&c);
484 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
485 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
486 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
487 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
488 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
490 url = xmalloc(strlen(repo->base) + 50);
491 strcpy(url, repo->base);
492 posn = url + strlen(repo->base);
493 strcpy(posn, "objects/");
494 posn += 8;
495 memcpy(posn, hex, 2);
496 posn += 2;
497 *(posn++) = '/';
498 strcpy(posn, hex + 2);
500 curl_easy_setopt(curl, CURLOPT_URL, url);
502 /* If a previous temp file is present, process what was already
503 fetched. */
504 prevlocal = open(prevfile, O_RDONLY);
505 if (prevlocal != -1) {
506 do {
507 prev_read = read(prevlocal, prev_buf, PREV_BUF_SIZE);
508 if (prev_read>0) {
509 if (fwrite_sha1_file(prev_buf,
511 prev_read,
512 NULL) == prev_read) {
513 prev_posn += prev_read;
514 } else {
515 prev_read = -1;
518 } while (prev_read > 0);
519 close(prevlocal);
521 unlink(prevfile);
523 /* Reset inflate/SHA1 if there was an error reading the previous temp
524 file; also rewind to the beginning of the local file. */
525 if (prev_read == -1) {
526 memset(&stream, 0, sizeof(stream));
527 inflateInit(&stream);
528 SHA1_Init(&c);
529 if (prev_posn>0) {
530 prev_posn = 0;
531 lseek(local, SEEK_SET, 0);
532 ftruncate(local, 0);
536 /* If we have successfully processed data from a previous fetch
537 attempt, only fetch the data we don't already have. */
538 if (prev_posn>0) {
539 if (get_verbosely)
540 fprintf(stderr,
541 "Resuming fetch of object %s at byte %ld\n",
542 hex, prev_posn);
543 sprintf(range, "Range: bytes=%ld-", prev_posn);
544 range_header = curl_slist_append(range_header, range);
545 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, range_header);
548 /* Clear out the Range: header after performing the request, so
549 other curl requests don't inherit inappropriate header data */
550 curl_result = curl_easy_perform(curl);
551 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_range_header);
552 if (curl_result != 0) {
553 return error("%s", curl_errorstr);
556 fchmod(local, 0444);
557 close(local);
558 inflateEnd(&stream);
559 SHA1_Final(real_sha1, &c);
560 if (zret != Z_STREAM_END) {
561 unlink(tmpfile);
562 return error("File %s (%s) corrupt\n", hex, url);
564 if (memcmp(sha1, real_sha1, 20)) {
565 unlink(tmpfile);
566 return error("File %s has bad hash\n", hex);
568 ret = relink_or_rename(tmpfile, filename);
569 if (ret)
570 return error("unable to write sha1 filename %s: %s",
571 filename, strerror(ret));
573 pull_say("got %s\n", hex);
574 return 0;
577 int fetch(unsigned char *sha1)
579 struct alt_base *altbase = alt;
580 while (altbase) {
581 if (!fetch_object(altbase, sha1))
582 return 0;
583 if (!fetch_pack(altbase, sha1))
584 return 0;
585 if (fetch_alternates(altbase->base) > 0) {
586 altbase = alt;
587 continue;
589 altbase = altbase->next;
591 return error("Unable to find %s under %s\n", sha1_to_hex(sha1),
592 initial_base);
595 int fetch_ref(char *ref, unsigned char *sha1)
597 char *url, *posn;
598 char hex[42];
599 struct buffer buffer;
600 char *base = initial_base;
601 buffer.size = 41;
602 buffer.posn = 0;
603 buffer.buffer = hex;
604 hex[41] = '\0';
606 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
607 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
608 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
609 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
611 url = xmalloc(strlen(base) + 6 + strlen(ref));
612 strcpy(url, base);
613 posn = url + strlen(base);
614 strcpy(posn, "refs/");
615 posn += 5;
616 strcpy(posn, ref);
618 curl_easy_setopt(curl, CURLOPT_URL, url);
620 if (curl_easy_perform(curl))
621 return error("Couldn't get %s for %s\n%s",
622 url, ref, curl_errorstr);
624 hex[40] = '\0';
625 get_sha1_hex(hex, sha1);
626 return 0;
629 int main(int argc, char **argv)
631 char *commit_id;
632 char *url;
633 int arg = 1;
635 while (arg < argc && argv[arg][0] == '-') {
636 if (argv[arg][1] == 't') {
637 get_tree = 1;
638 } else if (argv[arg][1] == 'c') {
639 get_history = 1;
640 } else if (argv[arg][1] == 'a') {
641 get_all = 1;
642 get_tree = 1;
643 get_history = 1;
644 } else if (argv[arg][1] == 'v') {
645 get_verbosely = 1;
646 } else if (argv[arg][1] == 'w') {
647 write_ref = argv[arg + 1];
648 arg++;
649 } else if (!strcmp(argv[arg], "--recover")) {
650 get_recover = 1;
652 arg++;
654 if (argc < arg + 2) {
655 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
656 return 1;
658 commit_id = argv[arg];
659 url = argv[arg + 1];
661 curl_global_init(CURL_GLOBAL_ALL);
663 curl = curl_easy_init();
664 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
665 no_range_header = curl_slist_append(no_range_header, "Range:");
667 curl_ssl_verify = getenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
668 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
669 #if LIBCURL_VERSION_NUM >= 0x070907
670 curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
671 #endif
673 if ((ssl_cert = getenv("GIT_SSL_CERT")) != NULL) {
674 curl_easy_setopt(curl, CURLOPT_SSLCERT, ssl_cert);
676 #if LIBCURL_VERSION_NUM >= 0x070902
677 if ((ssl_key = getenv("GIT_SSL_KEY")) != NULL) {
678 curl_easy_setopt(curl, CURLOPT_SSLKEY, ssl_key);
680 #endif
681 #if LIBCURL_VERSION_NUM >= 0x070908
682 if ((ssl_capath = getenv("GIT_SSL_CAPATH")) != NULL) {
683 curl_easy_setopt(curl, CURLOPT_CAPATH, ssl_capath);
685 #endif
686 if ((ssl_cainfo = getenv("GIT_SSL_CAINFO")) != NULL) {
687 curl_easy_setopt(curl, CURLOPT_CAINFO, ssl_cainfo);
690 alt = xmalloc(sizeof(*alt));
691 alt->base = url;
692 alt->got_indices = 0;
693 alt->packs = NULL;
694 alt->next = NULL;
695 initial_base = url;
697 if (pull(commit_id))
698 return 1;
700 curl_slist_free_all(no_pragma_header);
701 curl_global_cleanup();
702 return 0;