write-tree is now willing to write empty tree
[git/dscho.git] / sha1_file.c
blobf1c1c70d784aa0587cd4c7143c3d464fd8e5ddc6
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 #ifndef O_NOATIME
13 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
14 #define O_NOATIME 01000000
15 #else
16 #define O_NOATIME 0
17 #endif
18 #endif
20 static unsigned int sha1_file_open_flag = O_NOATIME;
22 static unsigned hexval(char c)
24 if (c >= '0' && c <= '9')
25 return c - '0';
26 if (c >= 'a' && c <= 'f')
27 return c - 'a' + 10;
28 if (c >= 'A' && c <= 'F')
29 return c - 'A' + 10;
30 return ~0;
33 int get_sha1_hex(const char *hex, unsigned char *sha1)
35 int i;
36 for (i = 0; i < 20; i++) {
37 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
38 if (val & ~0xff)
39 return -1;
40 *sha1++ = val;
41 hex += 2;
43 return 0;
46 int get_sha1_file(const char *path, unsigned char *result)
48 char buffer[60];
49 int fd = open(path, O_RDONLY);
50 int len;
52 if (fd < 0)
53 return -1;
54 len = read(fd, buffer, sizeof(buffer));
55 close(fd);
56 if (len < 40)
57 return -1;
58 return get_sha1_hex(buffer, result);
61 int get_sha1(const char *str, unsigned char *sha1)
63 static char pathname[PATH_MAX];
64 static const char *prefix[] = {
65 "",
66 "refs",
67 "refs/tags",
68 "refs/heads",
69 "refs/snap",
70 NULL
72 const char *gitdir;
73 const char **p;
75 if (!get_sha1_hex(str, sha1))
76 return 0;
78 gitdir = ".git";
79 for (p = prefix; *p; p++) {
80 snprintf(pathname, sizeof(pathname), "%s/%s/%s", gitdir, *p, str);
81 if (!get_sha1_file(pathname, sha1))
82 return 0;
85 return -1;
88 char * sha1_to_hex(const unsigned char *sha1)
90 static char buffer[50];
91 static const char hex[] = "0123456789abcdef";
92 char *buf = buffer;
93 int i;
95 for (i = 0; i < 20; i++) {
96 unsigned int val = *sha1++;
97 *buf++ = hex[val >> 4];
98 *buf++ = hex[val & 0xf];
100 return buffer;
104 * NOTE! This returns a statically allocated buffer, so you have to be
105 * careful about using it. Do a "strdup()" if you need to save the
106 * filename.
108 char *sha1_file_name(const unsigned char *sha1)
110 int i;
111 static char *name, *base;
113 if (!base) {
114 char *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
115 int len = strlen(sha1_file_directory);
116 base = xmalloc(len + 60);
117 memcpy(base, sha1_file_directory, len);
118 memset(base+len, 0, 60);
119 base[len] = '/';
120 base[len+3] = '/';
121 name = base + len + 1;
123 for (i = 0; i < 20; i++) {
124 static char hex[] = "0123456789abcdef";
125 unsigned int val = sha1[i];
126 char *pos = name + i*2 + (i > 0);
127 *pos++ = hex[val >> 4];
128 *pos = hex[val & 0xf];
130 return base;
133 int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)
135 char header[100];
136 unsigned char real_sha1[20];
137 SHA_CTX c;
139 SHA1_Init(&c);
140 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
141 SHA1_Update(&c, map, size);
142 SHA1_Final(real_sha1, &c);
143 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
146 void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
148 char *filename = sha1_file_name(sha1);
149 struct stat st;
150 void *map;
151 int fd;
153 fd = open(filename, O_RDONLY | sha1_file_open_flag);
154 if (fd < 0) {
155 /* See if it works without O_NOATIME */
156 switch (sha1_file_open_flag) {
157 default:
158 fd = open(filename, O_RDONLY);
159 if (fd >= 0)
160 break;
161 /* Fallthrough */
162 case 0:
163 perror(filename);
164 return NULL;
167 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
168 sha1_file_open_flag = 0;
170 if (fstat(fd, &st) < 0) {
171 close(fd);
172 return NULL;
174 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
175 close(fd);
176 if (-1 == (int)(long)map)
177 return NULL;
178 *size = st.st_size;
179 return map;
182 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
184 int ret, bytes;
185 z_stream stream;
186 char buffer[8192];
187 char *buf;
189 /* Get the data stream */
190 memset(&stream, 0, sizeof(stream));
191 stream.next_in = map;
192 stream.avail_in = mapsize;
193 stream.next_out = buffer;
194 stream.avail_out = sizeof(buffer);
196 inflateInit(&stream);
197 ret = inflate(&stream, 0);
198 if (ret < Z_OK)
199 return NULL;
200 if (sscanf(buffer, "%10s %lu", type, size) != 2)
201 return NULL;
203 bytes = strlen(buffer) + 1;
204 buf = xmalloc(*size);
206 memcpy(buf, buffer + bytes, stream.total_out - bytes);
207 bytes = stream.total_out - bytes;
208 if (bytes < *size && ret == Z_OK) {
209 stream.next_out = buf + bytes;
210 stream.avail_out = *size - bytes;
211 while (inflate(&stream, Z_FINISH) == Z_OK)
212 /* nothing */;
214 inflateEnd(&stream);
215 return buf;
218 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
220 unsigned long mapsize;
221 void *map, *buf;
223 map = map_sha1_file(sha1, &mapsize);
224 if (map) {
225 buf = unpack_sha1_file(map, mapsize, type, size);
226 munmap(map, mapsize);
227 return buf;
229 return NULL;
232 void *read_object_with_reference(const unsigned char *sha1,
233 const unsigned char *required_type,
234 unsigned long *size,
235 unsigned char *actual_sha1_return)
237 char type[20];
238 void *buffer;
239 unsigned long isize;
240 unsigned char actual_sha1[20];
242 memcpy(actual_sha1, sha1, 20);
243 while (1) {
244 int ref_length = -1;
245 const char *ref_type = NULL;
247 buffer = read_sha1_file(actual_sha1, type, &isize);
248 if (!buffer)
249 return NULL;
250 if (!strcmp(type, required_type)) {
251 *size = isize;
252 if (actual_sha1_return)
253 memcpy(actual_sha1_return, actual_sha1, 20);
254 return buffer;
256 /* Handle references */
257 else if (!strcmp(type, "commit"))
258 ref_type = "tree ";
259 else if (!strcmp(type, "tag"))
260 ref_type = "object ";
261 else {
262 free(buffer);
263 return NULL;
265 ref_length = strlen(ref_type);
267 if (memcmp(buffer, ref_type, ref_length) ||
268 get_sha1_hex(buffer + ref_length, actual_sha1)) {
269 free(buffer);
270 return NULL;
272 /* Now we have the ID of the referred-to object in
273 * actual_sha1. Check again. */
277 int write_sha1_file(char *buf, unsigned long len, const char *type, unsigned char *returnsha1)
279 int size;
280 char *compressed;
281 z_stream stream;
282 unsigned char sha1[20];
283 SHA_CTX c;
284 char *filename;
285 static char tmpfile[PATH_MAX];
286 char hdr[50];
287 int fd, hdrlen, ret;
289 /* Generate the header */
290 hdrlen = sprintf(hdr, "%s %lu", type, len)+1;
292 /* Sha1.. */
293 SHA1_Init(&c);
294 SHA1_Update(&c, hdr, hdrlen);
295 SHA1_Update(&c, buf, len);
296 SHA1_Final(sha1, &c);
298 if (returnsha1)
299 memcpy(returnsha1, sha1, 20);
301 filename = sha1_file_name(sha1);
302 fd = open(filename, O_RDONLY);
303 if (fd >= 0) {
305 * FIXME!!! We might do collision checking here, but we'd
306 * need to uncompress the old file and check it. Later.
308 close(fd);
309 return 0;
312 if (errno != ENOENT) {
313 fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno));
314 return -1;
317 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
318 fd = mkstemp(tmpfile);
319 if (fd < 0) {
320 fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno));
321 return -1;
324 /* Set it up */
325 memset(&stream, 0, sizeof(stream));
326 deflateInit(&stream, Z_BEST_COMPRESSION);
327 size = deflateBound(&stream, len+hdrlen);
328 compressed = xmalloc(size);
330 /* Compress it */
331 stream.next_out = compressed;
332 stream.avail_out = size;
334 /* First header.. */
335 stream.next_in = hdr;
336 stream.avail_in = hdrlen;
337 while (deflate(&stream, 0) == Z_OK)
338 /* nothing */
340 /* Then the data itself.. */
341 stream.next_in = buf;
342 stream.avail_in = len;
343 while (deflate(&stream, Z_FINISH) == Z_OK)
344 /* nothing */;
345 deflateEnd(&stream);
346 size = stream.total_out;
348 if (write(fd, compressed, size) != size)
349 die("unable to write file");
350 fchmod(fd, 0444);
351 close(fd);
353 ret = link(tmpfile, filename);
354 if (ret < 0) {
355 ret = errno;
358 * Coda hack - coda doesn't like cross-directory links,
359 * so we fall back to a rename, which will mean that it
360 * won't be able to check collisions, but that's not a
361 * big deal.
363 * When this succeeds, we just return 0. We have nothing
364 * left to unlink.
366 if (ret == EXDEV && !rename(tmpfile, filename))
367 return 0;
369 unlink(tmpfile);
370 if (ret) {
371 if (ret != EEXIST) {
372 fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
373 return -1;
375 /* FIXME!!! Collision check here ? */
378 return 0;
381 int write_sha1_from_fd(const unsigned char *sha1, int fd)
383 char *filename = sha1_file_name(sha1);
385 int local;
386 z_stream stream;
387 unsigned char real_sha1[20];
388 char buf[4096];
389 char discard[4096];
390 int ret;
391 SHA_CTX c;
393 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
395 if (local < 0)
396 return error("Couldn't open %s\n", filename);
398 memset(&stream, 0, sizeof(stream));
400 inflateInit(&stream);
402 SHA1_Init(&c);
404 do {
405 ssize_t size;
406 size = read(fd, buf, 4096);
407 if (size <= 0) {
408 close(local);
409 unlink(filename);
410 if (!size)
411 return error("Connection closed?");
412 perror("Reading from connection");
413 return -1;
415 write(local, buf, size);
416 stream.avail_in = size;
417 stream.next_in = buf;
418 do {
419 stream.next_out = discard;
420 stream.avail_out = sizeof(discard);
421 ret = inflate(&stream, Z_SYNC_FLUSH);
422 SHA1_Update(&c, discard, sizeof(discard) -
423 stream.avail_out);
424 } while (stream.avail_in && ret == Z_OK);
426 } while (ret == Z_OK);
427 inflateEnd(&stream);
429 close(local);
430 SHA1_Final(real_sha1, &c);
431 if (ret != Z_STREAM_END) {
432 unlink(filename);
433 return error("File %s corrupted", sha1_to_hex(sha1));
435 if (memcmp(sha1, real_sha1, 20)) {
436 unlink(filename);
437 return error("File %s has bad hash\n", sha1_to_hex(sha1));
440 return 0;
443 int has_sha1_file(const unsigned char *sha1)
445 char *filename = sha1_file_name(sha1);
446 struct stat st;
448 if (!stat(filename, &st))
449 return 1;
450 return 0;
453 int index_fd(unsigned char *sha1, int fd, struct stat *st)
455 unsigned long size = st->st_size;
456 void *buf;
457 int ret;
459 buf = "";
460 if (size)
461 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
462 close(fd);
463 if ((int)(long)buf == -1)
464 return -1;
466 ret = write_sha1_file(buf, size, "blob", sha1);
467 if (size)
468 munmap(buf, size);
469 return ret;