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