git-update-cache refuses to add a file where a directory is registed.
[git/dscho.git] / sha1_file.c
blobbf8fd6aa5fd76c119dd1a84cc540a5219e1987e4
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 <limits.h>
11 #include "cache.h"
13 #ifndef O_NOATIME
14 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
15 #define O_NOATIME 01000000
16 #else
17 #define O_NOATIME 0
18 #endif
19 #endif
21 static unsigned int sha1_file_open_flag = O_NOATIME;
23 static unsigned hexval(char c)
25 if (c >= '0' && c <= '9')
26 return c - '0';
27 if (c >= 'a' && c <= 'f')
28 return c - 'a' + 10;
29 if (c >= 'A' && c <= 'F')
30 return c - 'A' + 10;
31 return ~0;
34 int get_sha1_hex(const char *hex, unsigned char *sha1)
36 int i;
37 for (i = 0; i < 20; i++) {
38 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
39 if (val & ~0xff)
40 return -1;
41 *sha1++ = val;
42 hex += 2;
44 return 0;
47 int get_sha1_file(const char *path, unsigned char *result)
49 char buffer[60];
50 int fd = open(path, O_RDONLY);
51 int len;
53 if (fd < 0)
54 return -1;
55 len = read(fd, buffer, sizeof(buffer));
56 close(fd);
57 if (len < 40)
58 return -1;
59 return get_sha1_hex(buffer, result);
62 int get_sha1(const char *str, unsigned char *sha1)
64 static char pathname[PATH_MAX];
65 static const char *prefix[] = {
66 "",
67 "refs",
68 "refs/tags",
69 "refs/heads",
70 "refs/snap",
71 NULL
73 const char *gitdir;
74 const char **p;
76 if (!get_sha1_hex(str, sha1))
77 return 0;
79 gitdir = ".git";
80 for (p = prefix; *p; p++) {
81 snprintf(pathname, sizeof(pathname), "%s/%s/%s", gitdir, *p, str);
82 if (!get_sha1_file(pathname, sha1))
83 return 0;
86 return -1;
89 char * sha1_to_hex(const unsigned char *sha1)
91 static char buffer[50];
92 static const char hex[] = "0123456789abcdef";
93 char *buf = buffer;
94 int i;
96 for (i = 0; i < 20; i++) {
97 unsigned int val = *sha1++;
98 *buf++ = hex[val >> 4];
99 *buf++ = hex[val & 0xf];
101 return buffer;
104 static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
106 int i;
107 for (i = 0; i < 20; i++) {
108 static char hex[] = "0123456789abcdef";
109 unsigned int val = sha1[i];
110 char *pos = pathbuf + i*2 + (i > 0);
111 *pos++ = hex[val >> 4];
112 *pos = hex[val & 0xf];
117 * NOTE! This returns a statically allocated buffer, so you have to be
118 * careful about using it. Do a "strdup()" if you need to save the
119 * filename.
121 * Also note that this returns the location for creating. Reading
122 * SHA1 file can happen from any alternate directory listed in the
123 * SHA1_FILE_DIRECTORIES environment variable if it is not found in
124 * the primary object database.
126 char *sha1_file_name(const unsigned char *sha1)
128 static char *name, *base;
130 if (!base) {
131 char *sha1_file_directory = get_object_directory();
132 int len = strlen(sha1_file_directory);
133 base = xmalloc(len + 60);
134 memcpy(base, sha1_file_directory, len);
135 memset(base+len, 0, 60);
136 base[len] = '/';
137 base[len+3] = '/';
138 name = base + len + 1;
140 fill_sha1_path(name, sha1);
141 return base;
144 static struct alternate_object_database
146 char *base;
147 char *name;
148 } *alt_odb;
150 static void prepare_alt_odb(void)
152 int pass, totlen, i;
153 void *buf;
154 const char *cp, *last;
155 char *op = 0;
156 const char *alt = getenv(ALTERNATE_DB_ENVIRONMENT) ? : "";
158 for (totlen = pass = 0; pass < 2; pass++) {
159 last = alt;
160 i = 0;
161 do {
162 cp = strchr(last, ':') ? : last + strlen(last);
163 if (last != cp) {
164 /* 43 = 40-byte + 2 '/' + terminating NUL */
165 int pfxlen = cp - last;
166 int entlen = pfxlen + 43;
167 if (pass == 0)
168 totlen += entlen;
169 else {
170 alt_odb[i].base = op;
171 alt_odb[i].name = op + pfxlen + 1;
172 memcpy(op, last, pfxlen);
173 op[pfxlen] = op[pfxlen + 3] = '/';
174 op[entlen-1] = 0;
175 op += entlen;
177 i++;
179 while (*cp && *cp == ':')
180 cp++;
181 last = cp;
182 } while (*cp);
183 if (pass)
184 break;
185 alt_odb = buf = xmalloc(sizeof(*alt_odb) * (i + 1) + totlen);
186 alt_odb[i].base = alt_odb[i].name = 0;
187 op = (char*)(&alt_odb[i+1]);
191 static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
193 int i;
194 char *name = sha1_file_name(sha1);
196 if (!stat(name, st))
197 return name;
198 if (!alt_odb)
199 prepare_alt_odb();
200 for (i = 0; (name = alt_odb[i].name) != NULL; i++) {
201 fill_sha1_path(name, sha1);
202 if (!stat(alt_odb[i].base, st))
203 return alt_odb[i].base;
205 return NULL;
208 int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)
210 char header[100];
211 unsigned char real_sha1[20];
212 SHA_CTX c;
214 SHA1_Init(&c);
215 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
216 SHA1_Update(&c, map, size);
217 SHA1_Final(real_sha1, &c);
218 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
221 void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
223 struct stat st;
224 void *map;
225 int fd;
226 char *filename = find_sha1_file(sha1, &st);
228 if (!filename) {
229 error("cannot map sha1 file %s", sha1_to_hex(sha1));
230 return NULL;
233 fd = open(filename, O_RDONLY | sha1_file_open_flag);
234 if (fd < 0) {
235 /* See if it works without O_NOATIME */
236 switch (sha1_file_open_flag) {
237 default:
238 fd = open(filename, O_RDONLY);
239 if (fd >= 0)
240 break;
241 /* Fallthrough */
242 case 0:
243 perror(filename);
244 return NULL;
247 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
248 sha1_file_open_flag = 0;
250 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
251 close(fd);
252 if (-1 == (int)(long)map)
253 return NULL;
254 *size = st.st_size;
255 return map;
258 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
260 int ret, bytes;
261 z_stream stream;
262 char buffer[8192];
263 char *buf;
265 /* Get the data stream */
266 memset(&stream, 0, sizeof(stream));
267 stream.next_in = map;
268 stream.avail_in = mapsize;
269 stream.next_out = buffer;
270 stream.avail_out = sizeof(buffer);
272 inflateInit(&stream);
273 ret = inflate(&stream, 0);
274 if (ret < Z_OK)
275 return NULL;
276 if (sscanf(buffer, "%10s %lu", type, size) != 2)
277 return NULL;
279 bytes = strlen(buffer) + 1;
280 buf = xmalloc(*size);
282 memcpy(buf, buffer + bytes, stream.total_out - bytes);
283 bytes = stream.total_out - bytes;
284 if (bytes < *size && ret == Z_OK) {
285 stream.next_out = buf + bytes;
286 stream.avail_out = *size - bytes;
287 while (inflate(&stream, Z_FINISH) == Z_OK)
288 /* nothing */;
290 inflateEnd(&stream);
291 return buf;
294 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
296 unsigned long mapsize;
297 void *map, *buf;
299 map = map_sha1_file(sha1, &mapsize);
300 if (map) {
301 buf = unpack_sha1_file(map, mapsize, type, size);
302 munmap(map, mapsize);
303 return buf;
305 return NULL;
308 void *read_object_with_reference(const unsigned char *sha1,
309 const unsigned char *required_type,
310 unsigned long *size,
311 unsigned char *actual_sha1_return)
313 char type[20];
314 void *buffer;
315 unsigned long isize;
316 unsigned char actual_sha1[20];
318 memcpy(actual_sha1, sha1, 20);
319 while (1) {
320 int ref_length = -1;
321 const char *ref_type = NULL;
323 buffer = read_sha1_file(actual_sha1, type, &isize);
324 if (!buffer)
325 return NULL;
326 if (!strcmp(type, required_type)) {
327 *size = isize;
328 if (actual_sha1_return)
329 memcpy(actual_sha1_return, actual_sha1, 20);
330 return buffer;
332 /* Handle references */
333 else if (!strcmp(type, "commit"))
334 ref_type = "tree ";
335 else if (!strcmp(type, "tag"))
336 ref_type = "object ";
337 else {
338 free(buffer);
339 return NULL;
341 ref_length = strlen(ref_type);
343 if (memcmp(buffer, ref_type, ref_length) ||
344 get_sha1_hex(buffer + ref_length, actual_sha1)) {
345 free(buffer);
346 return NULL;
348 /* Now we have the ID of the referred-to object in
349 * actual_sha1. Check again. */
353 int write_sha1_file(char *buf, unsigned long len, const char *type, unsigned char *returnsha1)
355 int size;
356 char *compressed;
357 z_stream stream;
358 unsigned char sha1[20];
359 SHA_CTX c;
360 char *filename;
361 static char tmpfile[PATH_MAX];
362 char hdr[50];
363 int fd, hdrlen, ret;
365 /* Generate the header */
366 hdrlen = sprintf(hdr, "%s %lu", type, len)+1;
368 /* Sha1.. */
369 SHA1_Init(&c);
370 SHA1_Update(&c, hdr, hdrlen);
371 SHA1_Update(&c, buf, len);
372 SHA1_Final(sha1, &c);
374 if (returnsha1)
375 memcpy(returnsha1, sha1, 20);
377 filename = sha1_file_name(sha1);
378 fd = open(filename, O_RDONLY);
379 if (fd >= 0) {
381 * FIXME!!! We might do collision checking here, but we'd
382 * need to uncompress the old file and check it. Later.
384 close(fd);
385 return 0;
388 if (errno != ENOENT) {
389 fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno));
390 return -1;
393 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
395 fd = mkstemp(tmpfile);
396 if (fd < 0) {
397 fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno));
398 return -1;
401 /* Set it up */
402 memset(&stream, 0, sizeof(stream));
403 deflateInit(&stream, Z_BEST_COMPRESSION);
404 size = deflateBound(&stream, len+hdrlen);
405 compressed = xmalloc(size);
407 /* Compress it */
408 stream.next_out = compressed;
409 stream.avail_out = size;
411 /* First header.. */
412 stream.next_in = hdr;
413 stream.avail_in = hdrlen;
414 while (deflate(&stream, 0) == Z_OK)
415 /* nothing */
417 /* Then the data itself.. */
418 stream.next_in = buf;
419 stream.avail_in = len;
420 while (deflate(&stream, Z_FINISH) == Z_OK)
421 /* nothing */;
422 deflateEnd(&stream);
423 size = stream.total_out;
425 if (write(fd, compressed, size) != size)
426 die("unable to write file");
427 fchmod(fd, 0444);
428 close(fd);
429 free(compressed);
431 ret = link(tmpfile, filename);
432 if (ret < 0) {
433 ret = errno;
436 * Coda hack - coda doesn't like cross-directory links,
437 * so we fall back to a rename, which will mean that it
438 * won't be able to check collisions, but that's not a
439 * big deal.
441 * When this succeeds, we just return 0. We have nothing
442 * left to unlink.
444 if (ret == EXDEV && !rename(tmpfile, filename))
445 return 0;
447 unlink(tmpfile);
448 if (ret) {
449 if (ret != EEXIST) {
450 fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
451 return -1;
453 /* FIXME!!! Collision check here ? */
456 return 0;
459 int write_sha1_from_fd(const unsigned char *sha1, int fd)
461 char *filename = sha1_file_name(sha1);
463 int local;
464 z_stream stream;
465 unsigned char real_sha1[20];
466 char buf[4096];
467 char discard[4096];
468 int ret;
469 SHA_CTX c;
471 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
473 if (local < 0)
474 return error("Couldn't open %s\n", filename);
476 memset(&stream, 0, sizeof(stream));
478 inflateInit(&stream);
480 SHA1_Init(&c);
482 do {
483 ssize_t size;
484 size = read(fd, buf, 4096);
485 if (size <= 0) {
486 close(local);
487 unlink(filename);
488 if (!size)
489 return error("Connection closed?");
490 perror("Reading from connection");
491 return -1;
493 write(local, buf, size);
494 stream.avail_in = size;
495 stream.next_in = buf;
496 do {
497 stream.next_out = discard;
498 stream.avail_out = sizeof(discard);
499 ret = inflate(&stream, Z_SYNC_FLUSH);
500 SHA1_Update(&c, discard, sizeof(discard) -
501 stream.avail_out);
502 } while (stream.avail_in && ret == Z_OK);
504 } while (ret == Z_OK);
505 inflateEnd(&stream);
507 close(local);
508 SHA1_Final(real_sha1, &c);
509 if (ret != Z_STREAM_END) {
510 unlink(filename);
511 return error("File %s corrupted", sha1_to_hex(sha1));
513 if (memcmp(sha1, real_sha1, 20)) {
514 unlink(filename);
515 return error("File %s has bad hash\n", sha1_to_hex(sha1));
518 return 0;
521 int has_sha1_file(const unsigned char *sha1)
523 struct stat st;
524 return !!find_sha1_file(sha1, &st);
527 int index_fd(unsigned char *sha1, int fd, struct stat *st)
529 unsigned long size = st->st_size;
530 void *buf;
531 int ret;
533 buf = "";
534 if (size)
535 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
536 close(fd);
537 if ((int)(long)buf == -1)
538 return -1;
540 ret = write_sha1_file(buf, size, "blob", sha1);
541 if (size)
542 munmap(buf, size);
543 return ret;