index.c 26.1 KB
Newer Older
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
/*
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2,
 * as published by the Free Software Foundation.
 *
 * In addition to the permissions in the GNU General Public License,
 * the authors give you unlimited permission to link the compiled
 * version of this file into combinations with other programs,
 * and to distribute those combinations without any restriction
 * coming from the use of this file.  (The General Public License
 * restrictions do apply in other respects; for example, they cover
 * modification of the file, and distribution when not linked into
 * a combined executable.)
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <stddef.h>

#include "common.h"
29
#include "repository.h"
30 31
#include "index.h"
#include "hash.h"
32 33
#include "git2/odb.h"
#include "git2/blob.h"
34 35 36 37 38 39 40 41 42 43 44

#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;
45 46 47 48
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'};
49
static const char INDEX_EXT_UNMERGED_SIG[] = {'R', 'E', 'U', 'C'};
50 51 52 53 54 55 56 57 58 59 60 61

struct index_header {
	uint32_t signature;
	uint32_t version;
	uint32_t entry_count;
};

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

62 63 64 65 66
struct entry_time {
	uint32_t seconds;
	uint32_t nanoseconds;
};

67
struct entry_short {
68 69
	struct entry_time ctime;
	struct entry_time mtime;
70 71 72 73 74 75 76 77
	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
78
	char path[1]; /* arbitrary length */
79 80 81
};

struct entry_long {
82 83
	struct entry_time ctime;
	struct entry_time mtime;
84 85 86 87 88 89 90 91 92
	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
93
	char path[1]; /* arbitrary length */
94 95 96 97 98 99 100 101
};

/* 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);

static int read_tree(git_index *index, const char *buffer, size_t buffer_size);
102
static int read_tree_internal(git_index_tree **, const char **, const char *, git_index_tree *);
103

104
static int parse_index(git_index *index, const char *buffer, size_t buffer_size);
Vicent Marti committed
105
static int is_index_extended(git_index *index);
106
static void sort_index(git_index *index);
107
static int write_index(git_index *index, git_filebuf *file);
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
int index_srch(const void *key, const void *array_member)
{
	const char *filename = (const char *)key;
	const git_index_entry *entry = *(const git_index_entry **)(array_member);

	return strcmp(filename, entry->path);
}

int index_cmp(const void *a, const void *b)
{
	const git_index_entry *entry_a = *(const git_index_entry **)(a);
	const git_index_entry *entry_b = *(const git_index_entry **)(b);

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

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
int unmerged_srch(const void *key, const void *array_member)
{
	const char *path = (const char *) key;
	const git_index_entry_unmerged *entry = *(const git_index_entry_unmerged **) (array_member);

	return strcmp(path, entry->path);
}

int unmerged_cmp(const void *a, const void *b)
{
	const git_index_entry_unmerged *info_a = *(const git_index_entry_unmerged **)(a);
	const git_index_entry_unmerged *info_b = *(const git_index_entry_unmerged **)(b);

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

141 142 143 144
unsigned int index_create_mode(unsigned int mode)
{
	if (S_ISLNK(mode))
		return S_IFLNK;
145 146
	if (S_ISDIR(mode) || (mode & S_IFMT) == (S_IFLNK | S_IFDIR))
		return (S_IFLNK | S_IFDIR);
147 148 149
	return S_IFREG | ((mode & 0100) ? 0755 : 0644);
}

150
static int index_initialize(git_index **index_out, git_repository *owner, const char *index_path)
151 152 153
{
	git_index *index;

Vicent Marti committed
154
	assert(index_out && index_path);
155 156 157

	index = git__malloc(sizeof(git_index));
	if (index == NULL)
Vicent Marti committed
158
		return GIT_ENOMEM;
159 160 161 162 163 164

	memset(index, 0x0, sizeof(git_index));

	index->index_file_path = git__strdup(index_path);
	if (index->index_file_path == NULL) {
		free(index);
Vicent Marti committed
165
		return GIT_ENOMEM;
166 167
	}

168
	index->repository = owner;
169

170
	git_vector_init(&index->entries, 32, index_cmp);
171

172
	/* Check if index file is stored on disk already */
Vicent Marti committed
173
	if (git_futils_exists(index->index_file_path) == 0)
174 175
		index->on_disk = 1;

Vicent Marti committed
176
	*index_out = index;
177
	return git_index_read(index);
178 179
}

180
int git_index_open(git_index **index_out, const char *index_path)
181
{
182
	assert(index_out && index_path);
183 184 185
	return index_initialize(index_out, NULL, index_path);
}

186 187 188 189
/*
 * Moved from `repository.c`
 */
int git_repository_index(git_index **index_out, git_repository *repo)
190
{
191 192
	assert(index_out && repo);

193
	if (repo->is_bare)
194
		return git__throw(GIT_EBAREINDEX, "Failed to open index. Repository is bare");
195

196 197 198
	return index_initialize(index_out, repo, repo->path_index);
}

199 200
void git_index_free(git_index *index)
{
201
	if (index == NULL)
202 203 204 205
		return;

	git_index_clear(index);
	git_vector_free(&index->entries);
206
	git_vector_free(&index->unmerged);
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

	free(index->index_file_path);
	free(index);
}

static void free_tree(git_index_tree *tree)
{
	unsigned int i;

	if (tree == NULL)
		return;

	for (i = 0; i < tree->children_count; ++i)
		free_tree(tree->children[i]);

	free(tree->name);
	free(tree->children);
	free(tree);
}

227 228 229
void git_index_clear(git_index *index)
{
	unsigned int i;
230 231 232

	assert(index);

233 234 235
	for (i = 0; i < index->entries.length; ++i) {
		git_index_entry *e;
		e = git_vector_get(&index->entries, i);
236
		free((char *)e->path);
237 238
		free(e);
	}
239

240 241 242 243 244 245 246
	for (i = 0; i < index->unmerged.length; ++i) {
		git_index_entry_unmerged *e;
		e = git_vector_get(&index->unmerged, i);
		free((char *)e->path);
		free(e);
	}

247
	git_vector_clear(&index->entries);
248
	git_vector_clear(&index->unmerged);
249 250
	index->last_modified = 0;

251
	free_tree(index->tree);
252 253 254 255 256 257
	index->tree = NULL;
}

int git_index_read(git_index *index)
{
	struct stat indexst;
258
	int error = GIT_SUCCESS;
259

Vicent Marti committed
260
	assert(index->index_file_path);
261

Vicent Marti committed
262
	if (!index->on_disk || git_futils_exists(index->index_file_path) < 0) {
263 264
		git_index_clear(index);
		index->on_disk = 0;
265
		return GIT_SUCCESS;
266 267
	}

Vicent Marti committed
268
	if (p_stat(index->index_file_path, &indexst) < 0)
269
		return git__throw(GIT_EOSERR, "Failed to read index. %s does not exist or is corrupted", index->index_file_path);
270

271
	if (!S_ISREG(indexst.st_mode))
272
		return git__throw(GIT_ENOTFOUND, "Failed to read index. %s is not an index file", index->index_file_path);
273

274 275
	if (indexst.st_mtime != index->last_modified) {

Vicent Marti committed
276
		git_fbuffer buffer;
277

Vicent Marti committed
278
		if ((error = git_futils_readbuffer(&buffer, index->index_file_path)) < GIT_SUCCESS)
279
			return git__rethrow(error, "Failed to read index");
280 281

		git_index_clear(index);
282
		error = parse_index(index, buffer.data, buffer.len);
283

284
		if (error == GIT_SUCCESS)
285 286
			index->last_modified = indexst.st_mtime;

Vicent Marti committed
287
		git_futils_freebuffer(&buffer);
288 289
	}

290 291
	if (error < GIT_SUCCESS)
		return git__rethrow(error, "Failed to read index");
292 293 294 295 296
	return error;
}

int git_index_write(git_index *index)
{
297
	git_filebuf file;
298
	struct stat indexst;
299
	int error;
300

301
	sort_index(index);
302

303
	if ((error = git_filebuf_open(&file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS)) < GIT_SUCCESS)
304
		return git__rethrow(error, "Failed to write index");
305

306
	if ((error = write_index(index, &file)) < GIT_SUCCESS) {
307
		git_filebuf_cleanup(&file);
308
		return git__rethrow(error, "Failed to write index");
309 310
	}

311
	if ((error = git_filebuf_commit(&file)) < GIT_SUCCESS)
Jakob Pfender committed
312
		return git__rethrow(error, "Failed to write index");
313

Vicent Marti committed
314
	if (p_stat(index->index_file_path, &indexst) == 0) {
315 316 317 318
		index->last_modified = indexst.st_mtime;
		index->on_disk = 1;
	}

319
	return GIT_SUCCESS;
320 321
}

322 323 324
unsigned int git_index_entrycount(git_index *index)
{
	assert(index);
325
	return index->entries.length;
326 327
}

328
unsigned int git_index_entrycount_unmerged(git_index *index)
329 330 331 332 333
{
	assert(index);
	return index->unmerged.length;
}

334
git_index_entry *git_index_get(git_index *index, unsigned int n)
335
{
Vicent Marti committed
336
	assert(index);
337
	sort_index(index);
338
	return git_vector_get(&index->entries, n);
339 340
}

341
static void sort_index(git_index *index)
342
{
343
	git_vector_sort(&index->entries);
344 345
}

346
static int index_insert(git_index *index, const git_index_entry *source_entry, int replace)
347
{
348
	git_index_entry *entry;
349 350
	size_t path_length;
	int position;
351

352
	assert(index && source_entry);
353

354
	if (source_entry->path == NULL)
355
		return git__throw(GIT_EMISSINGOBJDATA, "Failed to insert into index. Entry has no path");
356

357 358 359
	entry = git__malloc(sizeof(git_index_entry));
	if (entry == NULL)
		return GIT_ENOMEM;
360

361
	memcpy(entry, source_entry, sizeof(git_index_entry));
362 363

	/* duplicate the path string so we own it */
364 365
	entry->path = git__strdup(entry->path);
	if (entry->path == NULL)
366 367 368
		return GIT_ENOMEM;

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

371
	entry->flags &= ~GIT_IDXENTRY_NAMEMASK;
372 373

	if (path_length < GIT_IDXENTRY_NAMEMASK)
374
		entry->flags |= path_length & GIT_IDXENTRY_NAMEMASK;
375
	else
376
		entry->flags |= GIT_IDXENTRY_NAMEMASK;;
377

378

379 380
	/* look if an entry with this path already exists */
	position = git_index_find(index, source_entry->path);
381

382 383
	/* if no entry exists and replace is not set,
	 * add the entry at the end;
384
	 * the index is no longer sorted */
385
	if (!replace || position == GIT_ENOTFOUND) {
386
		if (git_vector_insert(&index->entries, entry) < GIT_SUCCESS)
387
			return GIT_ENOMEM;
388

389 390
	/* if a previous entry exists and replace is set,
	 * replace it */
391 392
	} else {
		git_index_entry **entry_array = (git_index_entry **)index->entries.contents;
Vicent Marti committed
393

394
		free((char *)entry_array[position]->path);
395
		free(entry_array[position]);
396

397 398
		entry_array[position] = entry;
	}
399

Vicent Marti committed
400
	return GIT_SUCCESS;
401 402
}

403 404 405 406 407 408 409
static int index_init_entry(git_index_entry *entry, git_index *index, const char *rel_path, int stage)
{
	char full_path[GIT_PATH_MAX];
	struct stat st;
	int error;

	if (index->repository == NULL)
410
		return git__throw(GIT_EBAREINDEX, "Failed to initialize entry. Repository is bare");
411

Vicent Marti committed
412
	git_path_join(full_path, index->repository->path_workdir, rel_path);
413

Vicent Marti committed
414
	if (p_lstat(full_path, &st) < 0)
415
		return git__throw(GIT_EOSERR, "Failed to initialize entry. '%s' cannot be opened", full_path);
416 417

	if (stage < 0 || stage > 3)
418
		return git__throw(GIT_ERROR, "Failed to initialize entry. Invalid stage %i", stage);
419 420 421 422 423 424 425 426 427

	memset(entry, 0x0, sizeof(git_index_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;
428
	entry->mode = index_create_mode(st.st_mode);
429 430 431 432 433
	entry->uid = st.st_uid;
	entry->gid = st.st_gid;
	entry->file_size = st.st_size;

	/* write the blob to disk and get the oid */
434
	if ((error = git_blob_create_fromfile(&entry->oid, index->repository, rel_path)) < GIT_SUCCESS)
435
		return git__rethrow(error, "Failed to initialize index entry");
436 437 438 439 440 441 442 443 444 445 446 447

	entry->flags |= (stage << GIT_IDXENTRY_STAGESHIFT);
	entry->path = (char *)rel_path; /* do not duplicate; index_insert already does this */
	return GIT_SUCCESS;
}

int git_index_add(git_index *index, const char *path, int stage)
{
	int error;
	git_index_entry entry;

	if ((error = index_init_entry(&entry, index, path, stage)) < GIT_SUCCESS)
448
		return git__rethrow(error, "Failed to add to index");
449 450 451 452 453 454 455 456 457 458

	return index_insert(index, &entry, 1);
}

int git_index_append(git_index *index, const char *path, int stage)
{
	int error;
	git_index_entry entry;

	if ((error = index_init_entry(&entry, index, path, stage)) < GIT_SUCCESS)
459
		return git__rethrow(error, "Failed to append to index");
460 461 462 463 464 465 466 467 468

	return index_insert(index, &entry, 0);
}

int git_index_add2(git_index *index, const git_index_entry *source_entry)
{
	return index_insert(index, source_entry, 1);
}

469
int git_index_append2(git_index *index, const git_index_entry *source_entry)
470 471 472 473 474
{
	return index_insert(index, source_entry, 0);
}


475
int git_index_remove(git_index *index, int position)
476
{
477
	assert(index);
478
	sort_index(index);
479 480
	return git_vector_remove(&index->entries, (unsigned int)position);
}
481

482 483
int git_index_find(git_index *index, const char *path)
{
484
	sort_index(index);
485
	return git_vector_bsearch2(&index->entries, index_srch, path);
486 487
}

488
const git_index_entry_unmerged *git_index_get_unmerged_bypath(git_index *index, const char *path)
489 490
{
	int pos;
491
	assert(index && path);
492

493 494
	if (!index->unmerged.length)
		return NULL;
495

496 497
	if ((pos = git_vector_bsearch2(&index->unmerged, unmerged_srch, path)) < GIT_SUCCESS)
		return NULL;
498

499
	return git_vector_get(&index->unmerged, pos);
500 501
}

502 503 504 505 506 507
const git_index_entry_unmerged *git_index_get_unmerged_byindex(git_index *index, unsigned int n)
{
	assert(index);
	return git_vector_get(&index->unmerged, n);
}

508

509
static int read_tree_internal(git_index_tree **out,
510 511 512 513
		const char **buffer_in, const char *buffer_end, git_index_tree *parent)
{
	git_index_tree *tree;
	const char *name_start, *buffer;
514
	long count;
515
	int error = GIT_SUCCESS;
516 517

	if ((tree = git__malloc(sizeof(git_index_tree))) == NULL)
518
		return GIT_ENOMEM;
519 520 521 522 523 524

	memset(tree, 0x0, sizeof(git_index_tree));
	tree->parent = parent;

	buffer = name_start = *buffer_in;

525 526
	if ((buffer = memchr(buffer, '\0', buffer_end - buffer)) == NULL) {
		error = GIT_EOBJCORRUPTED;
527
		goto cleanup;
528
	}
529 530 531

	/* NUL-terminated tree name */
	tree->name = git__strdup(name_start);
532 533
	if (tree->name == NULL) {
		error = GIT_ENOMEM;
534
		goto cleanup;
535 536 537 538
	}

	if (++buffer >= buffer_end) {
		error = GIT_EOBJCORRUPTED;
539
		goto cleanup;
540
	}
541 542

	/* Blank-terminated ASCII decimal number of entries in this tree */
543
	if (git__strtol32(&count, buffer, &buffer, 10) < GIT_SUCCESS || count < -1) {
544
		error = GIT_EOBJCORRUPTED;
545
		goto cleanup;
546
	}
547

548 549
	/* Invalidated TREE. Free the tree but report success */
	if (count == -1) {
550 551
		/* FIXME: return buffer_end or the end position for
		 * this single tree entry */
552
		*buffer_in = buffer_end;
553
		*out = NULL;
554
		free_tree(tree); /* Needs to be done manually */
555
		return GIT_SUCCESS;
556 557
	}

558 559
	tree->entries = (size_t)count;

560 561
	if (*buffer != ' ' || ++buffer >= buffer_end) {
		error = GIT_EOBJCORRUPTED;
562
		goto cleanup;
563
	}
564 565

	 /* Number of children of the tree, newline-terminated */
566
	if (git__strtol32(&count, buffer, &buffer, 10) < GIT_SUCCESS ||
567 568
		count < 0) {
		error = GIT_EOBJCORRUPTED;
569
		goto cleanup;
570
	}
571 572 573

	tree->children_count = (size_t)count;

574 575
	if (*buffer != '\n' || ++buffer >= buffer_end) {
		error = GIT_EOBJCORRUPTED;
576
		goto cleanup;
577
	}
578 579

	/* 160-bit SHA-1 for this tree and it's children */
580 581
	if (buffer + GIT_OID_RAWSZ > buffer_end) {
		error = GIT_EOBJCORRUPTED;
582
		goto cleanup;
583
	}
584

Vicent Marti committed
585
	git_oid_fromraw(&tree->oid, (const unsigned char *)buffer);
586 587 588 589 590
	buffer += GIT_OID_RAWSZ;

	/* Parse children: */
	if (tree->children_count > 0) {
		unsigned int i;
591
		int err;
592 593 594

		tree->children = git__malloc(tree->children_count * sizeof(git_index_tree *));
		if (tree->children == NULL)
595
			goto cleanup;
596 597

		for (i = 0; i < tree->children_count; ++i) {
598
			err = read_tree_internal(&tree->children[i], &buffer, buffer_end, tree);
599

600
			if (err < GIT_SUCCESS)
601
				goto cleanup;
602 603 604 605
		}
	}

	*buffer_in = buffer;
606 607 608 609 610
	*out = tree;
	return GIT_SUCCESS;

 cleanup:
	free_tree(tree);
611
	return error;
612 613 614 615 616
}

static int read_tree(git_index *index, const char *buffer, size_t buffer_size)
{
	const char *buffer_end = buffer + buffer_size;
617 618 619
	int error;

	error = read_tree_internal(&index->tree, &buffer, buffer_end, NULL);
620

621 622 623 624
	if (buffer < buffer_end)
		return GIT_EOBJCORRUPTED;

	return error;
625 626
}

627
static int read_unmerged(git_index *index, const char *buffer, size_t size)
628
{
629 630
	const char *endptr;
	size_t len;
631 632
	int i;

633
	git_vector_init(&index->unmerged, 16, unmerged_cmp);
634 635 636 637 638 639

	while (size) {
		git_index_entry_unmerged *lost;

		len = strlen(buffer) + 1;
		if (size <= len)
640
			return git__throw(GIT_ERROR, "Failed to read unmerged entries");
641 642

		if ((lost = git__malloc(sizeof(git_index_entry_unmerged))) == NULL)
643
			return GIT_ENOMEM;
644 645

		if (git_vector_insert(&index->unmerged, lost) < GIT_SUCCESS)
646
			return git__throw(GIT_ERROR, "Failed to read unmerged entries");
647

648 649 650 651
		lost->path = git__strdup(buffer);
		if (!lost->path)
			return GIT_ENOMEM;

652 653 654 655
		size -= len;
		buffer += len;

		for (i = 0; i < 3; i++) {
656 657
			if (git__strtol32((long int *) &lost->mode[i], buffer, &endptr, 8) < GIT_SUCCESS ||
				!endptr || endptr == buffer || *endptr)
658
				return GIT_ERROR;
659

660 661
			len = (endptr + 1) - (char *) buffer;
			if (size <= len)
662
				return git__throw(GIT_ERROR, "Failed to read unmerged entries");
663

664 665 666 667 668 669 670 671
			size -= len;
			buffer += len;
		}

		for (i = 0; i < 3; i++) {
			if (!lost->mode[i])
				continue;
			if (size < 20)
672
				return git__throw(GIT_ERROR, "Failed to read unmerged entries");
Vicent Marti committed
673
			git_oid_fromraw(&lost->oid[i], (unsigned char *) buffer);
674 675 676 677 678 679 680 681
			size -= 20;
			buffer += 20;
		}
	}

	return GIT_SUCCESS;
}

682 683 684 685 686 687 688 689 690 691
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;
	const struct entry_short *source;

	if (INDEX_FOOTER_SIZE + minimal_entry_size > buffer_size)
		return 0;

692 693
	memset(dest, 0x0, sizeof(git_index_entry));

694 695
	source = (const struct entry_short *)(buffer);

696 697 698 699
	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);
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
	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) {
		struct entry_long *source_l = (struct entry_long *)source;
		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)
727
				return 0;
728 729 730 731 732 733 734 735 736 737 738 739 740

		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);
741
	assert(dest->path);
742 743 744 745 746 747 748 749 750

	return entry_size;
}

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

751 752
	dest->signature = ntohl(source->signature);
	if (dest->signature != INDEX_HEADER_SIG)
753
		return GIT_EOBJCORRUPTED;
754 755

	dest->version = ntohl(source->version);
756 757
	if (dest->version != INDEX_VERSION_NUMBER_EXT &&
		dest->version != INDEX_VERSION_NUMBER)
758
		return GIT_EOBJCORRUPTED;
759 760

	dest->entry_count = ntohl(source->entry_count);
761
	return GIT_SUCCESS;
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
}

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) {
784
			if (read_tree(index, buffer + 8, dest.extension_size) < GIT_SUCCESS)
785
				return 0;
786 787 788
		} else if (memcmp(dest.signature, INDEX_EXT_UNMERGED_SIG, 4) == 0) {
			if (read_unmerged(index, buffer + 8, dest.extension_size) < GIT_SUCCESS)
				return 0;
789
		}
790 791
		/* else, unsupported extension. We cannot parse this, but we can skip
		 * it by returning `total_size */
792 793 794 795 796 797 798 799 800
	} else {
		/* we cannot handle non-ignorable extensions;
		 * in fact they aren't even defined in the standard */
		return 0;
	}

	return total_size;
}

801
static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
802 803 804 805 806 807 808
{
	unsigned int i;
	struct index_header header;
	git_oid checksum_calculated, checksum_expected;

#define seek_forward(_increase) { \
	if (_increase >= buffer_size) \
809
		return git__throw(GIT_EOBJCORRUPTED, "Failed to seek forward. Buffer size exceeded"); \
810 811 812 813 814
	buffer += _increase; \
	buffer_size -= _increase;\
}

	if (buffer_size < INDEX_HEADER_SIZE + INDEX_FOOTER_SIZE)
815
		return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Buffer too small");
816 817 818 819 820 821

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

	/* Parse header */
822
	if (read_header(&header, buffer) < GIT_SUCCESS)
823
		return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Header is corrupted");
824 825 826

	seek_forward(INDEX_HEADER_SIZE);

827
	git_vector_clear(&index->entries);
828 829

	/* Parse all the entries */
830
	for (i = 0; i < header.entry_count && buffer_size > INDEX_FOOTER_SIZE; ++i) {
831
		size_t entry_size;
832 833 834 835 836 837 838
		git_index_entry *entry;

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

		entry_size = read_entry(entry, buffer, buffer_size);
839 840 841

		/* 0 bytes read means an object corruption */
		if (entry_size == 0)
842
			return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Entry size is zero");
843

844
		if (git_vector_insert(&index->entries, entry) < GIT_SUCCESS)
845 846
			return GIT_ENOMEM;

847 848 849
		seek_forward(entry_size);
	}

850
	if (i != header.entry_count)
851
		return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Header entries changed while parsing");
852

853 854 855 856 857 858 859 860
	/* 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)
861
			return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Extension size is zero");
862 863 864 865 866

		seek_forward(extension_size);
	}

	if (buffer_size != INDEX_FOOTER_SIZE)
867
		return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Buffer size does not match index footer size");
868 869

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

	if (git_oid_cmp(&checksum_calculated, &checksum_expected) != 0)
873
		return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Calculated checksum does not match expected checksum");
874 875 876

#undef seek_forward

877 878 879
	/* force sorting in the vector: the entries are
	 * assured to be sorted on the index */
	index->entries.sorted = 1;
880
	return GIT_SUCCESS;
881 882
}

Vicent Marti committed
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
static int is_index_extended(git_index *index)
{
	unsigned int i, extended;

	extended = 0;

	for (i = 0; i < index->entries.length; ++i) {
		git_index_entry *entry;
		entry = git_vector_get(&index->entries, i);
		entry->flags &= ~GIT_IDXENTRY_EXTENDED;
		if (entry->flags_extended & GIT_IDXENTRY_EXTENDED_FLAGS) {
			extended++;
			entry->flags |= GIT_IDXENTRY_EXTENDED;
		}
	}
	return extended;
}

901
static int write_disk_entry(git_filebuf *file, git_index_entry *entry)
902
{
903
	struct entry_short *ondisk;
904
	size_t path_len, disk_size;
905
	char *path;
906

907
	path_len = strlen(entry->path);
908

909
	if (entry->flags & GIT_IDXENTRY_EXTENDED)
910
		disk_size = long_entry_size(path_len);
911
	else
912
		disk_size = short_entry_size(path_len);
913

914 915 916 917
	if (git_filebuf_reserve(file, (void **)&ondisk, disk_size) < GIT_SUCCESS)
		return GIT_ENOMEM;

	memset(ondisk, 0x0, disk_size);
918

919 920
	ondisk->ctime.seconds = htonl((unsigned long)entry->ctime.seconds);
	ondisk->mtime.seconds = htonl((unsigned long)entry->mtime.seconds);
921 922 923 924 925 926 927
	ondisk->ctime.nanoseconds = htonl(entry->ctime.nanoseconds);
	ondisk->mtime.nanoseconds = htonl(entry->mtime.nanoseconds);
	ondisk->dev  = htonl(entry->dev);
	ondisk->ino  = htonl(entry->ino);
	ondisk->mode = htonl(entry->mode);
	ondisk->uid  = htonl(entry->uid);
	ondisk->gid  = htonl(entry->gid);
928
	ondisk->file_size = htonl((unsigned long)entry->file_size);
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943

	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);
944

945
	return GIT_SUCCESS;
946 947
}

948
static int write_entries(git_index *index, git_filebuf *file)
949 950
{
	unsigned int i;
951

952
	for (i = 0; i < index->entries.length; ++i) {
953
		git_index_entry *entry;
954
		entry = git_vector_get(&index->entries, i);
955
		if (write_disk_entry(file, entry) < GIT_SUCCESS)
956
			return GIT_ENOMEM;
957 958
	}

959 960 961
	return GIT_SUCCESS;
}

962
static int write_index(git_index *index, git_filebuf *file)
963 964 965 966 967 968
{
	int error = GIT_SUCCESS;
	git_oid hash_final;

	struct index_header header;

Vicent Marti committed
969
	int is_extended;
970

971
	assert(index && file);
972

Vicent Marti committed
973 974
	is_extended = is_index_extended(index);

975
	header.signature = htonl(INDEX_HEADER_SIG);
Vicent Marti committed
976
	header.version = htonl(is_extended ? INDEX_VERSION_NUMBER_EXT : INDEX_VERSION_NUMBER);
977 978
	header.entry_count = htonl(index->entries.length);

979
	git_filebuf_write(file, &header, sizeof(struct index_header));
980

981
	error = write_entries(index, file);
982
	if (error < GIT_SUCCESS)
983
		return git__rethrow(error, "Failed to write index");
984 985 986

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

987 988 989 990 991
	/* 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 */
	git_filebuf_write(file, hash_final.id, GIT_OID_RAWSZ);
992

993
	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to write index");
994
}
995 996 997 998 999

int git_index_entry_stage(const git_index_entry *entry)
{
	return (entry->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT;
}