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 static unsigned int sha1_file_open_flag = O_NOATIME;
25 static unsigned hexval(char c)
27 if (c >= '0' && c <= '9')
29 if (c >= 'a' && c <= 'f')
31 if (c >= 'A' && c <= 'F')
36 int get_sha1_hex(const char *hex, unsigned char *sha1)
39 for (i = 0; i < 20; i++) {
40 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
49 static char *git_dir, *git_object_dir, *git_index_file, *git_refs_dir,
51 static void setup_git_env(void)
53 git_dir = gitenv(GIT_DIR_ENVIRONMENT);
55 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
56 git_object_dir = gitenv(DB_ENVIRONMENT);
57 if (!git_object_dir) {
58 git_object_dir = xmalloc(strlen(git_dir) + 9);
59 sprintf(git_object_dir, "%s/objects", git_dir);
61 git_refs_dir = xmalloc(strlen(git_dir) + 6);
62 sprintf(git_refs_dir, "%s/refs", git_dir);
63 git_index_file = gitenv(INDEX_ENVIRONMENT);
64 if (!git_index_file) {
65 git_index_file = xmalloc(strlen(git_dir) + 7);
66 sprintf(git_index_file, "%s/index", git_dir);
68 git_graft_file = gitenv(GRAFT_ENVIRONMENT);
70 git_graft_file = strdup(git_path("info/grafts"));
73 char *get_object_directory(void)
77 return git_object_dir;
80 char *get_refs_directory(void)
87 char *get_index_file(void)
91 return git_index_file;
94 char *get_graft_file(void)
98 return git_graft_file;
101 int safe_create_leading_directories(char *path)
106 pos = strchr(pos, '/');
110 if (mkdir(path, 0777) < 0)
111 if (errno != EEXIST) {
120 char * sha1_to_hex(const unsigned char *sha1)
122 static char buffer[50];
123 static const char hex[] = "0123456789abcdef";
127 for (i = 0; i < 20; i++) {
128 unsigned int val = *sha1++;
129 *buf++ = hex[val >> 4];
130 *buf++ = hex[val & 0xf];
135 static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
138 for (i = 0; i < 20; i++) {
139 static char hex[] = "0123456789abcdef";
140 unsigned int val = sha1[i];
141 char *pos = pathbuf + i*2 + (i > 0);
142 *pos++ = hex[val >> 4];
143 *pos = hex[val & 0xf];
148 * NOTE! This returns a statically allocated buffer, so you have to be
149 * careful about using it. Do a "strdup()" if you need to save the
152 * Also note that this returns the location for creating. Reading
153 * SHA1 file can happen from any alternate directory listed in the
154 * DB_ENVIRONMENT environment variable if it is not found in
155 * the primary object database.
157 char *sha1_file_name(const unsigned char *sha1)
159 static char *name, *base;
162 const char *sha1_file_directory = get_object_directory();
163 int len = strlen(sha1_file_directory);
164 base = xmalloc(len + 60);
165 memcpy(base, sha1_file_directory, len);
166 memset(base+len, 0, 60);
169 name = base + len + 1;
171 fill_sha1_path(name, sha1);
175 char *sha1_pack_name(const unsigned char *sha1)
177 static const char hex[] = "0123456789abcdef";
178 static char *name, *base, *buf;
182 const char *sha1_file_directory = get_object_directory();
183 int len = strlen(sha1_file_directory);
184 base = xmalloc(len + 60);
185 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory);
186 name = base + len + 11;
191 for (i = 0; i < 20; i++) {
192 unsigned int val = *sha1++;
193 *buf++ = hex[val >> 4];
194 *buf++ = hex[val & 0xf];
200 char *sha1_pack_index_name(const unsigned char *sha1)
202 static const char hex[] = "0123456789abcdef";
203 static char *name, *base, *buf;
207 const char *sha1_file_directory = get_object_directory();
208 int len = strlen(sha1_file_directory);
209 base = xmalloc(len + 60);
210 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory);
211 name = base + len + 11;
216 for (i = 0; i < 20; i++) {
217 unsigned int val = *sha1++;
218 *buf++ = hex[val >> 4];
219 *buf++ = hex[val & 0xf];
225 struct alternate_object_database *alt_odb_list;
226 static struct alternate_object_database **alt_odb_tail;
229 * Prepare alternate object database registry.
231 * The variable alt_odb_list points at the list of struct
232 * alternate_object_database. The elements on this list come from
233 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
234 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
235 * whose contents is exactly in the same format as that environment
236 * variable. Its base points at a statically allocated buffer that
237 * contains "/the/directory/corresponding/to/.git/objects/...", while
238 * its name points just after the slash at the end of ".git/objects/"
239 * in the example above, and has enough space to hold 40-byte hex
240 * SHA1, an extra slash for the first level indirection, and the
243 static void link_alt_odb_entries(const char *alt, const char *ep, int sep)
245 const char *cp, *last;
246 struct alternate_object_database *ent;
251 if (cp < ep && *cp == '#') {
252 while (cp < ep && *cp != sep)
257 for ( ; cp < ep && *cp != sep; cp++)
260 /* 43 = 40-byte + 2 '/' + terminating NUL */
261 int pfxlen = cp - last;
262 int entlen = pfxlen + 43;
264 ent = xmalloc(sizeof(*ent) + entlen);
266 alt_odb_tail = &(ent->next);
269 memcpy(ent->base, last, pfxlen);
270 ent->name = ent->base + pfxlen + 1;
271 ent->base[pfxlen] = ent->base[pfxlen + 3] = '/';
272 ent->base[entlen-1] = 0;
274 while (cp < ep && *cp == sep)
280 void prepare_alt_odb(void)
286 char *alt = gitenv(ALTERNATE_DB_ENVIRONMENT) ? : "";
288 sprintf(path, "%s/info/alternates", get_object_directory());
291 alt_odb_tail = &alt_odb_list;
292 link_alt_odb_entries(alt, alt + strlen(alt), ':');
294 fd = open(path, O_RDONLY);
297 if (fstat(fd, &st) || (st.st_size == 0)) {
301 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
303 if (map == MAP_FAILED)
306 link_alt_odb_entries(map, map + st.st_size, '\n');
307 munmap(map, st.st_size);
310 static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
312 char *name = sha1_file_name(sha1);
313 struct alternate_object_database *alt;
318 for (alt = alt_odb_list; alt; alt = alt->next) {
320 fill_sha1_path(name, sha1);
321 if (!stat(alt->base, st))
327 #define PACK_MAX_SZ (1<<26)
328 static int pack_used_ctr;
329 static unsigned long pack_mapped;
330 struct packed_git *packed_git;
332 static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
337 unsigned long idx_size;
339 int fd = open(path, O_RDONLY);
343 if (fstat(fd, &st)) {
347 idx_size = st.st_size;
348 idx_map = mmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
350 if (idx_map == MAP_FAILED)
355 *idx_size_ = idx_size;
357 /* check index map */
358 if (idx_size < 4*256 + 20 + 20)
359 return error("index file too small");
361 for (i = 0; i < 256; i++) {
362 unsigned int n = ntohl(index[i]);
364 return error("non-monotonic index");
370 * - 256 index entries 4 bytes each
371 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
372 * - 20-byte SHA1 of the packfile
373 * - 20-byte SHA1 file checksum
375 if (idx_size != 4*256 + nr * 24 + 20 + 20)
376 return error("wrong index file size");
381 static int unuse_one_packed_git(void)
383 struct packed_git *p, *lru = NULL;
385 for (p = packed_git; p; p = p->next) {
386 if (p->pack_use_cnt || !p->pack_base)
388 if (!lru || p->pack_last_used < lru->pack_last_used)
393 munmap(lru->pack_base, lru->pack_size);
394 lru->pack_base = NULL;
398 void unuse_packed_git(struct packed_git *p)
403 int use_packed_git(struct packed_git *p)
407 // We created the struct before we had the pack
408 stat(p->pack_name, &st);
409 if (!S_ISREG(st.st_mode))
410 die("packfile %s not a regular file", p->pack_name);
411 p->pack_size = st.st_size;
418 pack_mapped += p->pack_size;
419 while (PACK_MAX_SZ < pack_mapped && unuse_one_packed_git())
421 fd = open(p->pack_name, O_RDONLY);
423 die("packfile %s cannot be opened", p->pack_name);
424 if (fstat(fd, &st)) {
426 die("packfile %s cannot be opened", p->pack_name);
428 if (st.st_size != p->pack_size)
429 die("packfile %s size mismatch.", p->pack_name);
430 map = mmap(NULL, p->pack_size, PROT_READ, MAP_PRIVATE, fd, 0);
432 if (map == MAP_FAILED)
433 die("packfile %s cannot be mapped.", p->pack_name);
436 /* Check if the pack file matches with the index file.
439 if (memcmp((char*)(p->index_base) + p->index_size - 40,
440 p->pack_base + p->pack_size - 20, 20)) {
442 die("packfile %s does not match index.", p->pack_name);
445 p->pack_last_used = pack_used_ctr++;
450 struct packed_git *add_packed_git(char *path, int path_len)
453 struct packed_git *p;
454 unsigned long idx_size;
457 if (check_packed_git_idx(path, &idx_size, &idx_map))
460 /* do we have a corresponding .pack file? */
461 strcpy(path + path_len - 4, ".pack");
462 if (stat(path, &st) || !S_ISREG(st.st_mode)) {
463 munmap(idx_map, idx_size);
466 /* ok, it looks sane as far as we can check without
467 * actually mapping the pack file.
469 p = xmalloc(sizeof(*p) + path_len + 2);
470 strcpy(p->pack_name, path);
471 p->index_size = idx_size;
472 p->pack_size = st.st_size;
473 p->index_base = idx_map;
476 p->pack_last_used = 0;
481 struct packed_git *parse_pack_index(unsigned char *sha1)
483 char *path = sha1_pack_index_name(sha1);
484 return parse_pack_index_file(sha1, path);
487 struct packed_git *parse_pack_index_file(unsigned char *sha1, char *idx_path)
489 struct packed_git *p;
490 unsigned long idx_size;
494 if (check_packed_git_idx(idx_path, &idx_size, &idx_map))
497 path = sha1_pack_name(sha1);
499 p = xmalloc(sizeof(*p) + strlen(path) + 2);
500 strcpy(p->pack_name, path);
501 p->index_size = idx_size;
503 p->index_base = idx_map;
506 p->pack_last_used = 0;
508 memcpy(p->sha1, sha1, 20);
512 void install_packed_git(struct packed_git *pack)
514 pack->next = packed_git;
518 static void prepare_packed_git_one(char *objdir)
525 sprintf(path, "%s/pack", objdir);
531 while ((de = readdir(dir)) != NULL) {
532 int namelen = strlen(de->d_name);
533 struct packed_git *p;
535 if (strcmp(de->d_name + namelen - 4, ".idx"))
538 /* we have .idx. Is it a file we can map? */
539 strcpy(path + len, de->d_name);
540 p = add_packed_git(path, len + namelen);
543 p->next = packed_git;
549 void prepare_packed_git(void)
551 static int run_once = 0;
552 struct alternate_object_database *alt;
556 prepare_packed_git_one(get_object_directory());
558 for (alt = alt_odb_list; alt; alt = alt->next) {
560 prepare_packed_git_one(alt->base);
565 int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)
568 unsigned char real_sha1[20];
572 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
573 SHA1_Update(&c, map, size);
574 SHA1_Final(real_sha1, &c);
575 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
578 static void *map_sha1_file_internal(const unsigned char *sha1,
584 char *filename = find_sha1_file(sha1, &st);
590 fd = open(filename, O_RDONLY | sha1_file_open_flag);
592 /* See if it works without O_NOATIME */
593 switch (sha1_file_open_flag) {
595 fd = open(filename, O_RDONLY);
603 /* If it failed once, it will probably fail again.
604 * Stop using O_NOATIME
606 sha1_file_open_flag = 0;
608 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
610 if (map == MAP_FAILED)
616 int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size)
618 /* Get the data stream */
619 memset(stream, 0, sizeof(*stream));
620 stream->next_in = map;
621 stream->avail_in = mapsize;
622 stream->next_out = buffer;
623 stream->avail_out = size;
626 return inflate(stream, 0);
629 static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
631 int bytes = strlen(buffer) + 1;
632 unsigned char *buf = xmalloc(1+size);
634 memcpy(buf, buffer + bytes, stream->total_out - bytes);
635 bytes = stream->total_out - bytes;
637 stream->next_out = buf + bytes;
638 stream->avail_out = size - bytes;
639 while (inflate(stream, Z_FINISH) == Z_OK)
648 * We used to just use "sscanf()", but that's actually way
649 * too permissive for what we want to check. So do an anal
650 * object header parse by hand.
652 int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
658 * The type can be at most ten bytes (including the
659 * terminating '\0' that we add), and is followed by
674 * The length must follow immediately, and be in canonical
675 * decimal format (ie "010" is not valid).
682 unsigned long c = *hdr - '0';
686 size = size * 10 + c;
692 * The length must be followed by a zero byte
694 return *hdr ? -1 : 0;
697 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
703 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
704 if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0)
707 return unpack_sha1_rest(&stream, hdr, *size);
710 /* forward declaration for a mutually recursive function */
711 static int packed_object_info(struct pack_entry *entry,
712 char *type, unsigned long *sizep);
714 static int packed_delta_info(unsigned char *base_sha1,
715 unsigned long delta_size,
718 unsigned long *sizep,
719 struct packed_git *p)
721 struct pack_entry base_ent;
724 die("truncated pack file");
726 /* The base entry _must_ be in the same pack */
727 if (!find_pack_entry_one(base_sha1, &base_ent, p))
728 die("failed to find delta-pack base object %s",
729 sha1_to_hex(base_sha1));
731 /* We choose to only get the type of the base object and
732 * ignore potentially corrupt pack file that expects the delta
733 * based on a base with a wrong size. This saves tons of
737 if (packed_object_info(&base_ent, type, NULL))
738 die("cannot get info for delta-pack base");
741 const unsigned char *data;
742 unsigned char delta_head[64];
743 unsigned long result_size;
747 memset(&stream, 0, sizeof(stream));
749 data = stream.next_in = base_sha1 + 20;
750 stream.avail_in = left - 20;
751 stream.next_out = delta_head;
752 stream.avail_out = sizeof(delta_head);
754 inflateInit(&stream);
755 st = inflate(&stream, Z_FINISH);
757 if ((st != Z_STREAM_END) &&
758 stream.total_out != sizeof(delta_head))
759 die("delta data unpack-initial failed");
761 /* Examine the initial part of the delta to figure out
765 get_delta_hdr_size(&data); /* ignore base size */
767 /* Read the result size */
768 result_size = get_delta_hdr_size(&data);
769 *sizep = result_size;
774 static unsigned long unpack_object_header(struct packed_git *p, unsigned long offset,
775 enum object_type *type, unsigned long *sizep)
778 unsigned char *pack, c;
781 if (offset >= p->pack_size)
782 die("object offset outside of pack file");
784 pack = p->pack_base + offset;
787 *type = (c >> 4) & 7;
791 if (offset >= p->pack_size)
792 die("object offset outside of pack file");
795 size += (c & 0x7f) << shift;
802 void packed_object_info_detail(struct pack_entry *e,
805 unsigned long *store_size,
806 int *delta_chain_length,
807 unsigned char *base_sha1)
809 struct packed_git *p = e->p;
810 unsigned long offset, left;
812 enum object_type kind;
814 offset = unpack_object_header(p, e->offset, &kind, size);
815 pack = p->pack_base + offset;
816 left = p->pack_size - offset;
817 if (kind != OBJ_DELTA)
818 *delta_chain_length = 0;
820 int chain_length = 0;
821 memcpy(base_sha1, pack, 20);
823 struct pack_entry base_ent;
826 find_pack_entry_one(pack, &base_ent, p);
827 offset = unpack_object_header(p, base_ent.offset,
829 pack = p->pack_base + offset;
831 } while (kind == OBJ_DELTA);
832 *delta_chain_length = chain_length;
836 strcpy(type, "commit");
839 strcpy(type, "tree");
842 strcpy(type, "blob");
848 die("corrupted pack file");
850 *store_size = 0; /* notyet */
853 static int packed_object_info(struct pack_entry *entry,
854 char *type, unsigned long *sizep)
856 struct packed_git *p = entry->p;
857 unsigned long offset, size, left;
859 enum object_type kind;
862 if (use_packed_git(p))
863 die("cannot map packed file");
865 offset = unpack_object_header(p, entry->offset, &kind, &size);
866 pack = p->pack_base + offset;
867 left = p->pack_size - offset;
871 retval = packed_delta_info(pack, size, left, type, sizep, p);
875 strcpy(type, "commit");
878 strcpy(type, "tree");
881 strcpy(type, "blob");
887 die("corrupted pack file");
895 /* forward declaration for a mutually recursive function */
896 static void *unpack_entry(struct pack_entry *, char *, unsigned long *);
898 static void *unpack_delta_entry(unsigned char *base_sha1,
899 unsigned long delta_size,
902 unsigned long *sizep,
903 struct packed_git *p)
905 struct pack_entry base_ent;
906 void *data, *delta_data, *result, *base;
907 unsigned long data_size, result_size, base_size;
912 die("truncated pack file");
913 data = base_sha1 + 20;
914 data_size = left - 20;
915 delta_data = xmalloc(delta_size);
917 memset(&stream, 0, sizeof(stream));
919 stream.next_in = data;
920 stream.avail_in = data_size;
921 stream.next_out = delta_data;
922 stream.avail_out = delta_size;
924 inflateInit(&stream);
925 st = inflate(&stream, Z_FINISH);
927 if ((st != Z_STREAM_END) || stream.total_out != delta_size)
928 die("delta data unpack failed");
930 /* The base entry _must_ be in the same pack */
931 if (!find_pack_entry_one(base_sha1, &base_ent, p))
932 die("failed to find delta-pack base object %s",
933 sha1_to_hex(base_sha1));
934 base = unpack_entry_gently(&base_ent, type, &base_size);
936 die("failed to read delta-pack base object %s",
937 sha1_to_hex(base_sha1));
938 result = patch_delta(base, base_size,
939 delta_data, delta_size,
942 die("failed to apply delta");
945 *sizep = result_size;
949 static void *unpack_non_delta_entry(unsigned char *data,
955 unsigned char *buffer;
957 buffer = xmalloc(size + 1);
959 memset(&stream, 0, sizeof(stream));
960 stream.next_in = data;
961 stream.avail_in = left;
962 stream.next_out = buffer;
963 stream.avail_out = size;
965 inflateInit(&stream);
966 st = inflate(&stream, Z_FINISH);
968 if ((st != Z_STREAM_END) || stream.total_out != size) {
976 static void *unpack_entry(struct pack_entry *entry,
977 char *type, unsigned long *sizep)
979 struct packed_git *p = entry->p;
982 if (use_packed_git(p))
983 die("cannot map packed file");
984 retval = unpack_entry_gently(entry, type, sizep);
987 die("corrupted pack file");
991 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
992 void *unpack_entry_gently(struct pack_entry *entry,
993 char *type, unsigned long *sizep)
995 struct packed_git *p = entry->p;
996 unsigned long offset, size, left;
998 enum object_type kind;
1001 offset = unpack_object_header(p, entry->offset, &kind, &size);
1002 pack = p->pack_base + offset;
1003 left = p->pack_size - offset;
1006 retval = unpack_delta_entry(pack, size, left, type, sizep, p);
1009 strcpy(type, "commit");
1012 strcpy(type, "tree");
1015 strcpy(type, "blob");
1018 strcpy(type, "tag");
1024 retval = unpack_non_delta_entry(pack, size, left);
1028 int num_packed_objects(const struct packed_git *p)
1030 /* See check_packed_git_idx() */
1031 return (p->index_size - 20 - 20 - 4*256) / 24;
1034 int nth_packed_object_sha1(const struct packed_git *p, int n,
1035 unsigned char* sha1)
1037 void *index = p->index_base + 256;
1038 if (n < 0 || num_packed_objects(p) <= n)
1040 memcpy(sha1, (index + 24 * n + 4), 20);
1044 int find_pack_entry_one(const unsigned char *sha1,
1045 struct pack_entry *e, struct packed_git *p)
1047 unsigned int *level1_ofs = p->index_base;
1048 int hi = ntohl(level1_ofs[*sha1]);
1049 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
1050 void *index = p->index_base + 256;
1053 int mi = (lo + hi) / 2;
1054 int cmp = memcmp(index + 24 * mi + 4, sha1, 20);
1056 e->offset = ntohl(*((int*)(index + 24 * mi)));
1057 memcpy(e->sha1, sha1, 20);
1069 static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
1071 struct packed_git *p;
1072 prepare_packed_git();
1074 for (p = packed_git; p; p = p->next) {
1075 if (find_pack_entry_one(sha1, e, p))
1081 struct packed_git *find_sha1_pack(const unsigned char *sha1,
1082 struct packed_git *packs)
1084 struct packed_git *p;
1085 struct pack_entry e;
1087 for (p = packs; p; p = p->next) {
1088 if (find_pack_entry_one(sha1, &e, p))
1095 int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
1098 unsigned long mapsize, size;
1103 map = map_sha1_file_internal(sha1, &mapsize);
1105 struct pack_entry e;
1107 if (!find_pack_entry(sha1, &e))
1108 return error("unable to find %s", sha1_to_hex(sha1));
1109 return packed_object_info(&e, type, sizep);
1111 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1112 status = error("unable to unpack %s header",
1114 if (parse_sha1_header(hdr, type, &size) < 0)
1115 status = error("unable to parse %s header", sha1_to_hex(sha1));
1121 inflateEnd(&stream);
1122 munmap(map, mapsize);
1126 static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size)
1128 struct pack_entry e;
1130 if (!find_pack_entry(sha1, &e)) {
1131 error("cannot read sha1_file for %s", sha1_to_hex(sha1));
1134 return unpack_entry(&e, type, size);
1137 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
1139 unsigned long mapsize;
1141 struct pack_entry e;
1143 if (find_pack_entry(sha1, &e))
1144 return read_packed_sha1(sha1, type, size);
1145 map = map_sha1_file_internal(sha1, &mapsize);
1147 buf = unpack_sha1_file(map, mapsize, type, size);
1148 munmap(map, mapsize);
1154 void *read_object_with_reference(const unsigned char *sha1,
1155 const char *required_type,
1156 unsigned long *size,
1157 unsigned char *actual_sha1_return)
1161 unsigned long isize;
1162 unsigned char actual_sha1[20];
1164 memcpy(actual_sha1, sha1, 20);
1166 int ref_length = -1;
1167 const char *ref_type = NULL;
1169 buffer = read_sha1_file(actual_sha1, type, &isize);
1172 if (!strcmp(type, required_type)) {
1174 if (actual_sha1_return)
1175 memcpy(actual_sha1_return, actual_sha1, 20);
1178 /* Handle references */
1179 else if (!strcmp(type, "commit"))
1181 else if (!strcmp(type, "tag"))
1182 ref_type = "object ";
1187 ref_length = strlen(ref_type);
1189 if (memcmp(buffer, ref_type, ref_length) ||
1190 get_sha1_hex(buffer + ref_length, actual_sha1)) {
1195 /* Now we have the ID of the referred-to object in
1196 * actual_sha1. Check again. */
1200 char *write_sha1_file_prepare(void *buf,
1203 unsigned char *sha1,
1209 /* Generate the header */
1210 *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
1214 SHA1_Update(&c, hdr, *hdrlen);
1215 SHA1_Update(&c, buf, len);
1216 SHA1_Final(sha1, &c);
1218 return sha1_file_name(sha1);
1221 int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
1224 unsigned char *compressed;
1226 unsigned char sha1[20];
1228 static char tmpfile[PATH_MAX];
1229 unsigned char hdr[50];
1230 int fd, hdrlen, ret;
1232 /* Normally if we have it in the pack then we do not bother writing
1233 * it out into .git/objects/??/?{38} file.
1235 filename = write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
1237 memcpy(returnsha1, sha1, 20);
1238 if (has_sha1_file(sha1))
1240 fd = open(filename, O_RDONLY);
1243 * FIXME!!! We might do collision checking here, but we'd
1244 * need to uncompress the old file and check it. Later.
1250 if (errno != ENOENT) {
1251 fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno));
1255 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
1257 fd = mkstemp(tmpfile);
1259 fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno));
1264 memset(&stream, 0, sizeof(stream));
1265 deflateInit(&stream, Z_BEST_COMPRESSION);
1266 size = deflateBound(&stream, len+hdrlen);
1267 compressed = xmalloc(size);
1270 stream.next_out = compressed;
1271 stream.avail_out = size;
1273 /* First header.. */
1274 stream.next_in = hdr;
1275 stream.avail_in = hdrlen;
1276 while (deflate(&stream, 0) == Z_OK)
1279 /* Then the data itself.. */
1280 stream.next_in = buf;
1281 stream.avail_in = len;
1282 while (deflate(&stream, Z_FINISH) == Z_OK)
1284 deflateEnd(&stream);
1285 size = stream.total_out;
1287 if (write(fd, compressed, size) != size)
1288 die("unable to write file");
1293 ret = link(tmpfile, filename);
1298 * Coda hack - coda doesn't like cross-directory links,
1299 * so we fall back to a rename, which will mean that it
1300 * won't be able to check collisions, but that's not a
1303 * When this succeeds, we just return 0. We have nothing
1306 if (ret == EXDEV && !rename(tmpfile, filename))
1311 if (ret != EEXIST) {
1312 fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
1315 /* FIXME!!! Collision check here ? */
1321 int write_sha1_to_fd(int fd, const unsigned char *sha1)
1324 unsigned long objsize;
1326 void *map = map_sha1_file_internal(sha1, &objsize);
1328 void *temp_obj = NULL;
1332 unsigned char *unpacked;
1337 // need to unpack and recompress it by itself
1338 unpacked = read_packed_sha1(sha1, type, &len);
1340 hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
1343 memset(&stream, 0, sizeof(stream));
1344 deflateInit(&stream, Z_BEST_COMPRESSION);
1345 size = deflateBound(&stream, len + hdrlen);
1346 temp_obj = buf = xmalloc(size);
1349 stream.next_out = buf;
1350 stream.avail_out = size;
1352 /* First header.. */
1353 stream.next_in = (void *)hdr;
1354 stream.avail_in = hdrlen;
1355 while (deflate(&stream, 0) == Z_OK)
1358 /* Then the data itself.. */
1359 stream.next_in = unpacked;
1360 stream.avail_in = len;
1361 while (deflate(&stream, Z_FINISH) == Z_OK)
1363 deflateEnd(&stream);
1366 objsize = stream.total_out;
1370 size = write(fd, buf + posn, objsize - posn);
1373 fprintf(stderr, "write closed");
1380 } while (posn < objsize);
1383 munmap(map, objsize);
1390 int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
1391 size_t bufsize, size_t *bufposn)
1393 char *filename = sha1_file_name(sha1);
1397 unsigned char real_sha1[20];
1398 unsigned char discard[4096];
1402 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
1405 return error("Couldn't open %s\n", filename);
1407 memset(&stream, 0, sizeof(stream));
1409 inflateInit(&stream);
1416 stream.avail_in = *bufposn;
1417 stream.next_in = (unsigned char *) buffer;
1419 stream.next_out = discard;
1420 stream.avail_out = sizeof(discard);
1421 ret = inflate(&stream, Z_SYNC_FLUSH);
1422 SHA1_Update(&c, discard, sizeof(discard) -
1424 } while (stream.avail_in && ret == Z_OK);
1425 write(local, buffer, *bufposn - stream.avail_in);
1426 memmove(buffer, buffer + *bufposn - stream.avail_in,
1428 *bufposn = stream.avail_in;
1432 size = read(fd, buffer + *bufposn, bufsize - *bufposn);
1437 return error("Connection closed?");
1438 perror("Reading from connection");
1443 inflateEnd(&stream);
1446 SHA1_Final(real_sha1, &c);
1447 if (ret != Z_STREAM_END) {
1449 return error("File %s corrupted", sha1_to_hex(sha1));
1451 if (memcmp(sha1, real_sha1, 20)) {
1453 return error("File %s has bad hash\n", sha1_to_hex(sha1));
1459 int has_pack_index(const unsigned char *sha1)
1462 if (stat(sha1_pack_index_name(sha1), &st))
1467 int has_pack_file(const unsigned char *sha1)
1470 if (stat(sha1_pack_name(sha1), &st))
1475 int has_sha1_pack(const unsigned char *sha1)
1477 struct pack_entry e;
1478 return find_pack_entry(sha1, &e);
1481 int has_sha1_file(const unsigned char *sha1)
1484 struct pack_entry e;
1486 if (find_pack_entry(sha1, &e))
1488 return find_sha1_file(sha1, &st) ? 1 : 0;
1491 int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type)
1493 unsigned long size = st->st_size;
1496 unsigned char hdr[50];
1501 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1503 if (buf == MAP_FAILED)
1509 ret = write_sha1_file(buf, size, type, sha1);
1511 write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);