Documentation/git-verify-pack.txt: added documentation for --.
[git/dscho.git] / http-fetch.c
blobad59f1cce61d73fa832828aabb880f082debf94d
1 #include "cache.h"
2 #include "commit.h"
3 #include "pack.h"
4 #include "fetch.h"
5 #include "http.h"
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;
14 struct alt_base
16 char *base;
17 int got_indices;
18 struct packed_git *packs;
19 struct alt_base *next;
22 static struct alt_base *alt = NULL;
24 enum object_request_state {
25 WAITING,
26 ABORTED,
27 ACTIVE,
28 COMPLETE,
31 struct object_request
33 unsigned char sha1[20];
34 struct alt_base *repo;
35 char *url;
36 char filename[PATH_MAX];
37 char tmpfile[PATH_MAX];
38 int local;
39 enum object_request_state state;
40 CURLcode curl_result;
41 char errorstr[CURL_ERROR_SIZE];
42 long http_code;
43 unsigned char real_sha1[20];
44 SHA_CTX c;
45 z_stream stream;
46 int zret;
47 int rename;
48 struct active_request_slot *slot;
49 struct object_request *next;
52 struct alternates_request {
53 char *base;
54 char *url;
55 struct buffer *buffer;
56 struct active_request_slot *slot;
57 int http_specific;
60 static struct object_request *object_queue_head = NULL;
62 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
63 void *data)
65 unsigned char expn[4096];
66 size_t size = eltsize * nmemb;
67 int posn = 0;
68 struct object_request *obj_req = (struct object_request *)data;
69 do {
70 ssize_t retval = write(obj_req->local,
71 ptr + posn, size - posn);
72 if (retval < 0)
73 return posn;
74 posn += retval;
75 } while (posn < size);
77 obj_req->stream.avail_in = size;
78 obj_req->stream.next_in = ptr;
79 do {
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);
86 data_received++;
87 return size;
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];
98 char *url;
99 char *posn;
100 int prevlocal;
101 unsigned char prev_buf[PREV_BUF_SIZE];
102 ssize_t prev_read = 0;
103 long prev_posn = 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);
109 unlink(prevfile);
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, '/');
122 if (dir) {
123 *dir = 0;
124 mkdir(obj_req->tmpfile, 0777);
125 *dir = '/';
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));
135 return;
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/");
149 posn += 8;
150 memcpy(posn, hex, 2);
151 posn += 2;
152 *(posn++) = '/';
153 strcpy(posn, hex + 2);
154 strcpy(obj_req->url, url);
156 /* If a previous temp file is present, process what was already
157 fetched. */
158 prevlocal = open(prevfile, O_RDONLY);
159 if (prevlocal != -1) {
160 do {
161 prev_read = read(prevlocal, prev_buf, PREV_BUF_SIZE);
162 if (prev_read>0) {
163 if (fwrite_sha1_file(prev_buf,
165 prev_read,
166 obj_req) == prev_read) {
167 prev_posn += prev_read;
168 } else {
169 prev_read = -1;
172 } while (prev_read > 0);
173 close(prevlocal);
175 unlink(prevfile);
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);
183 if (prev_posn>0) {
184 prev_posn = 0;
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. */
203 if (prev_posn>0) {
204 if (get_verbosely)
205 fprintf(stderr,
206 "Resuming fetch of object %s at byte %ld\n",
207 hex, prev_posn);
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;
220 free(obj_req->url);
221 return;
226 static void finish_object_request(struct object_request *obj_req)
228 struct stat st;
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)
237 if (st.st_size == 0)
238 unlink(obj_req->tmpfile);
239 return;
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);
246 return;
248 if (memcmp(obj_req->sha1, obj_req->real_sha1, 20)) {
249 unlink(obj_req->tmpfile);
250 return;
252 obj_req->rename =
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 fetch_alternates(alt->base);
272 if (obj_req->repo->next != NULL) {
273 obj_req->repo =
274 obj_req->repo->next;
275 close(obj_req->local);
276 obj_req->local = -1;
277 start_object_request(obj_req);
278 return;
282 finish_object_request(obj_req);
285 static void release_object_request(struct object_request *obj_req)
287 struct object_request *entry = object_queue_head;
289 if (obj_req->local != -1)
290 error("fd leakage in release: %d", obj_req->local);
291 if (obj_req == object_queue_head) {
292 object_queue_head = obj_req->next;
293 } else {
294 while (entry->next != NULL && entry->next != obj_req)
295 entry = entry->next;
296 if (entry->next == obj_req)
297 entry->next = entry->next->next;
300 free(obj_req->url);
301 free(obj_req);
304 #ifdef USE_CURL_MULTI
305 void fill_active_slots(void)
307 struct object_request *obj_req = object_queue_head;
308 struct active_request_slot *slot = active_queue_head;
309 int num_transfers;
311 while (active_requests < max_requests && obj_req != NULL) {
312 if (obj_req->state == WAITING) {
313 if (has_sha1_file(obj_req->sha1))
314 release_object_request(obj_req);
315 else
316 start_object_request(obj_req);
317 curl_multi_perform(curlm, &num_transfers);
319 obj_req = obj_req->next;
322 while (slot != NULL) {
323 if (!slot->in_use && slot->curl != NULL) {
324 curl_easy_cleanup(slot->curl);
325 slot->curl = NULL;
327 slot = slot->next;
330 #endif
332 void prefetch(unsigned char *sha1)
334 struct object_request *newreq;
335 struct object_request *tail;
336 char *filename = sha1_file_name(sha1);
338 newreq = xmalloc(sizeof(*newreq));
339 memcpy(newreq->sha1, sha1, 20);
340 newreq->repo = alt;
341 newreq->url = NULL;
342 newreq->local = -1;
343 newreq->state = WAITING;
344 snprintf(newreq->filename, sizeof(newreq->filename), "%s", filename);
345 snprintf(newreq->tmpfile, sizeof(newreq->tmpfile),
346 "%s.temp", filename);
347 newreq->next = NULL;
349 if (object_queue_head == NULL) {
350 object_queue_head = newreq;
351 } else {
352 tail = object_queue_head;
353 while (tail->next != NULL) {
354 tail = tail->next;
356 tail->next = newreq;
359 #ifdef USE_CURL_MULTI
360 fill_active_slots();
361 step_active_slots();
362 #endif
365 static int fetch_index(struct alt_base *repo, unsigned char *sha1)
367 char *hex = sha1_to_hex(sha1);
368 char *filename;
369 char *url;
370 char tmpfile[PATH_MAX];
371 long prev_posn = 0;
372 char range[RANGE_HEADER_SIZE];
373 struct curl_slist *range_header = NULL;
375 FILE *indexfile;
376 struct active_request_slot *slot;
378 if (has_pack_index(sha1))
379 return 0;
381 if (get_verbosely)
382 fprintf(stderr, "Getting index for pack %s\n", hex);
384 url = xmalloc(strlen(repo->base) + 64);
385 sprintf(url, "%s/objects/pack/pack-%s.idx", repo->base, hex);
387 filename = sha1_pack_index_name(sha1);
388 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
389 indexfile = fopen(tmpfile, "a");
390 if (!indexfile)
391 return error("Unable to open local file %s for pack index",
392 filename);
394 slot = get_active_slot();
395 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
396 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
397 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
398 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
399 slot->local = indexfile;
401 /* If there is data present from a previous transfer attempt,
402 resume where it left off */
403 prev_posn = ftell(indexfile);
404 if (prev_posn>0) {
405 if (get_verbosely)
406 fprintf(stderr,
407 "Resuming fetch of index for pack %s at byte %ld\n",
408 hex, prev_posn);
409 sprintf(range, "Range: bytes=%ld-", prev_posn);
410 range_header = curl_slist_append(range_header, range);
411 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
414 if (start_active_slot(slot)) {
415 run_active_slot(slot);
416 if (slot->curl_result != CURLE_OK) {
417 fclose(indexfile);
418 return error("Unable to get pack index %s\n%s", url,
419 curl_errorstr);
421 } else {
422 fclose(indexfile);
423 return error("Unable to start request");
426 fclose(indexfile);
428 return move_temp_to_file(tmpfile, filename);
431 static int setup_index(struct alt_base *repo, unsigned char *sha1)
433 struct packed_git *new_pack;
434 if (has_pack_file(sha1))
435 return 0; // don't list this as something we can get
437 if (fetch_index(repo, sha1))
438 return -1;
440 new_pack = parse_pack_index(sha1);
441 new_pack->next = repo->packs;
442 repo->packs = new_pack;
443 return 0;
446 static void process_alternates_response(void *callback_data)
448 struct alternates_request *alt_req =
449 (struct alternates_request *)callback_data;
450 struct active_request_slot *slot = alt_req->slot;
451 struct alt_base *tail = alt;
452 char *base = alt_req->base;
453 static const char null_byte = '\0';
454 char *data;
455 int i = 0;
457 if (alt_req->http_specific) {
458 if (slot->curl_result != CURLE_OK ||
459 !alt_req->buffer->posn) {
461 /* Try reusing the slot to get non-http alternates */
462 alt_req->http_specific = 0;
463 sprintf(alt_req->url, "%s/objects/info/alternates",
464 base);
465 curl_easy_setopt(slot->curl, CURLOPT_URL,
466 alt_req->url);
467 active_requests++;
468 slot->in_use = 1;
469 if (start_active_slot(slot)) {
470 return;
471 } else {
472 got_alternates = -1;
473 slot->in_use = 0;
474 return;
477 } else if (slot->curl_result != CURLE_OK) {
478 if (slot->http_code != 404) {
479 got_alternates = -1;
480 return;
484 fwrite_buffer(&null_byte, 1, 1, alt_req->buffer);
485 alt_req->buffer->posn--;
486 data = alt_req->buffer->buffer;
488 while (i < alt_req->buffer->posn) {
489 int posn = i;
490 while (posn < alt_req->buffer->posn && data[posn] != '\n')
491 posn++;
492 if (data[posn] == '\n') {
493 int okay = 0;
494 int serverlen = 0;
495 struct alt_base *newalt;
496 char *target = NULL;
497 if (data[i] == '/') {
498 serverlen = strchr(base + 8, '/') - base;
499 okay = 1;
500 } else if (!memcmp(data + i, "../", 3)) {
501 i += 3;
502 serverlen = strlen(base);
503 while (i + 2 < posn &&
504 !memcmp(data + i, "../", 3)) {
505 do {
506 serverlen--;
507 } while (serverlen &&
508 base[serverlen - 1] != '/');
509 i += 3;
511 // If the server got removed, give up.
512 okay = strchr(base, ':') - base + 3 <
513 serverlen;
514 } else if (alt_req->http_specific) {
515 char *colon = strchr(data + i, ':');
516 char *slash = strchr(data + i, '/');
517 if (colon && slash && colon < data + posn &&
518 slash < data + posn && colon < slash) {
519 okay = 1;
522 // skip 'objects' at end
523 if (okay) {
524 target = xmalloc(serverlen + posn - i - 6);
525 strncpy(target, base, serverlen);
526 strncpy(target + serverlen, data + i,
527 posn - i - 7);
528 target[serverlen + posn - i - 7] = '\0';
529 if (get_verbosely)
530 fprintf(stderr,
531 "Also look at %s\n", target);
532 newalt = xmalloc(sizeof(*newalt));
533 newalt->next = NULL;
534 newalt->base = target;
535 newalt->got_indices = 0;
536 newalt->packs = NULL;
537 while (tail->next != NULL)
538 tail = tail->next;
539 tail->next = newalt;
542 i = posn + 1;
545 got_alternates = 1;
548 static void fetch_alternates(char *base)
550 struct buffer buffer;
551 char *url;
552 char *data;
553 struct active_request_slot *slot;
554 static struct alternates_request alt_req;
556 /* If another request has already started fetching alternates,
557 wait for them to arrive and return to processing this request's
558 curl message */
559 #ifdef USE_CURL_MULTI
560 while (got_alternates == 0) {
561 step_active_slots();
563 #endif
565 /* Nothing to do if they've already been fetched */
566 if (got_alternates == 1)
567 return;
569 /* Start the fetch */
570 got_alternates = 0;
572 data = xmalloc(4096);
573 buffer.size = 4096;
574 buffer.posn = 0;
575 buffer.buffer = data;
577 if (get_verbosely)
578 fprintf(stderr, "Getting alternates list for %s\n", base);
580 url = xmalloc(strlen(base) + 31);
581 sprintf(url, "%s/objects/info/http-alternates", base);
583 /* Use a callback to process the result, since another request
584 may fail and need to have alternates loaded before continuing */
585 slot = get_active_slot();
586 slot->callback_func = process_alternates_response;
587 slot->callback_data = &alt_req;
589 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
590 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
591 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
593 alt_req.base = base;
594 alt_req.url = url;
595 alt_req.buffer = &buffer;
596 alt_req.http_specific = 1;
597 alt_req.slot = slot;
599 if (start_active_slot(slot))
600 run_active_slot(slot);
601 else
602 got_alternates = -1;
604 free(data);
605 free(url);
608 static int fetch_indices(struct alt_base *repo)
610 unsigned char sha1[20];
611 char *url;
612 struct buffer buffer;
613 char *data;
614 int i = 0;
616 struct active_request_slot *slot;
618 if (repo->got_indices)
619 return 0;
621 data = xmalloc(4096);
622 buffer.size = 4096;
623 buffer.posn = 0;
624 buffer.buffer = data;
626 if (get_verbosely)
627 fprintf(stderr, "Getting pack list for %s\n", repo->base);
629 url = xmalloc(strlen(repo->base) + 21);
630 sprintf(url, "%s/objects/info/packs", repo->base);
632 slot = get_active_slot();
633 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
634 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
635 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
636 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
637 if (start_active_slot(slot)) {
638 run_active_slot(slot);
639 if (slot->curl_result != CURLE_OK) {
640 if (slot->http_code == 404) {
641 repo->got_indices = 1;
642 free(buffer.buffer);
643 return 0;
644 } else {
645 repo->got_indices = 0;
646 free(buffer.buffer);
647 return error("%s", curl_errorstr);
650 } else {
651 repo->got_indices = 0;
652 free(buffer.buffer);
653 return error("Unable to start request");
656 data = buffer.buffer;
657 while (i < buffer.posn) {
658 switch (data[i]) {
659 case 'P':
660 i++;
661 if (i + 52 < buffer.posn &&
662 !strncmp(data + i, " pack-", 6) &&
663 !strncmp(data + i + 46, ".pack\n", 6)) {
664 get_sha1_hex(data + i + 6, sha1);
665 setup_index(repo, sha1);
666 i += 51;
667 break;
669 default:
670 while (data[i] != '\n')
671 i++;
673 i++;
676 free(buffer.buffer);
677 repo->got_indices = 1;
678 return 0;
681 static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
683 char *url;
684 struct packed_git *target;
685 struct packed_git **lst;
686 FILE *packfile;
687 char *filename;
688 char tmpfile[PATH_MAX];
689 int ret;
690 long prev_posn = 0;
691 char range[RANGE_HEADER_SIZE];
692 struct curl_slist *range_header = NULL;
694 struct active_request_slot *slot;
696 if (fetch_indices(repo))
697 return -1;
698 target = find_sha1_pack(sha1, repo->packs);
699 if (!target)
700 return -1;
702 if (get_verbosely) {
703 fprintf(stderr, "Getting pack %s\n",
704 sha1_to_hex(target->sha1));
705 fprintf(stderr, " which contains %s\n",
706 sha1_to_hex(sha1));
709 url = xmalloc(strlen(repo->base) + 65);
710 sprintf(url, "%s/objects/pack/pack-%s.pack",
711 repo->base, sha1_to_hex(target->sha1));
713 filename = sha1_pack_name(target->sha1);
714 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
715 packfile = fopen(tmpfile, "a");
716 if (!packfile)
717 return error("Unable to open local file %s for pack",
718 filename);
720 slot = get_active_slot();
721 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
722 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
723 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
724 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
725 slot->local = packfile;
727 /* If there is data present from a previous transfer attempt,
728 resume where it left off */
729 prev_posn = ftell(packfile);
730 if (prev_posn>0) {
731 if (get_verbosely)
732 fprintf(stderr,
733 "Resuming fetch of pack %s at byte %ld\n",
734 sha1_to_hex(target->sha1), prev_posn);
735 sprintf(range, "Range: bytes=%ld-", prev_posn);
736 range_header = curl_slist_append(range_header, range);
737 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
740 if (start_active_slot(slot)) {
741 run_active_slot(slot);
742 if (slot->curl_result != CURLE_OK) {
743 fclose(packfile);
744 return error("Unable to get pack file %s\n%s", url,
745 curl_errorstr);
747 } else {
748 fclose(packfile);
749 return error("Unable to start request");
752 fclose(packfile);
754 ret = move_temp_to_file(tmpfile, filename);
755 if (ret)
756 return ret;
758 lst = &repo->packs;
759 while (*lst != target)
760 lst = &((*lst)->next);
761 *lst = (*lst)->next;
763 if (verify_pack(target, 0))
764 return -1;
765 install_packed_git(target);
767 return 0;
770 static int fetch_object(struct alt_base *repo, unsigned char *sha1)
772 char *hex = sha1_to_hex(sha1);
773 int ret = 0;
774 struct object_request *obj_req = object_queue_head;
776 while (obj_req != NULL && memcmp(obj_req->sha1, sha1, 20))
777 obj_req = obj_req->next;
778 if (obj_req == NULL)
779 return error("Couldn't find request for %s in the queue", hex);
781 if (has_sha1_file(obj_req->sha1)) {
782 release_object_request(obj_req);
783 return 0;
786 #ifdef USE_CURL_MULTI
787 while (obj_req->state == WAITING) {
788 step_active_slots();
790 #else
791 start_object_request(obj_req);
792 #endif
794 while (obj_req->state == ACTIVE) {
795 run_active_slot(obj_req->slot);
797 if (obj_req->local != -1) {
798 close(obj_req->local); obj_req->local = -1;
801 if (obj_req->state == ABORTED) {
802 ret = error("Request for %s aborted", hex);
803 } else if (obj_req->curl_result != CURLE_OK &&
804 obj_req->http_code != 416) {
805 if (obj_req->http_code == 404)
806 ret = -1; /* Be silent, it is probably in a pack. */
807 else
808 ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
809 obj_req->errorstr, obj_req->curl_result,
810 obj_req->http_code, hex);
811 } else if (obj_req->zret != Z_STREAM_END) {
812 ret = error("File %s (%s) corrupt\n", hex, obj_req->url);
813 } else if (memcmp(obj_req->sha1, obj_req->real_sha1, 20)) {
814 ret = error("File %s has bad hash\n", hex);
815 } else if (obj_req->rename < 0) {
816 ret = error("unable to write sha1 filename %s: %s",
817 obj_req->filename,
818 strerror(obj_req->rename));
821 release_object_request(obj_req);
822 return ret;
825 int fetch(unsigned char *sha1)
827 struct alt_base *altbase = alt;
829 if (!fetch_object(altbase, sha1))
830 return 0;
831 while (altbase) {
832 if (!fetch_pack(altbase, sha1))
833 return 0;
834 fetch_alternates(alt->base);
835 altbase = altbase->next;
837 return error("Unable to find %s under %s\n", sha1_to_hex(sha1),
838 alt->base);
841 static inline int needs_quote(int ch)
843 switch (ch) {
844 case '/': case '-': case '.':
845 case 'A'...'Z': case 'a'...'z': case '0'...'9':
846 return 0;
847 default:
848 return 1;
852 static inline int hex(int v)
854 if (v < 10) return '0' + v;
855 else return 'A' + v - 10;
858 static char *quote_ref_url(const char *base, const char *ref)
860 const char *cp;
861 char *dp, *qref;
862 int len, baselen, ch;
864 baselen = strlen(base);
865 len = baselen + 6; /* "refs/" + NUL */
866 for (cp = ref; (ch = *cp) != 0; cp++, len++)
867 if (needs_quote(ch))
868 len += 2; /* extra two hex plus replacement % */
869 qref = xmalloc(len);
870 memcpy(qref, base, baselen);
871 memcpy(qref + baselen, "refs/", 5);
872 for (cp = ref, dp = qref + baselen + 5; (ch = *cp) != 0; cp++) {
873 if (needs_quote(ch)) {
874 *dp++ = '%';
875 *dp++ = hex((ch >> 4) & 0xF);
876 *dp++ = hex(ch & 0xF);
878 else
879 *dp++ = ch;
881 *dp = 0;
883 return qref;
886 int fetch_ref(char *ref, unsigned char *sha1)
888 char *url;
889 char hex[42];
890 struct buffer buffer;
891 char *base = alt->base;
892 struct active_request_slot *slot;
893 buffer.size = 41;
894 buffer.posn = 0;
895 buffer.buffer = hex;
896 hex[41] = '\0';
898 url = quote_ref_url(base, ref);
899 slot = get_active_slot();
900 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
901 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
902 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
903 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
904 if (start_active_slot(slot)) {
905 run_active_slot(slot);
906 if (slot->curl_result != CURLE_OK)
907 return error("Couldn't get %s for %s\n%s",
908 url, ref, curl_errorstr);
909 } else {
910 return error("Unable to start request");
913 hex[40] = '\0';
914 get_sha1_hex(hex, sha1);
915 return 0;
918 int main(int argc, char **argv)
920 char *commit_id;
921 char *url;
922 int arg = 1;
923 int rc = 0;
925 setup_git_directory();
927 while (arg < argc && argv[arg][0] == '-') {
928 if (argv[arg][1] == 't') {
929 get_tree = 1;
930 } else if (argv[arg][1] == 'c') {
931 get_history = 1;
932 } else if (argv[arg][1] == 'a') {
933 get_all = 1;
934 get_tree = 1;
935 get_history = 1;
936 } else if (argv[arg][1] == 'v') {
937 get_verbosely = 1;
938 } else if (argv[arg][1] == 'w') {
939 write_ref = argv[arg + 1];
940 arg++;
941 } else if (!strcmp(argv[arg], "--recover")) {
942 get_recover = 1;
944 arg++;
946 if (argc < arg + 2) {
947 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
948 return 1;
950 commit_id = argv[arg];
951 url = argv[arg + 1];
953 http_init();
955 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
957 alt = xmalloc(sizeof(*alt));
958 alt->base = url;
959 alt->got_indices = 0;
960 alt->packs = NULL;
961 alt->next = NULL;
963 if (pull(commit_id))
964 rc = 1;
966 curl_slist_free_all(no_pragma_header);
968 http_cleanup();
970 return rc;