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