[PATCH] Pull misc objects
[git/dkf.git] / read-cache.c
blob9b6ce46b9fa57e85b5c9e6bf552dc27f40f6e5c5
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
8 struct cache_entry **active_cache = NULL;
9 unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0;
12 * This only updates the "non-critical" parts of the directory
13 * cache, ie the parts that aren't tracked by GIT, and only used
14 * to validate the cache.
16 void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
18 ce->ce_ctime.sec = htonl(st->st_ctime);
19 ce->ce_mtime.sec = htonl(st->st_mtime);
20 #ifdef USE_NSEC
21 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
22 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
23 #endif
24 ce->ce_dev = htonl(st->st_dev);
25 ce->ce_ino = htonl(st->st_ino);
26 ce->ce_uid = htonl(st->st_uid);
27 ce->ce_gid = htonl(st->st_gid);
28 ce->ce_size = htonl(st->st_size);
31 int ce_match_stat(struct cache_entry *ce, struct stat *st)
33 unsigned int changed = 0;
35 switch (ntohl(ce->ce_mode) & S_IFMT) {
36 case S_IFREG:
37 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
38 /* We consider only the owner x bit to be relevant for "mode changes" */
39 if (0100 & (ntohl(ce->ce_mode) ^ st->st_mode))
40 changed |= MODE_CHANGED;
41 break;
42 case S_IFLNK:
43 changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0;
44 break;
45 default:
46 die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
48 if (ce->ce_mtime.sec != htonl(st->st_mtime))
49 changed |= MTIME_CHANGED;
50 if (ce->ce_ctime.sec != htonl(st->st_ctime))
51 changed |= CTIME_CHANGED;
53 #ifdef USE_NSEC
55 * nsec seems unreliable - not all filesystems support it, so
56 * as long as it is in the inode cache you get right nsec
57 * but after it gets flushed, you get zero nsec.
59 if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec))
60 changed |= MTIME_CHANGED;
61 if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec))
62 changed |= CTIME_CHANGED;
63 #endif
65 if (ce->ce_uid != htonl(st->st_uid) ||
66 ce->ce_gid != htonl(st->st_gid))
67 changed |= OWNER_CHANGED;
68 if (ce->ce_ino != htonl(st->st_ino))
69 changed |= INODE_CHANGED;
71 #ifdef USE_STDEV
73 * st_dev breaks on network filesystems where different
74 * clients will have different views of what "device"
75 * the filesystem is on
77 if (ce->ce_dev != htonl(st->st_dev))
78 changed |= INODE_CHANGED;
79 #endif
81 if (ce->ce_size != htonl(st->st_size))
82 changed |= DATA_CHANGED;
83 return changed;
86 int base_name_compare(const char *name1, int len1, int mode1,
87 const char *name2, int len2, int mode2)
89 unsigned char c1, c2;
90 int len = len1 < len2 ? len1 : len2;
91 int cmp;
93 cmp = memcmp(name1, name2, len);
94 if (cmp)
95 return cmp;
96 c1 = name1[len];
97 c2 = name2[len];
98 if (!c1 && S_ISDIR(mode1))
99 c1 = '/';
100 if (!c2 && S_ISDIR(mode2))
101 c2 = '/';
102 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
105 int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
107 int len1 = flags1 & CE_NAMEMASK;
108 int len2 = flags2 & CE_NAMEMASK;
109 int len = len1 < len2 ? len1 : len2;
110 int cmp;
112 cmp = memcmp(name1, name2, len);
113 if (cmp)
114 return cmp;
115 if (len1 < len2)
116 return -1;
117 if (len1 > len2)
118 return 1;
119 if (flags1 < flags2)
120 return -1;
121 if (flags1 > flags2)
122 return 1;
123 return 0;
126 int cache_name_pos(const char *name, int namelen)
128 int first, last;
130 first = 0;
131 last = active_nr;
132 while (last > first) {
133 int next = (last + first) >> 1;
134 struct cache_entry *ce = active_cache[next];
135 int cmp = cache_name_compare(name, namelen, ce->name, ntohs(ce->ce_flags));
136 if (!cmp)
137 return next;
138 if (cmp < 0) {
139 last = next;
140 continue;
142 first = next+1;
144 return -first-1;
147 /* Remove entry, return true if there are more entries to go.. */
148 int remove_cache_entry_at(int pos)
150 active_cache_changed = 1;
151 active_nr--;
152 if (pos >= active_nr)
153 return 0;
154 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
155 return 1;
158 int remove_file_from_cache(char *path)
160 int pos = cache_name_pos(path, strlen(path));
161 if (pos < 0)
162 pos = -pos-1;
163 while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
164 remove_cache_entry_at(pos);
165 return 0;
168 int ce_same_name(struct cache_entry *a, struct cache_entry *b)
170 int len = ce_namelen(a);
171 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
175 * Do we have another file that has the beginning components being a
176 * proper superset of the name we're trying to add?
178 static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replace)
180 int retval = 0;
181 int len = ce_namelen(ce);
182 const char *name = ce->name;
184 while (pos < active_nr) {
185 struct cache_entry *p = active_cache[pos++];
187 if (len >= ce_namelen(p))
188 break;
189 if (memcmp(name, p->name, len))
190 break;
191 if (p->name[len] != '/')
192 continue;
193 retval = -1;
194 if (!ok_to_replace)
195 break;
196 remove_cache_entry_at(--pos);
198 return retval;
202 * Do we have another file with a pathname that is a proper
203 * subset of the name we're trying to add?
205 static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace)
207 int retval = 0;
208 const char *name = ce->name;
209 const char *slash = name + ce_namelen(ce);
211 for (;;) {
212 int len;
214 for (;;) {
215 if (*--slash == '/')
216 break;
217 if (slash <= ce->name)
218 return retval;
220 len = slash - name;
222 pos = cache_name_pos(name, len);
223 if (pos >= 0) {
224 retval = -1;
225 if (ok_to_replace)
226 break;
227 remove_cache_entry_at(pos);
228 continue;
232 * Trivial optimization: if we find an entry that
233 * already matches the sub-directory, then we know
234 * we're ok, and we can exit
236 pos = -pos-1;
237 if (pos < active_nr) {
238 struct cache_entry *p = active_cache[pos];
239 if (ce_namelen(p) <= len)
240 continue;
241 if (p->name[len] != '/')
242 continue;
243 if (memcmp(p->name, name, len))
244 continue;
245 break;
248 return retval;
251 /* We may be in a situation where we already have path/file and path
252 * is being added, or we already have path and path/file is being
253 * added. Either one would result in a nonsense tree that has path
254 * twice when git-write-tree tries to write it out. Prevent it.
256 * If ok-to-replace is specified, we remove the conflicting entries
257 * from the cache so the caller should recompute the insert position.
258 * When this happens, we return non-zero.
260 static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace)
263 * We check if the path is a sub-path of a subsequent pathname
264 * first, since removing those will not change the position
265 * in the array
267 int retval = has_file_name(ce, pos, ok_to_replace);
269 * Then check if the path might have a clashing sub-directory
270 * before it.
272 return retval + has_dir_name(ce, pos, ok_to_replace);
275 int add_cache_entry(struct cache_entry *ce, int option)
277 int pos;
278 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
279 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
280 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
282 /* existing match? Just replace it */
283 if (pos >= 0) {
284 active_cache_changed = 1;
285 active_cache[pos] = ce;
286 return 0;
288 pos = -pos-1;
291 * Inserting a merged entry ("stage 0") into the index
292 * will always replace all non-merged entries..
294 if (pos < active_nr && ce_stage(ce) == 0) {
295 while (ce_same_name(active_cache[pos], ce)) {
296 ok_to_add = 1;
297 if (!remove_cache_entry_at(pos))
298 break;
302 if (!ok_to_add)
303 return -1;
305 if (!ce_stage(ce) && check_file_directory_conflict(ce, pos, ok_to_replace)) {
306 if (!ok_to_replace)
307 return -1;
308 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
309 pos = -pos-1;
312 /* Make sure the array is big enough .. */
313 if (active_nr == active_alloc) {
314 active_alloc = alloc_nr(active_alloc);
315 active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
318 /* Add it in.. */
319 active_nr++;
320 if (active_nr > pos)
321 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
322 active_cache[pos] = ce;
323 active_cache_changed = 1;
324 return 0;
327 static int verify_hdr(struct cache_header *hdr, unsigned long size)
329 SHA_CTX c;
330 unsigned char sha1[20];
332 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
333 return error("bad signature");
334 if (hdr->hdr_version != htonl(2))
335 return error("bad index version");
336 SHA1_Init(&c);
337 SHA1_Update(&c, hdr, size - 20);
338 SHA1_Final(sha1, &c);
339 if (memcmp(sha1, (void *)hdr + size - 20, 20))
340 return error("bad index file sha1 signature");
341 return 0;
344 int read_cache(void)
346 int fd, i;
347 struct stat st;
348 unsigned long size, offset;
349 void *map;
350 struct cache_header *hdr;
352 errno = EBUSY;
353 if (active_cache)
354 return error("more than one cachefile");
355 errno = ENOENT;
356 fd = open(get_index_file(), O_RDONLY);
357 if (fd < 0)
358 return (errno == ENOENT) ? 0 : error("open failed");
360 size = 0; // avoid gcc warning
361 map = (void *)-1;
362 if (!fstat(fd, &st)) {
363 size = st.st_size;
364 errno = EINVAL;
365 if (size >= sizeof(struct cache_header) + 20)
366 map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
368 close(fd);
369 if (-1 == (int)(long)map)
370 return error("mmap failed");
372 hdr = map;
373 if (verify_hdr(hdr, size) < 0)
374 goto unmap;
376 active_nr = ntohl(hdr->hdr_entries);
377 active_alloc = alloc_nr(active_nr);
378 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
380 offset = sizeof(*hdr);
381 for (i = 0; i < active_nr; i++) {
382 struct cache_entry *ce = map + offset;
383 offset = offset + ce_size(ce);
384 active_cache[i] = ce;
386 return active_nr;
388 unmap:
389 munmap(map, size);
390 errno = EINVAL;
391 return error("verify header failed");
394 #define WRITE_BUFFER_SIZE 8192
395 static unsigned char write_buffer[WRITE_BUFFER_SIZE];
396 static unsigned long write_buffer_len;
398 static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
400 while (len) {
401 unsigned int buffered = write_buffer_len;
402 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
403 if (partial > len)
404 partial = len;
405 memcpy(write_buffer + buffered, data, partial);
406 buffered += partial;
407 if (buffered == WRITE_BUFFER_SIZE) {
408 SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE);
409 if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE)
410 return -1;
411 buffered = 0;
413 write_buffer_len = buffered;
414 len -= partial;
415 data += partial;
417 return 0;
420 static int ce_flush(SHA_CTX *context, int fd)
422 unsigned int left = write_buffer_len;
424 if (left) {
425 write_buffer_len = 0;
426 SHA1_Update(context, write_buffer, left);
429 /* Append the SHA1 signature at the end */
430 SHA1_Final(write_buffer + left, context);
431 left += 20;
432 if (write(fd, write_buffer, left) != left)
433 return -1;
434 return 0;
437 int write_cache(int newfd, struct cache_entry **cache, int entries)
439 SHA_CTX c;
440 struct cache_header hdr;
441 int i, removed;
443 for (i = removed = 0; i < entries; i++)
444 if (!cache[i]->ce_mode)
445 removed++;
447 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
448 hdr.hdr_version = htonl(2);
449 hdr.hdr_entries = htonl(entries - removed);
451 SHA1_Init(&c);
452 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
453 return -1;
455 for (i = 0; i < entries; i++) {
456 struct cache_entry *ce = cache[i];
457 if (!ce->ce_mode)
458 continue;
459 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
460 return -1;
462 return ce_flush(&c, newfd);