]> git.ipfire.org Git - thirdparty/git.git/blame - read-cache.c
Fix usage of carets in git-rev-parse(1)
[thirdparty/git.git] / read-cache.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163
LT
6#include "cache.h"
7
e83c5163 8struct cache_entry **active_cache = NULL;
ee267527 9unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0;
e83c5163 10
415e96c8
JH
11/*
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.
15 */
16void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
17{
18 ce->ce_ctime.sec = htonl(st->st_ctime);
19 ce->ce_mtime.sec = htonl(st->st_mtime);
2cb45e95 20#ifdef USE_NSEC
415e96c8
JH
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);
29}
30
5d728c84 31int ce_match_stat(struct cache_entry *ce, struct stat *st)
734aab75
LT
32{
33 unsigned int changed = 0;
34
8ae0a8c5
KS
35 switch (ntohl(ce->ce_mode) & S_IFMT) {
36 case S_IFREG:
37 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
ffbe1add
KS
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;
8ae0a8c5
KS
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));
47 }
ccc4feb5 48 if (ce->ce_mtime.sec != htonl(st->st_mtime))
734aab75 49 changed |= MTIME_CHANGED;
ccc4feb5
LT
50 if (ce->ce_ctime.sec != htonl(st->st_ctime))
51 changed |= CTIME_CHANGED;
52
2cb45e95 53#ifdef USE_NSEC
ccc4feb5
LT
54 /*
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.
58 */
94dfb7f2 59 if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec))
ccc4feb5 60 changed |= MTIME_CHANGED;
94dfb7f2 61 if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec))
734aab75 62 changed |= CTIME_CHANGED;
ccc4feb5
LT
63#endif
64
65 if (ce->ce_uid != htonl(st->st_uid) ||
66 ce->ce_gid != htonl(st->st_gid))
734aab75 67 changed |= OWNER_CHANGED;
2cb45e95 68 if (ce->ce_ino != htonl(st->st_ino))
734aab75 69 changed |= INODE_CHANGED;
2cb45e95
LT
70
71#ifdef USE_STDEV
72 /*
73 * st_dev breaks on network filesystems where different
74 * clients will have different views of what "device"
75 * the filesystem is on
76 */
77 if (ce->ce_dev != htonl(st->st_dev))
78 changed |= INODE_CHANGED;
79#endif
80
ccc4feb5 81 if (ce->ce_size != htonl(st->st_size))
734aab75
LT
82 changed |= DATA_CHANGED;
83 return changed;
84}
85
b0391890
JH
86static int ce_compare_data(struct cache_entry *ce, struct stat *st)
87{
88 int match = -1;
89 int fd = open(ce->name, O_RDONLY);
90
91 if (fd >= 0) {
92 unsigned char sha1[20];
93 if (!index_fd(sha1, fd, st, 0, NULL))
94 match = memcmp(sha1, ce->sha1, 20);
95 close(fd);
96 }
97 return match;
98}
99
100static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size)
101{
102 int match = -1;
103 char *target;
104 void *buffer;
105 unsigned long size;
106 char type[10];
107 int len;
108
109 target = xmalloc(expected_size);
110 len = readlink(ce->name, target, expected_size);
111 if (len != expected_size) {
112 free(target);
113 return -1;
114 }
115 buffer = read_sha1_file(ce->sha1, type, &size);
116 if (!buffer) {
117 free(target);
118 return -1;
119 }
120 if (size == expected_size)
121 match = memcmp(buffer, target, size);
122 free(buffer);
123 free(target);
124 return match;
125}
126
127int ce_modified(struct cache_entry *ce, struct stat *st)
128{
129 int changed;
130 changed = ce_match_stat(ce, st);
131 if (!changed)
132 return 0;
133
134 /*
135 * If the mode or type has changed, there's no point in trying
136 * to refresh the entry - it's not going to match
137 */
138 if (changed & (MODE_CHANGED | TYPE_CHANGED))
139 return changed;
140
141 /* Immediately after read-tree or update-index --cacheinfo,
142 * the length field is zero. For other cases the ce_size
143 * should match the SHA1 recorded in the index entry.
144 */
145 if ((changed & DATA_CHANGED) && ce->ce_size != htonl(0))
146 return changed;
147
148 switch (st->st_mode & S_IFMT) {
149 case S_IFREG:
150 if (ce_compare_data(ce, st))
151 return changed | DATA_CHANGED;
152 break;
153 case S_IFLNK:
154 if (ce_compare_link(ce, st->st_size))
155 return changed | DATA_CHANGED;
156 break;
157 default:
158 return changed | TYPE_CHANGED;
159 }
160 return 0;
161}
162
958ba6c9
LT
163int base_name_compare(const char *name1, int len1, int mode1,
164 const char *name2, int len2, int mode2)
165{
166 unsigned char c1, c2;
167 int len = len1 < len2 ? len1 : len2;
168 int cmp;
169
170 cmp = memcmp(name1, name2, len);
171 if (cmp)
172 return cmp;
173 c1 = name1[len];
174 c2 = name2[len];
175 if (!c1 && S_ISDIR(mode1))
176 c1 = '/';
177 if (!c2 && S_ISDIR(mode2))
178 c2 = '/';
179 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
180}
181
95fd5bf8 182int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
eb38c22f 183{
95fd5bf8
LT
184 int len1 = flags1 & CE_NAMEMASK;
185 int len2 = flags2 & CE_NAMEMASK;
eb38c22f
LT
186 int len = len1 < len2 ? len1 : len2;
187 int cmp;
188
189 cmp = memcmp(name1, name2, len);
190 if (cmp)
191 return cmp;
192 if (len1 < len2)
193 return -1;
194 if (len1 > len2)
195 return 1;
95fd5bf8
LT
196 if (flags1 < flags2)
197 return -1;
198 if (flags1 > flags2)
199 return 1;
eb38c22f
LT
200 return 0;
201}
202
203int cache_name_pos(const char *name, int namelen)
204{
205 int first, last;
206
207 first = 0;
208 last = active_nr;
209 while (last > first) {
210 int next = (last + first) >> 1;
211 struct cache_entry *ce = active_cache[next];
972d1bb0 212 int cmp = cache_name_compare(name, namelen, ce->name, ntohs(ce->ce_flags));
eb38c22f 213 if (!cmp)
76e7f4ec 214 return next;
eb38c22f
LT
215 if (cmp < 0) {
216 last = next;
217 continue;
218 }
219 first = next+1;
220 }
76e7f4ec 221 return -first-1;
eb38c22f
LT
222}
223
7b937ca3 224/* Remove entry, return true if there are more entries to go.. */
dbbce55b 225int remove_cache_entry_at(int pos)
7b937ca3 226{
ee267527 227 active_cache_changed = 1;
7b937ca3
LT
228 active_nr--;
229 if (pos >= active_nr)
230 return 0;
231 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
232 return 1;
233}
234
6b5ee137 235int remove_file_from_cache(const char *path)
197ee8c9
LT
236{
237 int pos = cache_name_pos(path, strlen(path));
c4e3cca1
JH
238 if (pos < 0)
239 pos = -pos-1;
240 while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
dbbce55b 241 remove_cache_entry_at(pos);
197ee8c9
LT
242 return 0;
243}
244
dbbce55b 245int ce_same_name(struct cache_entry *a, struct cache_entry *b)
7b937ca3
LT
246{
247 int len = ce_namelen(a);
248 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
249}
250
c0fd1f51
LT
251int ce_path_match(const struct cache_entry *ce, const char **pathspec)
252{
253 const char *match, *name;
254 int len;
255
256 if (!pathspec)
257 return 1;
258
259 len = ce_namelen(ce);
260 name = ce->name;
261 while ((match = *pathspec++) != NULL) {
262 int matchlen = strlen(match);
263 if (matchlen > len)
264 continue;
265 if (memcmp(name, match, matchlen))
266 continue;
267 if (matchlen && name[matchlen-1] == '/')
268 return 1;
269 if (name[matchlen] == '/' || !name[matchlen])
270 return 1;
f332726e
LT
271 if (!matchlen)
272 return 1;
c0fd1f51
LT
273 }
274 return 0;
275}
276
12676608
LT
277/*
278 * Do we have another file that has the beginning components being a
279 * proper superset of the name we're trying to add?
0f1e4f04 280 */
12676608 281static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replace)
0f1e4f04 282{
12676608
LT
283 int retval = 0;
284 int len = ce_namelen(ce);
b155725d 285 int stage = ce_stage(ce);
12676608 286 const char *name = ce->name;
0f1e4f04 287
12676608
LT
288 while (pos < active_nr) {
289 struct cache_entry *p = active_cache[pos++];
0f1e4f04 290
12676608 291 if (len >= ce_namelen(p))
0f1e4f04 292 break;
12676608
LT
293 if (memcmp(name, p->name, len))
294 break;
b155725d
JH
295 if (ce_stage(p) != stage)
296 continue;
12676608
LT
297 if (p->name[len] != '/')
298 continue;
12676608
LT
299 retval = -1;
300 if (!ok_to_replace)
301 break;
302 remove_cache_entry_at(--pos);
0f1e4f04 303 }
12676608
LT
304 return retval;
305}
0f1e4f04 306
12676608
LT
307/*
308 * Do we have another file with a pathname that is a proper
309 * subset of the name we're trying to add?
310 */
311static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace)
312{
313 int retval = 0;
b155725d 314 int stage = ce_stage(ce);
12676608
LT
315 const char *name = ce->name;
316 const char *slash = name + ce_namelen(ce);
0f1e4f04 317
12676608
LT
318 for (;;) {
319 int len;
0f1e4f04 320
12676608
LT
321 for (;;) {
322 if (*--slash == '/')
323 break;
324 if (slash <= ce->name)
325 return retval;
326 }
327 len = slash - name;
0f1e4f04 328
b155725d 329 pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage)));
12676608
LT
330 if (pos >= 0) {
331 retval = -1;
332 if (ok_to_replace)
333 break;
dbbce55b 334 remove_cache_entry_at(pos);
12676608
LT
335 continue;
336 }
337
338 /*
339 * Trivial optimization: if we find an entry that
340 * already matches the sub-directory, then we know
b155725d 341 * we're ok, and we can exit.
12676608
LT
342 */
343 pos = -pos-1;
b155725d 344 while (pos < active_nr) {
12676608 345 struct cache_entry *p = active_cache[pos];
b155725d
JH
346 if ((ce_namelen(p) <= len) ||
347 (p->name[len] != '/') ||
348 memcmp(p->name, name, len))
349 break; /* not our subdirectory */
350 if (ce_stage(p) == stage)
351 /* p is at the same stage as our entry, and
352 * is a subdirectory of what we are looking
353 * at, so we cannot have conflicts at our
354 * level or anything shorter.
355 */
356 return retval;
357 pos++;
192268c1 358 }
0f1e4f04 359 }
12676608
LT
360 return retval;
361}
362
363/* We may be in a situation where we already have path/file and path
364 * is being added, or we already have path and path/file is being
365 * added. Either one would result in a nonsense tree that has path
366 * twice when git-write-tree tries to write it out. Prevent it.
367 *
368 * If ok-to-replace is specified, we remove the conflicting entries
369 * from the cache so the caller should recompute the insert position.
370 * When this happens, we return non-zero.
371 */
372static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace)
373{
374 /*
375 * We check if the path is a sub-path of a subsequent pathname
376 * first, since removing those will not change the position
377 * in the array
378 */
379 int retval = has_file_name(ce, pos, ok_to_replace);
380 /*
381 * Then check if the path might have a clashing sub-directory
382 * before it.
383 */
384 return retval + has_dir_name(ce, pos, ok_to_replace);
0f1e4f04
JH
385}
386
192268c1 387int add_cache_entry(struct cache_entry *ce, int option)
197ee8c9
LT
388{
389 int pos;
192268c1
JH
390 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
391 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
b155725d 392 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
972d1bb0 393 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
197ee8c9
LT
394
395 /* existing match? Just replace it */
76e7f4ec 396 if (pos >= 0) {
ee267527 397 active_cache_changed = 1;
76e7f4ec 398 active_cache[pos] = ce;
197ee8c9
LT
399 return 0;
400 }
76e7f4ec 401 pos = -pos-1;
197ee8c9 402
7b937ca3
LT
403 /*
404 * Inserting a merged entry ("stage 0") into the index
405 * will always replace all non-merged entries..
406 */
407 if (pos < active_nr && ce_stage(ce) == 0) {
dbbce55b 408 while (ce_same_name(active_cache[pos], ce)) {
7b937ca3 409 ok_to_add = 1;
dbbce55b 410 if (!remove_cache_entry_at(pos))
7b937ca3
LT
411 break;
412 }
413 }
414
121481ab
LT
415 if (!ok_to_add)
416 return -1;
417
b155725d 418 if (!skip_df_check && check_file_directory_conflict(ce, pos, ok_to_replace)) {
192268c1
JH
419 if (!ok_to_replace)
420 return -1;
972d1bb0 421 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
192268c1
JH
422 pos = -pos-1;
423 }
0f1e4f04 424
197ee8c9
LT
425 /* Make sure the array is big enough .. */
426 if (active_nr == active_alloc) {
427 active_alloc = alloc_nr(active_alloc);
812666c8 428 active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
197ee8c9
LT
429 }
430
431 /* Add it in.. */
432 active_nr++;
433 if (active_nr > pos)
434 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
435 active_cache[pos] = ce;
ee267527 436 active_cache_changed = 1;
197ee8c9
LT
437 return 0;
438}
439
e83c5163
LT
440static int verify_hdr(struct cache_header *hdr, unsigned long size)
441{
442 SHA_CTX c;
443 unsigned char sha1[20];
444
ccc4feb5 445 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
e83c5163 446 return error("bad signature");
ca9be054
LT
447 if (hdr->hdr_version != htonl(2))
448 return error("bad index version");
e83c5163 449 SHA1_Init(&c);
ca9be054 450 SHA1_Update(&c, hdr, size - 20);
e83c5163 451 SHA1_Final(sha1, &c);
ca9be054
LT
452 if (memcmp(sha1, (void *)hdr + size - 20, 20))
453 return error("bad index file sha1 signature");
e83c5163
LT
454 return 0;
455}
456
457int read_cache(void)
458{
459 int fd, i;
460 struct stat st;
461 unsigned long size, offset;
462 void *map;
463 struct cache_header *hdr;
464
465 errno = EBUSY;
466 if (active_cache)
5d1a5c02
LT
467 return active_nr;
468
e83c5163 469 errno = ENOENT;
bb233d69 470 fd = open(get_index_file(), O_RDONLY);
5d1a5c02
LT
471 if (fd < 0) {
472 if (errno == ENOENT)
473 return 0;
474 die("index file open failed (%s)", strerror(errno));
475 }
e83c5163 476
19b2860c 477 size = 0; // avoid gcc warning
e35f9824 478 map = MAP_FAILED;
e83c5163 479 if (!fstat(fd, &st)) {
e83c5163
LT
480 size = st.st_size;
481 errno = EINVAL;
ca9be054 482 if (size >= sizeof(struct cache_header) + 20)
520fc241 483 map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
e83c5163
LT
484 }
485 close(fd);
e35f9824 486 if (map == MAP_FAILED)
5d1a5c02 487 die("index file mmap failed (%s)", strerror(errno));
e83c5163
LT
488
489 hdr = map;
490 if (verify_hdr(hdr, size) < 0)
491 goto unmap;
492
ccc4feb5 493 active_nr = ntohl(hdr->hdr_entries);
e83c5163
LT
494 active_alloc = alloc_nr(active_nr);
495 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
496
497 offset = sizeof(*hdr);
ccc4feb5 498 for (i = 0; i < active_nr; i++) {
e83c5163
LT
499 struct cache_entry *ce = map + offset;
500 offset = offset + ce_size(ce);
501 active_cache[i] = ce;
502 }
503 return active_nr;
504
505unmap:
506 munmap(map, size);
507 errno = EINVAL;
5d1a5c02 508 die("index file corrupt");
e83c5163
LT
509}
510
4990aadc 511#define WRITE_BUFFER_SIZE 8192
bf0f910d 512static unsigned char write_buffer[WRITE_BUFFER_SIZE];
4990aadc
LT
513static unsigned long write_buffer_len;
514
ca9be054 515static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
4990aadc
LT
516{
517 while (len) {
518 unsigned int buffered = write_buffer_len;
519 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
520 if (partial > len)
521 partial = len;
522 memcpy(write_buffer + buffered, data, partial);
523 buffered += partial;
524 if (buffered == WRITE_BUFFER_SIZE) {
ca9be054 525 SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE);
4990aadc
LT
526 if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE)
527 return -1;
528 buffered = 0;
529 }
530 write_buffer_len = buffered;
531 len -= partial;
532 data += partial;
533 }
534 return 0;
535}
536
ca9be054 537static int ce_flush(SHA_CTX *context, int fd)
4990aadc
LT
538{
539 unsigned int left = write_buffer_len;
ca9be054 540
4990aadc
LT
541 if (left) {
542 write_buffer_len = 0;
ca9be054 543 SHA1_Update(context, write_buffer, left);
4990aadc 544 }
ca9be054 545
2c865d9a
QH
546 /* Flush first if not enough space for SHA1 signature */
547 if (left + 20 > WRITE_BUFFER_SIZE) {
548 if (write(fd, write_buffer, left) != left)
549 return -1;
550 left = 0;
551 }
552
ca9be054
LT
553 /* Append the SHA1 signature at the end */
554 SHA1_Final(write_buffer + left, context);
555 left += 20;
556 if (write(fd, write_buffer, left) != left)
557 return -1;
4990aadc
LT
558 return 0;
559}
560
197ee8c9
LT
561int write_cache(int newfd, struct cache_entry **cache, int entries)
562{
563 SHA_CTX c;
564 struct cache_header hdr;
025a0709
JH
565 int i, removed;
566
567 for (i = removed = 0; i < entries; i++)
568 if (!cache[i]->ce_mode)
569 removed++;
197ee8c9 570
ccc4feb5 571 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
ca9be054 572 hdr.hdr_version = htonl(2);
025a0709 573 hdr.hdr_entries = htonl(entries - removed);
197ee8c9
LT
574
575 SHA1_Init(&c);
ca9be054 576 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
197ee8c9
LT
577 return -1;
578
579 for (i = 0; i < entries; i++) {
580 struct cache_entry *ce = cache[i];
aa16021e
LT
581 if (!ce->ce_mode)
582 continue;
ca9be054 583 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
197ee8c9
LT
584 return -1;
585 }
ca9be054 586 return ce_flush(&c, newfd);
197ee8c9 587}