327888b8adc1efe57c15f9046590159d9aa0b2de
[git.git] / read-cache.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #include <stdarg.h>
7 #include "cache.h"
8
9 struct cache_entry **active_cache = NULL;
10 unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0;
11
12 int cache_match_stat(struct cache_entry *ce, struct stat *st)
13 {
14         unsigned int changed = 0;
15
16         switch (ntohl(ce->ce_mode) & S_IFMT) {
17         case S_IFREG:
18                 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
19                 /* We consider only the owner x bit to be relevant for "mode changes" */
20                 if (0100 & (ntohl(ce->ce_mode) ^ st->st_mode))
21                         changed |= MODE_CHANGED;
22                 break;
23         case S_IFLNK:
24                 changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0;
25                 break;
26         default:
27                 die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
28         }
29         if (ce->ce_mtime.sec != htonl(st->st_mtime))
30                 changed |= MTIME_CHANGED;
31         if (ce->ce_ctime.sec != htonl(st->st_ctime))
32                 changed |= CTIME_CHANGED;
33
34 #ifdef NSEC
35         /*
36          * nsec seems unreliable - not all filesystems support it, so
37          * as long as it is in the inode cache you get right nsec
38          * but after it gets flushed, you get zero nsec.
39          */
40         if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec))
41                 changed |= MTIME_CHANGED;
42         if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec))
43                 changed |= CTIME_CHANGED;
44 #endif  
45
46         if (ce->ce_uid != htonl(st->st_uid) ||
47             ce->ce_gid != htonl(st->st_gid))
48                 changed |= OWNER_CHANGED;
49         if (ce->ce_dev != htonl(st->st_dev) ||
50             ce->ce_ino != htonl(st->st_ino))
51                 changed |= INODE_CHANGED;
52         if (ce->ce_size != htonl(st->st_size))
53                 changed |= DATA_CHANGED;
54         return changed;
55 }
56
57 int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
58 {
59         int len1 = flags1 & CE_NAMEMASK;
60         int len2 = flags2 & CE_NAMEMASK;
61         int len = len1 < len2 ? len1 : len2;
62         int cmp;
63
64         cmp = memcmp(name1, name2, len);
65         if (cmp)
66                 return cmp;
67         if (len1 < len2)
68                 return -1;
69         if (len1 > len2)
70                 return 1;
71         if (flags1 < flags2)
72                 return -1;
73         if (flags1 > flags2)
74                 return 1;
75         return 0;
76 }
77
78 int cache_name_pos(const char *name, int namelen)
79 {
80         int first, last;
81
82         first = 0;
83         last = active_nr;
84         while (last > first) {
85                 int next = (last + first) >> 1;
86                 struct cache_entry *ce = active_cache[next];
87                 int cmp = cache_name_compare(name, namelen, ce->name, htons(ce->ce_flags));
88                 if (!cmp)
89                         return next;
90                 if (cmp < 0) {
91                         last = next;
92                         continue;
93                 }
94                 first = next+1;
95         }
96         return -first-1;
97 }
98
99 /* Remove entry, return true if there are more entries to go.. */
100 int remove_entry_at(int pos)
101 {
102         active_cache_changed = 1;
103         active_nr--;
104         if (pos >= active_nr)
105                 return 0;
106         memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
107         return 1;
108 }
109
110 int remove_file_from_cache(char *path)
111 {
112         int pos = cache_name_pos(path, strlen(path));
113         if (pos < 0)
114                 pos = -pos-1;
115         while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
116                 remove_entry_at(pos);
117         return 0;
118 }
119
120 int same_name(struct cache_entry *a, struct cache_entry *b)
121 {
122         int len = ce_namelen(a);
123         return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
124 }
125
126 /* We may be in a situation where we already have path/file and path
127  * is being added, or we already have path and path/file is being
128  * added.  Either one would result in a nonsense tree that has path
129  * twice when git-write-tree tries to write it out.  Prevent it.
130  */
131 static int check_file_directory_conflict(const struct cache_entry *ce)
132 {
133         int pos;
134         const char *path = ce->name;
135         int namelen = strlen(path);
136         int stage = ce_stage(ce);
137         char *pathbuf = xmalloc(namelen + 1);
138         char *cp;
139
140         memcpy(pathbuf, path, namelen + 1);
141
142         /*
143          * We are inserting path/file.  Do they have path registered at
144          * the same stage?  We need to do this for all the levels of our
145          * subpath.
146          */
147         cp = pathbuf;
148         while (1) {
149                 char *ep = strchr(cp, '/');
150                 if (ep == 0)
151                         break;
152                 *ep = 0;    /* first cut it at slash */
153                 pos = cache_name_pos(pathbuf,
154                                      htons(create_ce_flags(ep-cp, stage)));
155                 if (0 <= pos) {
156                         /* Our leading path component is registered as a file,
157                          * and we are trying to make it a directory.  This is
158                          * bad.
159                          */
160                         free(pathbuf);
161                         return -1;
162                 }
163                 *ep = '/';  /* then restore it and go downwards */
164                 cp = ep + 1;
165         }
166         free(pathbuf);
167
168         /* Do we have an entry in the cache that makes our path a prefix
169          * of it?  That is, are we creating a file where they already expect
170          * a directory there?
171          */
172         pos = cache_name_pos(path,
173                              htons(create_ce_flags(namelen, stage)));
174
175         /* (0 <= pos) cannot happen because add_cache_entry()
176          * should have taken care of that case.
177          */
178         pos = -pos-1;
179
180         /* pos would point at an existing entry that would come immediately
181          * after our path.  It could be the same as our path in higher stage,
182          * or different path but in a lower stage.
183          *
184          * E.g. when we are inserting path at stage 2,
185          *
186          *        1 path
187          * pos->  3 path
188          *        2 path/file
189          *        3 path/file
190          *
191          * We need to examine pos, ignore it because it is at different
192          * stage, examine next to find the path/file at stage 2, and
193          * complain.
194          */
195
196         while (pos < active_nr) {
197                 struct cache_entry *other = active_cache[pos];
198                 if (strncmp(other->name, path, namelen))
199                         break; /* it is not our "subdirectory" anymore */
200                 if ((ce_stage(other) == stage) && other->name[namelen] == '/')
201                         return -1;
202                 pos++;
203         }
204
205         return 0;
206 }
207
208 int add_cache_entry(struct cache_entry *ce, int ok_to_add)
209 {
210         int pos;
211
212         pos = cache_name_pos(ce->name, htons(ce->ce_flags));
213
214         /* existing match? Just replace it */
215         if (pos >= 0) {
216                 active_cache_changed = 1;
217                 active_cache[pos] = ce;
218                 return 0;
219         }
220         pos = -pos-1;
221
222         /*
223          * Inserting a merged entry ("stage 0") into the index
224          * will always replace all non-merged entries..
225          */
226         if (pos < active_nr && ce_stage(ce) == 0) {
227                 while (same_name(active_cache[pos], ce)) {
228                         ok_to_add = 1;
229                         if (!remove_entry_at(pos))
230                                 break;
231                 }
232         }
233
234         if (!ok_to_add)
235                 return -1;
236
237         if (check_file_directory_conflict(ce))
238                 return -1;
239
240         /* Make sure the array is big enough .. */
241         if (active_nr == active_alloc) {
242                 active_alloc = alloc_nr(active_alloc);
243                 active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
244         }
245
246         /* Add it in.. */
247         active_nr++;
248         if (active_nr > pos)
249                 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
250         active_cache[pos] = ce;
251         active_cache_changed = 1;
252         return 0;
253 }
254
255 static int verify_hdr(struct cache_header *hdr, unsigned long size)
256 {
257         SHA_CTX c;
258         unsigned char sha1[20];
259
260         if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
261                 return error("bad signature");
262         if (hdr->hdr_version != htonl(2))
263                 return error("bad index version");
264         SHA1_Init(&c);
265         SHA1_Update(&c, hdr, size - 20);
266         SHA1_Final(sha1, &c);
267         if (memcmp(sha1, (void *)hdr + size - 20, 20))
268                 return error("bad index file sha1 signature");
269         return 0;
270 }
271
272 int read_cache(void)
273 {
274         int fd, i;
275         struct stat st;
276         unsigned long size, offset;
277         void *map;
278         struct cache_header *hdr;
279
280         errno = EBUSY;
281         if (active_cache)
282                 return error("more than one cachefile");
283         errno = ENOENT;
284         fd = open(get_index_file(), O_RDONLY);
285         if (fd < 0)
286                 return (errno == ENOENT) ? 0 : error("open failed");
287
288         size = 0; // avoid gcc warning
289         map = (void *)-1;
290         if (!fstat(fd, &st)) {
291                 size = st.st_size;
292                 errno = EINVAL;
293                 if (size >= sizeof(struct cache_header) + 20)
294                         map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
295         }
296         close(fd);
297         if (-1 == (int)(long)map)
298                 return error("mmap failed");
299
300         hdr = map;
301         if (verify_hdr(hdr, size) < 0)
302                 goto unmap;
303
304         active_nr = ntohl(hdr->hdr_entries);
305         active_alloc = alloc_nr(active_nr);
306         active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
307
308         offset = sizeof(*hdr);
309         for (i = 0; i < active_nr; i++) {
310                 struct cache_entry *ce = map + offset;
311                 offset = offset + ce_size(ce);
312                 active_cache[i] = ce;
313         }
314         return active_nr;
315
316 unmap:
317         munmap(map, size);
318         errno = EINVAL;
319         return error("verify header failed");
320 }
321
322 #define WRITE_BUFFER_SIZE 8192
323 static char write_buffer[WRITE_BUFFER_SIZE];
324 static unsigned long write_buffer_len;
325
326 static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
327 {
328         while (len) {
329                 unsigned int buffered = write_buffer_len;
330                 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
331                 if (partial > len)
332                         partial = len;
333                 memcpy(write_buffer + buffered, data, partial);
334                 buffered += partial;
335                 if (buffered == WRITE_BUFFER_SIZE) {
336                         SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE);
337                         if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE)
338                                 return -1;
339                         buffered = 0;
340                 }
341                 write_buffer_len = buffered;
342                 len -= partial;
343                 data += partial;
344         }
345         return 0;
346 }
347
348 static int ce_flush(SHA_CTX *context, int fd)
349 {
350         unsigned int left = write_buffer_len;
351
352         if (left) {
353                 write_buffer_len = 0;
354                 SHA1_Update(context, write_buffer, left);
355         }
356
357         /* Append the SHA1 signature at the end */
358         SHA1_Final(write_buffer + left, context);
359         left += 20;
360         if (write(fd, write_buffer, left) != left)
361                 return -1;
362         return 0;
363 }
364
365 int write_cache(int newfd, struct cache_entry **cache, int entries)
366 {
367         SHA_CTX c;
368         struct cache_header hdr;
369         int i;
370
371         hdr.hdr_signature = htonl(CACHE_SIGNATURE);
372         hdr.hdr_version = htonl(2);
373         hdr.hdr_entries = htonl(entries);
374
375         SHA1_Init(&c);
376         if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
377                 return -1;
378
379         for (i = 0; i < entries; i++) {
380                 struct cache_entry *ce = cache[i];
381                 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
382                         return -1;
383         }
384         return ce_flush(&c, newfd);
385 }