2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
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) {
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;
24 changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0;
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;
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;
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;
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;
64 cmp = memcmp(name1, name2, len);
78 int cache_name_pos(const char *name, int namelen)
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));
99 /* Remove entry, return true if there are more entries to go.. */
100 int remove_entry_at(int pos)
102 active_cache_changed = 1;
104 if (pos >= active_nr)
106 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
110 int remove_file_from_cache(char *path)
112 int pos = cache_name_pos(path, strlen(path));
115 while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
116 remove_entry_at(pos);
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 int add_cache_entry(struct cache_entry *ce, int ok_to_add)
130 pos = cache_name_pos(ce->name, htons(ce->ce_flags));
132 /* existing match? Just replace it */
134 active_cache_changed = 1;
135 active_cache[pos] = ce;
141 * Inserting a merged entry ("stage 0") into the index
142 * will always replace all non-merged entries..
144 if (pos < active_nr && ce_stage(ce) == 0) {
145 while (same_name(active_cache[pos], ce)) {
147 if (!remove_entry_at(pos))
155 /* Make sure the array is big enough .. */
156 if (active_nr == active_alloc) {
157 active_alloc = alloc_nr(active_alloc);
158 active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
164 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
165 active_cache[pos] = ce;
166 active_cache_changed = 1;
170 static int verify_hdr(struct cache_header *hdr, unsigned long size)
173 unsigned char sha1[20];
175 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
176 return error("bad signature");
177 if (hdr->hdr_version != htonl(2))
178 return error("bad index version");
180 SHA1_Update(&c, hdr, size - 20);
181 SHA1_Final(sha1, &c);
182 if (memcmp(sha1, (void *)hdr + size - 20, 20))
183 return error("bad index file sha1 signature");
191 unsigned long size, offset;
193 struct cache_header *hdr;
197 return error("more than one cachefile");
199 sha1_file_directory = getenv(DB_ENVIRONMENT);
200 if (!sha1_file_directory)
201 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
202 if (access(sha1_file_directory, X_OK) < 0)
203 return error("no access to SHA1 file directory");
204 fd = open(get_index_file(), O_RDONLY);
206 return (errno == ENOENT) ? 0 : error("open failed");
208 size = 0; // avoid gcc warning
210 if (!fstat(fd, &st)) {
213 if (size >= sizeof(struct cache_header) + 20)
214 map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
217 if (-1 == (int)(long)map)
218 return error("mmap failed");
221 if (verify_hdr(hdr, size) < 0)
224 active_nr = ntohl(hdr->hdr_entries);
225 active_alloc = alloc_nr(active_nr);
226 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
228 offset = sizeof(*hdr);
229 for (i = 0; i < active_nr; i++) {
230 struct cache_entry *ce = map + offset;
231 offset = offset + ce_size(ce);
232 active_cache[i] = ce;
239 return error("verify header failed");
242 #define WRITE_BUFFER_SIZE 8192
243 static char write_buffer[WRITE_BUFFER_SIZE];
244 static unsigned long write_buffer_len;
246 static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
249 unsigned int buffered = write_buffer_len;
250 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
253 memcpy(write_buffer + buffered, data, partial);
255 if (buffered == WRITE_BUFFER_SIZE) {
256 SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE);
257 if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE)
261 write_buffer_len = buffered;
268 static int ce_flush(SHA_CTX *context, int fd)
270 unsigned int left = write_buffer_len;
273 write_buffer_len = 0;
274 SHA1_Update(context, write_buffer, left);
277 /* Append the SHA1 signature at the end */
278 SHA1_Final(write_buffer + left, context);
280 if (write(fd, write_buffer, left) != left)
285 int write_cache(int newfd, struct cache_entry **cache, int entries)
288 struct cache_header hdr;
291 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
292 hdr.hdr_version = htonl(2);
293 hdr.hdr_entries = htonl(entries);
296 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
299 for (i = 0; i < entries; i++) {
300 struct cache_entry *ce = cache[i];
301 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
304 return ce_flush(&c, newfd);