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