Merge branch 'jc/clone' into next
[git] / http-fetch.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "pack.h"
4 #include "fetch.h"
5 #include "http.h"
6
7 #define PREV_BUF_SIZE 4096
8 #define RANGE_HEADER_SIZE 30
9
10 static int got_alternates = -1;
11
12 static struct curl_slist *no_pragma_header;
13
14 struct alt_base
15 {
16         char *base;
17         int got_indices;
18         struct packed_git *packs;
19         struct alt_base *next;
20 };
21
22 static struct alt_base *alt = NULL;
23
24 enum object_request_state {
25         WAITING,
26         ABORTED,
27         ACTIVE,
28         COMPLETE,
29 };
30
31 struct object_request
32 {
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;
50 };
51
52 struct alternates_request {
53         char *base;
54         char *url;
55         struct buffer *buffer;
56         struct active_request_slot *slot;
57         int http_specific;
58 };
59
60 static struct object_request *object_queue_head = NULL;
61
62 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
63                                void *data)
64 {
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);
76
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;
88 }
89
90 static void fetch_alternates(char *base);
91
92 static void process_object_response(void *callback_data);
93
94 static void start_object_request(struct object_request *obj_req)
95 {
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;
107
108         snprintf(prevfile, sizeof(prevfile), "%s.prev", obj_req->filename);
109         unlink(prevfile);
110         rename(obj_req->tmpfile, prevfile);
111         unlink(obj_req->tmpfile);
112
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.
119          */
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 = '/';
126                 }
127                 obj_req->local = open(obj_req->tmpfile,
128                                       O_WRONLY | O_CREAT | O_EXCL, 0666);
129         }
130
131         if (obj_req->local < 0) {
132                 obj_req->state = ABORTED;
133                 error("Couldn't create temporary file %s for %s: %s",
134                       obj_req->tmpfile, obj_req->filename, strerror(errno));
135                 return;
136         }
137
138         memset(&obj_req->stream, 0, sizeof(obj_req->stream));
139
140         inflateInit(&obj_req->stream);
141
142         SHA1_Init(&obj_req->c);
143
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);
155
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,
164                                                      1,
165                                                      prev_read,
166                                                      obj_req) == prev_read) {
167                                         prev_posn += prev_read;
168                                 } else {
169                                         prev_read = -1;
170                                 }
171                         }
172                 } while (prev_read > 0);
173                 close(prevlocal);
174         }
175         unlink(prevfile);
176
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);
187                 }
188         }
189
190         slot = get_active_slot();
191         slot->callback_func = process_object_response;
192         slot->callback_data = obj_req;
193         obj_req->slot = slot;
194
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);
200
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);
212         }
213
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;
222         }
223 }
224
225 static void finish_object_request(struct object_request *obj_req)
226 {
227         struct stat st;
228
229         fchmod(obj_req->local, 0444);
230         close(obj_req->local); obj_req->local = -1;
231
232         if (obj_req->http_code == 416) {
233                 fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
234         } else if (obj_req->curl_result != CURLE_OK) {
235                 if (stat(obj_req->tmpfile, &st) == 0)
236                         if (st.st_size == 0)
237                                 unlink(obj_req->tmpfile);
238                 return;
239         }
240
241         inflateEnd(&obj_req->stream);
242         SHA1_Final(obj_req->real_sha1, &obj_req->c);
243         if (obj_req->zret != Z_STREAM_END) {
244                 unlink(obj_req->tmpfile);
245                 return;
246         }
247         if (memcmp(obj_req->sha1, obj_req->real_sha1, 20)) {
248                 unlink(obj_req->tmpfile);
249                 return;
250         }
251         obj_req->rename =
252                 move_temp_to_file(obj_req->tmpfile, obj_req->filename);
253
254         if (obj_req->rename == 0)
255                 pull_say("got %s\n", sha1_to_hex(obj_req->sha1));
256 }
257
258 static void process_object_response(void *callback_data)
259 {
260         struct object_request *obj_req =
261                 (struct object_request *)callback_data;
262
263         obj_req->curl_result = obj_req->slot->curl_result;
264         obj_req->http_code = obj_req->slot->http_code;
265         obj_req->slot = NULL;
266         obj_req->state = COMPLETE;
267
268         /* Use alternates if necessary */
269         if (obj_req->http_code == 404 ||
270             obj_req->curl_result == CURLE_FILE_COULDNT_READ_FILE) {
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;
279                 }
280         }
281
282         finish_object_request(obj_req);
283 }
284
285 static void release_object_request(struct object_request *obj_req)
286 {
287         struct object_request *entry = object_queue_head;
288
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;
298         }
299
300         free(obj_req->url);
301         free(obj_req);
302 }
303
304 #ifdef USE_CURL_MULTI
305 void fill_active_slots(void)
306 {
307         struct object_request *obj_req = object_queue_head;
308         struct active_request_slot *slot = active_queue_head;
309         int num_transfers;
310
311         while (active_requests < max_requests && obj_req != NULL) {
312                 if (obj_req->state == WAITING) {
313                         if (has_sha1_file(obj_req->sha1))
314                                 obj_req->state = COMPLETE;
315                         else
316                                 start_object_request(obj_req);
317                         curl_multi_perform(curlm, &num_transfers);
318                 }
319                 obj_req = obj_req->next;
320         }
321
322         while (slot != NULL) {
323                 if (!slot->in_use && slot->curl != NULL) {
324                         curl_easy_cleanup(slot->curl);
325                         slot->curl = NULL;
326                 }
327                 slot = slot->next;
328         }
329 }
330 #endif
331
332 void prefetch(unsigned char *sha1)
333 {
334         struct object_request *newreq;
335         struct object_request *tail;
336         char *filename = sha1_file_name(sha1);
337
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;
348
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;
355                 }
356                 tail->next = newreq;
357         }
358
359 #ifdef USE_CURL_MULTI
360         fill_active_slots();
361         step_active_slots();
362 #endif
363 }
364
365 static int fetch_index(struct alt_base *repo, unsigned char *sha1)
366 {
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;
374
375         FILE *indexfile;
376         struct active_request_slot *slot;
377         struct slot_results results;
378
379         if (has_pack_index(sha1))
380                 return 0;
381
382         if (get_verbosely)
383                 fprintf(stderr, "Getting index for pack %s\n", hex);
384
385         url = xmalloc(strlen(repo->base) + 64);
386         sprintf(url, "%s/objects/pack/pack-%s.idx", repo->base, hex);
387
388         filename = sha1_pack_index_name(sha1);
389         snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
390         indexfile = fopen(tmpfile, "a");
391         if (!indexfile)
392                 return error("Unable to open local file %s for pack index",
393                              filename);
394
395         slot = get_active_slot();
396         slot->results = &results;
397         curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
398         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
399         curl_easy_setopt(slot->curl, CURLOPT_URL, url);
400         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
401         slot->local = indexfile;
402
403         /* If there is data present from a previous transfer attempt,
404            resume where it left off */
405         prev_posn = ftell(indexfile);
406         if (prev_posn>0) {
407                 if (get_verbosely)
408                         fprintf(stderr,
409                                 "Resuming fetch of index for pack %s at byte %ld\n",
410                                 hex, prev_posn);
411                 sprintf(range, "Range: bytes=%ld-", prev_posn);
412                 range_header = curl_slist_append(range_header, range);
413                 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
414         }
415
416         if (start_active_slot(slot)) {
417                 run_active_slot(slot);
418                 if (results.curl_result != CURLE_OK) {
419                         fclose(indexfile);
420                         return error("Unable to get pack index %s\n%s", url,
421                                      curl_errorstr);
422                 }
423         } else {
424                 fclose(indexfile);
425                 return error("Unable to start request");
426         }
427
428         fclose(indexfile);
429
430         return move_temp_to_file(tmpfile, filename);
431 }
432
433 static int setup_index(struct alt_base *repo, unsigned char *sha1)
434 {
435         struct packed_git *new_pack;
436         if (has_pack_file(sha1))
437                 return 0; // don't list this as something we can get
438
439         if (fetch_index(repo, sha1))
440                 return -1;
441
442         new_pack = parse_pack_index(sha1);
443         new_pack->next = repo->packs;
444         repo->packs = new_pack;
445         return 0;
446 }
447
448 static void process_alternates_response(void *callback_data)
449 {
450         struct alternates_request *alt_req =
451                 (struct alternates_request *)callback_data;
452         struct active_request_slot *slot = alt_req->slot;
453         struct alt_base *tail = alt;
454         char *base = alt_req->base;
455         static const char null_byte = '\0';
456         char *data;
457         int i = 0;
458
459         if (alt_req->http_specific) {
460                 if (slot->curl_result != CURLE_OK ||
461                     !alt_req->buffer->posn) {
462
463                         /* Try reusing the slot to get non-http alternates */
464                         alt_req->http_specific = 0;
465                         sprintf(alt_req->url, "%s/objects/info/alternates",
466                                 base);
467                         curl_easy_setopt(slot->curl, CURLOPT_URL,
468                                          alt_req->url);
469                         active_requests++;
470                         slot->in_use = 1;
471                         if (slot->finished != NULL)
472                                 (*slot->finished) = 0;
473                         if (!start_active_slot(slot)) {
474                                 got_alternates = -1;
475                                 slot->in_use = 0;
476                                 if (slot->finished != NULL)
477                                         (*slot->finished) = 1;
478                         }
479                         return;
480                 }
481         } else if (slot->curl_result != CURLE_OK) {
482                 if (slot->http_code != 404 &&
483                     slot->curl_result != CURLE_FILE_COULDNT_READ_FILE) {
484                         got_alternates = -1;
485                         return;
486                 }
487         }
488
489         fwrite_buffer(&null_byte, 1, 1, alt_req->buffer);
490         alt_req->buffer->posn--;
491         data = alt_req->buffer->buffer;
492
493         while (i < alt_req->buffer->posn) {
494                 int posn = i;
495                 while (posn < alt_req->buffer->posn && data[posn] != '\n')
496                         posn++;
497                 if (data[posn] == '\n') {
498                         int okay = 0;
499                         int serverlen = 0;
500                         struct alt_base *newalt;
501                         char *target = NULL;
502                         if (data[i] == '/') {
503                                 serverlen = strchr(base + 8, '/') - base;
504                                 okay = 1;
505                         } else if (!memcmp(data + i, "../", 3)) {
506                                 i += 3;
507                                 serverlen = strlen(base);
508                                 while (i + 2 < posn &&
509                                        !memcmp(data + i, "../", 3)) {
510                                         do {
511                                                 serverlen--;
512                                         } while (serverlen &&
513                                                  base[serverlen - 1] != '/');
514                                         i += 3;
515                                 }
516                                 // If the server got removed, give up.
517                                 okay = strchr(base, ':') - base + 3 <
518                                         serverlen;
519                         } else if (alt_req->http_specific) {
520                                 char *colon = strchr(data + i, ':');
521                                 char *slash = strchr(data + i, '/');
522                                 if (colon && slash && colon < data + posn &&
523                                     slash < data + posn && colon < slash) {
524                                         okay = 1;
525                                 }
526                         }
527                         // skip 'objects' at end
528                         if (okay) {
529                                 target = xmalloc(serverlen + posn - i - 6);
530                                 strncpy(target, base, serverlen);
531                                 strncpy(target + serverlen, data + i,
532                                         posn - i - 7);
533                                 target[serverlen + posn - i - 7] = '\0';
534                                 if (get_verbosely)
535                                         fprintf(stderr,
536                                                 "Also look at %s\n", target);
537                                 newalt = xmalloc(sizeof(*newalt));
538                                 newalt->next = NULL;
539                                 newalt->base = target;
540                                 newalt->got_indices = 0;
541                                 newalt->packs = NULL;
542                                 while (tail->next != NULL)
543                                         tail = tail->next;
544                                 tail->next = newalt;
545                         }
546                 }
547                 i = posn + 1;
548         }
549
550         got_alternates = 1;
551 }
552
553 static void fetch_alternates(char *base)
554 {
555         struct buffer buffer;
556         char *url;
557         char *data;
558         struct active_request_slot *slot;
559         struct alternates_request alt_req;
560
561         /* If another request has already started fetching alternates,
562            wait for them to arrive and return to processing this request's
563            curl message */
564 #ifdef USE_CURL_MULTI
565         while (got_alternates == 0) {
566                 step_active_slots();
567         }
568 #endif
569
570         /* Nothing to do if they've already been fetched */
571         if (got_alternates == 1)
572                 return;
573
574         /* Start the fetch */
575         got_alternates = 0;
576
577         data = xmalloc(4096);
578         buffer.size = 4096;
579         buffer.posn = 0;
580         buffer.buffer = data;
581
582         if (get_verbosely)
583                 fprintf(stderr, "Getting alternates list for %s\n", base);
584
585         url = xmalloc(strlen(base) + 31);
586         sprintf(url, "%s/objects/info/http-alternates", base);
587
588         /* Use a callback to process the result, since another request
589            may fail and need to have alternates loaded before continuing */
590         slot = get_active_slot();
591         slot->callback_func = process_alternates_response;
592         slot->callback_data = &alt_req;
593
594         curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
595         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
596         curl_easy_setopt(slot->curl, CURLOPT_URL, url);
597
598         alt_req.base = base;
599         alt_req.url = url;
600         alt_req.buffer = &buffer;
601         alt_req.http_specific = 1;
602         alt_req.slot = slot;
603
604         if (start_active_slot(slot))
605                 run_active_slot(slot);
606         else
607                 got_alternates = -1;
608
609         free(data);
610         free(url);
611 }
612
613 static int fetch_indices(struct alt_base *repo)
614 {
615         unsigned char sha1[20];
616         char *url;
617         struct buffer buffer;
618         char *data;
619         int i = 0;
620
621         struct active_request_slot *slot;
622         struct slot_results results;
623
624         if (repo->got_indices)
625                 return 0;
626
627         data = xmalloc(4096);
628         buffer.size = 4096;
629         buffer.posn = 0;
630         buffer.buffer = data;
631
632         if (get_verbosely)
633                 fprintf(stderr, "Getting pack list for %s\n", repo->base);
634
635         url = xmalloc(strlen(repo->base) + 21);
636         sprintf(url, "%s/objects/info/packs", repo->base);
637
638         slot = get_active_slot();
639         slot->results = &results;
640         curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
641         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
642         curl_easy_setopt(slot->curl, CURLOPT_URL, url);
643         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
644         if (start_active_slot(slot)) {
645                 run_active_slot(slot);
646                 if (results.curl_result != CURLE_OK) {
647                         if (results.http_code == 404 ||
648                             results.curl_result == CURLE_FILE_COULDNT_READ_FILE) {
649                                 repo->got_indices = 1;
650                                 free(buffer.buffer);
651                                 return 0;
652                         } else {
653                                 repo->got_indices = 0;
654                                 free(buffer.buffer);
655                                 return error("%s", curl_errorstr);
656                         }
657                 }
658         } else {
659                 repo->got_indices = 0;
660                 free(buffer.buffer);
661                 return error("Unable to start request");
662         }
663
664         data = buffer.buffer;
665         while (i < buffer.posn) {
666                 switch (data[i]) {
667                 case 'P':
668                         i++;
669                         if (i + 52 <= buffer.posn &&
670                             !strncmp(data + i, " pack-", 6) &&
671                             !strncmp(data + i + 46, ".pack\n", 6)) {
672                                 get_sha1_hex(data + i + 6, sha1);
673                                 setup_index(repo, sha1);
674                                 i += 51;
675                                 break;
676                         }
677                 default:
678                         while (i < buffer.posn && data[i] != '\n')
679                                 i++;
680                 }
681                 i++;
682         }
683
684         free(buffer.buffer);
685         repo->got_indices = 1;
686         return 0;
687 }
688
689 static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
690 {
691         char *url;
692         struct packed_git *target;
693         struct packed_git **lst;
694         FILE *packfile;
695         char *filename;
696         char tmpfile[PATH_MAX];
697         int ret;
698         long prev_posn = 0;
699         char range[RANGE_HEADER_SIZE];
700         struct curl_slist *range_header = NULL;
701
702         struct active_request_slot *slot;
703         struct slot_results results;
704
705         if (fetch_indices(repo))
706                 return -1;
707         target = find_sha1_pack(sha1, repo->packs);
708         if (!target)
709                 return -1;
710
711         if (get_verbosely) {
712                 fprintf(stderr, "Getting pack %s\n",
713                         sha1_to_hex(target->sha1));
714                 fprintf(stderr, " which contains %s\n",
715                         sha1_to_hex(sha1));
716         }
717
718         url = xmalloc(strlen(repo->base) + 65);
719         sprintf(url, "%s/objects/pack/pack-%s.pack",
720                 repo->base, sha1_to_hex(target->sha1));
721
722         filename = sha1_pack_name(target->sha1);
723         snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
724         packfile = fopen(tmpfile, "a");
725         if (!packfile)
726                 return error("Unable to open local file %s for pack",
727                              filename);
728
729         slot = get_active_slot();
730         slot->results = &results;
731         curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
732         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
733         curl_easy_setopt(slot->curl, CURLOPT_URL, url);
734         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
735         slot->local = packfile;
736
737         /* If there is data present from a previous transfer attempt,
738            resume where it left off */
739         prev_posn = ftell(packfile);
740         if (prev_posn>0) {
741                 if (get_verbosely)
742                         fprintf(stderr,
743                                 "Resuming fetch of pack %s at byte %ld\n",
744                                 sha1_to_hex(target->sha1), prev_posn);
745                 sprintf(range, "Range: bytes=%ld-", prev_posn);
746                 range_header = curl_slist_append(range_header, range);
747                 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
748         }
749
750         if (start_active_slot(slot)) {
751                 run_active_slot(slot);
752                 if (results.curl_result != CURLE_OK) {
753                         fclose(packfile);
754                         return error("Unable to get pack file %s\n%s", url,
755                                      curl_errorstr);
756                 }
757         } else {
758                 fclose(packfile);
759                 return error("Unable to start request");
760         }
761
762         fclose(packfile);
763
764         ret = move_temp_to_file(tmpfile, filename);
765         if (ret)
766                 return ret;
767
768         lst = &repo->packs;
769         while (*lst != target)
770                 lst = &((*lst)->next);
771         *lst = (*lst)->next;
772
773         if (verify_pack(target, 0))
774                 return -1;
775         install_packed_git(target);
776
777         return 0;
778 }
779
780 static void abort_object_request(struct object_request *obj_req)
781 {
782         if (obj_req->local >= 0) {
783                 close(obj_req->local);
784                 obj_req->local = -1;
785         }
786         unlink(obj_req->tmpfile);
787         if (obj_req->slot) {
788                 release_active_slot(obj_req->slot);
789                 obj_req->slot = NULL;
790         }
791         release_object_request(obj_req);
792 }
793
794 static int fetch_object(struct alt_base *repo, unsigned char *sha1)
795 {
796         char *hex = sha1_to_hex(sha1);
797         int ret = 0;
798         struct object_request *obj_req = object_queue_head;
799
800         while (obj_req != NULL && memcmp(obj_req->sha1, sha1, 20))
801                 obj_req = obj_req->next;
802         if (obj_req == NULL)
803                 return error("Couldn't find request for %s in the queue", hex);
804
805         if (has_sha1_file(obj_req->sha1)) {
806                 abort_object_request(obj_req);
807                 return 0;
808         }
809
810 #ifdef USE_CURL_MULTI
811         while (obj_req->state == WAITING) {
812                 step_active_slots();
813         }
814 #else
815         start_object_request(obj_req);
816 #endif
817
818         while (obj_req->state == ACTIVE) {
819                 run_active_slot(obj_req->slot);
820         }
821         if (obj_req->local != -1) {
822                 close(obj_req->local); obj_req->local = -1;
823         }
824
825         if (obj_req->state == ABORTED) {
826                 ret = error("Request for %s aborted", hex);
827         } else if (obj_req->curl_result != CURLE_OK &&
828                    obj_req->http_code != 416) {
829                 if (obj_req->http_code == 404 ||
830                     obj_req->curl_result == CURLE_FILE_COULDNT_READ_FILE)
831                         ret = -1; /* Be silent, it is probably in a pack. */
832                 else
833                         ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
834                                     obj_req->errorstr, obj_req->curl_result,
835                                     obj_req->http_code, hex);
836         } else if (obj_req->zret != Z_STREAM_END) {
837                 ret = error("File %s (%s) corrupt", hex, obj_req->url);
838         } else if (memcmp(obj_req->sha1, obj_req->real_sha1, 20)) {
839                 ret = error("File %s has bad hash", hex);
840         } else if (obj_req->rename < 0) {
841                 ret = error("unable to write sha1 filename %s",
842                             obj_req->filename);
843         }
844
845         release_object_request(obj_req);
846         return ret;
847 }
848
849 int fetch(unsigned char *sha1)
850 {
851         struct alt_base *altbase = alt;
852
853         if (!fetch_object(altbase, sha1))
854                 return 0;
855         while (altbase) {
856                 if (!fetch_pack(altbase, sha1))
857                         return 0;
858                 fetch_alternates(alt->base);
859                 altbase = altbase->next;
860         }
861         return error("Unable to find %s under %s", sha1_to_hex(sha1),
862                      alt->base);
863 }
864
865 static inline int needs_quote(int ch)
866 {
867         switch (ch) {
868         case '/': case '-': case '.':
869         case 'A'...'Z': case 'a'...'z': case '0'...'9':
870                 return 0;
871         default:
872                 return 1;
873         }
874 }
875
876 static inline int hex(int v)
877 {
878         if (v < 10) return '0' + v;
879         else return 'A' + v - 10;
880 }
881
882 static char *quote_ref_url(const char *base, const char *ref)
883 {
884         const char *cp;
885         char *dp, *qref;
886         int len, baselen, ch;
887
888         baselen = strlen(base);
889         len = baselen + 6; /* "refs/" + NUL */
890         for (cp = ref; (ch = *cp) != 0; cp++, len++)
891                 if (needs_quote(ch))
892                         len += 2; /* extra two hex plus replacement % */
893         qref = xmalloc(len);
894         memcpy(qref, base, baselen);
895         memcpy(qref + baselen, "refs/", 5);
896         for (cp = ref, dp = qref + baselen + 5; (ch = *cp) != 0; cp++) {
897                 if (needs_quote(ch)) {
898                         *dp++ = '%';
899                         *dp++ = hex((ch >> 4) & 0xF);
900                         *dp++ = hex(ch & 0xF);
901                 }
902                 else
903                         *dp++ = ch;
904         }
905         *dp = 0;
906
907         return qref;
908 }
909
910 int fetch_ref(char *ref, unsigned char *sha1)
911 {
912         char *url;
913         char hex[42];
914         struct buffer buffer;
915         char *base = alt->base;
916         struct active_request_slot *slot;
917         struct slot_results results;
918         buffer.size = 41;
919         buffer.posn = 0;
920         buffer.buffer = hex;
921         hex[41] = '\0';
922
923         url = quote_ref_url(base, ref);
924         slot = get_active_slot();
925         slot->results = &results;
926         curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
927         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
928         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
929         curl_easy_setopt(slot->curl, CURLOPT_URL, url);
930         if (start_active_slot(slot)) {
931                 run_active_slot(slot);
932                 if (results.curl_result != CURLE_OK)
933                         return error("Couldn't get %s for %s\n%s",
934                                      url, ref, curl_errorstr);
935         } else {
936                 return error("Unable to start request");
937         }
938
939         hex[40] = '\0';
940         get_sha1_hex(hex, sha1);
941         return 0;
942 }
943
944 int main(int argc, char **argv)
945 {
946         char *commit_id;
947         char *url;
948         int arg = 1;
949         int rc = 0;
950
951         setup_git_directory();
952
953         while (arg < argc && argv[arg][0] == '-') {
954                 if (argv[arg][1] == 't') {
955                         get_tree = 1;
956                 } else if (argv[arg][1] == 'c') {
957                         get_history = 1;
958                 } else if (argv[arg][1] == 'a') {
959                         get_all = 1;
960                         get_tree = 1;
961                         get_history = 1;
962                 } else if (argv[arg][1] == 'v') {
963                         get_verbosely = 1;
964                 } else if (argv[arg][1] == 'w') {
965                         write_ref = argv[arg + 1];
966                         arg++;
967                 } else if (!strcmp(argv[arg], "--recover")) {
968                         get_recover = 1;
969                 }
970                 arg++;
971         }
972         if (argc < arg + 2) {
973                 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
974                 return 1;
975         }
976         commit_id = argv[arg];
977         url = argv[arg + 1];
978
979         http_init();
980
981         no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
982
983         alt = xmalloc(sizeof(*alt));
984         alt->base = url;
985         alt->got_indices = 0;
986         alt->packs = NULL;
987         alt->next = NULL;
988
989         if (pull(commit_id))
990                 rc = 1;
991
992         curl_slist_free_all(no_pragma_header);
993
994         http_cleanup();
995
996         return rc;
997 }