[PATCH] Document -R option for git-diff-tree.
[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 9struct cache_entry **active_cache = NULL;
ee267527 10unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0;
e83c5163 11
415e96c8
JH
12/*
13 * This only updates the "non-critical" parts of the directory
14 * cache, ie the parts that aren't tracked by GIT, and only used
15 * to validate the cache.
16 */
17void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
18{
19 ce->ce_ctime.sec = htonl(st->st_ctime);
20 ce->ce_mtime.sec = htonl(st->st_mtime);
21#ifdef NSEC
22 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
23 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
24#endif
25 ce->ce_dev = htonl(st->st_dev);
26 ce->ce_ino = htonl(st->st_ino);
27 ce->ce_uid = htonl(st->st_uid);
28 ce->ce_gid = htonl(st->st_gid);
29 ce->ce_size = htonl(st->st_size);
30}
31
5d728c84 32int ce_match_stat(struct cache_entry *ce, struct stat *st)
734aab75
LT
33{
34 unsigned int changed = 0;
35
8ae0a8c5
KS
36 switch (ntohl(ce->ce_mode) & S_IFMT) {
37 case S_IFREG:
38 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
ffbe1add
KS
39 /* We consider only the owner x bit to be relevant for "mode changes" */
40 if (0100 & (ntohl(ce->ce_mode) ^ st->st_mode))
41 changed |= MODE_CHANGED;
8ae0a8c5
KS
42 break;
43 case S_IFLNK:
44 changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0;
45 break;
46 default:
47 die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
48 }
ccc4feb5 49 if (ce->ce_mtime.sec != htonl(st->st_mtime))
734aab75 50 changed |= MTIME_CHANGED;
ccc4feb5
LT
51 if (ce->ce_ctime.sec != htonl(st->st_ctime))
52 changed |= CTIME_CHANGED;
53
bdd4da59 54#ifdef NSEC
ccc4feb5
LT
55 /*
56 * nsec seems unreliable - not all filesystems support it, so
57 * as long as it is in the inode cache you get right nsec
58 * but after it gets flushed, you get zero nsec.
59 */
94dfb7f2 60 if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec))
ccc4feb5 61 changed |= MTIME_CHANGED;
94dfb7f2 62 if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec))
734aab75 63 changed |= CTIME_CHANGED;
ccc4feb5
LT
64#endif
65
66 if (ce->ce_uid != htonl(st->st_uid) ||
67 ce->ce_gid != htonl(st->st_gid))
734aab75 68 changed |= OWNER_CHANGED;
ccc4feb5
LT
69 if (ce->ce_dev != htonl(st->st_dev) ||
70 ce->ce_ino != htonl(st->st_ino))
734aab75 71 changed |= INODE_CHANGED;
ccc4feb5 72 if (ce->ce_size != htonl(st->st_size))
734aab75
LT
73 changed |= DATA_CHANGED;
74 return changed;
75}
76
95fd5bf8 77int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
eb38c22f 78{
95fd5bf8
LT
79 int len1 = flags1 & CE_NAMEMASK;
80 int len2 = flags2 & CE_NAMEMASK;
eb38c22f
LT
81 int len = len1 < len2 ? len1 : len2;
82 int cmp;
83
84 cmp = memcmp(name1, name2, len);
85 if (cmp)
86 return cmp;
87 if (len1 < len2)
88 return -1;
89 if (len1 > len2)
90 return 1;
95fd5bf8
LT
91 if (flags1 < flags2)
92 return -1;
93 if (flags1 > flags2)
94 return 1;
eb38c22f
LT
95 return 0;
96}
97
98int cache_name_pos(const char *name, int namelen)
99{
100 int first, last;
101
102 first = 0;
103 last = active_nr;
104 while (last > first) {
105 int next = (last + first) >> 1;
106 struct cache_entry *ce = active_cache[next];
95fd5bf8 107 int cmp = cache_name_compare(name, namelen, ce->name, htons(ce->ce_flags));
eb38c22f 108 if (!cmp)
76e7f4ec 109 return next;
eb38c22f
LT
110 if (cmp < 0) {
111 last = next;
112 continue;
113 }
114 first = next+1;
115 }
76e7f4ec 116 return -first-1;
eb38c22f
LT
117}
118
7b937ca3 119/* Remove entry, return true if there are more entries to go.. */
dbbce55b 120int remove_cache_entry_at(int pos)
7b937ca3 121{
ee267527 122 active_cache_changed = 1;
7b937ca3
LT
123 active_nr--;
124 if (pos >= active_nr)
125 return 0;
126 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
127 return 1;
128}
129
197ee8c9
LT
130int remove_file_from_cache(char *path)
131{
132 int pos = cache_name_pos(path, strlen(path));
c4e3cca1
JH
133 if (pos < 0)
134 pos = -pos-1;
135 while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
dbbce55b 136 remove_cache_entry_at(pos);
197ee8c9
LT
137 return 0;
138}
139
dbbce55b 140int ce_same_name(struct cache_entry *a, struct cache_entry *b)
7b937ca3
LT
141{
142 int len = ce_namelen(a);
143 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
144}
145
0f1e4f04
JH
146/* We may be in a situation where we already have path/file and path
147 * is being added, or we already have path and path/file is being
148 * added. Either one would result in a nonsense tree that has path
149 * twice when git-write-tree tries to write it out. Prevent it.
192268c1
JH
150 *
151 * If ok-to-replace is specified, we remove the conflicting entries
152 * from the cache so the caller should recompute the insert position.
153 * When this happens, we return non-zero.
0f1e4f04 154 */
192268c1
JH
155static int check_file_directory_conflict(const struct cache_entry *ce,
156 int ok_to_replace)
0f1e4f04 157{
192268c1 158 int pos, replaced = 0;
0f1e4f04
JH
159 const char *path = ce->name;
160 int namelen = strlen(path);
161 int stage = ce_stage(ce);
162 char *pathbuf = xmalloc(namelen + 1);
163 char *cp;
164
165 memcpy(pathbuf, path, namelen + 1);
166
167 /*
168 * We are inserting path/file. Do they have path registered at
169 * the same stage? We need to do this for all the levels of our
170 * subpath.
171 */
172 cp = pathbuf;
173 while (1) {
174 char *ep = strchr(cp, '/');
aa575f81 175 if (!ep)
0f1e4f04
JH
176 break;
177 *ep = 0; /* first cut it at slash */
178 pos = cache_name_pos(pathbuf,
179 htons(create_ce_flags(ep-cp, stage)));
180 if (0 <= pos) {
181 /* Our leading path component is registered as a file,
182 * and we are trying to make it a directory. This is
183 * bad.
184 */
192268c1
JH
185 if (!ok_to_replace) {
186 free(pathbuf);
187 return -1;
188 }
189 fprintf(stderr, "removing file '%s' to replace it with a directory to create '%s'.\n", pathbuf, path);
dbbce55b 190 remove_cache_entry_at(pos);
192268c1 191 replaced = 1;
0f1e4f04
JH
192 }
193 *ep = '/'; /* then restore it and go downwards */
194 cp = ep + 1;
195 }
196 free(pathbuf);
197
198 /* Do we have an entry in the cache that makes our path a prefix
199 * of it? That is, are we creating a file where they already expect
200 * a directory there?
201 */
202 pos = cache_name_pos(path,
203 htons(create_ce_flags(namelen, stage)));
204
205 /* (0 <= pos) cannot happen because add_cache_entry()
206 * should have taken care of that case.
207 */
208 pos = -pos-1;
209
210 /* pos would point at an existing entry that would come immediately
211 * after our path. It could be the same as our path in higher stage,
212 * or different path but in a lower stage.
213 *
214 * E.g. when we are inserting path at stage 2,
215 *
216 * 1 path
217 * pos-> 3 path
192268c1
JH
218 * 2 path/file1
219 * 3 path/file1
220 * 2 path/file2
221 * 2 patho
0f1e4f04
JH
222 *
223 * We need to examine pos, ignore it because it is at different
224 * stage, examine next to find the path/file at stage 2, and
192268c1
JH
225 * complain. We need to do this until we are not the leading
226 * path of an existing entry anymore.
0f1e4f04
JH
227 */
228
229 while (pos < active_nr) {
230 struct cache_entry *other = active_cache[pos];
231 if (strncmp(other->name, path, namelen))
232 break; /* it is not our "subdirectory" anymore */
192268c1
JH
233 if ((ce_stage(other) == stage) &&
234 other->name[namelen] == '/') {
235 if (!ok_to_replace)
236 return -1;
237 fprintf(stderr, "removing file '%s' under '%s' to be replaced with a file\n", other->name, path);
dbbce55b 238 remove_cache_entry_at(pos);
192268c1
JH
239 replaced = 1;
240 continue; /* cycle without updating pos */
241 }
0f1e4f04
JH
242 pos++;
243 }
192268c1 244 return replaced;
0f1e4f04
JH
245}
246
192268c1 247int add_cache_entry(struct cache_entry *ce, int option)
197ee8c9
LT
248{
249 int pos;
192268c1
JH
250 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
251 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
95fd5bf8 252 pos = cache_name_pos(ce->name, htons(ce->ce_flags));
197ee8c9
LT
253
254 /* existing match? Just replace it */
76e7f4ec 255 if (pos >= 0) {
ee267527 256 active_cache_changed = 1;
76e7f4ec 257 active_cache[pos] = ce;
197ee8c9
LT
258 return 0;
259 }
76e7f4ec 260 pos = -pos-1;
197ee8c9 261
7b937ca3
LT
262 /*
263 * Inserting a merged entry ("stage 0") into the index
264 * will always replace all non-merged entries..
265 */
266 if (pos < active_nr && ce_stage(ce) == 0) {
dbbce55b 267 while (ce_same_name(active_cache[pos], ce)) {
7b937ca3 268 ok_to_add = 1;
dbbce55b 269 if (!remove_cache_entry_at(pos))
7b937ca3
LT
270 break;
271 }
272 }
273
121481ab
LT
274 if (!ok_to_add)
275 return -1;
276
192268c1
JH
277 if (check_file_directory_conflict(ce, ok_to_replace)) {
278 if (!ok_to_replace)
279 return -1;
280 pos = cache_name_pos(ce->name, htons(ce->ce_flags));
281 pos = -pos-1;
282 }
0f1e4f04 283
197ee8c9
LT
284 /* Make sure the array is big enough .. */
285 if (active_nr == active_alloc) {
286 active_alloc = alloc_nr(active_alloc);
812666c8 287 active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
197ee8c9
LT
288 }
289
290 /* Add it in.. */
291 active_nr++;
292 if (active_nr > pos)
293 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
294 active_cache[pos] = ce;
ee267527 295 active_cache_changed = 1;
197ee8c9
LT
296 return 0;
297}
298
e83c5163
LT
299static int verify_hdr(struct cache_header *hdr, unsigned long size)
300{
301 SHA_CTX c;
302 unsigned char sha1[20];
303
ccc4feb5 304 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
e83c5163 305 return error("bad signature");
ca9be054
LT
306 if (hdr->hdr_version != htonl(2))
307 return error("bad index version");
e83c5163 308 SHA1_Init(&c);
ca9be054 309 SHA1_Update(&c, hdr, size - 20);
e83c5163 310 SHA1_Final(sha1, &c);
ca9be054
LT
311 if (memcmp(sha1, (void *)hdr + size - 20, 20))
312 return error("bad index file sha1 signature");
e83c5163
LT
313 return 0;
314}
315
316int read_cache(void)
317{
318 int fd, i;
319 struct stat st;
320 unsigned long size, offset;
321 void *map;
322 struct cache_header *hdr;
323
324 errno = EBUSY;
325 if (active_cache)
326 return error("more than one cachefile");
327 errno = ENOENT;
bb233d69 328 fd = open(get_index_file(), O_RDONLY);
e83c5163
LT
329 if (fd < 0)
330 return (errno == ENOENT) ? 0 : error("open failed");
331
19b2860c 332 size = 0; // avoid gcc warning
e83c5163
LT
333 map = (void *)-1;
334 if (!fstat(fd, &st)) {
e83c5163
LT
335 size = st.st_size;
336 errno = EINVAL;
ca9be054 337 if (size >= sizeof(struct cache_header) + 20)
520fc241 338 map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
e83c5163
LT
339 }
340 close(fd);
341 if (-1 == (int)(long)map)
342 return error("mmap failed");
343
344 hdr = map;
345 if (verify_hdr(hdr, size) < 0)
346 goto unmap;
347
ccc4feb5 348 active_nr = ntohl(hdr->hdr_entries);
e83c5163
LT
349 active_alloc = alloc_nr(active_nr);
350 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
351
352 offset = sizeof(*hdr);
ccc4feb5 353 for (i = 0; i < active_nr; i++) {
e83c5163
LT
354 struct cache_entry *ce = map + offset;
355 offset = offset + ce_size(ce);
356 active_cache[i] = ce;
357 }
358 return active_nr;
359
360unmap:
361 munmap(map, size);
362 errno = EINVAL;
363 return error("verify header failed");
364}
365
4990aadc 366#define WRITE_BUFFER_SIZE 8192
bf0f910d 367static unsigned char write_buffer[WRITE_BUFFER_SIZE];
4990aadc
LT
368static unsigned long write_buffer_len;
369
ca9be054 370static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
4990aadc
LT
371{
372 while (len) {
373 unsigned int buffered = write_buffer_len;
374 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
375 if (partial > len)
376 partial = len;
377 memcpy(write_buffer + buffered, data, partial);
378 buffered += partial;
379 if (buffered == WRITE_BUFFER_SIZE) {
ca9be054 380 SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE);
4990aadc
LT
381 if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE)
382 return -1;
383 buffered = 0;
384 }
385 write_buffer_len = buffered;
386 len -= partial;
387 data += partial;
388 }
389 return 0;
390}
391
ca9be054 392static int ce_flush(SHA_CTX *context, int fd)
4990aadc
LT
393{
394 unsigned int left = write_buffer_len;
ca9be054 395
4990aadc
LT
396 if (left) {
397 write_buffer_len = 0;
ca9be054 398 SHA1_Update(context, write_buffer, left);
4990aadc 399 }
ca9be054
LT
400
401 /* Append the SHA1 signature at the end */
402 SHA1_Final(write_buffer + left, context);
403 left += 20;
404 if (write(fd, write_buffer, left) != left)
405 return -1;
4990aadc
LT
406 return 0;
407}
408
197ee8c9
LT
409int write_cache(int newfd, struct cache_entry **cache, int entries)
410{
411 SHA_CTX c;
412 struct cache_header hdr;
413 int i;
414
ccc4feb5 415 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
ca9be054 416 hdr.hdr_version = htonl(2);
ccc4feb5 417 hdr.hdr_entries = htonl(entries);
197ee8c9
LT
418
419 SHA1_Init(&c);
ca9be054 420 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
197ee8c9
LT
421 return -1;
422
423 for (i = 0; i < entries; i++) {
424 struct cache_entry *ce = cache[i];
ca9be054 425 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
197ee8c9
LT
426 return -1;
427 }
ca9be054 428 return ce_flush(&c, newfd);
197ee8c9 429}