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