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 int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size)
89 unsigned char real_sha1[20];
93 SHA1_Update(&c, map, size);
94 SHA1_Final(real_sha1, &c);
95 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
98 void *map_sha1_file(unsigned char *sha1, unsigned long *size)
100 char *filename = sha1_file_name(sha1);
101 int fd = open(filename, O_RDONLY);
109 if (fstat(fd, &st) < 0) {
113 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
115 if (-1 == (int)(long)map)
121 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
128 /* Get the data stream */
129 memset(&stream, 0, sizeof(stream));
130 stream.next_in = map;
131 stream.avail_in = mapsize;
132 stream.next_out = buffer;
133 stream.avail_out = sizeof(buffer);
135 inflateInit(&stream);
136 ret = inflate(&stream, 0);
137 if (sscanf(buffer, "%10s %lu", type, size) != 2)
140 bytes = strlen(buffer) + 1;
145 memcpy(buf, buffer + bytes, stream.total_out - bytes);
146 bytes = stream.total_out - bytes;
147 if (bytes < *size && ret == Z_OK) {
148 stream.next_out = buf + bytes;
149 stream.avail_out = *size - bytes;
150 while (inflate(&stream, Z_FINISH) == Z_OK)
157 void * read_sha1_file(unsigned char *sha1, char *type, unsigned long *size)
159 unsigned long mapsize;
162 map = map_sha1_file(sha1, &mapsize);
164 buf = unpack_sha1_file(map, mapsize, type, size);
165 munmap(map, mapsize);
171 int write_sha1_file(char *buf, unsigned len)
176 unsigned char sha1[20];
180 memset(&stream, 0, sizeof(stream));
181 deflateInit(&stream, Z_BEST_COMPRESSION);
182 size = deflateBound(&stream, len);
183 compressed = malloc(size);
186 stream.next_in = buf;
187 stream.avail_in = len;
188 stream.next_out = compressed;
189 stream.avail_out = size;
190 while (deflate(&stream, Z_FINISH) == Z_OK)
193 size = stream.total_out;
197 SHA1_Update(&c, compressed, size);
198 SHA1_Final(sha1, &c);
200 if (write_sha1_buffer(sha1, compressed, size) < 0)
202 printf("%s\n", sha1_to_hex(sha1));
206 int write_sha1_buffer(unsigned char *sha1, void *buf, unsigned int size)
208 char *filename = sha1_file_name(sha1);
211 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
213 return (errno == EEXIST) ? 0 : -1;
214 write(fd, buf, size);
219 static int error(const char * string)
221 fprintf(stderr, "error: %s\n", string);
225 int cache_match_stat(struct cache_entry *ce, struct stat *st)
227 unsigned int changed = 0;
229 if (ce->mtime.sec != (unsigned int)st->st_mtim.tv_sec ||
230 ce->mtime.nsec != (unsigned int)st->st_mtim.tv_nsec)
231 changed |= MTIME_CHANGED;
232 if (ce->ctime.sec != (unsigned int)st->st_ctim.tv_sec ||
233 ce->ctime.nsec != (unsigned int)st->st_ctim.tv_nsec)
234 changed |= CTIME_CHANGED;
235 if (ce->st_uid != (unsigned int)st->st_uid ||
236 ce->st_gid != (unsigned int)st->st_gid)
237 changed |= OWNER_CHANGED;
238 if (ce->st_mode != (unsigned int)st->st_mode)
239 changed |= MODE_CHANGED;
240 if (ce->st_dev != (unsigned int)st->st_dev ||
241 ce->st_ino != (unsigned int)st->st_ino)
242 changed |= INODE_CHANGED;
243 if (ce->st_size != (unsigned int)st->st_size)
244 changed |= DATA_CHANGED;
248 static int cache_name_compare(const char *name1, int len1, const char *name2, int len2)
250 int len = len1 < len2 ? len1 : len2;
253 cmp = memcmp(name1, name2, len);
263 int cache_name_pos(const char *name, int namelen)
269 while (last > first) {
270 int next = (last + first) >> 1;
271 struct cache_entry *ce = active_cache[next];
272 int cmp = cache_name_compare(name, namelen, ce->name, ce->namelen);
284 static int verify_hdr(struct cache_header *hdr, unsigned long size)
287 unsigned char sha1[20];
289 if (hdr->signature != CACHE_SIGNATURE)
290 return error("bad signature");
291 if (hdr->version != 1)
292 return error("bad version");
294 SHA1_Update(&c, hdr, offsetof(struct cache_header, sha1));
295 SHA1_Update(&c, hdr+1, size - sizeof(*hdr));
296 SHA1_Final(sha1, &c);
297 if (memcmp(sha1, hdr->sha1, 20))
298 return error("bad header sha1");
306 unsigned long size, offset;
308 struct cache_header *hdr;
312 return error("more than one cachefile");
314 sha1_file_directory = getenv(DB_ENVIRONMENT);
315 if (!sha1_file_directory)
316 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
317 if (access(sha1_file_directory, X_OK) < 0)
318 return error("no access to SHA1 file directory");
319 fd = open(".dircache/index", O_RDONLY);
321 return (errno == ENOENT) ? 0 : error("open failed");
323 size = 0; // avoid gcc warning
325 if (!fstat(fd, &st)) {
328 if (size >= sizeof(struct cache_header))
329 map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
332 if (-1 == (int)(long)map)
333 return error("mmap failed");
336 if (verify_hdr(hdr, size) < 0)
339 active_nr = hdr->entries;
340 active_alloc = alloc_nr(active_nr);
341 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
343 offset = sizeof(*hdr);
344 for (i = 0; i < hdr->entries; i++) {
345 struct cache_entry *ce = map + offset;
346 offset = offset + ce_size(ce);
347 active_cache[i] = ce;
354 return error("verify header failed");