fsck-cache: fix SIGSEGV on bad tag object
[git/git.git] / read-cache.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
2de381f9 6#include <stdarg.h>
e83c5163
LT
7#include "cache.h"
8
e83c5163
LT
9struct cache_entry **active_cache = NULL;
10unsigned int active_nr = 0, active_alloc = 0;
11
734aab75
LT
12int cache_match_stat(struct cache_entry *ce, struct stat *st)
13{
14 unsigned int changed = 0;
15
ccc4feb5 16 if (ce->ce_mtime.sec != htonl(st->st_mtime))
734aab75 17 changed |= MTIME_CHANGED;
ccc4feb5
LT
18 if (ce->ce_ctime.sec != htonl(st->st_ctime))
19 changed |= CTIME_CHANGED;
20
bdd4da59 21#ifdef NSEC
ccc4feb5
LT
22 /*
23 * nsec seems unreliable - not all filesystems support it, so
24 * as long as it is in the inode cache you get right nsec
25 * but after it gets flushed, you get zero nsec.
26 */
94dfb7f2 27 if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec))
ccc4feb5 28 changed |= MTIME_CHANGED;
94dfb7f2 29 if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec))
734aab75 30 changed |= CTIME_CHANGED;
ccc4feb5
LT
31#endif
32
33 if (ce->ce_uid != htonl(st->st_uid) ||
34 ce->ce_gid != htonl(st->st_gid))
734aab75 35 changed |= OWNER_CHANGED;
e4479470 36 /* We consider only the owner x bit to be relevant for "mode changes" */
e8871e88 37 if (0100 & (ntohl(ce->ce_mode) ^ st->st_mode))
734aab75 38 changed |= MODE_CHANGED;
ccc4feb5
LT
39 if (ce->ce_dev != htonl(st->st_dev) ||
40 ce->ce_ino != htonl(st->st_ino))
734aab75 41 changed |= INODE_CHANGED;
ccc4feb5 42 if (ce->ce_size != htonl(st->st_size))
734aab75
LT
43 changed |= DATA_CHANGED;
44 return changed;
45}
46
95fd5bf8 47int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
eb38c22f 48{
95fd5bf8
LT
49 int len1 = flags1 & CE_NAMEMASK;
50 int len2 = flags2 & CE_NAMEMASK;
eb38c22f
LT
51 int len = len1 < len2 ? len1 : len2;
52 int cmp;
53
54 cmp = memcmp(name1, name2, len);
55 if (cmp)
56 return cmp;
57 if (len1 < len2)
58 return -1;
59 if (len1 > len2)
60 return 1;
95fd5bf8
LT
61 if (flags1 < flags2)
62 return -1;
63 if (flags1 > flags2)
64 return 1;
eb38c22f
LT
65 return 0;
66}
67
68int cache_name_pos(const char *name, int namelen)
69{
70 int first, last;
71
72 first = 0;
73 last = active_nr;
74 while (last > first) {
75 int next = (last + first) >> 1;
76 struct cache_entry *ce = active_cache[next];
95fd5bf8 77 int cmp = cache_name_compare(name, namelen, ce->name, htons(ce->ce_flags));
eb38c22f 78 if (!cmp)
76e7f4ec 79 return next;
eb38c22f
LT
80 if (cmp < 0) {
81 last = next;
82 continue;
83 }
84 first = next+1;
85 }
76e7f4ec 86 return -first-1;
eb38c22f
LT
87}
88
7b937ca3 89/* Remove entry, return true if there are more entries to go.. */
b5af9107 90int remove_entry_at(int pos)
7b937ca3
LT
91{
92 active_nr--;
93 if (pos >= active_nr)
94 return 0;
95 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
96 return 1;
97}
98
197ee8c9
LT
99int remove_file_from_cache(char *path)
100{
101 int pos = cache_name_pos(path, strlen(path));
c4e3cca1
JH
102 if (pos < 0)
103 pos = -pos-1;
104 while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
7b937ca3 105 remove_entry_at(pos);
197ee8c9
LT
106 return 0;
107}
108
b5af9107 109int same_name(struct cache_entry *a, struct cache_entry *b)
7b937ca3
LT
110{
111 int len = ce_namelen(a);
112 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
113}
114
121481ab 115int add_cache_entry(struct cache_entry *ce, int ok_to_add)
197ee8c9
LT
116{
117 int pos;
118
95fd5bf8 119 pos = cache_name_pos(ce->name, htons(ce->ce_flags));
197ee8c9
LT
120
121 /* existing match? Just replace it */
76e7f4ec
LT
122 if (pos >= 0) {
123 active_cache[pos] = ce;
197ee8c9
LT
124 return 0;
125 }
76e7f4ec 126 pos = -pos-1;
197ee8c9 127
7b937ca3
LT
128 /*
129 * Inserting a merged entry ("stage 0") into the index
130 * will always replace all non-merged entries..
131 */
132 if (pos < active_nr && ce_stage(ce) == 0) {
133 while (same_name(active_cache[pos], ce)) {
134 ok_to_add = 1;
7b937ca3
LT
135 if (!remove_entry_at(pos))
136 break;
137 }
138 }
139
121481ab
LT
140 if (!ok_to_add)
141 return -1;
142
197ee8c9
LT
143 /* Make sure the array is big enough .. */
144 if (active_nr == active_alloc) {
145 active_alloc = alloc_nr(active_alloc);
812666c8 146 active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
197ee8c9
LT
147 }
148
149 /* Add it in.. */
150 active_nr++;
151 if (active_nr > pos)
152 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
153 active_cache[pos] = ce;
154 return 0;
155}
156
e83c5163
LT
157static int verify_hdr(struct cache_header *hdr, unsigned long size)
158{
159 SHA_CTX c;
160 unsigned char sha1[20];
161
ccc4feb5 162 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
e83c5163 163 return error("bad signature");
ca9be054
LT
164 if (hdr->hdr_version != htonl(2))
165 return error("bad index version");
e83c5163 166 SHA1_Init(&c);
ca9be054 167 SHA1_Update(&c, hdr, size - 20);
e83c5163 168 SHA1_Final(sha1, &c);
ca9be054
LT
169 if (memcmp(sha1, (void *)hdr + size - 20, 20))
170 return error("bad index file sha1 signature");
e83c5163
LT
171 return 0;
172}
173
174int read_cache(void)
175{
176 int fd, i;
177 struct stat st;
178 unsigned long size, offset;
179 void *map;
180 struct cache_header *hdr;
181
182 errno = EBUSY;
183 if (active_cache)
184 return error("more than one cachefile");
185 errno = ENOENT;
186 sha1_file_directory = getenv(DB_ENVIRONMENT);
187 if (!sha1_file_directory)
188 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
189 if (access(sha1_file_directory, X_OK) < 0)
190 return error("no access to SHA1 file directory");
bb233d69 191 fd = open(get_index_file(), O_RDONLY);
e83c5163
LT
192 if (fd < 0)
193 return (errno == ENOENT) ? 0 : error("open failed");
194
19b2860c 195 size = 0; // avoid gcc warning
e83c5163
LT
196 map = (void *)-1;
197 if (!fstat(fd, &st)) {
e83c5163
LT
198 size = st.st_size;
199 errno = EINVAL;
ca9be054 200 if (size >= sizeof(struct cache_header) + 20)
520fc241 201 map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
e83c5163
LT
202 }
203 close(fd);
204 if (-1 == (int)(long)map)
205 return error("mmap failed");
206
207 hdr = map;
208 if (verify_hdr(hdr, size) < 0)
209 goto unmap;
210
ccc4feb5 211 active_nr = ntohl(hdr->hdr_entries);
e83c5163
LT
212 active_alloc = alloc_nr(active_nr);
213 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
214
215 offset = sizeof(*hdr);
ccc4feb5 216 for (i = 0; i < active_nr; i++) {
e83c5163
LT
217 struct cache_entry *ce = map + offset;
218 offset = offset + ce_size(ce);
219 active_cache[i] = ce;
220 }
221 return active_nr;
222
223unmap:
224 munmap(map, size);
225 errno = EINVAL;
226 return error("verify header failed");
227}
228
4990aadc
LT
229#define WRITE_BUFFER_SIZE 8192
230static char write_buffer[WRITE_BUFFER_SIZE];
231static unsigned long write_buffer_len;
232
ca9be054 233static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
4990aadc
LT
234{
235 while (len) {
236 unsigned int buffered = write_buffer_len;
237 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
238 if (partial > len)
239 partial = len;
240 memcpy(write_buffer + buffered, data, partial);
241 buffered += partial;
242 if (buffered == WRITE_BUFFER_SIZE) {
ca9be054 243 SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE);
4990aadc
LT
244 if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE)
245 return -1;
246 buffered = 0;
247 }
248 write_buffer_len = buffered;
249 len -= partial;
250 data += partial;
251 }
252 return 0;
253}
254
ca9be054 255static int ce_flush(SHA_CTX *context, int fd)
4990aadc
LT
256{
257 unsigned int left = write_buffer_len;
ca9be054 258
4990aadc
LT
259 if (left) {
260 write_buffer_len = 0;
ca9be054 261 SHA1_Update(context, write_buffer, left);
4990aadc 262 }
ca9be054
LT
263
264 /* Append the SHA1 signature at the end */
265 SHA1_Final(write_buffer + left, context);
266 left += 20;
267 if (write(fd, write_buffer, left) != left)
268 return -1;
4990aadc
LT
269 return 0;
270}
271
197ee8c9
LT
272int write_cache(int newfd, struct cache_entry **cache, int entries)
273{
274 SHA_CTX c;
275 struct cache_header hdr;
276 int i;
277
ccc4feb5 278 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
ca9be054 279 hdr.hdr_version = htonl(2);
ccc4feb5 280 hdr.hdr_entries = htonl(entries);
197ee8c9
LT
281
282 SHA1_Init(&c);
ca9be054 283 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
197ee8c9
LT
284 return -1;
285
286 for (i = 0; i < entries; i++) {
287 struct cache_entry *ce = cache[i];
ca9be054 288 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
197ee8c9
LT
289 return -1;
290 }
ca9be054 291 return ce_flush(&c, newfd);
197ee8c9 292}