]> git.proxmox.com Git - libgit2.git/blob - src/tree-cache.c
tree-cache: extract the allocation
[libgit2.git] / src / tree-cache.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "tree-cache.h"
9
10 static git_tree_cache *find_child(
11 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 if (child->namelen == dirlen && !memcmp(path, child->name, dirlen))
19 return child;
20 }
21
22 return NULL;
23 }
24
25 void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path)
26 {
27 const char *ptr = path, *end;
28
29 if (tree == NULL)
30 return;
31
32 tree->entries = -1;
33
34 while (ptr != NULL) {
35 end = strchr(ptr, '/');
36
37 if (end == NULL) /* End of path */
38 break;
39
40 tree = find_child(tree, ptr, end);
41 if (tree == NULL) /* We don't have that tree */
42 return;
43
44 tree->entries = -1;
45 ptr = end + 1;
46 }
47 }
48
49 const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char *path)
50 {
51 const char *ptr = path, *end;
52
53 if (tree == NULL) {
54 return NULL;
55 }
56
57 while (1) {
58 end = strchr(ptr, '/');
59
60 tree = find_child(tree, ptr, end);
61 if (tree == NULL) /* Can't find it */
62 return NULL;
63
64 if (end == NULL || *end + 1 == '\0')
65 return tree;
66
67 ptr = end + 1;
68 }
69 }
70
71 static int read_tree_internal(git_tree_cache **out,
72 const char **buffer_in, const char *buffer_end, git_tree_cache *parent)
73 {
74 git_tree_cache *tree = NULL;
75 const char *name_start, *buffer;
76 int count;
77
78 buffer = name_start = *buffer_in;
79
80 if ((buffer = memchr(buffer, '\0', buffer_end - buffer)) == NULL)
81 goto corrupted;
82
83 if (++buffer >= buffer_end)
84 goto corrupted;
85
86 if (git_tree_cache_new(&tree, name_start, parent) < 0)
87 return -1;
88
89 /* Blank-terminated ASCII decimal number of entries in this tree */
90 if (git__strtol32(&count, buffer, &buffer, 10) < 0)
91 goto corrupted;
92
93 tree->entries = count;
94
95 if (*buffer != ' ' || ++buffer >= buffer_end)
96 goto corrupted;
97
98 /* Number of children of the tree, newline-terminated */
99 if (git__strtol32(&count, buffer, &buffer, 10) < 0 || count < 0)
100 goto corrupted;
101
102 tree->children_count = count;
103
104 if (*buffer != '\n' || ++buffer > buffer_end)
105 goto corrupted;
106
107 /* The SHA1 is only there if it's not invalidated */
108 if (tree->entries >= 0) {
109 /* 160-bit SHA-1 for this tree and it's children */
110 if (buffer + GIT_OID_RAWSZ > buffer_end)
111 goto corrupted;
112
113 git_oid_fromraw(&tree->oid, (const unsigned char *)buffer);
114 buffer += GIT_OID_RAWSZ;
115 }
116
117 /* Parse children: */
118 if (tree->children_count > 0) {
119 unsigned int i;
120
121 tree->children = git__malloc(tree->children_count * sizeof(git_tree_cache *));
122 GITERR_CHECK_ALLOC(tree->children);
123
124 memset(tree->children, 0x0, tree->children_count * sizeof(git_tree_cache *));
125
126 for (i = 0; i < tree->children_count; ++i) {
127 if (read_tree_internal(&tree->children[i], &buffer, buffer_end, tree) < 0)
128 goto corrupted;
129 }
130 }
131
132 *buffer_in = buffer;
133 *out = tree;
134 return 0;
135
136 corrupted:
137 git_tree_cache_free(tree);
138 giterr_set(GITERR_INDEX, "Corrupted TREE extension in index");
139 return -1;
140 }
141
142 int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size)
143 {
144 const char *buffer_end = buffer + buffer_size;
145
146 if (read_tree_internal(tree, &buffer, buffer_end, NULL) < 0)
147 return -1;
148
149 if (buffer < buffer_end) {
150 giterr_set(GITERR_INDEX, "Corrupted TREE extension in index (unexpected trailing data)");
151 return -1;
152 }
153
154 return 0;
155 }
156
157 int git_tree_cache_new(git_tree_cache **out, const char *name, git_tree_cache *parent)
158 {
159 size_t name_len;
160 git_tree_cache *tree;
161
162 name_len = strlen(name);
163 tree = git__malloc(sizeof(git_tree_cache) + name_len + 1);
164 GITERR_CHECK_ALLOC(tree);
165
166 memset(tree, 0x0, sizeof(git_tree_cache));
167 tree->parent = parent;
168 /* NUL-terminated tree name */
169 tree->namelen = name_len;
170 memcpy(tree->name, name, name_len);
171 tree->name[name_len] = '\0';
172
173 *out = tree;
174 return 0;
175 }
176
177 void git_tree_cache_free(git_tree_cache *tree)
178 {
179 unsigned int i;
180
181 if (tree == NULL)
182 return;
183
184 if (tree->children != NULL) {
185 for (i = 0; i < tree->children_count; ++i)
186 git_tree_cache_free(tree->children[i]);
187
188 git__free(tree->children);
189 }
190
191 git__free(tree);
192 }