]> git.ipfire.org Git - thirdparty/git.git/blame - read-cache.c
Advertise the ability to abort a commit
[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
9cb76b8c
JH
26static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
27{
28 istate->cache[nr] = ce;
96872bc2 29 add_name_hash(istate, ce);
9cb76b8c
JH
30}
31
cf558704
LT
32static void replace_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
33{
34 struct cache_entry *old = istate->cache[nr];
35
96872bc2 36 remove_name_hash(old);
a22c6371 37 set_index_entry(istate, nr, ce);
cf558704
LT
38 istate->cache_changed = 1;
39}
40
81dc2307
PB
41void rename_index_entry_at(struct index_state *istate, int nr, const char *new_name)
42{
43 struct cache_entry *old = istate->cache[nr], *new;
44 int namelen = strlen(new_name);
45
46 new = xmalloc(cache_entry_size(namelen));
47 copy_cache_entry(new, old);
48 new->ce_flags &= ~(CE_STATE_MASK | CE_NAMEMASK);
49 new->ce_flags |= (namelen >= CE_NAMEMASK ? CE_NAMEMASK : namelen);
50 memcpy(new->name, new_name, namelen + 1);
51
52 cache_tree_invalidate_path(istate->cache_tree, old->name);
53 remove_index_entry_at(istate, nr);
54 add_index_entry(istate, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
55}
56
415e96c8
JH
57/*
58 * This only updates the "non-critical" parts of the directory
59 * cache, ie the parts that aren't tracked by GIT, and only used
60 * to validate the cache.
61 */
62void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
63{
7a51ed66
LT
64 ce->ce_ctime = st->st_ctime;
65 ce->ce_mtime = st->st_mtime;
66 ce->ce_dev = st->st_dev;
67 ce->ce_ino = st->st_ino;
68 ce->ce_uid = st->st_uid;
69 ce->ce_gid = st->st_gid;
70 ce->ce_size = st->st_size;
5f73076c
JH
71
72 if (assume_unchanged)
7a51ed66 73 ce->ce_flags |= CE_VALID;
eadb5831
JH
74
75 if (S_ISREG(st->st_mode))
76 ce_mark_uptodate(ce);
415e96c8
JH
77}
78
29e4d363
JH
79static int ce_compare_data(struct cache_entry *ce, struct stat *st)
80{
81 int match = -1;
82 int fd = open(ce->name, O_RDONLY);
83
84 if (fd >= 0) {
85 unsigned char sha1[20];
53bca91a 86 if (!index_fd(sha1, fd, st, 0, OBJ_BLOB, ce->name))
a89fccd2 87 match = hashcmp(sha1, ce->sha1);
7f8508e8 88 /* index_fd() closed the file descriptor already */
29e4d363
JH
89 }
90 return match;
91}
92
dc49cd76 93static int ce_compare_link(struct cache_entry *ce, size_t expected_size)
29e4d363
JH
94{
95 int match = -1;
96 char *target;
97 void *buffer;
98 unsigned long size;
21666f1a 99 enum object_type type;
29e4d363
JH
100 int len;
101
102 target = xmalloc(expected_size);
103 len = readlink(ce->name, target, expected_size);
104 if (len != expected_size) {
105 free(target);
106 return -1;
107 }
21666f1a 108 buffer = read_sha1_file(ce->sha1, &type, &size);
29e4d363
JH
109 if (!buffer) {
110 free(target);
111 return -1;
112 }
113 if (size == expected_size)
114 match = memcmp(buffer, target, size);
115 free(buffer);
116 free(target);
117 return match;
118}
119
f35a6d3b
LT
120static int ce_compare_gitlink(struct cache_entry *ce)
121{
122 unsigned char sha1[20];
123
124 /*
125 * We don't actually require that the .git directory
302b9282 126 * under GITLINK directory be a valid git directory. It
f35a6d3b
LT
127 * might even be missing (in case nobody populated that
128 * sub-project).
129 *
130 * If so, we consider it always to match.
131 */
132 if (resolve_gitlink_ref(ce->name, "HEAD", sha1) < 0)
133 return 0;
134 return hashcmp(sha1, ce->sha1);
135}
136
29e4d363
JH
137static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)
138{
139 switch (st->st_mode & S_IFMT) {
140 case S_IFREG:
141 if (ce_compare_data(ce, st))
142 return DATA_CHANGED;
143 break;
144 case S_IFLNK:
dc49cd76 145 if (ce_compare_link(ce, xsize_t(st->st_size)))
29e4d363
JH
146 return DATA_CHANGED;
147 break;
a8ee75bc 148 case S_IFDIR:
7a51ed66 149 if (S_ISGITLINK(ce->ce_mode))
a8ee75bc 150 return 0;
29e4d363
JH
151 default:
152 return TYPE_CHANGED;
153 }
154 return 0;
155}
156
f49c2c22
LT
157static int is_empty_blob_sha1(const unsigned char *sha1)
158{
159 static const unsigned char empty_blob_sha1[20] = {
160 0xe6,0x9d,0xe2,0x9b,0xb2,0xd1,0xd6,0x43,0x4b,0x8b,
161 0x29,0xae,0x77,0x5a,0xd8,0xc2,0xe4,0x8c,0x53,0x91
162 };
163
164 return !hashcmp(sha1, empty_blob_sha1);
165}
166
407c8eb0 167static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st)
734aab75
LT
168{
169 unsigned int changed = 0;
170
7a51ed66
LT
171 if (ce->ce_flags & CE_REMOVE)
172 return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED;
173
174 switch (ce->ce_mode & S_IFMT) {
8ae0a8c5
KS
175 case S_IFREG:
176 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
3e09cdfd
JH
177 /* We consider only the owner x bit to be relevant for
178 * "mode changes"
179 */
180 if (trust_executable_bit &&
7a51ed66 181 (0100 & (ce->ce_mode ^ st->st_mode)))
ffbe1add 182 changed |= MODE_CHANGED;
8ae0a8c5
KS
183 break;
184 case S_IFLNK:
78a8d641
JS
185 if (!S_ISLNK(st->st_mode) &&
186 (has_symlinks || !S_ISREG(st->st_mode)))
187 changed |= TYPE_CHANGED;
8ae0a8c5 188 break;
302b9282 189 case S_IFGITLINK:
f35a6d3b
LT
190 if (!S_ISDIR(st->st_mode))
191 changed |= TYPE_CHANGED;
192 else if (ce_compare_gitlink(ce))
193 changed |= DATA_CHANGED;
a8ee75bc 194 return changed;
8ae0a8c5 195 default:
7a51ed66 196 die("internal error: ce_mode is %o", ce->ce_mode);
8ae0a8c5 197 }
7a51ed66 198 if (ce->ce_mtime != (unsigned int) st->st_mtime)
ccc4feb5 199 changed |= MTIME_CHANGED;
1ce4790b 200 if (trust_ctime && ce->ce_ctime != (unsigned int) st->st_ctime)
734aab75 201 changed |= CTIME_CHANGED;
ccc4feb5 202
7a51ed66
LT
203 if (ce->ce_uid != (unsigned int) st->st_uid ||
204 ce->ce_gid != (unsigned int) st->st_gid)
734aab75 205 changed |= OWNER_CHANGED;
7a51ed66 206 if (ce->ce_ino != (unsigned int) st->st_ino)
734aab75 207 changed |= INODE_CHANGED;
2cb45e95
LT
208
209#ifdef USE_STDEV
210 /*
211 * st_dev breaks on network filesystems where different
212 * clients will have different views of what "device"
213 * the filesystem is on
214 */
7a51ed66 215 if (ce->ce_dev != (unsigned int) st->st_dev)
2cb45e95
LT
216 changed |= INODE_CHANGED;
217#endif
218
7a51ed66 219 if (ce->ce_size != (unsigned int) st->st_size)
734aab75 220 changed |= DATA_CHANGED;
b0391890 221
f49c2c22
LT
222 /* Racily smudged entry? */
223 if (!ce->ce_size) {
224 if (!is_empty_blob_sha1(ce->sha1))
225 changed |= DATA_CHANGED;
226 }
227
407c8eb0
JH
228 return changed;
229}
230
d1f128b0 231static int is_racy_timestamp(const struct index_state *istate, struct cache_entry *ce)
6d91da6d 232{
050288d5
JH
233 return (!S_ISGITLINK(ce->ce_mode) &&
234 istate->timestamp &&
6d91da6d
JH
235 ((unsigned int)istate->timestamp) <= ce->ce_mtime);
236}
237
d1f128b0 238int ie_match_stat(const struct index_state *istate,
4bd5b7da
JH
239 struct cache_entry *ce, struct stat *st,
240 unsigned int options)
407c8eb0 241{
5f73076c 242 unsigned int changed;
4bd5b7da
JH
243 int ignore_valid = options & CE_MATCH_IGNORE_VALID;
244 int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY;
5f73076c
JH
245
246 /*
247 * If it's marked as always valid in the index, it's
248 * valid whatever the checked-out copy says.
249 */
7a51ed66 250 if (!ignore_valid && (ce->ce_flags & CE_VALID))
5f73076c
JH
251 return 0;
252
253 changed = ce_match_stat_basic(ce, st);
407c8eb0 254
29e4d363
JH
255 /*
256 * Within 1 second of this sequence:
257 * echo xyzzy >file && git-update-index --add file
258 * running this command:
259 * echo frotz >file
260 * would give a falsely clean cache entry. The mtime and
261 * length match the cache, and other stat fields do not change.
262 *
263 * We could detect this at update-index time (the cache entry
264 * being registered/updated records the same time as "now")
265 * and delay the return from git-update-index, but that would
266 * effectively mean we can make at most one commit per second,
267 * which is not acceptable. Instead, we check cache entries
268 * whose mtime are the same as the index file timestamp more
5f73076c 269 * carefully than others.
29e4d363 270 */
6d91da6d 271 if (!changed && is_racy_timestamp(istate, ce)) {
42f77406
JH
272 if (assume_racy_is_modified)
273 changed |= DATA_CHANGED;
274 else
275 changed |= ce_modified_check_fs(ce, st);
276 }
b0391890 277
29e4d363 278 return changed;
b0391890
JH
279}
280
d1f128b0 281int ie_modified(const struct index_state *istate,
4bd5b7da 282 struct cache_entry *ce, struct stat *st, unsigned int options)
b0391890 283{
29e4d363 284 int changed, changed_fs;
4bd5b7da
JH
285
286 changed = ie_match_stat(istate, ce, st, options);
b0391890
JH
287 if (!changed)
288 return 0;
b0391890
JH
289 /*
290 * If the mode or type has changed, there's no point in trying
291 * to refresh the entry - it's not going to match
292 */
293 if (changed & (MODE_CHANGED | TYPE_CHANGED))
294 return changed;
295
296 /* Immediately after read-tree or update-index --cacheinfo,
297 * the length field is zero. For other cases the ce_size
298 * should match the SHA1 recorded in the index entry.
299 */
7a51ed66 300 if ((changed & DATA_CHANGED) && ce->ce_size != 0)
b0391890
JH
301 return changed;
302
29e4d363
JH
303 changed_fs = ce_modified_check_fs(ce, st);
304 if (changed_fs)
305 return changed | changed_fs;
b0391890
JH
306 return 0;
307}
308
958ba6c9
LT
309int base_name_compare(const char *name1, int len1, int mode1,
310 const char *name2, int len2, int mode2)
311{
312 unsigned char c1, c2;
313 int len = len1 < len2 ? len1 : len2;
314 int cmp;
315
316 cmp = memcmp(name1, name2, len);
317 if (cmp)
318 return cmp;
319 c1 = name1[len];
320 c2 = name2[len];
1833a925 321 if (!c1 && S_ISDIR(mode1))
958ba6c9 322 c1 = '/';
1833a925 323 if (!c2 && S_ISDIR(mode2))
958ba6c9
LT
324 c2 = '/';
325 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
326}
327
0ab9e1e8
LT
328/*
329 * df_name_compare() is identical to base_name_compare(), except it
330 * compares conflicting directory/file entries as equal. Note that
331 * while a directory name compares as equal to a regular file, they
332 * then individually compare _differently_ to a filename that has
333 * a dot after the basename (because '\0' < '.' < '/').
334 *
335 * This is used by routines that want to traverse the git namespace
336 * but then handle conflicting entries together when possible.
337 */
338int df_name_compare(const char *name1, int len1, int mode1,
339 const char *name2, int len2, int mode2)
340{
341 int len = len1 < len2 ? len1 : len2, cmp;
342 unsigned char c1, c2;
343
344 cmp = memcmp(name1, name2, len);
345 if (cmp)
346 return cmp;
347 /* Directories and files compare equal (same length, same name) */
348 if (len1 == len2)
349 return 0;
350 c1 = name1[len];
351 if (!c1 && S_ISDIR(mode1))
352 c1 = '/';
353 c2 = name2[len];
354 if (!c2 && S_ISDIR(mode2))
355 c2 = '/';
356 if (c1 == '/' && !c2)
357 return 0;
358 if (c2 == '/' && !c1)
359 return 0;
360 return c1 - c2;
361}
362
95fd5bf8 363int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
eb38c22f 364{
95fd5bf8
LT
365 int len1 = flags1 & CE_NAMEMASK;
366 int len2 = flags2 & CE_NAMEMASK;
eb38c22f
LT
367 int len = len1 < len2 ? len1 : len2;
368 int cmp;
369
370 cmp = memcmp(name1, name2, len);
371 if (cmp)
372 return cmp;
373 if (len1 < len2)
374 return -1;
375 if (len1 > len2)
376 return 1;
5f73076c 377
7b80be15
JH
378 /* Compare stages */
379 flags1 &= CE_STAGEMASK;
380 flags2 &= CE_STAGEMASK;
5f73076c 381
95fd5bf8
LT
382 if (flags1 < flags2)
383 return -1;
384 if (flags1 > flags2)
385 return 1;
eb38c22f
LT
386 return 0;
387}
388
d1f128b0 389int index_name_pos(const struct index_state *istate, const char *name, int namelen)
eb38c22f
LT
390{
391 int first, last;
392
393 first = 0;
4aab5b46 394 last = istate->cache_nr;
eb38c22f
LT
395 while (last > first) {
396 int next = (last + first) >> 1;
4aab5b46 397 struct cache_entry *ce = istate->cache[next];
7a51ed66 398 int cmp = cache_name_compare(name, namelen, ce->name, ce->ce_flags);
eb38c22f 399 if (!cmp)
76e7f4ec 400 return next;
eb38c22f
LT
401 if (cmp < 0) {
402 last = next;
403 continue;
404 }
405 first = next+1;
406 }
76e7f4ec 407 return -first-1;
eb38c22f
LT
408}
409
7b937ca3 410/* Remove entry, return true if there are more entries to go.. */
4aab5b46 411int remove_index_entry_at(struct index_state *istate, int pos)
7b937ca3 412{
cf558704
LT
413 struct cache_entry *ce = istate->cache[pos];
414
96872bc2 415 remove_name_hash(ce);
4aab5b46
JH
416 istate->cache_changed = 1;
417 istate->cache_nr--;
418 if (pos >= istate->cache_nr)
7b937ca3 419 return 0;
4aab5b46
JH
420 memmove(istate->cache + pos,
421 istate->cache + pos + 1,
422 (istate->cache_nr - pos) * sizeof(struct cache_entry *));
7b937ca3
LT
423 return 1;
424}
425
4aab5b46 426int remove_file_from_index(struct index_state *istate, const char *path)
197ee8c9 427{
4aab5b46 428 int pos = index_name_pos(istate, path, strlen(path));
c4e3cca1
JH
429 if (pos < 0)
430 pos = -pos-1;
09d5dc32 431 cache_tree_invalidate_path(istate->cache_tree, path);
4aab5b46
JH
432 while (pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path))
433 remove_index_entry_at(istate, pos);
197ee8c9
LT
434 return 0;
435}
436
20314271
JS
437static int compare_name(struct cache_entry *ce, const char *path, int namelen)
438{
439 return namelen != ce_namelen(ce) || memcmp(path, ce->name, namelen);
440}
441
442static int index_name_pos_also_unmerged(struct index_state *istate,
443 const char *path, int namelen)
444{
445 int pos = index_name_pos(istate, path, namelen);
446 struct cache_entry *ce;
447
448 if (pos >= 0)
449 return pos;
450
451 /* maybe unmerged? */
452 pos = -1 - pos;
453 if (pos >= istate->cache_nr ||
454 compare_name((ce = istate->cache[pos]), path, namelen))
455 return -1;
456
457 /* order of preference: stage 2, 1, 3 */
458 if (ce_stage(ce) == 1 && pos + 1 < istate->cache_nr &&
459 ce_stage((ce = istate->cache[pos + 1])) == 2 &&
460 !compare_name(ce, path, namelen))
461 pos++;
462 return pos;
463}
464
1102952b
LT
465static int different_name(struct cache_entry *ce, struct cache_entry *alias)
466{
467 int len = ce_namelen(ce);
468 return ce_namelen(alias) != len || memcmp(ce->name, alias->name, len);
469}
470
471/*
472 * If we add a filename that aliases in the cache, we will use the
473 * name that we already have - but we don't want to update the same
474 * alias twice, because that implies that there were actually two
475 * different files with aliasing names!
476 *
477 * So we use the CE_ADDED flag to verify that the alias was an old
478 * one before we accept it as
479 */
480static struct cache_entry *create_alias_ce(struct cache_entry *ce, struct cache_entry *alias)
481{
482 int len;
483 struct cache_entry *new;
484
485 if (alias->ce_flags & CE_ADDED)
486 die("Will not add file alias '%s' ('%s' already exists in index)", ce->name, alias->name);
487
488 /* Ok, create the new entry using the name of the existing alias */
489 len = ce_namelen(alias);
490 new = xcalloc(1, cache_entry_size(len));
491 memcpy(new->name, alias->name, len);
492 copy_cache_entry(new, ce);
493 free(ce);
494 return new;
495}
496
38ed1d89 497int add_to_index(struct index_state *istate, const char *path, struct stat *st, int flags)
11be42a4 498{
38ed1d89 499 int size, namelen, was_same;
d177cab0 500 mode_t st_mode = st->st_mode;
6835550d 501 struct cache_entry *ce, *alias;
fb63d7f8 502 unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_RACY_IS_DIRTY;
38ed1d89
JH
503 int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND);
504 int pretend = flags & ADD_CACHE_PRETEND;
11be42a4 505
d177cab0 506 if (!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode))
960b8ad1 507 return error("%s: can only add regular files, symbolic links or git-directories", path);
11be42a4
JS
508
509 namelen = strlen(path);
d177cab0 510 if (S_ISDIR(st_mode)) {
09595258
LT
511 while (namelen && path[namelen-1] == '/')
512 namelen--;
513 }
11be42a4
JS
514 size = cache_entry_size(namelen);
515 ce = xcalloc(1, size);
516 memcpy(ce->name, path, namelen);
7a51ed66 517 ce->ce_flags = namelen;
d177cab0 518 fill_stat_cache_info(ce, st);
11be42a4 519
78a8d641 520 if (trust_executable_bit && has_symlinks)
d177cab0 521 ce->ce_mode = create_ce_mode(st_mode);
185c975f 522 else {
78a8d641
JS
523 /* If there is an existing entry, pick the mode bits and type
524 * from it, otherwise assume unexecutable regular file.
11be42a4 525 */
185c975f 526 struct cache_entry *ent;
20314271 527 int pos = index_name_pos_also_unmerged(istate, path, namelen);
185c975f 528
4aab5b46 529 ent = (0 <= pos) ? istate->cache[pos] : NULL;
d177cab0 530 ce->ce_mode = ce_mode_from_stat(ent, st_mode);
11be42a4
JS
531 }
532
6835550d 533 alias = index_name_exists(istate, ce->name, ce_namelen(ce), ignore_case);
d177cab0 534 if (alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) {
0781b8a9
JH
535 /* Nothing changed, really */
536 free(ce);
6835550d 537 ce_mark_uptodate(alias);
1102952b 538 alias->ce_flags |= CE_ADDED;
0781b8a9
JH
539 return 0;
540 }
d177cab0 541 if (index_path(ce->sha1, path, st, 1))
960b8ad1 542 return error("unable to index file %s", path);
1102952b
LT
543 if (ignore_case && alias && different_name(ce, alias))
544 ce = create_alias_ce(ce, alias);
545 ce->ce_flags |= CE_ADDED;
38ed1d89 546
3bf0dd1f 547 /* It was suspected to be racily clean, but it turns out to be Ok */
38ed1d89
JH
548 was_same = (alias &&
549 !ce_stage(alias) &&
550 !hashcmp(alias->sha1, ce->sha1) &&
551 ce->ce_mode == alias->ce_mode);
552
553 if (pretend)
554 ;
555 else if (add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
960b8ad1 556 return error("unable to add %s to index",path);
38ed1d89 557 if (verbose && !was_same)
11be42a4 558 printf("add '%s'\n", path);
11be42a4
JS
559 return 0;
560}
561
38ed1d89 562int add_file_to_index(struct index_state *istate, const char *path, int flags)
d177cab0
LT
563{
564 struct stat st;
565 if (lstat(path, &st))
566 die("%s: unable to stat (%s)", path, strerror(errno));
38ed1d89 567 return add_to_index(istate, path, &st, flags);
d177cab0
LT
568}
569
6640f881
CR
570struct cache_entry *make_cache_entry(unsigned int mode,
571 const unsigned char *sha1, const char *path, int stage,
572 int refresh)
573{
574 int size, len;
575 struct cache_entry *ce;
576
577 if (!verify_path(path))
578 return NULL;
579
580 len = strlen(path);
581 size = cache_entry_size(len);
582 ce = xcalloc(1, size);
583
584 hashcpy(ce->sha1, sha1);
585 memcpy(ce->name, path, len);
586 ce->ce_flags = create_ce_flags(len, stage);
587 ce->ce_mode = create_ce_mode(mode);
588
589 if (refresh)
590 return refresh_cache_entry(ce, 0);
591
592 return ce;
593}
594
dbbce55b 595int ce_same_name(struct cache_entry *a, struct cache_entry *b)
7b937ca3
LT
596{
597 int len = ce_namelen(a);
598 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
599}
600
c0fd1f51
LT
601int ce_path_match(const struct cache_entry *ce, const char **pathspec)
602{
603 const char *match, *name;
604 int len;
605
606 if (!pathspec)
607 return 1;
608
609 len = ce_namelen(ce);
610 name = ce->name;
611 while ((match = *pathspec++) != NULL) {
612 int matchlen = strlen(match);
613 if (matchlen > len)
614 continue;
615 if (memcmp(name, match, matchlen))
616 continue;
617 if (matchlen && name[matchlen-1] == '/')
618 return 1;
619 if (name[matchlen] == '/' || !name[matchlen])
620 return 1;
f332726e
LT
621 if (!matchlen)
622 return 1;
c0fd1f51
LT
623 }
624 return 0;
625}
626
8dcf39c4
LT
627/*
628 * We fundamentally don't like some paths: we don't want
629 * dot or dot-dot anywhere, and for obvious reasons don't
630 * want to recurse into ".git" either.
631 *
632 * Also, we don't want double slashes or slashes at the
633 * end that can make pathnames ambiguous.
634 */
635static int verify_dotfile(const char *rest)
636{
637 /*
638 * The first character was '.', but that
639 * has already been discarded, we now test
640 * the rest.
641 */
642 switch (*rest) {
643 /* "." is not allowed */
644 case '\0': case '/':
645 return 0;
646
647 /*
648 * ".git" followed by NUL or slash is bad. This
649 * shares the path end test with the ".." case.
650 */
651 case 'g':
652 if (rest[1] != 'i')
653 break;
654 if (rest[2] != 't')
655 break;
656 rest += 2;
657 /* fallthrough */
658 case '.':
659 if (rest[1] == '\0' || rest[1] == '/')
660 return 0;
661 }
662 return 1;
663}
664
665int verify_path(const char *path)
666{
667 char c;
668
669 goto inside;
670 for (;;) {
671 if (!c)
672 return 1;
673 if (c == '/') {
674inside:
675 c = *path++;
676 switch (c) {
677 default:
678 continue;
679 case '/': case '\0':
680 break;
681 case '.':
682 if (verify_dotfile(path))
683 continue;
684 }
685 return 0;
686 }
687 c = *path++;
688 }
689}
690
12676608
LT
691/*
692 * Do we have another file that has the beginning components being a
693 * proper superset of the name we're trying to add?
0f1e4f04 694 */
4aab5b46
JH
695static int has_file_name(struct index_state *istate,
696 const struct cache_entry *ce, int pos, int ok_to_replace)
0f1e4f04 697{
12676608
LT
698 int retval = 0;
699 int len = ce_namelen(ce);
b155725d 700 int stage = ce_stage(ce);
12676608 701 const char *name = ce->name;
0f1e4f04 702
4aab5b46
JH
703 while (pos < istate->cache_nr) {
704 struct cache_entry *p = istate->cache[pos++];
0f1e4f04 705
12676608 706 if (len >= ce_namelen(p))
0f1e4f04 707 break;
12676608
LT
708 if (memcmp(name, p->name, len))
709 break;
b155725d
JH
710 if (ce_stage(p) != stage)
711 continue;
12676608
LT
712 if (p->name[len] != '/')
713 continue;
7a51ed66 714 if (p->ce_flags & CE_REMOVE)
21cd8d00 715 continue;
12676608
LT
716 retval = -1;
717 if (!ok_to_replace)
718 break;
4aab5b46 719 remove_index_entry_at(istate, --pos);
0f1e4f04 720 }
12676608
LT
721 return retval;
722}
0f1e4f04 723
12676608
LT
724/*
725 * Do we have another file with a pathname that is a proper
726 * subset of the name we're trying to add?
727 */
4aab5b46
JH
728static int has_dir_name(struct index_state *istate,
729 const struct cache_entry *ce, int pos, int ok_to_replace)
12676608
LT
730{
731 int retval = 0;
b155725d 732 int stage = ce_stage(ce);
12676608
LT
733 const char *name = ce->name;
734 const char *slash = name + ce_namelen(ce);
0f1e4f04 735
12676608
LT
736 for (;;) {
737 int len;
0f1e4f04 738
12676608
LT
739 for (;;) {
740 if (*--slash == '/')
741 break;
742 if (slash <= ce->name)
743 return retval;
744 }
745 len = slash - name;
0f1e4f04 746
7a51ed66 747 pos = index_name_pos(istate, name, create_ce_flags(len, stage));
12676608 748 if (pos >= 0) {
21cd8d00
JH
749 /*
750 * Found one, but not so fast. This could
751 * be a marker that says "I was here, but
752 * I am being removed". Such an entry is
753 * not a part of the resulting tree, and
754 * it is Ok to have a directory at the same
755 * path.
756 */
077c48df 757 if (!(istate->cache[pos]->ce_flags & CE_REMOVE)) {
21cd8d00
JH
758 retval = -1;
759 if (!ok_to_replace)
760 break;
4aab5b46 761 remove_index_entry_at(istate, pos);
21cd8d00
JH
762 continue;
763 }
12676608 764 }
21cd8d00
JH
765 else
766 pos = -pos-1;
12676608
LT
767
768 /*
769 * Trivial optimization: if we find an entry that
770 * already matches the sub-directory, then we know
b155725d 771 * we're ok, and we can exit.
12676608 772 */
4aab5b46
JH
773 while (pos < istate->cache_nr) {
774 struct cache_entry *p = istate->cache[pos];
b155725d
JH
775 if ((ce_namelen(p) <= len) ||
776 (p->name[len] != '/') ||
777 memcmp(p->name, name, len))
778 break; /* not our subdirectory */
077c48df
JH
779 if (ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE))
780 /*
781 * p is at the same stage as our entry, and
b155725d
JH
782 * is a subdirectory of what we are looking
783 * at, so we cannot have conflicts at our
784 * level or anything shorter.
785 */
786 return retval;
787 pos++;
192268c1 788 }
0f1e4f04 789 }
12676608
LT
790 return retval;
791}
792
793/* We may be in a situation where we already have path/file and path
794 * is being added, or we already have path and path/file is being
795 * added. Either one would result in a nonsense tree that has path
796 * twice when git-write-tree tries to write it out. Prevent it.
a6080a0a 797 *
12676608
LT
798 * If ok-to-replace is specified, we remove the conflicting entries
799 * from the cache so the caller should recompute the insert position.
800 * When this happens, we return non-zero.
801 */
4aab5b46
JH
802static int check_file_directory_conflict(struct index_state *istate,
803 const struct cache_entry *ce,
804 int pos, int ok_to_replace)
12676608 805{
21cd8d00
JH
806 int retval;
807
808 /*
809 * When ce is an "I am going away" entry, we allow it to be added
810 */
7a51ed66 811 if (ce->ce_flags & CE_REMOVE)
21cd8d00
JH
812 return 0;
813
12676608
LT
814 /*
815 * We check if the path is a sub-path of a subsequent pathname
816 * first, since removing those will not change the position
21cd8d00 817 * in the array.
12676608 818 */
4aab5b46 819 retval = has_file_name(istate, ce, pos, ok_to_replace);
21cd8d00 820
12676608
LT
821 /*
822 * Then check if the path might have a clashing sub-directory
823 * before it.
824 */
4aab5b46 825 return retval + has_dir_name(istate, ce, pos, ok_to_replace);
0f1e4f04
JH
826}
827
af3785dc 828static int add_index_entry_with_check(struct index_state *istate, struct cache_entry *ce, int option)
197ee8c9
LT
829{
830 int pos;
192268c1
JH
831 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
832 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
b155725d 833 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
5f73076c 834
09d5dc32 835 cache_tree_invalidate_path(istate->cache_tree, ce->name);
7a51ed66 836 pos = index_name_pos(istate, ce->name, ce->ce_flags);
197ee8c9 837
3e09cdfd 838 /* existing match? Just replace it. */
76e7f4ec 839 if (pos >= 0) {
cf558704 840 replace_index_entry(istate, pos, ce);
197ee8c9
LT
841 return 0;
842 }
76e7f4ec 843 pos = -pos-1;
197ee8c9 844
7b937ca3
LT
845 /*
846 * Inserting a merged entry ("stage 0") into the index
847 * will always replace all non-merged entries..
848 */
4aab5b46
JH
849 if (pos < istate->cache_nr && ce_stage(ce) == 0) {
850 while (ce_same_name(istate->cache[pos], ce)) {
7b937ca3 851 ok_to_add = 1;
4aab5b46 852 if (!remove_index_entry_at(istate, pos))
7b937ca3
LT
853 break;
854 }
855 }
856
121481ab
LT
857 if (!ok_to_add)
858 return -1;
8dcf39c4
LT
859 if (!verify_path(ce->name))
860 return -1;
121481ab 861
3e09cdfd 862 if (!skip_df_check &&
4aab5b46 863 check_file_directory_conflict(istate, ce, pos, ok_to_replace)) {
192268c1 864 if (!ok_to_replace)
4aab5b46
JH
865 return error("'%s' appears as both a file and as a directory",
866 ce->name);
7a51ed66 867 pos = index_name_pos(istate, ce->name, ce->ce_flags);
192268c1
JH
868 pos = -pos-1;
869 }
af3785dc
JH
870 return pos + 1;
871}
872
873int add_index_entry(struct index_state *istate, struct cache_entry *ce, int option)
874{
875 int pos;
876
877 if (option & ADD_CACHE_JUST_APPEND)
878 pos = istate->cache_nr;
879 else {
880 int ret;
881 ret = add_index_entry_with_check(istate, ce, option);
882 if (ret <= 0)
883 return ret;
884 pos = ret - 1;
885 }
0f1e4f04 886
197ee8c9 887 /* Make sure the array is big enough .. */
4aab5b46
JH
888 if (istate->cache_nr == istate->cache_alloc) {
889 istate->cache_alloc = alloc_nr(istate->cache_alloc);
890 istate->cache = xrealloc(istate->cache,
891 istate->cache_alloc * sizeof(struct cache_entry *));
197ee8c9
LT
892 }
893
894 /* Add it in.. */
4aab5b46 895 istate->cache_nr++;
af3785dc 896 if (istate->cache_nr > pos + 1)
4aab5b46
JH
897 memmove(istate->cache + pos + 1,
898 istate->cache + pos,
899 (istate->cache_nr - pos - 1) * sizeof(ce));
cf558704 900 set_index_entry(istate, pos, ce);
4aab5b46 901 istate->cache_changed = 1;
197ee8c9
LT
902 return 0;
903}
904
405e5b2f
LT
905/*
906 * "refresh" does not calculate a new sha1 file or bring the
907 * cache up-to-date for mode/content changes. But what it
908 * _does_ do is to "re-match" the stat information of a file
909 * with the cache, so that you can refresh the cache for a
910 * file that hasn't been changed but where the stat entry is
911 * out of date.
912 *
913 * For example, you'd want to do this after doing a "git-read-tree",
914 * to link up the stat cache details with the proper files.
915 */
4aab5b46 916static struct cache_entry *refresh_cache_ent(struct index_state *istate,
4bd5b7da
JH
917 struct cache_entry *ce,
918 unsigned int options, int *err)
405e5b2f
LT
919{
920 struct stat st;
921 struct cache_entry *updated;
922 int changed, size;
4bd5b7da 923 int ignore_valid = options & CE_MATCH_IGNORE_VALID;
405e5b2f 924
eadb5831
JH
925 if (ce_uptodate(ce))
926 return ce;
927
aa9349d4
MSO
928 /*
929 * CE_VALID means the user promised us that the change to
930 * the work tree does not matter and told us not to worry.
931 */
932 if (!ignore_valid && (ce->ce_flags & CE_VALID)) {
933 ce_mark_uptodate(ce);
934 return ce;
935 }
936
8fd2cb40 937 if (lstat(ce->name, &st) < 0) {
ec0cc704
JH
938 if (err)
939 *err = errno;
8fd2cb40
JS
940 return NULL;
941 }
405e5b2f 942
4bd5b7da 943 changed = ie_match_stat(istate, ce, &st, options);
405e5b2f 944 if (!changed) {
4bd5b7da
JH
945 /*
946 * The path is unchanged. If we were told to ignore
947 * valid bit, then we did the actual stat check and
948 * found that the entry is unmodified. If the entry
949 * is not marked VALID, this is the place to mark it
950 * valid again, under "assume unchanged" mode.
951 */
952 if (ignore_valid && assume_unchanged &&
7a51ed66 953 !(ce->ce_flags & CE_VALID))
405e5b2f 954 ; /* mark this one VALID again */
eadb5831
JH
955 else {
956 /*
957 * We do not mark the index itself "modified"
958 * because CE_UPTODATE flag is in-core only;
959 * we are not going to write this change out.
960 */
961 ce_mark_uptodate(ce);
8fd2cb40 962 return ce;
eadb5831 963 }
405e5b2f
LT
964 }
965
4bd5b7da 966 if (ie_modified(istate, ce, &st, options)) {
ec0cc704
JH
967 if (err)
968 *err = EINVAL;
8fd2cb40
JS
969 return NULL;
970 }
405e5b2f
LT
971
972 size = ce_size(ce);
973 updated = xmalloc(size);
974 memcpy(updated, ce, size);
975 fill_stat_cache_info(updated, &st);
4bd5b7da
JH
976 /*
977 * If ignore_valid is not set, we should leave CE_VALID bit
978 * alone. Otherwise, paths marked with --no-assume-unchanged
979 * (i.e. things to be edited) will reacquire CE_VALID bit
980 * automatically, which is not really what we want.
405e5b2f 981 */
4bd5b7da 982 if (!ignore_valid && assume_unchanged &&
7a51ed66
LT
983 !(ce->ce_flags & CE_VALID))
984 updated->ce_flags &= ~CE_VALID;
405e5b2f
LT
985
986 return updated;
987}
988
d616813d 989int refresh_index(struct index_state *istate, unsigned int flags, const char **pathspec, char *seen)
405e5b2f
LT
990{
991 int i;
992 int has_errors = 0;
993 int really = (flags & REFRESH_REALLY) != 0;
994 int allow_unmerged = (flags & REFRESH_UNMERGED) != 0;
995 int quiet = (flags & REFRESH_QUIET) != 0;
996 int not_new = (flags & REFRESH_IGNORE_MISSING) != 0;
5fdeacb0 997 int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) != 0;
4bd5b7da 998 unsigned int options = really ? CE_MATCH_IGNORE_VALID : 0;
d14e7407 999 const char *needs_update_message;
405e5b2f 1000
d14e7407
JH
1001 needs_update_message = ((flags & REFRESH_SAY_CHANGED)
1002 ? "locally modified" : "needs update");
4aab5b46 1003 for (i = 0; i < istate->cache_nr; i++) {
405e5b2f 1004 struct cache_entry *ce, *new;
ec0cc704
JH
1005 int cache_errno = 0;
1006
4aab5b46 1007 ce = istate->cache[i];
5fdeacb0
JS
1008 if (ignore_submodules && S_ISGITLINK(ce->ce_mode))
1009 continue;
1010
405e5b2f 1011 if (ce_stage(ce)) {
4aab5b46
JH
1012 while ((i < istate->cache_nr) &&
1013 ! strcmp(istate->cache[i]->name, ce->name))
405e5b2f
LT
1014 i++;
1015 i--;
1016 if (allow_unmerged)
1017 continue;
1018 printf("%s: needs merge\n", ce->name);
1019 has_errors = 1;
1020 continue;
1021 }
1022
d616813d
AJ
1023 if (pathspec && !match_pathspec(pathspec, ce->name, strlen(ce->name), 0, seen))
1024 continue;
1025
4bd5b7da 1026 new = refresh_cache_ent(istate, ce, options, &cache_errno);
8fd2cb40 1027 if (new == ce)
405e5b2f 1028 continue;
8fd2cb40
JS
1029 if (!new) {
1030 if (not_new && cache_errno == ENOENT)
405e5b2f 1031 continue;
8fd2cb40 1032 if (really && cache_errno == EINVAL) {
405e5b2f
LT
1033 /* If we are doing --really-refresh that
1034 * means the index is not valid anymore.
1035 */
7a51ed66 1036 ce->ce_flags &= ~CE_VALID;
4aab5b46 1037 istate->cache_changed = 1;
405e5b2f
LT
1038 }
1039 if (quiet)
1040 continue;
d14e7407 1041 printf("%s: %s\n", ce->name, needs_update_message);
405e5b2f
LT
1042 has_errors = 1;
1043 continue;
1044 }
cf558704
LT
1045
1046 replace_index_entry(istate, i, new);
405e5b2f
LT
1047 }
1048 return has_errors;
1049}
1050
ec0cc704
JH
1051struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really)
1052{
4aab5b46 1053 return refresh_cache_ent(&the_index, ce, really, NULL);
ec0cc704
JH
1054}
1055
bad68ec9 1056static int verify_hdr(struct cache_header *hdr, unsigned long size)
e83c5163
LT
1057{
1058 SHA_CTX c;
bad68ec9 1059 unsigned char sha1[20];
e83c5163 1060
ccc4feb5 1061 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
e83c5163 1062 return error("bad signature");
ca9be054
LT
1063 if (hdr->hdr_version != htonl(2))
1064 return error("bad index version");
e83c5163 1065 SHA1_Init(&c);
ca9be054 1066 SHA1_Update(&c, hdr, size - 20);
e83c5163 1067 SHA1_Final(sha1, &c);
a89fccd2 1068 if (hashcmp(sha1, (unsigned char *)hdr + size - 20))
ca9be054 1069 return error("bad index file sha1 signature");
e83c5163
LT
1070 return 0;
1071}
1072
4aab5b46
JH
1073static int read_index_extension(struct index_state *istate,
1074 const char *ext, void *data, unsigned long sz)
bad68ec9
JH
1075{
1076 switch (CACHE_EXT(ext)) {
1077 case CACHE_EXT_TREE:
4aab5b46 1078 istate->cache_tree = cache_tree_read(data, sz);
bad68ec9
JH
1079 break;
1080 default:
1081 if (*ext < 'A' || 'Z' < *ext)
1082 return error("index uses %.4s extension, which we do not understand",
1083 ext);
1084 fprintf(stderr, "ignoring %.4s extension\n", ext);
1085 break;
1086 }
1087 return 0;
1088}
1089
4aab5b46 1090int read_index(struct index_state *istate)
8fd2cb40 1091{
4aab5b46 1092 return read_index_from(istate, get_index_file());
8fd2cb40
JS
1093}
1094
7a51ed66
LT
1095static void convert_from_disk(struct ondisk_cache_entry *ondisk, struct cache_entry *ce)
1096{
7fec10b7
JH
1097 size_t len;
1098
7a51ed66
LT
1099 ce->ce_ctime = ntohl(ondisk->ctime.sec);
1100 ce->ce_mtime = ntohl(ondisk->mtime.sec);
1101 ce->ce_dev = ntohl(ondisk->dev);
1102 ce->ce_ino = ntohl(ondisk->ino);
1103 ce->ce_mode = ntohl(ondisk->mode);
1104 ce->ce_uid = ntohl(ondisk->uid);
1105 ce->ce_gid = ntohl(ondisk->gid);
1106 ce->ce_size = ntohl(ondisk->size);
1107 /* On-disk flags are just 16 bits */
1108 ce->ce_flags = ntohs(ondisk->flags);
1109 hashcpy(ce->sha1, ondisk->sha1);
7fec10b7
JH
1110
1111 len = ce->ce_flags & CE_NAMEMASK;
1112 if (len == CE_NAMEMASK)
1113 len = strlen(ondisk->name);
1114 /*
1115 * NEEDSWORK: If the original index is crafted, this copy could
1116 * go unchecked.
1117 */
1118 memcpy(ce->name, ondisk->name, len + 1);
7a51ed66
LT
1119}
1120
cf558704
LT
1121static inline size_t estimate_cache_size(size_t ondisk_size, unsigned int entries)
1122{
1123 long per_entry;
1124
1125 per_entry = sizeof(struct cache_entry) - sizeof(struct ondisk_cache_entry);
1126
1127 /*
1128 * Alignment can cause differences. This should be "alignof", but
1129 * since that's a gcc'ism, just use the size of a pointer.
1130 */
1131 per_entry += sizeof(void *);
1132 return ondisk_size + entries*per_entry;
1133}
1134
8fd2cb40 1135/* remember to discard_cache() before reading a different cache! */
4aab5b46 1136int read_index_from(struct index_state *istate, const char *path)
e83c5163
LT
1137{
1138 int fd, i;
1139 struct stat st;
7a51ed66 1140 unsigned long src_offset, dst_offset;
e83c5163 1141 struct cache_header *hdr;
7a51ed66
LT
1142 void *mmap;
1143 size_t mmap_size;
e83c5163
LT
1144
1145 errno = EBUSY;
7a51ed66 1146 if (istate->alloc)
4aab5b46 1147 return istate->cache_nr;
5d1a5c02 1148
e83c5163 1149 errno = ENOENT;
4aab5b46 1150 istate->timestamp = 0;
8fd2cb40 1151 fd = open(path, O_RDONLY);
5d1a5c02
LT
1152 if (fd < 0) {
1153 if (errno == ENOENT)
1154 return 0;
1155 die("index file open failed (%s)", strerror(errno));
1156 }
e83c5163 1157
3511a377 1158 if (fstat(fd, &st))
5fe5c830 1159 die("cannot stat the open index (%s)", strerror(errno));
3511a377
LFC
1160
1161 errno = EINVAL;
7a51ed66
LT
1162 mmap_size = xsize_t(st.st_size);
1163 if (mmap_size < sizeof(struct cache_header) + 20)
3511a377
LFC
1164 die("index file smaller than expected");
1165
7a51ed66 1166 mmap = xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
e83c5163 1167 close(fd);
7a51ed66
LT
1168 if (mmap == MAP_FAILED)
1169 die("unable to map index file");
e83c5163 1170
7a51ed66
LT
1171 hdr = mmap;
1172 if (verify_hdr(hdr, mmap_size) < 0)
e83c5163
LT
1173 goto unmap;
1174
4aab5b46
JH
1175 istate->cache_nr = ntohl(hdr->hdr_entries);
1176 istate->cache_alloc = alloc_nr(istate->cache_nr);
1177 istate->cache = xcalloc(istate->cache_alloc, sizeof(struct cache_entry *));
e83c5163 1178
7a51ed66
LT
1179 /*
1180 * The disk format is actually larger than the in-memory format,
1181 * due to space for nsec etc, so even though the in-memory one
1182 * has room for a few more flags, we can allocate using the same
1183 * index size
1184 */
cf558704 1185 istate->alloc = xmalloc(estimate_cache_size(mmap_size, istate->cache_nr));
7a51ed66
LT
1186
1187 src_offset = sizeof(*hdr);
1188 dst_offset = 0;
4aab5b46 1189 for (i = 0; i < istate->cache_nr; i++) {
7a51ed66 1190 struct ondisk_cache_entry *disk_ce;
4aab5b46
JH
1191 struct cache_entry *ce;
1192
7a51ed66
LT
1193 disk_ce = (struct ondisk_cache_entry *)((char *)mmap + src_offset);
1194 ce = (struct cache_entry *)((char *)istate->alloc + dst_offset);
1195 convert_from_disk(disk_ce, ce);
cf558704 1196 set_index_entry(istate, i, ce);
7a51ed66
LT
1197
1198 src_offset += ondisk_ce_size(ce);
1199 dst_offset += ce_size(ce);
e83c5163 1200 }
4aab5b46 1201 istate->timestamp = st.st_mtime;
7a51ed66 1202 while (src_offset <= mmap_size - 20 - 8) {
bad68ec9
JH
1203 /* After an array of active_nr index entries,
1204 * there can be arbitrary number of extended
1205 * sections, each of which is prefixed with
1206 * extension name (4-byte) and section length
1207 * in 4-byte network byte order.
1208 */
1209 unsigned long extsize;
7a51ed66 1210 memcpy(&extsize, (char *)mmap + src_offset + 4, 4);
bad68ec9 1211 extsize = ntohl(extsize);
4aab5b46 1212 if (read_index_extension(istate,
7a51ed66
LT
1213 (const char *) mmap + src_offset,
1214 (char *) mmap + src_offset + 8,
1d7f171c 1215 extsize) < 0)
bad68ec9 1216 goto unmap;
7a51ed66
LT
1217 src_offset += 8;
1218 src_offset += extsize;
bad68ec9 1219 }
7a51ed66 1220 munmap(mmap, mmap_size);
4aab5b46 1221 return istate->cache_nr;
e83c5163
LT
1222
1223unmap:
7a51ed66 1224 munmap(mmap, mmap_size);
e83c5163 1225 errno = EINVAL;
5d1a5c02 1226 die("index file corrupt");
e83c5163
LT
1227}
1228
4aab5b46 1229int discard_index(struct index_state *istate)
6d297f81 1230{
4aab5b46
JH
1231 istate->cache_nr = 0;
1232 istate->cache_changed = 0;
1233 istate->timestamp = 0;
cf558704 1234 free_hash(&istate->name_hash);
4aab5b46 1235 cache_tree_free(&(istate->cache_tree));
7a51ed66
LT
1236 free(istate->alloc);
1237 istate->alloc = NULL;
6d297f81
JS
1238
1239 /* no need to throw away allocated active_cache */
7a51ed66 1240 return 0;
6d297f81
JS
1241}
1242
d1f128b0 1243int unmerged_index(const struct index_state *istate)
94a5728c
DB
1244{
1245 int i;
1246 for (i = 0; i < istate->cache_nr; i++) {
1247 if (ce_stage(istate->cache[i]))
1248 return 1;
1249 }
1250 return 0;
1251}
1252
4990aadc 1253#define WRITE_BUFFER_SIZE 8192
bf0f910d 1254static unsigned char write_buffer[WRITE_BUFFER_SIZE];
4990aadc
LT
1255static unsigned long write_buffer_len;
1256
6015c28b
JH
1257static int ce_write_flush(SHA_CTX *context, int fd)
1258{
1259 unsigned int buffered = write_buffer_len;
1260 if (buffered) {
1261 SHA1_Update(context, write_buffer, buffered);
93822c22 1262 if (write_in_full(fd, write_buffer, buffered) != buffered)
6015c28b
JH
1263 return -1;
1264 write_buffer_len = 0;
1265 }
1266 return 0;
1267}
1268
ca9be054 1269static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
4990aadc
LT
1270{
1271 while (len) {
1272 unsigned int buffered = write_buffer_len;
1273 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
1274 if (partial > len)
1275 partial = len;
1276 memcpy(write_buffer + buffered, data, partial);
1277 buffered += partial;
1278 if (buffered == WRITE_BUFFER_SIZE) {
6015c28b
JH
1279 write_buffer_len = buffered;
1280 if (ce_write_flush(context, fd))
4990aadc
LT
1281 return -1;
1282 buffered = 0;
1283 }
1284 write_buffer_len = buffered;
1285 len -= partial;
1d7f171c 1286 data = (char *) data + partial;
a6080a0a
JH
1287 }
1288 return 0;
4990aadc
LT
1289}
1290
bad68ec9 1291static int write_index_ext_header(SHA_CTX *context, int fd,
ac58c7b1 1292 unsigned int ext, unsigned int sz)
bad68ec9
JH
1293{
1294 ext = htonl(ext);
1295 sz = htonl(sz);
968a1d65
DR
1296 return ((ce_write(context, fd, &ext, 4) < 0) ||
1297 (ce_write(context, fd, &sz, 4) < 0)) ? -1 : 0;
bad68ec9
JH
1298}
1299
1300static int ce_flush(SHA_CTX *context, int fd)
4990aadc
LT
1301{
1302 unsigned int left = write_buffer_len;
ca9be054 1303
4990aadc
LT
1304 if (left) {
1305 write_buffer_len = 0;
ca9be054 1306 SHA1_Update(context, write_buffer, left);
4990aadc 1307 }
ca9be054 1308
2c865d9a
QH
1309 /* Flush first if not enough space for SHA1 signature */
1310 if (left + 20 > WRITE_BUFFER_SIZE) {
93822c22 1311 if (write_in_full(fd, write_buffer, left) != left)
2c865d9a
QH
1312 return -1;
1313 left = 0;
1314 }
1315
ca9be054 1316 /* Append the SHA1 signature at the end */
bad68ec9 1317 SHA1_Final(write_buffer + left, context);
ca9be054 1318 left += 20;
93822c22 1319 return (write_in_full(fd, write_buffer, left) != left) ? -1 : 0;
4990aadc
LT
1320}
1321
407c8eb0
JH
1322static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
1323{
1324 /*
1325 * The only thing we care about in this function is to smudge the
1326 * falsely clean entry due to touch-update-touch race, so we leave
1327 * everything else as they are. We are called for entries whose
1328 * ce_mtime match the index file mtime.
1329 */
1330 struct stat st;
1331
1332 if (lstat(ce->name, &st) < 0)
1333 return;
1334 if (ce_match_stat_basic(ce, &st))
1335 return;
1336 if (ce_modified_check_fs(ce, &st)) {
4b3511b0
JH
1337 /* This is "racily clean"; smudge it. Note that this
1338 * is a tricky code. At first glance, it may appear
1339 * that it can break with this sequence:
1340 *
1341 * $ echo xyzzy >frotz
1342 * $ git-update-index --add frotz
1343 * $ : >frotz
1344 * $ sleep 3
1345 * $ echo filfre >nitfol
1346 * $ git-update-index --add nitfol
1347 *
b7e58b17 1348 * but it does not. When the second update-index runs,
4b3511b0
JH
1349 * it notices that the entry "frotz" has the same timestamp
1350 * as index, and if we were to smudge it by resetting its
1351 * size to zero here, then the object name recorded
1352 * in index is the 6-byte file but the cached stat information
1353 * becomes zero --- which would then match what we would
a6080a0a 1354 * obtain from the filesystem next time we stat("frotz").
4b3511b0
JH
1355 *
1356 * However, the second update-index, before calling
1357 * this function, notices that the cached size is 6
1358 * bytes and what is on the filesystem is an empty
1359 * file, and never calls us, so the cached size information
1360 * for "frotz" stays 6 which does not match the filesystem.
1361 */
7a51ed66 1362 ce->ce_size = 0;
407c8eb0
JH
1363 }
1364}
1365
7a51ed66
LT
1366static int ce_write_entry(SHA_CTX *c, int fd, struct cache_entry *ce)
1367{
1368 int size = ondisk_ce_size(ce);
1369 struct ondisk_cache_entry *ondisk = xcalloc(1, size);
1370
1371 ondisk->ctime.sec = htonl(ce->ce_ctime);
1372 ondisk->ctime.nsec = 0;
1373 ondisk->mtime.sec = htonl(ce->ce_mtime);
1374 ondisk->mtime.nsec = 0;
1375 ondisk->dev = htonl(ce->ce_dev);
1376 ondisk->ino = htonl(ce->ce_ino);
1377 ondisk->mode = htonl(ce->ce_mode);
1378 ondisk->uid = htonl(ce->ce_uid);
1379 ondisk->gid = htonl(ce->ce_gid);
1380 ondisk->size = htonl(ce->ce_size);
1381 hashcpy(ondisk->sha1, ce->sha1);
1382 ondisk->flags = htons(ce->ce_flags);
1383 memcpy(ondisk->name, ce->name, ce_namelen(ce));
1384
1385 return ce_write(c, fd, ondisk, size);
1386}
1387
d1f128b0 1388int write_index(const struct index_state *istate, int newfd)
197ee8c9
LT
1389{
1390 SHA_CTX c;
1391 struct cache_header hdr;
1dffb8fa 1392 int i, err, removed;
4aab5b46
JH
1393 struct cache_entry **cache = istate->cache;
1394 int entries = istate->cache_nr;
025a0709
JH
1395
1396 for (i = removed = 0; i < entries; i++)
7a51ed66 1397 if (cache[i]->ce_flags & CE_REMOVE)
025a0709 1398 removed++;
197ee8c9 1399
ccc4feb5 1400 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
ca9be054 1401 hdr.hdr_version = htonl(2);
025a0709 1402 hdr.hdr_entries = htonl(entries - removed);
197ee8c9
LT
1403
1404 SHA1_Init(&c);
ca9be054 1405 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
197ee8c9
LT
1406 return -1;
1407
1408 for (i = 0; i < entries; i++) {
1409 struct cache_entry *ce = cache[i];
7a51ed66 1410 if (ce->ce_flags & CE_REMOVE)
aa16021e 1411 continue;
e06c43c7 1412 if (!ce_uptodate(ce) && is_racy_timestamp(istate, ce))
407c8eb0 1413 ce_smudge_racily_clean_entry(ce);
7a51ed66 1414 if (ce_write_entry(&c, newfd, ce) < 0)
197ee8c9
LT
1415 return -1;
1416 }
1af1c2b6 1417
bad68ec9 1418 /* Write extension data here */
4aab5b46 1419 if (istate->cache_tree) {
1dffb8fa
PH
1420 struct strbuf sb;
1421
1422 strbuf_init(&sb, 0);
1423 cache_tree_write(&sb, istate->cache_tree);
1424 err = write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) < 0
1425 || ce_write(&c, newfd, sb.buf, sb.len) < 0;
1426 strbuf_release(&sb);
1427 if (err)
bad68ec9 1428 return -1;
bad68ec9
JH
1429 }
1430 return ce_flush(&c, newfd);
197ee8c9 1431}
e46bbcf6
MV
1432
1433/*
1434 * Read the index file that is potentially unmerged into given
1435 * index_state, dropping any unmerged entries. Returns true is
1436 * the index is unmerged. Callers who want to refuse to work
1437 * from an unmerged state can call this and check its return value,
1438 * instead of calling read_cache().
1439 */
1440int read_index_unmerged(struct index_state *istate)
1441{
1442 int i;
1443 struct cache_entry **dst;
1444 struct cache_entry *last = NULL;
1445
1446 read_index(istate);
1447 dst = istate->cache;
1448 for (i = 0; i < istate->cache_nr; i++) {
1449 struct cache_entry *ce = istate->cache[i];
1450 if (ce_stage(ce)) {
1451 remove_name_hash(ce);
1452 if (last && !strcmp(ce->name, last->name))
1453 continue;
1454 cache_tree_invalidate_path(istate->cache_tree, ce->name);
1455 last = ce;
1456 continue;
1457 }
1458 *dst++ = ce;
1459 }
1460 istate->cache_nr = dst - istate->cache;
1461 return !!last;
1462}