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