Add "diff-tree" program to show which files have changed between two trees.
[git/debian.git] / read-cache.c
blobac8375a7f0e89dca5a2af30fce52f3b766dd153c
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 fprintf(stderr, "read-tree: %s\n", err);
15 exit(1);
18 static unsigned hexval(char c)
20 if (c >= '0' && c <= '9')
21 return c - '0';
22 if (c >= 'a' && c <= 'f')
23 return c - 'a' + 10;
24 if (c >= 'A' && c <= 'F')
25 return c - 'A' + 10;
26 return ~0;
29 int get_sha1_hex(const char *hex, unsigned char *sha1)
31 int i;
32 for (i = 0; i < 20; i++) {
33 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
34 if (val & ~0xff)
35 return -1;
36 *sha1++ = val;
37 hex += 2;
39 return 0;
42 char * sha1_to_hex(const unsigned char *sha1)
44 static char buffer[50];
45 static const char hex[] = "0123456789abcdef";
46 char *buf = buffer;
47 int i;
49 for (i = 0; i < 20; i++) {
50 unsigned int val = *sha1++;
51 *buf++ = hex[val >> 4];
52 *buf++ = hex[val & 0xf];
54 return buffer;
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
60 * filename.
62 char *sha1_file_name(unsigned char *sha1)
64 int i;
65 static char *name, *base;
67 if (!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);
73 base[len] = '/';
74 base[len+3] = '/';
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];
84 return base;
87 int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size)
89 unsigned char real_sha1[20];
90 SHA_CTX c;
92 SHA1_Init(&c);
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);
102 struct stat st;
103 void *map;
105 if (fd < 0) {
106 perror(filename);
107 return NULL;
109 if (fstat(fd, &st) < 0) {
110 close(fd);
111 return NULL;
113 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
114 close(fd);
115 if (-1 == (int)(long)map)
116 return NULL;
117 *size = st.st_size;
118 return map;
121 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
123 int ret, bytes;
124 z_stream stream;
125 char buffer[8192];
126 char *buf;
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)
138 return NULL;
140 bytes = strlen(buffer) + 1;
141 buf = malloc(*size);
142 if (!buf)
143 return NULL;
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)
151 /* nothing */;
153 inflateEnd(&stream);
154 return buf;
157 void * read_sha1_file(unsigned char *sha1, char *type, unsigned long *size)
159 unsigned long mapsize;
160 void *map, *buf;
162 map = map_sha1_file(sha1, &mapsize);
163 if (map) {
164 buf = unpack_sha1_file(map, mapsize, type, size);
165 munmap(map, mapsize);
166 return buf;
168 return NULL;
171 int write_sha1_file(char *buf, unsigned len)
173 int size;
174 char *compressed;
175 z_stream stream;
176 unsigned char sha1[20];
177 SHA_CTX c;
179 /* Set it up */
180 memset(&stream, 0, sizeof(stream));
181 deflateInit(&stream, Z_BEST_COMPRESSION);
182 size = deflateBound(&stream, len);
183 compressed = malloc(size);
185 /* Compress it */
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)
191 /* nothing */;
192 deflateEnd(&stream);
193 size = stream.total_out;
195 /* Sha1.. */
196 SHA1_Init(&c);
197 SHA1_Update(&c, compressed, size);
198 SHA1_Final(sha1, &c);
200 if (write_sha1_buffer(sha1, compressed, size) < 0)
201 return -1;
202 printf("%s\n", sha1_to_hex(sha1));
203 return 0;
206 int write_sha1_buffer(unsigned char *sha1, void *buf, unsigned int size)
208 char *filename = sha1_file_name(sha1);
209 int fd;
211 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
212 if (fd < 0)
213 return (errno == EEXIST) ? 0 : -1;
214 write(fd, buf, size);
215 close(fd);
216 return 0;
219 static int error(const char * string)
221 fprintf(stderr, "error: %s\n", string);
222 return -1;
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;
245 return changed;
248 int cache_name_compare(const char *name1, int len1, const char *name2, int len2)
250 int len = len1 < len2 ? len1 : len2;
251 int cmp;
253 cmp = memcmp(name1, name2, len);
254 if (cmp)
255 return cmp;
256 if (len1 < len2)
257 return -1;
258 if (len1 > len2)
259 return 1;
260 return 0;
263 int cache_name_pos(const char *name, int namelen)
265 int first, last;
267 first = 0;
268 last = active_nr;
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);
273 if (!cmp)
274 return -next-1;
275 if (cmp < 0) {
276 last = next;
277 continue;
279 first = next+1;
281 return first;
284 int remove_file_from_cache(char *path)
286 int pos = cache_name_pos(path, strlen(path));
287 if (pos < 0) {
288 pos = -pos-1;
289 active_nr--;
290 if (pos < active_nr)
291 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos - 1) * sizeof(struct cache_entry *));
293 return 0;
296 int add_cache_entry(struct cache_entry *ce)
298 int pos;
300 pos = cache_name_pos(ce->name, ce->namelen);
302 /* existing match? Just replace it */
303 if (pos < 0) {
304 active_cache[-pos-1] = ce;
305 return 0;
308 /* Make sure the array is big enough .. */
309 if (active_nr == active_alloc) {
310 active_alloc = alloc_nr(active_alloc);
311 active_cache = realloc(active_cache, active_alloc * sizeof(struct cache_entry *));
314 /* Add it in.. */
315 active_nr++;
316 if (active_nr > pos)
317 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
318 active_cache[pos] = ce;
319 return 0;
322 static int verify_hdr(struct cache_header *hdr, unsigned long size)
324 SHA_CTX c;
325 unsigned char sha1[20];
327 if (hdr->signature != CACHE_SIGNATURE)
328 return error("bad signature");
329 if (hdr->version != 1)
330 return error("bad version");
331 SHA1_Init(&c);
332 SHA1_Update(&c, hdr, offsetof(struct cache_header, sha1));
333 SHA1_Update(&c, hdr+1, size - sizeof(*hdr));
334 SHA1_Final(sha1, &c);
335 if (memcmp(sha1, hdr->sha1, 20))
336 return error("bad header sha1");
337 return 0;
340 int read_cache(void)
342 int fd, i;
343 struct stat st;
344 unsigned long size, offset;
345 void *map;
346 struct cache_header *hdr;
348 errno = EBUSY;
349 if (active_cache)
350 return error("more than one cachefile");
351 errno = ENOENT;
352 sha1_file_directory = getenv(DB_ENVIRONMENT);
353 if (!sha1_file_directory)
354 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
355 if (access(sha1_file_directory, X_OK) < 0)
356 return error("no access to SHA1 file directory");
357 fd = open(".dircache/index", O_RDONLY);
358 if (fd < 0)
359 return (errno == ENOENT) ? 0 : error("open failed");
361 size = 0; // avoid gcc warning
362 map = (void *)-1;
363 if (!fstat(fd, &st)) {
364 size = st.st_size;
365 errno = EINVAL;
366 if (size >= sizeof(struct cache_header))
367 map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
369 close(fd);
370 if (-1 == (int)(long)map)
371 return error("mmap failed");
373 hdr = map;
374 if (verify_hdr(hdr, size) < 0)
375 goto unmap;
377 active_nr = hdr->entries;
378 active_alloc = alloc_nr(active_nr);
379 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
381 offset = sizeof(*hdr);
382 for (i = 0; i < hdr->entries; i++) {
383 struct cache_entry *ce = map + offset;
384 offset = offset + ce_size(ce);
385 active_cache[i] = ce;
387 return active_nr;
389 unmap:
390 munmap(map, size);
391 errno = EINVAL;
392 return error("verify header failed");
395 int write_cache(int newfd, struct cache_entry **cache, int entries)
397 SHA_CTX c;
398 struct cache_header hdr;
399 int i;
401 hdr.signature = CACHE_SIGNATURE;
402 hdr.version = 1;
403 hdr.entries = entries;
405 SHA1_Init(&c);
406 SHA1_Update(&c, &hdr, offsetof(struct cache_header, sha1));
407 for (i = 0; i < entries; i++) {
408 struct cache_entry *ce = cache[i];
409 int size = ce_size(ce);
410 SHA1_Update(&c, ce, size);
412 SHA1_Final(hdr.sha1, &c);
414 if (write(newfd, &hdr, sizeof(hdr)) != sizeof(hdr))
415 return -1;
417 for (i = 0; i < entries; i++) {
418 struct cache_entry *ce = cache[i];
419 int size = ce_size(ce);
420 if (write(newfd, ce, size) != size)
421 return -1;
423 return 0;