git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
83f8cce3b12af7b68e31d9cac0b0bd640723f9da blob 26092 bytes (raw)

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
 
#include "cache.h"
#include "object.h"
#include "argv-array.h"
#include "odb-helper.h"
#include "run-command.h"
#include "sha1-lookup.h"
#include "fetch-object.h"
#include "sub-process.h"
#include "pkt-line.h"
#include "sigchain.h"

struct object_process {
	struct subprocess_entry subprocess;
	unsigned int supported_capabilities;
};

static struct hashmap subprocess_map;

static int check_object_process_status(int fd, struct strbuf *status)
{
	subprocess_read_status(fd, status);
	return strcmp(status->buf, "success");
}

static void parse_capabilities(char *cap_buf,
			       unsigned int *supported_capabilities,
			       const char *process_name)
{
	struct string_list cap_list = STRING_LIST_INIT_NODUP;

	string_list_split_in_place(&cap_list, cap_buf, '=', 1);

	if (cap_list.nr == 2 && !strcmp(cap_list.items[0].string, "capability")) {
		const char *cap_name = cap_list.items[1].string;

		if (!strcmp(cap_name, "get_git_obj")) {
			*supported_capabilities |= ODB_HELPER_CAP_GET_GIT_OBJ;
		} else if (!strcmp(cap_name, "get_raw_obj")) {
			*supported_capabilities |= ODB_HELPER_CAP_GET_RAW_OBJ;
		} else if (!strcmp(cap_name, "get_direct")) {
			*supported_capabilities |= ODB_HELPER_CAP_GET_DIRECT;
		} else if (!strcmp(cap_name, "put_git_obj")) {
			*supported_capabilities |= ODB_HELPER_CAP_PUT_GIT_OBJ;
		} else if (!strcmp(cap_name, "put_raw_obj")) {
			*supported_capabilities |= ODB_HELPER_CAP_PUT_RAW_OBJ;
		} else if (!strcmp(cap_name, "put_direct")) {
			*supported_capabilities |= ODB_HELPER_CAP_PUT_DIRECT;
		} else if (!strcmp(cap_name, "have")) {
			*supported_capabilities |= ODB_HELPER_CAP_HAVE;
		} else {
			warning("external process '%s' requested unsupported read-object capability '%s'",
				process_name, cap_name);
		}
	}

	string_list_clear(&cap_list, 0);
}

static int start_object_process_fn(struct subprocess_entry *subprocess)
{
	static int versions[] = {1, 0};
	static struct subprocess_capability capabilities[] = {
		{ "get_git_obj", ODB_HELPER_CAP_GET_GIT_OBJ },
		{ "get_raw_obj", ODB_HELPER_CAP_GET_RAW_OBJ },
		{ "get_direct",  ODB_HELPER_CAP_GET_DIRECT  },
		{ "put_git_obj", ODB_HELPER_CAP_PUT_GIT_OBJ },
		{ "put_raw_obj", ODB_HELPER_CAP_PUT_RAW_OBJ },
		{ "put_direct",  ODB_HELPER_CAP_PUT_DIRECT  },
		{ "have",        ODB_HELPER_CAP_HAVE },
		{ NULL, 0 }
	};
	struct object_process *entry = (struct object_process *)subprocess;
	return subprocess_handshake(subprocess, "git-read-object", versions, NULL,
				    capabilities,
				    &entry->supported_capabilities);
}

static struct object_process *launch_object_process(struct odb_helper *o,
						    unsigned int capability)
{
	struct object_process *entry = NULL;

	if (!subprocess_map.tablesize)
		hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp, NULL, 0);
	else
		entry = (struct object_process *)subprocess_find_entry(&subprocess_map, o->dealer);

	fflush(NULL);

	if (!entry) {
		entry = xmalloc(sizeof(*entry));
		entry->supported_capabilities = 0;

		if (subprocess_start(&subprocess_map, &entry->subprocess, o->dealer, start_object_process_fn)) {
			error("Could not launch process for cmd '%s'", o->dealer);
			free(entry);
			return NULL;
		}
	}

	o->supported_capabilities = entry->supported_capabilities;

	if (capability && !(capability & entry->supported_capabilities)) {
		error("The cmd '%s' does not support capability '%d'", o->dealer, capability);
		return NULL;
	}

	sigchain_push(SIGPIPE, SIG_IGN);

	return entry;
}

static int check_object_process_error(int err,
				      const char *status,
				      struct object_process *entry,
				      const char *cmd,
				      unsigned int capability)
{
	sigchain_pop(SIGPIPE);

	if (!err)
		return 0;

	if (!strcmp(status, "error")) {
		/* The process signaled a problem with the file. */
	} else if (!strcmp(status, "notfound")) {
		/* Object was not found */
		err = -1;
	} else if (!strcmp(status, "abort")) {
		/*
		 * The process signaled a permanent problem. Don't try to read
		 * objects with the same command for the lifetime of the current
		 * Git process.
		 */
		if (capability)
			entry->supported_capabilities &= ~capability;
	} else {
		/*
		 * Something went wrong with the read-object process.
		 * Force shutdown and restart if needed.
		 */
		error("external object process '%s' failed", cmd);
		subprocess_stop(&subprocess_map, &entry->subprocess);
		free(entry);
	}

	return err;
}

static int send_init_packets(struct object_process *entry,
			     struct strbuf *status)
{
	struct child_process *process = &entry->subprocess.process;

	return packet_write_fmt_gently(process->in, "command=init\n") ||
		packet_flush_gently(process->in) ||
		check_object_process_status(process->out, status);
}

static int init_object_process(struct odb_helper *o)
{
	int err;
	struct strbuf status = STRBUF_INIT;
	struct object_process *entry = launch_object_process(o, 0);
	if (!entry)
		return -1;

	err = send_init_packets(entry, &status);

	return check_object_process_error(err, status.buf, entry,
					  o->dealer, 0);
}

static ssize_t read_packetized_raw_object_to_fd(struct odb_helper *o,
						const unsigned char *sha1,
						int fd_in, int fd_out)
{
	ssize_t total_read = 0;
	unsigned long total_got = 0;
	int packet_len;

	char hdr[32];
	int hdrlen;

	int ret = Z_STREAM_END;
	unsigned char compressed[4096];
	git_zstream stream;
	git_SHA_CTX hash;
	unsigned char real_sha1[20];

	off_t size;
	enum object_type type;
	const char *s;
	int pkt_size;
	char *size_buf;

	size_buf = packet_read_line(fd_in, &pkt_size);
	if (!skip_prefix(size_buf, "size=", &s))
		return error("odb helper '%s' did not send size of plain object", o->name);
	size = strtoumax(s, NULL, 10);
	if (!skip_prefix(packet_read_line(fd_in, NULL), "kind=", &s))
		return error("odb helper '%s' did not send kind of plain object", o->name);
	/* Check if the object is not available */
	if (!strcmp(s, "none"))
		return -1;
	type = type_from_string_gently(s, strlen(s), 1);
	if (type < 0)
		return error("odb helper '%s' sent bad type '%s'", o->name, s);

	/* Set it up */
	git_deflate_init(&stream, zlib_compression_level);
	stream.next_out = compressed;
	stream.avail_out = sizeof(compressed);
	git_SHA1_Init(&hash);

	/* First header.. */
	hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", type_name(type), size) + 1;
	stream.next_in = (unsigned char *)hdr;
	stream.avail_in = hdrlen;
	while (git_deflate(&stream, 0) == Z_OK)
		; /* nothing */
	git_SHA1_Update(&hash, hdr, hdrlen);

	for (;;) {
		/* packet_read() writes a '\0' extra byte at the end */
		char buf[LARGE_PACKET_DATA_MAX + 1];

		packet_len = packet_read(fd_in, NULL, NULL,
			buf, LARGE_PACKET_DATA_MAX + 1,
			PACKET_READ_GENTLE_ON_EOF);

		if (packet_len <= 0)
			break;

		total_got += packet_len;

		/* Then the data itself.. */
		stream.next_in = (void *)buf;
		stream.avail_in = packet_len;
		do {
			unsigned char *in0 = stream.next_in;
			ret = git_deflate(&stream, Z_FINISH);
			git_SHA1_Update(&hash, in0, stream.next_in - in0);
			write_or_die(fd_out, compressed, stream.next_out - compressed);
			stream.next_out = compressed;
			stream.avail_out = sizeof(compressed);
		} while (ret == Z_OK);

		total_read += packet_len;
	}

	if (packet_len < 0) {
		error("unable to read from odb helper '%s': %s",
		      o->name, strerror(errno));
		git_deflate_end(&stream);
		return packet_len;
	}

	if (ret != Z_STREAM_END) {
		warning("bad zlib data from odb helper '%s' for %s",
			o->name, sha1_to_hex(sha1));
		return -1;
	}

	ret = git_deflate_end_gently(&stream);
	if (ret != Z_OK) {
		warning("deflateEnd on object %s from odb helper '%s' failed (%d)",
			sha1_to_hex(sha1), o->name, ret);
		return -1;
	}
	git_SHA1_Final(real_sha1, &hash);
	if (hashcmp(sha1, real_sha1)) {
		warning("sha1 mismatch from odb helper '%s' for %s (got %s)",
			o->name, sha1_to_hex(sha1), sha1_to_hex(real_sha1));
		return -1;
	}
	if (total_got != size) {
		warning("size mismatch from odb helper '%s' for %s (%lu != %lu)",
			o->name, sha1_to_hex(sha1), total_got, size);
		return -1;
	}

	return total_read;
}

static ssize_t read_packetized_git_object_to_fd(struct odb_helper *o,
						const unsigned char *sha1,
						int fd_in, int fd_out)
{
	ssize_t total_read = 0;
	unsigned long total_got = 0;
	int packet_len;
	git_zstream stream;
	int zret = Z_STREAM_END;
	git_SHA_CTX hash;
	unsigned char real_sha1[20];
	struct strbuf header = STRBUF_INIT;
	unsigned long hdr_size;

	memset(&stream, 0, sizeof(stream));
	git_inflate_init(&stream);
	git_SHA1_Init(&hash);

	for (;;) {
		/* packet_read() writes a '\0' extra byte at the end */
		char buf[LARGE_PACKET_DATA_MAX + 1];

		packet_len = packet_read(fd_in, NULL, NULL,
			buf, LARGE_PACKET_DATA_MAX + 1,
			PACKET_READ_GENTLE_ON_EOF);

		if (packet_len <= 0)
			break;

		write_or_die(fd_out, buf, packet_len);

		stream.next_in = (unsigned char *)buf;
		stream.avail_in = packet_len;
		do {
			unsigned char inflated[4096];
			unsigned long got;

			stream.next_out = inflated;
			stream.avail_out = sizeof(inflated);
			zret = git_inflate(&stream, Z_SYNC_FLUSH);
			got = sizeof(inflated) - stream.avail_out;

			git_SHA1_Update(&hash, inflated, got);
			/* skip header when counting size */
			if (!total_got) {
				const unsigned char *p = memchr(inflated, '\0', got);
				if (p) {
					unsigned long hdr_last = p - inflated + 1;
					strbuf_add(&header, inflated, hdr_last);
					got -= hdr_last;
				} else {
					strbuf_add(&header, inflated, got);
					got = 0;
				}
			}
			total_got += got;
		} while (stream.avail_in && zret == Z_OK);

		total_read += packet_len;
	}

	git_inflate_end(&stream);

	if (packet_len < 0) {
		strbuf_release(&header);
		return packet_len;
	}

	git_SHA1_Final(real_sha1, &hash);

	if (zret != Z_STREAM_END) {
		warning("bad zlib data from odb helper '%s' for %s",
			o->name, sha1_to_hex(sha1));
		strbuf_release(&header);
		return -1;
	}
	if (hashcmp(real_sha1, sha1)) {
		warning("sha1 mismatch from odb helper '%s' for %s (got %s)",
			o->name, sha1_to_hex(sha1), sha1_to_hex(real_sha1));
		strbuf_release(&header);
		return -1;
	}
	if (parse_sha1_header(header.buf, &hdr_size) < 0) {
		warning("could not parse header from odb helper '%s' for %s",
			o->name, sha1_to_hex(sha1));
		strbuf_release(&header);
		return -1;
	}
	if (total_got != hdr_size) {
		warning("size mismatch from odb helper '%s' for %s (%lu != %lu)",
			o->name, sha1_to_hex(sha1), total_got, hdr_size);
		strbuf_release(&header);
		return -1;
	}

	strbuf_release(&header);

	return total_read;
}

static int send_get_packets(struct odb_helper *o,
			    struct object_process *entry,
			    const unsigned char *sha1,
			    int fd,
			    unsigned int *cur_cap,
			    struct strbuf *status)
{
	const char *instruction;
	int err;
	struct child_process *process = &entry->subprocess.process;

	if (entry->supported_capabilities & ODB_HELPER_CAP_GET_GIT_OBJ) {
		*cur_cap = ODB_HELPER_CAP_GET_GIT_OBJ;
		instruction = "get_git_obj";
	} else if (entry->supported_capabilities & ODB_HELPER_CAP_GET_RAW_OBJ) {
		*cur_cap = ODB_HELPER_CAP_GET_RAW_OBJ;
		instruction = "get_raw_obj";
	} else if (entry->supported_capabilities & ODB_HELPER_CAP_GET_DIRECT) {
		*cur_cap = ODB_HELPER_CAP_GET_DIRECT;
		instruction = "get_direct";
	} else {
		BUG("No known ODB_HELPER_CAP_GET_XXX capability!");
	}

	err = packet_write_fmt_gently(process->in, "command=%s\n", instruction);
	if (err)
		return err;

	err = packet_write_fmt_gently(process->in, "sha1=%s\n", sha1_to_hex(sha1));
	if (err)
		return err;

	err = packet_flush_gently(process->in);
	if (err)
		return err;

	if (entry->supported_capabilities & ODB_HELPER_CAP_GET_RAW_OBJ)
		err = read_packetized_raw_object_to_fd(o, sha1, process->out, fd) < 0;
	else if (entry->supported_capabilities & ODB_HELPER_CAP_GET_GIT_OBJ)
		err = read_packetized_git_object_to_fd(o, sha1, process->out, fd) < 0;

	return check_object_process_status(process->out, status);
}

static int get_object_process(struct odb_helper *o, const unsigned char *sha1, int fd)
{
	int err;
	struct strbuf status = STRBUF_INIT;
	unsigned int cur_cap = 0;
	struct object_process *entry = launch_object_process(o, 0);
	if (!entry)
		return -1;

	err = send_get_packets(o, entry, sha1, fd, &cur_cap, &status);

	return check_object_process_error(err, status.buf, entry,
					  o->dealer, cur_cap);
}

static int send_put_packets(struct object_process *entry,
			    const unsigned char *sha1,
			    const void *buf,
			    size_t len,
			    struct strbuf *status)
{
	struct child_process *process = &entry->subprocess.process;
	int err = packet_write_fmt_gently(process->in, "command=put_raw_obj\n");
	if (err)
		return err;

	err = packet_write_fmt_gently(process->in, "sha1=%s\n", sha1_to_hex(sha1));
	if (err)
		return err;

	err = packet_write_fmt_gently(process->in, "size=%"PRIuMAX"\n", len);
	if (err)
		return err;

	err = packet_write_fmt_gently(process->in, "kind=blob\n");
	if (err)
		return err;

	err = packet_flush_gently(process->in);
	if (err)
		return err;

	err = write_packetized_from_buf(buf, len, process->in);
	if (err)
		return err;

	return check_object_process_status(process->out, status);
}

static int put_object_process(struct odb_helper *o,
			      const void *buf, size_t len,
			      const char *type, unsigned char *sha1)
{
	int err;
	struct object_process *entry;
	struct strbuf status = STRBUF_INIT;

	entry = launch_object_process(o, ODB_HELPER_CAP_PUT_RAW_OBJ);
	if (!entry)
		return -1;

	err = send_put_packets(entry, sha1, buf, len, &status);

	return check_object_process_error(err, status.buf, entry, o->dealer,
					  ODB_HELPER_CAP_PUT_RAW_OBJ);
}

struct odb_helper *odb_helper_new(const char *name, int namelen)
{
	struct odb_helper *o;

	o = xcalloc(1, sizeof(*o));
	o->name = xmemdupz(name, namelen);

	return o;
}

struct odb_helper_cmd {
	struct argv_array argv;
	struct child_process child;
};

/*
 * Callers are responsible to ensure that the result of vaddf(fmt, ap)
 * is properly shell-quoted.
 */
static void prepare_helper_command(struct argv_array *argv, const char *cmd,
				   const char *fmt, va_list ap)
{
	struct strbuf buf = STRBUF_INIT;

	strbuf_addstr(&buf, cmd);
	strbuf_addch(&buf, ' ');
	strbuf_vaddf(&buf, fmt, ap);

	argv_array_push(argv, buf.buf);
	strbuf_release(&buf);
}

__attribute__((format (printf,4,5)))
static int odb_helper_start(struct odb_helper *o,
			    struct odb_helper_cmd *cmd,
			    int use_stdin,
			    const char *fmt, ...)
{
	va_list ap;

	memset(cmd, 0, sizeof(*cmd));
	argv_array_init(&cmd->argv);

	if (!o->dealer)
		return -1;

	va_start(ap, fmt);
	prepare_helper_command(&cmd->argv, o->dealer, fmt, ap);
	va_end(ap);

	cmd->child.argv = cmd->argv.argv;
	cmd->child.use_shell = 1;
	if (use_stdin)
		cmd->child.in = -1;
	else
		cmd->child.no_stdin = 1;
	cmd->child.out = -1;

	if (start_command(&cmd->child) < 0) {
		argv_array_clear(&cmd->argv);
		return -1;
	}

	return 0;
}

static int odb_helper_finish(struct odb_helper *o,
			     struct odb_helper_cmd *cmd)
{
	int ret = finish_command(&cmd->child);
	argv_array_clear(&cmd->argv);
	if (ret) {
		warning("odb helper '%s' reported failure", o->name);
		return -1;
	}
	return 0;
}

static int init_object_script(struct odb_helper *o)
{
	struct odb_helper_cmd cmd;
	FILE *fh;
	struct strbuf line = STRBUF_INIT;

	if (odb_helper_start(o, &cmd, 0, "init") < 0)
		return -1;

	fh = xfdopen(cmd.child.out, "r");
	while (strbuf_getline(&line, fh) != EOF)
		parse_capabilities(line.buf, &o->supported_capabilities, o->name);

	strbuf_release(&line);
	fclose(fh);
	odb_helper_finish(o, &cmd);

	return 0;
}

int odb_helper_init(struct odb_helper *o)
{
	int res = 0;
	uint64_t start;

	if (o->initialized)
		return 0;
	o->initialized = 1;

	start = getnanotime();

	if (o->type == ODB_HELPER_SCRIPT_CMD)
		res = init_object_script(o);
	else if (o->type == ODB_HELPER_SUBPROCESS_CMD)
		res = init_object_process(o);

	trace_performance_since(start, "odb_helper_init");

	return res;
}

static int parse_object_line(struct odb_helper_object *o, const char *line)
{
	char *end;
	if (get_oid_hex(line, &o->oid) < 0)
		return -1;

	line += 40;
	if (*line++ != ' ')
		return -1;

	o->size = strtoul(line, &end, 10);
	if (line == end || *end++ != ' ')
		return -1;

	o->type = type_from_string(end);
	return 0;
}

static int add_have_entry(struct odb_helper *o, const char *line)
{
	ALLOC_GROW(o->have, o->have_nr+1, o->have_alloc);
	if (parse_object_line(&o->have[o->have_nr], line) < 0) {
		warning("bad 'have' input from odb helper '%s': %s",
			o->name, line);
		return 1;
	}
	o->have_nr++;
	return 0;
}

static int odb_helper_object_cmp(const void *va, const void *vb)
{
	const struct odb_helper_object *a = va, *b = vb;
	return oidcmp(&a->oid, &b->oid);
}

static void have_object_script(struct odb_helper *o)
{
	struct odb_helper_cmd cmd;
	FILE *fh;
	struct strbuf line = STRBUF_INIT;

	if (odb_helper_start(o, &cmd, 0, "have") < 0)
		return;

	fh = xfdopen(cmd.child.out, "r");
	while (strbuf_getline(&line, fh) != EOF)
		if (add_have_entry(o, line.buf))
			break;

	strbuf_release(&line);
	fclose(fh);
	odb_helper_finish(o, &cmd);
}

static void odb_helper_load_have(struct odb_helper *o)
{
	if (o->have_valid)
		return;
	o->have_valid = 1;

	if (o->type == ODB_HELPER_SCRIPT_CMD)
		have_object_script(o);

	qsort(o->have, o->have_nr, sizeof(*o->have), odb_helper_object_cmp);
}

static const unsigned char *have_sha1_access(size_t index, void *table)
{
	struct odb_helper_object *have = table;
	return have[index].oid.hash;
}

static struct odb_helper_object *odb_helper_lookup(struct odb_helper *o,
						   const unsigned char *sha1)
{
	int idx;

	odb_helper_load_have(o);
	idx = sha1_pos(sha1, o->have, o->have_nr, have_sha1_access);
	if (idx < 0)
		return NULL;
	return &o->have[idx];
}

int odb_helper_has_object(struct odb_helper *o, const unsigned char *sha1)
{
	return !!odb_helper_lookup(o, sha1);
}

static int odb_helper_get_raw_object(struct odb_helper *o,
				     const unsigned char *sha1,
				     int fd)
{
	struct odb_helper_object *obj;
	struct odb_helper_cmd cmd;
	unsigned long total_got = 0;

	char hdr[32];
	int hdrlen;

	int ret = Z_STREAM_END;
	unsigned char compressed[4096];
	git_zstream stream;
	git_SHA_CTX hash;
	unsigned char real_sha1[20];

	obj = odb_helper_lookup(o, sha1);
	if (!obj)
		return -1;

	if (odb_helper_start(o, &cmd, 0, "get_raw_obj %s", sha1_to_hex(sha1)) < 0)
		return -1;

	/* Set it up */
	git_deflate_init(&stream, zlib_compression_level);
	stream.next_out = compressed;
	stream.avail_out = sizeof(compressed);
	git_SHA1_Init(&hash);

	/* First header.. */
	hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", type_name(obj->type), obj->size) + 1;
	stream.next_in = (unsigned char *)hdr;
	stream.avail_in = hdrlen;
	while (git_deflate(&stream, 0) == Z_OK)
		; /* nothing */
	git_SHA1_Update(&hash, hdr, hdrlen);

	for (;;) {
		unsigned char buf[4096];
		int r;

		r = xread(cmd.child.out, buf, sizeof(buf));
		if (r < 0) {
			error("unable to read from odb helper '%s': %s",
			      o->name, strerror(errno));
			close(cmd.child.out);
			odb_helper_finish(o, &cmd);
			git_deflate_end(&stream);
			return -1;
		}
		if (r == 0)
			break;

		total_got += r;

		/* Then the data itself.. */
		stream.next_in = (void *)buf;
		stream.avail_in = r;
		do {
			unsigned char *in0 = stream.next_in;
			ret = git_deflate(&stream, Z_FINISH);
			git_SHA1_Update(&hash, in0, stream.next_in - in0);
			write_or_die(fd, compressed, stream.next_out - compressed);
			stream.next_out = compressed;
			stream.avail_out = sizeof(compressed);
		} while (ret == Z_OK);
	}

	close(cmd.child.out);
	if (ret != Z_STREAM_END) {
		warning("bad zlib data from odb helper '%s' for %s",
			o->name, sha1_to_hex(sha1));
		return -1;
	}
	ret = git_deflate_end_gently(&stream);
	if (ret != Z_OK) {
		warning("deflateEnd on object %s from odb helper '%s' failed (%d)",
			sha1_to_hex(sha1), o->name, ret);
		return -1;
	}
	git_SHA1_Final(real_sha1, &hash);
	if (hashcmp(sha1, real_sha1)) {
		warning("sha1 mismatch from odb helper '%s' for %s (got %s)",
			o->name, sha1_to_hex(sha1), sha1_to_hex(real_sha1));
		return -1;
	}
	if (odb_helper_finish(o, &cmd))
		return -1;
	if (total_got != obj->size) {
		warning("size mismatch from odb helper '%s' for %s (%lu != %lu)",
			o->name, sha1_to_hex(sha1), total_got, obj->size);
		return -1;
	}

	return 0;
}

static int odb_helper_get_git_object(struct odb_helper *o,
				     const unsigned char *sha1,
				     int fd)
{
	struct odb_helper_object *obj;
	struct odb_helper_cmd cmd;
	unsigned long total_got;
	git_zstream stream;
	int zret = Z_STREAM_END;
	git_SHA_CTX hash;
	unsigned char real_sha1[20];
	struct strbuf header = STRBUF_INIT;
	unsigned long hdr_size;

	obj = odb_helper_lookup(o, sha1);
	if (!obj)
		return -1;

	if (odb_helper_start(o, &cmd, 0, "get_git_obj %s", sha1_to_hex(sha1)) < 0)
		return -1;

	memset(&stream, 0, sizeof(stream));
	git_inflate_init(&stream);
	git_SHA1_Init(&hash);
	total_got = 0;

	for (;;) {
		unsigned char buf[4096];
		int r;

		r = xread(cmd.child.out, buf, sizeof(buf));
		if (r < 0) {
			error("unable to read from odb helper '%s': %s",
			      o->name, strerror(errno));
			close(cmd.child.out);
			odb_helper_finish(o, &cmd);
			git_inflate_end(&stream);
			return -1;
		}
		if (r == 0)
			break;

		write_or_die(fd, buf, r);

		stream.next_in = buf;
		stream.avail_in = r;
		do {
			unsigned char inflated[4096];
			unsigned long got;

			stream.next_out = inflated;
			stream.avail_out = sizeof(inflated);
			zret = git_inflate(&stream, Z_SYNC_FLUSH);
			got = sizeof(inflated) - stream.avail_out;

			git_SHA1_Update(&hash, inflated, got);
			/* skip header when counting size */
			if (!total_got) {
				const unsigned char *p = memchr(inflated, '\0', got);
				if (p) {
					unsigned long hdr_last = p - inflated + 1;
					strbuf_add(&header, inflated, hdr_last);
					got -= hdr_last;
				} else {
					strbuf_add(&header, inflated, got);
					got = 0;
				}
			}
			total_got += got;
		} while (stream.avail_in && zret == Z_OK);
	}

	close(cmd.child.out);
	git_inflate_end(&stream);
	git_SHA1_Final(real_sha1, &hash);
	if (odb_helper_finish(o, &cmd))
		return -1;
	if (zret != Z_STREAM_END) {
		warning("bad zlib data from odb helper '%s' for %s",
			o->name, sha1_to_hex(sha1));
		return -1;
	}
	if (total_got != obj->size) {
		warning("size mismatch from odb helper '%s' for %s (%lu != %lu)",
			o->name, sha1_to_hex(sha1), total_got, obj->size);
		return -1;
	}
	if (hashcmp(real_sha1, sha1)) {
		warning("sha1 mismatch from odb helper '%s' for %s (got %s)",
			o->name, sha1_to_hex(sha1), sha1_to_hex(real_sha1));
		return -1;
	}
	if (parse_sha1_header(header.buf, &hdr_size) < 0) {
		warning("could not parse header from odb helper '%s' for %s",
			o->name, sha1_to_hex(sha1));
		return -1;
	}
	if (total_got != hdr_size) {
		warning("size mismatch from odb helper '%s' for %s (%lu != %lu)",
			o->name, sha1_to_hex(sha1), total_got, hdr_size);
		return -1;
	}

	return 0;
}

static int get_direct_script(struct odb_helper *o, const unsigned char *sha1)
{
	struct odb_helper_cmd cmd;

	if (odb_helper_start(o, &cmd, 0, "get_direct %s", sha1_to_hex(sha1)) < 0)
		return -1;
	if (odb_helper_finish(o, &cmd))
		return -1;
	return 0;
}

int odb_helper_get_direct(struct odb_helper *o,
			  const unsigned char *sha1)
{
	int res = 0;
	uint64_t start;

	if (o->supported_capabilities & ODB_HELPER_CAP_HAVE &&
	    o->type != ODB_HELPER_GIT_REMOTE) {
		struct odb_helper_object *obj = odb_helper_lookup(o, sha1);
		if (!obj)
			return -1;
	}

	start = getnanotime();

	if (o->type == ODB_HELPER_GIT_REMOTE)
		fetch_object(o->dealer, sha1);
	else if (o->type == ODB_HELPER_SCRIPT_CMD)
		res = get_direct_script(o, sha1);
	else
		res = get_object_process(o, sha1, -1);

	trace_performance_since(start, "odb_helper_get_direct");

	return res;
}

int odb_helper_get_many_direct(struct odb_helper *o,
			       const struct oid_array *to_get)
{
	int res = 0;
	uint64_t start;

	start = getnanotime();

	if (o->type == ODB_HELPER_GIT_REMOTE)
		fetch_objects(o->dealer, to_get);

	trace_performance_since(start, "odb_helper_get_many_direct");

	return res;
}

static int get_object_script(struct odb_helper *o, const unsigned char *sha1, int fd)
{
	if (o->supported_capabilities & ODB_HELPER_CAP_GET_GIT_OBJ)
		return odb_helper_get_git_object(o, sha1, fd);
	if (o->supported_capabilities & ODB_HELPER_CAP_GET_RAW_OBJ)
		return odb_helper_get_raw_object(o, sha1, fd);
	if (o->supported_capabilities & ODB_HELPER_CAP_GET_DIRECT)
		return 0;

	BUG("invalid get capability (capabilities: '%d')", o->supported_capabilities);
}

int odb_helper_get_object(struct odb_helper *o,
			  const unsigned char *sha1,
			  int fd)
{
	int res = 0;
	uint64_t start = getnanotime();

	if (o->type == ODB_HELPER_SCRIPT_CMD)
		res = get_object_script(o, sha1, fd);
	else if (o->type == ODB_HELPER_SUBPROCESS_CMD)
		res = get_object_process(o, sha1, fd);

	trace_performance_since(start, "odb_helper_get_object");

	return res;
}

static int put_raw_object_script(struct odb_helper *o,
				 const void *buf, size_t len,
				 const char *type, unsigned char *sha1)
{
	struct odb_helper_cmd cmd;

	if (odb_helper_start(o, &cmd, 1, "put_raw_obj %s %"PRIuMAX" %s",
			     sha1_to_hex(sha1), (uintmax_t)len, type) < 0)
		return -1;

	do {
		int w = xwrite(cmd.child.in, buf, len);
		if (w < 0) {
			error("unable to write to odb helper '%s': %s",
			      o->name, strerror(errno));
			close(cmd.child.in);
			close(cmd.child.out);
			odb_helper_finish(o, &cmd);
			return -1;
		}
		len -= w;
	} while (len > 0);

	close(cmd.child.in);
	close(cmd.child.out);
	odb_helper_finish(o, &cmd);
	return 0;
}

int odb_helper_put_object(struct odb_helper *o,
			  const void *buf, size_t len,
			  const char *type, unsigned char *sha1)
{
	int res = 1;
	uint64_t start = getnanotime();

	if (o->type == ODB_HELPER_SCRIPT_CMD)
		res = put_raw_object_script(o, buf, len, type, sha1);
	else if (o->type == ODB_HELPER_SUBPROCESS_CMD)
		res = put_object_process(o, buf, len, type, sha1);

	trace_performance_since(start, "odb_helper_put_object");

	return res;
}
debug log:

solving 83f8cce3b1 ...
found 83f8cce3b1 in https://public-inbox.org/git/20180319133147.15413-32-chriscool@tuxfamily.org/
found cad3d1005c in https://public-inbox.org/git/20180319133147.15413-29-chriscool@tuxfamily.org/
found fdd062ec8d in https://public-inbox.org/git/20180319133147.15413-26-chriscool@tuxfamily.org/
found 2734af793b in https://public-inbox.org/git/20180319133147.15413-18-chriscool@tuxfamily.org/
found 30cfe689de in https://public-inbox.org/git/20180319133147.15413-16-chriscool@tuxfamily.org/
found 7e5eab94cf in https://public-inbox.org/git/20180319133147.15413-13-chriscool@tuxfamily.org/
found a99f4a1299 in https://public-inbox.org/git/20180319133147.15413-12-chriscool@tuxfamily.org/
found bf3c1ad8ac in https://public-inbox.org/git/20180319133147.15413-11-chriscool@tuxfamily.org/
found 677ea850a5 in https://public-inbox.org/git/20180319133147.15413-10-chriscool@tuxfamily.org/
found c6127ce81f in https://public-inbox.org/git/20180319133147.15413-6-chriscool@tuxfamily.org/
found 67196e6317 in https://public-inbox.org/git/20180319133147.15413-2-chriscool@tuxfamily.org/

applying [1/11] https://public-inbox.org/git/20180319133147.15413-2-chriscool@tuxfamily.org/
diff --git a/odb-helper.c b/odb-helper.c
new file mode 100644
index 0000000000..67196e6317


applying [2/11] https://public-inbox.org/git/20180319133147.15413-6-chriscool@tuxfamily.org/
diff --git a/odb-helper.c b/odb-helper.c
index 67196e6317..c6127ce81f 100644


applying [3/11] https://public-inbox.org/git/20180319133147.15413-10-chriscool@tuxfamily.org/
diff --git a/odb-helper.c b/odb-helper.c
index c6127ce81f..677ea850a5 100644


applying [4/11] https://public-inbox.org/git/20180319133147.15413-11-chriscool@tuxfamily.org/
diff --git a/odb-helper.c b/odb-helper.c
index 677ea850a5..bf3c1ad8ac 100644


applying [5/11] https://public-inbox.org/git/20180319133147.15413-12-chriscool@tuxfamily.org/
diff --git a/odb-helper.c b/odb-helper.c
index bf3c1ad8ac..a99f4a1299 100644


applying [6/11] https://public-inbox.org/git/20180319133147.15413-13-chriscool@tuxfamily.org/
diff --git a/odb-helper.c b/odb-helper.c
index a99f4a1299..7e5eab94cf 100644


applying [7/11] https://public-inbox.org/git/20180319133147.15413-16-chriscool@tuxfamily.org/
diff --git a/odb-helper.c b/odb-helper.c
index 7e5eab94cf..30cfe689de 100644


applying [8/11] https://public-inbox.org/git/20180319133147.15413-18-chriscool@tuxfamily.org/
diff --git a/odb-helper.c b/odb-helper.c
index 30cfe689de..2734af793b 100644


applying [9/11] https://public-inbox.org/git/20180319133147.15413-26-chriscool@tuxfamily.org/
diff --git a/odb-helper.c b/odb-helper.c
index 2734af793b..fdd062ec8d 100644


applying [10/11] https://public-inbox.org/git/20180319133147.15413-29-chriscool@tuxfamily.org/
diff --git a/odb-helper.c b/odb-helper.c
index fdd062ec8d..cad3d1005c 100644


applying [11/11] https://public-inbox.org/git/20180319133147.15413-32-chriscool@tuxfamily.org/
diff --git a/odb-helper.c b/odb-helper.c
index cad3d1005c..83f8cce3b1 100644

Checking patch odb-helper.c...
11:60: new blank line at EOF.
+
Applied patch odb-helper.c cleanly.
Checking patch odb-helper.c...
Applied patch odb-helper.c cleanly.
Checking patch odb-helper.c...
Applied patch odb-helper.c cleanly.
Checking patch odb-helper.c...
Applied patch odb-helper.c cleanly.
Checking patch odb-helper.c...
Applied patch odb-helper.c cleanly.
Checking patch odb-helper.c...
Applied patch odb-helper.c cleanly.
Checking patch odb-helper.c...
Applied patch odb-helper.c cleanly.
Checking patch odb-helper.c...
Applied patch odb-helper.c cleanly.
Checking patch odb-helper.c...
Applied patch odb-helper.c cleanly.
Checking patch odb-helper.c...
Applied patch odb-helper.c cleanly.
Checking patch odb-helper.c...
Applied patch odb-helper.c cleanly.
warning: 1 line adds whitespace errors.

index at:
100644 83f8cce3b12af7b68e31d9cac0b0bd640723f9da	odb-helper.c

Code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).