4ed369acf8a62212c759c16dae8177597d02d536
[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 int trust_executable_bit = 1;
9 struct cache_entry **active_cache = NULL;
10 unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0;
11
12 /*
13  * This only updates the "non-critical" parts of the directory
14  * cache, ie the parts that aren't tracked by GIT, and only used
15  * to validate the cache.
16  */
17 void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
18 {
19         ce->ce_ctime.sec = htonl(st->st_ctime);
20         ce->ce_mtime.sec = htonl(st->st_mtime);
21 #ifdef USE_NSEC
22         ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
23         ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
24 #endif
25         ce->ce_dev = htonl(st->st_dev);
26         ce->ce_ino = htonl(st->st_ino);
27         ce->ce_uid = htonl(st->st_uid);
28         ce->ce_gid = htonl(st->st_gid);
29         ce->ce_size = htonl(st->st_size);
30 }
31
32 int ce_match_stat(struct cache_entry *ce, struct stat *st)
33 {
34         unsigned int changed = 0;
35
36         switch (ntohl(ce->ce_mode) & S_IFMT) {
37         case S_IFREG:
38                 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
39                 /* We consider only the owner x bit to be relevant for
40                  * "mode changes"
41                  */
42                 if (trust_executable_bit &&
43                     (0100 & (ntohl(ce->ce_mode) ^ st->st_mode)))
44                         changed |= MODE_CHANGED;
45                 break;
46         case S_IFLNK:
47                 changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0;
48                 break;
49         default:
50                 die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
51         }
52         if (ce->ce_mtime.sec != htonl(st->st_mtime))
53                 changed |= MTIME_CHANGED;
54         if (ce->ce_ctime.sec != htonl(st->st_ctime))
55                 changed |= CTIME_CHANGED;
56
57 #ifdef USE_NSEC
58         /*
59          * nsec seems unreliable - not all filesystems support it, so
60          * as long as it is in the inode cache you get right nsec
61          * but after it gets flushed, you get zero nsec.
62          */
63         if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec))
64                 changed |= MTIME_CHANGED;
65         if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec))
66                 changed |= CTIME_CHANGED;
67 #endif  
68
69         if (ce->ce_uid != htonl(st->st_uid) ||
70             ce->ce_gid != htonl(st->st_gid))
71                 changed |= OWNER_CHANGED;
72         if (ce->ce_ino != htonl(st->st_ino))
73                 changed |= INODE_CHANGED;
74
75 #ifdef USE_STDEV
76         /*
77          * st_dev breaks on network filesystems where different
78          * clients will have different views of what "device"
79          * the filesystem is on
80          */
81         if (ce->ce_dev != htonl(st->st_dev))
82                 changed |= INODE_CHANGED;
83 #endif
84
85         if (ce->ce_size != htonl(st->st_size))
86                 changed |= DATA_CHANGED;
87         return changed;
88 }
89
90 static int ce_compare_data(struct cache_entry *ce, struct stat *st)
91 {
92         int match = -1;
93         int fd = open(ce->name, O_RDONLY);
94
95         if (fd >= 0) {
96                 unsigned char sha1[20];
97                 if (!index_fd(sha1, fd, st, 0, NULL))
98                         match = memcmp(sha1, ce->sha1, 20);
99                 close(fd);
100         }
101         return match;
102 }
103
104 static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size)
105 {
106         int match = -1;
107         char *target;
108         void *buffer;
109         unsigned long size;
110         char type[10];
111         int len;
112
113         target = xmalloc(expected_size);
114         len = readlink(ce->name, target, expected_size);
115         if (len != expected_size) {
116                 free(target);
117                 return -1;
118         }
119         buffer = read_sha1_file(ce->sha1, type, &size);
120         if (!buffer) {
121                 free(target);
122                 return -1;
123         }
124         if (size == expected_size)
125                 match = memcmp(buffer, target, size);
126         free(buffer);
127         free(target);
128         return match;
129 }
130
131 int ce_modified(struct cache_entry *ce, struct stat *st)
132 {
133         int changed;
134         changed = ce_match_stat(ce, st);
135         if (!changed)
136                 return 0;
137
138         /*
139          * If the mode or type has changed, there's no point in trying
140          * to refresh the entry - it's not going to match
141          */
142         if (changed & (MODE_CHANGED | TYPE_CHANGED))
143                 return changed;
144
145         /* Immediately after read-tree or update-index --cacheinfo,
146          * the length field is zero.  For other cases the ce_size
147          * should match the SHA1 recorded in the index entry.
148          */
149         if ((changed & DATA_CHANGED) && ce->ce_size != htonl(0))
150                 return changed;
151
152         switch (st->st_mode & S_IFMT) {
153         case S_IFREG:
154                 if (ce_compare_data(ce, st))
155                         return changed | DATA_CHANGED;
156                 break;
157         case S_IFLNK:
158                 if (ce_compare_link(ce, st->st_size))
159                         return changed | DATA_CHANGED;
160                 break;
161         default:
162                 return changed | TYPE_CHANGED;
163         }
164         return 0;
165 }
166
167 int base_name_compare(const char *name1, int len1, int mode1,
168                       const char *name2, int len2, int mode2)
169 {
170         unsigned char c1, c2;
171         int len = len1 < len2 ? len1 : len2;
172         int cmp;
173
174         cmp = memcmp(name1, name2, len);
175         if (cmp)
176                 return cmp;
177         c1 = name1[len];
178         c2 = name2[len];
179         if (!c1 && S_ISDIR(mode1))
180                 c1 = '/';
181         if (!c2 && S_ISDIR(mode2))
182                 c2 = '/';
183         return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
184 }
185
186 int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
187 {
188         int len1 = flags1 & CE_NAMEMASK;
189         int len2 = flags2 & CE_NAMEMASK;
190         int len = len1 < len2 ? len1 : len2;
191         int cmp;
192
193         cmp = memcmp(name1, name2, len);
194         if (cmp)
195                 return cmp;
196         if (len1 < len2)
197                 return -1;
198         if (len1 > len2)
199                 return 1;
200         if (flags1 < flags2)
201                 return -1;
202         if (flags1 > flags2)
203                 return 1;
204         return 0;
205 }
206
207 int cache_name_pos(const char *name, int namelen)
208 {
209         int first, last;
210
211         first = 0;
212         last = active_nr;
213         while (last > first) {
214                 int next = (last + first) >> 1;
215                 struct cache_entry *ce = active_cache[next];
216                 int cmp = cache_name_compare(name, namelen, ce->name, ntohs(ce->ce_flags));
217                 if (!cmp)
218                         return next;
219                 if (cmp < 0) {
220                         last = next;
221                         continue;
222                 }
223                 first = next+1;
224         }
225         return -first-1;
226 }
227
228 /* Remove entry, return true if there are more entries to go.. */
229 int remove_cache_entry_at(int pos)
230 {
231         active_cache_changed = 1;
232         active_nr--;
233         if (pos >= active_nr)
234                 return 0;
235         memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
236         return 1;
237 }
238
239 int remove_file_from_cache(const char *path)
240 {
241         int pos = cache_name_pos(path, strlen(path));
242         if (pos < 0)
243                 pos = -pos-1;
244         while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
245                 remove_cache_entry_at(pos);
246         return 0;
247 }
248
249 int ce_same_name(struct cache_entry *a, struct cache_entry *b)
250 {
251         int len = ce_namelen(a);
252         return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
253 }
254
255 int ce_path_match(const struct cache_entry *ce, const char **pathspec)
256 {
257         const char *match, *name;
258         int len;
259
260         if (!pathspec)
261                 return 1;
262
263         len = ce_namelen(ce);
264         name = ce->name;
265         while ((match = *pathspec++) != NULL) {
266                 int matchlen = strlen(match);
267                 if (matchlen > len)
268                         continue;
269                 if (memcmp(name, match, matchlen))
270                         continue;
271                 if (matchlen && name[matchlen-1] == '/')
272                         return 1;
273                 if (name[matchlen] == '/' || !name[matchlen])
274                         return 1;
275                 if (!matchlen)
276                         return 1;
277         }
278         return 0;
279 }
280
281 /*
282  * Do we have another file that has the beginning components being a
283  * proper superset of the name we're trying to add?
284  */
285 static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replace)
286 {
287         int retval = 0;
288         int len = ce_namelen(ce);
289         int stage = ce_stage(ce);
290         const char *name = ce->name;
291
292         while (pos < active_nr) {
293                 struct cache_entry *p = active_cache[pos++];
294
295                 if (len >= ce_namelen(p))
296                         break;
297                 if (memcmp(name, p->name, len))
298                         break;
299                 if (ce_stage(p) != stage)
300                         continue;
301                 if (p->name[len] != '/')
302                         continue;
303                 retval = -1;
304                 if (!ok_to_replace)
305                         break;
306                 remove_cache_entry_at(--pos);
307         }
308         return retval;
309 }
310
311 /*
312  * Do we have another file with a pathname that is a proper
313  * subset of the name we're trying to add?
314  */
315 static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace)
316 {
317         int retval = 0;
318         int stage = ce_stage(ce);
319         const char *name = ce->name;
320         const char *slash = name + ce_namelen(ce);
321
322         for (;;) {
323                 int len;
324
325                 for (;;) {
326                         if (*--slash == '/')
327                                 break;
328                         if (slash <= ce->name)
329                                 return retval;
330                 }
331                 len = slash - name;
332
333                 pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage)));
334                 if (pos >= 0) {
335                         retval = -1;
336                         if (ok_to_replace)
337                                 break;
338                         remove_cache_entry_at(pos);
339                         continue;
340                 }
341
342                 /*
343                  * Trivial optimization: if we find an entry that
344                  * already matches the sub-directory, then we know
345                  * we're ok, and we can exit.
346                  */
347                 pos = -pos-1;
348                 while (pos < active_nr) {
349                         struct cache_entry *p = active_cache[pos];
350                         if ((ce_namelen(p) <= len) ||
351                             (p->name[len] != '/') ||
352                             memcmp(p->name, name, len))
353                                 break; /* not our subdirectory */
354                         if (ce_stage(p) == stage)
355                                 /* p is at the same stage as our entry, and
356                                  * is a subdirectory of what we are looking
357                                  * at, so we cannot have conflicts at our
358                                  * level or anything shorter.
359                                  */
360                                 return retval;
361                         pos++;
362                 }
363         }
364         return retval;
365 }
366
367 /* We may be in a situation where we already have path/file and path
368  * is being added, or we already have path and path/file is being
369  * added.  Either one would result in a nonsense tree that has path
370  * twice when git-write-tree tries to write it out.  Prevent it.
371  * 
372  * If ok-to-replace is specified, we remove the conflicting entries
373  * from the cache so the caller should recompute the insert position.
374  * When this happens, we return non-zero.
375  */
376 static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace)
377 {
378         /*
379          * We check if the path is a sub-path of a subsequent pathname
380          * first, since removing those will not change the position
381          * in the array
382          */
383         int retval = has_file_name(ce, pos, ok_to_replace);
384         /*
385          * Then check if the path might have a clashing sub-directory
386          * before it.
387          */
388         return retval + has_dir_name(ce, pos, ok_to_replace);
389 }
390
391 int add_cache_entry(struct cache_entry *ce, int option)
392 {
393         int pos;
394         int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
395         int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
396         int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
397         pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
398
399         /* existing match? Just replace it. */
400         if (pos >= 0) {
401                 active_cache_changed = 1;
402                 active_cache[pos] = ce;
403                 return 0;
404         }
405         pos = -pos-1;
406
407         /*
408          * Inserting a merged entry ("stage 0") into the index
409          * will always replace all non-merged entries..
410          */
411         if (pos < active_nr && ce_stage(ce) == 0) {
412                 while (ce_same_name(active_cache[pos], ce)) {
413                         ok_to_add = 1;
414                         if (!remove_cache_entry_at(pos))
415                                 break;
416                 }
417         }
418
419         if (!ok_to_add)
420                 return -1;
421
422         if (!skip_df_check &&
423             check_file_directory_conflict(ce, pos, ok_to_replace)) {
424                 if (!ok_to_replace)
425                         return -1;
426                 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
427                 pos = -pos-1;
428         }
429
430         /* Make sure the array is big enough .. */
431         if (active_nr == active_alloc) {
432                 active_alloc = alloc_nr(active_alloc);
433                 active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
434         }
435
436         /* Add it in.. */
437         active_nr++;
438         if (active_nr > pos)
439                 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
440         active_cache[pos] = ce;
441         active_cache_changed = 1;
442         return 0;
443 }
444
445 static int verify_hdr(struct cache_header *hdr, unsigned long size)
446 {
447         SHA_CTX c;
448         unsigned char sha1[20];
449
450         if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
451                 return error("bad signature");
452         if (hdr->hdr_version != htonl(2))
453                 return error("bad index version");
454         SHA1_Init(&c);
455         SHA1_Update(&c, hdr, size - 20);
456         SHA1_Final(sha1, &c);
457         if (memcmp(sha1, (void *)hdr + size - 20, 20))
458                 return error("bad index file sha1 signature");
459         return 0;
460 }
461
462 int read_cache(void)
463 {
464         int fd, i;
465         struct stat st;
466         unsigned long size, offset;
467         void *map;
468         struct cache_header *hdr;
469
470         errno = EBUSY;
471         if (active_cache)
472                 return active_nr;
473
474         errno = ENOENT;
475         fd = open(get_index_file(), O_RDONLY);
476         if (fd < 0) {
477                 if (errno == ENOENT)
478                         return 0;
479                 die("index file open failed (%s)", strerror(errno));
480         }
481
482         size = 0; // avoid gcc warning
483         map = MAP_FAILED;
484         if (!fstat(fd, &st)) {
485                 size = st.st_size;
486                 errno = EINVAL;
487                 if (size >= sizeof(struct cache_header) + 20)
488                         map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
489         }
490         close(fd);
491         if (map == MAP_FAILED)
492                 die("index file mmap failed (%s)", strerror(errno));
493
494         hdr = map;
495         if (verify_hdr(hdr, size) < 0)
496                 goto unmap;
497
498         active_nr = ntohl(hdr->hdr_entries);
499         active_alloc = alloc_nr(active_nr);
500         active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
501
502         offset = sizeof(*hdr);
503         for (i = 0; i < active_nr; i++) {
504                 struct cache_entry *ce = map + offset;
505                 offset = offset + ce_size(ce);
506                 active_cache[i] = ce;
507         }
508         return active_nr;
509
510 unmap:
511         munmap(map, size);
512         errno = EINVAL;
513         die("index file corrupt");
514 }
515
516 #define WRITE_BUFFER_SIZE 8192
517 static unsigned char write_buffer[WRITE_BUFFER_SIZE];
518 static unsigned long write_buffer_len;
519
520 static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
521 {
522         while (len) {
523                 unsigned int buffered = write_buffer_len;
524                 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
525                 if (partial > len)
526                         partial = len;
527                 memcpy(write_buffer + buffered, data, partial);
528                 buffered += partial;
529                 if (buffered == WRITE_BUFFER_SIZE) {
530                         SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE);
531                         if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE)
532                                 return -1;
533                         buffered = 0;
534                 }
535                 write_buffer_len = buffered;
536                 len -= partial;
537                 data += partial;
538         }
539         return 0;
540 }
541
542 static int ce_flush(SHA_CTX *context, int fd)
543 {
544         unsigned int left = write_buffer_len;
545
546         if (left) {
547                 write_buffer_len = 0;
548                 SHA1_Update(context, write_buffer, left);
549         }
550
551         /* Flush first if not enough space for SHA1 signature */
552         if (left + 20 > WRITE_BUFFER_SIZE) {
553                 if (write(fd, write_buffer, left) != left)
554                         return -1;
555                 left = 0;
556         }
557
558         /* Append the SHA1 signature at the end */
559         SHA1_Final(write_buffer + left, context);
560         left += 20;
561         if (write(fd, write_buffer, left) != left)
562                 return -1;
563         return 0;
564 }
565
566 int write_cache(int newfd, struct cache_entry **cache, int entries)
567 {
568         SHA_CTX c;
569         struct cache_header hdr;
570         int i, removed;
571
572         for (i = removed = 0; i < entries; i++)
573                 if (!cache[i]->ce_mode)
574                         removed++;
575
576         hdr.hdr_signature = htonl(CACHE_SIGNATURE);
577         hdr.hdr_version = htonl(2);
578         hdr.hdr_entries = htonl(entries - removed);
579
580         SHA1_Init(&c);
581         if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
582                 return -1;
583
584         for (i = 0; i < entries; i++) {
585                 struct cache_entry *ce = cache[i];
586                 if (!ce->ce_mode)
587                         continue;
588                 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
589                         return -1;
590         }
591         return ce_flush(&c, newfd);
592 }