git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
412db53d28defb706278f116520dcb93acf07670 blob 42264 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
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
 
#include "cache.h"
#include "read-cache.h"
#include "string-list.h"
#include "resolve-undo.h"
#include "cache-tree.h"
#include "dir.h"

#define ptr_add(x,y) ((void *)(((char *)(x)) + (y)))

struct cache_header {
	unsigned int hdr_ndir;
	unsigned int hdr_nfile;
	unsigned int hdr_fblockoffset;
	unsigned int hdr_nextension;
};

/*****************************************************************
 * Index File I/O
 *****************************************************************/

struct ondisk_cache_entry {
	unsigned short flags;
	unsigned short mode;
	struct cache_time mtime;
	unsigned int size;
	int stat_crc;
	unsigned char sha1[20];
};

struct ondisk_directory_entry {
	unsigned int foffset;
	unsigned int cr;
	unsigned int ncr;
	unsigned int nsubtrees;
	unsigned int nfiles;
	unsigned int nentries;
	unsigned char sha1[20];
	unsigned short flags;
};

static int check_crc32(int initialcrc,
			void *data,
			size_t len,
			unsigned int expected_crc)
{
	int crc;

	crc = crc32(initialcrc, (Bytef*)data, len);
	return crc == expected_crc;
}

static int match_stat_crc(struct stat *st, uint32_t expected_crc)
{
	uint32_t data, stat_crc = 0;
	unsigned int ctimens = 0;

	data = htonl(st->st_ctime);
	stat_crc = crc32(0, (Bytef*)&data, 4);
#ifdef USE_NSEC
	ctimens = ST_CTIME_NSEC(*st);
#endif
	data = htonl(ctimens);
	stat_crc = crc32(stat_crc, (Bytef*)&data, 4);
	data = htonl(st->st_ino);
	stat_crc = crc32(stat_crc, (Bytef*)&data, 4);
	data = htonl(st->st_dev);
	stat_crc = crc32(stat_crc, (Bytef*)&data, 4);
	data = htonl(st->st_uid);
	stat_crc = crc32(stat_crc, (Bytef*)&data, 4);
	data = htonl(st->st_gid);
	stat_crc = crc32(stat_crc, (Bytef*)&data, 4);

	return stat_crc == expected_crc;
}

static int match_stat_basic(const struct cache_entry *ce,
			    struct stat *st,
			    int changed)
{

	if (ce->ce_stat_data.sd_mtime.sec != (unsigned int)st->st_mtime)
		changed |= MTIME_CHANGED;
#ifdef USE_NSEC
	if (ce->ce_stat_data.sd_mtime.nsec != ST_MTIME_NSEC(*st))
		changed |= MTIME_CHANGED;
#endif
	if (ce->ce_stat_data.sd_size != (unsigned int)st->st_size)
		changed |= DATA_CHANGED;

	if (trust_ctime && ce->ce_stat_crc != 0 && !match_stat_crc(st, ce->ce_stat_crc)) {
		changed |= OWNER_CHANGED;
		changed |= INODE_CHANGED;
	}
	/* Racily smudged entry? */
	if (ce->ce_flags & CE_SMUDGED) {
		if (!changed && !is_empty_blob_sha1(ce->sha1) && ce_modified_check_fs(ce, st))
			changed |= DATA_CHANGED;
	}
	return changed;
}

static int verify_hdr(void *mmap, unsigned long size)
{
	uint32_t *filecrc;
	unsigned int header_size;
	struct cache_version_header *hdr;
	struct cache_header *hdr_v5;

	if (size < sizeof(struct cache_version_header)
			+ sizeof (struct cache_header) + 4)
		die("index file smaller than expected");

	hdr = mmap;
	hdr_v5 = ptr_add(mmap, sizeof(*hdr));
	/* Size of the header + the size of the extensionoffsets */
	header_size = sizeof(*hdr) + sizeof(*hdr_v5) + hdr_v5->hdr_nextension * 4;
	/* Initialize crc */
	filecrc = ptr_add(mmap, header_size);
	if (!check_crc32(0, hdr, header_size, ntohl(*filecrc)))
		return error("bad index file header crc signature");
	return 0;
}

static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *ondisk,
						   struct directory_entry *de,
						   char *name,
						   size_t len,
						   size_t prefix_len)
{
	struct cache_entry *ce = xmalloc(cache_entry_size(len + de->de_pathlen));
	int flags;

	flags = ntoh_s(ondisk->flags);
	ce->ce_stat_data.sd_ctime.sec  = 0;
	ce->ce_stat_data.sd_mtime.sec  = ntoh_l(ondisk->mtime.sec);
	ce->ce_stat_data.sd_ctime.nsec = 0;
	ce->ce_stat_data.sd_mtime.nsec = ntoh_l(ondisk->mtime.nsec);
	ce->ce_stat_data.sd_dev        = 0;
	ce->ce_stat_data.sd_ino        = 0;
	ce->ce_stat_data.sd_uid        = 0;
	ce->ce_stat_data.sd_gid        = 0;
	ce->ce_stat_data.sd_size       = ntoh_l(ondisk->size);
	ce->ce_mode       = ntoh_s(ondisk->mode);
	ce->ce_flags      = flags & CE_STAGEMASK;
	ce->ce_flags     |= flags & CE_VALID;
	ce->ce_flags     |= flags & CE_SMUDGED;
	if (flags & CE_INTENT_TO_ADD_V5)
		ce->ce_flags |= CE_INTENT_TO_ADD;
	if (flags & CE_SKIP_WORKTREE_V5)
		ce->ce_flags |= CE_SKIP_WORKTREE;
	ce->ce_stat_crc   = ntoh_l(ondisk->stat_crc);
	ce->ce_namelen    = len + de->de_pathlen;
	hashcpy(ce->sha1, ondisk->sha1);
	memcpy(ce->name, de->pathname, de->de_pathlen);
	memcpy(ce->name + de->de_pathlen, name, len);
	ce->name[len + de->de_pathlen] = '\0';
	ce->next_ce = NULL;
	return ce;
}

static struct directory_entry *directory_entry_from_ondisk(struct ondisk_directory_entry *ondisk,
						   const char *name,
						   size_t len)
{
	struct directory_entry *de = xmalloc(directory_entry_size(len));


	memcpy(de->pathname, name, len);
	de->pathname[len] = '\0';
	de->de_flags      = ntoh_s(ondisk->flags);
	de->de_foffset    = ntoh_l(ondisk->foffset);
	de->de_cr         = ntoh_l(ondisk->cr);
	de->de_ncr        = ntoh_l(ondisk->ncr);
	de->de_nsubtrees  = ntoh_l(ondisk->nsubtrees);
	de->de_nfiles     = ntoh_l(ondisk->nfiles);
	de->de_nentries   = ntoh_l(ondisk->nentries);
	de->de_pathlen    = len;
	hashcpy(de->sha1, ondisk->sha1);
	return de;
}

static struct conflict_part *conflict_part_from_ondisk(struct ondisk_conflict_part *ondisk)
{
	struct conflict_part *cp = xmalloc(sizeof(struct conflict_part));

	cp->flags      = ntoh_s(ondisk->flags);
	cp->entry_mode = ntoh_s(ondisk->entry_mode);
	hashcpy(cp->sha1, ondisk->sha1);
	return cp;
}

static struct cache_entry *convert_conflict_part(struct conflict_part *cp,
						char * name,
						unsigned int len)
{

	struct cache_entry *ce = xmalloc(cache_entry_size(len));

	ce->ce_stat_data.sd_ctime.sec  = 0;
	ce->ce_stat_data.sd_mtime.sec  = 0;
	ce->ce_stat_data.sd_ctime.nsec = 0;
	ce->ce_stat_data.sd_mtime.nsec = 0;
	ce->ce_stat_data.sd_dev        = 0;
	ce->ce_stat_data.sd_ino        = 0;
	ce->ce_stat_data.sd_uid        = 0;
	ce->ce_stat_data.sd_gid        = 0;
	ce->ce_stat_data.sd_size       = 0;
	ce->ce_mode       = cp->entry_mode;
	ce->ce_flags      = conflict_stage(cp) << CE_STAGESHIFT;
	ce->ce_stat_crc   = 0;
	ce->ce_namelen    = len;
	hashcpy(ce->sha1, cp->sha1);
	memcpy(ce->name, name, len);
	ce->name[len] = '\0';
	return ce;
}

static struct directory_entry *read_directories(unsigned int *dir_offset,
				unsigned int *dir_table_offset,
				void *mmap,
				int mmap_size)
{
	int i, ondisk_directory_size;
	uint32_t *filecrc, *beginning, *end;
	struct directory_entry *current = NULL;
	struct ondisk_directory_entry *disk_de;
	struct directory_entry *de;
	unsigned int data_len, len;
	char *name;

	/* Length of pathname + nul byte for termination + size of
	 * members of ondisk_directory_entry. (Just using the size
	 * of the stuct doesn't work, because there may be padding
	 * bytes for the struct)
	 */
	ondisk_directory_size = sizeof(disk_de->flags)
		+ sizeof(disk_de->foffset)
		+ sizeof(disk_de->cr)
		+ sizeof(disk_de->ncr)
		+ sizeof(disk_de->nsubtrees)
		+ sizeof(disk_de->nfiles)
		+ sizeof(disk_de->nentries)
		+ sizeof(disk_de->sha1);
	name = ptr_add(mmap, *dir_offset);
	beginning = ptr_add(mmap, *dir_table_offset);
	end = ptr_add(mmap, *dir_table_offset + 4);
	len = ntoh_l(*end) - ntoh_l(*beginning) - ondisk_directory_size - 5;
	disk_de = ptr_add(mmap, *dir_offset + len + 1);
	de = directory_entry_from_ondisk(disk_de, name, len);
	de->next = NULL;

	data_len = len + 1 + ondisk_directory_size;
	filecrc = ptr_add(mmap, *dir_offset + data_len);
	if (!check_crc32(0, ptr_add(mmap, *dir_offset), data_len, ntoh_l(*filecrc)))
		goto unmap;

	*dir_table_offset += 4;
	*dir_offset += data_len + 4; /* crc code */

	current = de;
	for (i = 0; i < de->de_nsubtrees; i++) {
		current->next = read_directories(dir_offset, dir_table_offset,
						mmap, mmap_size);
		while (current->next)
			current = current->next;
	}

	return de;
unmap:
	munmap(mmap, mmap_size);
	die("directory crc doesn't match for '%s'", de->pathname);
}

static int read_entry(struct cache_entry **ce, struct directory_entry *de,
		      unsigned int *entry_offset,
		      void **mmap, unsigned long mmap_size,
		      unsigned int *foffsetblock)
{
	int len, offset_to_offset;
	char *name;
	uint32_t foffsetblockcrc;
	uint32_t *filecrc, *beginning, *end;
	struct ondisk_cache_entry *disk_ce;

	name = ptr_add(*mmap, *entry_offset);
	beginning = ptr_add(*mmap, *foffsetblock);
	end = ptr_add(*mmap, *foffsetblock + 4);
	len = ntoh_l(*end) - ntoh_l(*beginning) - sizeof(struct ondisk_cache_entry) - 5;
	disk_ce = ptr_add(*mmap, *entry_offset + len + 1);
	*ce = cache_entry_from_ondisk(disk_ce, de, name, len, de->de_pathlen);
	filecrc = ptr_add(*mmap, *entry_offset + len + 1 + sizeof(*disk_ce));
	offset_to_offset = htonl(*foffsetblock);
	foffsetblockcrc = crc32(0, (Bytef*)&offset_to_offset, 4);
	if (!check_crc32(foffsetblockcrc,
		ptr_add(*mmap, *entry_offset), len + 1 + sizeof(*disk_ce),
		ntoh_l(*filecrc)))
		return -1;

	*entry_offset += len + 1 + sizeof(*disk_ce) + 4;
	return 0;
}

static void ce_queue_push(struct cache_entry **head,
			     struct cache_entry **tail,
			     struct cache_entry *ce)
{
	if (!*head) {
		*head = *tail = ce;
		(*tail)->next = NULL;
		return;
	}

	(*tail)->next = ce;
	ce->next = NULL;
	*tail = (*tail)->next;
}

static void conflict_entry_push(struct conflict_entry **head,
				struct conflict_entry **tail,
				struct conflict_entry *conflict_entry)
{
	if (!*head) {
		*head = *tail = conflict_entry;
		(*tail)->next = NULL;
		return;
	}

	(*tail)->next = conflict_entry;
	conflict_entry->next = NULL;
	*tail = (*tail)->next;
}

static struct cache_entry *ce_queue_pop(struct cache_entry **head)
{
	struct cache_entry *ce;

	ce = *head;
	*head = (*head)->next;
	return ce;
}

static void conflict_part_head_remove(struct conflict_part **head)
{
	struct conflict_part *to_free;

	to_free = *head;
	*head = (*head)->next;
	free(to_free);
}

static void conflict_entry_head_remove(struct conflict_entry **head)
{
	struct conflict_entry *to_free;

	to_free = *head;
	*head = (*head)->next;
	free(to_free);
}

struct conflict_entry *create_new_conflict(char *name, int len, int pathlen)
{
	struct conflict_entry *conflict_entry;

	if (pathlen)
		pathlen++;
	conflict_entry = xmalloc(conflict_entry_size(len));
	conflict_entry->entries = NULL;
	conflict_entry->nfileconflicts = 0;
	conflict_entry->namelen = len;
	memcpy(conflict_entry->name, name, len);
	conflict_entry->name[len] = '\0';
	conflict_entry->pathlen = pathlen;
	conflict_entry->next = NULL;

	return conflict_entry;
}

void add_part_to_conflict_entry(struct directory_entry *de,
					struct conflict_entry *entry,
					struct conflict_part *conflict_part)
{

	struct conflict_part *conflict_search;

	entry->nfileconflicts++;
	de->conflict_size += sizeof(struct ondisk_conflict_part);
	if (!entry->entries)
		entry->entries = conflict_part;
	else {
		conflict_search = entry->entries;
		while (conflict_search->next)
			conflict_search = conflict_search->next;
		conflict_search->next = conflict_part;
	}
}

static int read_conflicts(struct conflict_entry **head,
			  struct directory_entry *de,
			  void **mmap, unsigned long mmap_size)
{
	struct conflict_entry *tail;
	unsigned int croffset, i;
	char *full_name;

	croffset = de->de_cr;
	tail = NULL;
	for (i = 0; i < de->de_ncr; i++) {
		struct conflict_entry *conflict_new;
		unsigned int len, *nfileconflicts;
		char *name;
		void *crc_start;
		int k, offset;
		uint32_t *filecrc;

		offset = croffset;
		crc_start = ptr_add(*mmap, offset);
		name = ptr_add(*mmap, offset);
		len = strlen(name);
		offset += len + 1;
		nfileconflicts = ptr_add(*mmap, offset);
		offset += 4;

		full_name = xmalloc(sizeof(char) * (len + de->de_pathlen));
		memcpy(full_name, de->pathname, de->de_pathlen);
		memcpy(full_name + de->de_pathlen, name, len);
		conflict_new = create_new_conflict(full_name,
				len + de->de_pathlen, de->de_pathlen);
		for (k = 0; k < ntoh_l(*nfileconflicts); k++) {
			struct ondisk_conflict_part *ondisk;
			struct conflict_part *cp;

			ondisk = ptr_add(*mmap, offset);
			cp = conflict_part_from_ondisk(ondisk);
			cp->next = NULL;
			add_part_to_conflict_entry(de, conflict_new, cp);
			offset += sizeof(struct ondisk_conflict_part);
		}
		filecrc = ptr_add(*mmap, offset);
		free(full_name);
		if (!check_crc32(0, crc_start,
			len + 1 + 4 + conflict_new->nfileconflicts
			* sizeof(struct ondisk_conflict_part),
			ntoh_l(*filecrc)))
			return -1;
		croffset = offset + 4;
		conflict_entry_push(head, &tail, conflict_new);
	}
	return 0;
}

static struct cache_tree *convert_one(struct directory_queue *queue, int dirnr)
{
	int i, subtree_nr;
	struct cache_tree *it;
	struct directory_queue *down;

	it = cache_tree();
	it->entry_count = queue[dirnr].de->de_nentries;
	subtree_nr = queue[dirnr].de->de_nsubtrees;
	if (0 <= it->entry_count)
		hashcpy(it->sha1, queue[dirnr].de->sha1);

	/*
	 * Just a heuristic -- we do not add directories that often but
	 * we do not want to have to extend it immediately when we do,
	 * hence +2.
	 */
	it->subtree_alloc = subtree_nr + 2;
	it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
	down = queue[dirnr].down;
	for (i = 0; i < subtree_nr; i++) {
		struct cache_tree *sub;
		struct cache_tree_sub *subtree;
		char *buf, *name;

		name = "";
		buf = strtok(down[i].de->pathname, "/");
		while (buf) {
			name = buf;
			buf = strtok(NULL, "/");
		}
		sub = convert_one(down, i);
		if(!sub)
			goto free_return;
		subtree = cache_tree_sub(it, name);
		subtree->cache_tree = sub;
	}
	if (subtree_nr != it->subtree_nr)
		die("cache-tree: internal error");
	return it;
 free_return:
	cache_tree_free(&it);
	return NULL;
}

static int compare_cache_tree_elements(const void *a, const void *b)
{
	const struct directory_entry *de1, *de2;

	de1 = ((const struct directory_queue *)a)->de;
	de2 = ((const struct directory_queue *)b)->de;
	return subtree_name_cmp(de1->pathname, de1->de_pathlen,
				de2->pathname, de2->de_pathlen);
}

static struct directory_entry *sort_directories(struct directory_entry *de,
						struct directory_queue *queue)
{
	int i, nsubtrees;

	nsubtrees = de->de_nsubtrees;
	for (i = 0; i < nsubtrees; i++) {
		struct directory_entry *new_de;
		de = de->next;
		new_de = xmalloc(directory_entry_size(de->de_pathlen));
		memcpy(new_de, de, directory_entry_size(de->de_pathlen));
		queue[i].de = new_de;
		if (de->de_nsubtrees) {
			queue[i].down = xcalloc(de->de_nsubtrees,
					sizeof(struct directory_queue));
			de = sort_directories(de,
					queue[i].down);
		}
	}
	qsort(queue, nsubtrees, sizeof(struct directory_queue),
			compare_cache_tree_elements);
	return de;
}

/*
 * This function modifys the directory argument that is given to it.
 * Don't use it if the directory entries are still needed after.
 */
static struct cache_tree *cache_tree_convert_v5(struct directory_entry *de)
{
	struct directory_queue *queue;

	if (!de->de_nentries)
		return NULL;
	queue = xcalloc(1, sizeof(struct directory_queue));
	queue[0].de = de;
	queue[0].down = xcalloc(de->de_nsubtrees, sizeof(struct directory_queue));

	sort_directories(de, queue[0].down);
	return convert_one(queue, 0);
}

static void resolve_undo_convert_v5(struct index_state *istate,
				    struct conflict_entry *conflict)
{
	int i;

	while (conflict) {
		struct string_list_item *lost;
		struct resolve_undo_info *ui;
		struct conflict_part *cp;

		if (conflict->entries &&
		    (conflict->entries->flags & CONFLICT_CONFLICTED) != 0) {
			conflict = conflict->next;
			continue;
		}
		if (!istate->resolve_undo) {
			istate->resolve_undo = xcalloc(1, sizeof(struct string_list));
			istate->resolve_undo->strdup_strings = 1;
		}

		lost = string_list_insert(istate->resolve_undo, conflict->name);
		if (!lost->util)
			lost->util = xcalloc(1, sizeof(*ui));
		ui = lost->util;

		cp = conflict->entries;
		for (i = 0; i < 3; i++)
			ui->mode[i] = 0;
		while (cp) {
			ui->mode[conflict_stage(cp) - 1] = cp->entry_mode;
			hashcpy(ui->sha1[conflict_stage(cp) - 1], cp->sha1);
			cp = cp->next;
		}
		conflict = conflict->next;
	}
}

static int read_entries(struct index_state *istate, struct directory_entry **de,
			unsigned int *entry_offset, void **mmap,
			unsigned long mmap_size, unsigned int *nr,
			unsigned int *foffsetblock, struct cache_entry **prev)
{
	struct cache_entry *head = NULL, *tail = NULL;
	struct conflict_entry *conflict_queue;
	struct cache_entry *ce;
	int i;

	conflict_queue = NULL;
	if (read_conflicts(&conflict_queue, *de, mmap, mmap_size) < 0)
		return -1;
	resolve_undo_convert_v5(istate, conflict_queue);
	for (i = 0; i < (*de)->de_nfiles; i++) {
		if (read_entry(&ce,
			       *de,
			       entry_offset,
			       mmap,
			       mmap_size,
			       foffsetblock) < 0)
			return -1;
		ce_queue_push(&head, &tail, ce);
		*foffsetblock += 4;

		/*
		 * Add the conflicted entries at the end of the index file
		 * to the in memory format
		 */
		if (conflict_queue &&
		    (conflict_queue->entries->flags & CONFLICT_CONFLICTED) != 0 &&
		    !cache_name_compare(conflict_queue->name, conflict_queue->namelen,
					ce->name, ce_namelen(ce))) {
			struct conflict_part *cp;
			cp = conflict_queue->entries;
			cp = cp->next;
			while (cp) {
				ce = convert_conflict_part(cp,
						conflict_queue->name,
						conflict_queue->namelen);
				ce_queue_push(&head, &tail, ce);
				conflict_part_head_remove(&cp);
			}
			conflict_entry_head_remove(&conflict_queue);
		}
	}

	*de = (*de)->next;

	while (head) {
		if (*de != NULL
		    && strcmp(head->name, (*de)->pathname) > 0) {
			read_entries(istate,
				     de,
				     entry_offset,
				     mmap,
				     mmap_size,
				     nr,
				     foffsetblock,
				     prev);
		} else {
			ce = ce_queue_pop(&head);
			set_index_entry(istate, *nr, ce);
			if (*prev)
				(*prev)->next_ce = ce;
			(*nr)++;
			*prev = ce;
			ce->next = NULL;
		}
	}
	return 0;
}

static struct directory_entry *read_head_directories(struct index_state *istate,
						     unsigned int *entry_offset,
						     unsigned int *foffsetblock,
						     unsigned int *ndirs,
						     void *mmap, unsigned long mmap_size)
{
	unsigned int dir_offset, dir_table_offset;
	struct cache_version_header *hdr;
	struct cache_header *hdr_v5;
	struct directory_entry *root_directory;

	hdr = mmap;
	hdr_v5 = ptr_add(mmap, sizeof(*hdr));
	istate->version = ntohl(hdr->hdr_version);
	istate->cache_alloc = alloc_nr(ntohl(hdr_v5->hdr_nfile));
	istate->cache = xcalloc(istate->cache_alloc, sizeof(struct cache_entry *));
	istate->initialized = 1;

	/* Skip size of the header + crc sum + size of offsets */
	dir_offset = sizeof(*hdr) + sizeof(*hdr_v5) + 4 + (ntohl(hdr_v5->hdr_ndir) + 1) * 4;
	dir_table_offset = sizeof(*hdr) + sizeof(*hdr_v5) + 4;
	root_directory = read_directories(&dir_offset, &dir_table_offset,
					  mmap, mmap_size);

	*entry_offset = ntohl(hdr_v5->hdr_fblockoffset);
	*foffsetblock = dir_offset;
	*ndirs = ntohl(hdr_v5->hdr_ndir);
	return root_directory;
}

static int read_index_filtered_v5(struct index_state *istate, void *mmap,
				  unsigned long mmap_size, struct filter_opts *opts)
{
	unsigned int entry_offset, ndirs, foffsetblock, nr = 0;
	struct directory_entry *root_directory, *de;
	int i, n;
	const char **adjusted_pathspec = NULL;
	int need_root = 1;
	char *seen, *oldpath;
	struct cache_entry *prev = NULL;

	root_directory = read_head_directories(istate, &entry_offset,
					       &foffsetblock, &ndirs,
					       mmap, mmap_size);

	if (opts && opts->pathspec) {
		need_root = 0;
		seen = xcalloc(1, ndirs);
		for (de = root_directory; de; de = de->next)
			match_pathspec(opts->pathspec, de->pathname, de->de_pathlen, 0, seen);
		for (n = 0; opts->pathspec[n]; n++)
			/* just count */;
		adjusted_pathspec = xmalloc((n+1)*sizeof(char *));
		adjusted_pathspec[n] = NULL;
		for (i = 0; i < n; i++) {
			if (seen[i] == MATCHED_EXACTLY)
				adjusted_pathspec[i] = opts->pathspec[i];
			else {
				char *super = strdup(opts->pathspec[i]);
				int len = strlen(super);
				while (len && super[len - 1] == '/')
					super[--len] = '\0'; /* strip trailing / */
				while (len && super[--len] != '/')
					; /* scan backwards to next / */
				if (len >= 0)
					super[len--] = '\0';
				if (len <= 0) {
					need_root = 1;
					break;
				}
				adjusted_pathspec[i] = super;
			}
		}
	}

	de = root_directory;
	while (de) {
		if (need_root ||
		    match_pathspec(adjusted_pathspec, de->pathname, de->de_pathlen, 0, NULL)) {
			unsigned int subdir_foffsetblock = de->de_foffset + foffsetblock;
			unsigned int *off = mmap + subdir_foffsetblock;
			unsigned int subdir_entry_offset = entry_offset + ntoh_l(*off);
			oldpath = de->pathname;
			do {
				if (read_entries(istate, &de, &subdir_entry_offset,
						 &mmap, mmap_size, &nr,
						 &subdir_foffsetblock, &prev) < 0)
					return -1;
			} while (de && !prefixcmp(de->pathname, oldpath));
		} else
			de = de->next;
	}
	istate->cache_tree = cache_tree_convert_v5(root_directory);
	istate->cache_nr = nr;
	istate->partially_read = 1;
	return 0;
}

static int read_index_v5(struct index_state *istate, void *mmap,
			 unsigned long mmap_size, struct filter_opts *opts)
{
	unsigned int entry_offset, ndirs, foffsetblock, nr = 0;
	struct directory_entry *root_directory, *de;
	struct cache_entry *prev = NULL;

	if (opts != NULL)
		return read_index_filtered_v5(istate, mmap, mmap_size, opts);

	root_directory = read_head_directories(istate, &entry_offset,
					       &foffsetblock, &ndirs,
					       mmap, mmap_size);
	de = root_directory;
	while (de)
		if (read_entries(istate, &de, &entry_offset, &mmap,
				 mmap_size, &nr, &foffsetblock, &prev) < 0)
			return -1;

	istate->cache_tree = cache_tree_convert_v5(root_directory);
	istate->cache_nr = nr;
	istate->partially_read = 0;
	return 0;
}

static void index_change_filter_opts_v5(struct index_state *istate, struct filter_opts *opts)
{
	if (istate->initialized == 1 &&
	    (((istate->filter_opts == NULL || opts == NULL) && istate->filter_opts != opts)
	     || (!memcmp(istate->filter_opts, opts, sizeof(*opts)))))
		return;
	discard_index(istate);
	read_index_filtered(istate, opts);
}

#define WRITE_BUFFER_SIZE 8192
static unsigned char write_buffer[WRITE_BUFFER_SIZE];
static unsigned long write_buffer_len;

static int ce_write_flush(int fd)
{
	unsigned int buffered = write_buffer_len;
	if (buffered) {
		if (write_in_full(fd, write_buffer, buffered) != buffered)
			return -1;
		write_buffer_len = 0;
	}
	return 0;
}

static int ce_write(uint32_t *crc, int fd, void *data, unsigned int len)
{
	if (crc)
		*crc = crc32(*crc, (Bytef*)data, len);
	while (len) {
		unsigned int buffered = write_buffer_len;
		unsigned int partial = WRITE_BUFFER_SIZE - buffered;
		if (partial > len)
			partial = len;
		memcpy(write_buffer + buffered, data, partial);
		buffered += partial;
		if (buffered == WRITE_BUFFER_SIZE) {
			write_buffer_len = buffered;
			if (ce_write_flush(fd))
				return -1;
			buffered = 0;
		}
		write_buffer_len = buffered;
		len -= partial;
		data = (char *) data + partial;
	}
	return 0;
}

static int ce_flush(int fd)
{
	unsigned int left = write_buffer_len;

	if (left)
		write_buffer_len = 0;

	if (write_in_full(fd, write_buffer, left) != left)
		return -1;

	return 0;
}

static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
{
	/*
	 * This method shall only be called if the timestamp of ce
	 * is racy (check with is_racy_timestamp). If the timestamp
	 * is racy, the writer will set the CE_SMUDGED flag.
	 *
	 * The reader (match_stat_basic) will then take care
	 * of checking if the entry is really changed or not, by
	 * taking into account the size and the stat_crc and if
	 * that hasn't changed checking the sha1.
	 */
	ce->ce_flags |= CE_SMUDGED;
}

char *super_directory(const char *filename)
{
	char *slash;

	slash = strrchr(filename, '/');
	if (slash)
		return xmemdupz(filename, slash-filename);
	return NULL;
}

struct directory_entry *init_directory_entry(char *pathname, int len)
{
	struct directory_entry *de = xmalloc(directory_entry_size(len));

	memcpy(de->pathname, pathname, len);
	de->pathname[len] = '\0';
	de->de_flags      = 0;
	de->de_foffset    = 0;
	de->de_cr         = 0;
	de->de_ncr        = 0;
	de->de_nsubtrees  = 0;
	de->de_nfiles     = 0;
	de->de_nentries   = 0;
	memset(de->sha1, 0, 20);
	de->de_pathlen    = len;
	de->next          = NULL;
	de->next_hash     = NULL;
	de->ce            = NULL;
	de->ce_last       = NULL;
	de->conflict      = NULL;
	de->conflict_last = NULL;
	de->conflict_size = 0;
	return de;
}

static void ondisk_from_directory_entry(struct directory_entry *de,
					struct ondisk_directory_entry *ondisk)
{
	ondisk->foffset   = htonl(de->de_foffset);
	ondisk->cr        = htonl(de->de_cr);
	ondisk->ncr       = htonl(de->de_ncr);
	ondisk->nsubtrees = htonl(de->de_nsubtrees);
	ondisk->nfiles    = htonl(de->de_nfiles);
	ondisk->nentries  = htonl(de->de_nentries);
	hashcpy(ondisk->sha1, de->sha1);
	ondisk->flags     = htons(de->de_flags);
}

static struct conflict_part *conflict_part_from_inmemory(struct cache_entry *ce)
{
	struct conflict_part *conflict;
	int flags;

	conflict = xmalloc(sizeof(struct conflict_part));
	flags                = CONFLICT_CONFLICTED;
	flags               |= ce_stage(ce) << CONFLICT_STAGESHIFT;
	conflict->flags      = flags;
	conflict->entry_mode = ce->ce_mode;
	conflict->next       = NULL;
	hashcpy(conflict->sha1, ce->sha1);
	return conflict;
}

static void conflict_to_ondisk(struct conflict_part *cp,
				struct ondisk_conflict_part *ondisk)
{
	ondisk->flags      = htons(cp->flags);
	ondisk->entry_mode = htons(cp->entry_mode);
	hashcpy(ondisk->sha1, cp->sha1);
}

void add_conflict_to_directory_entry(struct directory_entry *de,
					struct conflict_entry *conflict_entry)
{
	de->de_ncr++;
	de->conflict_size += conflict_entry->namelen + 1 + 8 - conflict_entry->pathlen;
	conflict_entry_push(&de->conflict, &de->conflict_last, conflict_entry);
}

void insert_directory_entry(struct directory_entry *de,
			struct hash_table *table,
			int *total_dir_len,
			unsigned int *ndir,
			uint32_t crc)
{
	struct directory_entry *insert;

	insert = (struct directory_entry *)insert_hash(crc, de, table);
	if (insert) {
		de->next_hash = insert->next_hash;
		insert->next_hash = de;
	}
	(*ndir)++;
	if (de->de_pathlen == 0)
		(*total_dir_len)++;
	else
		*total_dir_len += de->de_pathlen + 2;
}

static struct conflict_entry *create_conflict_entry_from_ce(struct cache_entry *ce,
								int pathlen)
{
	return create_new_conflict(ce->name, ce_namelen(ce), pathlen);
}

static void convert_one_to_ondisk_v5(struct hash_table *table, struct cache_tree *it,
				const char *path, int pathlen, uint32_t crc)
{
	int i;
	struct directory_entry *found, *search;

	crc = crc32(crc, (Bytef*)path, pathlen);
	found = lookup_hash(crc, table);
	search = found;
	while (search && strcmp(path, search->pathname + search->de_pathlen - strlen(path)) != 0)
		search = search->next_hash;
	if (!search)
		return;
	/*
	 * The number of subtrees is already calculated by
	 * compile_directory_data, therefore we only need to
	 * add the entry_count
	 */
	search->de_nentries = it->entry_count;
	if (0 <= it->entry_count)
		hashcpy(search->sha1, it->sha1);
	if (strcmp(path, "") != 0)
		crc = crc32(crc, (Bytef*)"/", 1);

#if DEBUG
	if (0 <= it->entry_count)
		fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
			pathlen, path, it->entry_count, it->subtree_nr,
			sha1_to_hex(it->sha1));
	else
		fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
			pathlen, path, it->subtree_nr);
#endif

	for (i = 0; i < it->subtree_nr; i++) {
		struct cache_tree_sub *down = it->down[i];
		if (i) {
			struct cache_tree_sub *prev = it->down[i-1];
			if (subtree_name_cmp(down->name, down->namelen,
					     prev->name, prev->namelen) <= 0)
				die("fatal - unsorted cache subtree");
		}
		convert_one_to_ondisk_v5(table, down->cache_tree, down->name, down->namelen, crc);
	}
}

static void cache_tree_to_ondisk_v5(struct hash_table *table, struct cache_tree *root)
{
	convert_one_to_ondisk_v5(table, root, "", 0, 0);
}

static void resolve_undo_to_ondisk_v5(struct hash_table *table,
				      struct string_list *resolve_undo,
				      unsigned int *ndir, int *total_dir_len,
				      struct directory_entry *de)
{
	struct string_list_item *item;
	struct directory_entry *search;

	if (!resolve_undo)
		return;
	for_each_string_list_item(item, resolve_undo) {
		struct conflict_entry *conflict_entry;
		struct resolve_undo_info *ui = item->util;
		char *super;
		int i, dir_len, len;
		uint32_t crc;
		struct directory_entry *found, *current, *new_tree;

		if (!ui)
			continue;

		super = super_directory(item->string);
		if (!super)
			dir_len = 0;
		else
			dir_len = strlen(super);
		crc = crc32(0, (Bytef*)super, dir_len);
		found = lookup_hash(crc, table);
		current = NULL;
		new_tree = NULL;

		while (!found) {
			struct directory_entry *new;

			new = init_directory_entry(super, dir_len);
			if (!current)
				current = new;
			insert_directory_entry(new, table, total_dir_len, ndir, crc);
			if (new_tree != NULL)
				new->de_nsubtrees = 1;
			new->next = new_tree;
			new_tree = new;
			super = super_directory(super);
			if (!super)
				dir_len = 0;
			else
				dir_len = strlen(super);
			crc = crc32(0, (Bytef*)super, dir_len);
			found = lookup_hash(crc, table);
		}
		search = found;
		while (search->next_hash && strcmp(super, search->pathname) != 0)
			search = search->next_hash;
		if (search && !current)
			current = search;
		if (!search && !current)
			current = new_tree;
		if (!super && new_tree) {
			new_tree->next = de->next;
			de->next = new_tree;
			de->de_nsubtrees++;
		} else if (new_tree) {
			struct directory_entry *temp;

			search = de->next;
			while (strcmp(super, search->pathname))
				search = search->next;
			temp = new_tree;
			while (temp->next)
				temp = temp->next;
			search->de_nsubtrees++;
			temp->next = search->next;
			search->next = new_tree;
		}

		len = strlen(item->string);
		conflict_entry = create_new_conflict(item->string, len, current->de_pathlen);
		add_conflict_to_directory_entry(current, conflict_entry);
		for (i = 0; i < 3; i++) {
			if (ui->mode[i]) {
				struct conflict_part *cp;

				cp = xmalloc(sizeof(struct conflict_part));
				cp->flags = (i + 1) << CONFLICT_STAGESHIFT;
				cp->entry_mode = ui->mode[i];
				cp->next = NULL;
				hashcpy(cp->sha1, ui->sha1[i]);
				add_part_to_conflict_entry(current, conflict_entry, cp);
			}
		}
	}
}

static struct directory_entry *compile_directory_data(struct index_state *istate,
						int nfile,
						unsigned int *ndir,
						int *non_conflicted,
						int *total_dir_len,
						int *total_file_len)
{
	int i, dir_len = -1;
	char *dir;
	struct directory_entry *de, *current, *search, *found, *new, *previous_entry;
	struct cache_entry **cache = istate->cache;
	struct conflict_entry *conflict_entry;
	struct hash_table table;
	uint32_t crc;

	init_hash(&table);
	de = init_directory_entry("", 0);
	current = de;
	*ndir = 1;
	*total_dir_len = 1;
	crc = crc32(0, (Bytef*)de->pathname, de->de_pathlen);
	insert_hash(crc, de, &table);
	conflict_entry = NULL;
	for (i = 0; i < nfile; i++) {
		int new_entry;
		if (cache[i]->ce_flags & CE_REMOVE)
			continue;

		new_entry = !ce_stage(cache[i]) || !conflict_entry
		    || cache_name_compare(conflict_entry->name, conflict_entry->namelen,
					cache[i]->name, ce_namelen(cache[i]));
		if (new_entry)
			(*non_conflicted)++;
		if (dir_len < 0 || strncmp(cache[i]->name, dir, dir_len)
		    || cache[i]->name[dir_len] != '/'
		    || strchr(cache[i]->name + dir_len + 1, '/')) {
			dir = super_directory(cache[i]->name);
			if (!dir)
				dir_len = 0;
			else
				dir_len = strlen(dir);
			crc = crc32(0, (Bytef*)dir, dir_len);
			found = lookup_hash(crc, &table);
			search = found;
			while (search && dir_len != 0 && strcmp(dir, search->pathname) != 0)
				search = search->next_hash;
		}
		previous_entry = current;
		if (!search || !found) {
			new = init_directory_entry(dir, dir_len);
			current->next = new;
			current = current->next;
			insert_directory_entry(new, &table, total_dir_len, ndir, crc);
			search = current;
		}
		if (new_entry) {
			search->de_nfiles++;
			*total_file_len += ce_namelen(cache[i]) + 1;
			if (search->de_pathlen)
				*total_file_len -= search->de_pathlen + 1;
			ce_queue_push(&(search->ce), &(search->ce_last), cache[i]);
		}
		if (ce_stage(cache[i]) > 0) {
			struct conflict_part *conflict_part;
			if (new_entry) {
				conflict_entry = create_conflict_entry_from_ce(cache[i], search->de_pathlen);
				add_conflict_to_directory_entry(search, conflict_entry);
			}
			conflict_part = conflict_part_from_inmemory(cache[i]);
			add_part_to_conflict_entry(search, conflict_entry, conflict_part);
		}
		if (dir && !found) {
			struct directory_entry *no_subtrees;

			no_subtrees = current;
			dir = super_directory(dir);
			if (dir)
				dir_len = strlen(dir);
			else
				dir_len = 0;
			crc = crc32(0, (Bytef*)dir, dir_len);
			found = lookup_hash(crc, &table);
			while (!found) {
				new = init_directory_entry(dir, dir_len);
				new->de_nsubtrees = 1;
				new->next = no_subtrees;
				no_subtrees = new;
				insert_directory_entry(new, &table, total_dir_len, ndir, crc);
				dir = super_directory(dir);
				if (!dir)
					dir_len = 0;
				else
					dir_len = strlen(dir);
				crc = crc32(0, (Bytef*)dir, dir_len);
				found = lookup_hash(crc, &table);
			}
			search = found;
			while (search->next_hash && strcmp(dir, search->pathname) != 0)
				search = search->next_hash;
			if (search)
				found = search;
			found->de_nsubtrees++;
			previous_entry->next = no_subtrees;
		}
	}
	if (istate->cache_tree)
		cache_tree_to_ondisk_v5(&table, istate->cache_tree);
	resolve_undo_to_ondisk_v5(&table, istate->resolve_undo, ndir, total_dir_len, de);
	return de;
}

static void ondisk_from_cache_entry(struct cache_entry *ce,
				    struct ondisk_cache_entry *ondisk)
{
	unsigned int flags;

	flags  = ce->ce_flags & CE_STAGEMASK;
	flags |= ce->ce_flags & CE_VALID;
	flags |= ce->ce_flags & CE_SMUDGED;
	if (ce->ce_flags & CE_INTENT_TO_ADD)
		flags |= CE_INTENT_TO_ADD_V5;
	if (ce->ce_flags & CE_SKIP_WORKTREE)
		flags |= CE_SKIP_WORKTREE_V5;
	ondisk->flags      = htons(flags);
	ondisk->mode       = htons(ce->ce_mode);
	ondisk->mtime.sec  = htonl(ce->ce_stat_data.sd_mtime.sec);
#ifdef USE_NSEC
	ondisk->mtime.nsec = htonl(ce->ce_stat_data.sd_mtime.nsec);
#else
	ondisk->mtime.nsec = 0;
#endif
	ondisk->size       = htonl(ce->ce_stat_data.sd_size);
	if (!ce->ce_stat_crc)
		ce->ce_stat_crc = calculate_stat_crc(ce);
	ondisk->stat_crc   = htonl(ce->ce_stat_crc);
	hashcpy(ondisk->sha1, ce->sha1);
}

static int write_directories(struct directory_entry *de, int fd, int conflict_offset)
{
	struct directory_entry *current;
	struct ondisk_directory_entry ondisk;
	int current_offset, offset_write, ondisk_size, foffset;
	uint32_t crc;

	/*
	 * This is needed because the compiler aligns structs to sizes multipe
	 * of 4
	 */
	ondisk_size = sizeof(ondisk.flags)
		+ sizeof(ondisk.foffset)
		+ sizeof(ondisk.cr)
		+ sizeof(ondisk.ncr)
		+ sizeof(ondisk.nsubtrees)
		+ sizeof(ondisk.nfiles)
		+ sizeof(ondisk.nentries)
		+ sizeof(ondisk.sha1);
	current = de;
	current_offset = 0;
	foffset = 0;
	while (current) {
		int pathlen;

		offset_write = htonl(current_offset);
		if (ce_write(NULL, fd, &offset_write, 4) < 0)
			return -1;
		if (current->de_pathlen == 0)
			pathlen = 0;
		else
			pathlen = current->de_pathlen + 1;
		current_offset += pathlen + 1 + ondisk_size + 4;
		current = current->next;
	}
	/*
	 * Write one more offset, which points to the end of the entries,
	 * because we use it for calculating the dir length, instead of
	 * using strlen.
	 */
	offset_write = htonl(current_offset);
	if (ce_write(NULL, fd, &offset_write, 4) < 0)
		return -1;
	current = de;
	while (current) {
		crc = 0;
		if (current->de_pathlen == 0) {
			if (ce_write(&crc, fd, current->pathname, 1) < 0)
				return -1;
		} else {
			char *path;
			path = xmalloc(sizeof(char) * (current->de_pathlen + 2));
			memcpy(path, current->pathname, current->de_pathlen);
			memcpy(path + current->de_pathlen, "/\0", 2);
			if (ce_write(&crc, fd, path, current->de_pathlen + 2) < 0)
				return -1;
		}
		current->de_foffset = foffset;
		current->de_cr = conflict_offset;
		ondisk_from_directory_entry(current, &ondisk);
		if (ce_write(&crc, fd, &ondisk, ondisk_size) < 0)
			return -1;
		crc = htonl(crc);
		if (ce_write(NULL, fd, &crc, 4) < 0)
			return -1;
		conflict_offset += current->conflict_size;
		foffset += current->de_nfiles * 4;
		current = current->next;
	}
	return 0;
}

static int write_entries(struct index_state *istate,
			    struct directory_entry *de,
			    int entries,
			    int fd,
			    int offset_to_offset)
{
	int offset, offset_write, ondisk_size;
	struct directory_entry *current;

	offset = 0;
	ondisk_size = sizeof(struct ondisk_cache_entry);
	current = de;
	while (current) {
		int pathlen;
		struct cache_entry *ce = current->ce;

		if (current->de_pathlen == 0)
			pathlen = 0;
		else
			pathlen = current->de_pathlen + 1;
		while (ce) {
			if (ce->ce_flags & CE_REMOVE)
				continue;
			if (!ce_uptodate(ce) && is_racy_timestamp(istate, ce))
				ce_smudge_racily_clean_entry(ce);
			if (is_null_sha1(ce->sha1))
				return error("cache entry has null sha1: %s", ce->name);

			offset_write = htonl(offset);
			if (ce_write(NULL, fd, &offset_write, 4) < 0)
				return -1;
			offset += ce_namelen(ce) - pathlen + 1 + ondisk_size + 4;
			ce = ce->next;
		}
		current = current->next;
	}
	/*
	 * Write one more offset, which points to the end of the entries,
	 * because we use it for calculating the file length, instead of
	 * using strlen.
	 */
	offset_write = htonl(offset);
	if (ce_write(NULL, fd, &offset_write, 4) < 0)
		return -1;

	offset = offset_to_offset;
	current = de;
	while (current) {
		int pathlen;
		struct cache_entry *ce = current->ce;

		if (current->de_pathlen == 0)
			pathlen = 0;
		else
			pathlen = current->de_pathlen + 1;
		while (ce) {
			struct ondisk_cache_entry ondisk;
			uint32_t crc, calc_crc;

			if (ce->ce_flags & CE_REMOVE)
				continue;
			calc_crc = htonl(offset);
			crc = crc32(0, (Bytef*)&calc_crc, 4);
			if (ce_write(&crc, fd, ce->name + pathlen,
					ce_namelen(ce) - pathlen + 1) < 0)
				return -1;
			ondisk_from_cache_entry(ce, &ondisk);
			if (ce_write(&crc, fd, &ondisk, ondisk_size) < 0)
				return -1;
			crc = htonl(crc);
			if (ce_write(NULL, fd, &crc, 4) < 0)
				return -1;
			offset += 4;
			ce = ce->next;
		}
		current = current->next;
	}
	return 0;
}

static int write_conflict(struct conflict_entry *conflict, int fd)
{
	struct conflict_entry *current;
	struct conflict_part *current_part;
	uint32_t crc;

	current = conflict;
	while (current) {
		unsigned int to_write;

		crc = 0;
		if (ce_write(&crc, fd,
		     (Bytef*)(current->name + current->pathlen),
		     current->namelen - current->pathlen) < 0)
			return -1;
		if (ce_write(&crc, fd, (Bytef*)"\0", 1) < 0)
			return -1;
		to_write = htonl(current->nfileconflicts);
		if (ce_write(&crc, fd, (Bytef*)&to_write, 4) < 0)
			return -1;
		current_part = current->entries;
		while (current_part) {
			struct ondisk_conflict_part ondisk;

			conflict_to_ondisk(current_part, &ondisk);
			if (ce_write(&crc, fd, (Bytef*)&ondisk, sizeof(struct ondisk_conflict_part)) < 0)
				return 0;
			current_part = current_part->next;
		}
		to_write = htonl(crc);
		if (ce_write(NULL, fd, (Bytef*)&to_write, 4) < 0)
			return -1;
		current = current->next;
	}
	return 0;
}

static int write_conflicts(struct index_state *istate,
			      struct directory_entry *de,
			      int fd)
{
	struct directory_entry *current;

	current = de;
	while (current) {
		if (current->de_ncr != 0) {
			if (write_conflict(current->conflict, fd) < 0)
				return -1;
		}
		current = current->next;
	}
	return 0;
}

static int write_index_v5(struct index_state *istate, int newfd)
{
	struct cache_version_header hdr;
	struct cache_header hdr_v5;
	struct cache_entry **cache = istate->cache;
	struct directory_entry *de;
	struct ondisk_directory_entry *ondisk;
	int entries = istate->cache_nr;
	int i, removed, non_conflicted, total_dir_len, ondisk_directory_size;
	int total_file_len, conflict_offset, offset_to_offset;
	unsigned int ndir;
	uint32_t crc;

	if (istate->partially_read)
		die("BUG: index: cannot write a partially read index");

	for (i = removed = 0; i < entries; i++) {
		if (cache[i]->ce_flags & CE_REMOVE)
			removed++;
	}
	hdr.hdr_signature = htonl(CACHE_SIGNATURE);
	hdr.hdr_version = htonl(istate->version);
	hdr_v5.hdr_nfile = htonl(entries - removed);
	hdr_v5.hdr_nextension = htonl(0); /* Currently no extensions are supported */

	non_conflicted = 0;
	total_dir_len = 0;
	total_file_len = 0;
	de = compile_directory_data(istate, entries, &ndir, &non_conflicted,
			&total_dir_len, &total_file_len);
	hdr_v5.hdr_ndir = htonl(ndir);

	/*
	 * This is needed because the compiler aligns structs to sizes multipe
	 * of 4
	 */
	ondisk_directory_size = sizeof(ondisk->flags)
		+ sizeof(ondisk->foffset)
		+ sizeof(ondisk->cr)
		+ sizeof(ondisk->ncr)
		+ sizeof(ondisk->nsubtrees)
		+ sizeof(ondisk->nfiles)
		+ sizeof(ondisk->nentries)
		+ sizeof(ondisk->sha1);
	hdr_v5.hdr_fblockoffset = htonl(sizeof(hdr) + sizeof(hdr_v5) + 4
		+ (ndir + 1) * 4
		+ total_dir_len
		+ ndir * (ondisk_directory_size + 4)
		+ (non_conflicted + 1) * 4);

	crc = 0;
	if (ce_write(&crc, newfd, &hdr, sizeof(hdr)) < 0)
		return -1;
	if (ce_write(&crc, newfd, &hdr_v5, sizeof(hdr_v5)) < 0)
		return -1;
	crc = htonl(crc);
	if (ce_write(NULL, newfd, &crc, 4) < 0)
		return -1;

	conflict_offset = sizeof(hdr) + sizeof(hdr_v5) + 4
		+ (ndir + 1) * 4
		+ total_dir_len
		+ ndir * (ondisk_directory_size + 4)
		+ (non_conflicted + 1) * 4
		+ total_file_len
		+ non_conflicted * (sizeof(struct ondisk_cache_entry) + 4);
	if (write_directories(de, newfd, conflict_offset) < 0)
		return -1;
	offset_to_offset = sizeof(hdr) + sizeof(hdr_v5) + 4
		+ (ndir + 1) * 4
		+ total_dir_len
		+ ndir * (ondisk_directory_size + 4);
	if (write_entries(istate, de, entries, newfd, offset_to_offset) < 0)
		return -1;
	if (write_conflicts(istate, de, newfd) < 0)
		return -1;
	return ce_flush(newfd);
}

struct index_ops v5_ops = {
	match_stat_basic,
	verify_hdr,
	read_index_v5,
	write_index_v5,
	index_change_filter_opts_v5
};
debug log:

solving 412db53 ...
found 412db53 in https://public-inbox.org/git/1373184720-29767-21-git-send-email-t.gummerer@gmail.com/
found 306de30 in https://public-inbox.org/git/1373184720-29767-20-git-send-email-t.gummerer@gmail.com/
found f056f6b in https://public-inbox.org/git/1373184720-29767-19-git-send-email-t.gummerer@gmail.com/
found f1ad132 in https://public-inbox.org/git/1373184720-29767-18-git-send-email-t.gummerer@gmail.com/
found 193970a in https://public-inbox.org/git/1373184720-29767-17-git-send-email-t.gummerer@gmail.com/
found e319f30 in https://public-inbox.org/git/1373184720-29767-16-git-send-email-t.gummerer@gmail.com/

applying [1/6] https://public-inbox.org/git/1373184720-29767-16-git-send-email-t.gummerer@gmail.com/
diff --git a/read-cache-v5.c b/read-cache-v5.c
new file mode 100644
index 0000000..e319f30


applying [2/6] https://public-inbox.org/git/1373184720-29767-17-git-send-email-t.gummerer@gmail.com/
diff --git a/read-cache-v5.c b/read-cache-v5.c
index e319f30..193970a 100644


applying [3/6] https://public-inbox.org/git/1373184720-29767-18-git-send-email-t.gummerer@gmail.com/
diff --git a/read-cache-v5.c b/read-cache-v5.c
index 193970a..f1ad132 100644


applying [4/6] https://public-inbox.org/git/1373184720-29767-19-git-send-email-t.gummerer@gmail.com/
diff --git a/read-cache-v5.c b/read-cache-v5.c
index f1ad132..f056f6b 100644


applying [5/6] https://public-inbox.org/git/1373184720-29767-20-git-send-email-t.gummerer@gmail.com/
diff --git a/read-cache-v5.c b/read-cache-v5.c
index f056f6b..306de30 100644


applying [6/6] https://public-inbox.org/git/1373184720-29767-21-git-send-email-t.gummerer@gmail.com/
diff --git a/read-cache-v5.c b/read-cache-v5.c
index 306de30..412db53 100644

Checking patch read-cache-v5.c...
Applied patch read-cache-v5.c cleanly.
Checking patch read-cache-v5.c...
Applied patch read-cache-v5.c cleanly.
Checking patch read-cache-v5.c...
Applied patch read-cache-v5.c cleanly.
Checking patch read-cache-v5.c...
Applied patch read-cache-v5.c cleanly.
Checking patch read-cache-v5.c...
Applied patch read-cache-v5.c cleanly.
Checking patch read-cache-v5.c...
Applied patch read-cache-v5.c cleanly.

index at:
100644 412db53d28defb706278f116520dcb93acf07670	read-cache-v5.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).