2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
16 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
17 #define O_NOATIME 01000000
23 const unsigned char null_sha1[20] = { 0, };
25 static unsigned int sha1_file_open_flag = O_NOATIME;
27 static unsigned hexval(char c)
29 if (c >= '0' && c <= '9')
31 if (c >= 'a' && c <= 'f')
33 if (c >= 'A' && c <= 'F')
38 int get_sha1_hex(const char *hex, unsigned char *sha1)
41 for (i = 0; i < 20; i++) {
42 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
51 static char *git_dir, *git_object_dir, *git_index_file, *git_refs_dir,
53 static void setup_git_env(void)
55 git_dir = getenv(GIT_DIR_ENVIRONMENT);
57 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
58 git_object_dir = getenv(DB_ENVIRONMENT);
59 if (!git_object_dir) {
60 git_object_dir = xmalloc(strlen(git_dir) + 9);
61 sprintf(git_object_dir, "%s/objects", git_dir);
63 git_refs_dir = xmalloc(strlen(git_dir) + 6);
64 sprintf(git_refs_dir, "%s/refs", git_dir);
65 git_index_file = getenv(INDEX_ENVIRONMENT);
66 if (!git_index_file) {
67 git_index_file = xmalloc(strlen(git_dir) + 7);
68 sprintf(git_index_file, "%s/index", git_dir);
70 git_graft_file = getenv(GRAFT_ENVIRONMENT);
72 git_graft_file = strdup(git_path("info/grafts"));
75 char *get_git_dir(void)
82 char *get_object_directory(void)
86 return git_object_dir;
89 char *get_refs_directory(void)
96 char *get_index_file(void)
100 return git_index_file;
103 char *get_graft_file(void)
107 return git_graft_file;
110 int safe_create_leading_directories(char *path)
115 pos = strchr(pos, '/');
119 if (mkdir(path, 0777) < 0)
120 if (errno != EEXIST) {
129 char * sha1_to_hex(const unsigned char *sha1)
131 static char buffer[50];
132 static const char hex[] = "0123456789abcdef";
136 for (i = 0; i < 20; i++) {
137 unsigned int val = *sha1++;
138 *buf++ = hex[val >> 4];
139 *buf++ = hex[val & 0xf];
144 static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
147 for (i = 0; i < 20; i++) {
148 static char hex[] = "0123456789abcdef";
149 unsigned int val = sha1[i];
150 char *pos = pathbuf + i*2 + (i > 0);
151 *pos++ = hex[val >> 4];
152 *pos = hex[val & 0xf];
157 * NOTE! This returns a statically allocated buffer, so you have to be
158 * careful about using it. Do a "strdup()" if you need to save the
161 * Also note that this returns the location for creating. Reading
162 * SHA1 file can happen from any alternate directory listed in the
163 * DB_ENVIRONMENT environment variable if it is not found in
164 * the primary object database.
166 char *sha1_file_name(const unsigned char *sha1)
168 static char *name, *base;
171 const char *sha1_file_directory = get_object_directory();
172 int len = strlen(sha1_file_directory);
173 base = xmalloc(len + 60);
174 memcpy(base, sha1_file_directory, len);
175 memset(base+len, 0, 60);
178 name = base + len + 1;
180 fill_sha1_path(name, sha1);
184 char *sha1_pack_name(const unsigned char *sha1)
186 static const char hex[] = "0123456789abcdef";
187 static char *name, *base, *buf;
191 const char *sha1_file_directory = get_object_directory();
192 int len = strlen(sha1_file_directory);
193 base = xmalloc(len + 60);
194 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory);
195 name = base + len + 11;
200 for (i = 0; i < 20; i++) {
201 unsigned int val = *sha1++;
202 *buf++ = hex[val >> 4];
203 *buf++ = hex[val & 0xf];
209 char *sha1_pack_index_name(const unsigned char *sha1)
211 static const char hex[] = "0123456789abcdef";
212 static char *name, *base, *buf;
216 const char *sha1_file_directory = get_object_directory();
217 int len = strlen(sha1_file_directory);
218 base = xmalloc(len + 60);
219 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory);
220 name = base + len + 11;
225 for (i = 0; i < 20; i++) {
226 unsigned int val = *sha1++;
227 *buf++ = hex[val >> 4];
228 *buf++ = hex[val & 0xf];
234 struct alternate_object_database *alt_odb_list;
235 static struct alternate_object_database **alt_odb_tail;
238 * Prepare alternate object database registry.
240 * The variable alt_odb_list points at the list of struct
241 * alternate_object_database. The elements on this list come from
242 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
243 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
244 * whose contents is exactly in the same format as that environment
245 * variable. Its base points at a statically allocated buffer that
246 * contains "/the/directory/corresponding/to/.git/objects/...", while
247 * its name points just after the slash at the end of ".git/objects/"
248 * in the example above, and has enough space to hold 40-byte hex
249 * SHA1, an extra slash for the first level indirection, and the
252 static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
253 const char *relative_base)
255 const char *cp, *last;
256 struct alternate_object_database *ent;
262 if (cp < ep && *cp == '#') {
263 while (cp < ep && *cp != sep)
268 for ( ; cp < ep && *cp != sep; cp++)
271 /* 43 = 40-byte + 2 '/' + terminating NUL */
272 int pfxlen = cp - last;
273 int entlen = pfxlen + 43;
275 if (*last != '/' && relative_base) {
276 /* Relative alt-odb */
278 base_len = strlen(relative_base) + 1;
282 ent = xmalloc(sizeof(*ent) + entlen);
284 alt_odb_tail = &(ent->next);
286 if (*last != '/' && relative_base) {
287 memcpy(ent->base, relative_base, base_len - 1);
288 ent->base[base_len - 1] = '/';
289 memcpy(ent->base + base_len,
293 memcpy(ent->base, last, pfxlen);
294 ent->name = ent->base + pfxlen + 1;
295 ent->base[pfxlen] = ent->base[pfxlen + 3] = '/';
296 ent->base[entlen-1] = 0;
298 while (cp < ep && *cp == sep)
304 void prepare_alt_odb(void)
312 alt = getenv(ALTERNATE_DB_ENVIRONMENT);
317 alt_odb_tail = &alt_odb_list;
318 link_alt_odb_entries(alt, alt + strlen(alt), ':', NULL);
320 sprintf(path, "%s/info/alternates", get_object_directory());
321 fd = open(path, O_RDONLY);
324 if (fstat(fd, &st) || (st.st_size == 0)) {
328 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
330 if (map == MAP_FAILED)
333 link_alt_odb_entries(map, map + st.st_size, '\n',
334 get_object_directory());
335 munmap(map, st.st_size);
338 static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
340 char *name = sha1_file_name(sha1);
341 struct alternate_object_database *alt;
346 for (alt = alt_odb_list; alt; alt = alt->next) {
348 fill_sha1_path(name, sha1);
349 if (!stat(alt->base, st))
355 #define PACK_MAX_SZ (1<<26)
356 static int pack_used_ctr;
357 static unsigned long pack_mapped;
358 struct packed_git *packed_git;
360 static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
365 unsigned long idx_size;
367 int fd = open(path, O_RDONLY);
371 if (fstat(fd, &st)) {
375 idx_size = st.st_size;
376 idx_map = mmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
378 if (idx_map == MAP_FAILED)
383 *idx_size_ = idx_size;
385 /* check index map */
386 if (idx_size < 4*256 + 20 + 20)
387 return error("index file too small");
389 for (i = 0; i < 256; i++) {
390 unsigned int n = ntohl(index[i]);
392 return error("non-monotonic index");
398 * - 256 index entries 4 bytes each
399 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
400 * - 20-byte SHA1 of the packfile
401 * - 20-byte SHA1 file checksum
403 if (idx_size != 4*256 + nr * 24 + 20 + 20)
404 return error("wrong index file size");
409 static int unuse_one_packed_git(void)
411 struct packed_git *p, *lru = NULL;
413 for (p = packed_git; p; p = p->next) {
414 if (p->pack_use_cnt || !p->pack_base)
416 if (!lru || p->pack_last_used < lru->pack_last_used)
421 munmap(lru->pack_base, lru->pack_size);
422 lru->pack_base = NULL;
426 void unuse_packed_git(struct packed_git *p)
431 int use_packed_git(struct packed_git *p)
435 // We created the struct before we had the pack
436 stat(p->pack_name, &st);
437 if (!S_ISREG(st.st_mode))
438 die("packfile %s not a regular file", p->pack_name);
439 p->pack_size = st.st_size;
446 pack_mapped += p->pack_size;
447 while (PACK_MAX_SZ < pack_mapped && unuse_one_packed_git())
449 fd = open(p->pack_name, O_RDONLY);
451 die("packfile %s cannot be opened", p->pack_name);
452 if (fstat(fd, &st)) {
454 die("packfile %s cannot be opened", p->pack_name);
456 if (st.st_size != p->pack_size)
457 die("packfile %s size mismatch.", p->pack_name);
458 map = mmap(NULL, p->pack_size, PROT_READ, MAP_PRIVATE, fd, 0);
460 if (map == MAP_FAILED)
461 die("packfile %s cannot be mapped.", p->pack_name);
464 /* Check if the pack file matches with the index file.
467 if (memcmp((char*)(p->index_base) + p->index_size - 40,
468 p->pack_base + p->pack_size - 20, 20)) {
470 die("packfile %s does not match index.", p->pack_name);
473 p->pack_last_used = pack_used_ctr++;
478 struct packed_git *add_packed_git(char *path, int path_len)
481 struct packed_git *p;
482 unsigned long idx_size;
485 if (check_packed_git_idx(path, &idx_size, &idx_map))
488 /* do we have a corresponding .pack file? */
489 strcpy(path + path_len - 4, ".pack");
490 if (stat(path, &st) || !S_ISREG(st.st_mode)) {
491 munmap(idx_map, idx_size);
494 /* ok, it looks sane as far as we can check without
495 * actually mapping the pack file.
497 p = xmalloc(sizeof(*p) + path_len + 2);
498 strcpy(p->pack_name, path);
499 p->index_size = idx_size;
500 p->pack_size = st.st_size;
501 p->index_base = idx_map;
504 p->pack_last_used = 0;
509 struct packed_git *parse_pack_index(unsigned char *sha1)
511 char *path = sha1_pack_index_name(sha1);
512 return parse_pack_index_file(sha1, path);
515 struct packed_git *parse_pack_index_file(const unsigned char *sha1, char *idx_path)
517 struct packed_git *p;
518 unsigned long idx_size;
522 if (check_packed_git_idx(idx_path, &idx_size, &idx_map))
525 path = sha1_pack_name(sha1);
527 p = xmalloc(sizeof(*p) + strlen(path) + 2);
528 strcpy(p->pack_name, path);
529 p->index_size = idx_size;
531 p->index_base = idx_map;
534 p->pack_last_used = 0;
536 memcpy(p->sha1, sha1, 20);
540 void install_packed_git(struct packed_git *pack)
542 pack->next = packed_git;
546 static void prepare_packed_git_one(char *objdir)
553 sprintf(path, "%s/pack", objdir);
559 while ((de = readdir(dir)) != NULL) {
560 int namelen = strlen(de->d_name);
561 struct packed_git *p;
563 if (strcmp(de->d_name + namelen - 4, ".idx"))
566 /* we have .idx. Is it a file we can map? */
567 strcpy(path + len, de->d_name);
568 p = add_packed_git(path, len + namelen);
571 p->next = packed_git;
577 void prepare_packed_git(void)
579 static int run_once = 0;
580 struct alternate_object_database *alt;
584 prepare_packed_git_one(get_object_directory());
586 for (alt = alt_odb_list; alt; alt = alt->next) {
588 prepare_packed_git_one(alt->base);
593 int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)
596 unsigned char real_sha1[20];
600 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
601 SHA1_Update(&c, map, size);
602 SHA1_Final(real_sha1, &c);
603 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
606 static void *map_sha1_file_internal(const unsigned char *sha1,
612 char *filename = find_sha1_file(sha1, &st);
618 fd = open(filename, O_RDONLY | sha1_file_open_flag);
620 /* See if it works without O_NOATIME */
621 switch (sha1_file_open_flag) {
623 fd = open(filename, O_RDONLY);
631 /* If it failed once, it will probably fail again.
632 * Stop using O_NOATIME
634 sha1_file_open_flag = 0;
636 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
638 if (map == MAP_FAILED)
644 int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size)
646 /* Get the data stream */
647 memset(stream, 0, sizeof(*stream));
648 stream->next_in = map;
649 stream->avail_in = mapsize;
650 stream->next_out = buffer;
651 stream->avail_out = size;
654 return inflate(stream, 0);
657 static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
659 int bytes = strlen(buffer) + 1;
660 unsigned char *buf = xmalloc(1+size);
662 memcpy(buf, buffer + bytes, stream->total_out - bytes);
663 bytes = stream->total_out - bytes;
665 stream->next_out = buf + bytes;
666 stream->avail_out = size - bytes;
667 while (inflate(stream, Z_FINISH) == Z_OK)
676 * We used to just use "sscanf()", but that's actually way
677 * too permissive for what we want to check. So do an anal
678 * object header parse by hand.
680 int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
686 * The type can be at most ten bytes (including the
687 * terminating '\0' that we add), and is followed by
702 * The length must follow immediately, and be in canonical
703 * decimal format (ie "010" is not valid).
710 unsigned long c = *hdr - '0';
714 size = size * 10 + c;
720 * The length must be followed by a zero byte
722 return *hdr ? -1 : 0;
725 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
731 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
732 if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0)
735 return unpack_sha1_rest(&stream, hdr, *size);
738 /* forward declaration for a mutually recursive function */
739 static int packed_object_info(struct pack_entry *entry,
740 char *type, unsigned long *sizep);
742 static int packed_delta_info(unsigned char *base_sha1,
743 unsigned long delta_size,
746 unsigned long *sizep,
747 struct packed_git *p)
749 struct pack_entry base_ent;
752 die("truncated pack file");
754 /* The base entry _must_ be in the same pack */
755 if (!find_pack_entry_one(base_sha1, &base_ent, p))
756 die("failed to find delta-pack base object %s",
757 sha1_to_hex(base_sha1));
759 /* We choose to only get the type of the base object and
760 * ignore potentially corrupt pack file that expects the delta
761 * based on a base with a wrong size. This saves tons of
765 if (packed_object_info(&base_ent, type, NULL))
766 die("cannot get info for delta-pack base");
769 const unsigned char *data;
770 unsigned char delta_head[64];
771 unsigned long result_size;
775 memset(&stream, 0, sizeof(stream));
777 data = stream.next_in = base_sha1 + 20;
778 stream.avail_in = left - 20;
779 stream.next_out = delta_head;
780 stream.avail_out = sizeof(delta_head);
782 inflateInit(&stream);
783 st = inflate(&stream, Z_FINISH);
785 if ((st != Z_STREAM_END) &&
786 stream.total_out != sizeof(delta_head))
787 die("delta data unpack-initial failed");
789 /* Examine the initial part of the delta to figure out
793 get_delta_hdr_size(&data); /* ignore base size */
795 /* Read the result size */
796 result_size = get_delta_hdr_size(&data);
797 *sizep = result_size;
802 static unsigned long unpack_object_header(struct packed_git *p, unsigned long offset,
803 enum object_type *type, unsigned long *sizep)
806 unsigned char *pack, c;
809 if (offset >= p->pack_size)
810 die("object offset outside of pack file");
812 pack = p->pack_base + offset;
815 *type = (c >> 4) & 7;
819 if (offset >= p->pack_size)
820 die("object offset outside of pack file");
823 size += (c & 0x7f) << shift;
830 void packed_object_info_detail(struct pack_entry *e,
833 unsigned long *store_size,
834 int *delta_chain_length,
835 unsigned char *base_sha1)
837 struct packed_git *p = e->p;
838 unsigned long offset, left;
840 enum object_type kind;
842 offset = unpack_object_header(p, e->offset, &kind, size);
843 pack = p->pack_base + offset;
844 left = p->pack_size - offset;
845 if (kind != OBJ_DELTA)
846 *delta_chain_length = 0;
848 int chain_length = 0;
849 memcpy(base_sha1, pack, 20);
851 struct pack_entry base_ent;
854 find_pack_entry_one(pack, &base_ent, p);
855 offset = unpack_object_header(p, base_ent.offset,
857 pack = p->pack_base + offset;
859 } while (kind == OBJ_DELTA);
860 *delta_chain_length = chain_length;
864 strcpy(type, "commit");
867 strcpy(type, "tree");
870 strcpy(type, "blob");
876 die("corrupted pack file %s containing object of kind %d",
879 *store_size = 0; /* notyet */
882 static int packed_object_info(struct pack_entry *entry,
883 char *type, unsigned long *sizep)
885 struct packed_git *p = entry->p;
886 unsigned long offset, size, left;
888 enum object_type kind;
891 if (use_packed_git(p))
892 die("cannot map packed file");
894 offset = unpack_object_header(p, entry->offset, &kind, &size);
895 pack = p->pack_base + offset;
896 left = p->pack_size - offset;
900 retval = packed_delta_info(pack, size, left, type, sizep, p);
904 strcpy(type, "commit");
907 strcpy(type, "tree");
910 strcpy(type, "blob");
916 die("corrupted pack file %s containing object of kind %d",
925 /* forward declaration for a mutually recursive function */
926 static void *unpack_entry(struct pack_entry *, char *, unsigned long *);
928 static void *unpack_delta_entry(unsigned char *base_sha1,
929 unsigned long delta_size,
932 unsigned long *sizep,
933 struct packed_git *p)
935 struct pack_entry base_ent;
936 void *data, *delta_data, *result, *base;
937 unsigned long data_size, result_size, base_size;
942 die("truncated pack file");
943 data = base_sha1 + 20;
944 data_size = left - 20;
945 delta_data = xmalloc(delta_size);
947 memset(&stream, 0, sizeof(stream));
949 stream.next_in = data;
950 stream.avail_in = data_size;
951 stream.next_out = delta_data;
952 stream.avail_out = delta_size;
954 inflateInit(&stream);
955 st = inflate(&stream, Z_FINISH);
957 if ((st != Z_STREAM_END) || stream.total_out != delta_size)
958 die("delta data unpack failed");
960 /* The base entry _must_ be in the same pack */
961 if (!find_pack_entry_one(base_sha1, &base_ent, p))
962 die("failed to find delta-pack base object %s",
963 sha1_to_hex(base_sha1));
964 base = unpack_entry_gently(&base_ent, type, &base_size);
966 die("failed to read delta-pack base object %s",
967 sha1_to_hex(base_sha1));
968 result = patch_delta(base, base_size,
969 delta_data, delta_size,
972 die("failed to apply delta");
975 *sizep = result_size;
979 static void *unpack_non_delta_entry(unsigned char *data,
985 unsigned char *buffer;
987 buffer = xmalloc(size + 1);
989 memset(&stream, 0, sizeof(stream));
990 stream.next_in = data;
991 stream.avail_in = left;
992 stream.next_out = buffer;
993 stream.avail_out = size;
995 inflateInit(&stream);
996 st = inflate(&stream, Z_FINISH);
998 if ((st != Z_STREAM_END) || stream.total_out != size) {
1006 static void *unpack_entry(struct pack_entry *entry,
1007 char *type, unsigned long *sizep)
1009 struct packed_git *p = entry->p;
1012 if (use_packed_git(p))
1013 die("cannot map packed file");
1014 retval = unpack_entry_gently(entry, type, sizep);
1015 unuse_packed_git(p);
1017 die("corrupted pack file %s", p->pack_name);
1021 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
1022 void *unpack_entry_gently(struct pack_entry *entry,
1023 char *type, unsigned long *sizep)
1025 struct packed_git *p = entry->p;
1026 unsigned long offset, size, left;
1027 unsigned char *pack;
1028 enum object_type kind;
1031 offset = unpack_object_header(p, entry->offset, &kind, &size);
1032 pack = p->pack_base + offset;
1033 left = p->pack_size - offset;
1036 retval = unpack_delta_entry(pack, size, left, type, sizep, p);
1039 strcpy(type, "commit");
1042 strcpy(type, "tree");
1045 strcpy(type, "blob");
1048 strcpy(type, "tag");
1054 retval = unpack_non_delta_entry(pack, size, left);
1058 int num_packed_objects(const struct packed_git *p)
1060 /* See check_packed_git_idx() */
1061 return (p->index_size - 20 - 20 - 4*256) / 24;
1064 int nth_packed_object_sha1(const struct packed_git *p, int n,
1065 unsigned char* sha1)
1067 void *index = p->index_base + 256;
1068 if (n < 0 || num_packed_objects(p) <= n)
1070 memcpy(sha1, (index + 24 * n + 4), 20);
1074 int find_pack_entry_one(const unsigned char *sha1,
1075 struct pack_entry *e, struct packed_git *p)
1077 unsigned int *level1_ofs = p->index_base;
1078 int hi = ntohl(level1_ofs[*sha1]);
1079 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
1080 void *index = p->index_base + 256;
1083 int mi = (lo + hi) / 2;
1084 int cmp = memcmp(index + 24 * mi + 4, sha1, 20);
1086 e->offset = ntohl(*((int*)(index + 24 * mi)));
1087 memcpy(e->sha1, sha1, 20);
1099 static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
1101 struct packed_git *p;
1102 prepare_packed_git();
1104 for (p = packed_git; p; p = p->next) {
1105 if (find_pack_entry_one(sha1, e, p))
1111 struct packed_git *find_sha1_pack(const unsigned char *sha1,
1112 struct packed_git *packs)
1114 struct packed_git *p;
1115 struct pack_entry e;
1117 for (p = packs; p; p = p->next) {
1118 if (find_pack_entry_one(sha1, &e, p))
1125 int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
1128 unsigned long mapsize, size;
1133 map = map_sha1_file_internal(sha1, &mapsize);
1135 struct pack_entry e;
1137 if (!find_pack_entry(sha1, &e))
1138 return error("unable to find %s", sha1_to_hex(sha1));
1139 return packed_object_info(&e, type, sizep);
1141 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1142 status = error("unable to unpack %s header",
1144 if (parse_sha1_header(hdr, type, &size) < 0)
1145 status = error("unable to parse %s header", sha1_to_hex(sha1));
1151 inflateEnd(&stream);
1152 munmap(map, mapsize);
1156 static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size)
1158 struct pack_entry e;
1160 if (!find_pack_entry(sha1, &e)) {
1161 error("cannot read sha1_file for %s", sha1_to_hex(sha1));
1164 return unpack_entry(&e, type, size);
1167 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
1169 unsigned long mapsize;
1171 struct pack_entry e;
1173 if (find_pack_entry(sha1, &e))
1174 return read_packed_sha1(sha1, type, size);
1175 map = map_sha1_file_internal(sha1, &mapsize);
1177 buf = unpack_sha1_file(map, mapsize, type, size);
1178 munmap(map, mapsize);
1184 void *read_object_with_reference(const unsigned char *sha1,
1185 const char *required_type,
1186 unsigned long *size,
1187 unsigned char *actual_sha1_return)
1191 unsigned long isize;
1192 unsigned char actual_sha1[20];
1194 memcpy(actual_sha1, sha1, 20);
1196 int ref_length = -1;
1197 const char *ref_type = NULL;
1199 buffer = read_sha1_file(actual_sha1, type, &isize);
1202 if (!strcmp(type, required_type)) {
1204 if (actual_sha1_return)
1205 memcpy(actual_sha1_return, actual_sha1, 20);
1208 /* Handle references */
1209 else if (!strcmp(type, "commit"))
1211 else if (!strcmp(type, "tag"))
1212 ref_type = "object ";
1217 ref_length = strlen(ref_type);
1219 if (memcmp(buffer, ref_type, ref_length) ||
1220 get_sha1_hex(buffer + ref_length, actual_sha1)) {
1225 /* Now we have the ID of the referred-to object in
1226 * actual_sha1. Check again. */
1230 char *write_sha1_file_prepare(void *buf,
1233 unsigned char *sha1,
1239 /* Generate the header */
1240 *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
1244 SHA1_Update(&c, hdr, *hdrlen);
1245 SHA1_Update(&c, buf, len);
1246 SHA1_Final(sha1, &c);
1248 return sha1_file_name(sha1);
1251 int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
1254 unsigned char *compressed;
1256 unsigned char sha1[20];
1258 static char tmpfile[PATH_MAX];
1259 unsigned char hdr[50];
1260 int fd, hdrlen, ret;
1262 /* Normally if we have it in the pack then we do not bother writing
1263 * it out into .git/objects/??/?{38} file.
1265 filename = write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
1267 memcpy(returnsha1, sha1, 20);
1268 if (has_sha1_file(sha1))
1270 fd = open(filename, O_RDONLY);
1273 * FIXME!!! We might do collision checking here, but we'd
1274 * need to uncompress the old file and check it. Later.
1280 if (errno != ENOENT) {
1281 fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno));
1285 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
1287 fd = mkstemp(tmpfile);
1289 fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno));
1294 memset(&stream, 0, sizeof(stream));
1295 deflateInit(&stream, Z_BEST_COMPRESSION);
1296 size = deflateBound(&stream, len+hdrlen);
1297 compressed = xmalloc(size);
1300 stream.next_out = compressed;
1301 stream.avail_out = size;
1303 /* First header.. */
1304 stream.next_in = hdr;
1305 stream.avail_in = hdrlen;
1306 while (deflate(&stream, 0) == Z_OK)
1309 /* Then the data itself.. */
1310 stream.next_in = buf;
1311 stream.avail_in = len;
1312 while (deflate(&stream, Z_FINISH) == Z_OK)
1314 deflateEnd(&stream);
1315 size = stream.total_out;
1317 if (write(fd, compressed, size) != size)
1318 die("unable to write file");
1323 ret = link(tmpfile, filename);
1328 * Coda hack - coda doesn't like cross-directory links,
1329 * so we fall back to a rename, which will mean that it
1330 * won't be able to check collisions, but that's not a
1333 * When this succeeds, we just return 0. We have nothing
1336 if (ret == EXDEV && !rename(tmpfile, filename))
1341 if (ret != EEXIST) {
1342 fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
1345 /* FIXME!!! Collision check here ? */
1351 int write_sha1_to_fd(int fd, const unsigned char *sha1)
1354 unsigned long objsize;
1356 void *map = map_sha1_file_internal(sha1, &objsize);
1358 void *temp_obj = NULL;
1362 unsigned char *unpacked;
1367 // need to unpack and recompress it by itself
1368 unpacked = read_packed_sha1(sha1, type, &len);
1370 hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
1373 memset(&stream, 0, sizeof(stream));
1374 deflateInit(&stream, Z_BEST_COMPRESSION);
1375 size = deflateBound(&stream, len + hdrlen);
1376 temp_obj = buf = xmalloc(size);
1379 stream.next_out = buf;
1380 stream.avail_out = size;
1382 /* First header.. */
1383 stream.next_in = (void *)hdr;
1384 stream.avail_in = hdrlen;
1385 while (deflate(&stream, 0) == Z_OK)
1388 /* Then the data itself.. */
1389 stream.next_in = unpacked;
1390 stream.avail_in = len;
1391 while (deflate(&stream, Z_FINISH) == Z_OK)
1393 deflateEnd(&stream);
1396 objsize = stream.total_out;
1400 size = write(fd, buf + posn, objsize - posn);
1403 fprintf(stderr, "write closed");
1410 } while (posn < objsize);
1413 munmap(map, objsize);
1420 int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
1421 size_t bufsize, size_t *bufposn)
1423 char *filename = sha1_file_name(sha1);
1427 unsigned char real_sha1[20];
1428 unsigned char discard[4096];
1432 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
1435 return error("Couldn't open %s\n", filename);
1437 memset(&stream, 0, sizeof(stream));
1439 inflateInit(&stream);
1446 stream.avail_in = *bufposn;
1447 stream.next_in = (unsigned char *) buffer;
1449 stream.next_out = discard;
1450 stream.avail_out = sizeof(discard);
1451 ret = inflate(&stream, Z_SYNC_FLUSH);
1452 SHA1_Update(&c, discard, sizeof(discard) -
1454 } while (stream.avail_in && ret == Z_OK);
1455 write(local, buffer, *bufposn - stream.avail_in);
1456 memmove(buffer, buffer + *bufposn - stream.avail_in,
1458 *bufposn = stream.avail_in;
1462 size = read(fd, buffer + *bufposn, bufsize - *bufposn);
1467 return error("Connection closed?");
1468 perror("Reading from connection");
1473 inflateEnd(&stream);
1476 SHA1_Final(real_sha1, &c);
1477 if (ret != Z_STREAM_END) {
1479 return error("File %s corrupted", sha1_to_hex(sha1));
1481 if (memcmp(sha1, real_sha1, 20)) {
1483 return error("File %s has bad hash\n", sha1_to_hex(sha1));
1489 int has_pack_index(const unsigned char *sha1)
1492 if (stat(sha1_pack_index_name(sha1), &st))
1497 int has_pack_file(const unsigned char *sha1)
1500 if (stat(sha1_pack_name(sha1), &st))
1505 int has_sha1_pack(const unsigned char *sha1)
1507 struct pack_entry e;
1508 return find_pack_entry(sha1, &e);
1511 int has_sha1_file(const unsigned char *sha1)
1514 struct pack_entry e;
1516 if (find_pack_entry(sha1, &e))
1518 return find_sha1_file(sha1, &st) ? 1 : 0;
1521 int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type)
1523 unsigned long size = st->st_size;
1526 unsigned char hdr[50];
1531 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1533 if (buf == MAP_FAILED)
1539 ret = write_sha1_file(buf, size, type, sha1);
1541 write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);