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