]> git.ipfire.org Git - thirdparty/git.git/blob - read-cache.c
[PATCH] Better error reporting for "git status"
[thirdparty/git.git] / read-cache.c
1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7
8 struct cache_entry **active_cache = NULL;
9 unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0;
10
11 /*
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.
15 */
16 void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
17 {
18 ce->ce_ctime.sec = htonl(st->st_ctime);
19 ce->ce_mtime.sec = htonl(st->st_mtime);
20 #ifdef USE_NSEC
21 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
22 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
23 #endif
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);
29 }
30
31 int ce_match_stat(struct cache_entry *ce, struct stat *st)
32 {
33 unsigned int changed = 0;
34
35 switch (ntohl(ce->ce_mode) & S_IFMT) {
36 case S_IFREG:
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;
41 break;
42 case S_IFLNK:
43 changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0;
44 break;
45 default:
46 die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
47 }
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;
52
53 #ifdef USE_NSEC
54 /*
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.
58 */
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;
63 #endif
64
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;
70
71 #ifdef USE_STDEV
72 /*
73 * st_dev breaks on network filesystems where different
74 * clients will have different views of what "device"
75 * the filesystem is on
76 */
77 if (ce->ce_dev != htonl(st->st_dev))
78 changed |= INODE_CHANGED;
79 #endif
80
81 if (ce->ce_size != htonl(st->st_size))
82 changed |= DATA_CHANGED;
83 return changed;
84 }
85
86 static int ce_compare_data(struct cache_entry *ce, struct stat *st)
87 {
88 int match = -1;
89 int fd = open(ce->name, O_RDONLY);
90
91 if (fd >= 0) {
92 unsigned char sha1[20];
93 if (!index_fd(sha1, fd, st, 0, NULL))
94 match = memcmp(sha1, ce->sha1, 20);
95 close(fd);
96 }
97 return match;
98 }
99
100 static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size)
101 {
102 int match = -1;
103 char *target;
104 void *buffer;
105 unsigned long size;
106 char type[10];
107 int len;
108
109 target = xmalloc(expected_size);
110 len = readlink(ce->name, target, expected_size);
111 if (len != expected_size) {
112 free(target);
113 return -1;
114 }
115 buffer = read_sha1_file(ce->sha1, type, &size);
116 if (!buffer) {
117 free(target);
118 return -1;
119 }
120 if (size == expected_size)
121 match = memcmp(buffer, target, size);
122 free(buffer);
123 free(target);
124 return match;
125 }
126
127 int ce_modified(struct cache_entry *ce, struct stat *st)
128 {
129 int changed;
130 changed = ce_match_stat(ce, st);
131 if (!changed)
132 return 0;
133
134 /*
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
137 */
138 if (changed & (MODE_CHANGED | TYPE_CHANGED))
139 return changed;
140
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.
144 */
145 if ((changed & DATA_CHANGED) && ce->ce_size != htonl(0))
146 return changed;
147
148 switch (st->st_mode & S_IFMT) {
149 case S_IFREG:
150 if (ce_compare_data(ce, st))
151 return changed | DATA_CHANGED;
152 break;
153 case S_IFLNK:
154 if (ce_compare_link(ce, st->st_size))
155 return changed | DATA_CHANGED;
156 break;
157 default:
158 return changed | TYPE_CHANGED;
159 }
160 return 0;
161 }
162
163 int base_name_compare(const char *name1, int len1, int mode1,
164 const char *name2, int len2, int mode2)
165 {
166 unsigned char c1, c2;
167 int len = len1 < len2 ? len1 : len2;
168 int cmp;
169
170 cmp = memcmp(name1, name2, len);
171 if (cmp)
172 return cmp;
173 c1 = name1[len];
174 c2 = name2[len];
175 if (!c1 && S_ISDIR(mode1))
176 c1 = '/';
177 if (!c2 && S_ISDIR(mode2))
178 c2 = '/';
179 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
180 }
181
182 int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
183 {
184 int len1 = flags1 & CE_NAMEMASK;
185 int len2 = flags2 & CE_NAMEMASK;
186 int len = len1 < len2 ? len1 : len2;
187 int cmp;
188
189 cmp = memcmp(name1, name2, len);
190 if (cmp)
191 return cmp;
192 if (len1 < len2)
193 return -1;
194 if (len1 > len2)
195 return 1;
196 if (flags1 < flags2)
197 return -1;
198 if (flags1 > flags2)
199 return 1;
200 return 0;
201 }
202
203 int cache_name_pos(const char *name, int namelen)
204 {
205 int first, last;
206
207 first = 0;
208 last = active_nr;
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));
213 if (!cmp)
214 return next;
215 if (cmp < 0) {
216 last = next;
217 continue;
218 }
219 first = next+1;
220 }
221 return -first-1;
222 }
223
224 /* Remove entry, return true if there are more entries to go.. */
225 int remove_cache_entry_at(int pos)
226 {
227 active_cache_changed = 1;
228 active_nr--;
229 if (pos >= active_nr)
230 return 0;
231 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
232 return 1;
233 }
234
235 int remove_file_from_cache(const char *path)
236 {
237 int pos = cache_name_pos(path, strlen(path));
238 if (pos < 0)
239 pos = -pos-1;
240 while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
241 remove_cache_entry_at(pos);
242 return 0;
243 }
244
245 int ce_same_name(struct cache_entry *a, struct cache_entry *b)
246 {
247 int len = ce_namelen(a);
248 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
249 }
250
251 int ce_path_match(const struct cache_entry *ce, const char **pathspec)
252 {
253 const char *match, *name;
254 int len;
255
256 if (!pathspec)
257 return 1;
258
259 len = ce_namelen(ce);
260 name = ce->name;
261 while ((match = *pathspec++) != NULL) {
262 int matchlen = strlen(match);
263 if (matchlen > len)
264 continue;
265 if (memcmp(name, match, matchlen))
266 continue;
267 if (matchlen && name[matchlen-1] == '/')
268 return 1;
269 if (name[matchlen] == '/' || !name[matchlen])
270 return 1;
271 if (!matchlen)
272 return 1;
273 }
274 return 0;
275 }
276
277 /*
278 * Do we have another file that has the beginning components being a
279 * proper superset of the name we're trying to add?
280 */
281 static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replace)
282 {
283 int retval = 0;
284 int len = ce_namelen(ce);
285 int stage = ce_stage(ce);
286 const char *name = ce->name;
287
288 while (pos < active_nr) {
289 struct cache_entry *p = active_cache[pos++];
290
291 if (len >= ce_namelen(p))
292 break;
293 if (memcmp(name, p->name, len))
294 break;
295 if (ce_stage(p) != stage)
296 continue;
297 if (p->name[len] != '/')
298 continue;
299 retval = -1;
300 if (!ok_to_replace)
301 break;
302 remove_cache_entry_at(--pos);
303 }
304 return retval;
305 }
306
307 /*
308 * Do we have another file with a pathname that is a proper
309 * subset of the name we're trying to add?
310 */
311 static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace)
312 {
313 int retval = 0;
314 int stage = ce_stage(ce);
315 const char *name = ce->name;
316 const char *slash = name + ce_namelen(ce);
317
318 for (;;) {
319 int len;
320
321 for (;;) {
322 if (*--slash == '/')
323 break;
324 if (slash <= ce->name)
325 return retval;
326 }
327 len = slash - name;
328
329 pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage)));
330 if (pos >= 0) {
331 retval = -1;
332 if (ok_to_replace)
333 break;
334 remove_cache_entry_at(pos);
335 continue;
336 }
337
338 /*
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.
342 */
343 pos = -pos-1;
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.
355 */
356 return retval;
357 pos++;
358 }
359 }
360 return retval;
361 }
362
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.
367 *
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.
371 */
372 static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace)
373 {
374 /*
375 * We check if the path is a sub-path of a subsequent pathname
376 * first, since removing those will not change the position
377 * in the array
378 */
379 int retval = has_file_name(ce, pos, ok_to_replace);
380 /*
381 * Then check if the path might have a clashing sub-directory
382 * before it.
383 */
384 return retval + has_dir_name(ce, pos, ok_to_replace);
385 }
386
387 int add_cache_entry(struct cache_entry *ce, int option)
388 {
389 int pos;
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));
394
395 /* existing match? Just replace it */
396 if (pos >= 0) {
397 active_cache_changed = 1;
398 active_cache[pos] = ce;
399 return 0;
400 }
401 pos = -pos-1;
402
403 /*
404 * Inserting a merged entry ("stage 0") into the index
405 * will always replace all non-merged entries..
406 */
407 if (pos < active_nr && ce_stage(ce) == 0) {
408 while (ce_same_name(active_cache[pos], ce)) {
409 ok_to_add = 1;
410 if (!remove_cache_entry_at(pos))
411 break;
412 }
413 }
414
415 if (!ok_to_add)
416 return -1;
417
418 if (!skip_df_check && check_file_directory_conflict(ce, pos, ok_to_replace)) {
419 if (!ok_to_replace)
420 return -1;
421 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
422 pos = -pos-1;
423 }
424
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 *));
429 }
430
431 /* Add it in.. */
432 active_nr++;
433 if (active_nr > pos)
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;
437 return 0;
438 }
439
440 static int verify_hdr(struct cache_header *hdr, unsigned long size)
441 {
442 SHA_CTX c;
443 unsigned char sha1[20];
444
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");
449 SHA1_Init(&c);
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");
454 return 0;
455 }
456
457 int read_cache(void)
458 {
459 int fd, i;
460 struct stat st;
461 unsigned long size, offset;
462 void *map;
463 struct cache_header *hdr;
464
465 errno = EBUSY;
466 if (active_cache)
467 return active_nr;
468
469 errno = ENOENT;
470 fd = open(get_index_file(), O_RDONLY);
471 if (fd < 0) {
472 if (errno == ENOENT)
473 return 0;
474 die("index file open failed (%s)", strerror(errno));
475 }
476
477 size = 0; // avoid gcc warning
478 map = MAP_FAILED;
479 if (!fstat(fd, &st)) {
480 size = st.st_size;
481 errno = EINVAL;
482 if (size >= sizeof(struct cache_header) + 20)
483 map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
484 }
485 close(fd);
486 if (map == MAP_FAILED)
487 die("index file mmap failed (%s)", strerror(errno));
488
489 hdr = map;
490 if (verify_hdr(hdr, size) < 0)
491 goto unmap;
492
493 active_nr = ntohl(hdr->hdr_entries);
494 active_alloc = alloc_nr(active_nr);
495 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
496
497 offset = sizeof(*hdr);
498 for (i = 0; i < active_nr; i++) {
499 struct cache_entry *ce = map + offset;
500 offset = offset + ce_size(ce);
501 active_cache[i] = ce;
502 }
503 return active_nr;
504
505 unmap:
506 munmap(map, size);
507 errno = EINVAL;
508 die("index file corrupt");
509 }
510
511 #define WRITE_BUFFER_SIZE 8192
512 static unsigned char write_buffer[WRITE_BUFFER_SIZE];
513 static unsigned long write_buffer_len;
514
515 static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
516 {
517 while (len) {
518 unsigned int buffered = write_buffer_len;
519 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
520 if (partial > len)
521 partial = len;
522 memcpy(write_buffer + buffered, data, partial);
523 buffered += partial;
524 if (buffered == WRITE_BUFFER_SIZE) {
525 SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE);
526 if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE)
527 return -1;
528 buffered = 0;
529 }
530 write_buffer_len = buffered;
531 len -= partial;
532 data += partial;
533 }
534 return 0;
535 }
536
537 static int ce_flush(SHA_CTX *context, int fd)
538 {
539 unsigned int left = write_buffer_len;
540
541 if (left) {
542 write_buffer_len = 0;
543 SHA1_Update(context, write_buffer, left);
544 }
545
546 /* Flush first if not enough space for SHA1 signature */
547 if (left + 20 > WRITE_BUFFER_SIZE) {
548 if (write(fd, write_buffer, left) != left)
549 return -1;
550 left = 0;
551 }
552
553 /* Append the SHA1 signature at the end */
554 SHA1_Final(write_buffer + left, context);
555 left += 20;
556 if (write(fd, write_buffer, left) != left)
557 return -1;
558 return 0;
559 }
560
561 int write_cache(int newfd, struct cache_entry **cache, int entries)
562 {
563 SHA_CTX c;
564 struct cache_header hdr;
565 int i, removed;
566
567 for (i = removed = 0; i < entries; i++)
568 if (!cache[i]->ce_mode)
569 removed++;
570
571 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
572 hdr.hdr_version = htonl(2);
573 hdr.hdr_entries = htonl(entries - removed);
574
575 SHA1_Init(&c);
576 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
577 return -1;
578
579 for (i = 0; i < entries; i++) {
580 struct cache_entry *ce = cache[i];
581 if (!ce->ce_mode)
582 continue;
583 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
584 return -1;
585 }
586 return ce_flush(&c, newfd);
587 }