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
41 if (trust_executable_bit &&
42 (0100 & (ntohl(ce->ce_mode) ^ st->st_mode)))
43 changed |= MODE_CHANGED;
46 changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0;
49 die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
51 if (ce->ce_mtime.sec != htonl(st->st_mtime))
52 changed |= MTIME_CHANGED;
53 if (ce->ce_ctime.sec != htonl(st->st_ctime))
54 changed |= CTIME_CHANGED;
58 * nsec seems unreliable - not all filesystems support it, so
59 * as long as it is in the inode cache you get right nsec
60 * but after it gets flushed, you get zero nsec.
62 if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec))
63 changed |= MTIME_CHANGED;
64 if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec))
65 changed |= CTIME_CHANGED;
68 if (ce->ce_uid != htonl(st->st_uid) ||
69 ce->ce_gid != htonl(st->st_gid))
70 changed |= OWNER_CHANGED;
71 if (ce->ce_ino != htonl(st->st_ino))
72 changed |= INODE_CHANGED;
76 * st_dev breaks on network filesystems where different
77 * clients will have different views of what "device"
78 * the filesystem is on
80 if (ce->ce_dev != htonl(st->st_dev))
81 changed |= INODE_CHANGED;
84 if (ce->ce_size != htonl(st->st_size))
85 changed |= DATA_CHANGED;
89 static int ce_compare_data(struct cache_entry *ce, struct stat *st)
92 int fd = open(ce->name, O_RDONLY);
95 unsigned char sha1[20];
96 if (!index_fd(sha1, fd, st, 0, NULL))
97 match = memcmp(sha1, ce->sha1, 20);
103 static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size)
112 target = xmalloc(expected_size);
113 len = readlink(ce->name, target, expected_size);
114 if (len != expected_size) {
118 buffer = read_sha1_file(ce->sha1, type, &size);
123 if (size == expected_size)
124 match = memcmp(buffer, target, size);
130 int ce_modified(struct cache_entry *ce, struct stat *st)
133 changed = ce_match_stat(ce, st);
138 * If the mode or type has changed, there's no point in trying
139 * to refresh the entry - it's not going to match
141 if (changed & (MODE_CHANGED | TYPE_CHANGED))
144 /* Immediately after read-tree or update-index --cacheinfo,
145 * the length field is zero. For other cases the ce_size
146 * should match the SHA1 recorded in the index entry.
148 if ((changed & DATA_CHANGED) && ce->ce_size != htonl(0))
151 switch (st->st_mode & S_IFMT) {
153 if (ce_compare_data(ce, st))
154 return changed | DATA_CHANGED;
157 if (ce_compare_link(ce, st->st_size))
158 return changed | DATA_CHANGED;
161 return changed | TYPE_CHANGED;
166 int base_name_compare(const char *name1, int len1, int mode1,
167 const char *name2, int len2, int mode2)
169 unsigned char c1, c2;
170 int len = len1 < len2 ? len1 : len2;
173 cmp = memcmp(name1, name2, len);
178 if (!c1 && S_ISDIR(mode1))
180 if (!c2 && S_ISDIR(mode2))
182 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
185 int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
187 int len1 = flags1 & CE_NAMEMASK;
188 int len2 = flags2 & CE_NAMEMASK;
189 int len = len1 < len2 ? len1 : len2;
192 cmp = memcmp(name1, name2, len);
206 int cache_name_pos(const char *name, int namelen)
212 while (last > first) {
213 int next = (last + first) >> 1;
214 struct cache_entry *ce = active_cache[next];
215 int cmp = cache_name_compare(name, namelen, ce->name, ntohs(ce->ce_flags));
227 /* Remove entry, return true if there are more entries to go.. */
228 int remove_cache_entry_at(int pos)
230 active_cache_changed = 1;
232 if (pos >= active_nr)
234 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
238 int remove_file_from_cache(const char *path)
240 int pos = cache_name_pos(path, strlen(path));
243 while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
244 remove_cache_entry_at(pos);
248 int ce_same_name(struct cache_entry *a, struct cache_entry *b)
250 int len = ce_namelen(a);
251 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
254 int ce_path_match(const struct cache_entry *ce, const char **pathspec)
256 const char *match, *name;
262 len = ce_namelen(ce);
264 while ((match = *pathspec++) != NULL) {
265 int matchlen = strlen(match);
268 if (memcmp(name, match, matchlen))
270 if (matchlen && name[matchlen-1] == '/')
272 if (name[matchlen] == '/' || !name[matchlen])
281 * Do we have another file that has the beginning components being a
282 * proper superset of the name we're trying to add?
284 static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replace)
287 int len = ce_namelen(ce);
288 int stage = ce_stage(ce);
289 const char *name = ce->name;
291 while (pos < active_nr) {
292 struct cache_entry *p = active_cache[pos++];
294 if (len >= ce_namelen(p))
296 if (memcmp(name, p->name, len))
298 if (ce_stage(p) != stage)
300 if (p->name[len] != '/')
305 remove_cache_entry_at(--pos);
311 * Do we have another file with a pathname that is a proper
312 * subset of the name we're trying to add?
314 static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace)
317 int stage = ce_stage(ce);
318 const char *name = ce->name;
319 const char *slash = name + ce_namelen(ce);
327 if (slash <= ce->name)
332 pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage)));
337 remove_cache_entry_at(pos);
342 * Trivial optimization: if we find an entry that
343 * already matches the sub-directory, then we know
344 * we're ok, and we can exit.
347 while (pos < active_nr) {
348 struct cache_entry *p = active_cache[pos];
349 if ((ce_namelen(p) <= len) ||
350 (p->name[len] != '/') ||
351 memcmp(p->name, name, len))
352 break; /* not our subdirectory */
353 if (ce_stage(p) == stage)
354 /* p is at the same stage as our entry, and
355 * is a subdirectory of what we are looking
356 * at, so we cannot have conflicts at our
357 * level or anything shorter.
366 /* We may be in a situation where we already have path/file and path
367 * is being added, or we already have path and path/file is being
368 * added. Either one would result in a nonsense tree that has path
369 * twice when git-write-tree tries to write it out. Prevent it.
371 * If ok-to-replace is specified, we remove the conflicting entries
372 * from the cache so the caller should recompute the insert position.
373 * When this happens, we return non-zero.
375 static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace)
378 * We check if the path is a sub-path of a subsequent pathname
379 * first, since removing those will not change the position
382 int retval = has_file_name(ce, pos, ok_to_replace);
384 * Then check if the path might have a clashing sub-directory
387 return retval + has_dir_name(ce, pos, ok_to_replace);
390 int add_cache_entry(struct cache_entry *ce, int option)
393 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
394 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
395 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
396 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
398 /* existing match? Just replace it. */
400 active_cache_changed = 1;
401 active_cache[pos] = ce;
407 * Inserting a merged entry ("stage 0") into the index
408 * will always replace all non-merged entries..
410 if (pos < active_nr && ce_stage(ce) == 0) {
411 while (ce_same_name(active_cache[pos], ce)) {
413 if (!remove_cache_entry_at(pos))
421 if (!skip_df_check &&
422 check_file_directory_conflict(ce, pos, ok_to_replace)) {
425 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
429 /* Make sure the array is big enough .. */
430 if (active_nr == active_alloc) {
431 active_alloc = alloc_nr(active_alloc);
432 active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
438 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
439 active_cache[pos] = ce;
440 active_cache_changed = 1;
444 static int verify_hdr(struct cache_header *hdr, unsigned long size)
447 unsigned char sha1[20];
449 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
450 return error("bad signature");
451 if (hdr->hdr_version != htonl(2))
452 return error("bad index version");
454 SHA1_Update(&c, hdr, size - 20);
455 SHA1_Final(sha1, &c);
456 if (memcmp(sha1, (void *)hdr + size - 20, 20))
457 return error("bad index file sha1 signature");
465 unsigned long size, offset;
467 struct cache_header *hdr;
474 fd = open(get_index_file(), O_RDONLY);
478 die("index file open failed (%s)", strerror(errno));
481 size = 0; // avoid gcc warning
483 if (!fstat(fd, &st)) {
486 if (size >= sizeof(struct cache_header) + 20)
487 map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
490 if (map == MAP_FAILED)
491 die("index file mmap failed (%s)", strerror(errno));
494 if (verify_hdr(hdr, size) < 0)
497 active_nr = ntohl(hdr->hdr_entries);
498 active_alloc = alloc_nr(active_nr);
499 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
501 offset = sizeof(*hdr);
502 for (i = 0; i < active_nr; i++) {
503 struct cache_entry *ce = map + offset;
504 offset = offset + ce_size(ce);
505 active_cache[i] = ce;
512 die("index file corrupt");
515 #define WRITE_BUFFER_SIZE 8192
516 static unsigned char write_buffer[WRITE_BUFFER_SIZE];
517 static unsigned long write_buffer_len;
519 static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
522 unsigned int buffered = write_buffer_len;
523 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
526 memcpy(write_buffer + buffered, data, partial);
528 if (buffered == WRITE_BUFFER_SIZE) {
529 SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE);
530 if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE)
534 write_buffer_len = buffered;
541 static int ce_flush(SHA_CTX *context, int fd)
543 unsigned int left = write_buffer_len;
546 write_buffer_len = 0;
547 SHA1_Update(context, write_buffer, left);
550 /* Flush first if not enough space for SHA1 signature */
551 if (left + 20 > WRITE_BUFFER_SIZE) {
552 if (write(fd, write_buffer, left) != left)
557 /* Append the SHA1 signature at the end */
558 SHA1_Final(write_buffer + left, context);
560 if (write(fd, write_buffer, left) != left)
565 int write_cache(int newfd, struct cache_entry **cache, int entries)
568 struct cache_header hdr;
571 for (i = removed = 0; i < entries; i++)
572 if (!cache[i]->ce_mode)
575 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
576 hdr.hdr_version = htonl(2);
577 hdr.hdr_entries = htonl(entries - removed);
580 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
583 for (i = 0; i < entries; i++) {
584 struct cache_entry *ce = cache[i];
587 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
590 return ce_flush(&c, newfd);