2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
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, ...)
18 vsnprintf(string, sizeof(string), err, args);
20 fprintf(stderr, "%s\n", string);
24 static unsigned hexval(char c)
26 if (c >= '0' && c <= '9')
28 if (c >= 'a' && c <= 'f')
30 if (c >= 'A' && c <= 'F')
35 int get_sha1_hex(const char *hex, unsigned char *sha1)
38 for (i = 0; i < 20; i++) {
39 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
48 char * sha1_to_hex(const unsigned char *sha1)
50 static char buffer[50];
51 static const char hex[] = "0123456789abcdef";
55 for (i = 0; i < 20; i++) {
56 unsigned int val = *sha1++;
57 *buf++ = hex[val >> 4];
58 *buf++ = hex[val & 0xf];
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
68 char *sha1_file_name(const unsigned char *sha1)
71 static char *name, *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);
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];
93 int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size)
95 unsigned char real_sha1[20];
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);
115 if (fstat(fd, &st) < 0) {
119 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
121 if (-1 == (int)(long)map)
127 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
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)
146 bytes = strlen(buffer) + 1;
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)
163 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
165 unsigned long mapsize;
168 map = map_sha1_file(sha1, &mapsize);
170 buf = unpack_sha1_file(map, mapsize, type, size);
171 munmap(map, mapsize);
177 int write_sha1_file(char *buf, unsigned len, unsigned char *returnsha1)
182 unsigned char sha1[20];
186 memset(&stream, 0, sizeof(stream));
187 deflateInit(&stream, Z_BEST_COMPRESSION);
188 size = deflateBound(&stream, len);
189 compressed = malloc(size);
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)
199 size = stream.total_out;
203 SHA1_Update(&c, compressed, size);
204 SHA1_Final(sha1, &c);
206 if (write_sha1_buffer(sha1, compressed, size) < 0)
209 memcpy(returnsha1, sha1, 20);
213 int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
215 char *filename = sha1_file_name(sha1);
218 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
221 static int error(const char * string);
225 #ifndef COLLISION_CHECK
226 fd = open(filename, O_RDONLY);
229 map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
230 if (map == MAP_FAILED)
232 if (memcmp(buf, map, size))
233 return error("SHA1 collision detected!"
234 " This is bad, bad, BAD!\a\n");
238 write(fd, buf, size);
243 static int error(const char * string)
245 fprintf(stderr, "error: %s\n", string);
249 int cache_match_stat(struct cache_entry *ce, struct stat *st)
251 unsigned int changed = 0;
253 /* nsec seems unreliable - not all filesystems support it, so
254 * as long as it is in the inode cache you get right nsec
255 * but after it gets flushed, you get zero nsec. */
256 if (ce->mtime.sec != (unsigned int)st->st_mtim.tv_sec
258 || ce->mtime.nsec != (unsigned int)st->st_mtim.tv_nsec
261 changed |= MTIME_CHANGED;
262 if (ce->ctime.sec != (unsigned int)st->st_ctim.tv_sec
264 || ce->ctime.nsec != (unsigned int)st->st_ctim.tv_nsec
267 changed |= CTIME_CHANGED;
268 if (ce->st_uid != (unsigned int)st->st_uid ||
269 ce->st_gid != (unsigned int)st->st_gid)
270 changed |= OWNER_CHANGED;
271 if (ce->st_mode != (unsigned int)st->st_mode)
272 changed |= MODE_CHANGED;
273 if (ce->st_dev != (unsigned int)st->st_dev ||
274 ce->st_ino != (unsigned int)st->st_ino)
275 changed |= INODE_CHANGED;
276 if (ce->st_size != (unsigned int)st->st_size)
277 changed |= DATA_CHANGED;
281 int cache_name_compare(const char *name1, int len1, const char *name2, int len2)
283 int len = len1 < len2 ? len1 : len2;
286 cmp = memcmp(name1, name2, len);
296 int cache_name_pos(const char *name, int namelen)
302 while (last > first) {
303 int next = (last + first) >> 1;
304 struct cache_entry *ce = active_cache[next];
305 int cmp = cache_name_compare(name, namelen, ce->name, ce->namelen);
317 int remove_file_from_cache(char *path)
319 int pos = cache_name_pos(path, strlen(path));
323 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
328 int add_cache_entry(struct cache_entry *ce, int ok_to_add)
332 pos = cache_name_pos(ce->name, ce->namelen);
334 /* existing match? Just replace it */
336 active_cache[pos] = ce;
344 /* Make sure the array is big enough .. */
345 if (active_nr == active_alloc) {
346 active_alloc = alloc_nr(active_alloc);
347 active_cache = realloc(active_cache, active_alloc * sizeof(struct cache_entry *));
353 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
354 active_cache[pos] = ce;
358 static int verify_hdr(struct cache_header *hdr, unsigned long size)
361 unsigned char sha1[20];
363 if (hdr->signature != CACHE_SIGNATURE)
364 return error("bad signature");
365 if (hdr->version != 1)
366 return error("bad version");
368 SHA1_Update(&c, hdr, offsetof(struct cache_header, sha1));
369 SHA1_Update(&c, hdr+1, size - sizeof(*hdr));
370 SHA1_Final(sha1, &c);
371 if (memcmp(sha1, hdr->sha1, 20))
372 return error("bad header sha1");
380 unsigned long size, offset;
382 struct cache_header *hdr;
386 return error("more than one cachefile");
388 sha1_file_directory = getenv(DB_ENVIRONMENT);
389 if (!sha1_file_directory)
390 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
391 if (access(sha1_file_directory, X_OK) < 0)
392 return error("no access to SHA1 file directory");
393 fd = open(".git/index", O_RDONLY);
395 return (errno == ENOENT) ? 0 : error("open failed");
397 size = 0; // avoid gcc warning
399 if (!fstat(fd, &st)) {
402 if (size >= sizeof(struct cache_header))
403 map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
406 if (-1 == (int)(long)map)
407 return error("mmap failed");
410 if (verify_hdr(hdr, size) < 0)
413 active_nr = hdr->entries;
414 active_alloc = alloc_nr(active_nr);
415 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
417 offset = sizeof(*hdr);
418 for (i = 0; i < hdr->entries; i++) {
419 struct cache_entry *ce = map + offset;
420 offset = offset + ce_size(ce);
421 active_cache[i] = ce;
428 return error("verify header failed");
431 int write_cache(int newfd, struct cache_entry **cache, int entries)
434 struct cache_header hdr;
437 hdr.signature = CACHE_SIGNATURE;
439 hdr.entries = entries;
442 SHA1_Update(&c, &hdr, offsetof(struct cache_header, sha1));
443 for (i = 0; i < entries; i++) {
444 struct cache_entry *ce = cache[i];
445 int size = ce_size(ce);
446 SHA1_Update(&c, ce, size);
448 SHA1_Final(hdr.sha1, &c);
450 if (write(newfd, &hdr, sizeof(hdr)) != sizeof(hdr))
453 for (i = 0; i < entries; i++) {
454 struct cache_entry *ce = cache[i];
455 int size = ce_size(ce);
456 if (write(newfd, ce, size) != size)