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 * read_sha1_file(unsigned char *sha1, char *type, unsigned long *size)
94 char *filename = sha1_file_name(sha1);
96 fd = open(filename, O_RDONLY);
101 if (fstat(fd, &st) < 0) {
105 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
107 if (-1 == (int)(long)map)
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);
117 inflateInit(&stream);
118 ret = inflate(&stream, 0);
119 if (sscanf(buffer, "%10s %lu", type, size) != 2)
121 bytes = strlen(buffer) + 1;
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)
138 int write_sha1_file(char *buf, unsigned len)
143 unsigned char sha1[20];
147 memset(&stream, 0, sizeof(stream));
148 deflateInit(&stream, Z_BEST_COMPRESSION);
149 size = deflateBound(&stream, len);
150 compressed = malloc(size);
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)
160 size = stream.total_out;
164 SHA1_Update(&c, compressed, size);
165 SHA1_Final(sha1, &c);
167 if (write_sha1_buffer(sha1, compressed, size) < 0)
169 printf("%s\n", sha1_to_hex(sha1));
173 int write_sha1_buffer(unsigned char *sha1, void *buf, unsigned int size)
175 char *filename = sha1_file_name(sha1);
178 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
180 return (errno == EEXIST) ? 0 : -1;
181 write(fd, buf, size);
186 static int error(const char * string)
188 fprintf(stderr, "error: %s\n", string);
192 static int verify_hdr(struct cache_header *hdr, unsigned long size)
195 unsigned char sha1[20];
197 if (hdr->signature != CACHE_SIGNATURE)
198 return error("bad signature");
199 if (hdr->version != 1)
200 return error("bad version");
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");
214 unsigned long size, offset;
216 struct cache_header *hdr;
220 return error("more than one cachefile");
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);
229 return (errno == ENOENT) ? 0 : error("open failed");
231 size = 0; // avoid gcc warning
233 if (!fstat(fd, &st)) {
237 if (size > sizeof(struct cache_header))
238 map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
241 if (-1 == (int)(long)map)
242 return error("mmap failed");
245 if (verify_hdr(hdr, size) < 0)
248 active_nr = hdr->entries;
249 active_alloc = alloc_nr(active_nr);
250 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
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;
263 return error("verify header failed");