Add "-R" flag to "diff-tree", so that it will recursively traverse a tree of trees
[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 const char *sha1_file_directory = NULL;
9 struct cache_entry **active_cache = NULL;
10 unsigned int active_nr = 0, active_alloc = 0;
11
12 void usage(const char *err, ...)
13 {
14         va_list args;
15
16         va_start(args, err);
17         vfprintf(stderr, err, args);
18         va_end(args);
19         exit(1);
20 }
21
22 static unsigned hexval(char c)
23 {
24         if (c >= '0' && c <= '9')
25                 return c - '0';
26         if (c >= 'a' && c <= 'f')
27                 return c - 'a' + 10;
28         if (c >= 'A' && c <= 'F')
29                 return c - 'A' + 10;
30         return ~0;
31 }
32
33 int get_sha1_hex(const char *hex, unsigned char *sha1)
34 {
35         int i;
36         for (i = 0; i < 20; i++) {
37                 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
38                 if (val & ~0xff)
39                         return -1;
40                 *sha1++ = val;
41                 hex += 2;
42         }
43         return 0;
44 }
45
46 char * sha1_to_hex(const unsigned char *sha1)
47 {
48         static char buffer[50];
49         static const char hex[] = "0123456789abcdef";
50         char *buf = buffer;
51         int i;
52
53         for (i = 0; i < 20; i++) {
54                 unsigned int val = *sha1++;
55                 *buf++ = hex[val >> 4];
56                 *buf++ = hex[val & 0xf];
57         }
58         return buffer;
59 }
60
61 /*
62  * NOTE! This returns a statically allocated buffer, so you have to be
63  * careful about using it. Do a "strdup()" if you need to save the
64  * filename.
65  */
66 char *sha1_file_name(const unsigned char *sha1)
67 {
68         int i;
69         static char *name, *base;
70
71         if (!base) {
72                 char *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
73                 int len = strlen(sha1_file_directory);
74                 base = malloc(len + 60);
75                 memcpy(base, sha1_file_directory, len);
76                 memset(base+len, 0, 60);
77                 base[len] = '/';
78                 base[len+3] = '/';
79                 name = base + len + 1;
80         }
81         for (i = 0; i < 20; i++) {
82                 static char hex[] = "0123456789abcdef";
83                 unsigned int val = sha1[i];
84                 char *pos = name + i*2 + (i > 0);
85                 *pos++ = hex[val >> 4];
86                 *pos = hex[val & 0xf];
87         }
88         return base;
89 }
90
91 int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size)
92 {
93         unsigned char real_sha1[20];
94         SHA_CTX c;
95
96         SHA1_Init(&c);
97         SHA1_Update(&c, map, size);
98         SHA1_Final(real_sha1, &c);
99         return memcmp(sha1, real_sha1, 20) ? -1 : 0;
100 }
101
102 void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
103 {
104         char *filename = sha1_file_name(sha1);
105         int fd = open(filename, O_RDONLY);
106         struct stat st;
107         void *map;
108
109         if (fd < 0) {
110                 perror(filename);
111                 return NULL;
112         }
113         if (fstat(fd, &st) < 0) {
114                 close(fd);  
115                 return NULL;
116         }
117         map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
118         close(fd);
119         if (-1 == (int)(long)map)
120                 return NULL;
121         *size = st.st_size;
122         return map;
123 }
124
125 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
126 {
127         int ret, bytes;
128         z_stream stream;
129         char buffer[8192];
130         char *buf;
131
132         /* Get the data stream */
133         memset(&stream, 0, sizeof(stream));
134         stream.next_in = map;
135         stream.avail_in = mapsize;
136         stream.next_out = buffer;
137         stream.avail_out = sizeof(buffer);
138
139         inflateInit(&stream);
140         ret = inflate(&stream, 0);
141         if (sscanf(buffer, "%10s %lu", type, size) != 2)
142                 return NULL;
143
144         bytes = strlen(buffer) + 1;
145         buf = malloc(*size);
146         if (!buf)
147                 return NULL;
148
149         memcpy(buf, buffer + bytes, stream.total_out - bytes);
150         bytes = stream.total_out - bytes;
151         if (bytes < *size && ret == Z_OK) {
152                 stream.next_out = buf + bytes;
153                 stream.avail_out = *size - bytes;
154                 while (inflate(&stream, Z_FINISH) == Z_OK)
155                         /* nothing */;
156         }
157         inflateEnd(&stream);
158         return buf;
159 }
160
161 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
162 {
163         unsigned long mapsize;
164         void *map, *buf;
165
166         map = map_sha1_file(sha1, &mapsize);
167         if (map) {
168                 buf = unpack_sha1_file(map, mapsize, type, size);
169                 munmap(map, mapsize);
170                 return buf;
171         }
172         return NULL;
173 }
174
175 int write_sha1_file(char *buf, unsigned len, unsigned char *returnsha1)
176 {
177         int size;
178         char *compressed;
179         z_stream stream;
180         unsigned char sha1[20];
181         SHA_CTX c;
182
183         /* Set it up */
184         memset(&stream, 0, sizeof(stream));
185         deflateInit(&stream, Z_BEST_COMPRESSION);
186         size = deflateBound(&stream, len);
187         compressed = malloc(size);
188
189         /* Compress it */
190         stream.next_in = buf;
191         stream.avail_in = len;
192         stream.next_out = compressed;
193         stream.avail_out = size;
194         while (deflate(&stream, Z_FINISH) == Z_OK)
195                 /* nothing */;
196         deflateEnd(&stream);
197         size = stream.total_out;
198
199         /* Sha1.. */
200         SHA1_Init(&c);
201         SHA1_Update(&c, compressed, size);
202         SHA1_Final(sha1, &c);
203
204         if (write_sha1_buffer(sha1, compressed, size) < 0)
205                 return -1;
206         if (returnsha1)
207                 memcpy(returnsha1, sha1, 20);
208         return 0;
209 }
210
211 int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
212 {
213         char *filename = sha1_file_name(sha1);
214         int fd;
215
216         fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
217         if (fd < 0)
218                 return (errno == EEXIST) ? 0 : -1;
219         write(fd, buf, size);
220         close(fd);
221         return 0;
222 }
223
224 static int error(const char * string)
225 {
226         fprintf(stderr, "error: %s\n", string);
227         return -1;
228 }
229
230 int cache_match_stat(struct cache_entry *ce, struct stat *st)
231 {
232         unsigned int changed = 0;
233
234         if (ce->mtime.sec  != (unsigned int)st->st_mtim.tv_sec ||
235             ce->mtime.nsec != (unsigned int)st->st_mtim.tv_nsec)
236                 changed |= MTIME_CHANGED;
237         if (ce->ctime.sec  != (unsigned int)st->st_ctim.tv_sec ||
238             ce->ctime.nsec != (unsigned int)st->st_ctim.tv_nsec)
239                 changed |= CTIME_CHANGED;
240         if (ce->st_uid != (unsigned int)st->st_uid ||
241             ce->st_gid != (unsigned int)st->st_gid)
242                 changed |= OWNER_CHANGED;
243         if (ce->st_mode != (unsigned int)st->st_mode)
244                 changed |= MODE_CHANGED;
245         if (ce->st_dev != (unsigned int)st->st_dev ||
246             ce->st_ino != (unsigned int)st->st_ino)
247                 changed |= INODE_CHANGED;
248         if (ce->st_size != (unsigned int)st->st_size)
249                 changed |= DATA_CHANGED;
250         return changed;
251 }
252
253 int cache_name_compare(const char *name1, int len1, const char *name2, int len2)
254 {
255         int len = len1 < len2 ? len1 : len2;
256         int cmp;
257
258         cmp = memcmp(name1, name2, len);
259         if (cmp)
260                 return cmp;
261         if (len1 < len2)
262                 return -1;
263         if (len1 > len2)
264                 return 1;
265         return 0;
266 }
267
268 int cache_name_pos(const char *name, int namelen)
269 {
270         int first, last;
271
272         first = 0;
273         last = active_nr;
274         while (last > first) {
275                 int next = (last + first) >> 1;
276                 struct cache_entry *ce = active_cache[next];
277                 int cmp = cache_name_compare(name, namelen, ce->name, ce->namelen);
278                 if (!cmp)
279                         return -next-1;
280                 if (cmp < 0) {
281                         last = next;
282                         continue;
283                 }
284                 first = next+1;
285         }
286         return first;
287 }
288
289 int remove_file_from_cache(char *path)
290 {
291         int pos = cache_name_pos(path, strlen(path));
292         if (pos < 0) {
293                 pos = -pos-1;
294                 active_nr--;
295                 if (pos < active_nr)
296                         memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos - 1) * sizeof(struct cache_entry *));
297         }
298         return 0;
299 }
300
301 int add_cache_entry(struct cache_entry *ce, int ok_to_add)
302 {
303         int pos;
304
305         pos = cache_name_pos(ce->name, ce->namelen);
306
307         /* existing match? Just replace it */
308         if (pos < 0) {
309                 active_cache[-pos-1] = ce;
310                 return 0;
311         }
312
313         if (!ok_to_add)
314                 return -1;
315
316         /* Make sure the array is big enough .. */
317         if (active_nr == active_alloc) {
318                 active_alloc = alloc_nr(active_alloc);
319                 active_cache = realloc(active_cache, active_alloc * sizeof(struct cache_entry *));
320         }
321
322         /* Add it in.. */
323         active_nr++;
324         if (active_nr > pos)
325                 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
326         active_cache[pos] = ce;
327         return 0;
328 }
329
330 static int verify_hdr(struct cache_header *hdr, unsigned long size)
331 {
332         SHA_CTX c;
333         unsigned char sha1[20];
334
335         if (hdr->signature != CACHE_SIGNATURE)
336                 return error("bad signature");
337         if (hdr->version != 1)
338                 return error("bad version");
339         SHA1_Init(&c);
340         SHA1_Update(&c, hdr, offsetof(struct cache_header, sha1));
341         SHA1_Update(&c, hdr+1, size - sizeof(*hdr));
342         SHA1_Final(sha1, &c);
343         if (memcmp(sha1, hdr->sha1, 20))
344                 return error("bad header sha1");
345         return 0;
346 }
347
348 int read_cache(void)
349 {
350         int fd, i;
351         struct stat st;
352         unsigned long size, offset;
353         void *map;
354         struct cache_header *hdr;
355
356         errno = EBUSY;
357         if (active_cache)
358                 return error("more than one cachefile");
359         errno = ENOENT;
360         sha1_file_directory = getenv(DB_ENVIRONMENT);
361         if (!sha1_file_directory)
362                 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
363         if (access(sha1_file_directory, X_OK) < 0)
364                 return error("no access to SHA1 file directory");
365         fd = open(".dircache/index", O_RDONLY);
366         if (fd < 0)
367                 return (errno == ENOENT) ? 0 : error("open failed");
368
369         size = 0; // avoid gcc warning
370         map = (void *)-1;
371         if (!fstat(fd, &st)) {
372                 size = st.st_size;
373                 errno = EINVAL;
374                 if (size >= sizeof(struct cache_header))
375                         map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
376         }
377         close(fd);
378         if (-1 == (int)(long)map)
379                 return error("mmap failed");
380
381         hdr = map;
382         if (verify_hdr(hdr, size) < 0)
383                 goto unmap;
384
385         active_nr = hdr->entries;
386         active_alloc = alloc_nr(active_nr);
387         active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
388
389         offset = sizeof(*hdr);
390         for (i = 0; i < hdr->entries; i++) {
391                 struct cache_entry *ce = map + offset;
392                 offset = offset + ce_size(ce);
393                 active_cache[i] = ce;
394         }
395         return active_nr;
396
397 unmap:
398         munmap(map, size);
399         errno = EINVAL;
400         return error("verify header failed");
401 }
402
403 int write_cache(int newfd, struct cache_entry **cache, int entries)
404 {
405         SHA_CTX c;
406         struct cache_header hdr;
407         int i;
408
409         hdr.signature = CACHE_SIGNATURE;
410         hdr.version = 1;
411         hdr.entries = entries;
412
413         SHA1_Init(&c);
414         SHA1_Update(&c, &hdr, offsetof(struct cache_header, sha1));
415         for (i = 0; i < entries; i++) {
416                 struct cache_entry *ce = cache[i];
417                 int size = ce_size(ce);
418                 SHA1_Update(&c, ce, size);
419         }
420         SHA1_Final(hdr.sha1, &c);
421
422         if (write(newfd, &hdr, sizeof(hdr)) != sizeof(hdr))
423                 return -1;
424
425         for (i = 0; i < entries; i++) {
426                 struct cache_entry *ce = cache[i];
427                 int size = ce_size(ce);
428                 if (write(newfd, ce, size) != size)
429                         return -1;
430         }
431         return 0;
432 }