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 int get_sha1_file(const char *path, unsigned char *result)
52 int fd = open(path, O_RDONLY);
57 len = read(fd, buffer, sizeof(buffer));
61 return get_sha1_hex(buffer, result);
64 static char *git_dir, *git_object_dir, *git_index_file, *git_refs_dir;
65 static void setup_git_env(void)
67 git_dir = gitenv(GIT_DIR_ENVIRONMENT);
69 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
70 git_object_dir = gitenv(DB_ENVIRONMENT);
71 if (!git_object_dir) {
72 git_object_dir = xmalloc(strlen(git_dir) + 9);
73 sprintf(git_object_dir, "%s/objects", git_dir);
75 git_refs_dir = xmalloc(strlen(git_dir) + 6);
76 sprintf(git_refs_dir, "%s/refs", git_dir);
77 git_index_file = gitenv(INDEX_ENVIRONMENT);
78 if (!git_index_file) {
79 git_index_file = xmalloc(strlen(git_dir) + 7);
80 sprintf(git_index_file, "%s/index", git_dir);
84 char *get_object_directory(void)
88 return git_object_dir;
91 char *get_refs_directory(void)
98 char *get_index_file(void)
102 return git_index_file;
105 char *git_path(const char *fmt, ...)
107 static char pathname[PATH_MAX], *ret;
113 len = strlen(git_dir);
114 if (len > PATH_MAX-100)
116 memcpy(pathname, git_dir, len);
117 if (len && git_dir[len-1] != '/')
118 pathname[len++] = '/';
120 vsnprintf(pathname + len, sizeof(pathname) - len, fmt, args);
125 if (!memcmp(pathname, "./", 2)) {
133 int get_sha1(const char *str, unsigned char *sha1)
135 static const char *prefix[] = {
145 if (!get_sha1_hex(str, sha1))
148 for (p = prefix; *p; p++) {
149 char * pathname = git_path("%s/%s", *p, str);
150 if (!get_sha1_file(pathname, sha1))
157 char * sha1_to_hex(const unsigned char *sha1)
159 static char buffer[50];
160 static const char hex[] = "0123456789abcdef";
164 for (i = 0; i < 20; i++) {
165 unsigned int val = *sha1++;
166 *buf++ = hex[val >> 4];
167 *buf++ = hex[val & 0xf];
172 static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
175 for (i = 0; i < 20; i++) {
176 static char hex[] = "0123456789abcdef";
177 unsigned int val = sha1[i];
178 char *pos = pathbuf + i*2 + (i > 0);
179 *pos++ = hex[val >> 4];
180 *pos = hex[val & 0xf];
185 * NOTE! This returns a statically allocated buffer, so you have to be
186 * careful about using it. Do a "strdup()" if you need to save the
189 * Also note that this returns the location for creating. Reading
190 * SHA1 file can happen from any alternate directory listed in the
191 * DB_ENVIRONMENT environment variable if it is not found in
192 * the primary object database.
194 char *sha1_file_name(const unsigned char *sha1)
196 static char *name, *base;
199 const char *sha1_file_directory = get_object_directory();
200 int len = strlen(sha1_file_directory);
201 base = xmalloc(len + 60);
202 memcpy(base, sha1_file_directory, len);
203 memset(base+len, 0, 60);
206 name = base + len + 1;
208 fill_sha1_path(name, sha1);
212 struct alternate_object_database *alt_odb;
215 * Prepare alternate object database registry.
216 * alt_odb points at an array of struct alternate_object_database.
217 * This array is terminated with an element that has both its base
218 * and name set to NULL. alt_odb[n] comes from n'th non-empty
219 * element from colon separated ALTERNATE_DB_ENVIRONMENT environment
220 * variable, and its base points at a statically allocated buffer
221 * that contains "/the/directory/corresponding/to/.git/objects/...",
222 * while its name points just after the slash at the end of
223 * ".git/objects/" in the example above, and has enough space to hold
224 * 40-byte hex SHA1, an extra slash for the first level indirection,
225 * and the terminating NUL.
226 * This function allocates the alt_odb array and all the strings
227 * pointed by base fields of the array elements with one xmalloc();
228 * the string pool immediately follows the array.
230 void prepare_alt_odb(void)
233 const char *cp, *last;
235 const char *alt = gitenv(ALTERNATE_DB_ENVIRONMENT) ? : "";
239 /* The first pass counts how large an area to allocate to
240 * hold the entire alt_odb structure, including array of
241 * structs and path buffers for them. The second pass fills
242 * the structure and prepares the path buffers for use by
245 for (totlen = pass = 0; pass < 2; pass++) {
249 cp = strchr(last, ':') ? : last + strlen(last);
251 /* 43 = 40-byte + 2 '/' + terminating NUL */
252 int pfxlen = cp - last;
253 int entlen = pfxlen + 43;
257 alt_odb[i].base = op;
258 alt_odb[i].name = op + pfxlen + 1;
259 memcpy(op, last, pfxlen);
260 op[pfxlen] = op[pfxlen + 3] = '/';
266 while (*cp && *cp == ':')
272 alt_odb = xmalloc(sizeof(*alt_odb) * (i + 1) + totlen);
273 alt_odb[i].base = alt_odb[i].name = NULL;
274 op = (char*)(&alt_odb[i+1]);
278 static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
281 char *name = sha1_file_name(sha1);
286 for (i = 0; (name = alt_odb[i].name) != NULL; i++) {
287 fill_sha1_path(name, sha1);
288 if (!stat(alt_odb[i].base, st))
289 return alt_odb[i].base;
294 #define PACK_MAX_SZ (1<<26)
295 static int pack_used_ctr;
296 static unsigned long pack_mapped;
297 struct packed_git *packed_git;
299 static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
304 unsigned long idx_size;
306 int fd = open(path, O_RDONLY);
310 if (fstat(fd, &st)) {
314 idx_size = st.st_size;
315 idx_map = mmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
317 if (idx_map == MAP_FAILED)
322 *idx_size_ = idx_size;
324 /* check index map */
325 if (idx_size < 4*256 + 20 + 20)
326 return error("index file too small");
328 for (i = 0; i < 256; i++) {
329 unsigned int n = ntohl(index[i]);
331 return error("non-monotonic index");
337 * - 256 index entries 4 bytes each
338 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
339 * - 20-byte SHA1 of the packfile
340 * - 20-byte SHA1 file checksum
342 if (idx_size != 4*256 + nr * 24 + 20 + 20)
343 return error("wrong index file size");
348 static int unuse_one_packed_git(void)
350 struct packed_git *p, *lru = NULL;
352 for (p = packed_git; p; p = p->next) {
353 if (p->pack_use_cnt || !p->pack_base)
355 if (!lru || p->pack_last_used < lru->pack_last_used)
360 munmap(lru->pack_base, lru->pack_size);
361 lru->pack_base = NULL;
365 void unuse_packed_git(struct packed_git *p)
370 int use_packed_git(struct packed_git *p)
377 pack_mapped += p->pack_size;
378 while (PACK_MAX_SZ < pack_mapped && unuse_one_packed_git())
380 fd = open(p->pack_name, O_RDONLY);
382 die("packfile %s cannot be opened", p->pack_name);
383 if (fstat(fd, &st)) {
385 die("packfile %s cannot be opened", p->pack_name);
387 if (st.st_size != p->pack_size)
388 die("packfile %s size mismatch.", p->pack_name);
389 map = mmap(NULL, p->pack_size, PROT_READ, MAP_PRIVATE, fd, 0);
391 if (map == MAP_FAILED)
392 die("packfile %s cannot be mapped.", p->pack_name);
395 /* Check if the pack file matches with the index file.
398 if (memcmp((char*)(p->index_base) + p->index_size - 40,
399 p->pack_base + p->pack_size - 20, 20))
400 die("packfile %s does not match index.", p->pack_name);
402 p->pack_last_used = pack_used_ctr++;
407 struct packed_git *add_packed_git(char *path, int path_len)
410 struct packed_git *p;
411 unsigned long idx_size;
414 if (check_packed_git_idx(path, &idx_size, &idx_map))
417 /* do we have a corresponding .pack file? */
418 strcpy(path + path_len - 4, ".pack");
419 if (stat(path, &st) || !S_ISREG(st.st_mode)) {
420 munmap(idx_map, idx_size);
423 /* ok, it looks sane as far as we can check without
424 * actually mapping the pack file.
426 p = xmalloc(sizeof(*p) + path_len + 2);
427 strcpy(p->pack_name, path);
428 p->index_size = idx_size;
429 p->pack_size = st.st_size;
430 p->index_base = idx_map;
433 p->pack_last_used = 0;
438 static void prepare_packed_git_one(char *objdir)
445 sprintf(path, "%s/pack", objdir);
451 while ((de = readdir(dir)) != NULL) {
452 int namelen = strlen(de->d_name);
453 struct packed_git *p;
455 if (strcmp(de->d_name + namelen - 4, ".idx"))
458 /* we have .idx. Is it a file we can map? */
459 strcpy(path + len, de->d_name);
460 p = add_packed_git(path, len + namelen);
463 p->next = packed_git;
469 void prepare_packed_git(void)
472 static int run_once = 0;
477 prepare_packed_git_one(get_object_directory());
479 for (i = 0; alt_odb[i].base != NULL; i++) {
480 alt_odb[i].name[0] = 0;
481 prepare_packed_git_one(alt_odb[i].base);
485 int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)
488 unsigned char real_sha1[20];
492 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
493 SHA1_Update(&c, map, size);
494 SHA1_Final(real_sha1, &c);
495 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
498 static void *map_sha1_file_internal(const unsigned char *sha1,
505 char *filename = find_sha1_file(sha1, &st);
509 error("cannot map sha1 file %s", sha1_to_hex(sha1));
513 fd = open(filename, O_RDONLY | sha1_file_open_flag);
515 /* See if it works without O_NOATIME */
516 switch (sha1_file_open_flag) {
518 fd = open(filename, O_RDONLY);
528 /* If it failed once, it will probably fail again.
529 * Stop using O_NOATIME
531 sha1_file_open_flag = 0;
533 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
535 if (-1 == (int)(long)map)
541 void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
543 return map_sha1_file_internal(sha1, size, 1);
546 int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size)
548 /* Get the data stream */
549 memset(stream, 0, sizeof(*stream));
550 stream->next_in = map;
551 stream->avail_in = mapsize;
552 stream->next_out = buffer;
553 stream->avail_out = size;
556 return inflate(stream, 0);
559 static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
561 int bytes = strlen(buffer) + 1;
562 unsigned char *buf = xmalloc(1+size);
564 memcpy(buf, buffer + bytes, stream->total_out - bytes);
565 bytes = stream->total_out - bytes;
567 stream->next_out = buf + bytes;
568 stream->avail_out = size - bytes;
569 while (inflate(stream, Z_FINISH) == Z_OK)
578 * We used to just use "sscanf()", but that's actually way
579 * too permissive for what we want to check. So do an anal
580 * object header parse by hand.
582 int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
588 * The type can be at most ten bytes (including the
589 * terminating '\0' that we add), and is followed by
604 * The length must follow immediately, and be in canonical
605 * decimal format (ie "010" is not valid).
612 unsigned long c = *hdr - '0';
616 size = size * 10 + c;
622 * The length must be followed by a zero byte
624 return *hdr ? -1 : 0;
627 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
633 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
634 if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0)
637 return unpack_sha1_rest(&stream, hdr, *size);
640 /* forward declaration for a mutually recursive function */
641 static int packed_object_info(struct pack_entry *entry,
642 char *type, unsigned long *sizep);
644 static int packed_delta_info(unsigned char *base_sha1,
645 unsigned long delta_size,
648 unsigned long *sizep,
649 struct packed_git *p)
651 struct pack_entry base_ent;
654 die("truncated pack file");
656 /* The base entry _must_ be in the same pack */
657 if (!find_pack_entry_one(base_sha1, &base_ent, p))
658 die("failed to find delta-pack base object %s",
659 sha1_to_hex(base_sha1));
661 /* We choose to only get the type of the base object and
662 * ignore potentially corrupt pack file that expects the delta
663 * based on a base with a wrong size. This saves tons of
667 if (packed_object_info(&base_ent, type, NULL))
668 die("cannot get info for delta-pack base");
671 const unsigned char *data;
672 unsigned char delta_head[64];
673 unsigned long result_size;
677 memset(&stream, 0, sizeof(stream));
679 data = stream.next_in = base_sha1 + 20;
680 stream.avail_in = left - 20;
681 stream.next_out = delta_head;
682 stream.avail_out = sizeof(delta_head);
684 inflateInit(&stream);
685 st = inflate(&stream, Z_FINISH);
687 if ((st != Z_STREAM_END) &&
688 stream.total_out != sizeof(delta_head))
689 die("delta data unpack-initial failed");
691 /* Examine the initial part of the delta to figure out
695 get_delta_hdr_size(&data); /* ignore base size */
697 /* Read the result size */
698 result_size = get_delta_hdr_size(&data);
699 *sizep = result_size;
704 static unsigned long unpack_object_header(struct packed_git *p, unsigned long offset,
705 enum object_type *type, unsigned long *sizep)
708 unsigned char *pack, c;
711 if (offset >= p->pack_size)
712 die("object offset outside of pack file");
714 pack = p->pack_base + offset;
717 *type = (c >> 4) & 7;
721 if (offset >= p->pack_size)
722 die("object offset outside of pack file");
725 size += (c & 0x7f) << shift;
732 void packed_object_info_detail(struct pack_entry *e,
735 unsigned long *store_size,
736 int *delta_chain_length,
737 unsigned char *base_sha1)
739 struct packed_git *p = e->p;
740 unsigned long offset, left;
742 enum object_type kind;
744 offset = unpack_object_header(p, e->offset, &kind, size);
745 pack = p->pack_base + offset;
746 left = p->pack_size - offset;
747 if (kind != OBJ_DELTA)
748 *delta_chain_length = 0;
750 int chain_length = 0;
751 memcpy(base_sha1, pack, 20);
753 struct pack_entry base_ent;
756 find_pack_entry_one(pack, &base_ent, p);
757 offset = unpack_object_header(p, base_ent.offset,
759 pack = p->pack_base + offset;
761 } while (kind == OBJ_DELTA);
762 *delta_chain_length = chain_length;
766 strcpy(type, "commit");
769 strcpy(type, "tree");
772 strcpy(type, "blob");
778 die("corrupted pack file");
780 *store_size = 0; /* notyet */
783 static int packed_object_info(struct pack_entry *entry,
784 char *type, unsigned long *sizep)
786 struct packed_git *p = entry->p;
787 unsigned long offset, size, left;
789 enum object_type kind;
792 if (use_packed_git(p))
793 die("cannot map packed file");
795 offset = unpack_object_header(p, entry->offset, &kind, &size);
796 pack = p->pack_base + offset;
797 left = p->pack_size - offset;
801 retval = packed_delta_info(pack, size, left, type, sizep, p);
805 strcpy(type, "commit");
808 strcpy(type, "tree");
811 strcpy(type, "blob");
817 die("corrupted pack file");
825 /* forward declaration for a mutually recursive function */
826 static void *unpack_entry(struct pack_entry *, char *, unsigned long *);
828 static void *unpack_delta_entry(unsigned char *base_sha1,
829 unsigned long delta_size,
832 unsigned long *sizep,
833 struct packed_git *p)
835 struct pack_entry base_ent;
836 void *data, *delta_data, *result, *base;
837 unsigned long data_size, result_size, base_size;
842 die("truncated pack file");
843 data = base_sha1 + 20;
844 data_size = left - 20;
845 delta_data = xmalloc(delta_size);
847 memset(&stream, 0, sizeof(stream));
849 stream.next_in = data;
850 stream.avail_in = data_size;
851 stream.next_out = delta_data;
852 stream.avail_out = delta_size;
854 inflateInit(&stream);
855 st = inflate(&stream, Z_FINISH);
857 if ((st != Z_STREAM_END) || stream.total_out != delta_size)
858 die("delta data unpack failed");
860 /* The base entry _must_ be in the same pack */
861 if (!find_pack_entry_one(base_sha1, &base_ent, p))
862 die("failed to find delta-pack base object %s",
863 sha1_to_hex(base_sha1));
864 base = unpack_entry_gently(&base_ent, type, &base_size);
866 die("failed to read delta-pack base object %s",
867 sha1_to_hex(base_sha1));
868 result = patch_delta(base, base_size,
869 delta_data, delta_size,
872 die("failed to apply delta");
875 *sizep = result_size;
879 static void *unpack_non_delta_entry(unsigned char *data,
885 unsigned char *buffer;
887 buffer = xmalloc(size + 1);
889 memset(&stream, 0, sizeof(stream));
890 stream.next_in = data;
891 stream.avail_in = left;
892 stream.next_out = buffer;
893 stream.avail_out = size;
895 inflateInit(&stream);
896 st = inflate(&stream, Z_FINISH);
898 if ((st != Z_STREAM_END) || stream.total_out != size) {
906 static void *unpack_entry(struct pack_entry *entry,
907 char *type, unsigned long *sizep)
909 struct packed_git *p = entry->p;
912 if (use_packed_git(p))
913 die("cannot map packed file");
914 retval = unpack_entry_gently(entry, type, sizep);
917 die("corrupted pack file");
921 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
922 void *unpack_entry_gently(struct pack_entry *entry,
923 char *type, unsigned long *sizep)
925 struct packed_git *p = entry->p;
926 unsigned long offset, size, left;
928 enum object_type kind;
931 offset = unpack_object_header(p, entry->offset, &kind, &size);
932 pack = p->pack_base + offset;
933 left = p->pack_size - offset;
936 retval = unpack_delta_entry(pack, size, left, type, sizep, p);
939 strcpy(type, "commit");
942 strcpy(type, "tree");
945 strcpy(type, "blob");
954 retval = unpack_non_delta_entry(pack, size, left);
958 int num_packed_objects(const struct packed_git *p)
960 /* See check_packed_git_idx() */
961 return (p->index_size - 20 - 20 - 4*256) / 24;
964 int nth_packed_object_sha1(const struct packed_git *p, int n,
967 void *index = p->index_base + 256;
968 if (n < 0 || num_packed_objects(p) <= n)
970 memcpy(sha1, (index + 24 * n + 4), 20);
974 int find_pack_entry_one(const unsigned char *sha1,
975 struct pack_entry *e, struct packed_git *p)
977 unsigned int *level1_ofs = p->index_base;
978 int hi = ntohl(level1_ofs[*sha1]);
979 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
980 void *index = p->index_base + 256;
983 int mi = (lo + hi) / 2;
984 int cmp = memcmp(index + 24 * mi + 4, sha1, 20);
986 e->offset = ntohl(*((int*)(index + 24 * mi)));
987 memcpy(e->sha1, sha1, 20);
999 static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
1001 struct packed_git *p;
1002 prepare_packed_git();
1004 for (p = packed_git; p; p = p->next) {
1005 if (find_pack_entry_one(sha1, e, p))
1011 int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
1014 unsigned long mapsize, size;
1019 map = map_sha1_file_internal(sha1, &mapsize, 0);
1021 struct pack_entry e;
1023 if (!find_pack_entry(sha1, &e))
1024 return error("unable to find %s", sha1_to_hex(sha1));
1025 return packed_object_info(&e, type, sizep);
1027 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1028 status = error("unable to unpack %s header",
1030 if (parse_sha1_header(hdr, type, &size) < 0)
1031 status = error("unable to parse %s header", sha1_to_hex(sha1));
1037 inflateEnd(&stream);
1038 munmap(map, mapsize);
1042 static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size)
1044 struct pack_entry e;
1046 if (!find_pack_entry(sha1, &e)) {
1047 error("cannot read sha1_file for %s", sha1_to_hex(sha1));
1050 return unpack_entry(&e, type, size);
1053 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
1055 unsigned long mapsize;
1058 map = map_sha1_file_internal(sha1, &mapsize, 0);
1060 buf = unpack_sha1_file(map, mapsize, type, size);
1061 munmap(map, mapsize);
1064 return read_packed_sha1(sha1, type, size);
1067 void *read_object_with_reference(const unsigned char *sha1,
1068 const char *required_type,
1069 unsigned long *size,
1070 unsigned char *actual_sha1_return)
1074 unsigned long isize;
1075 unsigned char actual_sha1[20];
1077 memcpy(actual_sha1, sha1, 20);
1079 int ref_length = -1;
1080 const char *ref_type = NULL;
1082 buffer = read_sha1_file(actual_sha1, type, &isize);
1085 if (!strcmp(type, required_type)) {
1087 if (actual_sha1_return)
1088 memcpy(actual_sha1_return, actual_sha1, 20);
1091 /* Handle references */
1092 else if (!strcmp(type, "commit"))
1094 else if (!strcmp(type, "tag"))
1095 ref_type = "object ";
1100 ref_length = strlen(ref_type);
1102 if (memcmp(buffer, ref_type, ref_length) ||
1103 get_sha1_hex(buffer + ref_length, actual_sha1)) {
1107 /* Now we have the ID of the referred-to object in
1108 * actual_sha1. Check again. */
1112 static char *write_sha1_file_prepare(void *buf,
1115 unsigned char *sha1,
1121 /* Generate the header */
1122 *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
1126 SHA1_Update(&c, hdr, *hdrlen);
1127 SHA1_Update(&c, buf, len);
1128 SHA1_Final(sha1, &c);
1130 return sha1_file_name(sha1);
1133 int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
1136 unsigned char *compressed;
1138 unsigned char sha1[20];
1140 static char tmpfile[PATH_MAX];
1141 unsigned char hdr[50];
1142 int fd, hdrlen, ret;
1144 /* Normally if we have it in the pack then we do not bother writing
1145 * it out into .git/objects/??/?{38} file.
1147 filename = write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
1149 memcpy(returnsha1, sha1, 20);
1150 if (has_sha1_file(sha1))
1152 fd = open(filename, O_RDONLY);
1155 * FIXME!!! We might do collision checking here, but we'd
1156 * need to uncompress the old file and check it. Later.
1162 if (errno != ENOENT) {
1163 fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno));
1167 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
1169 fd = mkstemp(tmpfile);
1171 fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno));
1176 memset(&stream, 0, sizeof(stream));
1177 deflateInit(&stream, Z_BEST_COMPRESSION);
1178 size = deflateBound(&stream, len+hdrlen);
1179 compressed = xmalloc(size);
1182 stream.next_out = compressed;
1183 stream.avail_out = size;
1185 /* First header.. */
1186 stream.next_in = hdr;
1187 stream.avail_in = hdrlen;
1188 while (deflate(&stream, 0) == Z_OK)
1191 /* Then the data itself.. */
1192 stream.next_in = buf;
1193 stream.avail_in = len;
1194 while (deflate(&stream, Z_FINISH) == Z_OK)
1196 deflateEnd(&stream);
1197 size = stream.total_out;
1199 if (write(fd, compressed, size) != size)
1200 die("unable to write file");
1205 ret = link(tmpfile, filename);
1210 * Coda hack - coda doesn't like cross-directory links,
1211 * so we fall back to a rename, which will mean that it
1212 * won't be able to check collisions, but that's not a
1215 * When this succeeds, we just return 0. We have nothing
1218 if (ret == EXDEV && !rename(tmpfile, filename))
1223 if (ret != EEXIST) {
1224 fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
1227 /* FIXME!!! Collision check here ? */
1233 int write_sha1_from_fd(const unsigned char *sha1, int fd)
1235 char *filename = sha1_file_name(sha1);
1239 unsigned char real_sha1[20];
1240 unsigned char buf[4096];
1241 unsigned char discard[4096];
1245 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
1248 return error("Couldn't open %s\n", filename);
1250 memset(&stream, 0, sizeof(stream));
1252 inflateInit(&stream);
1258 size = read(fd, buf, 4096);
1263 return error("Connection closed?");
1264 perror("Reading from connection");
1267 write(local, buf, size);
1268 stream.avail_in = size;
1269 stream.next_in = buf;
1271 stream.next_out = discard;
1272 stream.avail_out = sizeof(discard);
1273 ret = inflate(&stream, Z_SYNC_FLUSH);
1274 SHA1_Update(&c, discard, sizeof(discard) -
1276 } while (stream.avail_in && ret == Z_OK);
1278 } while (ret == Z_OK);
1279 inflateEnd(&stream);
1282 SHA1_Final(real_sha1, &c);
1283 if (ret != Z_STREAM_END) {
1285 return error("File %s corrupted", sha1_to_hex(sha1));
1287 if (memcmp(sha1, real_sha1, 20)) {
1289 return error("File %s has bad hash\n", sha1_to_hex(sha1));
1295 int has_sha1_pack(const unsigned char *sha1)
1297 struct pack_entry e;
1298 return find_pack_entry(sha1, &e);
1301 int has_sha1_file(const unsigned char *sha1)
1304 struct pack_entry e;
1306 if (find_sha1_file(sha1, &st))
1308 return find_pack_entry(sha1, &e);
1311 int index_fd(unsigned char *sha1, int fd, struct stat *st)
1313 unsigned long size = st->st_size;
1319 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1321 if ((int)(long)buf == -1)
1324 ret = write_sha1_file(buf, size, "blob", sha1);