tree-cache.c 6.35 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7 8
 *
 * 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"
9

10
#include "pool.h"
11
#include "tree.h"
12

13 14
static git_tree_cache *find_child(
	const git_tree_cache *tree, const char *path, const char *end)
15
{
16
	size_t i, dirlen = end ? (size_t)(end - path) : strlen(path);
17 18

	for (i = 0; i < tree->children_count; ++i) {
19
		git_tree_cache *child = tree->children[i];
20

21 22
		if (child->namelen == dirlen && !memcmp(path, child->name, dirlen))
			return child;
23 24 25 26 27 28 29 30 31 32 33 34
	}

	return NULL;
}

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

	if (tree == NULL)
		return;

35
	tree->entry_count = -1;
36 37 38 39 40 41 42

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

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

43
		tree = find_child(tree, ptr, end);
44 45 46
		if (tree == NULL) /* We don't have that tree */
			return;

47
		tree->entry_count = -1;
48 49 50 51
		ptr = end + 1;
	}
}

52 53 54 55 56 57 58 59 60 61 62
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, '/');

63 64
		tree = find_child(tree, ptr, end);
		if (tree == NULL) /* Can't find it */
65 66
			return NULL;

67
		if (end == NULL || *end + 1 == '\0')
68 69 70 71 72 73
			return tree;

		ptr = end + 1;
	}
}

74
static int read_tree_internal(git_tree_cache **out,
75
			      const char **buffer_in, const char *buffer_end,
76
			      git_pool *pool)
77
{
78
	git_tree_cache *tree = NULL;
79 80 81 82 83
	const char *name_start, *buffer;
	int count;

	buffer = name_start = *buffer_in;

84 85
	if ((buffer = memchr(buffer, '\0', buffer_end - buffer)) == NULL)
		goto corrupted;
86

87 88
	if (++buffer >= buffer_end)
		goto corrupted;
89

90
	if (git_tree_cache_new(&tree, name_start, pool) < 0)
91
		return -1;
92

93
	/* Blank-terminated ASCII decimal number of entries in this tree */
94
	if (git__strntol32(&count, buffer, buffer_end - buffer, &buffer, 10) < 0)
95
		goto corrupted;
96

97
	tree->entry_count = count;
98

99 100
	if (*buffer != ' ' || ++buffer >= buffer_end)
		goto corrupted;
101 102

	 /* Number of children of the tree, newline-terminated */
103
	if (git__strntol32(&count, buffer, buffer_end - buffer, &buffer, 10) < 0 || count < 0)
104
		goto corrupted;
105 106 107

	tree->children_count = count;

108 109
	if (*buffer != '\n' || ++buffer > buffer_end)
		goto corrupted;
110

111
	/* The SHA1 is only there if it's not invalidated */
112
	if (tree->entry_count >= 0) {
113
		/* 160-bit SHA-1 for this tree and it's children */
114 115
		if (buffer + GIT_OID_RAWSZ > buffer_end)
			goto corrupted;
116

117 118 119
		git_oid_fromraw(&tree->oid, (const unsigned char *)buffer);
		buffer += GIT_OID_RAWSZ;
	}
120 121 122

	/* Parse children: */
	if (tree->children_count > 0) {
123
		size_t i, bufsize;
124

125
		GIT_ERROR_CHECK_ALLOC_MULTIPLY(&bufsize, tree->children_count, sizeof(git_tree_cache*));
126 127

		tree->children = git_pool_malloc(pool, bufsize);
128
		GIT_ERROR_CHECK_ALLOC(tree->children);
129

130
		memset(tree->children, 0x0, bufsize);
131

132
		for (i = 0; i < tree->children_count; ++i) {
133
			if (read_tree_internal(&tree->children[i], &buffer, buffer_end, pool) < 0)
134
				goto corrupted;
135 136 137 138 139
		}
	}

	*buffer_in = buffer;
	*out = tree;
140
	return 0;
141

142
 corrupted:
143
	git_error_set(GIT_ERROR_INDEX, "corrupted TREE extension in index");
144
	return -1;
145 146
}

147
int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size, git_pool *pool)
148 149 150
{
	const char *buffer_end = buffer + buffer_size;

151
	if (read_tree_internal(tree, &buffer, buffer_end, pool) < 0)
152
		return -1;
153

154
	if (buffer < buffer_end) {
155
		git_error_set(GIT_ERROR_INDEX, "corrupted TREE extension in index (unexpected trailing data)");
156 157
		return -1;
	}
158

159
	return 0;
160 161
}

162 163 164
static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_pool *pool)
{
	git_repository *repo;
165
	size_t i, j, nentries, ntrees, alloc_size;
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	int error;

	repo = git_tree_owner(tree);

	git_oid_cpy(&cache->oid, git_tree_id(tree));
	nentries = git_tree_entrycount(tree);

	/*
	 * We make sure we know how many trees we need to allocate for
	 * so we don't have to realloc and change the pointers for the
	 * parents.
	 */
	ntrees = 0;
	for (i = 0; i < nentries; i++) {
		const git_tree_entry *entry;

		entry = git_tree_entry_byindex(tree, i);
		if (git_tree_entry_filemode(entry) == GIT_FILEMODE_TREE)
			ntrees++;
	}

187
	GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloc_size, ntrees, sizeof(git_tree_cache *));
188

189
	cache->children_count = ntrees;
190
	cache->children = git_pool_mallocz(pool, alloc_size);
191
	GIT_ERROR_CHECK_ALLOC(cache->children);
192 193 194 195 196 197 198

	j = 0;
	for (i = 0; i < nentries; i++) {
		const git_tree_entry *entry;
		git_tree *subtree;

		entry = git_tree_entry_byindex(tree, i);
199 200
		if (git_tree_entry_filemode(entry) != GIT_FILEMODE_TREE) {
			cache->entry_count++;
201
			continue;
202
		}
203

204
		if ((error = git_tree_cache_new(&cache->children[j], git_tree_entry_name(entry), pool)) < 0)
205 206 207 208 209 210 211
			return error;

		if ((error = git_tree_lookup(&subtree, repo, git_tree_entry_id(entry))) < 0)
			return error;

		error = read_tree_recursive(cache->children[j], subtree, pool);
		git_tree_free(subtree);
212
		cache->entry_count += cache->children[j]->entry_count;
213 214 215 216 217 218 219 220 221 222 223 224 225 226
		j++;

		if (error < 0)
			return error;
	}

	return 0;
}

int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_pool *pool)
{
	int error;
	git_tree_cache *cache;

227
	if ((error = git_tree_cache_new(&cache, "", pool)) < 0)
228 229 230 231 232 233 234 235 236
		return error;

	if ((error = read_tree_recursive(cache, tree, pool)) < 0)
		return error;

	*out = cache;
	return 0;
}

237
int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool)
238
{
239
	size_t name_len, alloc_size;
240 241 242
	git_tree_cache *tree;

	name_len = strlen(name);
243 244 245

	GIT_ERROR_CHECK_ALLOC_ADD3(&alloc_size, sizeof(git_tree_cache), name_len, 1);

246
	tree = git_pool_malloc(pool, alloc_size);
247
	GIT_ERROR_CHECK_ALLOC(tree);
248 249 250 251 252 253 254 255 256 257

	memset(tree, 0x0, sizeof(git_tree_cache));
	/* NUL-terminated tree name */
	tree->namelen = name_len;
	memcpy(tree->name, name, name_len);
	tree->name[name_len] = '\0';

	*out = tree;
	return 0;
}
258

259
static void write_tree(git_str *out, git_tree_cache *tree)
260 261 262
{
	size_t i;

263
	git_str_printf(out, "%s%c%"PRIdZ" %"PRIuZ"\n", tree->name, 0, tree->entry_count, tree->children_count);
264

265
	if (tree->entry_count != -1)
266
		git_str_put(out, (const char *) &tree->oid, GIT_OID_RAWSZ);
267 268 269 270 271

	for (i = 0; i < tree->children_count; i++)
		write_tree(out, tree->children[i]);
}

272
int git_tree_cache_write(git_str *out, git_tree_cache *tree)
273 274 275
{
	write_tree(out, tree);

276
	return git_str_oom(out) ? -1 : 0;
277
}