tree-cache.c 4 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
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 11
static git_tree_cache *find_child(
	const git_tree_cache *tree, const char *path, const char *end)
12
{
13
	size_t i, dirlen = end ? (size_t)(end - path) : strlen(path);
14 15

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

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

	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;

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

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

49 50 51 52 53 54 55 56 57 58 59
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, '/');

60 61
		tree = find_child(tree, ptr, end);
		if (tree == NULL) /* Can't find it */
62 63
			return NULL;

64
		if (end == NULL || *end + 1 == '\0')
65 66 67 68 69 70
			return tree;

		ptr = end + 1;
	}
}

71 72 73
static int read_tree_internal(git_tree_cache **out,
		const char **buffer_in, const char *buffer_end, git_tree_cache *parent)
{
74
	git_tree_cache *tree = NULL;
75 76
	const char *name_start, *buffer;
	int count;
77
	size_t name_len;
78 79 80

	buffer = name_start = *buffer_in;

81 82
	if ((buffer = memchr(buffer, '\0', buffer_end - buffer)) == NULL)
		goto corrupted;
83

84 85
	if (++buffer >= buffer_end)
		goto corrupted;
86

87
	name_len = strlen(name_start);
88 89
	tree = git__malloc(sizeof(git_tree_cache) + name_len + 1);
	GITERR_CHECK_ALLOC(tree);
90 91 92 93 94

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

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

99
	/* Blank-terminated ASCII decimal number of entries in this tree */
100
	if (git__strtol32(&count, buffer, &buffer, 10) < 0)
101
		goto corrupted;
102 103 104

	tree->entries = count;

105 106
	if (*buffer != ' ' || ++buffer >= buffer_end)
		goto corrupted;
107 108

	 /* Number of children of the tree, newline-terminated */
109 110
	if (git__strtol32(&count, buffer, &buffer, 10) < 0 || count < 0)
		goto corrupted;
111 112 113

	tree->children_count = count;

114 115
	if (*buffer != '\n' || ++buffer > buffer_end)
		goto corrupted;
116

117 118 119
	/* 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 */
120 121
		if (buffer + GIT_OID_RAWSZ > buffer_end)
			goto corrupted;
122

123 124 125
		git_oid_fromraw(&tree->oid, (const unsigned char *)buffer);
		buffer += GIT_OID_RAWSZ;
	}
126 127 128 129 130 131

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

		tree->children = git__malloc(tree->children_count * sizeof(git_tree_cache *));
132
		GITERR_CHECK_ALLOC(tree->children);
133

134 135
		memset(tree->children, 0x0, tree->children_count * sizeof(git_tree_cache *));

136
		for (i = 0; i < tree->children_count; ++i) {
137
			if (read_tree_internal(&tree->children[i], &buffer, buffer_end, tree) < 0)
138
				goto corrupted;
139 140 141 142 143
		}
	}

	*buffer_in = buffer;
	*out = tree;
144
	return 0;
145

146
 corrupted:
147
	git_tree_cache_free(tree);
148
	giterr_set(GITERR_INDEX, "Corrupted TREE extension in index");
149
	return -1;
150 151 152 153 154 155
}

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

156 157
	if (read_tree_internal(tree, &buffer, buffer_end, NULL) < 0)
		return -1;
158

159
	if (buffer < buffer_end) {
160
		giterr_set(GITERR_INDEX, "Corrupted TREE extension in index (unexpected trailing data)");
161 162
		return -1;
	}
163

164
	return 0;
165 166 167 168 169 170 171 172 173
}

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

	if (tree == NULL)
		return;

174 175 176 177 178 179
	if (tree->children != NULL) {
		for (i = 0; i < tree->children_count; ++i)
			git_tree_cache_free(tree->children[i]);

		git__free(tree->children);
	}
180

181
	git__free(tree);
182
}