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