2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
11 #include "string-list.h"
17 #include "run-command.h"
20 #include "tree-walk.h"
22 #include "pack-revindex.h"
23 #include "sha1-lookup.h"
24 #include "bulk-checkin.h"
25 #include "streaming.h"
29 #include "mergesort.h"
33 const unsigned char null_sha1
[GIT_MAX_RAWSZ
];
34 const struct object_id null_oid
;
35 const struct object_id empty_tree_oid
= {
36 EMPTY_TREE_SHA1_BIN_LITERAL
38 const struct object_id empty_blob_oid
= {
39 EMPTY_BLOB_SHA1_BIN_LITERAL
42 static void git_hash_sha1_init(void *ctx
)
44 git_SHA1_Init((git_SHA_CTX
*)ctx
);
47 static void git_hash_sha1_update(void *ctx
, const void *data
, size_t len
)
49 git_SHA1_Update((git_SHA_CTX
*)ctx
, data
, len
);
52 static void git_hash_sha1_final(unsigned char *hash
, void *ctx
)
54 git_SHA1_Final(hash
, (git_SHA_CTX
*)ctx
);
57 static void git_hash_unknown_init(void *ctx
)
59 die("trying to init unknown hash");
62 static void git_hash_unknown_update(void *ctx
, const void *data
, size_t len
)
64 die("trying to update unknown hash");
67 static void git_hash_unknown_final(unsigned char *hash
, void *ctx
)
69 die("trying to finalize unknown hash");
72 const struct git_hash_algo hash_algos
[GIT_HASH_NALGOS
] = {
79 git_hash_unknown_init
,
80 git_hash_unknown_update
,
81 git_hash_unknown_final
,
87 /* "sha1", big-endian */
101 * This is meant to hold a *small* number of objects that you would
102 * want read_sha1_file() to be able to return, but yet you do not want
103 * to write them into the object store (e.g. a browse-only
106 static struct cached_object
{
107 unsigned char sha1
[20];
108 enum object_type type
;
112 static int cached_object_nr
, cached_object_alloc
;
114 static struct cached_object empty_tree
= {
115 EMPTY_TREE_SHA1_BIN_LITERAL
,
121 static struct cached_object
*find_cached_object(const unsigned char *sha1
)
124 struct cached_object
*co
= cached_objects
;
126 for (i
= 0; i
< cached_object_nr
; i
++, co
++) {
127 if (!hashcmp(co
->sha1
, sha1
))
130 if (!hashcmp(sha1
, empty_tree
.sha1
))
135 int mkdir_in_gitdir(const char *path
)
137 if (mkdir(path
, 0777)) {
138 int saved_errno
= errno
;
140 struct strbuf sb
= STRBUF_INIT
;
145 * Are we looking at a path in a symlinked worktree
146 * whose original repository does not yet have it?
147 * e.g. .git/rr-cache pointing at its original
148 * repository in which the user hasn't performed any
149 * conflict resolution yet?
151 if (lstat(path
, &st
) || !S_ISLNK(st
.st_mode
) ||
152 strbuf_readlink(&sb
, path
, st
.st_size
) ||
153 !is_absolute_path(sb
.buf
) ||
154 mkdir(sb
.buf
, 0777)) {
161 return adjust_shared_perm(path
);
164 enum scld_error
safe_create_leading_directories(char *path
)
166 char *next_component
= path
+ offset_1st_component(path
);
167 enum scld_error ret
= SCLD_OK
;
169 while (ret
== SCLD_OK
&& next_component
) {
171 char *slash
= next_component
, slash_character
;
173 while (*slash
&& !is_dir_sep(*slash
))
179 next_component
= slash
+ 1;
180 while (is_dir_sep(*next_component
))
182 if (!*next_component
)
185 slash_character
= *slash
;
187 if (!stat(path
, &st
)) {
189 if (!S_ISDIR(st
.st_mode
)) {
193 } else if (mkdir(path
, 0777)) {
194 if (errno
== EEXIST
&&
195 !stat(path
, &st
) && S_ISDIR(st
.st_mode
))
196 ; /* somebody created it since we checked */
197 else if (errno
== ENOENT
)
199 * Either mkdir() failed because
200 * somebody just pruned the containing
201 * directory, or stat() failed because
202 * the file that was in our way was
203 * just removed. Either way, inform
204 * the caller that it might be worth
210 } else if (adjust_shared_perm(path
)) {
213 *slash
= slash_character
;
218 enum scld_error
safe_create_leading_directories_const(const char *path
)
221 /* path points to cache entries, so xstrdup before messing with it */
222 char *buf
= xstrdup(path
);
223 enum scld_error result
= safe_create_leading_directories(buf
);
231 int raceproof_create_file(const char *path
, create_file_fn fn
, void *cb
)
234 * The number of times we will try to remove empty directories
235 * in the way of path. This is only 1 because if another
236 * process is racily creating directories that conflict with
237 * us, we don't want to fight against them.
239 int remove_directories_remaining
= 1;
242 * The number of times that we will try to create the
243 * directories containing path. We are willing to attempt this
244 * more than once, because another process could be trying to
245 * clean up empty directories at the same time as we are
246 * trying to create them.
248 int create_directories_remaining
= 3;
250 /* A scratch copy of path, filled lazily if we need it: */
251 struct strbuf path_copy
= STRBUF_INIT
;
264 if (errno
== EISDIR
&& remove_directories_remaining
-- > 0) {
266 * A directory is in the way. Maybe it is empty; try
270 strbuf_addstr(&path_copy
, path
);
272 if (!remove_dir_recursively(&path_copy
, REMOVE_DIR_EMPTY_ONLY
))
274 } else if (errno
== ENOENT
&& create_directories_remaining
-- > 0) {
276 * Maybe the containing directory didn't exist, or
277 * maybe it was just deleted by a process that is
278 * racing with us to clean up empty directories. Try
281 enum scld_error scld_result
;
284 strbuf_addstr(&path_copy
, path
);
287 scld_result
= safe_create_leading_directories(path_copy
.buf
);
288 if (scld_result
== SCLD_OK
)
290 } while (scld_result
== SCLD_VANISHED
&& create_directories_remaining
-- > 0);
294 strbuf_release(&path_copy
);
299 static void fill_sha1_path(struct strbuf
*buf
, const unsigned char *sha1
)
302 for (i
= 0; i
< 20; i
++) {
303 static char hex
[] = "0123456789abcdef";
304 unsigned int val
= sha1
[i
];
305 strbuf_addch(buf
, hex
[val
>> 4]);
306 strbuf_addch(buf
, hex
[val
& 0xf]);
308 strbuf_addch(buf
, '/');
312 const char *sha1_file_name(const unsigned char *sha1
)
314 static struct strbuf buf
= STRBUF_INIT
;
317 strbuf_addf(&buf
, "%s/", get_object_directory());
319 fill_sha1_path(&buf
, sha1
);
323 struct strbuf
*alt_scratch_buf(struct alternate_object_database
*alt
)
325 strbuf_setlen(&alt
->scratch
, alt
->base_len
);
326 return &alt
->scratch
;
329 static const char *alt_sha1_path(struct alternate_object_database
*alt
,
330 const unsigned char *sha1
)
332 struct strbuf
*buf
= alt_scratch_buf(alt
);
333 fill_sha1_path(buf
, sha1
);
337 struct alternate_object_database
*alt_odb_list
;
338 static struct alternate_object_database
**alt_odb_tail
;
341 * Return non-zero iff the path is usable as an alternate object database.
343 static int alt_odb_usable(struct strbuf
*path
, const char *normalized_objdir
)
345 struct alternate_object_database
*alt
;
347 /* Detect cases where alternate disappeared */
348 if (!is_directory(path
->buf
)) {
349 error("object directory %s does not exist; "
350 "check .git/objects/info/alternates.",
356 * Prevent the common mistake of listing the same
357 * thing twice, or object directory itself.
359 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
360 if (!fspathcmp(path
->buf
, alt
->path
))
363 if (!fspathcmp(path
->buf
, normalized_objdir
))
370 * Prepare alternate object database registry.
372 * The variable alt_odb_list points at the list of struct
373 * alternate_object_database. The elements on this list come from
374 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
375 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
376 * whose contents is similar to that environment variable but can be
377 * LF separated. Its base points at a statically allocated buffer that
378 * contains "/the/directory/corresponding/to/.git/objects/...", while
379 * its name points just after the slash at the end of ".git/objects/"
380 * in the example above, and has enough space to hold 40-byte hex
381 * SHA1, an extra slash for the first level indirection, and the
384 static void read_info_alternates(const char * relative_base
, int depth
);
385 static int link_alt_odb_entry(const char *entry
, const char *relative_base
,
386 int depth
, const char *normalized_objdir
)
388 struct alternate_object_database
*ent
;
389 struct strbuf pathbuf
= STRBUF_INIT
;
391 if (!is_absolute_path(entry
) && relative_base
) {
392 strbuf_realpath(&pathbuf
, relative_base
, 1);
393 strbuf_addch(&pathbuf
, '/');
395 strbuf_addstr(&pathbuf
, entry
);
397 if (strbuf_normalize_path(&pathbuf
) < 0 && relative_base
) {
398 error("unable to normalize alternate object path: %s",
400 strbuf_release(&pathbuf
);
405 * The trailing slash after the directory name is given by
406 * this function at the end. Remove duplicates.
408 while (pathbuf
.len
&& pathbuf
.buf
[pathbuf
.len
- 1] == '/')
409 strbuf_setlen(&pathbuf
, pathbuf
.len
- 1);
411 if (!alt_odb_usable(&pathbuf
, normalized_objdir
)) {
412 strbuf_release(&pathbuf
);
416 ent
= alloc_alt_odb(pathbuf
.buf
);
418 /* add the alternate entry */
420 alt_odb_tail
= &(ent
->next
);
423 /* recursively add alternates */
424 read_info_alternates(pathbuf
.buf
, depth
+ 1);
426 strbuf_release(&pathbuf
);
430 static const char *parse_alt_odb_entry(const char *string
,
438 if (*string
== '#') {
439 /* comment; consume up to next separator */
440 end
= strchrnul(string
, sep
);
441 } else if (*string
== '"' && !unquote_c_style(out
, string
, &end
)) {
443 * quoted path; unquote_c_style has copied the
444 * data for us and set "end". Broken quoting (e.g.,
445 * an entry that doesn't end with a quote) falls
446 * back to the unquoted case below.
449 /* normal, unquoted path */
450 end
= strchrnul(string
, sep
);
451 strbuf_add(out
, string
, end
- string
);
459 static void link_alt_odb_entries(const char *alt
, int sep
,
460 const char *relative_base
, int depth
)
462 struct strbuf objdirbuf
= STRBUF_INIT
;
463 struct strbuf entry
= STRBUF_INIT
;
466 error("%s: ignoring alternate object stores, nesting too deep.",
471 strbuf_add_absolute_path(&objdirbuf
, get_object_directory());
472 if (strbuf_normalize_path(&objdirbuf
) < 0)
473 die("unable to normalize object directory: %s",
477 alt
= parse_alt_odb_entry(alt
, sep
, &entry
);
480 link_alt_odb_entry(entry
.buf
, relative_base
, depth
, objdirbuf
.buf
);
482 strbuf_release(&entry
);
483 strbuf_release(&objdirbuf
);
486 static void read_info_alternates(const char * relative_base
, int depth
)
489 struct strbuf buf
= STRBUF_INIT
;
491 path
= xstrfmt("%s/info/alternates", relative_base
);
492 if (strbuf_read_file(&buf
, path
, 1024) < 0) {
493 warn_on_fopen_errors(path
);
498 link_alt_odb_entries(buf
.buf
, '\n', relative_base
, depth
);
499 strbuf_release(&buf
);
503 struct alternate_object_database
*alloc_alt_odb(const char *dir
)
505 struct alternate_object_database
*ent
;
507 FLEX_ALLOC_STR(ent
, path
, dir
);
508 strbuf_init(&ent
->scratch
, 0);
509 strbuf_addf(&ent
->scratch
, "%s/", dir
);
510 ent
->base_len
= ent
->scratch
.len
;
515 void add_to_alternates_file(const char *reference
)
517 struct lock_file lock
= LOCK_INIT
;
518 char *alts
= git_pathdup("objects/info/alternates");
522 hold_lock_file_for_update(&lock
, alts
, LOCK_DIE_ON_ERROR
);
523 out
= fdopen_lock_file(&lock
, "w");
525 die_errno("unable to fdopen alternates lockfile");
527 in
= fopen(alts
, "r");
529 struct strbuf line
= STRBUF_INIT
;
531 while (strbuf_getline(&line
, in
) != EOF
) {
532 if (!strcmp(reference
, line
.buf
)) {
536 fprintf_or_die(out
, "%s\n", line
.buf
);
539 strbuf_release(&line
);
542 else if (errno
!= ENOENT
)
543 die_errno("unable to read alternates file");
546 rollback_lock_file(&lock
);
548 fprintf_or_die(out
, "%s\n", reference
);
549 if (commit_lock_file(&lock
))
550 die_errno("unable to move new alternates file into place");
552 link_alt_odb_entries(reference
, '\n', NULL
, 0);
557 void add_to_alternates_memory(const char *reference
)
560 * Make sure alternates are initialized, or else our entry may be
561 * overwritten when they are.
565 link_alt_odb_entries(reference
, '\n', NULL
, 0);
569 * Compute the exact path an alternate is at and returns it. In case of
570 * error NULL is returned and the human readable error is added to `err`
571 * `path` may be relative and should point to $GITDIR.
572 * `err` must not be null.
574 char *compute_alternate_path(const char *path
, struct strbuf
*err
)
576 char *ref_git
= NULL
;
577 const char *repo
, *ref_git_s
;
580 ref_git_s
= real_path_if_valid(path
);
583 strbuf_addf(err
, _("path '%s' does not exist"), path
);
587 * Beware: read_gitfile(), real_path() and mkpath()
588 * return static buffer
590 ref_git
= xstrdup(ref_git_s
);
592 repo
= read_gitfile(ref_git
);
594 repo
= read_gitfile(mkpath("%s/.git", ref_git
));
597 ref_git
= xstrdup(repo
);
600 if (!repo
&& is_directory(mkpath("%s/.git/objects", ref_git
))) {
601 char *ref_git_git
= mkpathdup("%s/.git", ref_git
);
603 ref_git
= ref_git_git
;
604 } else if (!is_directory(mkpath("%s/objects", ref_git
))) {
605 struct strbuf sb
= STRBUF_INIT
;
607 if (get_common_dir(&sb
, ref_git
)) {
609 _("reference repository '%s' as a linked "
610 "checkout is not supported yet."),
615 strbuf_addf(err
, _("reference repository '%s' is not a "
616 "local repository."), path
);
620 if (!access(mkpath("%s/shallow", ref_git
), F_OK
)) {
621 strbuf_addf(err
, _("reference repository '%s' is shallow"),
627 if (!access(mkpath("%s/info/grafts", ref_git
), F_OK
)) {
629 _("reference repository '%s' is grafted"),
637 FREE_AND_NULL(ref_git
);
643 int foreach_alt_odb(alt_odb_fn fn
, void *cb
)
645 struct alternate_object_database
*ent
;
649 for (ent
= alt_odb_list
; ent
; ent
= ent
->next
) {
657 void prepare_alt_odb(void)
664 alt
= getenv(ALTERNATE_DB_ENVIRONMENT
);
667 alt_odb_tail
= &alt_odb_list
;
668 link_alt_odb_entries(alt
, PATH_SEP
, NULL
, 0);
670 read_info_alternates(get_object_directory(), 0);
673 /* Returns 1 if we have successfully freshened the file, 0 otherwise. */
674 static int freshen_file(const char *fn
)
677 t
.actime
= t
.modtime
= time(NULL
);
678 return !utime(fn
, &t
);
682 * All of the check_and_freshen functions return 1 if the file exists and was
683 * freshened (if freshening was requested), 0 otherwise. If they return
684 * 0, you should not assume that it is safe to skip a write of the object (it
685 * either does not exist on disk, or has a stale mtime and may be subject to
688 int check_and_freshen_file(const char *fn
, int freshen
)
690 if (access(fn
, F_OK
))
692 if (freshen
&& !freshen_file(fn
))
697 static int check_and_freshen_local(const unsigned char *sha1
, int freshen
)
699 return check_and_freshen_file(sha1_file_name(sha1
), freshen
);
702 static int check_and_freshen_nonlocal(const unsigned char *sha1
, int freshen
)
704 struct alternate_object_database
*alt
;
706 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
707 const char *path
= alt_sha1_path(alt
, sha1
);
708 if (check_and_freshen_file(path
, freshen
))
714 static int check_and_freshen(const unsigned char *sha1
, int freshen
)
716 return check_and_freshen_local(sha1
, freshen
) ||
717 check_and_freshen_nonlocal(sha1
, freshen
);
720 int has_loose_object_nonlocal(const unsigned char *sha1
)
722 return check_and_freshen_nonlocal(sha1
, 0);
725 static int has_loose_object(const unsigned char *sha1
)
727 return check_and_freshen(sha1
, 0);
730 static void mmap_limit_check(size_t length
)
732 static size_t limit
= 0;
734 limit
= git_env_ulong("GIT_MMAP_LIMIT", 0);
739 die("attempting to mmap %"PRIuMAX
" over limit %"PRIuMAX
,
740 (uintmax_t)length
, (uintmax_t)limit
);
743 void *xmmap_gently(void *start
, size_t length
,
744 int prot
, int flags
, int fd
, off_t offset
)
748 mmap_limit_check(length
);
749 ret
= mmap(start
, length
, prot
, flags
, fd
, offset
);
750 if (ret
== MAP_FAILED
) {
753 release_pack_memory(length
);
754 ret
= mmap(start
, length
, prot
, flags
, fd
, offset
);
759 void *xmmap(void *start
, size_t length
,
760 int prot
, int flags
, int fd
, off_t offset
)
762 void *ret
= xmmap_gently(start
, length
, prot
, flags
, fd
, offset
);
763 if (ret
== MAP_FAILED
)
764 die_errno("mmap failed");
769 * With an in-core object data in "map", rehash it to make sure the
770 * object name actually matches "sha1" to detect object corruption.
771 * With "map" == NULL, try reading the object named with "sha1" using
772 * the streaming interface and rehash it to do the same.
774 int check_sha1_signature(const unsigned char *sha1
, void *map
,
775 unsigned long size
, const char *type
)
777 unsigned char real_sha1
[20];
778 enum object_type obj_type
;
779 struct git_istream
*st
;
785 hash_sha1_file(map
, size
, type
, real_sha1
);
786 return hashcmp(sha1
, real_sha1
) ? -1 : 0;
789 st
= open_istream(sha1
, &obj_type
, &size
, NULL
);
793 /* Generate the header */
794 hdrlen
= xsnprintf(hdr
, sizeof(hdr
), "%s %lu", typename(obj_type
), size
) + 1;
798 git_SHA1_Update(&c
, hdr
, hdrlen
);
801 ssize_t readlen
= read_istream(st
, buf
, sizeof(buf
));
809 git_SHA1_Update(&c
, buf
, readlen
);
811 git_SHA1_Final(real_sha1
, &c
);
813 return hashcmp(sha1
, real_sha1
) ? -1 : 0;
816 int git_open_cloexec(const char *name
, int flags
)
819 static int o_cloexec
= O_CLOEXEC
;
821 fd
= open(name
, flags
| o_cloexec
);
822 if ((o_cloexec
& O_CLOEXEC
) && fd
< 0 && errno
== EINVAL
) {
823 /* Try again w/o O_CLOEXEC: the kernel might not support it */
824 o_cloexec
&= ~O_CLOEXEC
;
825 fd
= open(name
, flags
| o_cloexec
);
828 #if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
830 static int fd_cloexec
= FD_CLOEXEC
;
832 if (!o_cloexec
&& 0 <= fd
&& fd_cloexec
) {
833 /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */
834 int flags
= fcntl(fd
, F_GETFD
);
835 if (fcntl(fd
, F_SETFD
, flags
| fd_cloexec
))
844 * Find "sha1" as a loose object in the local repository or in an alternate.
845 * Returns 0 on success, negative on failure.
847 * The "path" out-parameter will give the path of the object we found (if any).
848 * Note that it may point to static storage and is only valid until another
849 * call to sha1_file_name(), etc.
851 static int stat_sha1_file(const unsigned char *sha1
, struct stat
*st
,
854 struct alternate_object_database
*alt
;
856 *path
= sha1_file_name(sha1
);
857 if (!lstat(*path
, st
))
862 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
863 *path
= alt_sha1_path(alt
, sha1
);
864 if (!lstat(*path
, st
))
872 * Like stat_sha1_file(), but actually open the object and return the
873 * descriptor. See the caveats on the "path" parameter above.
875 static int open_sha1_file(const unsigned char *sha1
, const char **path
)
878 struct alternate_object_database
*alt
;
879 int most_interesting_errno
;
881 *path
= sha1_file_name(sha1
);
882 fd
= git_open(*path
);
885 most_interesting_errno
= errno
;
888 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
889 *path
= alt_sha1_path(alt
, sha1
);
890 fd
= git_open(*path
);
893 if (most_interesting_errno
== ENOENT
)
894 most_interesting_errno
= errno
;
896 errno
= most_interesting_errno
;
901 * Map the loose object at "path" if it is not NULL, or the path found by
902 * searching for a loose object named "sha1".
904 static void *map_sha1_file_1(const char *path
,
905 const unsigned char *sha1
,
914 fd
= open_sha1_file(sha1
, &path
);
919 if (!fstat(fd
, &st
)) {
920 *size
= xsize_t(st
.st_size
);
922 /* mmap() is forbidden on empty files */
923 error("object file %s is empty", path
);
926 map
= xmmap(NULL
, *size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
933 void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
935 return map_sha1_file_1(NULL
, sha1
, size
);
938 static int unpack_sha1_short_header(git_zstream
*stream
,
939 unsigned char *map
, unsigned long mapsize
,
940 void *buffer
, unsigned long bufsiz
)
942 /* Get the data stream */
943 memset(stream
, 0, sizeof(*stream
));
944 stream
->next_in
= map
;
945 stream
->avail_in
= mapsize
;
946 stream
->next_out
= buffer
;
947 stream
->avail_out
= bufsiz
;
949 git_inflate_init(stream
);
950 return git_inflate(stream
, 0);
953 int unpack_sha1_header(git_zstream
*stream
,
954 unsigned char *map
, unsigned long mapsize
,
955 void *buffer
, unsigned long bufsiz
)
957 int status
= unpack_sha1_short_header(stream
, map
, mapsize
,
963 /* Make sure we have the terminating NUL */
964 if (!memchr(buffer
, '\0', stream
->next_out
- (unsigned char *)buffer
))
969 static int unpack_sha1_header_to_strbuf(git_zstream
*stream
, unsigned char *map
,
970 unsigned long mapsize
, void *buffer
,
971 unsigned long bufsiz
, struct strbuf
*header
)
975 status
= unpack_sha1_short_header(stream
, map
, mapsize
, buffer
, bufsiz
);
980 * Check if entire header is unpacked in the first iteration.
982 if (memchr(buffer
, '\0', stream
->next_out
- (unsigned char *)buffer
))
986 * buffer[0..bufsiz] was not large enough. Copy the partial
987 * result out to header, and then append the result of further
988 * reading the stream.
990 strbuf_add(header
, buffer
, stream
->next_out
- (unsigned char *)buffer
);
991 stream
->next_out
= buffer
;
992 stream
->avail_out
= bufsiz
;
995 status
= git_inflate(stream
, 0);
996 strbuf_add(header
, buffer
, stream
->next_out
- (unsigned char *)buffer
);
997 if (memchr(buffer
, '\0', stream
->next_out
- (unsigned char *)buffer
))
999 stream
->next_out
= buffer
;
1000 stream
->avail_out
= bufsiz
;
1001 } while (status
!= Z_STREAM_END
);
1005 static void *unpack_sha1_rest(git_zstream
*stream
, void *buffer
, unsigned long size
, const unsigned char *sha1
)
1007 int bytes
= strlen(buffer
) + 1;
1008 unsigned char *buf
= xmallocz(size
);
1012 n
= stream
->total_out
- bytes
;
1015 memcpy(buf
, (char *) buffer
+ bytes
, n
);
1017 if (bytes
<= size
) {
1019 * The above condition must be (bytes <= size), not
1020 * (bytes < size). In other words, even though we
1021 * expect no more output and set avail_out to zero,
1022 * the input zlib stream may have bytes that express
1023 * "this concludes the stream", and we *do* want to
1026 * Otherwise we would not be able to test that we
1027 * consumed all the input to reach the expected size;
1028 * we also want to check that zlib tells us that all
1029 * went well with status == Z_STREAM_END at the end.
1031 stream
->next_out
= buf
+ bytes
;
1032 stream
->avail_out
= size
- bytes
;
1033 while (status
== Z_OK
)
1034 status
= git_inflate(stream
, Z_FINISH
);
1036 if (status
== Z_STREAM_END
&& !stream
->avail_in
) {
1037 git_inflate_end(stream
);
1042 error("corrupt loose object '%s'", sha1_to_hex(sha1
));
1043 else if (stream
->avail_in
)
1044 error("garbage at end of loose object '%s'",
1051 * We used to just use "sscanf()", but that's actually way
1052 * too permissive for what we want to check. So do an anal
1053 * object header parse by hand.
1055 static int parse_sha1_header_extended(const char *hdr
, struct object_info
*oi
,
1058 const char *type_buf
= hdr
;
1060 int type
, type_len
= 0;
1063 * The type can be of any size but is followed by
1075 type
= type_from_string_gently(type_buf
, type_len
, 1);
1077 strbuf_add(oi
->typename
, type_buf
, type_len
);
1079 * Set type to 0 if its an unknown object and
1080 * we're obtaining the type using '--allow-unknown-type'
1083 if ((flags
& OBJECT_INFO_ALLOW_UNKNOWN_TYPE
) && (type
< 0))
1086 die("invalid object type");
1091 * The length must follow immediately, and be in canonical
1092 * decimal format (ie "010" is not valid).
1094 size
= *hdr
++ - '0';
1099 unsigned long c
= *hdr
- '0';
1103 size
= size
* 10 + c
;
1111 * The length must be followed by a zero byte
1113 return *hdr
? -1 : type
;
1116 int parse_sha1_header(const char *hdr
, unsigned long *sizep
)
1118 struct object_info oi
= OBJECT_INFO_INIT
;
1121 return parse_sha1_header_extended(hdr
, &oi
, 0);
1124 static int sha1_loose_object_info(const unsigned char *sha1
,
1125 struct object_info
*oi
,
1129 unsigned long mapsize
;
1133 struct strbuf hdrbuf
= STRBUF_INIT
;
1134 unsigned long size_scratch
;
1136 if (oi
->delta_base_sha1
)
1137 hashclr(oi
->delta_base_sha1
);
1140 * If we don't care about type or size, then we don't
1141 * need to look inside the object at all. Note that we
1142 * do not optimize out the stat call, even if the
1143 * caller doesn't care about the disk-size, since our
1144 * return value implicitly indicates whether the
1145 * object even exists.
1147 if (!oi
->typep
&& !oi
->typename
&& !oi
->sizep
&& !oi
->contentp
) {
1150 if (stat_sha1_file(sha1
, &st
, &path
) < 0)
1153 *oi
->disk_sizep
= st
.st_size
;
1157 map
= map_sha1_file(sha1
, &mapsize
);
1162 oi
->sizep
= &size_scratch
;
1165 *oi
->disk_sizep
= mapsize
;
1166 if ((flags
& OBJECT_INFO_ALLOW_UNKNOWN_TYPE
)) {
1167 if (unpack_sha1_header_to_strbuf(&stream
, map
, mapsize
, hdr
, sizeof(hdr
), &hdrbuf
) < 0)
1168 status
= error("unable to unpack %s header with --allow-unknown-type",
1170 } else if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
1171 status
= error("unable to unpack %s header",
1175 else if (hdrbuf
.len
) {
1176 if ((status
= parse_sha1_header_extended(hdrbuf
.buf
, oi
, flags
)) < 0)
1177 status
= error("unable to parse %s header with --allow-unknown-type",
1179 } else if ((status
= parse_sha1_header_extended(hdr
, oi
, flags
)) < 0)
1180 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
1182 if (status
>= 0 && oi
->contentp
) {
1183 *oi
->contentp
= unpack_sha1_rest(&stream
, hdr
,
1185 if (!*oi
->contentp
) {
1186 git_inflate_end(&stream
);
1190 git_inflate_end(&stream
);
1192 munmap(map
, mapsize
);
1193 if (status
&& oi
->typep
)
1194 *oi
->typep
= status
;
1195 if (oi
->sizep
== &size_scratch
)
1197 strbuf_release(&hdrbuf
);
1198 oi
->whence
= OI_LOOSE
;
1199 return (status
< 0) ? status
: 0;
1202 int sha1_object_info_extended(const unsigned char *sha1
, struct object_info
*oi
, unsigned flags
)
1204 static struct object_info blank_oi
= OBJECT_INFO_INIT
;
1205 struct pack_entry e
;
1207 const unsigned char *real
= (flags
& OBJECT_INFO_LOOKUP_REPLACE
) ?
1208 lookup_replace_object(sha1
) :
1214 if (!(flags
& OBJECT_INFO_SKIP_CACHED
)) {
1215 struct cached_object
*co
= find_cached_object(real
);
1218 *(oi
->typep
) = co
->type
;
1220 *(oi
->sizep
) = co
->size
;
1222 *(oi
->disk_sizep
) = 0;
1223 if (oi
->delta_base_sha1
)
1224 hashclr(oi
->delta_base_sha1
);
1226 strbuf_addstr(oi
->typename
, typename(co
->type
));
1228 *oi
->contentp
= xmemdupz(co
->buf
, co
->size
);
1229 oi
->whence
= OI_CACHED
;
1234 if (!find_pack_entry(real
, &e
)) {
1235 /* Most likely it's a loose object. */
1236 if (!sha1_loose_object_info(real
, oi
, flags
))
1239 /* Not a loose object; someone else may have just packed it. */
1240 if (flags
& OBJECT_INFO_QUICK
) {
1243 reprepare_packed_git();
1244 if (!find_pack_entry(real
, &e
))
1249 if (oi
== &blank_oi
)
1251 * We know that the caller doesn't actually need the
1252 * information below, so return early.
1256 rtype
= packed_object_info(e
.p
, e
.offset
, oi
);
1258 mark_bad_packed_object(e
.p
, real
);
1259 return sha1_object_info_extended(real
, oi
, 0);
1260 } else if (oi
->whence
== OI_PACKED
) {
1261 oi
->u
.packed
.offset
= e
.offset
;
1262 oi
->u
.packed
.pack
= e
.p
;
1263 oi
->u
.packed
.is_delta
= (rtype
== OBJ_REF_DELTA
||
1264 rtype
== OBJ_OFS_DELTA
);
1270 /* returns enum object_type or negative */
1271 int sha1_object_info(const unsigned char *sha1
, unsigned long *sizep
)
1273 enum object_type type
;
1274 struct object_info oi
= OBJECT_INFO_INIT
;
1278 if (sha1_object_info_extended(sha1
, &oi
,
1279 OBJECT_INFO_LOOKUP_REPLACE
) < 0)
1284 static void *read_object(const unsigned char *sha1
, enum object_type
*type
,
1285 unsigned long *size
)
1287 struct object_info oi
= OBJECT_INFO_INIT
;
1291 oi
.contentp
= &content
;
1293 if (sha1_object_info_extended(sha1
, &oi
, 0) < 0)
1298 int pretend_sha1_file(void *buf
, unsigned long len
, enum object_type type
,
1299 unsigned char *sha1
)
1301 struct cached_object
*co
;
1303 hash_sha1_file(buf
, len
, typename(type
), sha1
);
1304 if (has_sha1_file(sha1
) || find_cached_object(sha1
))
1306 ALLOC_GROW(cached_objects
, cached_object_nr
+ 1, cached_object_alloc
);
1307 co
= &cached_objects
[cached_object_nr
++];
1310 co
->buf
= xmalloc(len
);
1311 memcpy(co
->buf
, buf
, len
);
1312 hashcpy(co
->sha1
, sha1
);
1317 * This function dies on corrupt objects; the callers who want to
1318 * deal with them should arrange to call read_object() and give error
1319 * messages themselves.
1321 void *read_sha1_file_extended(const unsigned char *sha1
,
1322 enum object_type
*type
,
1323 unsigned long *size
,
1327 const struct packed_git
*p
;
1330 const unsigned char *repl
= lookup_replace
? lookup_replace_object(sha1
)
1334 data
= read_object(repl
, type
, size
);
1338 if (errno
&& errno
!= ENOENT
)
1339 die_errno("failed to read object %s", sha1_to_hex(sha1
));
1341 /* die if we replaced an object with one that does not exist */
1343 die("replacement %s not found for %s",
1344 sha1_to_hex(repl
), sha1_to_hex(sha1
));
1346 if (!stat_sha1_file(repl
, &st
, &path
))
1347 die("loose object %s (stored in %s) is corrupt",
1348 sha1_to_hex(repl
), path
);
1350 if ((p
= has_packed_and_bad(repl
)) != NULL
)
1351 die("packed object %s (stored in %s) is corrupt",
1352 sha1_to_hex(repl
), p
->pack_name
);
1357 void *read_object_with_reference(const unsigned char *sha1
,
1358 const char *required_type_name
,
1359 unsigned long *size
,
1360 unsigned char *actual_sha1_return
)
1362 enum object_type type
, required_type
;
1364 unsigned long isize
;
1365 unsigned char actual_sha1
[20];
1367 required_type
= type_from_string(required_type_name
);
1368 hashcpy(actual_sha1
, sha1
);
1370 int ref_length
= -1;
1371 const char *ref_type
= NULL
;
1373 buffer
= read_sha1_file(actual_sha1
, &type
, &isize
);
1376 if (type
== required_type
) {
1378 if (actual_sha1_return
)
1379 hashcpy(actual_sha1_return
, actual_sha1
);
1382 /* Handle references */
1383 else if (type
== OBJ_COMMIT
)
1385 else if (type
== OBJ_TAG
)
1386 ref_type
= "object ";
1391 ref_length
= strlen(ref_type
);
1393 if (ref_length
+ 40 > isize
||
1394 memcmp(buffer
, ref_type
, ref_length
) ||
1395 get_sha1_hex((char *) buffer
+ ref_length
, actual_sha1
)) {
1400 /* Now we have the ID of the referred-to object in
1401 * actual_sha1. Check again. */
1405 static void write_sha1_file_prepare(const void *buf
, unsigned long len
,
1406 const char *type
, unsigned char *sha1
,
1407 char *hdr
, int *hdrlen
)
1411 /* Generate the header */
1412 *hdrlen
= xsnprintf(hdr
, *hdrlen
, "%s %lu", type
, len
)+1;
1416 git_SHA1_Update(&c
, hdr
, *hdrlen
);
1417 git_SHA1_Update(&c
, buf
, len
);
1418 git_SHA1_Final(sha1
, &c
);
1422 * Move the just written object into its final resting place.
1424 int finalize_object_file(const char *tmpfile
, const char *filename
)
1428 if (object_creation_mode
== OBJECT_CREATION_USES_RENAMES
)
1430 else if (link(tmpfile
, filename
))
1434 * Coda hack - coda doesn't like cross-directory links,
1435 * so we fall back to a rename, which will mean that it
1436 * won't be able to check collisions, but that's not a
1439 * The same holds for FAT formatted media.
1441 * When this succeeds, we just return. We have nothing
1444 if (ret
&& ret
!= EEXIST
) {
1446 if (!rename(tmpfile
, filename
))
1450 unlink_or_warn(tmpfile
);
1452 if (ret
!= EEXIST
) {
1453 return error_errno("unable to write sha1 filename %s", filename
);
1455 /* FIXME!!! Collision check here ? */
1459 if (adjust_shared_perm(filename
))
1460 return error("unable to set permission to '%s'", filename
);
1464 static int write_buffer(int fd
, const void *buf
, size_t len
)
1466 if (write_in_full(fd
, buf
, len
) < 0)
1467 return error_errno("file write error");
1471 int hash_sha1_file(const void *buf
, unsigned long len
, const char *type
,
1472 unsigned char *sha1
)
1475 int hdrlen
= sizeof(hdr
);
1476 write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
1480 /* Finalize a file on disk, and close it. */
1481 static void close_sha1_file(int fd
)
1483 if (fsync_object_files
)
1484 fsync_or_die(fd
, "sha1 file");
1486 die_errno("error when closing sha1 file");
1489 /* Size of directory component, including the ending '/' */
1490 static inline int directory_size(const char *filename
)
1492 const char *s
= strrchr(filename
, '/');
1495 return s
- filename
+ 1;
1499 * This creates a temporary file in the same directory as the final
1502 * We want to avoid cross-directory filename renames, because those
1503 * can have problems on various filesystems (FAT, NFS, Coda).
1505 static int create_tmpfile(struct strbuf
*tmp
, const char *filename
)
1507 int fd
, dirlen
= directory_size(filename
);
1510 strbuf_add(tmp
, filename
, dirlen
);
1511 strbuf_addstr(tmp
, "tmp_obj_XXXXXX");
1512 fd
= git_mkstemp_mode(tmp
->buf
, 0444);
1513 if (fd
< 0 && dirlen
&& errno
== ENOENT
) {
1515 * Make sure the directory exists; note that the contents
1516 * of the buffer are undefined after mkstemp returns an
1517 * error, so we have to rewrite the whole buffer from
1521 strbuf_add(tmp
, filename
, dirlen
- 1);
1522 if (mkdir(tmp
->buf
, 0777) && errno
!= EEXIST
)
1524 if (adjust_shared_perm(tmp
->buf
))
1528 strbuf_addstr(tmp
, "/tmp_obj_XXXXXX");
1529 fd
= git_mkstemp_mode(tmp
->buf
, 0444);
1534 static int write_loose_object(const unsigned char *sha1
, char *hdr
, int hdrlen
,
1535 const void *buf
, unsigned long len
, time_t mtime
)
1538 unsigned char compressed
[4096];
1541 unsigned char parano_sha1
[20];
1542 static struct strbuf tmp_file
= STRBUF_INIT
;
1543 const char *filename
= sha1_file_name(sha1
);
1545 fd
= create_tmpfile(&tmp_file
, filename
);
1547 if (errno
== EACCES
)
1548 return error("insufficient permission for adding an object to repository database %s", get_object_directory());
1550 return error_errno("unable to create temporary file");
1554 git_deflate_init(&stream
, zlib_compression_level
);
1555 stream
.next_out
= compressed
;
1556 stream
.avail_out
= sizeof(compressed
);
1559 /* First header.. */
1560 stream
.next_in
= (unsigned char *)hdr
;
1561 stream
.avail_in
= hdrlen
;
1562 while (git_deflate(&stream
, 0) == Z_OK
)
1564 git_SHA1_Update(&c
, hdr
, hdrlen
);
1566 /* Then the data itself.. */
1567 stream
.next_in
= (void *)buf
;
1568 stream
.avail_in
= len
;
1570 unsigned char *in0
= stream
.next_in
;
1571 ret
= git_deflate(&stream
, Z_FINISH
);
1572 git_SHA1_Update(&c
, in0
, stream
.next_in
- in0
);
1573 if (write_buffer(fd
, compressed
, stream
.next_out
- compressed
) < 0)
1574 die("unable to write sha1 file");
1575 stream
.next_out
= compressed
;
1576 stream
.avail_out
= sizeof(compressed
);
1577 } while (ret
== Z_OK
);
1579 if (ret
!= Z_STREAM_END
)
1580 die("unable to deflate new object %s (%d)", sha1_to_hex(sha1
), ret
);
1581 ret
= git_deflate_end_gently(&stream
);
1583 die("deflateEnd on object %s failed (%d)", sha1_to_hex(sha1
), ret
);
1584 git_SHA1_Final(parano_sha1
, &c
);
1585 if (hashcmp(sha1
, parano_sha1
) != 0)
1586 die("confused by unstable object source data for %s", sha1_to_hex(sha1
));
1588 close_sha1_file(fd
);
1593 utb
.modtime
= mtime
;
1594 if (utime(tmp_file
.buf
, &utb
) < 0)
1595 warning_errno("failed utime() on %s", tmp_file
.buf
);
1598 return finalize_object_file(tmp_file
.buf
, filename
);
1601 static int freshen_loose_object(const unsigned char *sha1
)
1603 return check_and_freshen(sha1
, 1);
1606 static int freshen_packed_object(const unsigned char *sha1
)
1608 struct pack_entry e
;
1609 if (!find_pack_entry(sha1
, &e
))
1613 if (!freshen_file(e
.p
->pack_name
))
1619 int write_sha1_file(const void *buf
, unsigned long len
, const char *type
, unsigned char *sha1
)
1622 int hdrlen
= sizeof(hdr
);
1624 /* Normally if we have it in the pack then we do not bother writing
1625 * it out into .git/objects/??/?{38} file.
1627 write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
1628 if (freshen_packed_object(sha1
) || freshen_loose_object(sha1
))
1630 return write_loose_object(sha1
, hdr
, hdrlen
, buf
, len
, 0);
1633 int hash_sha1_file_literally(const void *buf
, unsigned long len
, const char *type
,
1634 struct object_id
*oid
, unsigned flags
)
1637 int hdrlen
, status
= 0;
1639 /* type string, SP, %lu of the length plus NUL must fit this */
1640 hdrlen
= strlen(type
) + 32;
1641 header
= xmalloc(hdrlen
);
1642 write_sha1_file_prepare(buf
, len
, type
, oid
->hash
, header
, &hdrlen
);
1644 if (!(flags
& HASH_WRITE_OBJECT
))
1646 if (freshen_packed_object(oid
->hash
) || freshen_loose_object(oid
->hash
))
1648 status
= write_loose_object(oid
->hash
, header
, hdrlen
, buf
, len
, 0);
1655 int force_object_loose(const unsigned char *sha1
, time_t mtime
)
1659 enum object_type type
;
1664 if (has_loose_object(sha1
))
1666 buf
= read_object(sha1
, &type
, &len
);
1668 return error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
1669 hdrlen
= xsnprintf(hdr
, sizeof(hdr
), "%s %lu", typename(type
), len
) + 1;
1670 ret
= write_loose_object(sha1
, hdr
, hdrlen
, buf
, len
, mtime
);
1676 int has_sha1_file_with_flags(const unsigned char *sha1
, int flags
)
1678 if (!startup_info
->have_repository
)
1680 return sha1_object_info_extended(sha1
, NULL
,
1681 flags
| OBJECT_INFO_SKIP_CACHED
) >= 0;
1684 int has_object_file(const struct object_id
*oid
)
1686 return has_sha1_file(oid
->hash
);
1689 int has_object_file_with_flags(const struct object_id
*oid
, int flags
)
1691 return has_sha1_file_with_flags(oid
->hash
, flags
);
1694 static void check_tree(const void *buf
, size_t size
)
1696 struct tree_desc desc
;
1697 struct name_entry entry
;
1699 init_tree_desc(&desc
, buf
, size
);
1700 while (tree_entry(&desc
, &entry
))
1702 * tree_entry() will die() on malformed entries */
1706 static void check_commit(const void *buf
, size_t size
)
1709 memset(&c
, 0, sizeof(c
));
1710 if (parse_commit_buffer(&c
, buf
, size
))
1711 die("corrupt commit");
1714 static void check_tag(const void *buf
, size_t size
)
1717 memset(&t
, 0, sizeof(t
));
1718 if (parse_tag_buffer(&t
, buf
, size
))
1722 static int index_mem(struct object_id
*oid
, void *buf
, size_t size
,
1723 enum object_type type
,
1724 const char *path
, unsigned flags
)
1726 int ret
, re_allocated
= 0;
1727 int write_object
= flags
& HASH_WRITE_OBJECT
;
1733 * Convert blobs to git internal format
1735 if ((type
== OBJ_BLOB
) && path
) {
1736 struct strbuf nbuf
= STRBUF_INIT
;
1737 if (convert_to_git(&the_index
, path
, buf
, size
, &nbuf
,
1738 write_object
? safe_crlf
: SAFE_CRLF_FALSE
)) {
1739 buf
= strbuf_detach(&nbuf
, &size
);
1743 if (flags
& HASH_FORMAT_CHECK
) {
1744 if (type
== OBJ_TREE
)
1745 check_tree(buf
, size
);
1746 if (type
== OBJ_COMMIT
)
1747 check_commit(buf
, size
);
1748 if (type
== OBJ_TAG
)
1749 check_tag(buf
, size
);
1753 ret
= write_sha1_file(buf
, size
, typename(type
), oid
->hash
);
1755 ret
= hash_sha1_file(buf
, size
, typename(type
), oid
->hash
);
1761 static int index_stream_convert_blob(struct object_id
*oid
, int fd
,
1762 const char *path
, unsigned flags
)
1765 const int write_object
= flags
& HASH_WRITE_OBJECT
;
1766 struct strbuf sbuf
= STRBUF_INIT
;
1769 assert(would_convert_to_git_filter_fd(path
));
1771 convert_to_git_filter_fd(&the_index
, path
, fd
, &sbuf
,
1772 write_object
? safe_crlf
: SAFE_CRLF_FALSE
);
1775 ret
= write_sha1_file(sbuf
.buf
, sbuf
.len
, typename(OBJ_BLOB
),
1778 ret
= hash_sha1_file(sbuf
.buf
, sbuf
.len
, typename(OBJ_BLOB
),
1780 strbuf_release(&sbuf
);
1784 static int index_pipe(struct object_id
*oid
, int fd
, enum object_type type
,
1785 const char *path
, unsigned flags
)
1787 struct strbuf sbuf
= STRBUF_INIT
;
1790 if (strbuf_read(&sbuf
, fd
, 4096) >= 0)
1791 ret
= index_mem(oid
, sbuf
.buf
, sbuf
.len
, type
, path
, flags
);
1794 strbuf_release(&sbuf
);
1798 #define SMALL_FILE_SIZE (32*1024)
1800 static int index_core(struct object_id
*oid
, int fd
, size_t size
,
1801 enum object_type type
, const char *path
,
1807 ret
= index_mem(oid
, "", size
, type
, path
, flags
);
1808 } else if (size
<= SMALL_FILE_SIZE
) {
1809 char *buf
= xmalloc(size
);
1810 ssize_t read_result
= read_in_full(fd
, buf
, size
);
1811 if (read_result
< 0)
1812 ret
= error_errno("read error while indexing %s",
1813 path
? path
: "<unknown>");
1814 else if (read_result
!= size
)
1815 ret
= error("short read while indexing %s",
1816 path
? path
: "<unknown>");
1818 ret
= index_mem(oid
, buf
, size
, type
, path
, flags
);
1821 void *buf
= xmmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1822 ret
= index_mem(oid
, buf
, size
, type
, path
, flags
);
1829 * This creates one packfile per large blob unless bulk-checkin
1830 * machinery is "plugged".
1832 * This also bypasses the usual "convert-to-git" dance, and that is on
1833 * purpose. We could write a streaming version of the converting
1834 * functions and insert that before feeding the data to fast-import
1835 * (or equivalent in-core API described above). However, that is
1836 * somewhat complicated, as we do not know the size of the filter
1837 * result, which we need to know beforehand when writing a git object.
1838 * Since the primary motivation for trying to stream from the working
1839 * tree file and to avoid mmaping it in core is to deal with large
1840 * binary blobs, they generally do not want to get any conversion, and
1841 * callers should avoid this code path when filters are requested.
1843 static int index_stream(struct object_id
*oid
, int fd
, size_t size
,
1844 enum object_type type
, const char *path
,
1847 return index_bulk_checkin(oid
->hash
, fd
, size
, type
, path
, flags
);
1850 int index_fd(struct object_id
*oid
, int fd
, struct stat
*st
,
1851 enum object_type type
, const char *path
, unsigned flags
)
1856 * Call xsize_t() only when needed to avoid potentially unnecessary
1857 * die() for large files.
1859 if (type
== OBJ_BLOB
&& path
&& would_convert_to_git_filter_fd(path
))
1860 ret
= index_stream_convert_blob(oid
, fd
, path
, flags
);
1861 else if (!S_ISREG(st
->st_mode
))
1862 ret
= index_pipe(oid
, fd
, type
, path
, flags
);
1863 else if (st
->st_size
<= big_file_threshold
|| type
!= OBJ_BLOB
||
1864 (path
&& would_convert_to_git(&the_index
, path
)))
1865 ret
= index_core(oid
, fd
, xsize_t(st
->st_size
), type
, path
,
1868 ret
= index_stream(oid
, fd
, xsize_t(st
->st_size
), type
, path
,
1874 int index_path(struct object_id
*oid
, const char *path
, struct stat
*st
, unsigned flags
)
1877 struct strbuf sb
= STRBUF_INIT
;
1880 switch (st
->st_mode
& S_IFMT
) {
1882 fd
= open(path
, O_RDONLY
);
1884 return error_errno("open(\"%s\")", path
);
1885 if (index_fd(oid
, fd
, st
, OBJ_BLOB
, path
, flags
) < 0)
1886 return error("%s: failed to insert into database",
1890 if (strbuf_readlink(&sb
, path
, st
->st_size
))
1891 return error_errno("readlink(\"%s\")", path
);
1892 if (!(flags
& HASH_WRITE_OBJECT
))
1893 hash_sha1_file(sb
.buf
, sb
.len
, blob_type
, oid
->hash
);
1894 else if (write_sha1_file(sb
.buf
, sb
.len
, blob_type
, oid
->hash
))
1895 rc
= error("%s: failed to insert into database", path
);
1896 strbuf_release(&sb
);
1899 return resolve_gitlink_ref(path
, "HEAD", oid
);
1901 return error("%s: unsupported file type", path
);
1906 int read_pack_header(int fd
, struct pack_header
*header
)
1908 if (read_in_full(fd
, header
, sizeof(*header
)) != sizeof(*header
))
1909 /* "eof before pack header was fully read" */
1910 return PH_ERROR_EOF
;
1912 if (header
->hdr_signature
!= htonl(PACK_SIGNATURE
))
1913 /* "protocol error (pack signature mismatch detected)" */
1914 return PH_ERROR_PACK_SIGNATURE
;
1915 if (!pack_version_ok(header
->hdr_version
))
1916 /* "protocol error (pack version unsupported)" */
1917 return PH_ERROR_PROTOCOL
;
1921 void assert_sha1_type(const unsigned char *sha1
, enum object_type expect
)
1923 enum object_type type
= sha1_object_info(sha1
, NULL
);
1925 die("%s is not a valid object", sha1_to_hex(sha1
));
1927 die("%s is not a valid '%s' object", sha1_to_hex(sha1
),
1931 int for_each_file_in_obj_subdir(unsigned int subdir_nr
,
1932 struct strbuf
*path
,
1933 each_loose_object_fn obj_cb
,
1934 each_loose_cruft_fn cruft_cb
,
1935 each_loose_subdir_fn subdir_cb
,
1938 size_t origlen
, baselen
;
1942 struct object_id oid
;
1944 if (subdir_nr
> 0xff)
1945 BUG("invalid loose object subdirectory: %x", subdir_nr
);
1947 origlen
= path
->len
;
1948 strbuf_complete(path
, '/');
1949 strbuf_addf(path
, "%02x", subdir_nr
);
1950 baselen
= path
->len
;
1952 dir
= opendir(path
->buf
);
1954 if (errno
!= ENOENT
)
1955 r
= error_errno("unable to open %s", path
->buf
);
1956 strbuf_setlen(path
, origlen
);
1960 oid
.hash
[0] = subdir_nr
;
1962 while ((de
= readdir(dir
))) {
1963 if (is_dot_or_dotdot(de
->d_name
))
1966 strbuf_setlen(path
, baselen
);
1967 strbuf_addf(path
, "/%s", de
->d_name
);
1969 if (strlen(de
->d_name
) == GIT_SHA1_HEXSZ
- 2 &&
1970 !hex_to_bytes(oid
.hash
+ 1, de
->d_name
,
1971 GIT_SHA1_RAWSZ
- 1)) {
1973 r
= obj_cb(&oid
, path
->buf
, data
);
1981 r
= cruft_cb(de
->d_name
, path
->buf
, data
);
1988 strbuf_setlen(path
, baselen
);
1989 if (!r
&& subdir_cb
)
1990 r
= subdir_cb(subdir_nr
, path
->buf
, data
);
1992 strbuf_setlen(path
, origlen
);
1997 int for_each_loose_file_in_objdir_buf(struct strbuf
*path
,
1998 each_loose_object_fn obj_cb
,
1999 each_loose_cruft_fn cruft_cb
,
2000 each_loose_subdir_fn subdir_cb
,
2006 for (i
= 0; i
< 256; i
++) {
2007 r
= for_each_file_in_obj_subdir(i
, path
, obj_cb
, cruft_cb
,
2016 int for_each_loose_file_in_objdir(const char *path
,
2017 each_loose_object_fn obj_cb
,
2018 each_loose_cruft_fn cruft_cb
,
2019 each_loose_subdir_fn subdir_cb
,
2022 struct strbuf buf
= STRBUF_INIT
;
2025 strbuf_addstr(&buf
, path
);
2026 r
= for_each_loose_file_in_objdir_buf(&buf
, obj_cb
, cruft_cb
,
2028 strbuf_release(&buf
);
2033 struct loose_alt_odb_data
{
2034 each_loose_object_fn
*cb
;
2038 static int loose_from_alt_odb(struct alternate_object_database
*alt
,
2041 struct loose_alt_odb_data
*data
= vdata
;
2042 struct strbuf buf
= STRBUF_INIT
;
2045 strbuf_addstr(&buf
, alt
->path
);
2046 r
= for_each_loose_file_in_objdir_buf(&buf
,
2047 data
->cb
, NULL
, NULL
,
2049 strbuf_release(&buf
);
2053 int for_each_loose_object(each_loose_object_fn cb
, void *data
, unsigned flags
)
2055 struct loose_alt_odb_data alt
;
2058 r
= for_each_loose_file_in_objdir(get_object_directory(),
2059 cb
, NULL
, NULL
, data
);
2063 if (flags
& FOR_EACH_OBJECT_LOCAL_ONLY
)
2068 return foreach_alt_odb(loose_from_alt_odb
, &alt
);
2071 static int check_stream_sha1(git_zstream
*stream
,
2075 const unsigned char *expected_sha1
)
2078 unsigned char real_sha1
[GIT_MAX_RAWSZ
];
2079 unsigned char buf
[4096];
2080 unsigned long total_read
;
2084 git_SHA1_Update(&c
, hdr
, stream
->total_out
);
2087 * We already read some bytes into hdr, but the ones up to the NUL
2088 * do not count against the object's content size.
2090 total_read
= stream
->total_out
- strlen(hdr
) - 1;
2093 * This size comparison must be "<=" to read the final zlib packets;
2094 * see the comment in unpack_sha1_rest for details.
2096 while (total_read
<= size
&&
2097 (status
== Z_OK
|| status
== Z_BUF_ERROR
)) {
2098 stream
->next_out
= buf
;
2099 stream
->avail_out
= sizeof(buf
);
2100 if (size
- total_read
< stream
->avail_out
)
2101 stream
->avail_out
= size
- total_read
;
2102 status
= git_inflate(stream
, Z_FINISH
);
2103 git_SHA1_Update(&c
, buf
, stream
->next_out
- buf
);
2104 total_read
+= stream
->next_out
- buf
;
2106 git_inflate_end(stream
);
2108 if (status
!= Z_STREAM_END
) {
2109 error("corrupt loose object '%s'", sha1_to_hex(expected_sha1
));
2112 if (stream
->avail_in
) {
2113 error("garbage at end of loose object '%s'",
2114 sha1_to_hex(expected_sha1
));
2118 git_SHA1_Final(real_sha1
, &c
);
2119 if (hashcmp(expected_sha1
, real_sha1
)) {
2120 error("sha1 mismatch for %s (expected %s)", path
,
2121 sha1_to_hex(expected_sha1
));
2128 int read_loose_object(const char *path
,
2129 const unsigned char *expected_sha1
,
2130 enum object_type
*type
,
2131 unsigned long *size
,
2136 unsigned long mapsize
;
2142 map
= map_sha1_file_1(path
, NULL
, &mapsize
);
2144 error_errno("unable to mmap %s", path
);
2148 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0) {
2149 error("unable to unpack header of %s", path
);
2153 *type
= parse_sha1_header(hdr
, size
);
2155 error("unable to parse header of %s", path
);
2156 git_inflate_end(&stream
);
2160 if (*type
== OBJ_BLOB
) {
2161 if (check_stream_sha1(&stream
, hdr
, *size
, path
, expected_sha1
) < 0)
2164 *contents
= unpack_sha1_rest(&stream
, hdr
, *size
, expected_sha1
);
2166 error("unable to unpack contents of %s", path
);
2167 git_inflate_end(&stream
);
2170 if (check_sha1_signature(expected_sha1
, *contents
,
2171 *size
, typename(*type
))) {
2172 error("sha1 mismatch for %s (expected %s)", path
,
2173 sha1_to_hex(expected_sha1
));
2179 ret
= 0; /* everything checks out */
2183 munmap(map
, mapsize
);