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)
14 fprintf(stderr, "read-tree: %s\n", err);
18 static unsigned hexval(char c)
20 if (c >= '0' && c <= '9')
22 if (c >= 'a' && c <= 'f')
24 if (c >= 'A' && c <= 'F')
29 int get_sha1_hex(char *hex, unsigned char *sha1)
32 for (i = 0; i < 20; i++) {
33 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
42 char * sha1_to_hex(unsigned char *sha1)
44 static char buffer[50];
45 static const char hex[] = "0123456789abcdef";
49 for (i = 0; i < 20; i++) {
50 unsigned int val = *sha1++;
51 *buf++ = hex[val >> 4];
52 *buf++ = hex[val & 0xf];
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
62 char *sha1_file_name(unsigned char *sha1)
65 static char *name, *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);
75 name = base + len + 1;
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];
87 void *map_sha1_file(unsigned char *sha1, unsigned long *size)
89 char *filename = sha1_file_name(sha1);
90 int fd = open(filename, O_RDONLY);
98 if (fstat(fd, &st) < 0) {
102 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
104 if (-1 == (int)(long)map)
110 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
117 /* Get the data stream */
118 memset(&stream, 0, sizeof(stream));
119 stream.next_in = map;
120 stream.avail_in = mapsize;
121 stream.next_out = buffer;
122 stream.avail_out = sizeof(buffer);
124 inflateInit(&stream);
125 ret = inflate(&stream, 0);
126 if (sscanf(buffer, "%10s %lu", type, size) != 2)
129 bytes = strlen(buffer) + 1;
134 memcpy(buf, buffer + bytes, stream.total_out - bytes);
135 bytes = stream.total_out - bytes;
136 if (bytes < *size && ret == Z_OK) {
137 stream.next_out = buf + bytes;
138 stream.avail_out = *size - bytes;
139 while (inflate(&stream, Z_FINISH) == Z_OK)
146 void * read_sha1_file(unsigned char *sha1, char *type, unsigned long *size)
148 unsigned long mapsize;
151 map = map_sha1_file(sha1, &mapsize);
153 buf = unpack_sha1_file(map, mapsize, type, size);
154 munmap(map, mapsize);
160 int write_sha1_file(char *buf, unsigned len)
165 unsigned char sha1[20];
169 memset(&stream, 0, sizeof(stream));
170 deflateInit(&stream, Z_BEST_COMPRESSION);
171 size = deflateBound(&stream, len);
172 compressed = malloc(size);
175 stream.next_in = buf;
176 stream.avail_in = len;
177 stream.next_out = compressed;
178 stream.avail_out = size;
179 while (deflate(&stream, Z_FINISH) == Z_OK)
182 size = stream.total_out;
186 SHA1_Update(&c, compressed, size);
187 SHA1_Final(sha1, &c);
189 if (write_sha1_buffer(sha1, compressed, size) < 0)
191 printf("%s\n", sha1_to_hex(sha1));
195 int write_sha1_buffer(unsigned char *sha1, void *buf, unsigned int size)
197 char *filename = sha1_file_name(sha1);
200 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
202 return (errno == EEXIST) ? 0 : -1;
203 write(fd, buf, size);
208 static int error(const char * string)
210 fprintf(stderr, "error: %s\n", string);
214 static int verify_hdr(struct cache_header *hdr, unsigned long size)
217 unsigned char sha1[20];
219 if (hdr->signature != CACHE_SIGNATURE)
220 return error("bad signature");
221 if (hdr->version != 1)
222 return error("bad version");
224 SHA1_Update(&c, hdr, offsetof(struct cache_header, sha1));
225 SHA1_Update(&c, hdr+1, size - sizeof(*hdr));
226 SHA1_Final(sha1, &c);
227 if (memcmp(sha1, hdr->sha1, 20))
228 return error("bad header sha1");
236 unsigned long size, offset;
238 struct cache_header *hdr;
242 return error("more than one cachefile");
244 sha1_file_directory = getenv(DB_ENVIRONMENT);
245 if (!sha1_file_directory)
246 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
247 if (access(sha1_file_directory, X_OK) < 0)
248 return error("no access to SHA1 file directory");
249 fd = open(".dircache/index", O_RDONLY);
251 return (errno == ENOENT) ? 0 : error("open failed");
253 size = 0; // avoid gcc warning
255 if (!fstat(fd, &st)) {
259 if (size > sizeof(struct cache_header))
260 map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
263 if (-1 == (int)(long)map)
264 return error("mmap failed");
267 if (verify_hdr(hdr, size) < 0)
270 active_nr = hdr->entries;
271 active_alloc = alloc_nr(active_nr);
272 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
274 offset = sizeof(*hdr);
275 for (i = 0; i < hdr->entries; i++) {
276 struct cache_entry *ce = map + offset;
277 offset = offset + ce_size(ce);
278 active_cache[i] = ce;
285 return error("verify header failed");