]> git.ipfire.org Git - thirdparty/git.git/blame - read-cache.c
Fix passing of TCLTK_PATH to git-gui
[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 6#include "cache.h"
bad68ec9
JH
7#include "cache-tree.h"
8
9/* Index extensions.
10 *
11 * The first letter should be 'A'..'Z' for extensions that are not
12 * necessary for a correct operation (i.e. optimization data).
13 * When new extensions are added that _needs_ to be understood in
14 * order to correctly interpret the index file, pick character that
15 * is outside the range, to cause the reader to abort.
16 */
17
18#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
19#define CACHE_EXT_TREE 0x54524545 /* "TREE" */
e83c5163 20
96f1e58f 21struct cache_entry **active_cache;
29e4d363 22static time_t index_file_timestamp;
96f1e58f 23unsigned int active_nr, active_alloc, active_cache_changed;
e83c5163 24
96f1e58f 25struct cache_tree *active_cache_tree;
bad68ec9 26
96f1e58f
DR
27static void *cache_mmap;
28static size_t cache_mmap_size;
8fd2cb40 29
415e96c8
JH
30/*
31 * This only updates the "non-critical" parts of the directory
32 * cache, ie the parts that aren't tracked by GIT, and only used
33 * to validate the cache.
34 */
35void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
36{
37 ce->ce_ctime.sec = htonl(st->st_ctime);
38 ce->ce_mtime.sec = htonl(st->st_mtime);
2cb45e95 39#ifdef USE_NSEC
415e96c8
JH
40 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
41 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
42#endif
43 ce->ce_dev = htonl(st->st_dev);
44 ce->ce_ino = htonl(st->st_ino);
45 ce->ce_uid = htonl(st->st_uid);
46 ce->ce_gid = htonl(st->st_gid);
47 ce->ce_size = htonl(st->st_size);
5f73076c
JH
48
49 if (assume_unchanged)
50 ce->ce_flags |= htons(CE_VALID);
415e96c8
JH
51}
52
29e4d363
JH
53static int ce_compare_data(struct cache_entry *ce, struct stat *st)
54{
55 int match = -1;
56 int fd = open(ce->name, O_RDONLY);
57
58 if (fd >= 0) {
59 unsigned char sha1[20];
53bca91a 60 if (!index_fd(sha1, fd, st, 0, OBJ_BLOB, ce->name))
a89fccd2 61 match = hashcmp(sha1, ce->sha1);
7f8508e8 62 /* index_fd() closed the file descriptor already */
29e4d363
JH
63 }
64 return match;
65}
66
dc49cd76 67static int ce_compare_link(struct cache_entry *ce, size_t expected_size)
29e4d363
JH
68{
69 int match = -1;
70 char *target;
71 void *buffer;
72 unsigned long size;
21666f1a 73 enum object_type type;
29e4d363
JH
74 int len;
75
76 target = xmalloc(expected_size);
77 len = readlink(ce->name, target, expected_size);
78 if (len != expected_size) {
79 free(target);
80 return -1;
81 }
21666f1a 82 buffer = read_sha1_file(ce->sha1, &type, &size);
29e4d363
JH
83 if (!buffer) {
84 free(target);
85 return -1;
86 }
87 if (size == expected_size)
88 match = memcmp(buffer, target, size);
89 free(buffer);
90 free(target);
91 return match;
92}
93
94static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)
95{
96 switch (st->st_mode & S_IFMT) {
97 case S_IFREG:
98 if (ce_compare_data(ce, st))
99 return DATA_CHANGED;
100 break;
101 case S_IFLNK:
dc49cd76 102 if (ce_compare_link(ce, xsize_t(st->st_size)))
29e4d363
JH
103 return DATA_CHANGED;
104 break;
105 default:
106 return TYPE_CHANGED;
107 }
108 return 0;
109}
110
407c8eb0 111static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st)
734aab75
LT
112{
113 unsigned int changed = 0;
114
8ae0a8c5
KS
115 switch (ntohl(ce->ce_mode) & S_IFMT) {
116 case S_IFREG:
117 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
3e09cdfd
JH
118 /* We consider only the owner x bit to be relevant for
119 * "mode changes"
120 */
121 if (trust_executable_bit &&
122 (0100 & (ntohl(ce->ce_mode) ^ st->st_mode)))
ffbe1add 123 changed |= MODE_CHANGED;
8ae0a8c5
KS
124 break;
125 case S_IFLNK:
78a8d641
JS
126 if (!S_ISLNK(st->st_mode) &&
127 (has_symlinks || !S_ISREG(st->st_mode)))
128 changed |= TYPE_CHANGED;
8ae0a8c5
KS
129 break;
130 default:
131 die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
132 }
ccc4feb5 133 if (ce->ce_mtime.sec != htonl(st->st_mtime))
734aab75 134 changed |= MTIME_CHANGED;
ccc4feb5
LT
135 if (ce->ce_ctime.sec != htonl(st->st_ctime))
136 changed |= CTIME_CHANGED;
137
2cb45e95 138#ifdef USE_NSEC
ccc4feb5
LT
139 /*
140 * nsec seems unreliable - not all filesystems support it, so
141 * as long as it is in the inode cache you get right nsec
142 * but after it gets flushed, you get zero nsec.
143 */
94dfb7f2 144 if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec))
ccc4feb5 145 changed |= MTIME_CHANGED;
94dfb7f2 146 if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec))
734aab75 147 changed |= CTIME_CHANGED;
ccc4feb5
LT
148#endif
149
150 if (ce->ce_uid != htonl(st->st_uid) ||
151 ce->ce_gid != htonl(st->st_gid))
734aab75 152 changed |= OWNER_CHANGED;
2cb45e95 153 if (ce->ce_ino != htonl(st->st_ino))
734aab75 154 changed |= INODE_CHANGED;
2cb45e95
LT
155
156#ifdef USE_STDEV
157 /*
158 * st_dev breaks on network filesystems where different
159 * clients will have different views of what "device"
160 * the filesystem is on
161 */
162 if (ce->ce_dev != htonl(st->st_dev))
163 changed |= INODE_CHANGED;
164#endif
165
ccc4feb5 166 if (ce->ce_size != htonl(st->st_size))
734aab75 167 changed |= DATA_CHANGED;
b0391890 168
407c8eb0
JH
169 return changed;
170}
171
42f77406 172int ce_match_stat(struct cache_entry *ce, struct stat *st, int options)
407c8eb0 173{
5f73076c 174 unsigned int changed;
42f77406
JH
175 int ignore_valid = options & 01;
176 int assume_racy_is_modified = options & 02;
5f73076c
JH
177
178 /*
179 * If it's marked as always valid in the index, it's
180 * valid whatever the checked-out copy says.
181 */
182 if (!ignore_valid && (ce->ce_flags & htons(CE_VALID)))
183 return 0;
184
185 changed = ce_match_stat_basic(ce, st);
407c8eb0 186
29e4d363
JH
187 /*
188 * Within 1 second of this sequence:
189 * echo xyzzy >file && git-update-index --add file
190 * running this command:
191 * echo frotz >file
192 * would give a falsely clean cache entry. The mtime and
193 * length match the cache, and other stat fields do not change.
194 *
195 * We could detect this at update-index time (the cache entry
196 * being registered/updated records the same time as "now")
197 * and delay the return from git-update-index, but that would
198 * effectively mean we can make at most one commit per second,
199 * which is not acceptable. Instead, we check cache entries
200 * whose mtime are the same as the index file timestamp more
5f73076c 201 * carefully than others.
29e4d363
JH
202 */
203 if (!changed &&
204 index_file_timestamp &&
42f77406
JH
205 index_file_timestamp <= ntohl(ce->ce_mtime.sec)) {
206 if (assume_racy_is_modified)
207 changed |= DATA_CHANGED;
208 else
209 changed |= ce_modified_check_fs(ce, st);
210 }
b0391890 211
29e4d363 212 return changed;
b0391890
JH
213}
214
5f73076c 215int ce_modified(struct cache_entry *ce, struct stat *st, int really)
b0391890 216{
29e4d363 217 int changed, changed_fs;
5f73076c 218 changed = ce_match_stat(ce, st, really);
b0391890
JH
219 if (!changed)
220 return 0;
b0391890
JH
221 /*
222 * If the mode or type has changed, there's no point in trying
223 * to refresh the entry - it's not going to match
224 */
225 if (changed & (MODE_CHANGED | TYPE_CHANGED))
226 return changed;
227
228 /* Immediately after read-tree or update-index --cacheinfo,
229 * the length field is zero. For other cases the ce_size
230 * should match the SHA1 recorded in the index entry.
231 */
232 if ((changed & DATA_CHANGED) && ce->ce_size != htonl(0))
233 return changed;
234
29e4d363
JH
235 changed_fs = ce_modified_check_fs(ce, st);
236 if (changed_fs)
237 return changed | changed_fs;
b0391890
JH
238 return 0;
239}
240
958ba6c9
LT
241int base_name_compare(const char *name1, int len1, int mode1,
242 const char *name2, int len2, int mode2)
243{
244 unsigned char c1, c2;
245 int len = len1 < len2 ? len1 : len2;
246 int cmp;
247
248 cmp = memcmp(name1, name2, len);
249 if (cmp)
250 return cmp;
251 c1 = name1[len];
252 c2 = name2[len];
253 if (!c1 && S_ISDIR(mode1))
254 c1 = '/';
255 if (!c2 && S_ISDIR(mode2))
256 c2 = '/';
257 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
258}
259
95fd5bf8 260int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
eb38c22f 261{
95fd5bf8
LT
262 int len1 = flags1 & CE_NAMEMASK;
263 int len2 = flags2 & CE_NAMEMASK;
eb38c22f
LT
264 int len = len1 < len2 ? len1 : len2;
265 int cmp;
266
267 cmp = memcmp(name1, name2, len);
268 if (cmp)
269 return cmp;
270 if (len1 < len2)
271 return -1;
272 if (len1 > len2)
273 return 1;
5f73076c 274
7b80be15
JH
275 /* Compare stages */
276 flags1 &= CE_STAGEMASK;
277 flags2 &= CE_STAGEMASK;
5f73076c 278
95fd5bf8
LT
279 if (flags1 < flags2)
280 return -1;
281 if (flags1 > flags2)
282 return 1;
eb38c22f
LT
283 return 0;
284}
285
286int cache_name_pos(const char *name, int namelen)
287{
288 int first, last;
289
290 first = 0;
291 last = active_nr;
292 while (last > first) {
293 int next = (last + first) >> 1;
294 struct cache_entry *ce = active_cache[next];
972d1bb0 295 int cmp = cache_name_compare(name, namelen, ce->name, ntohs(ce->ce_flags));
eb38c22f 296 if (!cmp)
76e7f4ec 297 return next;
eb38c22f
LT
298 if (cmp < 0) {
299 last = next;
300 continue;
301 }
302 first = next+1;
303 }
76e7f4ec 304 return -first-1;
eb38c22f
LT
305}
306
7b937ca3 307/* Remove entry, return true if there are more entries to go.. */
dbbce55b 308int remove_cache_entry_at(int pos)
7b937ca3 309{
ee267527 310 active_cache_changed = 1;
7b937ca3
LT
311 active_nr--;
312 if (pos >= active_nr)
313 return 0;
314 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
315 return 1;
316}
317
6b5ee137 318int remove_file_from_cache(const char *path)
197ee8c9
LT
319{
320 int pos = cache_name_pos(path, strlen(path));
c4e3cca1
JH
321 if (pos < 0)
322 pos = -pos-1;
323 while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
dbbce55b 324 remove_cache_entry_at(pos);
197ee8c9
LT
325 return 0;
326}
327
fd1c3bf0 328int add_file_to_cache(const char *path, int verbose)
11be42a4
JS
329{
330 int size, namelen;
331 struct stat st;
332 struct cache_entry *ce;
333
334 if (lstat(path, &st))
335 die("%s: unable to stat (%s)", path, strerror(errno));
336
337 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))
338 die("%s: can only add regular files or symbolic links", path);
339
340 namelen = strlen(path);
341 size = cache_entry_size(namelen);
342 ce = xcalloc(1, size);
343 memcpy(ce->name, path, namelen);
344 ce->ce_flags = htons(namelen);
345 fill_stat_cache_info(ce, &st);
346
78a8d641 347 if (trust_executable_bit && has_symlinks)
185c975f
JH
348 ce->ce_mode = create_ce_mode(st.st_mode);
349 else {
78a8d641
JS
350 /* If there is an existing entry, pick the mode bits and type
351 * from it, otherwise assume unexecutable regular file.
11be42a4 352 */
185c975f 353 struct cache_entry *ent;
11be42a4 354 int pos = cache_name_pos(path, namelen);
185c975f
JH
355
356 ent = (0 <= pos) ? active_cache[pos] : NULL;
357 ce->ce_mode = ce_mode_from_stat(ent, st.st_mode);
11be42a4
JS
358 }
359
360 if (index_path(ce->sha1, path, &st, 1))
361 die("unable to index file %s", path);
c33ab0dd 362 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
11be42a4
JS
363 die("unable to add %s to index",path);
364 if (verbose)
365 printf("add '%s'\n", path);
366 cache_tree_invalidate_path(active_cache_tree, path);
367 return 0;
368}
369
dbbce55b 370int ce_same_name(struct cache_entry *a, struct cache_entry *b)
7b937ca3
LT
371{
372 int len = ce_namelen(a);
373 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
374}
375
c0fd1f51
LT
376int ce_path_match(const struct cache_entry *ce, const char **pathspec)
377{
378 const char *match, *name;
379 int len;
380
381 if (!pathspec)
382 return 1;
383
384 len = ce_namelen(ce);
385 name = ce->name;
386 while ((match = *pathspec++) != NULL) {
387 int matchlen = strlen(match);
388 if (matchlen > len)
389 continue;
390 if (memcmp(name, match, matchlen))
391 continue;
392 if (matchlen && name[matchlen-1] == '/')
393 return 1;
394 if (name[matchlen] == '/' || !name[matchlen])
395 return 1;
f332726e
LT
396 if (!matchlen)
397 return 1;
c0fd1f51
LT
398 }
399 return 0;
400}
401
8dcf39c4
LT
402/*
403 * We fundamentally don't like some paths: we don't want
404 * dot or dot-dot anywhere, and for obvious reasons don't
405 * want to recurse into ".git" either.
406 *
407 * Also, we don't want double slashes or slashes at the
408 * end that can make pathnames ambiguous.
409 */
410static int verify_dotfile(const char *rest)
411{
412 /*
413 * The first character was '.', but that
414 * has already been discarded, we now test
415 * the rest.
416 */
417 switch (*rest) {
418 /* "." is not allowed */
419 case '\0': case '/':
420 return 0;
421
422 /*
423 * ".git" followed by NUL or slash is bad. This
424 * shares the path end test with the ".." case.
425 */
426 case 'g':
427 if (rest[1] != 'i')
428 break;
429 if (rest[2] != 't')
430 break;
431 rest += 2;
432 /* fallthrough */
433 case '.':
434 if (rest[1] == '\0' || rest[1] == '/')
435 return 0;
436 }
437 return 1;
438}
439
440int verify_path(const char *path)
441{
442 char c;
443
444 goto inside;
445 for (;;) {
446 if (!c)
447 return 1;
448 if (c == '/') {
449inside:
450 c = *path++;
451 switch (c) {
452 default:
453 continue;
454 case '/': case '\0':
455 break;
456 case '.':
457 if (verify_dotfile(path))
458 continue;
459 }
460 return 0;
461 }
462 c = *path++;
463 }
464}
465
12676608
LT
466/*
467 * Do we have another file that has the beginning components being a
468 * proper superset of the name we're trying to add?
0f1e4f04 469 */
12676608 470static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replace)
0f1e4f04 471{
12676608
LT
472 int retval = 0;
473 int len = ce_namelen(ce);
b155725d 474 int stage = ce_stage(ce);
12676608 475 const char *name = ce->name;
0f1e4f04 476
12676608
LT
477 while (pos < active_nr) {
478 struct cache_entry *p = active_cache[pos++];
0f1e4f04 479
12676608 480 if (len >= ce_namelen(p))
0f1e4f04 481 break;
12676608
LT
482 if (memcmp(name, p->name, len))
483 break;
b155725d
JH
484 if (ce_stage(p) != stage)
485 continue;
12676608
LT
486 if (p->name[len] != '/')
487 continue;
12676608
LT
488 retval = -1;
489 if (!ok_to_replace)
490 break;
491 remove_cache_entry_at(--pos);
0f1e4f04 492 }
12676608
LT
493 return retval;
494}
0f1e4f04 495
12676608
LT
496/*
497 * Do we have another file with a pathname that is a proper
498 * subset of the name we're trying to add?
499 */
500static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace)
501{
502 int retval = 0;
b155725d 503 int stage = ce_stage(ce);
12676608
LT
504 const char *name = ce->name;
505 const char *slash = name + ce_namelen(ce);
0f1e4f04 506
12676608
LT
507 for (;;) {
508 int len;
0f1e4f04 509
12676608
LT
510 for (;;) {
511 if (*--slash == '/')
512 break;
513 if (slash <= ce->name)
514 return retval;
515 }
516 len = slash - name;
0f1e4f04 517
b155725d 518 pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage)));
12676608
LT
519 if (pos >= 0) {
520 retval = -1;
81a361be 521 if (!ok_to_replace)
12676608 522 break;
dbbce55b 523 remove_cache_entry_at(pos);
12676608
LT
524 continue;
525 }
526
527 /*
528 * Trivial optimization: if we find an entry that
529 * already matches the sub-directory, then we know
b155725d 530 * we're ok, and we can exit.
12676608
LT
531 */
532 pos = -pos-1;
b155725d 533 while (pos < active_nr) {
12676608 534 struct cache_entry *p = active_cache[pos];
b155725d
JH
535 if ((ce_namelen(p) <= len) ||
536 (p->name[len] != '/') ||
537 memcmp(p->name, name, len))
538 break; /* not our subdirectory */
539 if (ce_stage(p) == stage)
540 /* p is at the same stage as our entry, and
541 * is a subdirectory of what we are looking
542 * at, so we cannot have conflicts at our
543 * level or anything shorter.
544 */
545 return retval;
546 pos++;
192268c1 547 }
0f1e4f04 548 }
12676608
LT
549 return retval;
550}
551
552/* We may be in a situation where we already have path/file and path
553 * is being added, or we already have path and path/file is being
554 * added. Either one would result in a nonsense tree that has path
555 * twice when git-write-tree tries to write it out. Prevent it.
556 *
557 * If ok-to-replace is specified, we remove the conflicting entries
558 * from the cache so the caller should recompute the insert position.
559 * When this happens, we return non-zero.
560 */
561static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace)
562{
563 /*
564 * We check if the path is a sub-path of a subsequent pathname
565 * first, since removing those will not change the position
566 * in the array
567 */
568 int retval = has_file_name(ce, pos, ok_to_replace);
569 /*
570 * Then check if the path might have a clashing sub-directory
571 * before it.
572 */
573 return retval + has_dir_name(ce, pos, ok_to_replace);
0f1e4f04
JH
574}
575
192268c1 576int add_cache_entry(struct cache_entry *ce, int option)
197ee8c9
LT
577{
578 int pos;
192268c1
JH
579 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
580 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
b155725d 581 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
5f73076c 582
972d1bb0 583 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
197ee8c9 584
3e09cdfd 585 /* existing match? Just replace it. */
76e7f4ec 586 if (pos >= 0) {
ee267527 587 active_cache_changed = 1;
76e7f4ec 588 active_cache[pos] = ce;
197ee8c9
LT
589 return 0;
590 }
76e7f4ec 591 pos = -pos-1;
197ee8c9 592
7b937ca3
LT
593 /*
594 * Inserting a merged entry ("stage 0") into the index
595 * will always replace all non-merged entries..
596 */
597 if (pos < active_nr && ce_stage(ce) == 0) {
dbbce55b 598 while (ce_same_name(active_cache[pos], ce)) {
7b937ca3 599 ok_to_add = 1;
dbbce55b 600 if (!remove_cache_entry_at(pos))
7b937ca3
LT
601 break;
602 }
603 }
604
121481ab
LT
605 if (!ok_to_add)
606 return -1;
8dcf39c4
LT
607 if (!verify_path(ce->name))
608 return -1;
121481ab 609
3e09cdfd
JH
610 if (!skip_df_check &&
611 check_file_directory_conflict(ce, pos, ok_to_replace)) {
192268c1 612 if (!ok_to_replace)
790fa0e2 613 return error("'%s' appears as both a file and as a directory", ce->name);
972d1bb0 614 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
192268c1
JH
615 pos = -pos-1;
616 }
0f1e4f04 617
197ee8c9
LT
618 /* Make sure the array is big enough .. */
619 if (active_nr == active_alloc) {
620 active_alloc = alloc_nr(active_alloc);
812666c8 621 active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
197ee8c9
LT
622 }
623
624 /* Add it in.. */
625 active_nr++;
626 if (active_nr > pos)
627 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
628 active_cache[pos] = ce;
ee267527 629 active_cache_changed = 1;
197ee8c9
LT
630 return 0;
631}
632
405e5b2f
LT
633/*
634 * "refresh" does not calculate a new sha1 file or bring the
635 * cache up-to-date for mode/content changes. But what it
636 * _does_ do is to "re-match" the stat information of a file
637 * with the cache, so that you can refresh the cache for a
638 * file that hasn't been changed but where the stat entry is
639 * out of date.
640 *
641 * For example, you'd want to do this after doing a "git-read-tree",
642 * to link up the stat cache details with the proper files.
643 */
ec0cc704 644static struct cache_entry *refresh_cache_ent(struct cache_entry *ce, int really, int *err)
405e5b2f
LT
645{
646 struct stat st;
647 struct cache_entry *updated;
648 int changed, size;
649
8fd2cb40 650 if (lstat(ce->name, &st) < 0) {
ec0cc704
JH
651 if (err)
652 *err = errno;
8fd2cb40
JS
653 return NULL;
654 }
405e5b2f
LT
655
656 changed = ce_match_stat(ce, &st, really);
657 if (!changed) {
658 if (really && assume_unchanged &&
659 !(ce->ce_flags & htons(CE_VALID)))
660 ; /* mark this one VALID again */
661 else
8fd2cb40 662 return ce;
405e5b2f
LT
663 }
664
8fd2cb40 665 if (ce_modified(ce, &st, really)) {
ec0cc704
JH
666 if (err)
667 *err = EINVAL;
8fd2cb40
JS
668 return NULL;
669 }
405e5b2f
LT
670
671 size = ce_size(ce);
672 updated = xmalloc(size);
673 memcpy(updated, ce, size);
674 fill_stat_cache_info(updated, &st);
675
676 /* In this case, if really is not set, we should leave
677 * CE_VALID bit alone. Otherwise, paths marked with
678 * --no-assume-unchanged (i.e. things to be edited) will
679 * reacquire CE_VALID bit automatically, which is not
680 * really what we want.
681 */
682 if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID)))
683 updated->ce_flags &= ~htons(CE_VALID);
684
685 return updated;
686}
687
688int refresh_cache(unsigned int flags)
689{
690 int i;
691 int has_errors = 0;
692 int really = (flags & REFRESH_REALLY) != 0;
693 int allow_unmerged = (flags & REFRESH_UNMERGED) != 0;
694 int quiet = (flags & REFRESH_QUIET) != 0;
695 int not_new = (flags & REFRESH_IGNORE_MISSING) != 0;
696
697 for (i = 0; i < active_nr; i++) {
698 struct cache_entry *ce, *new;
ec0cc704
JH
699 int cache_errno = 0;
700
405e5b2f
LT
701 ce = active_cache[i];
702 if (ce_stage(ce)) {
703 while ((i < active_nr) &&
704 ! strcmp(active_cache[i]->name, ce->name))
705 i++;
706 i--;
707 if (allow_unmerged)
708 continue;
709 printf("%s: needs merge\n", ce->name);
710 has_errors = 1;
711 continue;
712 }
713
ec0cc704 714 new = refresh_cache_ent(ce, really, &cache_errno);
8fd2cb40 715 if (new == ce)
405e5b2f 716 continue;
8fd2cb40
JS
717 if (!new) {
718 if (not_new && cache_errno == ENOENT)
405e5b2f 719 continue;
8fd2cb40 720 if (really && cache_errno == EINVAL) {
405e5b2f
LT
721 /* If we are doing --really-refresh that
722 * means the index is not valid anymore.
723 */
724 ce->ce_flags &= ~htons(CE_VALID);
725 active_cache_changed = 1;
726 }
727 if (quiet)
728 continue;
729 printf("%s: needs update\n", ce->name);
730 has_errors = 1;
731 continue;
732 }
733 active_cache_changed = 1;
734 /* You can NOT just free active_cache[i] here, since it
735 * might not be necessarily malloc()ed but can also come
736 * from mmap(). */
737 active_cache[i] = new;
738 }
739 return has_errors;
740}
741
ec0cc704
JH
742struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really)
743{
744 return refresh_cache_ent(ce, really, NULL);
745}
746
bad68ec9 747static int verify_hdr(struct cache_header *hdr, unsigned long size)
e83c5163
LT
748{
749 SHA_CTX c;
bad68ec9 750 unsigned char sha1[20];
e83c5163 751
ccc4feb5 752 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
e83c5163 753 return error("bad signature");
ca9be054
LT
754 if (hdr->hdr_version != htonl(2))
755 return error("bad index version");
e83c5163 756 SHA1_Init(&c);
ca9be054 757 SHA1_Update(&c, hdr, size - 20);
e83c5163 758 SHA1_Final(sha1, &c);
a89fccd2 759 if (hashcmp(sha1, (unsigned char *)hdr + size - 20))
ca9be054 760 return error("bad index file sha1 signature");
e83c5163
LT
761 return 0;
762}
763
bad68ec9
JH
764static int read_index_extension(const char *ext, void *data, unsigned long sz)
765{
766 switch (CACHE_EXT(ext)) {
767 case CACHE_EXT_TREE:
768 active_cache_tree = cache_tree_read(data, sz);
769 break;
770 default:
771 if (*ext < 'A' || 'Z' < *ext)
772 return error("index uses %.4s extension, which we do not understand",
773 ext);
774 fprintf(stderr, "ignoring %.4s extension\n", ext);
775 break;
776 }
777 return 0;
778}
779
780int read_cache(void)
8fd2cb40
JS
781{
782 return read_cache_from(get_index_file());
783}
784
785/* remember to discard_cache() before reading a different cache! */
786int read_cache_from(const char *path)
e83c5163
LT
787{
788 int fd, i;
789 struct stat st;
8fd2cb40 790 unsigned long offset;
e83c5163
LT
791 struct cache_header *hdr;
792
793 errno = EBUSY;
8fd2cb40 794 if (cache_mmap)
5d1a5c02
LT
795 return active_nr;
796
e83c5163 797 errno = ENOENT;
29e4d363 798 index_file_timestamp = 0;
8fd2cb40 799 fd = open(path, O_RDONLY);
5d1a5c02
LT
800 if (fd < 0) {
801 if (errno == ENOENT)
802 return 0;
803 die("index file open failed (%s)", strerror(errno));
804 }
e83c5163 805
e83c5163 806 if (!fstat(fd, &st)) {
dc49cd76 807 cache_mmap_size = xsize_t(st.st_size);
e83c5163 808 errno = EINVAL;
8fd2cb40 809 if (cache_mmap_size >= sizeof(struct cache_header) + 20)
c4712e45 810 cache_mmap = xmmap(NULL, cache_mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
5fe5c830
SP
811 else
812 die("index file smaller than expected");
813 } else
814 die("cannot stat the open index (%s)", strerror(errno));
e83c5163 815 close(fd);
e83c5163 816
8fd2cb40
JS
817 hdr = cache_mmap;
818 if (verify_hdr(hdr, cache_mmap_size) < 0)
e83c5163
LT
819 goto unmap;
820
ccc4feb5 821 active_nr = ntohl(hdr->hdr_entries);
e83c5163 822 active_alloc = alloc_nr(active_nr);
28cc4ab4 823 active_cache = xcalloc(active_alloc, sizeof(struct cache_entry *));
e83c5163
LT
824
825 offset = sizeof(*hdr);
ccc4feb5 826 for (i = 0; i < active_nr; i++) {
8fd2cb40 827 struct cache_entry *ce = (struct cache_entry *) ((char *) cache_mmap + offset);
e83c5163
LT
828 offset = offset + ce_size(ce);
829 active_cache[i] = ce;
830 }
29e4d363 831 index_file_timestamp = st.st_mtime;
8fd2cb40 832 while (offset <= cache_mmap_size - 20 - 8) {
bad68ec9
JH
833 /* After an array of active_nr index entries,
834 * there can be arbitrary number of extended
835 * sections, each of which is prefixed with
836 * extension name (4-byte) and section length
837 * in 4-byte network byte order.
838 */
839 unsigned long extsize;
8fd2cb40 840 memcpy(&extsize, (char *) cache_mmap + offset + 4, 4);
bad68ec9 841 extsize = ntohl(extsize);
8fd2cb40
JS
842 if (read_index_extension(((const char *) cache_mmap) + offset,
843 (char *) cache_mmap + offset + 8,
1d7f171c 844 extsize) < 0)
bad68ec9
JH
845 goto unmap;
846 offset += 8;
847 offset += extsize;
848 }
e83c5163
LT
849 return active_nr;
850
851unmap:
8fd2cb40 852 munmap(cache_mmap, cache_mmap_size);
e83c5163 853 errno = EINVAL;
5d1a5c02 854 die("index file corrupt");
e83c5163
LT
855}
856
a6e8a767 857int discard_cache(void)
6d297f81
JS
858{
859 int ret;
860
4147d801
JS
861 active_nr = active_cache_changed = 0;
862 index_file_timestamp = 0;
863 cache_tree_free(&active_cache_tree);
6d297f81
JS
864 if (cache_mmap == NULL)
865 return 0;
866 ret = munmap(cache_mmap, cache_mmap_size);
867 cache_mmap = NULL;
868 cache_mmap_size = 0;
6d297f81
JS
869
870 /* no need to throw away allocated active_cache */
871 return ret;
872}
873
4990aadc 874#define WRITE_BUFFER_SIZE 8192
bf0f910d 875static unsigned char write_buffer[WRITE_BUFFER_SIZE];
4990aadc
LT
876static unsigned long write_buffer_len;
877
6015c28b
JH
878static int ce_write_flush(SHA_CTX *context, int fd)
879{
880 unsigned int buffered = write_buffer_len;
881 if (buffered) {
882 SHA1_Update(context, write_buffer, buffered);
93822c22 883 if (write_in_full(fd, write_buffer, buffered) != buffered)
6015c28b
JH
884 return -1;
885 write_buffer_len = 0;
886 }
887 return 0;
888}
889
ca9be054 890static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
4990aadc
LT
891{
892 while (len) {
893 unsigned int buffered = write_buffer_len;
894 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
895 if (partial > len)
896 partial = len;
897 memcpy(write_buffer + buffered, data, partial);
898 buffered += partial;
899 if (buffered == WRITE_BUFFER_SIZE) {
6015c28b
JH
900 write_buffer_len = buffered;
901 if (ce_write_flush(context, fd))
4990aadc
LT
902 return -1;
903 buffered = 0;
904 }
905 write_buffer_len = buffered;
906 len -= partial;
1d7f171c 907 data = (char *) data + partial;
4990aadc
LT
908 }
909 return 0;
910}
911
bad68ec9 912static int write_index_ext_header(SHA_CTX *context, int fd,
ac58c7b1 913 unsigned int ext, unsigned int sz)
bad68ec9
JH
914{
915 ext = htonl(ext);
916 sz = htonl(sz);
968a1d65
DR
917 return ((ce_write(context, fd, &ext, 4) < 0) ||
918 (ce_write(context, fd, &sz, 4) < 0)) ? -1 : 0;
bad68ec9
JH
919}
920
921static int ce_flush(SHA_CTX *context, int fd)
4990aadc
LT
922{
923 unsigned int left = write_buffer_len;
ca9be054 924
4990aadc
LT
925 if (left) {
926 write_buffer_len = 0;
ca9be054 927 SHA1_Update(context, write_buffer, left);
4990aadc 928 }
ca9be054 929
2c865d9a
QH
930 /* Flush first if not enough space for SHA1 signature */
931 if (left + 20 > WRITE_BUFFER_SIZE) {
93822c22 932 if (write_in_full(fd, write_buffer, left) != left)
2c865d9a
QH
933 return -1;
934 left = 0;
935 }
936
ca9be054 937 /* Append the SHA1 signature at the end */
bad68ec9 938 SHA1_Final(write_buffer + left, context);
ca9be054 939 left += 20;
93822c22 940 return (write_in_full(fd, write_buffer, left) != left) ? -1 : 0;
4990aadc
LT
941}
942
407c8eb0
JH
943static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
944{
945 /*
946 * The only thing we care about in this function is to smudge the
947 * falsely clean entry due to touch-update-touch race, so we leave
948 * everything else as they are. We are called for entries whose
949 * ce_mtime match the index file mtime.
950 */
951 struct stat st;
952
953 if (lstat(ce->name, &st) < 0)
954 return;
955 if (ce_match_stat_basic(ce, &st))
956 return;
957 if (ce_modified_check_fs(ce, &st)) {
4b3511b0
JH
958 /* This is "racily clean"; smudge it. Note that this
959 * is a tricky code. At first glance, it may appear
960 * that it can break with this sequence:
961 *
962 * $ echo xyzzy >frotz
963 * $ git-update-index --add frotz
964 * $ : >frotz
965 * $ sleep 3
966 * $ echo filfre >nitfol
967 * $ git-update-index --add nitfol
968 *
b7e58b17 969 * but it does not. When the second update-index runs,
4b3511b0
JH
970 * it notices that the entry "frotz" has the same timestamp
971 * as index, and if we were to smudge it by resetting its
972 * size to zero here, then the object name recorded
973 * in index is the 6-byte file but the cached stat information
974 * becomes zero --- which would then match what we would
975 * obtain from the filesystem next time we stat("frotz").
976 *
977 * However, the second update-index, before calling
978 * this function, notices that the cached size is 6
979 * bytes and what is on the filesystem is an empty
980 * file, and never calls us, so the cached size information
981 * for "frotz" stays 6 which does not match the filesystem.
982 */
407c8eb0
JH
983 ce->ce_size = htonl(0);
984 }
985}
986
bad68ec9 987int write_cache(int newfd, struct cache_entry **cache, int entries)
197ee8c9
LT
988{
989 SHA_CTX c;
990 struct cache_header hdr;
0fc82cff 991 int i, removed;
025a0709
JH
992
993 for (i = removed = 0; i < entries; i++)
994 if (!cache[i]->ce_mode)
995 removed++;
197ee8c9 996
ccc4feb5 997 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
ca9be054 998 hdr.hdr_version = htonl(2);
025a0709 999 hdr.hdr_entries = htonl(entries - removed);
197ee8c9
LT
1000
1001 SHA1_Init(&c);
ca9be054 1002 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
197ee8c9
LT
1003 return -1;
1004
1005 for (i = 0; i < entries; i++) {
1006 struct cache_entry *ce = cache[i];
aa16021e
LT
1007 if (!ce->ce_mode)
1008 continue;
6015c28b
JH
1009 if (index_file_timestamp &&
1010 index_file_timestamp <= ntohl(ce->ce_mtime.sec))
407c8eb0 1011 ce_smudge_racily_clean_entry(ce);
ca9be054 1012 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
197ee8c9
LT
1013 return -1;
1014 }
1af1c2b6 1015
bad68ec9
JH
1016 /* Write extension data here */
1017 if (active_cache_tree) {
1018 unsigned long sz;
1019 void *data = cache_tree_write(active_cache_tree, &sz);
1020 if (data &&
1021 !write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sz) &&
1022 !ce_write(&c, newfd, data, sz))
2cdf9509 1023 free(data);
bad68ec9
JH
1024 else {
1025 free(data);
1026 return -1;
1027 }
1028 }
1029 return ce_flush(&c, newfd);
197ee8c9 1030}