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 get_sha1(const char *str, unsigned char *sha1)
107 static char pathname[PATH_MAX];
108 static const char *prefix[] = {
118 if (!get_sha1_hex(str, sha1))
123 for (p = prefix; *p; p++) {
124 snprintf(pathname, sizeof(pathname), "%s/%s/%s",
126 if (!get_sha1_file(pathname, sha1))
133 char * sha1_to_hex(const unsigned char *sha1)
135 static char buffer[50];
136 static const char hex[] = "0123456789abcdef";
140 for (i = 0; i < 20; i++) {
141 unsigned int val = *sha1++;
142 *buf++ = hex[val >> 4];
143 *buf++ = hex[val & 0xf];
148 static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
151 for (i = 0; i < 20; i++) {
152 static char hex[] = "0123456789abcdef";
153 unsigned int val = sha1[i];
154 char *pos = pathbuf + i*2 + (i > 0);
155 *pos++ = hex[val >> 4];
156 *pos = hex[val & 0xf];
161 * NOTE! This returns a statically allocated buffer, so you have to be
162 * careful about using it. Do a "strdup()" if you need to save the
165 * Also note that this returns the location for creating. Reading
166 * SHA1 file can happen from any alternate directory listed in the
167 * DB_ENVIRONMENT environment variable if it is not found in
168 * the primary object database.
170 char *sha1_file_name(const unsigned char *sha1)
172 static char *name, *base;
175 const char *sha1_file_directory = get_object_directory();
176 int len = strlen(sha1_file_directory);
177 base = xmalloc(len + 60);
178 memcpy(base, sha1_file_directory, len);
179 memset(base+len, 0, 60);
182 name = base + len + 1;
184 fill_sha1_path(name, sha1);
188 struct alternate_object_database *alt_odb;
191 * Prepare alternate object database registry.
192 * alt_odb points at an array of struct alternate_object_database.
193 * This array is terminated with an element that has both its base
194 * and name set to NULL. alt_odb[n] comes from n'th non-empty
195 * element from colon separated ALTERNATE_DB_ENVIRONMENT environment
196 * variable, and its base points at a statically allocated buffer
197 * that contains "/the/directory/corresponding/to/.git/objects/...",
198 * while its name points just after the slash at the end of
199 * ".git/objects/" in the example above, and has enough space to hold
200 * 40-byte hex SHA1, an extra slash for the first level indirection,
201 * and the terminating NUL.
202 * This function allocates the alt_odb array and all the strings
203 * pointed by base fields of the array elements with one xmalloc();
204 * the string pool immediately follows the array.
206 void prepare_alt_odb(void)
209 const char *cp, *last;
211 const char *alt = gitenv(ALTERNATE_DB_ENVIRONMENT) ? : "";
215 /* The first pass counts how large an area to allocate to
216 * hold the entire alt_odb structure, including array of
217 * structs and path buffers for them. The second pass fills
218 * the structure and prepares the path buffers for use by
221 for (totlen = pass = 0; pass < 2; pass++) {
225 cp = strchr(last, ':') ? : last + strlen(last);
227 /* 43 = 40-byte + 2 '/' + terminating NUL */
228 int pfxlen = cp - last;
229 int entlen = pfxlen + 43;
233 alt_odb[i].base = op;
234 alt_odb[i].name = op + pfxlen + 1;
235 memcpy(op, last, pfxlen);
236 op[pfxlen] = op[pfxlen + 3] = '/';
242 while (*cp && *cp == ':')
248 alt_odb = xmalloc(sizeof(*alt_odb) * (i + 1) + totlen);
249 alt_odb[i].base = alt_odb[i].name = NULL;
250 op = (char*)(&alt_odb[i+1]);
254 static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
257 char *name = sha1_file_name(sha1);
262 for (i = 0; (name = alt_odb[i].name) != NULL; i++) {
263 fill_sha1_path(name, sha1);
264 if (!stat(alt_odb[i].base, st))
265 return alt_odb[i].base;
270 #define PACK_MAX_SZ (1<<26)
271 static int pack_used_ctr;
272 static unsigned long pack_mapped;
273 struct packed_git *packed_git;
277 unsigned char sha1[20];
278 struct packed_git *p;
281 static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
286 unsigned long idx_size;
288 int fd = open(path, O_RDONLY);
292 if (fstat(fd, &st)) {
296 idx_size = st.st_size;
297 idx_map = mmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
299 if (idx_map == MAP_FAILED)
304 /* check index map */
305 if (idx_size < 4*256 + 20)
306 return error("index file too small");
308 for (i = 0; i < 256; i++) {
309 unsigned int n = ntohl(index[i]);
311 return error("non-monotonic index");
317 * - 256 index entries 4 bytes each
318 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
319 * - 20-byte SHA1 of the packfile
320 * - 20-byte SHA1 file checksum
322 if (idx_size != 4*256 + nr * 24 + 20 + 20)
323 return error("wrong index file size");
326 *idx_size_ = idx_size;
330 static void unuse_one_packed_git(void)
335 static int use_packed_git(struct packed_git *p)
342 pack_mapped += p->pack_size;
343 while (PACK_MAX_SZ < pack_mapped)
344 unuse_one_packed_git();
345 fd = open(p->pack_name, O_RDONLY);
348 if (fstat(fd, &st)) {
352 if (st.st_size != p->pack_size)
354 map = mmap(NULL, p->pack_size, PROT_READ, MAP_PRIVATE, fd, 0);
356 if (map == MAP_FAILED)
360 p->pack_last_used = pack_used_ctr++;
364 static struct packed_git *add_packed_git(char *path, int path_len)
367 struct packed_git *p;
368 unsigned long idx_size;
371 if (check_packed_git_idx(path, &idx_size, &idx_map))
374 /* do we have a corresponding .pack file? */
375 strcpy(path + path_len - 4, ".pack");
376 if (stat(path, &st) || !S_ISREG(st.st_mode)) {
377 munmap(idx_map, idx_size);
380 /* ok, it looks sane as far as we can check without
381 * actually mapping the pack file.
383 p = xmalloc(sizeof(*p) + path_len + 2);
384 strcpy(p->pack_name, path);
385 p->index_size = idx_size;
386 p->pack_size = st.st_size;
387 p->index_base = idx_map;
390 p->pack_last_used = 0;
394 static void prepare_packed_git_one(char *objdir)
401 sprintf(path, "%s/pack", objdir);
407 while ((de = readdir(dir)) != NULL) {
408 int namelen = strlen(de->d_name);
409 struct packed_git *p;
411 if (strcmp(de->d_name + namelen - 4, ".idx"))
414 /* we have .idx. Is it a file we can map? */
415 strcpy(path + len, de->d_name);
416 p = add_packed_git(path, len + namelen);
419 p->next = packed_git;
424 void prepare_packed_git(void)
427 static int run_once = 0;
432 prepare_packed_git_one(get_object_directory());
434 for (i = 0; alt_odb[i].base != NULL; i++) {
435 alt_odb[i].name[0] = 0;
436 prepare_packed_git_one(alt_odb[i].base);
440 int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)
443 unsigned char real_sha1[20];
447 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
448 SHA1_Update(&c, map, size);
449 SHA1_Final(real_sha1, &c);
450 return memcmp(sha1, real_sha1, 20) ? -1 : 0;
453 static void *map_sha1_file_internal(const unsigned char *sha1,
460 char *filename = find_sha1_file(sha1, &st);
464 error("cannot map sha1 file %s", sha1_to_hex(sha1));
468 fd = open(filename, O_RDONLY | sha1_file_open_flag);
470 /* See if it works without O_NOATIME */
471 switch (sha1_file_open_flag) {
473 fd = open(filename, O_RDONLY);
483 /* If it failed once, it will probably fail again.
484 * Stop using O_NOATIME
486 sha1_file_open_flag = 0;
488 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
490 if (-1 == (int)(long)map)
496 void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
498 return map_sha1_file_internal(sha1, size, 1);
501 int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size)
503 /* Get the data stream */
504 memset(stream, 0, sizeof(*stream));
505 stream->next_in = map;
506 stream->avail_in = mapsize;
507 stream->next_out = buffer;
508 stream->avail_out = size;
511 return inflate(stream, 0);
514 void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
516 int bytes = strlen(buffer) + 1;
517 unsigned char *buf = xmalloc(1+size);
519 memcpy(buf, buffer + bytes, stream->total_out - bytes);
520 bytes = stream->total_out - bytes;
522 stream->next_out = buf + bytes;
523 stream->avail_out = size - bytes;
524 while (inflate(stream, Z_FINISH) == Z_OK)
533 * We used to just use "sscanf()", but that's actually way
534 * too permissive for what we want to check. So do an anal
535 * object header parse by hand.
537 int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
543 * The type can be at most ten bytes (including the
544 * terminating '\0' that we add), and is followed by
559 * The length must follow immediately, and be in canonical
560 * decimal format (ie "010" is not valid).
567 unsigned long c = *hdr - '0';
571 size = size * 10 + c;
577 * The length must be followed by a zero byte
579 return *hdr ? -1 : 0;
582 void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
588 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
589 if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0)
592 return unpack_sha1_rest(&stream, hdr, *size);
595 static int packed_delta_info(unsigned char *base_sha1,
596 unsigned long delta_size,
599 unsigned long *sizep)
602 unsigned char delta_head[64];
605 unsigned long data_size, result_size, base_size, verify_base_size;
610 die("truncated pack file");
611 if (sha1_object_info(base_sha1, type, &base_size))
612 die("cannot get info for delta-pack base");
614 data = base_sha1 + 20;
615 data_size = left - 20;
617 memset(&stream, 0, sizeof(stream));
619 stream.next_in = data;
620 stream.avail_in = data_size;
621 stream.next_out = delta_head;
622 stream.avail_out = sizeof(delta_head);
624 inflateInit(&stream);
625 st = inflate(&stream, Z_FINISH);
627 if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head))
628 die("delta data unpack-initial failed");
630 /* Examine the initial part of the delta to figure out
631 * the result size. Verify the base size while we are at it.
634 verify_base_size = i = 0;
638 verify_base_size |= *data++ << i;
643 /* Read the result size */
648 result_size |= *data++ << i;
652 if (verify_base_size != base_size)
653 die("delta base size mismatch");
655 *sizep = result_size;
659 static unsigned long unpack_object_header(struct packed_git *p, unsigned long offset,
660 enum object_type *type, unsigned long *sizep)
663 unsigned char *pack, c;
666 if (offset >= p->pack_size)
667 die("object offset outside of pack file");
669 pack = p->pack_base + offset;
672 *type = (c >> 4) & 7;
676 if (offset >= p->pack_size)
677 die("object offset outside of pack file");
680 size += (c & 0x7f) << shift;
687 static int packed_object_info(struct pack_entry *entry,
688 char *type, unsigned long *sizep)
690 struct packed_git *p = entry->p;
691 unsigned long offset, size, left;
693 enum object_type kind;
695 if (use_packed_git(p))
696 die("cannot map packed file");
698 offset = unpack_object_header(p, entry->offset, &kind, &size);
699 pack = p->pack_base + offset;
700 left = p->pack_size - offset;
704 return packed_delta_info(pack, size, left, type, sizep);
707 strcpy(type, "commit");
710 strcpy(type, "tree");
713 strcpy(type, "blob");
719 die("corrupted pack file");
725 /* forward declaration for a mutually recursive function */
726 static void *unpack_entry(struct pack_entry *, char *, unsigned long *);
728 static void *unpack_delta_entry(unsigned char *base_sha1,
729 unsigned long delta_size,
732 unsigned long *sizep)
734 void *data, *delta_data, *result, *base;
735 unsigned long data_size, result_size, base_size;
740 die("truncated pack file");
741 data = base_sha1 + 20;
742 data_size = left - 20;
743 delta_data = xmalloc(delta_size);
745 memset(&stream, 0, sizeof(stream));
747 stream.next_in = data;
748 stream.avail_in = data_size;
749 stream.next_out = delta_data;
750 stream.avail_out = delta_size;
752 inflateInit(&stream);
753 st = inflate(&stream, Z_FINISH);
755 if ((st != Z_STREAM_END) || stream.total_out != delta_size)
756 die("delta data unpack failed");
758 /* This may recursively unpack the base, which is what we want */
759 base = read_sha1_file(base_sha1, type, &base_size);
761 die("failed to read delta-pack base object %s",
762 sha1_to_hex(base_sha1));
763 result = patch_delta(base, base_size,
764 delta_data, delta_size,
767 die("failed to apply delta");
770 *sizep = result_size;
774 static void *unpack_non_delta_entry(unsigned char *data,
782 buffer = xmalloc(size + 1);
784 memset(&stream, 0, sizeof(stream));
785 stream.next_in = data;
786 stream.avail_in = left;
787 stream.next_out = buffer;
788 stream.avail_out = size;
790 inflateInit(&stream);
791 st = inflate(&stream, Z_FINISH);
793 if ((st != Z_STREAM_END) || stream.total_out != size) {
801 static void *unpack_entry(struct pack_entry *entry,
802 char *type, unsigned long *sizep)
804 struct packed_git *p = entry->p;
805 unsigned long offset, size, left;
807 enum object_type kind;
809 if (use_packed_git(p))
810 die("cannot map packed file");
812 offset = unpack_object_header(p, entry->offset, &kind, &size);
813 pack = p->pack_base + offset;
814 left = p->pack_size - offset;
817 return unpack_delta_entry(pack, size, left, type, sizep);
819 strcpy(type, "commit");
822 strcpy(type, "tree");
825 strcpy(type, "blob");
831 die("corrupted pack file");
834 return unpack_non_delta_entry(pack, size, left);
837 int num_packed_objects(const struct packed_git *p)
839 /* See check_packed_git_idx and pack-objects.c */
840 return (p->index_size - 20 - 20 - 4*256) / 24;
843 int nth_packed_object_sha1(const struct packed_git *p, int n,
846 void *index = p->index_base + 256;
847 if (n < 0 || num_packed_objects(p) <= n)
849 memcpy(sha1, (index + 24 * n + 4), 20);
853 static int find_pack_entry_1(const unsigned char *sha1,
854 struct pack_entry *e, struct packed_git *p)
856 int *level1_ofs = p->index_base;
857 int hi = ntohl(level1_ofs[*sha1]);
858 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
859 void *index = p->index_base + 256;
862 int mi = (lo + hi) / 2;
863 int cmp = memcmp(index + 24 * mi + 4, sha1, 20);
865 e->offset = ntohl(*((int*)(index + 24 * mi)));
866 memcpy(e->sha1, sha1, 20);
878 static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
880 struct packed_git *p;
881 prepare_packed_git();
883 for (p = packed_git; p; p = p->next) {
884 if (find_pack_entry_1(sha1, e, p))
890 int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
893 unsigned long mapsize, size;
898 map = map_sha1_file_internal(sha1, &mapsize, 0);
902 if (!find_pack_entry(sha1, &e))
903 return error("unable to find %s", sha1_to_hex(sha1));
904 if (!packed_object_info(&e, type, sizep))
907 map = unpack_entry(&e, type, sizep);
909 return (map == NULL) ? 0 : -1;
911 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
912 status = error("unable to unpack %s header",
914 if (parse_sha1_header(hdr, type, &size) < 0)
915 status = error("unable to parse %s header", sha1_to_hex(sha1));
921 munmap(map, mapsize);
925 static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size)
929 if (!find_pack_entry(sha1, &e)) {
930 error("cannot read sha1_file for %s", sha1_to_hex(sha1));
933 return unpack_entry(&e, type, size);
936 void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
938 unsigned long mapsize;
941 map = map_sha1_file_internal(sha1, &mapsize, 0);
943 buf = unpack_sha1_file(map, mapsize, type, size);
944 munmap(map, mapsize);
947 return read_packed_sha1(sha1, type, size);
950 void *read_object_with_reference(const unsigned char *sha1,
951 const char *required_type,
953 unsigned char *actual_sha1_return)
958 unsigned char actual_sha1[20];
960 memcpy(actual_sha1, sha1, 20);
963 const char *ref_type = NULL;
965 buffer = read_sha1_file(actual_sha1, type, &isize);
968 if (!strcmp(type, required_type)) {
970 if (actual_sha1_return)
971 memcpy(actual_sha1_return, actual_sha1, 20);
974 /* Handle references */
975 else if (!strcmp(type, "commit"))
977 else if (!strcmp(type, "tag"))
978 ref_type = "object ";
983 ref_length = strlen(ref_type);
985 if (memcmp(buffer, ref_type, ref_length) ||
986 get_sha1_hex(buffer + ref_length, actual_sha1)) {
990 /* Now we have the ID of the referred-to object in
991 * actual_sha1. Check again. */
995 static char *write_sha1_file_prepare(void *buf,
1004 /* Generate the header */
1005 *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
1009 SHA1_Update(&c, hdr, *hdrlen);
1010 SHA1_Update(&c, buf, len);
1011 SHA1_Final(sha1, &c);
1013 return sha1_file_name(sha1);
1016 int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
1019 unsigned char *compressed;
1021 unsigned char sha1[20];
1023 static char tmpfile[PATH_MAX];
1024 unsigned char hdr[50];
1025 int fd, hdrlen, ret;
1027 /* Normally if we have it in the pack then we do not bother writing
1028 * it out into .git/objects/??/?{38} file.
1030 filename = write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
1032 memcpy(returnsha1, sha1, 20);
1033 if (has_sha1_file(sha1))
1035 fd = open(filename, O_RDONLY);
1038 * FIXME!!! We might do collision checking here, but we'd
1039 * need to uncompress the old file and check it. Later.
1045 if (errno != ENOENT) {
1046 fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno));
1050 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
1052 fd = mkstemp(tmpfile);
1054 fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno));
1059 memset(&stream, 0, sizeof(stream));
1060 deflateInit(&stream, Z_BEST_COMPRESSION);
1061 size = deflateBound(&stream, len+hdrlen);
1062 compressed = xmalloc(size);
1065 stream.next_out = compressed;
1066 stream.avail_out = size;
1068 /* First header.. */
1069 stream.next_in = hdr;
1070 stream.avail_in = hdrlen;
1071 while (deflate(&stream, 0) == Z_OK)
1074 /* Then the data itself.. */
1075 stream.next_in = buf;
1076 stream.avail_in = len;
1077 while (deflate(&stream, Z_FINISH) == Z_OK)
1079 deflateEnd(&stream);
1080 size = stream.total_out;
1082 if (write(fd, compressed, size) != size)
1083 die("unable to write file");
1088 ret = link(tmpfile, filename);
1093 * Coda hack - coda doesn't like cross-directory links,
1094 * so we fall back to a rename, which will mean that it
1095 * won't be able to check collisions, but that's not a
1098 * When this succeeds, we just return 0. We have nothing
1101 if (ret == EXDEV && !rename(tmpfile, filename))
1106 if (ret != EEXIST) {
1107 fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
1110 /* FIXME!!! Collision check here ? */
1116 int write_sha1_from_fd(const unsigned char *sha1, int fd)
1118 char *filename = sha1_file_name(sha1);
1122 unsigned char real_sha1[20];
1123 unsigned char buf[4096];
1124 unsigned char discard[4096];
1128 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
1131 return error("Couldn't open %s\n", filename);
1133 memset(&stream, 0, sizeof(stream));
1135 inflateInit(&stream);
1141 size = read(fd, buf, 4096);
1146 return error("Connection closed?");
1147 perror("Reading from connection");
1150 write(local, buf, size);
1151 stream.avail_in = size;
1152 stream.next_in = buf;
1154 stream.next_out = discard;
1155 stream.avail_out = sizeof(discard);
1156 ret = inflate(&stream, Z_SYNC_FLUSH);
1157 SHA1_Update(&c, discard, sizeof(discard) -
1159 } while (stream.avail_in && ret == Z_OK);
1161 } while (ret == Z_OK);
1162 inflateEnd(&stream);
1165 SHA1_Final(real_sha1, &c);
1166 if (ret != Z_STREAM_END) {
1168 return error("File %s corrupted", sha1_to_hex(sha1));
1170 if (memcmp(sha1, real_sha1, 20)) {
1172 return error("File %s has bad hash\n", sha1_to_hex(sha1));
1178 int has_sha1_file(const unsigned char *sha1)
1181 struct pack_entry e;
1183 if (find_sha1_file(sha1, &st))
1185 return find_pack_entry(sha1, &e);
1188 int index_fd(unsigned char *sha1, int fd, struct stat *st)
1190 unsigned long size = st->st_size;
1196 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1198 if ((int)(long)buf == -1)
1201 ret = write_sha1_file(buf, size, "blob", sha1);