]> git.proxmox.com Git - libgit2.git/blob - src/tree-cache.c
Add git_tree_cache_invalidate_path
[libgit2.git] / src / tree-cache.c
1 /*
2 * Copyright (C) 2009-2011 the libgit2 contributors
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(git_tree_cache *tree, const char *path)
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
32 void 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
56 static int read_tree_internal(git_tree_cache **out,
57 const char **buffer_in, const char *buffer_end, git_tree_cache *parent)
58 {
59 git_tree_cache *tree = NULL;
60 const char *name_start, *buffer;
61 int count;
62 int error = GIT_SUCCESS;
63 size_t name_len;
64
65 buffer = name_start = *buffer_in;
66
67 if ((buffer = memchr(buffer, '\0', buffer_end - buffer)) == NULL) {
68 error = GIT_EOBJCORRUPTED;
69 goto cleanup;
70 }
71
72 if (++buffer >= buffer_end) {
73 error = GIT_EOBJCORRUPTED;
74 goto cleanup;
75 }
76
77 name_len = strlen(name_start);
78 if ((tree = git__malloc(sizeof(git_tree_cache) + name_len + 1)) == NULL)
79 return GIT_ENOMEM;
80
81 memset(tree, 0x0, sizeof(git_tree_cache));
82 tree->parent = parent;
83
84 /* NUL-terminated tree name */
85 memcpy(tree->name, name_start, name_len);
86 tree->name[name_len] = '\0';
87
88 /* Blank-terminated ASCII decimal number of entries in this tree */
89 if (git__strtol32(&count, buffer, &buffer, 10) < GIT_SUCCESS || count < -1) {
90 error = GIT_EOBJCORRUPTED;
91 goto cleanup;
92 }
93
94 tree->entries = count;
95
96 if (*buffer != ' ' || ++buffer >= buffer_end) {
97 error = GIT_EOBJCORRUPTED;
98 goto cleanup;
99 }
100
101 /* Number of children of the tree, newline-terminated */
102 if (git__strtol32(&count, buffer, &buffer, 10) < GIT_SUCCESS ||
103 count < 0) {
104 error = GIT_EOBJCORRUPTED;
105 goto cleanup;
106 }
107
108 tree->children_count = count;
109
110 if (*buffer != '\n' || ++buffer >= buffer_end) {
111 error = GIT_EOBJCORRUPTED;
112 goto cleanup;
113 }
114
115 /* The SHA1 is only there if it's not invalidated */
116 if (tree->entries >= 0) {
117 /* 160-bit SHA-1 for this tree and it's children */
118 if (buffer + GIT_OID_RAWSZ > buffer_end) {
119 error = GIT_EOBJCORRUPTED;
120 goto cleanup;
121 }
122
123 git_oid_fromraw(&tree->oid, (const unsigned char *)buffer);
124 buffer += GIT_OID_RAWSZ;
125 }
126
127 /* Parse children: */
128 if (tree->children_count > 0) {
129 unsigned int i;
130 int err;
131
132 tree->children = git__malloc(tree->children_count * sizeof(git_tree_cache *));
133 if (tree->children == NULL)
134 goto cleanup;
135
136 for (i = 0; i < tree->children_count; ++i) {
137 err = read_tree_internal(&tree->children[i], &buffer, buffer_end, tree);
138
139 if (err < GIT_SUCCESS)
140 goto cleanup;
141 }
142 }
143
144 *buffer_in = buffer;
145 *out = tree;
146 return GIT_SUCCESS;
147
148 cleanup:
149 git_tree_cache_free(tree);
150 return error;
151 }
152
153 int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size)
154 {
155 const char *buffer_end = buffer + buffer_size;
156 int error;
157
158 error = read_tree_internal(tree, &buffer, buffer_end, NULL);
159
160 if (buffer < buffer_end)
161 return GIT_EOBJCORRUPTED;
162
163 return error;
164 }
165
166 void git_tree_cache_free(git_tree_cache *tree)
167 {
168 unsigned int i;
169
170 if (tree == NULL)
171 return;
172
173 for (i = 0; i < tree->children_count; ++i)
174 git_tree_cache_free(tree->children[i]);
175
176 free(tree->children);
177 free(tree);
178 }