]> git.ipfire.org Git - thirdparty/git.git/blame - read-cache.c
git-blame: prevent argument parsing segfault
[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 27int cache_errno;
8fd2cb40 28
96f1e58f
DR
29static void *cache_mmap;
30static size_t cache_mmap_size;
8fd2cb40 31
415e96c8
JH
32/*
33 * This only updates the "non-critical" parts of the directory
34 * cache, ie the parts that aren't tracked by GIT, and only used
35 * to validate the cache.
36 */
37void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
38{
39 ce->ce_ctime.sec = htonl(st->st_ctime);
40 ce->ce_mtime.sec = htonl(st->st_mtime);
2cb45e95 41#ifdef USE_NSEC
415e96c8
JH
42 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
43 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
44#endif
45 ce->ce_dev = htonl(st->st_dev);
46 ce->ce_ino = htonl(st->st_ino);
47 ce->ce_uid = htonl(st->st_uid);
48 ce->ce_gid = htonl(st->st_gid);
49 ce->ce_size = htonl(st->st_size);
5f73076c
JH
50
51 if (assume_unchanged)
52 ce->ce_flags |= htons(CE_VALID);
415e96c8
JH
53}
54
29e4d363
JH
55static int ce_compare_data(struct cache_entry *ce, struct stat *st)
56{
57 int match = -1;
58 int fd = open(ce->name, O_RDONLY);
59
60 if (fd >= 0) {
61 unsigned char sha1[20];
62 if (!index_fd(sha1, fd, st, 0, NULL))
a89fccd2 63 match = hashcmp(sha1, ce->sha1);
7f8508e8 64 /* index_fd() closed the file descriptor already */
29e4d363
JH
65 }
66 return match;
67}
68
69static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size)
70{
71 int match = -1;
72 char *target;
73 void *buffer;
74 unsigned long size;
75 char type[10];
76 int len;
77
78 target = xmalloc(expected_size);
79 len = readlink(ce->name, target, expected_size);
80 if (len != expected_size) {
81 free(target);
82 return -1;
83 }
84 buffer = read_sha1_file(ce->sha1, type, &size);
85 if (!buffer) {
86 free(target);
87 return -1;
88 }
89 if (size == expected_size)
90 match = memcmp(buffer, target, size);
91 free(buffer);
92 free(target);
93 return match;
94}
95
96static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)
97{
98 switch (st->st_mode & S_IFMT) {
99 case S_IFREG:
100 if (ce_compare_data(ce, st))
101 return DATA_CHANGED;
102 break;
103 case S_IFLNK:
104 if (ce_compare_link(ce, st->st_size))
105 return DATA_CHANGED;
106 break;
107 default:
108 return TYPE_CHANGED;
109 }
110 return 0;
111}
112
407c8eb0 113static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st)
734aab75
LT
114{
115 unsigned int changed = 0;
116
8ae0a8c5
KS
117 switch (ntohl(ce->ce_mode) & S_IFMT) {
118 case S_IFREG:
119 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
3e09cdfd
JH
120 /* We consider only the owner x bit to be relevant for
121 * "mode changes"
122 */
123 if (trust_executable_bit &&
124 (0100 & (ntohl(ce->ce_mode) ^ st->st_mode)))
ffbe1add 125 changed |= MODE_CHANGED;
8ae0a8c5
KS
126 break;
127 case S_IFLNK:
128 changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0;
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
11be42a4
JS
328int add_file_to_index(const char *path, int verbose)
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
347 ce->ce_mode = create_ce_mode(st.st_mode);
348 if (!trust_executable_bit) {
349 /* If there is an existing entry, pick the mode bits
2bbaaed9 350 * from it, otherwise assume unexecutable.
11be42a4
JS
351 */
352 int pos = cache_name_pos(path, namelen);
353 if (pos >= 0)
354 ce->ce_mode = active_cache[pos]->ce_mode;
2bbaaed9
JH
355 else if (S_ISREG(st.st_mode))
356 ce->ce_mode = create_ce_mode(S_IFREG | 0666);
11be42a4
JS
357 }
358
359 if (index_path(ce->sha1, path, &st, 1))
360 die("unable to index file %s", path);
c33ab0dd 361 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
11be42a4
JS
362 die("unable to add %s to index",path);
363 if (verbose)
364 printf("add '%s'\n", path);
365 cache_tree_invalidate_path(active_cache_tree, path);
366 return 0;
367}
368
dbbce55b 369int ce_same_name(struct cache_entry *a, struct cache_entry *b)
7b937ca3
LT
370{
371 int len = ce_namelen(a);
372 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
373}
374
c0fd1f51
LT
375int ce_path_match(const struct cache_entry *ce, const char **pathspec)
376{
377 const char *match, *name;
378 int len;
379
380 if (!pathspec)
381 return 1;
382
383 len = ce_namelen(ce);
384 name = ce->name;
385 while ((match = *pathspec++) != NULL) {
386 int matchlen = strlen(match);
387 if (matchlen > len)
388 continue;
389 if (memcmp(name, match, matchlen))
390 continue;
391 if (matchlen && name[matchlen-1] == '/')
392 return 1;
393 if (name[matchlen] == '/' || !name[matchlen])
394 return 1;
f332726e
LT
395 if (!matchlen)
396 return 1;
c0fd1f51
LT
397 }
398 return 0;
399}
400
8dcf39c4
LT
401/*
402 * We fundamentally don't like some paths: we don't want
403 * dot or dot-dot anywhere, and for obvious reasons don't
404 * want to recurse into ".git" either.
405 *
406 * Also, we don't want double slashes or slashes at the
407 * end that can make pathnames ambiguous.
408 */
409static int verify_dotfile(const char *rest)
410{
411 /*
412 * The first character was '.', but that
413 * has already been discarded, we now test
414 * the rest.
415 */
416 switch (*rest) {
417 /* "." is not allowed */
418 case '\0': case '/':
419 return 0;
420
421 /*
422 * ".git" followed by NUL or slash is bad. This
423 * shares the path end test with the ".." case.
424 */
425 case 'g':
426 if (rest[1] != 'i')
427 break;
428 if (rest[2] != 't')
429 break;
430 rest += 2;
431 /* fallthrough */
432 case '.':
433 if (rest[1] == '\0' || rest[1] == '/')
434 return 0;
435 }
436 return 1;
437}
438
439int verify_path(const char *path)
440{
441 char c;
442
443 goto inside;
444 for (;;) {
445 if (!c)
446 return 1;
447 if (c == '/') {
448inside:
449 c = *path++;
450 switch (c) {
451 default:
452 continue;
453 case '/': case '\0':
454 break;
455 case '.':
456 if (verify_dotfile(path))
457 continue;
458 }
459 return 0;
460 }
461 c = *path++;
462 }
463}
464
12676608
LT
465/*
466 * Do we have another file that has the beginning components being a
467 * proper superset of the name we're trying to add?
0f1e4f04 468 */
12676608 469static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replace)
0f1e4f04 470{
12676608
LT
471 int retval = 0;
472 int len = ce_namelen(ce);
b155725d 473 int stage = ce_stage(ce);
12676608 474 const char *name = ce->name;
0f1e4f04 475
12676608
LT
476 while (pos < active_nr) {
477 struct cache_entry *p = active_cache[pos++];
0f1e4f04 478
12676608 479 if (len >= ce_namelen(p))
0f1e4f04 480 break;
12676608
LT
481 if (memcmp(name, p->name, len))
482 break;
b155725d
JH
483 if (ce_stage(p) != stage)
484 continue;
12676608
LT
485 if (p->name[len] != '/')
486 continue;
12676608
LT
487 retval = -1;
488 if (!ok_to_replace)
489 break;
490 remove_cache_entry_at(--pos);
0f1e4f04 491 }
12676608
LT
492 return retval;
493}
0f1e4f04 494
12676608
LT
495/*
496 * Do we have another file with a pathname that is a proper
497 * subset of the name we're trying to add?
498 */
499static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace)
500{
501 int retval = 0;
b155725d 502 int stage = ce_stage(ce);
12676608
LT
503 const char *name = ce->name;
504 const char *slash = name + ce_namelen(ce);
0f1e4f04 505
12676608
LT
506 for (;;) {
507 int len;
0f1e4f04 508
12676608
LT
509 for (;;) {
510 if (*--slash == '/')
511 break;
512 if (slash <= ce->name)
513 return retval;
514 }
515 len = slash - name;
0f1e4f04 516
b155725d 517 pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage)));
12676608
LT
518 if (pos >= 0) {
519 retval = -1;
81a361be 520 if (!ok_to_replace)
12676608 521 break;
dbbce55b 522 remove_cache_entry_at(pos);
12676608
LT
523 continue;
524 }
525
526 /*
527 * Trivial optimization: if we find an entry that
528 * already matches the sub-directory, then we know
b155725d 529 * we're ok, and we can exit.
12676608
LT
530 */
531 pos = -pos-1;
b155725d 532 while (pos < active_nr) {
12676608 533 struct cache_entry *p = active_cache[pos];
b155725d
JH
534 if ((ce_namelen(p) <= len) ||
535 (p->name[len] != '/') ||
536 memcmp(p->name, name, len))
537 break; /* not our subdirectory */
538 if (ce_stage(p) == stage)
539 /* p is at the same stage as our entry, and
540 * is a subdirectory of what we are looking
541 * at, so we cannot have conflicts at our
542 * level or anything shorter.
543 */
544 return retval;
545 pos++;
192268c1 546 }
0f1e4f04 547 }
12676608
LT
548 return retval;
549}
550
551/* We may be in a situation where we already have path/file and path
552 * is being added, or we already have path and path/file is being
553 * added. Either one would result in a nonsense tree that has path
554 * twice when git-write-tree tries to write it out. Prevent it.
555 *
556 * If ok-to-replace is specified, we remove the conflicting entries
557 * from the cache so the caller should recompute the insert position.
558 * When this happens, we return non-zero.
559 */
560static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace)
561{
562 /*
563 * We check if the path is a sub-path of a subsequent pathname
564 * first, since removing those will not change the position
565 * in the array
566 */
567 int retval = has_file_name(ce, pos, ok_to_replace);
568 /*
569 * Then check if the path might have a clashing sub-directory
570 * before it.
571 */
572 return retval + has_dir_name(ce, pos, ok_to_replace);
0f1e4f04
JH
573}
574
192268c1 575int add_cache_entry(struct cache_entry *ce, int option)
197ee8c9
LT
576{
577 int pos;
192268c1
JH
578 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
579 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
b155725d 580 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
5f73076c 581
972d1bb0 582 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
197ee8c9 583
3e09cdfd 584 /* existing match? Just replace it. */
76e7f4ec 585 if (pos >= 0) {
ee267527 586 active_cache_changed = 1;
76e7f4ec 587 active_cache[pos] = ce;
197ee8c9
LT
588 return 0;
589 }
76e7f4ec 590 pos = -pos-1;
197ee8c9 591
7b937ca3
LT
592 /*
593 * Inserting a merged entry ("stage 0") into the index
594 * will always replace all non-merged entries..
595 */
596 if (pos < active_nr && ce_stage(ce) == 0) {
dbbce55b 597 while (ce_same_name(active_cache[pos], ce)) {
7b937ca3 598 ok_to_add = 1;
dbbce55b 599 if (!remove_cache_entry_at(pos))
7b937ca3
LT
600 break;
601 }
602 }
603
121481ab
LT
604 if (!ok_to_add)
605 return -1;
8dcf39c4
LT
606 if (!verify_path(ce->name))
607 return -1;
121481ab 608
3e09cdfd
JH
609 if (!skip_df_check &&
610 check_file_directory_conflict(ce, pos, ok_to_replace)) {
192268c1 611 if (!ok_to_replace)
790fa0e2 612 return error("'%s' appears as both a file and as a directory", ce->name);
972d1bb0 613 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
192268c1
JH
614 pos = -pos-1;
615 }
0f1e4f04 616
197ee8c9
LT
617 /* Make sure the array is big enough .. */
618 if (active_nr == active_alloc) {
619 active_alloc = alloc_nr(active_alloc);
812666c8 620 active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
197ee8c9
LT
621 }
622
623 /* Add it in.. */
624 active_nr++;
625 if (active_nr > pos)
626 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
627 active_cache[pos] = ce;
ee267527 628 active_cache_changed = 1;
197ee8c9
LT
629 return 0;
630}
631
405e5b2f
LT
632/*
633 * "refresh" does not calculate a new sha1 file or bring the
634 * cache up-to-date for mode/content changes. But what it
635 * _does_ do is to "re-match" the stat information of a file
636 * with the cache, so that you can refresh the cache for a
637 * file that hasn't been changed but where the stat entry is
638 * out of date.
639 *
640 * For example, you'd want to do this after doing a "git-read-tree",
641 * to link up the stat cache details with the proper files.
642 */
8fd2cb40 643struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really)
405e5b2f
LT
644{
645 struct stat st;
646 struct cache_entry *updated;
647 int changed, size;
648
8fd2cb40
JS
649 if (lstat(ce->name, &st) < 0) {
650 cache_errno = errno;
651 return NULL;
652 }
405e5b2f
LT
653
654 changed = ce_match_stat(ce, &st, really);
655 if (!changed) {
656 if (really && assume_unchanged &&
657 !(ce->ce_flags & htons(CE_VALID)))
658 ; /* mark this one VALID again */
659 else
8fd2cb40 660 return ce;
405e5b2f
LT
661 }
662
8fd2cb40
JS
663 if (ce_modified(ce, &st, really)) {
664 cache_errno = EINVAL;
665 return NULL;
666 }
405e5b2f
LT
667
668 size = ce_size(ce);
669 updated = xmalloc(size);
670 memcpy(updated, ce, size);
671 fill_stat_cache_info(updated, &st);
672
673 /* In this case, if really is not set, we should leave
674 * CE_VALID bit alone. Otherwise, paths marked with
675 * --no-assume-unchanged (i.e. things to be edited) will
676 * reacquire CE_VALID bit automatically, which is not
677 * really what we want.
678 */
679 if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID)))
680 updated->ce_flags &= ~htons(CE_VALID);
681
682 return updated;
683}
684
685int refresh_cache(unsigned int flags)
686{
687 int i;
688 int has_errors = 0;
689 int really = (flags & REFRESH_REALLY) != 0;
690 int allow_unmerged = (flags & REFRESH_UNMERGED) != 0;
691 int quiet = (flags & REFRESH_QUIET) != 0;
692 int not_new = (flags & REFRESH_IGNORE_MISSING) != 0;
693
694 for (i = 0; i < active_nr; i++) {
695 struct cache_entry *ce, *new;
696 ce = active_cache[i];
697 if (ce_stage(ce)) {
698 while ((i < active_nr) &&
699 ! strcmp(active_cache[i]->name, ce->name))
700 i++;
701 i--;
702 if (allow_unmerged)
703 continue;
704 printf("%s: needs merge\n", ce->name);
705 has_errors = 1;
706 continue;
707 }
708
8fd2cb40
JS
709 new = refresh_cache_entry(ce, really);
710 if (new == ce)
405e5b2f 711 continue;
8fd2cb40
JS
712 if (!new) {
713 if (not_new && cache_errno == ENOENT)
405e5b2f 714 continue;
8fd2cb40 715 if (really && cache_errno == EINVAL) {
405e5b2f
LT
716 /* If we are doing --really-refresh that
717 * means the index is not valid anymore.
718 */
719 ce->ce_flags &= ~htons(CE_VALID);
720 active_cache_changed = 1;
721 }
722 if (quiet)
723 continue;
724 printf("%s: needs update\n", ce->name);
725 has_errors = 1;
726 continue;
727 }
728 active_cache_changed = 1;
729 /* You can NOT just free active_cache[i] here, since it
730 * might not be necessarily malloc()ed but can also come
731 * from mmap(). */
732 active_cache[i] = new;
733 }
734 return has_errors;
735}
736
bad68ec9 737static int verify_hdr(struct cache_header *hdr, unsigned long size)
e83c5163
LT
738{
739 SHA_CTX c;
bad68ec9 740 unsigned char sha1[20];
e83c5163 741
ccc4feb5 742 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
e83c5163 743 return error("bad signature");
ca9be054
LT
744 if (hdr->hdr_version != htonl(2))
745 return error("bad index version");
e83c5163 746 SHA1_Init(&c);
ca9be054 747 SHA1_Update(&c, hdr, size - 20);
e83c5163 748 SHA1_Final(sha1, &c);
a89fccd2 749 if (hashcmp(sha1, (unsigned char *)hdr + size - 20))
ca9be054 750 return error("bad index file sha1 signature");
e83c5163
LT
751 return 0;
752}
753
bad68ec9
JH
754static int read_index_extension(const char *ext, void *data, unsigned long sz)
755{
756 switch (CACHE_EXT(ext)) {
757 case CACHE_EXT_TREE:
758 active_cache_tree = cache_tree_read(data, sz);
759 break;
760 default:
761 if (*ext < 'A' || 'Z' < *ext)
762 return error("index uses %.4s extension, which we do not understand",
763 ext);
764 fprintf(stderr, "ignoring %.4s extension\n", ext);
765 break;
766 }
767 return 0;
768}
769
770int read_cache(void)
8fd2cb40
JS
771{
772 return read_cache_from(get_index_file());
773}
774
775/* remember to discard_cache() before reading a different cache! */
776int read_cache_from(const char *path)
e83c5163
LT
777{
778 int fd, i;
779 struct stat st;
8fd2cb40 780 unsigned long offset;
e83c5163
LT
781 struct cache_header *hdr;
782
783 errno = EBUSY;
8fd2cb40 784 if (cache_mmap)
5d1a5c02
LT
785 return active_nr;
786
e83c5163 787 errno = ENOENT;
29e4d363 788 index_file_timestamp = 0;
8fd2cb40 789 fd = open(path, O_RDONLY);
5d1a5c02
LT
790 if (fd < 0) {
791 if (errno == ENOENT)
792 return 0;
793 die("index file open failed (%s)", strerror(errno));
794 }
e83c5163 795
e83c5163 796 if (!fstat(fd, &st)) {
8fd2cb40 797 cache_mmap_size = st.st_size;
e83c5163 798 errno = EINVAL;
8fd2cb40 799 if (cache_mmap_size >= sizeof(struct cache_header) + 20)
c4712e45 800 cache_mmap = xmmap(NULL, cache_mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
5fe5c830
SP
801 else
802 die("index file smaller than expected");
803 } else
804 die("cannot stat the open index (%s)", strerror(errno));
e83c5163 805 close(fd);
e83c5163 806
8fd2cb40
JS
807 hdr = cache_mmap;
808 if (verify_hdr(hdr, cache_mmap_size) < 0)
e83c5163
LT
809 goto unmap;
810
ccc4feb5 811 active_nr = ntohl(hdr->hdr_entries);
e83c5163 812 active_alloc = alloc_nr(active_nr);
28cc4ab4 813 active_cache = xcalloc(active_alloc, sizeof(struct cache_entry *));
e83c5163
LT
814
815 offset = sizeof(*hdr);
ccc4feb5 816 for (i = 0; i < active_nr; i++) {
8fd2cb40 817 struct cache_entry *ce = (struct cache_entry *) ((char *) cache_mmap + offset);
e83c5163
LT
818 offset = offset + ce_size(ce);
819 active_cache[i] = ce;
820 }
29e4d363 821 index_file_timestamp = st.st_mtime;
8fd2cb40 822 while (offset <= cache_mmap_size - 20 - 8) {
bad68ec9
JH
823 /* After an array of active_nr index entries,
824 * there can be arbitrary number of extended
825 * sections, each of which is prefixed with
826 * extension name (4-byte) and section length
827 * in 4-byte network byte order.
828 */
829 unsigned long extsize;
8fd2cb40 830 memcpy(&extsize, (char *) cache_mmap + offset + 4, 4);
bad68ec9 831 extsize = ntohl(extsize);
8fd2cb40
JS
832 if (read_index_extension(((const char *) cache_mmap) + offset,
833 (char *) cache_mmap + offset + 8,
1d7f171c 834 extsize) < 0)
bad68ec9
JH
835 goto unmap;
836 offset += 8;
837 offset += extsize;
838 }
e83c5163
LT
839 return active_nr;
840
841unmap:
8fd2cb40 842 munmap(cache_mmap, cache_mmap_size);
e83c5163 843 errno = EINVAL;
5d1a5c02 844 die("index file corrupt");
e83c5163
LT
845}
846
a6e8a767 847int discard_cache(void)
6d297f81
JS
848{
849 int ret;
850
4147d801
JS
851 active_nr = active_cache_changed = 0;
852 index_file_timestamp = 0;
853 cache_tree_free(&active_cache_tree);
6d297f81
JS
854 if (cache_mmap == NULL)
855 return 0;
856 ret = munmap(cache_mmap, cache_mmap_size);
857 cache_mmap = NULL;
858 cache_mmap_size = 0;
6d297f81
JS
859
860 /* no need to throw away allocated active_cache */
861 return ret;
862}
863
4990aadc 864#define WRITE_BUFFER_SIZE 8192
bf0f910d 865static unsigned char write_buffer[WRITE_BUFFER_SIZE];
4990aadc
LT
866static unsigned long write_buffer_len;
867
6015c28b
JH
868static int ce_write_flush(SHA_CTX *context, int fd)
869{
870 unsigned int buffered = write_buffer_len;
871 if (buffered) {
872 SHA1_Update(context, write_buffer, buffered);
93822c22 873 if (write_in_full(fd, write_buffer, buffered) != buffered)
6015c28b
JH
874 return -1;
875 write_buffer_len = 0;
876 }
877 return 0;
878}
879
ca9be054 880static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
4990aadc
LT
881{
882 while (len) {
883 unsigned int buffered = write_buffer_len;
884 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
885 if (partial > len)
886 partial = len;
887 memcpy(write_buffer + buffered, data, partial);
888 buffered += partial;
889 if (buffered == WRITE_BUFFER_SIZE) {
6015c28b
JH
890 write_buffer_len = buffered;
891 if (ce_write_flush(context, fd))
4990aadc
LT
892 return -1;
893 buffered = 0;
894 }
895 write_buffer_len = buffered;
896 len -= partial;
1d7f171c 897 data = (char *) data + partial;
4990aadc
LT
898 }
899 return 0;
900}
901
bad68ec9 902static int write_index_ext_header(SHA_CTX *context, int fd,
ac58c7b1 903 unsigned int ext, unsigned int sz)
bad68ec9
JH
904{
905 ext = htonl(ext);
906 sz = htonl(sz);
968a1d65
DR
907 return ((ce_write(context, fd, &ext, 4) < 0) ||
908 (ce_write(context, fd, &sz, 4) < 0)) ? -1 : 0;
bad68ec9
JH
909}
910
911static int ce_flush(SHA_CTX *context, int fd)
4990aadc
LT
912{
913 unsigned int left = write_buffer_len;
ca9be054 914
4990aadc
LT
915 if (left) {
916 write_buffer_len = 0;
ca9be054 917 SHA1_Update(context, write_buffer, left);
4990aadc 918 }
ca9be054 919
2c865d9a
QH
920 /* Flush first if not enough space for SHA1 signature */
921 if (left + 20 > WRITE_BUFFER_SIZE) {
93822c22 922 if (write_in_full(fd, write_buffer, left) != left)
2c865d9a
QH
923 return -1;
924 left = 0;
925 }
926
ca9be054 927 /* Append the SHA1 signature at the end */
bad68ec9 928 SHA1_Final(write_buffer + left, context);
ca9be054 929 left += 20;
93822c22 930 return (write_in_full(fd, write_buffer, left) != left) ? -1 : 0;
4990aadc
LT
931}
932
407c8eb0
JH
933static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
934{
935 /*
936 * The only thing we care about in this function is to smudge the
937 * falsely clean entry due to touch-update-touch race, so we leave
938 * everything else as they are. We are called for entries whose
939 * ce_mtime match the index file mtime.
940 */
941 struct stat st;
942
943 if (lstat(ce->name, &st) < 0)
944 return;
945 if (ce_match_stat_basic(ce, &st))
946 return;
947 if (ce_modified_check_fs(ce, &st)) {
4b3511b0
JH
948 /* This is "racily clean"; smudge it. Note that this
949 * is a tricky code. At first glance, it may appear
950 * that it can break with this sequence:
951 *
952 * $ echo xyzzy >frotz
953 * $ git-update-index --add frotz
954 * $ : >frotz
955 * $ sleep 3
956 * $ echo filfre >nitfol
957 * $ git-update-index --add nitfol
958 *
b7e58b17 959 * but it does not. When the second update-index runs,
4b3511b0
JH
960 * it notices that the entry "frotz" has the same timestamp
961 * as index, and if we were to smudge it by resetting its
962 * size to zero here, then the object name recorded
963 * in index is the 6-byte file but the cached stat information
964 * becomes zero --- which would then match what we would
965 * obtain from the filesystem next time we stat("frotz").
966 *
967 * However, the second update-index, before calling
968 * this function, notices that the cached size is 6
969 * bytes and what is on the filesystem is an empty
970 * file, and never calls us, so the cached size information
971 * for "frotz" stays 6 which does not match the filesystem.
972 */
407c8eb0
JH
973 ce->ce_size = htonl(0);
974 }
975}
976
bad68ec9 977int write_cache(int newfd, struct cache_entry **cache, int entries)
197ee8c9
LT
978{
979 SHA_CTX c;
980 struct cache_header hdr;
0fc82cff 981 int i, removed;
025a0709
JH
982
983 for (i = removed = 0; i < entries; i++)
984 if (!cache[i]->ce_mode)
985 removed++;
197ee8c9 986
ccc4feb5 987 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
ca9be054 988 hdr.hdr_version = htonl(2);
025a0709 989 hdr.hdr_entries = htonl(entries - removed);
197ee8c9
LT
990
991 SHA1_Init(&c);
ca9be054 992 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
197ee8c9
LT
993 return -1;
994
995 for (i = 0; i < entries; i++) {
996 struct cache_entry *ce = cache[i];
aa16021e
LT
997 if (!ce->ce_mode)
998 continue;
6015c28b
JH
999 if (index_file_timestamp &&
1000 index_file_timestamp <= ntohl(ce->ce_mtime.sec))
407c8eb0 1001 ce_smudge_racily_clean_entry(ce);
ca9be054 1002 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
197ee8c9
LT
1003 return -1;
1004 }
1af1c2b6 1005
bad68ec9
JH
1006 /* Write extension data here */
1007 if (active_cache_tree) {
1008 unsigned long sz;
1009 void *data = cache_tree_write(active_cache_tree, &sz);
1010 if (data &&
1011 !write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sz) &&
1012 !ce_write(&c, newfd, data, sz))
2cdf9509 1013 free(data);
bad68ec9
JH
1014 else {
1015 free(data);
1016 return -1;
1017 }
1018 }
1019 return ce_flush(&c, newfd);
197ee8c9 1020}