Use "-Wall -O2" for the compiler to get more warnings.
[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         fprintf(stderr, "read-tree: %s\n", err);
15         exit(1);
16 }
17
18 static unsigned hexval(char c)
19 {
20         if (c >= '0' && c <= '9')
21                 return c - '0';
22         if (c >= 'a' && c <= 'f')
23                 return c - 'a' + 10;
24         if (c >= 'A' && c <= 'F')
25                 return c - 'A' + 10;
26         return ~0;
27 }
28
29 int get_sha1_hex(char *hex, unsigned char *sha1)
30 {
31         int i;
32         for (i = 0; i < 20; i++) {
33                 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
34                 if (val & ~0xff)
35                         return -1;
36                 *sha1++ = val;
37                 hex += 2;
38         }
39         return 0;
40 }
41
42 char * sha1_to_hex(unsigned char *sha1)
43 {
44         static char buffer[50];
45         static const char hex[] = "0123456789abcdef";
46         char *buf = buffer;
47         int i;
48
49         for (i = 0; i < 20; i++) {
50                 unsigned int val = *sha1++;
51                 *buf++ = hex[val >> 4];
52                 *buf++ = hex[val & 0xf];
53         }
54         return buffer;
55 }
56
57 /*
58  * NOTE! This returns a statically allocated buffer, so you have to be
59  * careful about using it. Do a "strdup()" if you need to save the
60  * filename.
61  */
62 char *sha1_file_name(unsigned char *sha1)
63 {
64         int i;
65         static char *name, *base;
66
67         if (!base) {
68                 char *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
69                 int len = strlen(sha1_file_directory);
70                 base = malloc(len + 60);
71                 memcpy(base, sha1_file_directory, len);
72                 memset(base+len, 0, 60);
73                 base[len] = '/';
74                 base[len+3] = '/';
75                 name = base + len + 1;
76         }
77         for (i = 0; i < 20; i++) {
78                 static char hex[] = "0123456789abcdef";
79                 unsigned int val = sha1[i];
80                 char *pos = name + i*2 + (i > 0);
81                 *pos++ = hex[val >> 4];
82                 *pos = hex[val & 0xf];
83         }
84         return base;
85 }
86
87 void * read_sha1_file(unsigned char *sha1, char *type, unsigned long *size)
88 {
89         z_stream stream;
90         char buffer[8192];
91         struct stat st;
92         int fd, ret, bytes;
93         void *map, *buf;
94         char *filename = sha1_file_name(sha1);
95
96         fd = open(filename, O_RDONLY);
97         if (fd < 0) {
98                 perror(filename);
99                 return NULL;
100         }
101         if (fstat(fd, &st) < 0) {
102                 close(fd);
103                 return NULL;
104         }
105         map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
106         close(fd);
107         if (-1 == (int)(long)map)
108                 return NULL;
109
110         /* Get the data stream */
111         memset(&stream, 0, sizeof(stream));
112         stream.next_in = map;
113         stream.avail_in = st.st_size;
114         stream.next_out = buffer;
115         stream.avail_out = sizeof(buffer);
116
117         inflateInit(&stream);
118         ret = inflate(&stream, 0);
119         if (sscanf(buffer, "%10s %lu", type, size) != 2)
120                 return NULL;
121         bytes = strlen(buffer) + 1;
122         buf = malloc(*size);
123         if (!buf)
124                 return NULL;
125
126         memcpy(buf, buffer + bytes, stream.total_out - bytes);
127         bytes = stream.total_out - bytes;
128         if (bytes < *size && ret == Z_OK) {
129                 stream.next_out = buf + bytes;
130                 stream.avail_out = *size - bytes;
131                 while (inflate(&stream, Z_FINISH) == Z_OK)
132                         /* nothing */;
133         }
134         inflateEnd(&stream);
135         return buf;
136 }
137
138 int write_sha1_file(char *buf, unsigned len)
139 {
140         int size;
141         char *compressed;
142         z_stream stream;
143         unsigned char sha1[20];
144         SHA_CTX c;
145
146         /* Set it up */
147         memset(&stream, 0, sizeof(stream));
148         deflateInit(&stream, Z_BEST_COMPRESSION);
149         size = deflateBound(&stream, len);
150         compressed = malloc(size);
151
152         /* Compress it */
153         stream.next_in = buf;
154         stream.avail_in = len;
155         stream.next_out = compressed;
156         stream.avail_out = size;
157         while (deflate(&stream, Z_FINISH) == Z_OK)
158                 /* nothing */;
159         deflateEnd(&stream);
160         size = stream.total_out;
161
162         /* Sha1.. */
163         SHA1_Init(&c);
164         SHA1_Update(&c, compressed, size);
165         SHA1_Final(sha1, &c);
166
167         if (write_sha1_buffer(sha1, compressed, size) < 0)
168                 return -1;
169         printf("%s\n", sha1_to_hex(sha1));
170         return 0;
171 }
172
173 int write_sha1_buffer(unsigned char *sha1, void *buf, unsigned int size)
174 {
175         char *filename = sha1_file_name(sha1);
176         int fd;
177
178         fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
179         if (fd < 0)
180                 return (errno == EEXIST) ? 0 : -1;
181         write(fd, buf, size);
182         close(fd);
183         return 0;
184 }
185
186 static int error(const char * string)
187 {
188         fprintf(stderr, "error: %s\n", string);
189         return -1;
190 }
191
192 static int verify_hdr(struct cache_header *hdr, unsigned long size)
193 {
194         SHA_CTX c;
195         unsigned char sha1[20];
196
197         if (hdr->signature != CACHE_SIGNATURE)
198                 return error("bad signature");
199         if (hdr->version != 1)
200                 return error("bad version");
201         SHA1_Init(&c);
202         SHA1_Update(&c, hdr, offsetof(struct cache_header, sha1));
203         SHA1_Update(&c, hdr+1, size - sizeof(*hdr));
204         SHA1_Final(sha1, &c);
205         if (memcmp(sha1, hdr->sha1, 20))
206                 return error("bad header sha1");
207         return 0;
208 }
209
210 int read_cache(void)
211 {
212         int fd, i;
213         struct stat st;
214         unsigned long size, offset;
215         void *map;
216         struct cache_header *hdr;
217
218         errno = EBUSY;
219         if (active_cache)
220                 return error("more than one cachefile");
221         errno = ENOENT;
222         sha1_file_directory = getenv(DB_ENVIRONMENT);
223         if (!sha1_file_directory)
224                 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
225         if (access(sha1_file_directory, X_OK) < 0)
226                 return error("no access to SHA1 file directory");
227         fd = open(".dircache/index", O_RDONLY);
228         if (fd < 0)
229                 return (errno == ENOENT) ? 0 : error("open failed");
230
231         size = 0; // avoid gcc warning
232         map = (void *)-1;
233         if (!fstat(fd, &st)) {
234                 map = NULL;
235                 size = st.st_size;
236                 errno = EINVAL;
237                 if (size > sizeof(struct cache_header))
238                         map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
239         }
240         close(fd);
241         if (-1 == (int)(long)map)
242                 return error("mmap failed");
243
244         hdr = map;
245         if (verify_hdr(hdr, size) < 0)
246                 goto unmap;
247
248         active_nr = hdr->entries;
249         active_alloc = alloc_nr(active_nr);
250         active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
251
252         offset = sizeof(*hdr);
253         for (i = 0; i < hdr->entries; i++) {
254                 struct cache_entry *ce = map + offset;
255                 offset = offset + ce_size(ce);
256                 active_cache[i] = ce;
257         }
258         return active_nr;
259
260 unmap:
261         munmap(map, size);
262         errno = EINVAL;
263         return error("verify header failed");
264 }
265