Diff clean-up.
[git/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 error("more than one cachefile");
468 errno = ENOENT;
469 fd = open(get_index_file(), O_RDONLY);
470 if (fd < 0)
471 return (errno == ENOENT) ? 0 : error("open failed");
472
473 size = 0; // avoid gcc warning
474 map = MAP_FAILED;
475 if (!fstat(fd, &st)) {
476 size = st.st_size;
477 errno = EINVAL;
478 if (size >= sizeof(struct cache_header) + 20)
479 map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
480 }
481 close(fd);
482 if (map == MAP_FAILED)
483 return error("mmap failed");
484
485 hdr = map;
486 if (verify_hdr(hdr, size) < 0)
487 goto unmap;
488
489 active_nr = ntohl(hdr->hdr_entries);
490 active_alloc = alloc_nr(active_nr);
491 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
492
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;
498 }
499 return active_nr;
500
501 unmap:
502 munmap(map, size);
503 errno = EINVAL;
504 return error("verify header failed");
505 }
506
507 #define WRITE_BUFFER_SIZE 8192
508 static unsigned char write_buffer[WRITE_BUFFER_SIZE];
509 static unsigned long write_buffer_len;
510
511 static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
512 {
513 while (len) {
514 unsigned int buffered = write_buffer_len;
515 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
516 if (partial > len)
517 partial = len;
518 memcpy(write_buffer + buffered, data, partial);
519 buffered += 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)
523 return -1;
524 buffered = 0;
525 }
526 write_buffer_len = buffered;
527 len -= partial;
528 data += partial;
529 }
530 return 0;
531 }
532
533 static int ce_flush(SHA_CTX *context, int fd)
534 {
535 unsigned int left = write_buffer_len;
536
537 if (left) {
538 write_buffer_len = 0;
539 SHA1_Update(context, write_buffer, left);
540 }
541
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)
545 return -1;
546 left = 0;
547 }
548
549 /* Append the SHA1 signature at the end */
550 SHA1_Final(write_buffer + left, context);
551 left += 20;
552 if (write(fd, write_buffer, left) != left)
553 return -1;
554 return 0;
555 }
556
557 int write_cache(int newfd, struct cache_entry **cache, int entries)
558 {
559 SHA_CTX c;
560 struct cache_header hdr;
561 int i, removed;
562
563 for (i = removed = 0; i < entries; i++)
564 if (!cache[i]->ce_mode)
565 removed++;
566
567 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
568 hdr.hdr_version = htonl(2);
569 hdr.hdr_entries = htonl(entries - removed);
570
571 SHA1_Init(&c);
572 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
573 return -1;
574
575 for (i = 0; i < entries; i++) {
576 struct cache_entry *ce = cache[i];
577 if (!ce->ce_mode)
578 continue;
579 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
580 return -1;
581 }
582 return ce_flush(&c, newfd);
583 }