2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
14 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
15 #define O_NOATIME 01000000
21 static unsigned int sha1_file_open_flag = O_NOATIME;
23 static unsigned hexval(char c)
25 if (c >= '0' && c <= '9')
27 if (c >= 'a' && c <= 'f')
29 if (c >= 'A' && c <= 'F')
34 int get_sha1_hex(const char *hex, unsigned char *sha1)
37 for (i = 0; i < 20; i++) {
38 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
47 int get_sha1_file(const char *path, unsigned char *result)
50 int fd = open(path, O_RDONLY);
55 len = read(fd, buffer, sizeof(buffer));
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[] = {
76 if (!get_sha1_hex(str, sha1))
80 for (p = prefix; *p; p++) {
81 snprintf(pathname, sizeof(pathname), "%s/%s/%s", gitdir, *p, str);
82 if (!get_sha1_file(pathname, sha1))
89 char * sha1_to_hex(const unsigned char *sha1)
91 static char buffer[50];
92 static const char hex[] = "0123456789abcdef";
96 for (i = 0; i < 20; i++) {
97 unsigned int val = *sha1++;
98 *buf++ = hex[val >> 4];
99 *buf++ = hex[val & 0xf];
104 static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
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
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;
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);
138 name = base + len + 1;
140 fill_sha1_path(name, sha1);
144 static struct alternate_object_database {
150 * Prepare alternate object database registry.
151 * alt_odb points at an array of struct alternate_object_database.
152 * This array is terminated with an element that has both its base
153 * and name set to NULL. alt_odb[n] comes from n'th non-empty
154 * element from colon separated $SHA1_FILE_DIRECTORIES environment
155 * variable, and its base points at a statically allocated buffer
156 * that contains "/the/directory/corresponding/to/.git/objects/...",
157 * while its name points just after the slash at the end of
158 * ".git/objects/" in the example above, and has enough space to hold
159 * 40-byte hex SHA1, an extra slash for the first level indirection,
160 * and the terminating NUL.
161 * This function allocates the alt_odb array and all the strings
162 * pointed by base fields of the array elements with one xmalloc();
163 * the string pool immediately follows the array.
165 static void prepare_alt_odb(void)
168 const char *cp, *last;
170 const char *alt = getenv(ALTERNATE_DB_ENVIRONMENT) ? : "";
172 /* The first pass counts how large an area to allocate to
173 * hold the entire alt_odb structure, including array of
174 * structs and path buffers for them. The second pass fills
175 * the structure and prepares the path buffers for use by
178 for (totlen = pass = 0; pass < 2; pass++) {
182 cp = strchr(last, ':') ? : last + strlen(last);
184 /* 43 = 40-byte + 2 '/' + terminating NUL */
185 int pfxlen = cp - last;
186 int entlen = pfxlen + 43;
190 alt_odb[i].base = op;
191 alt_odb[i].name = op + pfxlen + 1;
192 memcpy(op, last, pfxlen);
193 op[pfxlen] = op[pfxlen + 3] = '/';
199 while (*cp && *cp == ':')
205 alt_odb = xmalloc(sizeof(*alt_odb) * (i + 1) + totlen);
206 alt_odb[i].base = alt_odb[i].name = 0;
207 op = (char*)(&alt_odb[i+1]);
211 static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
214 char *name = sha1_file_name(sha1);
220 for (i = 0; (name = alt_odb[i].name) != NULL; i++) {
221 fill_sha1_path(name, sha1);
222 if (!stat(alt_odb[i].base, st))
223 return alt_odb[i].base;
228 int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)
231 unsigned char real_sha1[20];
235 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
236 SHA1_Update(&c, map, size);
237 SHA1_Final(real_sha1, &c);
238 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
241 void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
246 char *filename = find_sha1_file(sha1, &st);
249 error("cannot map sha1 file %s", sha1_to_hex(sha1));
253 fd = open(filename, O_RDONLY | sha1_file_open_flag);
255 /* See if it works without O_NOATIME */
256 switch (sha1_file_open_flag) {
258 fd = open(filename, O_RDONLY);
267 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
268 sha1_file_open_flag = 0;
270 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
272 if (-1 == (int)(long)map)
278 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
285 /* Get the data stream */
286 memset(&stream, 0, sizeof(stream));
287 stream.next_in = map;
288 stream.avail_in = mapsize;
289 stream.next_out = buffer;
290 stream.avail_out = sizeof(buffer);
292 inflateInit(&stream);
293 ret = inflate(&stream, 0);
296 if (sscanf(buffer, "%10s %lu", type, size) != 2)
299 bytes = strlen(buffer) + 1;
300 buf = xmalloc(*size);
302 memcpy(buf, buffer + bytes, stream.total_out - bytes);
303 bytes = stream.total_out - bytes;
304 if (bytes < *size && ret == Z_OK) {
305 stream.next_out = buf + bytes;
306 stream.avail_out = *size - bytes;
307 while (inflate(&stream, Z_FINISH) == Z_OK)
314 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
316 unsigned long mapsize;
319 map = map_sha1_file(sha1, &mapsize);
321 buf = unpack_sha1_file(map, mapsize, type, size);
322 munmap(map, mapsize);
328 void *read_object_with_reference(const unsigned char *sha1,
329 const unsigned char *required_type,
331 unsigned char *actual_sha1_return)
336 unsigned char actual_sha1[20];
338 memcpy(actual_sha1, sha1, 20);
341 const char *ref_type = NULL;
343 buffer = read_sha1_file(actual_sha1, type, &isize);
346 if (!strcmp(type, required_type)) {
348 if (actual_sha1_return)
349 memcpy(actual_sha1_return, actual_sha1, 20);
352 /* Handle references */
353 else if (!strcmp(type, "commit"))
355 else if (!strcmp(type, "tag"))
356 ref_type = "object ";
361 ref_length = strlen(ref_type);
363 if (memcmp(buffer, ref_type, ref_length) ||
364 get_sha1_hex(buffer + ref_length, actual_sha1)) {
368 /* Now we have the ID of the referred-to object in
369 * actual_sha1. Check again. */
373 int write_sha1_file(char *buf, unsigned long len, const char *type, unsigned char *returnsha1)
378 unsigned char sha1[20];
381 static char tmpfile[PATH_MAX];
385 /* Generate the header */
386 hdrlen = sprintf(hdr, "%s %lu", type, len)+1;
390 SHA1_Update(&c, hdr, hdrlen);
391 SHA1_Update(&c, buf, len);
392 SHA1_Final(sha1, &c);
395 memcpy(returnsha1, sha1, 20);
397 filename = sha1_file_name(sha1);
398 fd = open(filename, O_RDONLY);
401 * FIXME!!! We might do collision checking here, but we'd
402 * need to uncompress the old file and check it. Later.
408 if (errno != ENOENT) {
409 fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno));
413 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
415 fd = mkstemp(tmpfile);
417 fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno));
422 memset(&stream, 0, sizeof(stream));
423 deflateInit(&stream, Z_BEST_COMPRESSION);
424 size = deflateBound(&stream, len+hdrlen);
425 compressed = xmalloc(size);
428 stream.next_out = compressed;
429 stream.avail_out = size;
432 stream.next_in = hdr;
433 stream.avail_in = hdrlen;
434 while (deflate(&stream, 0) == Z_OK)
437 /* Then the data itself.. */
438 stream.next_in = buf;
439 stream.avail_in = len;
440 while (deflate(&stream, Z_FINISH) == Z_OK)
443 size = stream.total_out;
445 if (write(fd, compressed, size) != size)
446 die("unable to write file");
451 ret = link(tmpfile, filename);
456 * Coda hack - coda doesn't like cross-directory links,
457 * so we fall back to a rename, which will mean that it
458 * won't be able to check collisions, but that's not a
461 * When this succeeds, we just return 0. We have nothing
464 if (ret == EXDEV && !rename(tmpfile, filename))
470 fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
473 /* FIXME!!! Collision check here ? */
479 int write_sha1_from_fd(const unsigned char *sha1, int fd)
481 char *filename = sha1_file_name(sha1);
485 unsigned char real_sha1[20];
491 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
494 return error("Couldn't open %s\n", filename);
496 memset(&stream, 0, sizeof(stream));
498 inflateInit(&stream);
504 size = read(fd, buf, 4096);
509 return error("Connection closed?");
510 perror("Reading from connection");
513 write(local, buf, size);
514 stream.avail_in = size;
515 stream.next_in = buf;
517 stream.next_out = discard;
518 stream.avail_out = sizeof(discard);
519 ret = inflate(&stream, Z_SYNC_FLUSH);
520 SHA1_Update(&c, discard, sizeof(discard) -
522 } while (stream.avail_in && ret == Z_OK);
524 } while (ret == Z_OK);
528 SHA1_Final(real_sha1, &c);
529 if (ret != Z_STREAM_END) {
531 return error("File %s corrupted", sha1_to_hex(sha1));
533 if (memcmp(sha1, real_sha1, 20)) {
535 return error("File %s has bad hash\n", sha1_to_hex(sha1));
541 int has_sha1_file(const unsigned char *sha1)
544 return !!find_sha1_file(sha1, &st);
547 int index_fd(unsigned char *sha1, int fd, struct stat *st)
549 unsigned long size = st->st_size;
555 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
557 if ((int)(long)buf == -1)
560 ret = write_sha1_file(buf, size, "blob", sha1);