[PATCH] read_tree_recursive(): Fix leaks
[git/repo.git] / read-cache.c
blobda2adf4612c3a1cb39fbb76e303302e9b7e1356b
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include <stdarg.h>
7 #include "cache.h"
9 struct cache_entry **active_cache = NULL;
10 unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0;
12 int cache_match_stat(struct cache_entry *ce, struct stat *st)
14 unsigned int changed = 0;
16 switch (ntohl(ce->ce_mode) & S_IFMT) {
17 case S_IFREG:
18 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
19 /* We consider only the owner x bit to be relevant for "mode changes" */
20 if (0100 & (ntohl(ce->ce_mode) ^ st->st_mode))
21 changed |= MODE_CHANGED;
22 break;
23 case S_IFLNK:
24 changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0;
25 break;
26 default:
27 die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
29 if (ce->ce_mtime.sec != htonl(st->st_mtime))
30 changed |= MTIME_CHANGED;
31 if (ce->ce_ctime.sec != htonl(st->st_ctime))
32 changed |= CTIME_CHANGED;
34 #ifdef NSEC
36 * nsec seems unreliable - not all filesystems support it, so
37 * as long as it is in the inode cache you get right nsec
38 * but after it gets flushed, you get zero nsec.
40 if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec))
41 changed |= MTIME_CHANGED;
42 if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec))
43 changed |= CTIME_CHANGED;
44 #endif
46 if (ce->ce_uid != htonl(st->st_uid) ||
47 ce->ce_gid != htonl(st->st_gid))
48 changed |= OWNER_CHANGED;
49 if (ce->ce_dev != htonl(st->st_dev) ||
50 ce->ce_ino != htonl(st->st_ino))
51 changed |= INODE_CHANGED;
52 if (ce->ce_size != htonl(st->st_size))
53 changed |= DATA_CHANGED;
54 return changed;
57 int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
59 int len1 = flags1 & CE_NAMEMASK;
60 int len2 = flags2 & CE_NAMEMASK;
61 int len = len1 < len2 ? len1 : len2;
62 int cmp;
64 cmp = memcmp(name1, name2, len);
65 if (cmp)
66 return cmp;
67 if (len1 < len2)
68 return -1;
69 if (len1 > len2)
70 return 1;
71 if (flags1 < flags2)
72 return -1;
73 if (flags1 > flags2)
74 return 1;
75 return 0;
78 int cache_name_pos(const char *name, int namelen)
80 int first, last;
82 first = 0;
83 last = active_nr;
84 while (last > first) {
85 int next = (last + first) >> 1;
86 struct cache_entry *ce = active_cache[next];
87 int cmp = cache_name_compare(name, namelen, ce->name, htons(ce->ce_flags));
88 if (!cmp)
89 return next;
90 if (cmp < 0) {
91 last = next;
92 continue;
94 first = next+1;
96 return -first-1;
99 /* Remove entry, return true if there are more entries to go.. */
100 int remove_entry_at(int pos)
102 active_cache_changed = 1;
103 active_nr--;
104 if (pos >= active_nr)
105 return 0;
106 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
107 return 1;
110 int remove_file_from_cache(char *path)
112 int pos = cache_name_pos(path, strlen(path));
113 if (pos < 0)
114 pos = -pos-1;
115 while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
116 remove_entry_at(pos);
117 return 0;
120 int same_name(struct cache_entry *a, struct cache_entry *b)
122 int len = ce_namelen(a);
123 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
126 /* We may be in a situation where we already have path/file and path
127 * is being added, or we already have path and path/file is being
128 * added. Either one would result in a nonsense tree that has path
129 * twice when git-write-tree tries to write it out. Prevent it.
131 * If ok-to-replace is specified, we remove the conflicting entries
132 * from the cache so the caller should recompute the insert position.
133 * When this happens, we return non-zero.
135 static int check_file_directory_conflict(const struct cache_entry *ce,
136 int ok_to_replace)
138 int pos, replaced = 0;
139 const char *path = ce->name;
140 int namelen = strlen(path);
141 int stage = ce_stage(ce);
142 char *pathbuf = xmalloc(namelen + 1);
143 char *cp;
145 memcpy(pathbuf, path, namelen + 1);
148 * We are inserting path/file. Do they have path registered at
149 * the same stage? We need to do this for all the levels of our
150 * subpath.
152 cp = pathbuf;
153 while (1) {
154 char *ep = strchr(cp, '/');
155 if (!ep)
156 break;
157 *ep = 0; /* first cut it at slash */
158 pos = cache_name_pos(pathbuf,
159 htons(create_ce_flags(ep-cp, stage)));
160 if (0 <= pos) {
161 /* Our leading path component is registered as a file,
162 * and we are trying to make it a directory. This is
163 * bad.
165 if (!ok_to_replace) {
166 free(pathbuf);
167 return -1;
169 fprintf(stderr, "removing file '%s' to replace it with a directory to create '%s'.\n", pathbuf, path);
170 remove_entry_at(pos);
171 replaced = 1;
173 *ep = '/'; /* then restore it and go downwards */
174 cp = ep + 1;
176 free(pathbuf);
178 /* Do we have an entry in the cache that makes our path a prefix
179 * of it? That is, are we creating a file where they already expect
180 * a directory there?
182 pos = cache_name_pos(path,
183 htons(create_ce_flags(namelen, stage)));
185 /* (0 <= pos) cannot happen because add_cache_entry()
186 * should have taken care of that case.
188 pos = -pos-1;
190 /* pos would point at an existing entry that would come immediately
191 * after our path. It could be the same as our path in higher stage,
192 * or different path but in a lower stage.
194 * E.g. when we are inserting path at stage 2,
196 * 1 path
197 * pos-> 3 path
198 * 2 path/file1
199 * 3 path/file1
200 * 2 path/file2
201 * 2 patho
203 * We need to examine pos, ignore it because it is at different
204 * stage, examine next to find the path/file at stage 2, and
205 * complain. We need to do this until we are not the leading
206 * path of an existing entry anymore.
209 while (pos < active_nr) {
210 struct cache_entry *other = active_cache[pos];
211 if (strncmp(other->name, path, namelen))
212 break; /* it is not our "subdirectory" anymore */
213 if ((ce_stage(other) == stage) &&
214 other->name[namelen] == '/') {
215 if (!ok_to_replace)
216 return -1;
217 fprintf(stderr, "removing file '%s' under '%s' to be replaced with a file\n", other->name, path);
218 remove_entry_at(pos);
219 replaced = 1;
220 continue; /* cycle without updating pos */
222 pos++;
224 return replaced;
227 int add_cache_entry(struct cache_entry *ce, int option)
229 int pos;
230 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
231 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
232 pos = cache_name_pos(ce->name, htons(ce->ce_flags));
234 /* existing match? Just replace it */
235 if (pos >= 0) {
236 active_cache_changed = 1;
237 active_cache[pos] = ce;
238 return 0;
240 pos = -pos-1;
243 * Inserting a merged entry ("stage 0") into the index
244 * will always replace all non-merged entries..
246 if (pos < active_nr && ce_stage(ce) == 0) {
247 while (same_name(active_cache[pos], ce)) {
248 ok_to_add = 1;
249 if (!remove_entry_at(pos))
250 break;
254 if (!ok_to_add)
255 return -1;
257 if (check_file_directory_conflict(ce, ok_to_replace)) {
258 if (!ok_to_replace)
259 return -1;
260 pos = cache_name_pos(ce->name, htons(ce->ce_flags));
261 pos = -pos-1;
264 /* Make sure the array is big enough .. */
265 if (active_nr == active_alloc) {
266 active_alloc = alloc_nr(active_alloc);
267 active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
270 /* Add it in.. */
271 active_nr++;
272 if (active_nr > pos)
273 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
274 active_cache[pos] = ce;
275 active_cache_changed = 1;
276 return 0;
279 static int verify_hdr(struct cache_header *hdr, unsigned long size)
281 SHA_CTX c;
282 unsigned char sha1[20];
284 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
285 return error("bad signature");
286 if (hdr->hdr_version != htonl(2))
287 return error("bad index version");
288 SHA1_Init(&c);
289 SHA1_Update(&c, hdr, size - 20);
290 SHA1_Final(sha1, &c);
291 if (memcmp(sha1, (void *)hdr + size - 20, 20))
292 return error("bad index file sha1 signature");
293 return 0;
296 int read_cache(void)
298 int fd, i;
299 struct stat st;
300 unsigned long size, offset;
301 void *map;
302 struct cache_header *hdr;
304 errno = EBUSY;
305 if (active_cache)
306 return error("more than one cachefile");
307 errno = ENOENT;
308 fd = open(get_index_file(), O_RDONLY);
309 if (fd < 0)
310 return (errno == ENOENT) ? 0 : error("open failed");
312 size = 0; // avoid gcc warning
313 map = (void *)-1;
314 if (!fstat(fd, &st)) {
315 size = st.st_size;
316 errno = EINVAL;
317 if (size >= sizeof(struct cache_header) + 20)
318 map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
320 close(fd);
321 if (-1 == (int)(long)map)
322 return error("mmap failed");
324 hdr = map;
325 if (verify_hdr(hdr, size) < 0)
326 goto unmap;
328 active_nr = ntohl(hdr->hdr_entries);
329 active_alloc = alloc_nr(active_nr);
330 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
332 offset = sizeof(*hdr);
333 for (i = 0; i < active_nr; i++) {
334 struct cache_entry *ce = map + offset;
335 offset = offset + ce_size(ce);
336 active_cache[i] = ce;
338 return active_nr;
340 unmap:
341 munmap(map, size);
342 errno = EINVAL;
343 return error("verify header failed");
346 #define WRITE_BUFFER_SIZE 8192
347 static char write_buffer[WRITE_BUFFER_SIZE];
348 static unsigned long write_buffer_len;
350 static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
352 while (len) {
353 unsigned int buffered = write_buffer_len;
354 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
355 if (partial > len)
356 partial = len;
357 memcpy(write_buffer + buffered, data, partial);
358 buffered += partial;
359 if (buffered == WRITE_BUFFER_SIZE) {
360 SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE);
361 if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE)
362 return -1;
363 buffered = 0;
365 write_buffer_len = buffered;
366 len -= partial;
367 data += partial;
369 return 0;
372 static int ce_flush(SHA_CTX *context, int fd)
374 unsigned int left = write_buffer_len;
376 if (left) {
377 write_buffer_len = 0;
378 SHA1_Update(context, write_buffer, left);
381 /* Append the SHA1 signature at the end */
382 SHA1_Final(write_buffer + left, context);
383 left += 20;
384 if (write(fd, write_buffer, left) != left)
385 return -1;
386 return 0;
389 int write_cache(int newfd, struct cache_entry **cache, int entries)
391 SHA_CTX c;
392 struct cache_header hdr;
393 int i;
395 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
396 hdr.hdr_version = htonl(2);
397 hdr.hdr_entries = htonl(entries);
399 SHA1_Init(&c);
400 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
401 return -1;
403 for (i = 0; i < entries; i++) {
404 struct cache_entry *ce = cache[i];
405 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
406 return -1;
408 return ce_flush(&c, newfd);