2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
12 const char *sha1_file_directory = NULL;
15 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
16 #define O_NOATIME 01000000
22 static unsigned int sha1_file_open_flag = O_NOATIME;
24 static unsigned hexval(char c)
26 if (c >= '0' && c <= '9')
28 if (c >= 'a' && c <= 'f')
30 if (c >= 'A' && c <= 'F')
35 int get_sha1_hex(const char *hex, unsigned char *sha1)
38 for (i = 0; i < 20; i++) {
39 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
48 int get_sha1_file(const char *path, unsigned char *result)
51 int fd = open(path, O_RDONLY);
56 len = read(fd, buffer, sizeof(buffer));
60 return get_sha1_hex(buffer, result);
63 int get_sha1(const char *str, unsigned char *sha1)
65 static char pathname[PATH_MAX];
67 if (!get_sha1_hex(str, sha1))
69 if (!get_sha1_file(str, sha1))
71 snprintf(pathname, sizeof(pathname), ".git/%s", str);
72 if (!get_sha1_file(pathname, sha1))
77 char * sha1_to_hex(const unsigned char *sha1)
79 static char buffer[50];
80 static const char hex[] = "0123456789abcdef";
84 for (i = 0; i < 20; i++) {
85 unsigned int val = *sha1++;
86 *buf++ = hex[val >> 4];
87 *buf++ = hex[val & 0xf];
93 * NOTE! This returns a statically allocated buffer, so you have to be
94 * careful about using it. Do a "strdup()" if you need to save the
97 char *sha1_file_name(const unsigned char *sha1)
100 static char *name, *base;
103 char *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
104 int len = strlen(sha1_file_directory);
105 base = xmalloc(len + 60);
106 memcpy(base, sha1_file_directory, len);
107 memset(base+len, 0, 60);
110 name = base + len + 1;
112 for (i = 0; i < 20; i++) {
113 static char hex[] = "0123456789abcdef";
114 unsigned int val = sha1[i];
115 char *pos = name + i*2 + (i > 0);
116 *pos++ = hex[val >> 4];
117 *pos = hex[val & 0xf];
122 int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)
125 unsigned char real_sha1[20];
129 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
130 SHA1_Update(&c, map, size);
131 SHA1_Final(real_sha1, &c);
132 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
135 void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
137 char *filename = sha1_file_name(sha1);
142 fd = open(filename, O_RDONLY | sha1_file_open_flag);
144 /* See if it works without O_NOATIME */
145 switch (sha1_file_open_flag) {
147 fd = open(filename, O_RDONLY);
156 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
157 sha1_file_open_flag = 0;
159 if (fstat(fd, &st) < 0) {
163 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
165 if (-1 == (int)(long)map)
171 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
178 /* Get the data stream */
179 memset(&stream, 0, sizeof(stream));
180 stream.next_in = map;
181 stream.avail_in = mapsize;
182 stream.next_out = buffer;
183 stream.avail_out = sizeof(buffer);
185 inflateInit(&stream);
186 ret = inflate(&stream, 0);
189 if (sscanf(buffer, "%10s %lu", type, size) != 2)
192 bytes = strlen(buffer) + 1;
193 buf = xmalloc(*size);
195 memcpy(buf, buffer + bytes, stream.total_out - bytes);
196 bytes = stream.total_out - bytes;
197 if (bytes < *size && ret == Z_OK) {
198 stream.next_out = buf + bytes;
199 stream.avail_out = *size - bytes;
200 while (inflate(&stream, Z_FINISH) == Z_OK)
207 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
209 unsigned long mapsize;
212 map = map_sha1_file(sha1, &mapsize);
214 buf = unpack_sha1_file(map, mapsize, type, size);
215 munmap(map, mapsize);
221 void *read_object_with_reference(const unsigned char *sha1,
222 const unsigned char *required_type,
224 unsigned char *actual_sha1_return)
229 unsigned char actual_sha1[20];
231 memcpy(actual_sha1, sha1, 20);
234 const char *ref_type = NULL;
236 buffer = read_sha1_file(actual_sha1, type, &isize);
239 if (!strcmp(type, required_type)) {
241 if (actual_sha1_return)
242 memcpy(actual_sha1_return, actual_sha1, 20);
245 /* Handle references */
246 else if (!strcmp(type, "commit"))
248 else if (!strcmp(type, "tag"))
249 ref_type = "object ";
254 ref_length = strlen(ref_type);
256 if (memcmp(buffer, ref_type, ref_length) ||
257 get_sha1_hex(buffer + ref_length, actual_sha1)) {
261 /* Now we have the ID of the referred-to object in
262 * actual_sha1. Check again. */
266 int write_sha1_file(char *buf, unsigned long len, const char *type, unsigned char *returnsha1)
271 unsigned char sha1[20];
277 /* Generate the header */
278 hdrlen = sprintf(hdr, "%s %lu", type, len)+1;
282 SHA1_Update(&c, hdr, hdrlen);
283 SHA1_Update(&c, buf, len);
284 SHA1_Final(sha1, &c);
287 memcpy(returnsha1, sha1, 20);
289 filename = sha1_file_name(sha1);
290 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
296 * We might do collision checking here, but we'd need to
297 * uncompress the old file and check it. Later.
303 memset(&stream, 0, sizeof(stream));
304 deflateInit(&stream, Z_BEST_COMPRESSION);
305 size = deflateBound(&stream, len+hdrlen);
306 compressed = xmalloc(size);
309 stream.next_out = compressed;
310 stream.avail_out = size;
313 stream.next_in = hdr;
314 stream.avail_in = hdrlen;
315 while (deflate(&stream, 0) == Z_OK)
318 /* Then the data itself.. */
319 stream.next_in = buf;
320 stream.avail_in = len;
321 while (deflate(&stream, Z_FINISH) == Z_OK)
324 size = stream.total_out;
326 if (write(fd, compressed, size) != size)
327 die("unable to write file");
333 static inline int collision_check(char *filename, void *buf, unsigned int size)
335 #ifdef COLLISION_CHECK
337 int fd = open(filename, O_RDONLY);
341 /* Unreadable object, or object went away? Strange. */
345 if (fstat(fd, &st) < 0 || size != st.st_size)
348 map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
350 if (map == MAP_FAILED)
352 cmp = memcmp(buf, map, size);
360 int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
362 char *filename = sha1_file_name(sha1);
365 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
369 if (collision_check(filename, buf, size))
370 return error("SHA1 collision detected!"
371 " This is bad, bad, BAD!\a\n");
374 write(fd, buf, size);
379 int write_sha1_from_fd(const unsigned char *sha1, int fd)
381 char *filename = sha1_file_name(sha1);
385 unsigned char real_sha1[20];
391 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
394 return error("Couldn't open %s\n", filename);
396 memset(&stream, 0, sizeof(stream));
398 inflateInit(&stream);
404 size = read(fd, buf, 4096);
409 return error("Connection closed?");
410 perror("Reading from connection");
413 write(local, buf, size);
414 stream.avail_in = size;
415 stream.next_in = buf;
417 stream.next_out = discard;
418 stream.avail_out = sizeof(discard);
419 ret = inflate(&stream, Z_SYNC_FLUSH);
420 SHA1_Update(&c, discard, sizeof(discard) -
422 } while (stream.avail_in && ret == Z_OK);
424 } while (ret == Z_OK);
428 SHA1_Final(real_sha1, &c);
429 if (ret != Z_STREAM_END) {
431 return error("File %s corrupted", sha1_to_hex(sha1));
433 if (memcmp(sha1, real_sha1, 20)) {
435 return error("File %s has bad hash\n", sha1_to_hex(sha1));
441 int has_sha1_file(const unsigned char *sha1)
443 char *filename = sha1_file_name(sha1);
446 if (!stat(filename, &st))