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 int safe_create_leading_directories(char *path)
110 pos = strchr(pos, '/');
114 if (mkdir(path, 0777) < 0)
115 if (errno != EEXIST) {
124 int get_sha1(const char *str, unsigned char *sha1)
126 static const char *prefix[] = {
136 if (!get_sha1_hex(str, sha1))
139 for (p = prefix; *p; p++) {
140 char * pathname = git_path("%s/%s", *p, str);
141 if (!get_sha1_file(pathname, sha1))
148 char * sha1_to_hex(const unsigned char *sha1)
150 static char buffer[50];
151 static const char hex[] = "0123456789abcdef";
155 for (i = 0; i < 20; i++) {
156 unsigned int val = *sha1++;
157 *buf++ = hex[val >> 4];
158 *buf++ = hex[val & 0xf];
163 static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
166 for (i = 0; i < 20; i++) {
167 static char hex[] = "0123456789abcdef";
168 unsigned int val = sha1[i];
169 char *pos = pathbuf + i*2 + (i > 0);
170 *pos++ = hex[val >> 4];
171 *pos = hex[val & 0xf];
176 * NOTE! This returns a statically allocated buffer, so you have to be
177 * careful about using it. Do a "strdup()" if you need to save the
180 * Also note that this returns the location for creating. Reading
181 * SHA1 file can happen from any alternate directory listed in the
182 * DB_ENVIRONMENT environment variable if it is not found in
183 * the primary object database.
185 char *sha1_file_name(const unsigned char *sha1)
187 static char *name, *base;
190 const char *sha1_file_directory = get_object_directory();
191 int len = strlen(sha1_file_directory);
192 base = xmalloc(len + 60);
193 memcpy(base, sha1_file_directory, len);
194 memset(base+len, 0, 60);
197 name = base + len + 1;
199 fill_sha1_path(name, sha1);
203 struct alternate_object_database *alt_odb;
206 * Prepare alternate object database registry.
207 * alt_odb points at an array of struct alternate_object_database.
208 * This array is terminated with an element that has both its base
209 * and name set to NULL. alt_odb[n] comes from n'th non-empty
210 * element from colon separated ALTERNATE_DB_ENVIRONMENT environment
211 * variable, and its base points at a statically allocated buffer
212 * that contains "/the/directory/corresponding/to/.git/objects/...",
213 * while its name points just after the slash at the end of
214 * ".git/objects/" in the example above, and has enough space to hold
215 * 40-byte hex SHA1, an extra slash for the first level indirection,
216 * and the terminating NUL.
217 * This function allocates the alt_odb array and all the strings
218 * pointed by base fields of the array elements with one xmalloc();
219 * the string pool immediately follows the array.
221 void prepare_alt_odb(void)
224 const char *cp, *last;
226 const char *alt = gitenv(ALTERNATE_DB_ENVIRONMENT) ? : "";
230 /* The first pass counts how large an area to allocate to
231 * hold the entire alt_odb structure, including array of
232 * structs and path buffers for them. The second pass fills
233 * the structure and prepares the path buffers for use by
236 for (totlen = pass = 0; pass < 2; pass++) {
240 cp = strchr(last, ':') ? : last + strlen(last);
242 /* 43 = 40-byte + 2 '/' + terminating NUL */
243 int pfxlen = cp - last;
244 int entlen = pfxlen + 43;
248 alt_odb[i].base = op;
249 alt_odb[i].name = op + pfxlen + 1;
250 memcpy(op, last, pfxlen);
251 op[pfxlen] = op[pfxlen + 3] = '/';
257 while (*cp && *cp == ':')
263 alt_odb = xmalloc(sizeof(*alt_odb) * (i + 1) + totlen);
264 alt_odb[i].base = alt_odb[i].name = NULL;
265 op = (char*)(&alt_odb[i+1]);
269 static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
272 char *name = sha1_file_name(sha1);
277 for (i = 0; (name = alt_odb[i].name) != NULL; i++) {
278 fill_sha1_path(name, sha1);
279 if (!stat(alt_odb[i].base, st))
280 return alt_odb[i].base;
285 #define PACK_MAX_SZ (1<<26)
286 static int pack_used_ctr;
287 static unsigned long pack_mapped;
288 struct packed_git *packed_git;
290 static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
295 unsigned long idx_size;
297 int fd = open(path, O_RDONLY);
301 if (fstat(fd, &st)) {
305 idx_size = st.st_size;
306 idx_map = mmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
308 if (idx_map == MAP_FAILED)
313 *idx_size_ = idx_size;
315 /* check index map */
316 if (idx_size < 4*256 + 20 + 20)
317 return error("index file too small");
319 for (i = 0; i < 256; i++) {
320 unsigned int n = ntohl(index[i]);
322 return error("non-monotonic index");
328 * - 256 index entries 4 bytes each
329 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
330 * - 20-byte SHA1 of the packfile
331 * - 20-byte SHA1 file checksum
333 if (idx_size != 4*256 + nr * 24 + 20 + 20)
334 return error("wrong index file size");
339 static int unuse_one_packed_git(void)
341 struct packed_git *p, *lru = NULL;
343 for (p = packed_git; p; p = p->next) {
344 if (p->pack_use_cnt || !p->pack_base)
346 if (!lru || p->pack_last_used < lru->pack_last_used)
351 munmap(lru->pack_base, lru->pack_size);
352 lru->pack_base = NULL;
356 void unuse_packed_git(struct packed_git *p)
361 int use_packed_git(struct packed_git *p)
368 pack_mapped += p->pack_size;
369 while (PACK_MAX_SZ < pack_mapped && unuse_one_packed_git())
371 fd = open(p->pack_name, O_RDONLY);
373 die("packfile %s cannot be opened", p->pack_name);
374 if (fstat(fd, &st)) {
376 die("packfile %s cannot be opened", p->pack_name);
378 if (st.st_size != p->pack_size)
379 die("packfile %s size mismatch.", p->pack_name);
380 map = mmap(NULL, p->pack_size, PROT_READ, MAP_PRIVATE, fd, 0);
382 if (map == MAP_FAILED)
383 die("packfile %s cannot be mapped.", p->pack_name);
386 /* Check if the pack file matches with the index file.
389 if (memcmp((char*)(p->index_base) + p->index_size - 40,
390 p->pack_base + p->pack_size - 20, 20))
391 die("packfile %s does not match index.", p->pack_name);
393 p->pack_last_used = pack_used_ctr++;
398 struct packed_git *add_packed_git(char *path, int path_len)
401 struct packed_git *p;
402 unsigned long idx_size;
405 if (check_packed_git_idx(path, &idx_size, &idx_map))
408 /* do we have a corresponding .pack file? */
409 strcpy(path + path_len - 4, ".pack");
410 if (stat(path, &st) || !S_ISREG(st.st_mode)) {
411 munmap(idx_map, idx_size);
414 /* ok, it looks sane as far as we can check without
415 * actually mapping the pack file.
417 p = xmalloc(sizeof(*p) + path_len + 2);
418 strcpy(p->pack_name, path);
419 p->index_size = idx_size;
420 p->pack_size = st.st_size;
421 p->index_base = idx_map;
424 p->pack_last_used = 0;
429 static void prepare_packed_git_one(char *objdir)
436 sprintf(path, "%s/pack", objdir);
442 while ((de = readdir(dir)) != NULL) {
443 int namelen = strlen(de->d_name);
444 struct packed_git *p;
446 if (strcmp(de->d_name + namelen - 4, ".idx"))
449 /* we have .idx. Is it a file we can map? */
450 strcpy(path + len, de->d_name);
451 p = add_packed_git(path, len + namelen);
454 p->next = packed_git;
460 void prepare_packed_git(void)
463 static int run_once = 0;
468 prepare_packed_git_one(get_object_directory());
470 for (i = 0; alt_odb[i].base != NULL; i++) {
471 alt_odb[i].name[0] = 0;
472 prepare_packed_git_one(alt_odb[i].base);
476 int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)
479 unsigned char real_sha1[20];
483 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
484 SHA1_Update(&c, map, size);
485 SHA1_Final(real_sha1, &c);
486 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
489 static void *map_sha1_file_internal(const unsigned char *sha1,
495 char *filename = find_sha1_file(sha1, &st);
501 fd = open(filename, O_RDONLY | sha1_file_open_flag);
503 /* See if it works without O_NOATIME */
504 switch (sha1_file_open_flag) {
506 fd = open(filename, O_RDONLY);
514 /* If it failed once, it will probably fail again.
515 * Stop using O_NOATIME
517 sha1_file_open_flag = 0;
519 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
521 if (-1 == (int)(long)map)
527 int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size)
529 /* Get the data stream */
530 memset(stream, 0, sizeof(*stream));
531 stream->next_in = map;
532 stream->avail_in = mapsize;
533 stream->next_out = buffer;
534 stream->avail_out = size;
537 return inflate(stream, 0);
540 static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
542 int bytes = strlen(buffer) + 1;
543 unsigned char *buf = xmalloc(1+size);
545 memcpy(buf, buffer + bytes, stream->total_out - bytes);
546 bytes = stream->total_out - bytes;
548 stream->next_out = buf + bytes;
549 stream->avail_out = size - bytes;
550 while (inflate(stream, Z_FINISH) == Z_OK)
559 * We used to just use "sscanf()", but that's actually way
560 * too permissive for what we want to check. So do an anal
561 * object header parse by hand.
563 int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
569 * The type can be at most ten bytes (including the
570 * terminating '\0' that we add), and is followed by
585 * The length must follow immediately, and be in canonical
586 * decimal format (ie "010" is not valid).
593 unsigned long c = *hdr - '0';
597 size = size * 10 + c;
603 * The length must be followed by a zero byte
605 return *hdr ? -1 : 0;
608 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
614 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
615 if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0)
618 return unpack_sha1_rest(&stream, hdr, *size);
621 /* forward declaration for a mutually recursive function */
622 static int packed_object_info(struct pack_entry *entry,
623 char *type, unsigned long *sizep);
625 static int packed_delta_info(unsigned char *base_sha1,
626 unsigned long delta_size,
629 unsigned long *sizep,
630 struct packed_git *p)
632 struct pack_entry base_ent;
635 die("truncated pack file");
637 /* The base entry _must_ be in the same pack */
638 if (!find_pack_entry_one(base_sha1, &base_ent, p))
639 die("failed to find delta-pack base object %s",
640 sha1_to_hex(base_sha1));
642 /* We choose to only get the type of the base object and
643 * ignore potentially corrupt pack file that expects the delta
644 * based on a base with a wrong size. This saves tons of
648 if (packed_object_info(&base_ent, type, NULL))
649 die("cannot get info for delta-pack base");
652 const unsigned char *data;
653 unsigned char delta_head[64];
654 unsigned long result_size;
658 memset(&stream, 0, sizeof(stream));
660 data = stream.next_in = base_sha1 + 20;
661 stream.avail_in = left - 20;
662 stream.next_out = delta_head;
663 stream.avail_out = sizeof(delta_head);
665 inflateInit(&stream);
666 st = inflate(&stream, Z_FINISH);
668 if ((st != Z_STREAM_END) &&
669 stream.total_out != sizeof(delta_head))
670 die("delta data unpack-initial failed");
672 /* Examine the initial part of the delta to figure out
676 get_delta_hdr_size(&data); /* ignore base size */
678 /* Read the result size */
679 result_size = get_delta_hdr_size(&data);
680 *sizep = result_size;
685 static unsigned long unpack_object_header(struct packed_git *p, unsigned long offset,
686 enum object_type *type, unsigned long *sizep)
689 unsigned char *pack, c;
692 if (offset >= p->pack_size)
693 die("object offset outside of pack file");
695 pack = p->pack_base + offset;
698 *type = (c >> 4) & 7;
702 if (offset >= p->pack_size)
703 die("object offset outside of pack file");
706 size += (c & 0x7f) << shift;
713 void packed_object_info_detail(struct pack_entry *e,
716 unsigned long *store_size,
717 int *delta_chain_length,
718 unsigned char *base_sha1)
720 struct packed_git *p = e->p;
721 unsigned long offset, left;
723 enum object_type kind;
725 offset = unpack_object_header(p, e->offset, &kind, size);
726 pack = p->pack_base + offset;
727 left = p->pack_size - offset;
728 if (kind != OBJ_DELTA)
729 *delta_chain_length = 0;
731 int chain_length = 0;
732 memcpy(base_sha1, pack, 20);
734 struct pack_entry base_ent;
737 find_pack_entry_one(pack, &base_ent, p);
738 offset = unpack_object_header(p, base_ent.offset,
740 pack = p->pack_base + offset;
742 } while (kind == OBJ_DELTA);
743 *delta_chain_length = chain_length;
747 strcpy(type, "commit");
750 strcpy(type, "tree");
753 strcpy(type, "blob");
759 die("corrupted pack file");
761 *store_size = 0; /* notyet */
764 static int packed_object_info(struct pack_entry *entry,
765 char *type, unsigned long *sizep)
767 struct packed_git *p = entry->p;
768 unsigned long offset, size, left;
770 enum object_type kind;
773 if (use_packed_git(p))
774 die("cannot map packed file");
776 offset = unpack_object_header(p, entry->offset, &kind, &size);
777 pack = p->pack_base + offset;
778 left = p->pack_size - offset;
782 retval = packed_delta_info(pack, size, left, type, sizep, p);
786 strcpy(type, "commit");
789 strcpy(type, "tree");
792 strcpy(type, "blob");
798 die("corrupted pack file");
806 /* forward declaration for a mutually recursive function */
807 static void *unpack_entry(struct pack_entry *, char *, unsigned long *);
809 static void *unpack_delta_entry(unsigned char *base_sha1,
810 unsigned long delta_size,
813 unsigned long *sizep,
814 struct packed_git *p)
816 struct pack_entry base_ent;
817 void *data, *delta_data, *result, *base;
818 unsigned long data_size, result_size, base_size;
823 die("truncated pack file");
824 data = base_sha1 + 20;
825 data_size = left - 20;
826 delta_data = xmalloc(delta_size);
828 memset(&stream, 0, sizeof(stream));
830 stream.next_in = data;
831 stream.avail_in = data_size;
832 stream.next_out = delta_data;
833 stream.avail_out = delta_size;
835 inflateInit(&stream);
836 st = inflate(&stream, Z_FINISH);
838 if ((st != Z_STREAM_END) || stream.total_out != delta_size)
839 die("delta data unpack failed");
841 /* The base entry _must_ be in the same pack */
842 if (!find_pack_entry_one(base_sha1, &base_ent, p))
843 die("failed to find delta-pack base object %s",
844 sha1_to_hex(base_sha1));
845 base = unpack_entry_gently(&base_ent, type, &base_size);
847 die("failed to read delta-pack base object %s",
848 sha1_to_hex(base_sha1));
849 result = patch_delta(base, base_size,
850 delta_data, delta_size,
853 die("failed to apply delta");
856 *sizep = result_size;
860 static void *unpack_non_delta_entry(unsigned char *data,
866 unsigned char *buffer;
868 buffer = xmalloc(size + 1);
870 memset(&stream, 0, sizeof(stream));
871 stream.next_in = data;
872 stream.avail_in = left;
873 stream.next_out = buffer;
874 stream.avail_out = size;
876 inflateInit(&stream);
877 st = inflate(&stream, Z_FINISH);
879 if ((st != Z_STREAM_END) || stream.total_out != size) {
887 static void *unpack_entry(struct pack_entry *entry,
888 char *type, unsigned long *sizep)
890 struct packed_git *p = entry->p;
893 if (use_packed_git(p))
894 die("cannot map packed file");
895 retval = unpack_entry_gently(entry, type, sizep);
898 die("corrupted pack file");
902 /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
903 void *unpack_entry_gently(struct pack_entry *entry,
904 char *type, unsigned long *sizep)
906 struct packed_git *p = entry->p;
907 unsigned long offset, size, left;
909 enum object_type kind;
912 offset = unpack_object_header(p, entry->offset, &kind, &size);
913 pack = p->pack_base + offset;
914 left = p->pack_size - offset;
917 retval = unpack_delta_entry(pack, size, left, type, sizep, p);
920 strcpy(type, "commit");
923 strcpy(type, "tree");
926 strcpy(type, "blob");
935 retval = unpack_non_delta_entry(pack, size, left);
939 int num_packed_objects(const struct packed_git *p)
941 /* See check_packed_git_idx() */
942 return (p->index_size - 20 - 20 - 4*256) / 24;
945 int nth_packed_object_sha1(const struct packed_git *p, int n,
948 void *index = p->index_base + 256;
949 if (n < 0 || num_packed_objects(p) <= n)
951 memcpy(sha1, (index + 24 * n + 4), 20);
955 int find_pack_entry_one(const unsigned char *sha1,
956 struct pack_entry *e, struct packed_git *p)
958 unsigned int *level1_ofs = p->index_base;
959 int hi = ntohl(level1_ofs[*sha1]);
960 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
961 void *index = p->index_base + 256;
964 int mi = (lo + hi) / 2;
965 int cmp = memcmp(index + 24 * mi + 4, sha1, 20);
967 e->offset = ntohl(*((int*)(index + 24 * mi)));
968 memcpy(e->sha1, sha1, 20);
980 static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
982 struct packed_git *p;
983 prepare_packed_git();
985 for (p = packed_git; p; p = p->next) {
986 if (find_pack_entry_one(sha1, e, p))
992 int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
995 unsigned long mapsize, size;
1000 map = map_sha1_file_internal(sha1, &mapsize);
1002 struct pack_entry e;
1004 if (!find_pack_entry(sha1, &e))
1005 return error("unable to find %s", sha1_to_hex(sha1));
1006 return packed_object_info(&e, type, sizep);
1008 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1009 status = error("unable to unpack %s header",
1011 if (parse_sha1_header(hdr, type, &size) < 0)
1012 status = error("unable to parse %s header", sha1_to_hex(sha1));
1018 inflateEnd(&stream);
1019 munmap(map, mapsize);
1023 static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size)
1025 struct pack_entry e;
1027 if (!find_pack_entry(sha1, &e)) {
1028 error("cannot read sha1_file for %s", sha1_to_hex(sha1));
1031 return unpack_entry(&e, type, size);
1034 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
1036 unsigned long mapsize;
1039 map = map_sha1_file_internal(sha1, &mapsize);
1041 buf = unpack_sha1_file(map, mapsize, type, size);
1042 munmap(map, mapsize);
1045 return read_packed_sha1(sha1, type, size);
1048 void *read_object_with_reference(const unsigned char *sha1,
1049 const char *required_type,
1050 unsigned long *size,
1051 unsigned char *actual_sha1_return)
1055 unsigned long isize;
1056 unsigned char actual_sha1[20];
1058 memcpy(actual_sha1, sha1, 20);
1060 int ref_length = -1;
1061 const char *ref_type = NULL;
1063 buffer = read_sha1_file(actual_sha1, type, &isize);
1066 if (!strcmp(type, required_type)) {
1068 if (actual_sha1_return)
1069 memcpy(actual_sha1_return, actual_sha1, 20);
1072 /* Handle references */
1073 else if (!strcmp(type, "commit"))
1075 else if (!strcmp(type, "tag"))
1076 ref_type = "object ";
1081 ref_length = strlen(ref_type);
1083 if (memcmp(buffer, ref_type, ref_length) ||
1084 get_sha1_hex(buffer + ref_length, actual_sha1)) {
1088 /* Now we have the ID of the referred-to object in
1089 * actual_sha1. Check again. */
1093 char *write_sha1_file_prepare(void *buf,
1096 unsigned char *sha1,
1102 /* Generate the header */
1103 *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
1107 SHA1_Update(&c, hdr, *hdrlen);
1108 SHA1_Update(&c, buf, len);
1109 SHA1_Final(sha1, &c);
1111 return sha1_file_name(sha1);
1114 int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
1117 unsigned char *compressed;
1119 unsigned char sha1[20];
1121 static char tmpfile[PATH_MAX];
1122 unsigned char hdr[50];
1123 int fd, hdrlen, ret;
1125 /* Normally if we have it in the pack then we do not bother writing
1126 * it out into .git/objects/??/?{38} file.
1128 filename = write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
1130 memcpy(returnsha1, sha1, 20);
1131 if (has_sha1_file(sha1))
1133 fd = open(filename, O_RDONLY);
1136 * FIXME!!! We might do collision checking here, but we'd
1137 * need to uncompress the old file and check it. Later.
1143 if (errno != ENOENT) {
1144 fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno));
1148 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
1150 fd = mkstemp(tmpfile);
1152 fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno));
1157 memset(&stream, 0, sizeof(stream));
1158 deflateInit(&stream, Z_BEST_COMPRESSION);
1159 size = deflateBound(&stream, len+hdrlen);
1160 compressed = xmalloc(size);
1163 stream.next_out = compressed;
1164 stream.avail_out = size;
1166 /* First header.. */
1167 stream.next_in = hdr;
1168 stream.avail_in = hdrlen;
1169 while (deflate(&stream, 0) == Z_OK)
1172 /* Then the data itself.. */
1173 stream.next_in = buf;
1174 stream.avail_in = len;
1175 while (deflate(&stream, Z_FINISH) == Z_OK)
1177 deflateEnd(&stream);
1178 size = stream.total_out;
1180 if (write(fd, compressed, size) != size)
1181 die("unable to write file");
1186 ret = link(tmpfile, filename);
1191 * Coda hack - coda doesn't like cross-directory links,
1192 * so we fall back to a rename, which will mean that it
1193 * won't be able to check collisions, but that's not a
1196 * When this succeeds, we just return 0. We have nothing
1199 if (ret == EXDEV && !rename(tmpfile, filename))
1204 if (ret != EEXIST) {
1205 fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
1208 /* FIXME!!! Collision check here ? */
1214 int write_sha1_to_fd(int fd, const unsigned char *sha1)
1217 unsigned long objsize;
1219 void *buf = map_sha1_file_internal(sha1, &objsize);
1222 unsigned char *unpacked;
1227 // need to unpack and recompress it by itself
1228 unpacked = read_packed_sha1(sha1, type, &len);
1230 hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
1233 memset(&stream, 0, sizeof(stream));
1234 deflateInit(&stream, Z_BEST_COMPRESSION);
1235 size = deflateBound(&stream, len + hdrlen);
1236 buf = xmalloc(size);
1239 stream.next_out = buf;
1240 stream.avail_out = size;
1242 /* First header.. */
1243 stream.next_in = (void *)hdr;
1244 stream.avail_in = hdrlen;
1245 while (deflate(&stream, 0) == Z_OK)
1248 /* Then the data itself.. */
1249 stream.next_in = unpacked;
1250 stream.avail_in = len;
1251 while (deflate(&stream, Z_FINISH) == Z_OK)
1253 deflateEnd(&stream);
1255 objsize = stream.total_out;
1259 size = write(fd, buf + posn, objsize - posn);
1262 fprintf(stderr, "write closed");
1269 } while (posn < objsize);
1273 int write_sha1_from_fd(const unsigned char *sha1, int fd)
1275 char *filename = sha1_file_name(sha1);
1279 unsigned char real_sha1[20];
1280 unsigned char buf[4096];
1281 unsigned char discard[4096];
1285 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
1288 return error("Couldn't open %s\n", filename);
1290 memset(&stream, 0, sizeof(stream));
1292 inflateInit(&stream);
1298 size = read(fd, buf, 4096);
1303 return error("Connection closed?");
1304 perror("Reading from connection");
1307 write(local, buf, size);
1308 stream.avail_in = size;
1309 stream.next_in = buf;
1311 stream.next_out = discard;
1312 stream.avail_out = sizeof(discard);
1313 ret = inflate(&stream, Z_SYNC_FLUSH);
1314 SHA1_Update(&c, discard, sizeof(discard) -
1316 } while (stream.avail_in && ret == Z_OK);
1318 } while (ret == Z_OK);
1319 inflateEnd(&stream);
1322 SHA1_Final(real_sha1, &c);
1323 if (ret != Z_STREAM_END) {
1325 return error("File %s corrupted", sha1_to_hex(sha1));
1327 if (memcmp(sha1, real_sha1, 20)) {
1329 return error("File %s has bad hash\n", sha1_to_hex(sha1));
1335 int has_sha1_pack(const unsigned char *sha1)
1337 struct pack_entry e;
1338 return find_pack_entry(sha1, &e);
1341 int has_sha1_file(const unsigned char *sha1)
1344 struct pack_entry e;
1346 if (find_sha1_file(sha1, &st))
1348 return find_pack_entry(sha1, &e);
1351 int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type)
1353 unsigned long size = st->st_size;
1356 unsigned char hdr[50];
1361 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1363 if ((int)(long)buf == -1)
1369 ret = write_sha1_file(buf, size, type, sha1);
1371 write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);