2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 struct cache_entry
**active_cache
= NULL
;
9 unsigned int active_nr
= 0, active_alloc
= 0, active_cache_changed
= 0;
12 * This only updates the "non-critical" parts of the directory
13 * cache, ie the parts that aren't tracked by GIT, and only used
14 * to validate the cache.
16 void fill_stat_cache_info(struct cache_entry
*ce
, struct stat
*st
)
18 ce
->ce_ctime
.sec
= htonl(st
->st_ctime
);
19 ce
->ce_mtime
.sec
= htonl(st
->st_mtime
);
21 ce
->ce_ctime
.nsec
= htonl(st
->st_ctim
.tv_nsec
);
22 ce
->ce_mtime
.nsec
= htonl(st
->st_mtim
.tv_nsec
);
24 ce
->ce_dev
= htonl(st
->st_dev
);
25 ce
->ce_ino
= htonl(st
->st_ino
);
26 ce
->ce_uid
= htonl(st
->st_uid
);
27 ce
->ce_gid
= htonl(st
->st_gid
);
28 ce
->ce_size
= htonl(st
->st_size
);
31 int ce_match_stat(struct cache_entry
*ce
, struct stat
*st
)
33 unsigned int changed
= 0;
35 switch (ntohl(ce
->ce_mode
) & S_IFMT
) {
37 changed
|= !S_ISREG(st
->st_mode
) ? TYPE_CHANGED
: 0;
38 /* We consider only the owner x bit to be relevant for "mode changes" */
39 if (0100 & (ntohl(ce
->ce_mode
) ^ st
->st_mode
))
40 changed
|= MODE_CHANGED
;
43 changed
|= !S_ISLNK(st
->st_mode
) ? TYPE_CHANGED
: 0;
46 die("internal error: ce_mode is %o", ntohl(ce
->ce_mode
));
48 if (ce
->ce_mtime
.sec
!= htonl(st
->st_mtime
))
49 changed
|= MTIME_CHANGED
;
50 if (ce
->ce_ctime
.sec
!= htonl(st
->st_ctime
))
51 changed
|= CTIME_CHANGED
;
55 * nsec seems unreliable - not all filesystems support it, so
56 * as long as it is in the inode cache you get right nsec
57 * but after it gets flushed, you get zero nsec.
59 if (ce
->ce_mtime
.nsec
!= htonl(st
->st_mtim
.tv_nsec
))
60 changed
|= MTIME_CHANGED
;
61 if (ce
->ce_ctime
.nsec
!= htonl(st
->st_ctim
.tv_nsec
))
62 changed
|= CTIME_CHANGED
;
65 if (ce
->ce_uid
!= htonl(st
->st_uid
) ||
66 ce
->ce_gid
!= htonl(st
->st_gid
))
67 changed
|= OWNER_CHANGED
;
68 if (ce
->ce_ino
!= htonl(st
->st_ino
))
69 changed
|= INODE_CHANGED
;
73 * st_dev breaks on network filesystems where different
74 * clients will have different views of what "device"
75 * the filesystem is on
77 if (ce
->ce_dev
!= htonl(st
->st_dev
))
78 changed
|= INODE_CHANGED
;
81 if (ce
->ce_size
!= htonl(st
->st_size
))
82 changed
|= DATA_CHANGED
;
86 static int ce_compare_data(struct cache_entry
*ce
, struct stat
*st
)
89 int fd
= open(ce
->name
, O_RDONLY
);
92 unsigned char sha1
[20];
93 if (!index_fd(sha1
, fd
, st
, 0, NULL
))
94 match
= memcmp(sha1
, ce
->sha1
, 20);
100 static int ce_compare_link(struct cache_entry
*ce
, unsigned long expected_size
)
109 target
= xmalloc(expected_size
);
110 len
= readlink(ce
->name
, target
, expected_size
);
111 if (len
!= expected_size
) {
115 buffer
= read_sha1_file(ce
->sha1
, type
, &size
);
120 if (size
== expected_size
)
121 match
= memcmp(buffer
, target
, size
);
127 int ce_modified(struct cache_entry
*ce
, struct stat
*st
)
130 changed
= ce_match_stat(ce
, st
);
135 * If the mode or type has changed, there's no point in trying
136 * to refresh the entry - it's not going to match
138 if (changed
& (MODE_CHANGED
| TYPE_CHANGED
))
141 /* Immediately after read-tree or update-index --cacheinfo,
142 * the length field is zero. For other cases the ce_size
143 * should match the SHA1 recorded in the index entry.
145 if ((changed
& DATA_CHANGED
) && ce
->ce_size
!= htonl(0))
148 switch (st
->st_mode
& S_IFMT
) {
150 if (ce_compare_data(ce
, st
))
151 return changed
| DATA_CHANGED
;
154 if (ce_compare_link(ce
, st
->st_size
))
155 return changed
| DATA_CHANGED
;
158 return changed
| TYPE_CHANGED
;
163 int base_name_compare(const char *name1
, int len1
, int mode1
,
164 const char *name2
, int len2
, int mode2
)
166 unsigned char c1
, c2
;
167 int len
= len1
< len2
? len1
: len2
;
170 cmp
= memcmp(name1
, name2
, len
);
175 if (!c1
&& S_ISDIR(mode1
))
177 if (!c2
&& S_ISDIR(mode2
))
179 return (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
182 int cache_name_compare(const char *name1
, int flags1
, const char *name2
, int flags2
)
184 int len1
= flags1
& CE_NAMEMASK
;
185 int len2
= flags2
& CE_NAMEMASK
;
186 int len
= len1
< len2
? len1
: len2
;
189 cmp
= memcmp(name1
, name2
, len
);
203 int cache_name_pos(const char *name
, int namelen
)
209 while (last
> first
) {
210 int next
= (last
+ first
) >> 1;
211 struct cache_entry
*ce
= active_cache
[next
];
212 int cmp
= cache_name_compare(name
, namelen
, ce
->name
, ntohs(ce
->ce_flags
));
224 /* Remove entry, return true if there are more entries to go.. */
225 int remove_cache_entry_at(int pos
)
227 active_cache_changed
= 1;
229 if (pos
>= active_nr
)
231 memmove(active_cache
+ pos
, active_cache
+ pos
+ 1, (active_nr
- pos
) * sizeof(struct cache_entry
*));
235 int remove_file_from_cache(char *path
)
237 int pos
= cache_name_pos(path
, strlen(path
));
240 while (pos
< active_nr
&& !strcmp(active_cache
[pos
]->name
, path
))
241 remove_cache_entry_at(pos
);
245 int ce_same_name(struct cache_entry
*a
, struct cache_entry
*b
)
247 int len
= ce_namelen(a
);
248 return ce_namelen(b
) == len
&& !memcmp(a
->name
, b
->name
, len
);
251 int ce_path_match(const struct cache_entry
*ce
, const char **pathspec
)
253 const char *match
, *name
;
259 len
= ce_namelen(ce
);
261 while ((match
= *pathspec
++) != NULL
) {
262 int matchlen
= strlen(match
);
265 if (memcmp(name
, match
, matchlen
))
267 if (matchlen
&& name
[matchlen
-1] == '/')
269 if (name
[matchlen
] == '/' || !name
[matchlen
])
278 * Do we have another file that has the beginning components being a
279 * proper superset of the name we're trying to add?
281 static int has_file_name(const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
284 int len
= ce_namelen(ce
);
285 int stage
= ce_stage(ce
);
286 const char *name
= ce
->name
;
288 while (pos
< active_nr
) {
289 struct cache_entry
*p
= active_cache
[pos
++];
291 if (len
>= ce_namelen(p
))
293 if (memcmp(name
, p
->name
, len
))
295 if (ce_stage(p
) != stage
)
297 if (p
->name
[len
] != '/')
302 remove_cache_entry_at(--pos
);
308 * Do we have another file with a pathname that is a proper
309 * subset of the name we're trying to add?
311 static int has_dir_name(const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
314 int stage
= ce_stage(ce
);
315 const char *name
= ce
->name
;
316 const char *slash
= name
+ ce_namelen(ce
);
324 if (slash
<= ce
->name
)
329 pos
= cache_name_pos(name
, ntohs(create_ce_flags(len
, stage
)));
334 remove_cache_entry_at(pos
);
339 * Trivial optimization: if we find an entry that
340 * already matches the sub-directory, then we know
341 * we're ok, and we can exit.
344 while (pos
< active_nr
) {
345 struct cache_entry
*p
= active_cache
[pos
];
346 if ((ce_namelen(p
) <= len
) ||
347 (p
->name
[len
] != '/') ||
348 memcmp(p
->name
, name
, len
))
349 break; /* not our subdirectory */
350 if (ce_stage(p
) == stage
)
351 /* p is at the same stage as our entry, and
352 * is a subdirectory of what we are looking
353 * at, so we cannot have conflicts at our
354 * level or anything shorter.
363 /* We may be in a situation where we already have path/file and path
364 * is being added, or we already have path and path/file is being
365 * added. Either one would result in a nonsense tree that has path
366 * twice when git-write-tree tries to write it out. Prevent it.
368 * If ok-to-replace is specified, we remove the conflicting entries
369 * from the cache so the caller should recompute the insert position.
370 * When this happens, we return non-zero.
372 static int check_file_directory_conflict(const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
375 * We check if the path is a sub-path of a subsequent pathname
376 * first, since removing those will not change the position
379 int retval
= has_file_name(ce
, pos
, ok_to_replace
);
381 * Then check if the path might have a clashing sub-directory
384 return retval
+ has_dir_name(ce
, pos
, ok_to_replace
);
387 int add_cache_entry(struct cache_entry
*ce
, int option
)
390 int ok_to_add
= option
& ADD_CACHE_OK_TO_ADD
;
391 int ok_to_replace
= option
& ADD_CACHE_OK_TO_REPLACE
;
392 int skip_df_check
= option
& ADD_CACHE_SKIP_DFCHECK
;
393 pos
= cache_name_pos(ce
->name
, ntohs(ce
->ce_flags
));
395 /* existing match? Just replace it */
397 active_cache_changed
= 1;
398 active_cache
[pos
] = ce
;
404 * Inserting a merged entry ("stage 0") into the index
405 * will always replace all non-merged entries..
407 if (pos
< active_nr
&& ce_stage(ce
) == 0) {
408 while (ce_same_name(active_cache
[pos
], ce
)) {
410 if (!remove_cache_entry_at(pos
))
418 if (!skip_df_check
&& check_file_directory_conflict(ce
, pos
, ok_to_replace
)) {
421 pos
= cache_name_pos(ce
->name
, ntohs(ce
->ce_flags
));
425 /* Make sure the array is big enough .. */
426 if (active_nr
== active_alloc
) {
427 active_alloc
= alloc_nr(active_alloc
);
428 active_cache
= xrealloc(active_cache
, active_alloc
* sizeof(struct cache_entry
*));
434 memmove(active_cache
+ pos
+ 1, active_cache
+ pos
, (active_nr
- pos
- 1) * sizeof(ce
));
435 active_cache
[pos
] = ce
;
436 active_cache_changed
= 1;
440 static int verify_hdr(struct cache_header
*hdr
, unsigned long size
)
443 unsigned char sha1
[20];
445 if (hdr
->hdr_signature
!= htonl(CACHE_SIGNATURE
))
446 return error("bad signature");
447 if (hdr
->hdr_version
!= htonl(2))
448 return error("bad index version");
450 SHA1_Update(&c
, hdr
, size
- 20);
451 SHA1_Final(sha1
, &c
);
452 if (memcmp(sha1
, (void *)hdr
+ size
- 20, 20))
453 return error("bad index file sha1 signature");
461 unsigned long size
, offset
;
463 struct cache_header
*hdr
;
467 return error("more than one cachefile");
469 fd
= open(get_index_file(), O_RDONLY
);
471 return (errno
== ENOENT
) ? 0 : error("open failed");
473 size
= 0; // avoid gcc warning
475 if (!fstat(fd
, &st
)) {
478 if (size
>= sizeof(struct cache_header
) + 20)
479 map
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
482 if (map
== MAP_FAILED
)
483 return error("mmap failed");
486 if (verify_hdr(hdr
, size
) < 0)
489 active_nr
= ntohl(hdr
->hdr_entries
);
490 active_alloc
= alloc_nr(active_nr
);
491 active_cache
= calloc(active_alloc
, sizeof(struct cache_entry
*));
493 offset
= sizeof(*hdr
);
494 for (i
= 0; i
< active_nr
; i
++) {
495 struct cache_entry
*ce
= map
+ offset
;
496 offset
= offset
+ ce_size(ce
);
497 active_cache
[i
] = ce
;
504 return error("verify header failed");
507 #define WRITE_BUFFER_SIZE 8192
508 static unsigned char write_buffer
[WRITE_BUFFER_SIZE
];
509 static unsigned long write_buffer_len
;
511 static int ce_write(SHA_CTX
*context
, int fd
, void *data
, unsigned int len
)
514 unsigned int buffered
= write_buffer_len
;
515 unsigned int partial
= WRITE_BUFFER_SIZE
- buffered
;
518 memcpy(write_buffer
+ buffered
, data
, partial
);
520 if (buffered
== WRITE_BUFFER_SIZE
) {
521 SHA1_Update(context
, write_buffer
, WRITE_BUFFER_SIZE
);
522 if (write(fd
, write_buffer
, WRITE_BUFFER_SIZE
) != WRITE_BUFFER_SIZE
)
526 write_buffer_len
= buffered
;
533 static int ce_flush(SHA_CTX
*context
, int fd
)
535 unsigned int left
= write_buffer_len
;
538 write_buffer_len
= 0;
539 SHA1_Update(context
, write_buffer
, left
);
542 /* Flush first if not enough space for SHA1 signature */
543 if (left
+ 20 > WRITE_BUFFER_SIZE
) {
544 if (write(fd
, write_buffer
, left
) != left
)
549 /* Append the SHA1 signature at the end */
550 SHA1_Final(write_buffer
+ left
, context
);
552 if (write(fd
, write_buffer
, left
) != left
)
557 int write_cache(int newfd
, struct cache_entry
**cache
, int entries
)
560 struct cache_header hdr
;
563 for (i
= removed
= 0; i
< entries
; i
++)
564 if (!cache
[i
]->ce_mode
)
567 hdr
.hdr_signature
= htonl(CACHE_SIGNATURE
);
568 hdr
.hdr_version
= htonl(2);
569 hdr
.hdr_entries
= htonl(entries
- removed
);
572 if (ce_write(&c
, newfd
, &hdr
, sizeof(hdr
)) < 0)
575 for (i
= 0; i
< entries
; i
++) {
576 struct cache_entry
*ce
= cache
[i
];
579 if (ce_write(&c
, newfd
, ce
, ce_size(ce
)) < 0)
582 return ce_flush(&c
, newfd
);