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