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

#include "tree-cache.h"

10
static git_tree_cache *find_child(const git_tree_cache *tree, const char *path)
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
{
	size_t i, dirlen;
	const char *end;

	end = strchr(path, '/');
	if (end == NULL) {
		end = strrchr(path, '\0');
	}

	dirlen = end - path;

	for (i = 0; i < tree->children_count; ++i) {
		const char *childname = tree->children[i]->name;

		if (strlen(childname) == dirlen && !memcmp(path, childname, dirlen))
			return tree->children[i];
	}

	return NULL;
}

void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path)
{
	const char *ptr = path, *end;

	if (tree == NULL)
		return;

	tree->entries = -1;

	while (ptr != NULL) {
		end = strchr(ptr, '/');

		if (end == NULL) /* End of path */
			break;

		tree = find_child(tree, ptr);
		if (tree == NULL) /* We don't have that tree */
			return;

		tree->entries = -1;
		ptr = end + 1;
	}
}

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char *path)
{
	const char *ptr = path, *end;

	if (tree == NULL) {
		return NULL;
	}

	while (1) {
		end = strchr(ptr, '/');

		tree = find_child(tree, ptr);
		if (tree == NULL) { /* Can't find it */
			return NULL;
		}

72
		if (end == NULL || *end + 1 == '\0')
73 74 75 76 77 78
			return tree;

		ptr = end + 1;
	}
}

79 80 81
static int read_tree_internal(git_tree_cache **out,
		const char **buffer_in, const char *buffer_end, git_tree_cache *parent)
{
82
	git_tree_cache *tree = NULL;
83 84
	const char *name_start, *buffer;
	int count;
85
	size_t name_len;
86 87 88

	buffer = name_start = *buffer_in;

89 90
	if ((buffer = memchr(buffer, '\0', buffer_end - buffer)) == NULL)
		goto corrupted;
91

92 93
	if (++buffer >= buffer_end)
		goto corrupted;
94

95
	name_len = strlen(name_start);
96 97
	tree = git__malloc(sizeof(git_tree_cache) + name_len + 1);
	GITERR_CHECK_ALLOC(tree);
98 99 100 101 102 103 104 105

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

	/* NUL-terminated tree name */
	memcpy(tree->name, name_start, name_len);
	tree->name[name_len] = '\0';

106
	/* Blank-terminated ASCII decimal number of entries in this tree */
107 108
	if (git__strtol32(&count, buffer, &buffer, 10) < 0 || count < -1)
		goto corrupted;
109 110 111

	tree->entries = count;

112 113
	if (*buffer != ' ' || ++buffer >= buffer_end)
		goto corrupted;
114 115

	 /* Number of children of the tree, newline-terminated */
116 117
	if (git__strtol32(&count, buffer, &buffer, 10) < 0 || count < 0)
		goto corrupted;
118 119 120

	tree->children_count = count;

121 122
	if (*buffer != '\n' || ++buffer > buffer_end)
		goto corrupted;
123

124 125 126
	/* The SHA1 is only there if it's not invalidated */
	if (tree->entries >= 0) {
		/* 160-bit SHA-1 for this tree and it's children */
127 128
		if (buffer + GIT_OID_RAWSZ > buffer_end)
			goto corrupted;
129

130 131 132
		git_oid_fromraw(&tree->oid, (const unsigned char *)buffer);
		buffer += GIT_OID_RAWSZ;
	}
133 134 135 136 137 138

	/* Parse children: */
	if (tree->children_count > 0) {
		unsigned int i;

		tree->children = git__malloc(tree->children_count * sizeof(git_tree_cache *));
139
		GITERR_CHECK_ALLOC(tree->children);
140 141

		for (i = 0; i < tree->children_count; ++i) {
142 143
			if (read_tree_internal(&tree->children[i], &buffer, buffer_end, tree) < 0)
				return -1;
144 145 146 147 148
		}
	}

	*buffer_in = buffer;
	*out = tree;
149
	return 0;
150

151
 corrupted:
152
	git_tree_cache_free(tree);
153 154
	giterr_set(GITERR_INDEX, "Corruped TREE extension in index");
	return -1;
155 156 157 158 159 160
}

int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size)
{
	const char *buffer_end = buffer + buffer_size;

161 162
	if (read_tree_internal(tree, &buffer, buffer_end, NULL) < 0)
		return -1;
163

164 165 166 167
	if (buffer < buffer_end) {
		giterr_set(GITERR_INDEX, "Corruped TREE extension in index (unexpected trailing data)");
		return -1;
	}
168

169
	return 0;
170 171 172 173 174 175 176 177 178 179 180 181
}

void git_tree_cache_free(git_tree_cache *tree)
{
	unsigned int i;

	if (tree == NULL)
		return;

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

182 183
	git__free(tree->children);
	git__free(tree);
184
}