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 static char *git_dir, *git_object_dir, *git_index_file;
63 static void setup_git_env(void)
65 git_dir = gitenv(GIT_DIR_ENVIRONMENT);
67 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
68 git_object_dir = gitenv(DB_ENVIRONMENT);
69 if (!git_object_dir) {
70 git_object_dir = xmalloc(strlen(git_dir) + 9);
71 sprintf(git_object_dir, "%s/objects", git_dir);
73 git_index_file = gitenv(INDEX_ENVIRONMENT);
74 if (!git_index_file) {
75 git_index_file = xmalloc(strlen(git_dir) + 7);
76 sprintf(git_index_file, "%s/index", git_dir);
80 char *get_object_directory(void)
84 return git_object_dir;
87 char *get_index_file(void)
91 return git_index_file;
94 int get_sha1(const char *str, unsigned char *sha1)
96 static char pathname[PATH_MAX];
97 static const char *prefix[] = {
107 if (!get_sha1_hex(str, sha1))
112 for (p = prefix; *p; p++) {
113 snprintf(pathname, sizeof(pathname), "%s/%s/%s",
115 if (!get_sha1_file(pathname, sha1))
122 char * sha1_to_hex(const unsigned char *sha1)
124 static char buffer[50];
125 static const char hex[] = "0123456789abcdef";
129 for (i = 0; i < 20; i++) {
130 unsigned int val = *sha1++;
131 *buf++ = hex[val >> 4];
132 *buf++ = hex[val & 0xf];
137 static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
140 for (i = 0; i < 20; i++) {
141 static char hex[] = "0123456789abcdef";
142 unsigned int val = sha1[i];
143 char *pos = pathbuf + i*2 + (i > 0);
144 *pos++ = hex[val >> 4];
145 *pos = hex[val & 0xf];
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
154 * Also note that this returns the location for creating. Reading
155 * SHA1 file can happen from any alternate directory listed in the
156 * DB_ENVIRONMENT environment variable if it is not found in
157 * the primary object database.
159 char *sha1_file_name(const unsigned char *sha1)
161 static char *name, *base;
164 const char *sha1_file_directory = get_object_directory();
165 int len = strlen(sha1_file_directory);
166 base = xmalloc(len + 60);
167 memcpy(base, sha1_file_directory, len);
168 memset(base+len, 0, 60);
171 name = base + len + 1;
173 fill_sha1_path(name, sha1);
177 static struct alternate_object_database {
183 * Prepare alternate object database registry.
184 * alt_odb points at an array of struct alternate_object_database.
185 * This array is terminated with an element that has both its base
186 * and name set to NULL. alt_odb[n] comes from n'th non-empty
187 * element from colon separated ALTERNATE_DB_ENVIRONMENT environment
188 * variable, and its base points at a statically allocated buffer
189 * that contains "/the/directory/corresponding/to/.git/objects/...",
190 * while its name points just after the slash at the end of
191 * ".git/objects/" in the example above, and has enough space to hold
192 * 40-byte hex SHA1, an extra slash for the first level indirection,
193 * and the terminating NUL.
194 * This function allocates the alt_odb array and all the strings
195 * pointed by base fields of the array elements with one xmalloc();
196 * the string pool immediately follows the array.
198 static void prepare_alt_odb(void)
201 const char *cp, *last;
203 const char *alt = gitenv(ALTERNATE_DB_ENVIRONMENT) ? : "";
205 /* The first pass counts how large an area to allocate to
206 * hold the entire alt_odb structure, including array of
207 * structs and path buffers for them. The second pass fills
208 * the structure and prepares the path buffers for use by
211 for (totlen = pass = 0; pass < 2; pass++) {
215 cp = strchr(last, ':') ? : last + strlen(last);
217 /* 43 = 40-byte + 2 '/' + terminating NUL */
218 int pfxlen = cp - last;
219 int entlen = pfxlen + 43;
223 alt_odb[i].base = op;
224 alt_odb[i].name = op + pfxlen + 1;
225 memcpy(op, last, pfxlen);
226 op[pfxlen] = op[pfxlen + 3] = '/';
232 while (*cp && *cp == ':')
238 alt_odb = xmalloc(sizeof(*alt_odb) * (i + 1) + totlen);
239 alt_odb[i].base = alt_odb[i].name = 0;
240 op = (char*)(&alt_odb[i+1]);
244 static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
247 char *name = sha1_file_name(sha1);
253 for (i = 0; (name = alt_odb[i].name) != NULL; i++) {
254 fill_sha1_path(name, sha1);
255 if (!stat(alt_odb[i].base, st))
256 return alt_odb[i].base;
261 int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)
264 unsigned char real_sha1[20];
268 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
269 SHA1_Update(&c, map, size);
270 SHA1_Final(real_sha1, &c);
271 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
274 void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
279 char *filename = find_sha1_file(sha1, &st);
282 error("cannot map sha1 file %s", sha1_to_hex(sha1));
286 fd = open(filename, O_RDONLY | sha1_file_open_flag);
288 /* See if it works without O_NOATIME */
289 switch (sha1_file_open_flag) {
291 fd = open(filename, O_RDONLY);
300 /* If it failed once, it will probably fail again. Stop using O_NOATIME */
301 sha1_file_open_flag = 0;
303 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
305 if (-1 == (int)(long)map)
311 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
318 /* Get the data stream */
319 memset(&stream, 0, sizeof(stream));
320 stream.next_in = map;
321 stream.avail_in = mapsize;
322 stream.next_out = (unsigned char *)buffer;
323 stream.avail_out = sizeof(buffer);
325 inflateInit(&stream);
326 ret = inflate(&stream, 0);
329 if (sscanf(buffer, "%10s %lu", type, size) != 2)
332 bytes = strlen(buffer) + 1;
333 buf = xmalloc(*size);
335 memcpy(buf, buffer + bytes, stream.total_out - bytes);
336 bytes = stream.total_out - bytes;
337 if (bytes < *size && ret == Z_OK) {
338 stream.next_out = buf + bytes;
339 stream.avail_out = *size - bytes;
340 while (inflate(&stream, Z_FINISH) == Z_OK)
347 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
349 unsigned long mapsize;
352 map = map_sha1_file(sha1, &mapsize);
354 buf = unpack_sha1_file(map, mapsize, type, size);
355 munmap(map, mapsize);
361 void *read_object_with_reference(const unsigned char *sha1,
362 const char *required_type,
364 unsigned char *actual_sha1_return)
369 unsigned char actual_sha1[20];
371 memcpy(actual_sha1, sha1, 20);
374 const char *ref_type = NULL;
376 buffer = read_sha1_file(actual_sha1, type, &isize);
379 if (!strcmp(type, required_type)) {
381 if (actual_sha1_return)
382 memcpy(actual_sha1_return, actual_sha1, 20);
385 /* Handle references */
386 else if (!strcmp(type, "commit"))
388 else if (!strcmp(type, "tag"))
389 ref_type = "object ";
394 ref_length = strlen(ref_type);
396 if (memcmp(buffer, ref_type, ref_length) ||
397 get_sha1_hex(buffer + ref_length, actual_sha1)) {
401 /* Now we have the ID of the referred-to object in
402 * actual_sha1. Check again. */
406 int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
409 unsigned char *compressed;
411 unsigned char sha1[20];
414 static char tmpfile[PATH_MAX];
415 unsigned char hdr[50];
418 /* Generate the header */
419 hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
423 SHA1_Update(&c, hdr, hdrlen);
424 SHA1_Update(&c, buf, len);
425 SHA1_Final(sha1, &c);
428 memcpy(returnsha1, sha1, 20);
430 filename = sha1_file_name(sha1);
431 fd = open(filename, O_RDONLY);
434 * FIXME!!! We might do collision checking here, but we'd
435 * need to uncompress the old file and check it. Later.
441 if (errno != ENOENT) {
442 fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno));
446 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
448 fd = mkstemp(tmpfile);
450 fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno));
455 memset(&stream, 0, sizeof(stream));
456 deflateInit(&stream, Z_BEST_COMPRESSION);
457 size = deflateBound(&stream, len+hdrlen);
458 compressed = xmalloc(size);
461 stream.next_out = compressed;
462 stream.avail_out = size;
465 stream.next_in = hdr;
466 stream.avail_in = hdrlen;
467 while (deflate(&stream, 0) == Z_OK)
470 /* Then the data itself.. */
471 stream.next_in = buf;
472 stream.avail_in = len;
473 while (deflate(&stream, Z_FINISH) == Z_OK)
476 size = stream.total_out;
478 if (write(fd, compressed, size) != size)
479 die("unable to write file");
484 ret = link(tmpfile, filename);
489 * Coda hack - coda doesn't like cross-directory links,
490 * so we fall back to a rename, which will mean that it
491 * won't be able to check collisions, but that's not a
494 * When this succeeds, we just return 0. We have nothing
497 if (ret == EXDEV && !rename(tmpfile, filename))
503 fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
506 /* FIXME!!! Collision check here ? */
512 int write_sha1_from_fd(const unsigned char *sha1, int fd)
514 char *filename = sha1_file_name(sha1);
518 unsigned char real_sha1[20];
519 unsigned char buf[4096];
520 unsigned char discard[4096];
524 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
527 return error("Couldn't open %s\n", filename);
529 memset(&stream, 0, sizeof(stream));
531 inflateInit(&stream);
537 size = read(fd, buf, 4096);
542 return error("Connection closed?");
543 perror("Reading from connection");
546 write(local, buf, size);
547 stream.avail_in = size;
548 stream.next_in = buf;
550 stream.next_out = discard;
551 stream.avail_out = sizeof(discard);
552 ret = inflate(&stream, Z_SYNC_FLUSH);
553 SHA1_Update(&c, discard, sizeof(discard) -
555 } while (stream.avail_in && ret == Z_OK);
557 } while (ret == Z_OK);
561 SHA1_Final(real_sha1, &c);
562 if (ret != Z_STREAM_END) {
564 return error("File %s corrupted", sha1_to_hex(sha1));
566 if (memcmp(sha1, real_sha1, 20)) {
568 return error("File %s has bad hash\n", sha1_to_hex(sha1));
574 int has_sha1_file(const unsigned char *sha1)
577 return !!find_sha1_file(sha1, &st);
580 int index_fd(unsigned char *sha1, int fd, struct stat *st)
582 unsigned long size = st->st_size;
588 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
590 if ((int)(long)buf == -1)
593 ret = write_sha1_file(buf, size, "blob", sha1);