Rename ".dircache" directory to ".git"
[git/jnareb-git.git] / read-cache.c
blob54536944648c166008f06d29f65314b58b0c3988
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
8 const char *sha1_file_directory = NULL;
9 struct cache_entry **active_cache = NULL;
10 unsigned int active_nr = 0, active_alloc = 0;
12 void usage(const char *err, ...)
14 va_list args;
15 char string[200];
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);
24 static unsigned hexval(char c)
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;
35 int get_sha1_hex(const char *hex, unsigned char *sha1)
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;
45 return 0;
48 char * sha1_to_hex(const unsigned char *sha1)
50 static char buffer[50];
51 static const char hex[] = "0123456789abcdef";
52 char *buf = buffer;
53 int i;
55 for (i = 0; i < 20; i++) {
56 unsigned int val = *sha1++;
57 *buf++ = hex[val >> 4];
58 *buf++ = hex[val & 0xf];
60 return buffer;
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.
68 char *sha1_file_name(const unsigned char *sha1)
70 int i;
71 static char *name, *base;
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;
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];
90 return base;
93 int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size)
95 unsigned char real_sha1[20];
96 SHA_CTX c;
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;
104 void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
106 char *filename = sha1_file_name(sha1);
107 int fd = open(filename, O_RDONLY);
108 struct stat st;
109 void *map;
111 if (fd < 0) {
112 perror(filename);
113 return NULL;
115 if (fstat(fd, &st) < 0) {
116 close(fd);
117 return NULL;
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;
127 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
129 int ret, bytes;
130 z_stream stream;
131 char buffer[8192];
132 char *buf;
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);
141 inflateInit(&stream);
142 ret = inflate(&stream, 0);
143 if (sscanf(buffer, "%10s %lu", type, size) != 2)
144 return NULL;
146 bytes = strlen(buffer) + 1;
147 buf = malloc(*size);
148 if (!buf)
149 return NULL;
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 */;
159 inflateEnd(&stream);
160 return buf;
163 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
165 unsigned long mapsize;
166 void *map, *buf;
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;
174 return NULL;
177 int write_sha1_file(char *buf, unsigned len, unsigned char *returnsha1)
179 int size;
180 char *compressed;
181 z_stream stream;
182 unsigned char sha1[20];
183 SHA_CTX c;
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);
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;
201 /* Sha1.. */
202 SHA1_Init(&c);
203 SHA1_Update(&c, compressed, size);
204 SHA1_Final(sha1, &c);
206 if (write_sha1_buffer(sha1, compressed, size) < 0)
207 return -1;
208 if (returnsha1)
209 memcpy(returnsha1, sha1, 20);
210 return 0;
213 int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
215 char *filename = sha1_file_name(sha1);
216 int fd;
218 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
219 if (fd < 0)
220 return (errno == EEXIST) ? 0 : -1;
221 write(fd, buf, size);
222 close(fd);
223 return 0;
226 static int error(const char * string)
228 fprintf(stderr, "error: %s\n", string);
229 return -1;
232 int cache_match_stat(struct cache_entry *ce, struct stat *st)
234 unsigned int changed = 0;
236 if (ce->mtime.sec != (unsigned int)st->st_mtim.tv_sec ||
237 ce->mtime.nsec != (unsigned int)st->st_mtim.tv_nsec)
238 changed |= MTIME_CHANGED;
239 if (ce->ctime.sec != (unsigned int)st->st_ctim.tv_sec ||
240 ce->ctime.nsec != (unsigned int)st->st_ctim.tv_nsec)
241 changed |= CTIME_CHANGED;
242 if (ce->st_uid != (unsigned int)st->st_uid ||
243 ce->st_gid != (unsigned int)st->st_gid)
244 changed |= OWNER_CHANGED;
245 if (ce->st_mode != (unsigned int)st->st_mode)
246 changed |= MODE_CHANGED;
247 if (ce->st_dev != (unsigned int)st->st_dev ||
248 ce->st_ino != (unsigned int)st->st_ino)
249 changed |= INODE_CHANGED;
250 if (ce->st_size != (unsigned int)st->st_size)
251 changed |= DATA_CHANGED;
252 return changed;
255 int cache_name_compare(const char *name1, int len1, const char *name2, int len2)
257 int len = len1 < len2 ? len1 : len2;
258 int cmp;
260 cmp = memcmp(name1, name2, len);
261 if (cmp)
262 return cmp;
263 if (len1 < len2)
264 return -1;
265 if (len1 > len2)
266 return 1;
267 return 0;
270 int cache_name_pos(const char *name, int namelen)
272 int first, last;
274 first = 0;
275 last = active_nr;
276 while (last > first) {
277 int next = (last + first) >> 1;
278 struct cache_entry *ce = active_cache[next];
279 int cmp = cache_name_compare(name, namelen, ce->name, ce->namelen);
280 if (!cmp)
281 return next;
282 if (cmp < 0) {
283 last = next;
284 continue;
286 first = next+1;
288 return -first-1;
291 int remove_file_from_cache(char *path)
293 int pos = cache_name_pos(path, strlen(path));
294 if (pos >= 0) {
295 active_nr--;
296 if (pos < active_nr)
297 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
299 return 0;
302 int add_cache_entry(struct cache_entry *ce, int ok_to_add)
304 int pos;
306 pos = cache_name_pos(ce->name, ce->namelen);
308 /* existing match? Just replace it */
309 if (pos >= 0) {
310 active_cache[pos] = ce;
311 return 0;
313 pos = -pos-1;
315 if (!ok_to_add)
316 return -1;
318 /* Make sure the array is big enough .. */
319 if (active_nr == active_alloc) {
320 active_alloc = alloc_nr(active_alloc);
321 active_cache = realloc(active_cache, active_alloc * sizeof(struct cache_entry *));
324 /* Add it in.. */
325 active_nr++;
326 if (active_nr > pos)
327 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
328 active_cache[pos] = ce;
329 return 0;
332 static int verify_hdr(struct cache_header *hdr, unsigned long size)
334 SHA_CTX c;
335 unsigned char sha1[20];
337 if (hdr->signature != CACHE_SIGNATURE)
338 return error("bad signature");
339 if (hdr->version != 1)
340 return error("bad version");
341 SHA1_Init(&c);
342 SHA1_Update(&c, hdr, offsetof(struct cache_header, sha1));
343 SHA1_Update(&c, hdr+1, size - sizeof(*hdr));
344 SHA1_Final(sha1, &c);
345 if (memcmp(sha1, hdr->sha1, 20))
346 return error("bad header sha1");
347 return 0;
350 int read_cache(void)
352 int fd, i;
353 struct stat st;
354 unsigned long size, offset;
355 void *map;
356 struct cache_header *hdr;
358 errno = EBUSY;
359 if (active_cache)
360 return error("more than one cachefile");
361 errno = ENOENT;
362 sha1_file_directory = getenv(DB_ENVIRONMENT);
363 if (!sha1_file_directory)
364 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
365 if (access(sha1_file_directory, X_OK) < 0)
366 return error("no access to SHA1 file directory");
367 fd = open(".git/index", O_RDONLY);
368 if (fd < 0)
369 return (errno == ENOENT) ? 0 : error("open failed");
371 size = 0; // avoid gcc warning
372 map = (void *)-1;
373 if (!fstat(fd, &st)) {
374 size = st.st_size;
375 errno = EINVAL;
376 if (size >= sizeof(struct cache_header))
377 map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
379 close(fd);
380 if (-1 == (int)(long)map)
381 return error("mmap failed");
383 hdr = map;
384 if (verify_hdr(hdr, size) < 0)
385 goto unmap;
387 active_nr = hdr->entries;
388 active_alloc = alloc_nr(active_nr);
389 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
391 offset = sizeof(*hdr);
392 for (i = 0; i < hdr->entries; i++) {
393 struct cache_entry *ce = map + offset;
394 offset = offset + ce_size(ce);
395 active_cache[i] = ce;
397 return active_nr;
399 unmap:
400 munmap(map, size);
401 errno = EINVAL;
402 return error("verify header failed");
405 int write_cache(int newfd, struct cache_entry **cache, int entries)
407 SHA_CTX c;
408 struct cache_header hdr;
409 int i;
411 hdr.signature = CACHE_SIGNATURE;
412 hdr.version = 1;
413 hdr.entries = entries;
415 SHA1_Init(&c);
416 SHA1_Update(&c, &hdr, offsetof(struct cache_header, sha1));
417 for (i = 0; i < entries; i++) {
418 struct cache_entry *ce = cache[i];
419 int size = ce_size(ce);
420 SHA1_Update(&c, ce, size);
422 SHA1_Final(hdr.sha1, &c);
424 if (write(newfd, &hdr, sizeof(hdr)) != sizeof(hdr))
425 return -1;
427 for (i = 0; i < entries; i++) {
428 struct cache_entry *ce = cache[i];
429 int size = ce_size(ce);
430 if (write(newfd, ce, size) != size)
431 return -1;
433 return 0;