]>
Commit | Line | Data |
---|---|---|
175785e5 DB |
1 | #include "tree.h" |
2 | #include "blob.h" | |
3 | #include "cache.h" | |
4 | #include <stdlib.h> | |
5 | ||
6 | const char *tree_type = "tree"; | |
7 | ||
94537c78 LT |
8 | static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage) |
9 | { | |
10 | int len = strlen(pathname); | |
11 | unsigned int size = cache_entry_size(baselen + len); | |
812666c8 | 12 | struct cache_entry *ce = xmalloc(size); |
94537c78 LT |
13 | |
14 | memset(ce, 0, size); | |
15 | ||
16 | ce->ce_mode = create_ce_mode(mode); | |
17 | ce->ce_flags = create_ce_flags(baselen + len, stage); | |
18 | memcpy(ce->name, base, baselen); | |
19 | memcpy(ce->name + baselen, pathname, len+1); | |
20 | memcpy(ce->sha1, sha1, 20); | |
b155725d | 21 | return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); |
94537c78 LT |
22 | } |
23 | ||
3e587635 | 24 | static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths) |
0ca14a57 | 25 | { |
3e587635 | 26 | const char *match; |
0ca14a57 LT |
27 | int pathlen; |
28 | ||
29 | if (!paths) | |
30 | return 1; | |
31 | pathlen = strlen(path); | |
32 | while ((match = *paths++) != NULL) { | |
33 | int matchlen = strlen(match); | |
34 | ||
35 | if (baselen >= matchlen) { | |
36 | /* If it doesn't match, move along... */ | |
37 | if (strncmp(base, match, matchlen)) | |
38 | continue; | |
39 | /* The base is a subdirectory of a path which was specified. */ | |
40 | return 1; | |
41 | } | |
42 | ||
43 | /* Does the base match? */ | |
44 | if (strncmp(base, match, baselen)) | |
45 | continue; | |
46 | ||
47 | match += baselen; | |
48 | matchlen -= baselen; | |
49 | ||
50 | if (pathlen > matchlen) | |
51 | continue; | |
52 | ||
53 | if (matchlen > pathlen) { | |
54 | if (match[pathlen] != '/') | |
55 | continue; | |
56 | if (!S_ISDIR(mode)) | |
57 | continue; | |
58 | } | |
59 | ||
60 | if (strncmp(path, match, pathlen)) | |
61 | continue; | |
3e587635 LT |
62 | |
63 | return 1; | |
0ca14a57 LT |
64 | } |
65 | return 0; | |
66 | } | |
67 | ||
94537c78 | 68 | static int read_tree_recursive(void *buffer, unsigned long size, |
0ca14a57 | 69 | const char *base, int baselen, |
3e587635 | 70 | int stage, const char **match) |
94537c78 LT |
71 | { |
72 | while (size) { | |
73 | int len = strlen(buffer)+1; | |
74 | unsigned char *sha1 = buffer + len; | |
75 | char *path = strchr(buffer, ' ')+1; | |
76 | unsigned int mode; | |
77 | ||
78 | if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1) | |
79 | return -1; | |
80 | ||
81 | buffer = sha1 + 20; | |
82 | size -= len + 20; | |
83 | ||
0ca14a57 LT |
84 | if (!match_tree_entry(base, baselen, path, mode, match)) |
85 | continue; | |
86 | ||
94537c78 LT |
87 | if (S_ISDIR(mode)) { |
88 | int retval; | |
89 | int pathlen = strlen(path); | |
1c9da46d | 90 | char *newbase; |
94537c78 LT |
91 | void *eltbuf; |
92 | char elttype[20]; | |
93 | unsigned long eltsize; | |
94 | ||
95 | eltbuf = read_sha1_file(sha1, elttype, &eltsize); | |
1c9da46d JF |
96 | if (!eltbuf || strcmp(elttype, "tree")) { |
97 | if (eltbuf) free(eltbuf); | |
94537c78 | 98 | return -1; |
1c9da46d JF |
99 | } |
100 | newbase = xmalloc(baselen + 1 + pathlen); | |
94537c78 LT |
101 | memcpy(newbase, base, baselen); |
102 | memcpy(newbase + baselen, path, pathlen); | |
103 | newbase[baselen + pathlen] = '/'; | |
104 | retval = read_tree_recursive(eltbuf, eltsize, | |
105 | newbase, | |
0ca14a57 LT |
106 | baselen + pathlen + 1, |
107 | stage, match); | |
94537c78 LT |
108 | free(eltbuf); |
109 | free(newbase); | |
110 | if (retval) | |
111 | return -1; | |
112 | continue; | |
113 | } | |
114 | if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0) | |
115 | return -1; | |
116 | } | |
117 | return 0; | |
118 | } | |
119 | ||
3e587635 | 120 | int read_tree(void *buffer, unsigned long size, int stage, const char **match) |
94537c78 | 121 | { |
0ca14a57 | 122 | return read_tree_recursive(buffer, size, "", 0, stage, match); |
94537c78 LT |
123 | } |
124 | ||
5d6ccf5c | 125 | struct tree *lookup_tree(const unsigned char *sha1) |
175785e5 DB |
126 | { |
127 | struct object *obj = lookup_object(sha1); | |
128 | if (!obj) { | |
812666c8 | 129 | struct tree *ret = xmalloc(sizeof(struct tree)); |
175785e5 DB |
130 | memset(ret, 0, sizeof(struct tree)); |
131 | created_object(sha1, &ret->object); | |
d32987be | 132 | ret->object.type = tree_type; |
175785e5 DB |
133 | return ret; |
134 | } | |
d1af002d NP |
135 | if (!obj->type) |
136 | obj->type = tree_type; | |
c35dfe85 | 137 | if (obj->type != tree_type) { |
175785e5 DB |
138 | error("Object %s is a %s, not a tree", |
139 | sha1_to_hex(sha1), obj->type); | |
140 | return NULL; | |
141 | } | |
142 | return (struct tree *) obj; | |
143 | } | |
144 | ||
bd2c39f5 | 145 | int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size) |
175785e5 | 146 | { |
bd2c39f5 | 147 | void *bufptr = buffer; |
08692164 | 148 | struct tree_entry_list **list_p; |
bd2c39f5 | 149 | |
175785e5 DB |
150 | if (item->object.parsed) |
151 | return 0; | |
152 | item->object.parsed = 1; | |
08692164 | 153 | list_p = &item->entries; |
175785e5 DB |
154 | while (size) { |
155 | struct object *obj; | |
08692164 | 156 | struct tree_entry_list *entry; |
175785e5 DB |
157 | int len = 1+strlen(bufptr); |
158 | unsigned char *file_sha1 = bufptr + len; | |
159 | char *path = strchr(bufptr, ' '); | |
160 | unsigned int mode; | |
161 | if (size < len + 20 || !path || | |
bd2c39f5 | 162 | sscanf(bufptr, "%o", &mode) != 1) |
175785e5 DB |
163 | return -1; |
164 | ||
812666c8 | 165 | entry = xmalloc(sizeof(struct tree_entry_list)); |
08692164 | 166 | entry->name = strdup(path + 1); |
42ea9cb2 LT |
167 | entry->directory = S_ISDIR(mode) != 0; |
168 | entry->executable = (mode & S_IXUSR) != 0; | |
169 | entry->symlink = S_ISLNK(mode) != 0; | |
64071805 | 170 | entry->zeropad = *(char *)bufptr == '0'; |
42ea9cb2 | 171 | entry->mode = mode; |
08692164 DB |
172 | entry->next = NULL; |
173 | ||
175785e5 DB |
174 | bufptr += len + 20; |
175 | size -= len + 20; | |
176 | ||
08692164 DB |
177 | if (entry->directory) { |
178 | entry->item.tree = lookup_tree(file_sha1); | |
179 | obj = &entry->item.tree->object; | |
175785e5 | 180 | } else { |
08692164 DB |
181 | entry->item.blob = lookup_blob(file_sha1); |
182 | obj = &entry->item.blob->object; | |
175785e5 | 183 | } |
235ac407 LT |
184 | if (obj) |
185 | add_ref(&item->object, obj); | |
6af1f019 | 186 | entry->parent = NULL; /* needs to be filled by the user */ |
08692164 DB |
187 | *list_p = entry; |
188 | list_p = &entry->next; | |
175785e5 DB |
189 | } |
190 | return 0; | |
191 | } | |
bd2c39f5 NP |
192 | |
193 | int parse_tree(struct tree *item) | |
194 | { | |
195 | char type[20]; | |
196 | void *buffer; | |
197 | unsigned long size; | |
198 | int ret; | |
199 | ||
200 | if (item->object.parsed) | |
201 | return 0; | |
202 | buffer = read_sha1_file(item->object.sha1, type, &size); | |
203 | if (!buffer) | |
204 | return error("Could not read %s", | |
205 | sha1_to_hex(item->object.sha1)); | |
206 | if (strcmp(type, tree_type)) { | |
207 | free(buffer); | |
208 | return error("Object %s not a tree", | |
209 | sha1_to_hex(item->object.sha1)); | |
210 | } | |
211 | ret = parse_tree_buffer(item, buffer, size); | |
212 | free(buffer); | |
213 | return ret; | |
214 | } |