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