git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / Atom feed
f1ad1322277c61b31d0d45e9e705ee1ff1f4b3f9 blob 22134 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
 
#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);
}

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

solving f1ad132 ...
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/3] 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/3] 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/3] 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

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 f1ad1322277c61b31d0d45e9e705ee1ff1f4b3f9	read-cache-v5.c

git@vger.kernel.org list mirror (unofficial, one of many)

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 git git/ https://public-inbox.org/git \
		git@vger.kernel.org
	public-inbox-index git

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://ou63pmih66umazou.onion/inbox.comp.version-control.git
	nntp://czquwvybam4bgbro.onion/inbox.comp.version-control.git
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.version-control.git
	nntp://news.gmane.io/gmane.comp.version-control.git
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for the project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git