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