Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
2de381f9 | 6 | #include <stdarg.h> |
e83c5163 LT |
7 | #include "cache.h" |
8 | ||
9 | const char *sha1_file_directory = NULL; | |
10 | struct cache_entry **active_cache = NULL; | |
11 | unsigned int active_nr = 0, active_alloc = 0; | |
12 | ||
2de381f9 | 13 | void usage(const char *err) |
e83c5163 | 14 | { |
2de381f9 PB |
15 | fprintf(stderr, "usage: %s\n", err); |
16 | exit(1); | |
17 | } | |
18 | ||
19 | static void report(const char *prefix, const char *err, va_list params) | |
20 | { | |
21 | fputs(prefix, stderr); | |
22 | vfprintf(stderr, err, params); | |
23 | fputs("\n", stderr); | |
24 | } | |
121481ab | 25 | |
2de381f9 PB |
26 | void die(const char *err, ...) |
27 | { | |
28 | va_list params; | |
29 | ||
30 | va_start(params, err); | |
31 | report("fatal: ", err, params); | |
32 | va_end(params); | |
e83c5163 LT |
33 | exit(1); |
34 | } | |
35 | ||
2de381f9 PB |
36 | int error(const char *err, ...) |
37 | { | |
38 | va_list params; | |
39 | ||
40 | va_start(params, err); | |
41 | report("error: ", err, params); | |
42 | va_end(params); | |
43 | return -1; | |
44 | } | |
45 | ||
46 | ||
e83c5163 LT |
47 | static unsigned hexval(char c) |
48 | { | |
49 | if (c >= '0' && c <= '9') | |
50 | return c - '0'; | |
51 | if (c >= 'a' && c <= 'f') | |
52 | return c - 'a' + 10; | |
53 | if (c >= 'A' && c <= 'F') | |
54 | return c - 'A' + 10; | |
55 | return ~0; | |
56 | } | |
57 | ||
197ee8c9 | 58 | int get_sha1_hex(const char *hex, unsigned char *sha1) |
e83c5163 LT |
59 | { |
60 | int i; | |
61 | for (i = 0; i < 20; i++) { | |
62 | unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]); | |
63 | if (val & ~0xff) | |
64 | return -1; | |
65 | *sha1++ = val; | |
66 | hex += 2; | |
67 | } | |
68 | return 0; | |
69 | } | |
70 | ||
197ee8c9 | 71 | char * sha1_to_hex(const unsigned char *sha1) |
e83c5163 LT |
72 | { |
73 | static char buffer[50]; | |
74 | static const char hex[] = "0123456789abcdef"; | |
75 | char *buf = buffer; | |
76 | int i; | |
77 | ||
78 | for (i = 0; i < 20; i++) { | |
79 | unsigned int val = *sha1++; | |
80 | *buf++ = hex[val >> 4]; | |
81 | *buf++ = hex[val & 0xf]; | |
82 | } | |
83 | return buffer; | |
84 | } | |
85 | ||
86 | /* | |
87 | * NOTE! This returns a statically allocated buffer, so you have to be | |
88 | * careful about using it. Do a "strdup()" if you need to save the | |
89 | * filename. | |
90 | */ | |
73134b6d | 91 | char *sha1_file_name(const unsigned char *sha1) |
e83c5163 LT |
92 | { |
93 | int i; | |
94 | static char *name, *base; | |
95 | ||
96 | if (!base) { | |
97 | char *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT; | |
98 | int len = strlen(sha1_file_directory); | |
99 | base = malloc(len + 60); | |
100 | memcpy(base, sha1_file_directory, len); | |
101 | memset(base+len, 0, 60); | |
102 | base[len] = '/'; | |
103 | base[len+3] = '/'; | |
104 | name = base + len + 1; | |
105 | } | |
106 | for (i = 0; i < 20; i++) { | |
107 | static char hex[] = "0123456789abcdef"; | |
108 | unsigned int val = sha1[i]; | |
109 | char *pos = name + i*2 + (i > 0); | |
110 | *pos++ = hex[val >> 4]; | |
111 | *pos = hex[val & 0xf]; | |
112 | } | |
113 | return base; | |
114 | } | |
115 | ||
2ade9340 LT |
116 | int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size) |
117 | { | |
118 | unsigned char real_sha1[20]; | |
119 | SHA_CTX c; | |
120 | ||
121 | SHA1_Init(&c); | |
122 | SHA1_Update(&c, map, size); | |
123 | SHA1_Final(real_sha1, &c); | |
124 | return memcmp(sha1, real_sha1, 20) ? -1 : 0; | |
125 | } | |
126 | ||
73134b6d | 127 | void *map_sha1_file(const unsigned char *sha1, unsigned long *size) |
e83c5163 | 128 | { |
e83c5163 | 129 | char *filename = sha1_file_name(sha1); |
24778e33 LT |
130 | int fd = open(filename, O_RDONLY); |
131 | struct stat st; | |
132 | void *map; | |
e83c5163 | 133 | |
e83c5163 LT |
134 | if (fd < 0) { |
135 | perror(filename); | |
136 | return NULL; | |
137 | } | |
138 | if (fstat(fd, &st) < 0) { | |
aebb2679 | 139 | close(fd); |
e83c5163 LT |
140 | return NULL; |
141 | } | |
142 | map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
143 | close(fd); | |
144 | if (-1 == (int)(long)map) | |
145 | return NULL; | |
24778e33 LT |
146 | *size = st.st_size; |
147 | return map; | |
148 | } | |
149 | ||
150 | void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size) | |
151 | { | |
152 | int ret, bytes; | |
153 | z_stream stream; | |
154 | char buffer[8192]; | |
155 | char *buf; | |
e83c5163 LT |
156 | |
157 | /* Get the data stream */ | |
158 | memset(&stream, 0, sizeof(stream)); | |
159 | stream.next_in = map; | |
24778e33 | 160 | stream.avail_in = mapsize; |
e83c5163 LT |
161 | stream.next_out = buffer; |
162 | stream.avail_out = sizeof(buffer); | |
163 | ||
164 | inflateInit(&stream); | |
165 | ret = inflate(&stream, 0); | |
166 | if (sscanf(buffer, "%10s %lu", type, size) != 2) | |
167 | return NULL; | |
24778e33 | 168 | |
e83c5163 LT |
169 | bytes = strlen(buffer) + 1; |
170 | buf = malloc(*size); | |
171 | if (!buf) | |
172 | return NULL; | |
173 | ||
174 | memcpy(buf, buffer + bytes, stream.total_out - bytes); | |
175 | bytes = stream.total_out - bytes; | |
176 | if (bytes < *size && ret == Z_OK) { | |
177 | stream.next_out = buf + bytes; | |
178 | stream.avail_out = *size - bytes; | |
179 | while (inflate(&stream, Z_FINISH) == Z_OK) | |
180 | /* nothing */; | |
181 | } | |
182 | inflateEnd(&stream); | |
183 | return buf; | |
184 | } | |
185 | ||
73134b6d | 186 | void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size) |
24778e33 LT |
187 | { |
188 | unsigned long mapsize; | |
189 | void *map, *buf; | |
190 | ||
191 | map = map_sha1_file(sha1, &mapsize); | |
192 | if (map) { | |
193 | buf = unpack_sha1_file(map, mapsize, type, size); | |
194 | munmap(map, mapsize); | |
195 | return buf; | |
196 | } | |
197 | return NULL; | |
198 | } | |
199 | ||
d6d3f9d0 | 200 | int write_sha1_file(char *buf, unsigned len, unsigned char *returnsha1) |
e83c5163 LT |
201 | { |
202 | int size; | |
203 | char *compressed; | |
204 | z_stream stream; | |
205 | unsigned char sha1[20]; | |
206 | SHA_CTX c; | |
207 | ||
208 | /* Set it up */ | |
209 | memset(&stream, 0, sizeof(stream)); | |
210 | deflateInit(&stream, Z_BEST_COMPRESSION); | |
211 | size = deflateBound(&stream, len); | |
212 | compressed = malloc(size); | |
213 | ||
214 | /* Compress it */ | |
215 | stream.next_in = buf; | |
216 | stream.avail_in = len; | |
217 | stream.next_out = compressed; | |
218 | stream.avail_out = size; | |
219 | while (deflate(&stream, Z_FINISH) == Z_OK) | |
220 | /* nothing */; | |
221 | deflateEnd(&stream); | |
222 | size = stream.total_out; | |
223 | ||
224 | /* Sha1.. */ | |
225 | SHA1_Init(&c); | |
226 | SHA1_Update(&c, compressed, size); | |
227 | SHA1_Final(sha1, &c); | |
228 | ||
229 | if (write_sha1_buffer(sha1, compressed, size) < 0) | |
230 | return -1; | |
d6d3f9d0 LT |
231 | if (returnsha1) |
232 | memcpy(returnsha1, sha1, 20); | |
e83c5163 LT |
233 | return 0; |
234 | } | |
235 | ||
f864ba74 LT |
236 | static inline int collision_check(char *filename, void *buf, unsigned int size) |
237 | { | |
238 | #ifdef COLLISION_CHECK | |
239 | void *map; | |
240 | int fd = open(filename, O_RDONLY); | |
241 | struct stat st; | |
242 | int cmp; | |
243 | ||
244 | /* Unreadable object, or object went away? Strange. */ | |
245 | if (fd < 0) | |
246 | return -1; | |
247 | ||
248 | if (fstat(fd, &st) < 0 || size != st.st_size) | |
249 | return -1; | |
250 | ||
251 | map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); | |
252 | close(fd); | |
253 | if (map == MAP_FAILED) | |
254 | return -1; | |
255 | cmp = memcmp(buf, map, size); | |
256 | munmap(map, size); | |
257 | if (cmp) | |
258 | return -1; | |
259 | #endif | |
260 | return 0; | |
261 | } | |
262 | ||
73134b6d | 263 | int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size) |
e83c5163 LT |
264 | { |
265 | char *filename = sha1_file_name(sha1); | |
19b2860c | 266 | int fd; |
e83c5163 LT |
267 | |
268 | fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666); | |
5c2a7fbc | 269 | if (fd < 0) { |
5c2a7fbc PB |
270 | if (errno != EEXIST) |
271 | return -1; | |
f864ba74 | 272 | if (collision_check(filename, buf, size)) |
5c2a7fbc PB |
273 | return error("SHA1 collision detected!" |
274 | " This is bad, bad, BAD!\a\n"); | |
5c2a7fbc PB |
275 | return 0; |
276 | } | |
e83c5163 LT |
277 | write(fd, buf, size); |
278 | close(fd); | |
279 | return 0; | |
280 | } | |
281 | ||
734aab75 LT |
282 | int cache_match_stat(struct cache_entry *ce, struct stat *st) |
283 | { | |
284 | unsigned int changed = 0; | |
285 | ||
ccc4feb5 | 286 | if (ce->ce_mtime.sec != htonl(st->st_mtime)) |
734aab75 | 287 | changed |= MTIME_CHANGED; |
ccc4feb5 LT |
288 | if (ce->ce_ctime.sec != htonl(st->st_ctime)) |
289 | changed |= CTIME_CHANGED; | |
290 | ||
bdd4da59 | 291 | #ifdef NSEC |
ccc4feb5 LT |
292 | /* |
293 | * nsec seems unreliable - not all filesystems support it, so | |
294 | * as long as it is in the inode cache you get right nsec | |
295 | * but after it gets flushed, you get zero nsec. | |
296 | */ | |
297 | if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec) | |
298 | changed |= MTIME_CHANGED; | |
299 | if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec) | |
734aab75 | 300 | changed |= CTIME_CHANGED; |
ccc4feb5 LT |
301 | #endif |
302 | ||
303 | if (ce->ce_uid != htonl(st->st_uid) || | |
304 | ce->ce_gid != htonl(st->st_gid)) | |
734aab75 | 305 | changed |= OWNER_CHANGED; |
e4479470 LT |
306 | /* We consider only the owner x bit to be relevant for "mode changes" */ |
307 | if (0100 & (ntohs(ce->ce_mode) ^ st->st_mode)) | |
734aab75 | 308 | changed |= MODE_CHANGED; |
ccc4feb5 LT |
309 | if (ce->ce_dev != htonl(st->st_dev) || |
310 | ce->ce_ino != htonl(st->st_ino)) | |
734aab75 | 311 | changed |= INODE_CHANGED; |
ccc4feb5 | 312 | if (ce->ce_size != htonl(st->st_size)) |
734aab75 LT |
313 | changed |= DATA_CHANGED; |
314 | return changed; | |
315 | } | |
316 | ||
95fd5bf8 | 317 | int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2) |
eb38c22f | 318 | { |
95fd5bf8 LT |
319 | int len1 = flags1 & CE_NAMEMASK; |
320 | int len2 = flags2 & CE_NAMEMASK; | |
eb38c22f LT |
321 | int len = len1 < len2 ? len1 : len2; |
322 | int cmp; | |
323 | ||
324 | cmp = memcmp(name1, name2, len); | |
325 | if (cmp) | |
326 | return cmp; | |
327 | if (len1 < len2) | |
328 | return -1; | |
329 | if (len1 > len2) | |
330 | return 1; | |
95fd5bf8 LT |
331 | if (flags1 < flags2) |
332 | return -1; | |
333 | if (flags1 > flags2) | |
334 | return 1; | |
eb38c22f LT |
335 | return 0; |
336 | } | |
337 | ||
338 | int cache_name_pos(const char *name, int namelen) | |
339 | { | |
340 | int first, last; | |
341 | ||
342 | first = 0; | |
343 | last = active_nr; | |
344 | while (last > first) { | |
345 | int next = (last + first) >> 1; | |
346 | struct cache_entry *ce = active_cache[next]; | |
95fd5bf8 | 347 | int cmp = cache_name_compare(name, namelen, ce->name, htons(ce->ce_flags)); |
eb38c22f | 348 | if (!cmp) |
76e7f4ec | 349 | return next; |
eb38c22f LT |
350 | if (cmp < 0) { |
351 | last = next; | |
352 | continue; | |
353 | } | |
354 | first = next+1; | |
355 | } | |
76e7f4ec | 356 | return -first-1; |
eb38c22f LT |
357 | } |
358 | ||
7b937ca3 LT |
359 | /* Remove entry, return true if there are more entries to go.. */ |
360 | static int remove_entry_at(int pos) | |
361 | { | |
362 | active_nr--; | |
363 | if (pos >= active_nr) | |
364 | return 0; | |
365 | memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *)); | |
366 | return 1; | |
367 | } | |
368 | ||
197ee8c9 LT |
369 | int remove_file_from_cache(char *path) |
370 | { | |
371 | int pos = cache_name_pos(path, strlen(path)); | |
7b937ca3 LT |
372 | if (pos >= 0) |
373 | remove_entry_at(pos); | |
197ee8c9 LT |
374 | return 0; |
375 | } | |
376 | ||
7b937ca3 LT |
377 | static int same_name(struct cache_entry *a, struct cache_entry *b) |
378 | { | |
379 | int len = ce_namelen(a); | |
380 | return ce_namelen(b) == len && !memcmp(a->name, b->name, len); | |
381 | } | |
382 | ||
121481ab | 383 | int add_cache_entry(struct cache_entry *ce, int ok_to_add) |
197ee8c9 LT |
384 | { |
385 | int pos; | |
386 | ||
95fd5bf8 | 387 | pos = cache_name_pos(ce->name, htons(ce->ce_flags)); |
197ee8c9 LT |
388 | |
389 | /* existing match? Just replace it */ | |
76e7f4ec LT |
390 | if (pos >= 0) { |
391 | active_cache[pos] = ce; | |
197ee8c9 LT |
392 | return 0; |
393 | } | |
76e7f4ec | 394 | pos = -pos-1; |
197ee8c9 | 395 | |
7b937ca3 LT |
396 | /* |
397 | * Inserting a merged entry ("stage 0") into the index | |
398 | * will always replace all non-merged entries.. | |
399 | */ | |
400 | if (pos < active_nr && ce_stage(ce) == 0) { | |
401 | while (same_name(active_cache[pos], ce)) { | |
402 | ok_to_add = 1; | |
403 | active_nr--; | |
404 | if (!remove_entry_at(pos)) | |
405 | break; | |
406 | } | |
407 | } | |
408 | ||
121481ab LT |
409 | if (!ok_to_add) |
410 | return -1; | |
411 | ||
197ee8c9 LT |
412 | /* Make sure the array is big enough .. */ |
413 | if (active_nr == active_alloc) { | |
414 | active_alloc = alloc_nr(active_alloc); | |
415 | active_cache = realloc(active_cache, active_alloc * sizeof(struct cache_entry *)); | |
416 | } | |
417 | ||
418 | /* Add it in.. */ | |
419 | active_nr++; | |
420 | if (active_nr > pos) | |
421 | memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce)); | |
422 | active_cache[pos] = ce; | |
423 | return 0; | |
424 | } | |
425 | ||
e83c5163 LT |
426 | static int verify_hdr(struct cache_header *hdr, unsigned long size) |
427 | { | |
428 | SHA_CTX c; | |
429 | unsigned char sha1[20]; | |
430 | ||
ccc4feb5 | 431 | if (hdr->hdr_signature != htonl(CACHE_SIGNATURE)) |
e83c5163 | 432 | return error("bad signature"); |
ccc4feb5 | 433 | if (hdr->hdr_version != htonl(1)) |
e83c5163 LT |
434 | return error("bad version"); |
435 | SHA1_Init(&c); | |
436 | SHA1_Update(&c, hdr, offsetof(struct cache_header, sha1)); | |
437 | SHA1_Update(&c, hdr+1, size - sizeof(*hdr)); | |
438 | SHA1_Final(sha1, &c); | |
439 | if (memcmp(sha1, hdr->sha1, 20)) | |
440 | return error("bad header sha1"); | |
441 | return 0; | |
442 | } | |
443 | ||
444 | int read_cache(void) | |
445 | { | |
446 | int fd, i; | |
447 | struct stat st; | |
448 | unsigned long size, offset; | |
449 | void *map; | |
450 | struct cache_header *hdr; | |
451 | ||
452 | errno = EBUSY; | |
453 | if (active_cache) | |
454 | return error("more than one cachefile"); | |
455 | errno = ENOENT; | |
456 | sha1_file_directory = getenv(DB_ENVIRONMENT); | |
457 | if (!sha1_file_directory) | |
458 | sha1_file_directory = DEFAULT_DB_ENVIRONMENT; | |
459 | if (access(sha1_file_directory, X_OK) < 0) | |
460 | return error("no access to SHA1 file directory"); | |
4bb04f21 | 461 | fd = open(".git/index", O_RDONLY); |
e83c5163 LT |
462 | if (fd < 0) |
463 | return (errno == ENOENT) ? 0 : error("open failed"); | |
464 | ||
19b2860c | 465 | size = 0; // avoid gcc warning |
e83c5163 LT |
466 | map = (void *)-1; |
467 | if (!fstat(fd, &st)) { | |
e83c5163 LT |
468 | size = st.st_size; |
469 | errno = EINVAL; | |
59c1e249 | 470 | if (size >= sizeof(struct cache_header)) |
e83c5163 LT |
471 | map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); |
472 | } | |
473 | close(fd); | |
474 | if (-1 == (int)(long)map) | |
475 | return error("mmap failed"); | |
476 | ||
477 | hdr = map; | |
478 | if (verify_hdr(hdr, size) < 0) | |
479 | goto unmap; | |
480 | ||
ccc4feb5 | 481 | active_nr = ntohl(hdr->hdr_entries); |
e83c5163 LT |
482 | active_alloc = alloc_nr(active_nr); |
483 | active_cache = calloc(active_alloc, sizeof(struct cache_entry *)); | |
484 | ||
485 | offset = sizeof(*hdr); | |
ccc4feb5 | 486 | for (i = 0; i < active_nr; i++) { |
e83c5163 LT |
487 | struct cache_entry *ce = map + offset; |
488 | offset = offset + ce_size(ce); | |
489 | active_cache[i] = ce; | |
490 | } | |
491 | return active_nr; | |
492 | ||
493 | unmap: | |
494 | munmap(map, size); | |
495 | errno = EINVAL; | |
496 | return error("verify header failed"); | |
497 | } | |
498 | ||
197ee8c9 LT |
499 | int write_cache(int newfd, struct cache_entry **cache, int entries) |
500 | { | |
501 | SHA_CTX c; | |
502 | struct cache_header hdr; | |
503 | int i; | |
504 | ||
ccc4feb5 LT |
505 | hdr.hdr_signature = htonl(CACHE_SIGNATURE); |
506 | hdr.hdr_version = htonl(1); | |
507 | hdr.hdr_entries = htonl(entries); | |
197ee8c9 LT |
508 | |
509 | SHA1_Init(&c); | |
510 | SHA1_Update(&c, &hdr, offsetof(struct cache_header, sha1)); | |
511 | for (i = 0; i < entries; i++) { | |
512 | struct cache_entry *ce = cache[i]; | |
513 | int size = ce_size(ce); | |
514 | SHA1_Update(&c, ce, size); | |
515 | } | |
516 | SHA1_Final(hdr.sha1, &c); | |
517 | ||
518 | if (write(newfd, &hdr, sizeof(hdr)) != sizeof(hdr)) | |
519 | return -1; | |
520 | ||
521 | for (i = 0; i < entries; i++) { | |
522 | struct cache_entry *ce = cache[i]; | |
523 | int size = ce_size(ce); | |
524 | if (write(newfd, ce, size) != size) | |
525 | return -1; | |
526 | } | |
527 | return 0; | |
528 | } |