Add "look up parent" logic for the simple names.
[git/gitweb.git] / sha1_file.c
blobe1ee0ad349497d6470bc00aa7f1085d9d1b83b43
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
7 * creation etc.
8 */
9 #include <stdarg.h>
10 #include "cache.h"
12 const char *sha1_file_directory = NULL;
14 #ifndef O_NOATIME
15 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
16 #define O_NOATIME 01000000
17 #else
18 #define O_NOATIME 0
19 #endif
20 #endif
22 static unsigned int sha1_file_open_flag = O_NOATIME;
24 static unsigned hexval(char c)
26 if (c >= '0' && c <= '9')
27 return c - '0';
28 if (c >= 'a' && c <= 'f')
29 return c - 'a' + 10;
30 if (c >= 'A' && c <= 'F')
31 return c - 'A' + 10;
32 return ~0;
35 int get_sha1_hex(const char *hex, unsigned char *sha1)
37 int i;
38 for (i = 0; i < 20; i++) {
39 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
40 if (val & ~0xff)
41 return -1;
42 *sha1++ = val;
43 hex += 2;
45 return 0;
48 int get_sha1_file(const char *path, unsigned char *result)
50 char buffer[60];
51 int fd = open(path, O_RDONLY);
52 int len;
54 if (fd < 0)
55 return -1;
56 len = read(fd, buffer, sizeof(buffer));
57 close(fd);
58 if (len < 40)
59 return -1;
60 return get_sha1_hex(buffer, result);
63 static int get_parent(int index, const char *str, unsigned char *result)
65 unsigned char sha1[20];
66 char *buffer;
67 unsigned long size, offset;
68 int ret;
70 if (get_sha1(str, sha1) < 0)
71 return -1;
72 buffer = read_object_with_reference(sha1, "commit", &size, NULL);
73 if (!buffer)
74 return -1;
75 ret = -1;
76 offset = 46;
77 for (;;) {
78 if (offset + 48 > size)
79 break;
80 if (memcmp(buffer + offset, "parent ", 7))
81 break;
82 if (index > 0) {
83 offset += 48;
84 index--;
85 continue;
87 ret = get_sha1_hex(buffer + offset + 7, result);
88 break;
90 free(buffer);
91 return ret;
94 int get_sha1(const char *str, unsigned char *sha1)
96 static char pathname[PATH_MAX];
97 static const char *prefix[] = {
98 "",
99 "refs",
100 "refs/tags",
101 "refs/heads",
102 "refs/snap",
103 NULL
105 const char *gitdir;
106 const char **p;
108 if (!get_sha1_hex(str, sha1))
109 return 0;
111 switch (*str) {
112 case '/':
113 if (!get_sha1_file(str, sha1))
114 return 0;
115 break;
116 case '-':
117 return get_parent(0, str+1, sha1);
118 case '0' ... '9':
119 if (str[1] == '-')
120 return get_parent(*str - '0', str+2, sha1);
121 break;
124 gitdir = ".git";
125 for (p = prefix; *p; p++) {
126 snprintf(pathname, sizeof(pathname), "%s/%s/%s", gitdir, *p, str);
127 if (!get_sha1_file(pathname, sha1))
128 return 0;
131 return -1;
134 char * sha1_to_hex(const unsigned char *sha1)
136 static char buffer[50];
137 static const char hex[] = "0123456789abcdef";
138 char *buf = buffer;
139 int i;
141 for (i = 0; i < 20; i++) {
142 unsigned int val = *sha1++;
143 *buf++ = hex[val >> 4];
144 *buf++ = hex[val & 0xf];
146 return buffer;
150 * NOTE! This returns a statically allocated buffer, so you have to be
151 * careful about using it. Do a "strdup()" if you need to save the
152 * filename.
154 char *sha1_file_name(const unsigned char *sha1)
156 int i;
157 static char *name, *base;
159 if (!base) {
160 char *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
161 int len = strlen(sha1_file_directory);
162 base = xmalloc(len + 60);
163 memcpy(base, sha1_file_directory, len);
164 memset(base+len, 0, 60);
165 base[len] = '/';
166 base[len+3] = '/';
167 name = base + len + 1;
169 for (i = 0; i < 20; i++) {
170 static char hex[] = "0123456789abcdef";
171 unsigned int val = sha1[i];
172 char *pos = name + i*2 + (i > 0);
173 *pos++ = hex[val >> 4];
174 *pos = hex[val & 0xf];
176 return base;
179 int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)
181 char header[100];
182 unsigned char real_sha1[20];
183 SHA_CTX c;
185 SHA1_Init(&c);
186 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
187 SHA1_Update(&c, map, size);
188 SHA1_Final(real_sha1, &c);
189 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
192 void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
194 char *filename = sha1_file_name(sha1);
195 struct stat st;
196 void *map;
197 int fd;
199 fd = open(filename, O_RDONLY | sha1_file_open_flag);
200 if (fd < 0) {
201 /* See if it works without O_NOATIME */
202 switch (sha1_file_open_flag) {
203 default:
204 fd = open(filename, O_RDONLY);
205 if (fd >= 0)
206 break;
207 /* Fallthrough */
208 case 0:
209 perror(filename);
210 return NULL;
213 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
214 sha1_file_open_flag = 0;
216 if (fstat(fd, &st) < 0) {
217 close(fd);
218 return NULL;
220 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
221 close(fd);
222 if (-1 == (int)(long)map)
223 return NULL;
224 *size = st.st_size;
225 return map;
228 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
230 int ret, bytes;
231 z_stream stream;
232 char buffer[8192];
233 char *buf;
235 /* Get the data stream */
236 memset(&stream, 0, sizeof(stream));
237 stream.next_in = map;
238 stream.avail_in = mapsize;
239 stream.next_out = buffer;
240 stream.avail_out = sizeof(buffer);
242 inflateInit(&stream);
243 ret = inflate(&stream, 0);
244 if (ret < Z_OK)
245 return NULL;
246 if (sscanf(buffer, "%10s %lu", type, size) != 2)
247 return NULL;
249 bytes = strlen(buffer) + 1;
250 buf = xmalloc(*size);
252 memcpy(buf, buffer + bytes, stream.total_out - bytes);
253 bytes = stream.total_out - bytes;
254 if (bytes < *size && ret == Z_OK) {
255 stream.next_out = buf + bytes;
256 stream.avail_out = *size - bytes;
257 while (inflate(&stream, Z_FINISH) == Z_OK)
258 /* nothing */;
260 inflateEnd(&stream);
261 return buf;
264 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
266 unsigned long mapsize;
267 void *map, *buf;
269 map = map_sha1_file(sha1, &mapsize);
270 if (map) {
271 buf = unpack_sha1_file(map, mapsize, type, size);
272 munmap(map, mapsize);
273 return buf;
275 return NULL;
278 void *read_object_with_reference(const unsigned char *sha1,
279 const unsigned char *required_type,
280 unsigned long *size,
281 unsigned char *actual_sha1_return)
283 char type[20];
284 void *buffer;
285 unsigned long isize;
286 unsigned char actual_sha1[20];
288 memcpy(actual_sha1, sha1, 20);
289 while (1) {
290 int ref_length = -1;
291 const char *ref_type = NULL;
293 buffer = read_sha1_file(actual_sha1, type, &isize);
294 if (!buffer)
295 return NULL;
296 if (!strcmp(type, required_type)) {
297 *size = isize;
298 if (actual_sha1_return)
299 memcpy(actual_sha1_return, actual_sha1, 20);
300 return buffer;
302 /* Handle references */
303 else if (!strcmp(type, "commit"))
304 ref_type = "tree ";
305 else if (!strcmp(type, "tag"))
306 ref_type = "object ";
307 else {
308 free(buffer);
309 return NULL;
311 ref_length = strlen(ref_type);
313 if (memcmp(buffer, ref_type, ref_length) ||
314 get_sha1_hex(buffer + ref_length, actual_sha1)) {
315 free(buffer);
316 return NULL;
318 /* Now we have the ID of the referred-to object in
319 * actual_sha1. Check again. */
323 int write_sha1_file(char *buf, unsigned long len, const char *type, unsigned char *returnsha1)
325 int size;
326 char *compressed;
327 z_stream stream;
328 unsigned char sha1[20];
329 SHA_CTX c;
330 char *filename;
331 char hdr[50];
332 int fd, hdrlen;
334 /* Generate the header */
335 hdrlen = sprintf(hdr, "%s %lu", type, len)+1;
337 /* Sha1.. */
338 SHA1_Init(&c);
339 SHA1_Update(&c, hdr, hdrlen);
340 SHA1_Update(&c, buf, len);
341 SHA1_Final(sha1, &c);
343 if (returnsha1)
344 memcpy(returnsha1, sha1, 20);
346 filename = sha1_file_name(sha1);
347 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
348 if (fd < 0) {
349 if (errno != EEXIST)
350 return -1;
353 * We might do collision checking here, but we'd need to
354 * uncompress the old file and check it. Later.
356 return 0;
359 /* Set it up */
360 memset(&stream, 0, sizeof(stream));
361 deflateInit(&stream, Z_BEST_COMPRESSION);
362 size = deflateBound(&stream, len+hdrlen);
363 compressed = xmalloc(size);
365 /* Compress it */
366 stream.next_out = compressed;
367 stream.avail_out = size;
369 /* First header.. */
370 stream.next_in = hdr;
371 stream.avail_in = hdrlen;
372 while (deflate(&stream, 0) == Z_OK)
373 /* nothing */
375 /* Then the data itself.. */
376 stream.next_in = buf;
377 stream.avail_in = len;
378 while (deflate(&stream, Z_FINISH) == Z_OK)
379 /* nothing */;
380 deflateEnd(&stream);
381 size = stream.total_out;
383 if (write(fd, compressed, size) != size)
384 die("unable to write file");
385 close(fd);
387 return 0;
390 static inline int collision_check(char *filename, void *buf, unsigned int size)
392 #ifdef COLLISION_CHECK
393 void *map;
394 int fd = open(filename, O_RDONLY);
395 struct stat st;
396 int cmp;
398 /* Unreadable object, or object went away? Strange. */
399 if (fd < 0)
400 return -1;
402 if (fstat(fd, &st) < 0 || size != st.st_size)
403 return -1;
405 map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
406 close(fd);
407 if (map == MAP_FAILED)
408 return -1;
409 cmp = memcmp(buf, map, size);
410 munmap(map, size);
411 if (cmp)
412 return -1;
413 #endif
414 return 0;
417 int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
419 char *filename = sha1_file_name(sha1);
420 int fd;
422 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
423 if (fd < 0) {
424 if (errno != EEXIST)
425 return -1;
426 if (collision_check(filename, buf, size))
427 return error("SHA1 collision detected!"
428 " This is bad, bad, BAD!\a\n");
429 return 0;
431 write(fd, buf, size);
432 close(fd);
433 return 0;
436 int write_sha1_from_fd(const unsigned char *sha1, int fd)
438 char *filename = sha1_file_name(sha1);
440 int local;
441 z_stream stream;
442 unsigned char real_sha1[20];
443 char buf[4096];
444 char discard[4096];
445 int ret;
446 SHA_CTX c;
448 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
450 if (local < 0)
451 return error("Couldn't open %s\n", filename);
453 memset(&stream, 0, sizeof(stream));
455 inflateInit(&stream);
457 SHA1_Init(&c);
459 do {
460 ssize_t size;
461 size = read(fd, buf, 4096);
462 if (size <= 0) {
463 close(local);
464 unlink(filename);
465 if (!size)
466 return error("Connection closed?");
467 perror("Reading from connection");
468 return -1;
470 write(local, buf, size);
471 stream.avail_in = size;
472 stream.next_in = buf;
473 do {
474 stream.next_out = discard;
475 stream.avail_out = sizeof(discard);
476 ret = inflate(&stream, Z_SYNC_FLUSH);
477 SHA1_Update(&c, discard, sizeof(discard) -
478 stream.avail_out);
479 } while (stream.avail_in && ret == Z_OK);
481 } while (ret == Z_OK);
482 inflateEnd(&stream);
484 close(local);
485 SHA1_Final(real_sha1, &c);
486 if (ret != Z_STREAM_END) {
487 unlink(filename);
488 return error("File %s corrupted", sha1_to_hex(sha1));
490 if (memcmp(sha1, real_sha1, 20)) {
491 unlink(filename);
492 return error("File %s has bad hash\n", sha1_to_hex(sha1));
495 return 0;
498 int has_sha1_file(const unsigned char *sha1)
500 char *filename = sha1_file_name(sha1);
501 struct stat st;
503 if (!stat(filename, &st))
504 return 1;
505 return 0;