index.c 23.1 KB
Newer Older
1
/*
schu committed
2
 * Copyright (C) 2009-2012 the libgit2 contributors
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7 8 9 10
 */

#include <stddef.h>

#include "common.h"
11
#include "repository.h"
12
#include "index.h"
13
#include "tree.h"
14
#include "tree-cache.h"
15
#include "hash.h"
16 17
#include "git2/odb.h"
#include "git2/blob.h"
18 19 20 21 22 23 24 25 26 27 28

#define entry_size(type,len) ((offsetof(type, path) + (len) + 8) & ~7)
#define short_entry_size(len) entry_size(struct entry_short, len)
#define long_entry_size(len) entry_size(struct entry_long, len)

#define minimal_entry_size (offsetof(struct entry_short, path))

static const size_t INDEX_FOOTER_SIZE = GIT_OID_RAWSZ;
static const size_t INDEX_HEADER_SIZE = 12;

static const unsigned int INDEX_VERSION_NUMBER = 2;
29 30 31 32
static const unsigned int INDEX_VERSION_NUMBER_EXT = 3;

static const unsigned int INDEX_HEADER_SIG = 0x44495243;
static const char INDEX_EXT_TREECACHE_SIG[] = {'T', 'R', 'E', 'E'};
33
static const char INDEX_EXT_UNMERGED_SIG[] = {'R', 'E', 'U', 'C'};
34

35 36
#define INDEX_OWNER(idx) ((git_repository *)(GIT_REFCOUNT_OWNER(idx)))

37 38 39 40 41 42 43 44 45 46 47
struct index_header {
	uint32_t signature;
	uint32_t version;
	uint32_t entry_count;
};

struct index_extension {
	char signature[4];
	uint32_t extension_size;
};

48 49 50 51 52
struct entry_time {
	uint32_t seconds;
	uint32_t nanoseconds;
};

53
struct entry_short {
54 55
	struct entry_time ctime;
	struct entry_time mtime;
56 57 58 59 60 61 62 63
	uint32_t dev;
	uint32_t ino;
	uint32_t mode;
	uint32_t uid;
	uint32_t gid;
	uint32_t file_size;
	git_oid oid;
	uint16_t flags;
schu committed
64
	char path[1]; /* arbitrary length */
65 66 67
};

struct entry_long {
68 69
	struct entry_time ctime;
	struct entry_time mtime;
70 71 72 73 74 75 76 77 78
	uint32_t dev;
	uint32_t ino;
	uint32_t mode;
	uint32_t uid;
	uint32_t gid;
	uint32_t file_size;
	git_oid oid;
	uint16_t flags;
	uint16_t flags_extended;
schu committed
79
	char path[1]; /* arbitrary length */
80 81 82 83 84 85 86
};

/* local declarations */
static size_t read_extension(git_index *index, const char *buffer, size_t buffer_size);
static size_t read_entry(git_index_entry *dest, const void *buffer, size_t buffer_size);
static int read_header(struct index_header *dest, const void *buffer);

87
static int parse_index(git_index *index, const char *buffer, size_t buffer_size);
Vicent Marti committed
88
static int is_index_extended(git_index *index);
89
static int write_index(git_index *index, git_filebuf *file);
90

91 92
static void index_entry_free(git_index_entry *entry);

93
static int index_srch(const void *key, const void *array_member)
94
{
95
	const git_index_entry *entry = array_member;
96

97
	return strcmp(key, entry->path);
98 99
}

100
static int index_cmp(const void *a, const void *b)
101
{
102 103
	const git_index_entry *entry_a = a;
	const git_index_entry *entry_b = b;
104 105 106 107

	return strcmp(entry_a->path, entry_b->path);
}

108
static int unmerged_srch(const void *key, const void *array_member)
109
{
110
	const git_index_entry_unmerged *entry = array_member;
111

112
	return strcmp(key, entry->path);
113 114
}

115
static int unmerged_cmp(const void *a, const void *b)
116
{
117 118
	const git_index_entry_unmerged *info_a = a;
	const git_index_entry_unmerged *info_b = b;
119 120 121

	return strcmp(info_a->path, info_b->path);
}
122

123
static unsigned int index_create_mode(unsigned int mode)
124 125 126
{
	if (S_ISLNK(mode))
		return S_IFLNK;
127 128
	if (S_ISDIR(mode) || (mode & S_IFMT) == (S_IFLNK | S_IFDIR))
		return (S_IFLNK | S_IFDIR);
129 130 131
	return S_IFREG | ((mode & 0100) ? 0755 : 0644);
}

132
int git_index_open(git_index **index_out, const char *index_path)
133 134 135
{
	git_index *index;

Vicent Marti committed
136
	assert(index_out && index_path);
137

138 139
	index = git__calloc(1, sizeof(git_index));
	GITERR_CHECK_ALLOC(index);
140 141

	index->index_file_path = git__strdup(index_path);
142
	GITERR_CHECK_ALLOC(index->index_file_path);
143

144 145
	if (git_vector_init(&index->entries, 32, index_cmp) < 0)
		return -1;
146

147
	/* Check if index file is stored on disk already */
148
	if (git_path_exists(index->index_file_path) == true)
149 150
		index->on_disk = 1;

Vicent Marti committed
151
	*index_out = index;
152
	GIT_REFCOUNT_INC(index);
153
	return git_index_read(index);
154 155
}

156
static void index_free(git_index *index)
157
{
158 159 160
	git_index_entry *e;
	unsigned int i;

161
	git_index_clear(index);
162 163 164
	git_vector_foreach(&index->entries, i, e) {
		index_entry_free(e);
	}
165
	git_vector_free(&index->entries);
166 167 168
	git_vector_foreach(&index->unmerged, i, e) {
		index_entry_free(e);
	}
169
	git_vector_free(&index->unmerged);
170

171 172
	git__free(index->index_file_path);
	git__free(index);
173 174
}

175 176
void git_index_free(git_index *index)
{
177
	if (index == NULL)
178 179
		return;

180
	GIT_REFCOUNT_DEC(index, index_free);
181 182
}

183 184 185
void git_index_clear(git_index *index)
{
	unsigned int i;
186 187 188

	assert(index);

189 190 191
	for (i = 0; i < index->entries.length; ++i) {
		git_index_entry *e;
		e = git_vector_get(&index->entries, i);
192 193
		git__free(e->path);
		git__free(e);
194
	}
195

196 197 198
	for (i = 0; i < index->unmerged.length; ++i) {
		git_index_entry_unmerged *e;
		e = git_vector_get(&index->unmerged, i);
199 200
		git__free(e->path);
		git__free(e);
201 202
	}

203
	git_vector_clear(&index->entries);
204
	git_vector_clear(&index->unmerged);
205 206
	index->last_modified = 0;

207
	git_tree_cache_free(index->tree);
208 209 210 211 212
	index->tree = NULL;
}

int git_index_read(git_index *index)
{
213
	int error, updated;
214
	git_buf buffer = GIT_BUF_INIT;
215
	time_t mtime;
216

Vicent Marti committed
217
	assert(index->index_file_path);
218

219
	if (!index->on_disk || git_path_exists(index->index_file_path) == false) {
220 221
		git_index_clear(index);
		index->on_disk = 0;
222
		return 0;
223 224
	}

225 226
	/* We don't want to update the mtime if we fail to parse the index */
	mtime = index->last_modified;
227 228 229 230
	error = git_futils_readbuffer_updated(
		&buffer, index->index_file_path, &mtime, &updated);
	if (error < 0)
		return error;
231

232
	if (updated) {
233
		git_index_clear(index);
234
		error = parse_index(index, buffer.ptr, buffer.size);
235

236
		if (!error)
237
			index->last_modified = mtime;
238

239
		git_buf_free(&buffer);
240 241 242 243 244 245 246
	}

	return error;
}

int git_index_write(git_index *index)
{
247
	git_filebuf file = GIT_FILEBUF_INIT;
248
	struct stat indexst;
249
	int error;
250

251
	git_vector_sort(&index->entries);
252

253 254 255
	if ((error = git_filebuf_open(
			 &file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS)) < 0)
		return error;
256

257
	if ((error = write_index(index, &file)) < 0) {
258
		git_filebuf_cleanup(&file);
259
		return error;
260 261
	}

262 263
	if ((error = git_filebuf_commit(&file, GIT_INDEX_FILE_MODE)) < 0)
		return error;
264

Vicent Marti committed
265
	if (p_stat(index->index_file_path, &indexst) == 0) {
266 267 268 269
		index->last_modified = indexst.st_mtime;
		index->on_disk = 1;
	}

270
	return 0;
271 272
}

273 274 275
unsigned int git_index_entrycount(git_index *index)
{
	assert(index);
276
	return index->entries.length;
277 278
}

279
unsigned int git_index_entrycount_unmerged(git_index *index)
280 281 282 283 284
{
	assert(index);
	return index->unmerged.length;
}

285
git_index_entry *git_index_get(git_index *index, unsigned int n)
286
{
287
	git_vector_sort(&index->entries);
288
	return git_vector_get(&index->entries, n);
289 290
}

291 292 293 294 295 296 297 298 299 300 301 302 303 304
void git_index__init_entry_from_stat(struct stat *st, git_index_entry *entry)
{
	entry->ctime.seconds = (git_time_t)st->st_ctime;
	entry->mtime.seconds = (git_time_t)st->st_mtime;
	/* entry->mtime.nanoseconds = st->st_mtimensec; */
	/* entry->ctime.nanoseconds = st->st_ctimensec; */
	entry->dev  = st->st_rdev;
	entry->ino  = st->st_ino;
	entry->mode = index_create_mode(st->st_mode);
	entry->uid  = st->st_uid;
	entry->gid  = st->st_gid;
	entry->file_size = st->st_size;
}

305 306
static int index_entry_init(git_index_entry **entry_out, git_index *index, const char *rel_path, int stage)
{
307
	git_index_entry *entry = NULL;
308 309
	struct stat st;
	git_oid oid;
310 311
	const char *workdir;
	git_buf full_path = GIT_BUF_INIT;
312
	int error;
313

314
	assert(stage >= 0 && stage <= 3);
315

316 317 318 319 320 321 322
	if (INDEX_OWNER(index) == NULL ||
		(workdir = git_repository_workdir(INDEX_OWNER(index))) == NULL)
	{
		giterr_set(GITERR_INDEX,
			"Could not initialize index entry. Repository is bare");
		return -1;
	}
323

324
	if ((error = git_buf_joinpath(&full_path, workdir, rel_path)) < 0)
325 326
		return error;

327
	if ((error = git_path_lstat(full_path.ptr, &st)) < 0) {
328 329 330 331 332 333
		git_buf_free(&full_path);
		return error;
	}

	git_buf_free(&full_path); /* done with full path */

334 335 336 337
	/* There is no need to validate the rel_path here, since it will be
	 * immediately validated by the call to git_blob_create_fromfile.
	 */

338
	/* write the blob to disk and get the oid */
339 340
	if ((error = git_blob_create_fromfile(&oid, INDEX_OWNER(index), rel_path)) < 0)
		return error;
341

342
	entry = git__calloc(1, sizeof(git_index_entry));
343
	GITERR_CHECK_ALLOC(entry);
344

345 346 347
	git_index__init_entry_from_stat(&st, entry);

	entry->oid = oid;
348 349
	entry->flags |= (stage << GIT_IDXENTRY_STAGESHIFT);
	entry->path = git__strdup(rel_path);
350
	GITERR_CHECK_ALLOC(entry->path);
351 352

	*entry_out = entry;
353
	return 0;
354 355
}

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
static git_index_entry *index_entry_dup(const git_index_entry *source_entry)
{
	git_index_entry *entry;

	entry = git__malloc(sizeof(git_index_entry));
	if (!entry)
		return NULL;

	memcpy(entry, source_entry, sizeof(git_index_entry));

	/* duplicate the path string so we own it */
	entry->path = git__strdup(entry->path);
	if (!entry->path)
		return NULL;

	return entry;
}

374 375 376 377
static void index_entry_free(git_index_entry *entry)
{
	if (!entry)
		return;
378 379
	git__free(entry->path);
	git__free(entry);
380 381
}

382
static int index_insert(git_index *index, git_index_entry *entry, int replace)
383
{
384 385
	size_t path_length;
	int position;
386
	git_index_entry **entry_array;
387

388
	assert(index && entry && entry->path != NULL);
389

390
	/* make sure that the path length flag is correct */
391
	path_length = strlen(entry->path);
392

393
	entry->flags &= ~GIT_IDXENTRY_NAMEMASK;
394 395

	if (path_length < GIT_IDXENTRY_NAMEMASK)
396
		entry->flags |= path_length & GIT_IDXENTRY_NAMEMASK;
397
	else
398
		entry->flags |= GIT_IDXENTRY_NAMEMASK;;
399

400 401 402 403
	/*
	 * replacing is not requested: just insert entry at the end;
	 * the index is no longer sorted
	 */
404 405
	if (!replace)
		return git_vector_insert(&index->entries, entry);
406

407
	/* look if an entry with this path already exists */
408
	position = git_index_find(index, entry->path);
409

410 411 412 413
	/*
	 * if no entry exists add the entry at the end;
	 * the index is no longer sorted
	 */
414 415
	if (position == GIT_ENOTFOUND)
		return git_vector_insert(&index->entries, entry);
416 417 418

	/* exists, replace it */
	entry_array = (git_index_entry **) index->entries.contents;
419 420
	git__free(entry_array[position]->path);
	git__free(entry_array[position]);
421
	entry_array[position] = entry;
422

423
	return 0;
424 425
}

426
static int index_add(git_index *index, const char *path, int stage, int replace)
427
{
428 429
	git_index_entry *entry = NULL;
	int ret;
430

431 432 433 434 435 436
	if ((ret = index_entry_init(&entry, index, path, stage)) < 0 ||
		(ret = index_insert(index, entry, replace)) < 0)
	{
		index_entry_free(entry);
		return ret;
	}
437

438
	git_tree_cache_invalidate_path(index->tree, entry->path);
439
	return 0;
440 441 442 443
}

int git_index_add(git_index *index, const char *path, int stage)
{
444
	return index_add(index, path, stage, 1);
445 446 447 448
}

int git_index_append(git_index *index, const char *path, int stage)
{
449
	return index_add(index, path, stage, 0);
450 451
}

452 453
static int index_add2(
	git_index *index, const git_index_entry *source_entry, int replace)
454 455 456 457 458
{
	git_index_entry *entry = NULL;
	int ret;

	entry = index_entry_dup(source_entry);
459 460
	if (entry == NULL)
		return -1;
461

462 463 464 465
	if ((ret = index_insert(index, entry, replace)) < 0) {
		index_entry_free(entry);
		return ret;
	}
466

467
	git_tree_cache_invalidate_path(index->tree, entry->path);
468
	return 0;
469 470 471 472
}

int git_index_add2(git_index *index, const git_index_entry *source_entry)
{
473
	return index_add2(index, source_entry, 1);
474 475
}

476
int git_index_append2(git_index *index, const git_index_entry *source_entry)
477
{
478
	return index_add2(index, source_entry, 1);
479 480
}

481
int git_index_remove(git_index *index, int position)
482
{
483
	int error;
484 485
	git_index_entry *entry;

486
	git_vector_sort(&index->entries);
487

488 489 490 491
	entry = git_vector_get(&index->entries, position);
	if (entry != NULL)
		git_tree_cache_invalidate_path(index->tree, entry->path);

492 493
	error = git_vector_remove(&index->entries, (unsigned int)position);

494
	if (!error)
495 496 497
		index_entry_free(entry);

	return error;
498
}
499

500 501
int git_index_find(git_index *index, const char *path)
{
502
	return git_vector_bsearch2(&index->entries, index_srch, path);
503 504
}

505 506 507 508 509
void git_index_uniq(git_index *index)
{
	git_vector_uniq(&index->entries);
}

510 511
const git_index_entry_unmerged *git_index_get_unmerged_bypath(
	git_index *index, const char *path)
512 513
{
	int pos;
514
	assert(index && path);
515

516 517
	if (!index->unmerged.length)
		return NULL;
518

519
	if ((pos = git_vector_bsearch2(&index->unmerged, unmerged_srch, path)) < 0)
520
		return NULL;
521

522
	return git_vector_get(&index->unmerged, pos);
523 524
}

525 526
const git_index_entry_unmerged *git_index_get_unmerged_byindex(
	git_index *index, unsigned int n)
527 528 529 530 531
{
	assert(index);
	return git_vector_get(&index->unmerged, n);
}

532 533 534 535 536 537
static int index_error_invalid(const char *message)
{
	giterr_set(GITERR_INDEX, "Invalid data in index - %s", message);
	return -1;
}

538
static int read_unmerged(git_index *index, const char *buffer, size_t size)
539
{
540 541
	const char *endptr;
	size_t len;
542 543
	int i;

544 545
	if (git_vector_init(&index->unmerged, 16, unmerged_cmp) < 0)
		return -1;
546 547 548 549 550 551

	while (size) {
		git_index_entry_unmerged *lost;

		len = strlen(buffer) + 1;
		if (size <= len)
552
			return index_error_invalid("reading unmerged entries");
553

554 555
		lost = git__malloc(sizeof(git_index_entry_unmerged));
		GITERR_CHECK_ALLOC(lost);
556

557 558
		if (git_vector_insert(&index->unmerged, lost) < 0)
			return -1;
559

560
		/* read NUL-terminated pathname for entry */
561
		lost->path = git__strdup(buffer);
562
		GITERR_CHECK_ALLOC(lost->path);
563

564 565 566
		size -= len;
		buffer += len;

567
		/* read 3 ASCII octal numbers for stage entries */
568
		for (i = 0; i < 3; i++) {
569
			int tmp;
570

571 572 573 574
			if (git__strtol32(&tmp, buffer, &endptr, 8) < 0 ||
				!endptr || endptr == buffer || *endptr ||
				(unsigned)tmp > UINT_MAX)
				return index_error_invalid("reading unmerged entry stage");
575

576 577
			lost->mode[i] = tmp;

578
			len = (endptr + 1) - buffer;
579
			if (size <= len)
580
				return index_error_invalid("reading unmerged entry stage");
581

582 583 584 585
			size -= len;
			buffer += len;
		}

586
		/* read up to 3 OIDs for stage entries */
587 588 589 590
		for (i = 0; i < 3; i++) {
			if (!lost->mode[i])
				continue;
			if (size < 20)
591 592
				return index_error_invalid("reading unmerged entry oid");

593
			git_oid_fromraw(&lost->oid[i], (const unsigned char *) buffer);
594 595 596 597 598
			size -= 20;
			buffer += 20;
		}
	}

599
	return 0;
600 601
}

602 603 604 605 606
static size_t read_entry(git_index_entry *dest, const void *buffer, size_t buffer_size)
{
	size_t path_length, entry_size;
	uint16_t flags_raw;
	const char *path_ptr;
607
	const struct entry_short *source = buffer;
608 609 610 611

	if (INDEX_FOOTER_SIZE + minimal_entry_size > buffer_size)
		return 0;

612 613
	memset(dest, 0x0, sizeof(git_index_entry));

614 615 616 617
	dest->ctime.seconds = (git_time_t)ntohl(source->ctime.seconds);
	dest->ctime.nanoseconds = ntohl(source->ctime.nanoseconds);
	dest->mtime.seconds = (git_time_t)ntohl(source->mtime.seconds);
	dest->mtime.nanoseconds = ntohl(source->mtime.nanoseconds);
618 619 620 621 622 623 624 625 626 627
	dest->dev = ntohl(source->dev);
	dest->ino = ntohl(source->ino);
	dest->mode = ntohl(source->mode);
	dest->uid = ntohl(source->uid);
	dest->gid = ntohl(source->gid);
	dest->file_size = ntohl(source->file_size);
	git_oid_cpy(&dest->oid, &source->oid);
	dest->flags = ntohs(source->flags);

	if (dest->flags & GIT_IDXENTRY_EXTENDED) {
628
		const struct entry_long *source_l = (const struct entry_long *)source;
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
		path_ptr = source_l->path;

		flags_raw = ntohs(source_l->flags_extended);
		memcpy(&dest->flags_extended, &flags_raw, 2);
	} else
		path_ptr = source->path;

	path_length = dest->flags & GIT_IDXENTRY_NAMEMASK;

	/* if this is a very long string, we must find its
	 * real length without overflowing */
	if (path_length == 0xFFF) {
		const char *path_end;

		path_end = memchr(path_ptr, '\0', buffer_size);
		if (path_end == NULL)
645
			return 0;
646 647 648 649 650 651 652 653 654 655 656 657 658

		path_length = path_end - path_ptr;
	}

	if (dest->flags & GIT_IDXENTRY_EXTENDED)
		entry_size = long_entry_size(path_length);
	else
		entry_size = short_entry_size(path_length);

	if (INDEX_FOOTER_SIZE + entry_size > buffer_size)
		return 0;

	dest->path = git__strdup(path_ptr);
659
	assert(dest->path);
660 661 662 663 664 665

	return entry_size;
}

static int read_header(struct index_header *dest, const void *buffer)
{
666
	const struct index_header *source = buffer;
667

668 669
	dest->signature = ntohl(source->signature);
	if (dest->signature != INDEX_HEADER_SIG)
670
		return index_error_invalid("incorrect header signature");
671 672

	dest->version = ntohl(source->version);
673 674
	if (dest->version != INDEX_VERSION_NUMBER_EXT &&
		dest->version != INDEX_VERSION_NUMBER)
675
		return index_error_invalid("incorrect header version");
676 677

	dest->entry_count = ntohl(source->entry_count);
678
	return 0;
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
}

static size_t read_extension(git_index *index, const char *buffer, size_t buffer_size)
{
	const struct index_extension *source;
	struct index_extension dest;
	size_t total_size;

	source = (const struct index_extension *)(buffer);

	memcpy(dest.signature, source->signature, 4);
	dest.extension_size = ntohl(source->extension_size);

	total_size = dest.extension_size + sizeof(struct index_extension);

	if (buffer_size - total_size < INDEX_FOOTER_SIZE)
		return 0;

	/* optional extension */
	if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') {
		/* tree cache */
		if (memcmp(dest.signature, INDEX_EXT_TREECACHE_SIG, 4) == 0) {
701
			if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size) < 0)
702
				return 0;
703
		} else if (memcmp(dest.signature, INDEX_EXT_UNMERGED_SIG, 4) == 0) {
704
			if (read_unmerged(index, buffer + 8, dest.extension_size) < 0)
705
				return 0;
706
		}
707 708
		/* else, unsupported extension. We cannot parse this, but we can skip
		 * it by returning `total_size */
709 710 711 712 713 714 715 716 717
	} else {
		/* we cannot handle non-ignorable extensions;
		 * in fact they aren't even defined in the standard */
		return 0;
	}

	return total_size;
}

718
static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
719 720 721 722 723 724 725
{
	unsigned int i;
	struct index_header header;
	git_oid checksum_calculated, checksum_expected;

#define seek_forward(_increase) { \
	if (_increase >= buffer_size) \
726
		return index_error_invalid("ran out of data while parsing"); \
727 728 729 730 731
	buffer += _increase; \
	buffer_size -= _increase;\
}

	if (buffer_size < INDEX_HEADER_SIZE + INDEX_FOOTER_SIZE)
732
		return index_error_invalid("insufficient buffer space");
733 734 735

	/* Precalculate the SHA1 of the files's contents -- we'll match it to
	 * the provided SHA1 in the footer */
736
	git_hash_buf(&checksum_calculated, buffer, buffer_size - INDEX_FOOTER_SIZE);
737 738

	/* Parse header */
739 740
	if (read_header(&header, buffer) < 0)
		return -1;
741 742 743

	seek_forward(INDEX_HEADER_SIZE);

744
	git_vector_clear(&index->entries);
745 746

	/* Parse all the entries */
747
	for (i = 0; i < header.entry_count && buffer_size > INDEX_FOOTER_SIZE; ++i) {
748
		size_t entry_size;
749 750 751
		git_index_entry *entry;

		entry = git__malloc(sizeof(git_index_entry));
752
		GITERR_CHECK_ALLOC(entry);
753 754

		entry_size = read_entry(entry, buffer, buffer_size);
755 756 757

		/* 0 bytes read means an object corruption */
		if (entry_size == 0)
758
			return index_error_invalid("invalid entry");
759

760 761
		if (git_vector_insert(&index->entries, entry) < 0)
			return -1;
762

763 764 765
		seek_forward(entry_size);
	}

766
	if (i != header.entry_count)
767
		return index_error_invalid("header entries changed while parsing");
768

769 770 771 772 773 774 775 776
	/* There's still space for some extensions! */
	while (buffer_size > INDEX_FOOTER_SIZE) {
		size_t extension_size;

		extension_size = read_extension(index, buffer, buffer_size);

		/* see if we have read any bytes from the extension */
		if (extension_size == 0)
777
			return index_error_invalid("extension size is zero");
778 779 780 781 782

		seek_forward(extension_size);
	}

	if (buffer_size != INDEX_FOOTER_SIZE)
783
		return index_error_invalid("buffer size does not match index footer size");
784 785

	/* 160-bit SHA-1 over the content of the index file before this checksum. */
Vicent Marti committed
786
	git_oid_fromraw(&checksum_expected, (const unsigned char *)buffer);
787 788

	if (git_oid_cmp(&checksum_calculated, &checksum_expected) != 0)
789
		return index_error_invalid("calculated checksum does not match expected");
790 791 792

#undef seek_forward

793 794 795
	/* force sorting in the vector: the entries are
	 * assured to be sorted on the index */
	index->entries.sorted = 1;
796
	return 0;
797 798
}

Vicent Marti committed
799 800 801
static int is_index_extended(git_index *index)
{
	unsigned int i, extended;
802
	git_index_entry *entry;
Vicent Marti committed
803 804 805

	extended = 0;

806
	git_vector_foreach(&index->entries, i, entry) {
Vicent Marti committed
807 808 809 810 811 812
		entry->flags &= ~GIT_IDXENTRY_EXTENDED;
		if (entry->flags_extended & GIT_IDXENTRY_EXTENDED_FLAGS) {
			extended++;
			entry->flags |= GIT_IDXENTRY_EXTENDED;
		}
	}
813

Vicent Marti committed
814 815 816
	return extended;
}

817
static int write_disk_entry(git_filebuf *file, git_index_entry *entry)
818
{
819
	void *mem = NULL;
820
	struct entry_short *ondisk;
821
	size_t path_len, disk_size;
822
	char *path;
823

824
	path_len = strlen(entry->path);
825

826
	if (entry->flags & GIT_IDXENTRY_EXTENDED)
827
		disk_size = long_entry_size(path_len);
828
	else
829
		disk_size = short_entry_size(path_len);
830

831 832
	if (git_filebuf_reserve(file, &mem, disk_size) < 0)
		return -1;
833

834 835
	ondisk = (struct entry_short *)mem;

836
	memset(ondisk, 0x0, disk_size);
837

838 839 840 841 842 843 844 845 846 847 848 849
	/**
	 * Yes, we have to truncate.
	 *
	 * The on-disk format for Index entries clearly defines
	 * the time and size fields to be 4 bytes each -- so even if
	 * we store these values with 8 bytes on-memory, they must
	 * be truncated to 4 bytes before writing to disk.
	 *
	 * In 2038 I will be either too dead or too rich to care about this
	 */
	ondisk->ctime.seconds = htonl((uint32_t)entry->ctime.seconds);
	ondisk->mtime.seconds = htonl((uint32_t)entry->mtime.seconds);
850 851
	ondisk->ctime.nanoseconds = htonl(entry->ctime.nanoseconds);
	ondisk->mtime.nanoseconds = htonl(entry->mtime.nanoseconds);
Vicent Marti committed
852 853
	ondisk->dev = htonl(entry->dev);
	ondisk->ino = htonl(entry->ino);
854
	ondisk->mode = htonl(entry->mode);
Vicent Marti committed
855 856
	ondisk->uid = htonl(entry->uid);
	ondisk->gid = htonl(entry->gid);
857
	ondisk->file_size = htonl((uint32_t)entry->file_size);
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872

	git_oid_cpy(&ondisk->oid, &entry->oid);

	ondisk->flags = htons(entry->flags);

	if (entry->flags & GIT_IDXENTRY_EXTENDED) {
		struct entry_long *ondisk_ext;
		ondisk_ext = (struct entry_long *)ondisk;
		ondisk_ext->flags_extended = htons(entry->flags_extended);
		path = ondisk_ext->path;
	}
	else
		path = ondisk->path;

	memcpy(path, entry->path, path_len);
873

874
	return 0;
875 876
}

877
static int write_entries(git_index *index, git_filebuf *file)
878 879
{
	unsigned int i;
880

881
	for (i = 0; i < index->entries.length; ++i) {
882
		git_index_entry *entry;
883
		entry = git_vector_get(&index->entries, i);
884 885
		if (write_disk_entry(file, entry) < 0)
			return -1;
886 887
	}

888
	return 0;
889 890
}

891
static int write_index(git_index *index, git_filebuf *file)
892 893 894 895 896
{
	git_oid hash_final;

	struct index_header header;

Vicent Marti committed
897
	int is_extended;
898

899
	assert(index && file);
900

Vicent Marti committed
901 902
	is_extended = is_index_extended(index);

903
	header.signature = htonl(INDEX_HEADER_SIG);
Vicent Marti committed
904
	header.version = htonl(is_extended ? INDEX_VERSION_NUMBER_EXT : INDEX_VERSION_NUMBER);
905 906
	header.entry_count = htonl(index->entries.length);

907 908
	if (git_filebuf_write(file, &header, sizeof(struct index_header)) < 0)
		return -1;
909

910 911
	if (write_entries(index, file) < 0)
		return -1;
912 913 914

	/* TODO: write extensions (tree cache) */

915 916 917 918
	/* get out the hash for all the contents we've appended to the file */
	git_filebuf_hash(&hash_final, file);

	/* write it at the end of the file */
919
	return git_filebuf_write(file, hash_final.id, GIT_OID_RAWSZ);
920
}
921 922 923 924 925

int git_index_entry_stage(const git_index_entry *entry)
{
	return (entry->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT;
}
926 927 928 929 930 931 932 933

static int read_tree_cb(const char *root, git_tree_entry *tentry, void *data)
{
	git_index *index = data;
	git_index_entry *entry = NULL;
	git_buf path = GIT_BUF_INIT;

	if (entry_is_tree(tentry))
934
		return 0;
935

936 937
	if (git_buf_joinpath(&path, root, tentry->filename) < 0)
		return -1;
938 939

	entry = git__calloc(1, sizeof(git_index_entry));
940
	GITERR_CHECK_ALLOC(entry);
941 942 943 944 945 946

	entry->mode = tentry->attr;
	entry->oid = tentry->oid;
	entry->path = git_buf_detach(&path);
	git_buf_free(&path);

947
	if (index_insert(index, entry, 0) < 0) {
948
		index_entry_free(entry);
949 950 951 952
		return -1;
	}

	return 0;
953 954 955 956 957 958 959 960
}

int git_index_read_tree(git_index *index, git_tree *tree)
{
	git_index_clear(index);

	return git_tree_walk(tree, read_tree_cb, GIT_TREEWALK_POST, index);
}