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