368
#endif
369
}
370
371
-static int fetch_index(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
372
-{
373
- int ret = 0;
374
- char *hex = xstrdup(sha1_to_hex(sha1));
375
- char *filename;
376
- char *url;
377
- char tmpfile[PATH_MAX];
378
- long prev_posn = 0;
379
- char range[RANGE_HEADER_SIZE];
380
- struct curl_slist *range_header = NULL;
381
-
382
- FILE *indexfile;
383
- struct active_request_slot *slot;
384
- struct slot_results results;
385
-
386
- /* Don't use the index if the pack isn't there */
387
- url = xmalloc(strlen(repo->base) + 64);
388
- sprintf(url, "%s/objects/pack/pack-%s.pack", repo->base, hex);
389
- slot = get_active_slot();
390
- slot->results = &results;
391
- curl_easy_setopt(slot->curl, CURLOPT_URL, url);
392
- curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
393
- if (start_active_slot(slot)) {
394
- run_active_slot(slot);
395
- if (results.curl_result != CURLE_OK) {
396
- ret = error("Unable to verify pack %s is available",
397
- hex);
398
- goto cleanup_pack;
399
- }
400
- } else {
401
- ret = error("Unable to start request");
402
- goto cleanup_pack;
403
- }
404
-
405
- if (has_pack_index(sha1)) {
406
- ret = 0;
407
- goto cleanup_pack;
408
- }
409
-
410
- if (walker->get_verbosely)
411
- fprintf(stderr, "Getting index for pack %s\n", hex);
412
-
413
- sprintf(url, "%s/objects/pack/pack-%s.idx", repo->base, hex);
414
-
415
- filename = sha1_pack_index_name(sha1);
416
- snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
417
- indexfile = fopen(tmpfile, "a");
418
- if (!indexfile) {
419
- ret = error("Unable to open local file %s for pack index",
420
- tmpfile);
421
- goto cleanup_pack;
422
- }
423
-
424
- slot = get_active_slot();
425
- slot->results = &results;
426
- curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
427
- curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
428
- curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
429
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
430
- curl_easy_setopt(slot->curl, CURLOPT_URL, url);
431
- curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
432
- slot->local = indexfile;
433
-
434
- /*
435
- * If there is data present from a previous transfer attempt,
436
- * resume where it left off
437
- */
438
- prev_posn = ftell(indexfile);
439
- if (prev_posn>0) {
440
- if (walker->get_verbosely)
441
- fprintf(stderr,
442
- "Resuming fetch of index for pack %s at byte %ld\n",
443
- hex, prev_posn);
444
- sprintf(range, "Range: bytes=%ld-", prev_posn);
445
- range_header = curl_slist_append(range_header, range);
446
- curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
447
- }
448
-
449
- if (start_active_slot(slot)) {
450
- run_active_slot(slot);
451
- if (results.curl_result != CURLE_OK) {
452
- ret = error("Unable to get pack index %s\n%s", url,
453
- curl_errorstr);
454
- goto cleanup_index;
455
- }
456
- } else {
457
- ret = error("Unable to start request");
458
- goto cleanup_index;
459
- }
460
-
461
- ret = move_temp_to_file(tmpfile, filename);
462
-
463
-cleanup_index:
464
- fclose(indexfile);
465
- slot->local = NULL;
466
-cleanup_pack:
467
- free(url);
468
- free(hex);
469
- return ret;
470
-}
471
-
472
-static int setup_index(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
473
-{
474
- struct packed_git *new_pack;
475
- if (has_pack_file(sha1))
476
- return 0; /* don't list this as something we can get */
477
-
478
- if (fetch_index(walker, repo, sha1))
479
- return -1;
480
-
481
- new_pack = parse_pack_index(sha1);
482
- if (!new_pack)
483
- return -1; /* parse_pack_index() already issued error message */
484
- new_pack->next = repo->packs;
485
- repo->packs = new_pack;
486
- return 0;
487
-}
488
-
371
static void process_alternates_response(void *callback_data)
372
{
373
struct alternates_request *alt_req =
566
567
static int fetch_indices(struct walker *walker, struct alt_base *repo)
568
{
687
- unsigned char sha1[20];
688
- char *url;
689
- struct strbuf buffer = STRBUF_INIT;
690
- char *data;
691
- int i = 0;
692
- int ret = 0;
693
-
694
- struct active_request_slot *slot;
695
- struct slot_results results;
569
+ int ret;
570
571
if (repo->got_indices)
572
return 0;
574
if (walker->get_verbosely)
575
fprintf(stderr, "Getting pack list for %s\n", repo->base);
576
703
- url = xmalloc(strlen(repo->base) + 21);
704
- sprintf(url, "%s/objects/info/packs", repo->base);
705
-
706
- slot = get_active_slot();
707
- slot->results = &results;
708
- curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
709
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
710
- curl_easy_setopt(slot->curl, CURLOPT_URL, url);
711
- curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
712
- if (start_active_slot(slot)) {
713
- run_active_slot(slot);
714
- if (results.curl_result != CURLE_OK) {
715
- if (missing_target(&results)) {
716
- repo->got_indices = 1;
717
- goto cleanup;
718
- } else {
719
- repo->got_indices = 0;
720
- ret = error("%s", curl_errorstr);
721
- goto cleanup;
722
- }
723
- }
724
- } else {
577
+ switch (http_get_info_packs(repo->base, &repo->packs)) {
578
+ case HTTP_OK:
579
+ case HTTP_MISSING_TARGET:
580
+ repo->got_indices = 1;
581
+ ret = 0;
582
+ break;
583
+ default:
584
repo->got_indices = 0;
726
- ret = error("Unable to start request");
727
- goto cleanup;
585
+ ret = -1;
586
}
587
730
- data = buffer.buf;
731
- while (i < buffer.len) {
732
- switch (data[i]) {
733
- case 'P':
734
- i++;
735
- if (i + 52 <= buffer.len &&
736
- !prefixcmp(data + i, " pack-") &&
737
- !prefixcmp(data + i + 46, ".pack\n")) {
738
- get_sha1_hex(data + i + 6, sha1);
739
- setup_index(walker, repo, sha1);
740
- i += 51;
741
- break;
742
- }
743
- default:
744
- while (i < buffer.len && data[i] != '\n')
745
- i++;
746
- }
747
- i++;
748
- }
749
-
750
- repo->got_indices = 1;
751
-cleanup:
752
- strbuf_release(&buffer);
753
- free(url);
588
return ret;
589
}
590