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 "repository.h"
26 #include "replace-object.h"
27 #include "streaming.h"
30 #include "mergesort.h"
33 #include "fetch-object.h"
34 #include "object-store.h"
36 /* The maximum size for an object header. */
37 #define MAX_HEADER_LEN 32
40 #define EMPTY_TREE_SHA1_BIN_LITERAL \
41 "\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60" \
42 "\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04"
44 #define EMPTY_BLOB_SHA1_BIN_LITERAL \
45 "\xe6\x9d\xe2\x9b\xb2\xd1\xd6\x43\x4b\x8b" \
46 "\x29\xae\x77\x5a\xd8\xc2\xe4\x8c\x53\x91"
48 const unsigned char null_sha1
[GIT_MAX_RAWSZ
];
49 const struct object_id null_oid
;
50 static const struct object_id empty_tree_oid
= {
51 EMPTY_TREE_SHA1_BIN_LITERAL
53 static const struct object_id empty_blob_oid
= {
54 EMPTY_BLOB_SHA1_BIN_LITERAL
57 static void git_hash_sha1_init(git_hash_ctx
*ctx
)
59 git_SHA1_Init(&ctx
->sha1
);
62 static void git_hash_sha1_update(git_hash_ctx
*ctx
, const void *data
, size_t len
)
64 git_SHA1_Update(&ctx
->sha1
, data
, len
);
67 static void git_hash_sha1_final(unsigned char *hash
, git_hash_ctx
*ctx
)
69 git_SHA1_Final(hash
, &ctx
->sha1
);
72 static void git_hash_unknown_init(git_hash_ctx
*ctx
)
74 BUG("trying to init unknown hash");
77 static void git_hash_unknown_update(git_hash_ctx
*ctx
, const void *data
, size_t len
)
79 BUG("trying to update unknown hash");
82 static void git_hash_unknown_final(unsigned char *hash
, git_hash_ctx
*ctx
)
84 BUG("trying to finalize unknown hash");
87 const struct git_hash_algo hash_algos
[GIT_HASH_NALGOS
] = {
93 git_hash_unknown_init
,
94 git_hash_unknown_update
,
95 git_hash_unknown_final
,
101 /* "sha1", big-endian */
106 git_hash_sha1_update
,
113 const char *empty_tree_oid_hex(void)
115 static char buf
[GIT_MAX_HEXSZ
+ 1];
116 return oid_to_hex_r(buf
, the_hash_algo
->empty_tree
);
119 const char *empty_blob_oid_hex(void)
121 static char buf
[GIT_MAX_HEXSZ
+ 1];
122 return oid_to_hex_r(buf
, the_hash_algo
->empty_blob
);
126 * This is meant to hold a *small* number of objects that you would
127 * want read_sha1_file() to be able to return, but yet you do not want
128 * to write them into the object store (e.g. a browse-only
131 static struct cached_object
{
132 struct object_id oid
;
133 enum object_type type
;
137 static int cached_object_nr
, cached_object_alloc
;
139 static struct cached_object empty_tree
= {
140 { EMPTY_TREE_SHA1_BIN_LITERAL
},
146 static struct cached_object
*find_cached_object(const struct object_id
*oid
)
149 struct cached_object
*co
= cached_objects
;
151 for (i
= 0; i
< cached_object_nr
; i
++, co
++) {
152 if (oideq(&co
->oid
, oid
))
155 if (oideq(oid
, the_hash_algo
->empty_tree
))
161 static int get_conv_flags(unsigned flags
)
163 if (flags
& HASH_RENORMALIZE
)
164 return CONV_EOL_RENORMALIZE
;
165 else if (flags
& HASH_WRITE_OBJECT
)
166 return global_conv_flags_eol
| CONV_WRITE_OBJECT
;
172 int mkdir_in_gitdir(const char *path
)
174 if (mkdir(path
, 0777)) {
175 int saved_errno
= errno
;
177 struct strbuf sb
= STRBUF_INIT
;
182 * Are we looking at a path in a symlinked worktree
183 * whose original repository does not yet have it?
184 * e.g. .git/rr-cache pointing at its original
185 * repository in which the user hasn't performed any
186 * conflict resolution yet?
188 if (lstat(path
, &st
) || !S_ISLNK(st
.st_mode
) ||
189 strbuf_readlink(&sb
, path
, st
.st_size
) ||
190 !is_absolute_path(sb
.buf
) ||
191 mkdir(sb
.buf
, 0777)) {
198 return adjust_shared_perm(path
);
201 enum scld_error
safe_create_leading_directories(char *path
)
203 char *next_component
= path
+ offset_1st_component(path
);
204 enum scld_error ret
= SCLD_OK
;
206 while (ret
== SCLD_OK
&& next_component
) {
208 char *slash
= next_component
, slash_character
;
210 while (*slash
&& !is_dir_sep(*slash
))
216 next_component
= slash
+ 1;
217 while (is_dir_sep(*next_component
))
219 if (!*next_component
)
222 slash_character
= *slash
;
224 if (!stat(path
, &st
)) {
226 if (!S_ISDIR(st
.st_mode
)) {
230 } else if (mkdir(path
, 0777)) {
231 if (errno
== EEXIST
&&
232 !stat(path
, &st
) && S_ISDIR(st
.st_mode
))
233 ; /* somebody created it since we checked */
234 else if (errno
== ENOENT
)
236 * Either mkdir() failed because
237 * somebody just pruned the containing
238 * directory, or stat() failed because
239 * the file that was in our way was
240 * just removed. Either way, inform
241 * the caller that it might be worth
247 } else if (adjust_shared_perm(path
)) {
250 *slash
= slash_character
;
255 enum scld_error
safe_create_leading_directories_const(const char *path
)
258 /* path points to cache entries, so xstrdup before messing with it */
259 char *buf
= xstrdup(path
);
260 enum scld_error result
= safe_create_leading_directories(buf
);
268 int raceproof_create_file(const char *path
, create_file_fn fn
, void *cb
)
271 * The number of times we will try to remove empty directories
272 * in the way of path. This is only 1 because if another
273 * process is racily creating directories that conflict with
274 * us, we don't want to fight against them.
276 int remove_directories_remaining
= 1;
279 * The number of times that we will try to create the
280 * directories containing path. We are willing to attempt this
281 * more than once, because another process could be trying to
282 * clean up empty directories at the same time as we are
283 * trying to create them.
285 int create_directories_remaining
= 3;
287 /* A scratch copy of path, filled lazily if we need it: */
288 struct strbuf path_copy
= STRBUF_INIT
;
301 if (errno
== EISDIR
&& remove_directories_remaining
-- > 0) {
303 * A directory is in the way. Maybe it is empty; try
307 strbuf_addstr(&path_copy
, path
);
309 if (!remove_dir_recursively(&path_copy
, REMOVE_DIR_EMPTY_ONLY
))
311 } else if (errno
== ENOENT
&& create_directories_remaining
-- > 0) {
313 * Maybe the containing directory didn't exist, or
314 * maybe it was just deleted by a process that is
315 * racing with us to clean up empty directories. Try
318 enum scld_error scld_result
;
321 strbuf_addstr(&path_copy
, path
);
324 scld_result
= safe_create_leading_directories(path_copy
.buf
);
325 if (scld_result
== SCLD_OK
)
327 } while (scld_result
== SCLD_VANISHED
&& create_directories_remaining
-- > 0);
331 strbuf_release(&path_copy
);
336 static void fill_sha1_path(struct strbuf
*buf
, const unsigned char *sha1
)
339 for (i
= 0; i
< the_hash_algo
->rawsz
; i
++) {
340 static char hex
[] = "0123456789abcdef";
341 unsigned int val
= sha1
[i
];
342 strbuf_addch(buf
, hex
[val
>> 4]);
343 strbuf_addch(buf
, hex
[val
& 0xf]);
345 strbuf_addch(buf
, '/');
349 void sha1_file_name(struct repository
*r
, struct strbuf
*buf
, const unsigned char *sha1
)
351 strbuf_addstr(buf
, r
->objects
->objectdir
);
352 strbuf_addch(buf
, '/');
353 fill_sha1_path(buf
, sha1
);
356 struct strbuf
*alt_scratch_buf(struct alternate_object_database
*alt
)
358 strbuf_setlen(&alt
->scratch
, alt
->base_len
);
359 return &alt
->scratch
;
362 static const char *alt_sha1_path(struct alternate_object_database
*alt
,
363 const unsigned char *sha1
)
365 struct strbuf
*buf
= alt_scratch_buf(alt
);
366 fill_sha1_path(buf
, sha1
);
371 * Return non-zero iff the path is usable as an alternate object database.
373 static int alt_odb_usable(struct raw_object_store
*o
,
375 const char *normalized_objdir
)
377 struct alternate_object_database
*alt
;
379 /* Detect cases where alternate disappeared */
380 if (!is_directory(path
->buf
)) {
381 error(_("object directory %s does not exist; "
382 "check .git/objects/info/alternates"),
388 * Prevent the common mistake of listing the same
389 * thing twice, or object directory itself.
391 for (alt
= o
->alt_odb_list
; alt
; alt
= alt
->next
) {
392 if (!fspathcmp(path
->buf
, alt
->path
))
395 if (!fspathcmp(path
->buf
, normalized_objdir
))
402 * Prepare alternate object database registry.
404 * The variable alt_odb_list points at the list of struct
405 * alternate_object_database. The elements on this list come from
406 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
407 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
408 * whose contents is similar to that environment variable but can be
409 * LF separated. Its base points at a statically allocated buffer that
410 * contains "/the/directory/corresponding/to/.git/objects/...", while
411 * its name points just after the slash at the end of ".git/objects/"
412 * in the example above, and has enough space to hold 40-byte hex
413 * SHA1, an extra slash for the first level indirection, and the
416 static void read_info_alternates(struct repository
*r
,
417 const char *relative_base
,
419 static int link_alt_odb_entry(struct repository
*r
, const char *entry
,
420 const char *relative_base
, int depth
, const char *normalized_objdir
)
422 struct alternate_object_database
*ent
;
423 struct strbuf pathbuf
= STRBUF_INIT
;
425 if (!is_absolute_path(entry
) && relative_base
) {
426 strbuf_realpath(&pathbuf
, relative_base
, 1);
427 strbuf_addch(&pathbuf
, '/');
429 strbuf_addstr(&pathbuf
, entry
);
431 if (strbuf_normalize_path(&pathbuf
) < 0 && relative_base
) {
432 error(_("unable to normalize alternate object path: %s"),
434 strbuf_release(&pathbuf
);
439 * The trailing slash after the directory name is given by
440 * this function at the end. Remove duplicates.
442 while (pathbuf
.len
&& pathbuf
.buf
[pathbuf
.len
- 1] == '/')
443 strbuf_setlen(&pathbuf
, pathbuf
.len
- 1);
445 if (!alt_odb_usable(r
->objects
, &pathbuf
, normalized_objdir
)) {
446 strbuf_release(&pathbuf
);
450 ent
= alloc_alt_odb(pathbuf
.buf
);
452 /* add the alternate entry */
453 *r
->objects
->alt_odb_tail
= ent
;
454 r
->objects
->alt_odb_tail
= &(ent
->next
);
457 /* recursively add alternates */
458 read_info_alternates(r
, pathbuf
.buf
, depth
+ 1);
460 strbuf_release(&pathbuf
);
464 static const char *parse_alt_odb_entry(const char *string
,
472 if (*string
== '#') {
473 /* comment; consume up to next separator */
474 end
= strchrnul(string
, sep
);
475 } else if (*string
== '"' && !unquote_c_style(out
, string
, &end
)) {
477 * quoted path; unquote_c_style has copied the
478 * data for us and set "end". Broken quoting (e.g.,
479 * an entry that doesn't end with a quote) falls
480 * back to the unquoted case below.
483 /* normal, unquoted path */
484 end
= strchrnul(string
, sep
);
485 strbuf_add(out
, string
, end
- string
);
493 static void link_alt_odb_entries(struct repository
*r
, const char *alt
,
494 int sep
, const char *relative_base
, int depth
)
496 struct strbuf objdirbuf
= STRBUF_INIT
;
497 struct strbuf entry
= STRBUF_INIT
;
503 error(_("%s: ignoring alternate object stores, nesting too deep"),
508 strbuf_add_absolute_path(&objdirbuf
, r
->objects
->objectdir
);
509 if (strbuf_normalize_path(&objdirbuf
) < 0)
510 die(_("unable to normalize object directory: %s"),
514 alt
= parse_alt_odb_entry(alt
, sep
, &entry
);
517 link_alt_odb_entry(r
, entry
.buf
,
518 relative_base
, depth
, objdirbuf
.buf
);
520 strbuf_release(&entry
);
521 strbuf_release(&objdirbuf
);
524 static void read_info_alternates(struct repository
*r
,
525 const char *relative_base
,
529 struct strbuf buf
= STRBUF_INIT
;
531 path
= xstrfmt("%s/info/alternates", relative_base
);
532 if (strbuf_read_file(&buf
, path
, 1024) < 0) {
533 warn_on_fopen_errors(path
);
538 link_alt_odb_entries(r
, buf
.buf
, '\n', relative_base
, depth
);
539 strbuf_release(&buf
);
543 struct alternate_object_database
*alloc_alt_odb(const char *dir
)
545 struct alternate_object_database
*ent
;
547 FLEX_ALLOC_STR(ent
, path
, dir
);
548 strbuf_init(&ent
->scratch
, 0);
549 strbuf_addf(&ent
->scratch
, "%s/", dir
);
550 ent
->base_len
= ent
->scratch
.len
;
555 void add_to_alternates_file(const char *reference
)
557 struct lock_file lock
= LOCK_INIT
;
558 char *alts
= git_pathdup("objects/info/alternates");
562 hold_lock_file_for_update(&lock
, alts
, LOCK_DIE_ON_ERROR
);
563 out
= fdopen_lock_file(&lock
, "w");
565 die_errno(_("unable to fdopen alternates lockfile"));
567 in
= fopen(alts
, "r");
569 struct strbuf line
= STRBUF_INIT
;
571 while (strbuf_getline(&line
, in
) != EOF
) {
572 if (!strcmp(reference
, line
.buf
)) {
576 fprintf_or_die(out
, "%s\n", line
.buf
);
579 strbuf_release(&line
);
582 else if (errno
!= ENOENT
)
583 die_errno(_("unable to read alternates file"));
586 rollback_lock_file(&lock
);
588 fprintf_or_die(out
, "%s\n", reference
);
589 if (commit_lock_file(&lock
))
590 die_errno(_("unable to move new alternates file into place"));
591 if (the_repository
->objects
->alt_odb_tail
)
592 link_alt_odb_entries(the_repository
, reference
,
598 void add_to_alternates_memory(const char *reference
)
601 * Make sure alternates are initialized, or else our entry may be
602 * overwritten when they are.
604 prepare_alt_odb(the_repository
);
606 link_alt_odb_entries(the_repository
, reference
,
611 * Compute the exact path an alternate is at and returns it. In case of
612 * error NULL is returned and the human readable error is added to `err`
613 * `path` may be relative and should point to $GIT_DIR.
614 * `err` must not be null.
616 char *compute_alternate_path(const char *path
, struct strbuf
*err
)
618 char *ref_git
= NULL
;
619 const char *repo
, *ref_git_s
;
622 ref_git_s
= real_path_if_valid(path
);
625 strbuf_addf(err
, _("path '%s' does not exist"), path
);
629 * Beware: read_gitfile(), real_path() and mkpath()
630 * return static buffer
632 ref_git
= xstrdup(ref_git_s
);
634 repo
= read_gitfile(ref_git
);
636 repo
= read_gitfile(mkpath("%s/.git", ref_git
));
639 ref_git
= xstrdup(repo
);
642 if (!repo
&& is_directory(mkpath("%s/.git/objects", ref_git
))) {
643 char *ref_git_git
= mkpathdup("%s/.git", ref_git
);
645 ref_git
= ref_git_git
;
646 } else if (!is_directory(mkpath("%s/objects", ref_git
))) {
647 struct strbuf sb
= STRBUF_INIT
;
649 if (get_common_dir(&sb
, ref_git
)) {
651 _("reference repository '%s' as a linked "
652 "checkout is not supported yet."),
657 strbuf_addf(err
, _("reference repository '%s' is not a "
658 "local repository."), path
);
662 if (!access(mkpath("%s/shallow", ref_git
), F_OK
)) {
663 strbuf_addf(err
, _("reference repository '%s' is shallow"),
669 if (!access(mkpath("%s/info/grafts", ref_git
), F_OK
)) {
671 _("reference repository '%s' is grafted"),
679 FREE_AND_NULL(ref_git
);
685 int foreach_alt_odb(alt_odb_fn fn
, void *cb
)
687 struct alternate_object_database
*ent
;
690 prepare_alt_odb(the_repository
);
691 for (ent
= the_repository
->objects
->alt_odb_list
; ent
; ent
= ent
->next
) {
699 void prepare_alt_odb(struct repository
*r
)
701 if (r
->objects
->alt_odb_tail
)
704 r
->objects
->alt_odb_tail
= &r
->objects
->alt_odb_list
;
705 link_alt_odb_entries(r
, r
->objects
->alternate_db
, PATH_SEP
, NULL
, 0);
707 read_info_alternates(r
, r
->objects
->objectdir
, 0);
710 /* Returns 1 if we have successfully freshened the file, 0 otherwise. */
711 static int freshen_file(const char *fn
)
714 t
.actime
= t
.modtime
= time(NULL
);
715 return !utime(fn
, &t
);
719 * All of the check_and_freshen functions return 1 if the file exists and was
720 * freshened (if freshening was requested), 0 otherwise. If they return
721 * 0, you should not assume that it is safe to skip a write of the object (it
722 * either does not exist on disk, or has a stale mtime and may be subject to
725 int check_and_freshen_file(const char *fn
, int freshen
)
727 if (access(fn
, F_OK
))
729 if (freshen
&& !freshen_file(fn
))
734 static int check_and_freshen_local(const struct object_id
*oid
, int freshen
)
736 static struct strbuf buf
= STRBUF_INIT
;
739 sha1_file_name(the_repository
, &buf
, oid
->hash
);
741 return check_and_freshen_file(buf
.buf
, freshen
);
744 static int check_and_freshen_nonlocal(const struct object_id
*oid
, int freshen
)
746 struct alternate_object_database
*alt
;
747 prepare_alt_odb(the_repository
);
748 for (alt
= the_repository
->objects
->alt_odb_list
; alt
; alt
= alt
->next
) {
749 const char *path
= alt_sha1_path(alt
, oid
->hash
);
750 if (check_and_freshen_file(path
, freshen
))
756 static int check_and_freshen(const struct object_id
*oid
, int freshen
)
758 return check_and_freshen_local(oid
, freshen
) ||
759 check_and_freshen_nonlocal(oid
, freshen
);
762 int has_loose_object_nonlocal(const struct object_id
*oid
)
764 return check_and_freshen_nonlocal(oid
, 0);
767 static int has_loose_object(const struct object_id
*oid
)
769 return check_and_freshen(oid
, 0);
772 static void mmap_limit_check(size_t length
)
774 static size_t limit
= 0;
776 limit
= git_env_ulong("GIT_MMAP_LIMIT", 0);
781 die(_("attempting to mmap %"PRIuMAX
" over limit %"PRIuMAX
),
782 (uintmax_t)length
, (uintmax_t)limit
);
785 void *xmmap_gently(void *start
, size_t length
,
786 int prot
, int flags
, int fd
, off_t offset
)
790 mmap_limit_check(length
);
791 ret
= mmap(start
, length
, prot
, flags
, fd
, offset
);
792 if (ret
== MAP_FAILED
) {
795 release_pack_memory(length
);
796 ret
= mmap(start
, length
, prot
, flags
, fd
, offset
);
801 void *xmmap(void *start
, size_t length
,
802 int prot
, int flags
, int fd
, off_t offset
)
804 void *ret
= xmmap_gently(start
, length
, prot
, flags
, fd
, offset
);
805 if (ret
== MAP_FAILED
)
806 die_errno(_("mmap failed"));
811 * With an in-core object data in "map", rehash it to make sure the
812 * object name actually matches "sha1" to detect object corruption.
813 * With "map" == NULL, try reading the object named with "sha1" using
814 * the streaming interface and rehash it to do the same.
816 int check_object_signature(const struct object_id
*oid
, void *map
,
817 unsigned long size
, const char *type
)
819 struct object_id real_oid
;
820 enum object_type obj_type
;
821 struct git_istream
*st
;
823 char hdr
[MAX_HEADER_LEN
];
827 hash_object_file(map
, size
, type
, &real_oid
);
828 return !oideq(oid
, &real_oid
) ? -1 : 0;
831 st
= open_istream(oid
, &obj_type
, &size
, NULL
);
835 /* Generate the header */
836 hdrlen
= xsnprintf(hdr
, sizeof(hdr
), "%s %"PRIuMAX
, type_name(obj_type
), (uintmax_t)size
) + 1;
839 the_hash_algo
->init_fn(&c
);
840 the_hash_algo
->update_fn(&c
, hdr
, hdrlen
);
843 ssize_t readlen
= read_istream(st
, buf
, sizeof(buf
));
851 the_hash_algo
->update_fn(&c
, buf
, readlen
);
853 the_hash_algo
->final_fn(real_oid
.hash
, &c
);
855 return !oideq(oid
, &real_oid
) ? -1 : 0;
858 int git_open_cloexec(const char *name
, int flags
)
861 static int o_cloexec
= O_CLOEXEC
;
863 fd
= open(name
, flags
| o_cloexec
);
864 if ((o_cloexec
& O_CLOEXEC
) && fd
< 0 && errno
== EINVAL
) {
865 /* Try again w/o O_CLOEXEC: the kernel might not support it */
866 o_cloexec
&= ~O_CLOEXEC
;
867 fd
= open(name
, flags
| o_cloexec
);
870 #if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
872 static int fd_cloexec
= FD_CLOEXEC
;
874 if (!o_cloexec
&& 0 <= fd
&& fd_cloexec
) {
875 /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */
876 int flags
= fcntl(fd
, F_GETFD
);
877 if (fcntl(fd
, F_SETFD
, flags
| fd_cloexec
))
886 * Find "sha1" as a loose object in the local repository or in an alternate.
887 * Returns 0 on success, negative on failure.
889 * The "path" out-parameter will give the path of the object we found (if any).
890 * Note that it may point to static storage and is only valid until another
891 * call to sha1_file_name(), etc.
893 static int stat_sha1_file(struct repository
*r
, const unsigned char *sha1
,
894 struct stat
*st
, const char **path
)
896 struct alternate_object_database
*alt
;
897 static struct strbuf buf
= STRBUF_INIT
;
900 sha1_file_name(r
, &buf
, sha1
);
903 if (!lstat(*path
, st
))
908 for (alt
= r
->objects
->alt_odb_list
; alt
; alt
= alt
->next
) {
909 *path
= alt_sha1_path(alt
, sha1
);
910 if (!lstat(*path
, st
))
918 * Like stat_sha1_file(), but actually open the object and return the
919 * descriptor. See the caveats on the "path" parameter above.
921 static int open_sha1_file(struct repository
*r
,
922 const unsigned char *sha1
, const char **path
)
925 struct alternate_object_database
*alt
;
926 int most_interesting_errno
;
927 static struct strbuf buf
= STRBUF_INIT
;
930 sha1_file_name(r
, &buf
, sha1
);
933 fd
= git_open(*path
);
936 most_interesting_errno
= errno
;
939 for (alt
= r
->objects
->alt_odb_list
; alt
; alt
= alt
->next
) {
940 *path
= alt_sha1_path(alt
, sha1
);
941 fd
= git_open(*path
);
944 if (most_interesting_errno
== ENOENT
)
945 most_interesting_errno
= errno
;
947 errno
= most_interesting_errno
;
952 * Map the loose object at "path" if it is not NULL, or the path found by
953 * searching for a loose object named "sha1".
955 static void *map_sha1_file_1(struct repository
*r
, const char *path
,
956 const unsigned char *sha1
, unsigned long *size
)
964 fd
= open_sha1_file(r
, sha1
, &path
);
969 if (!fstat(fd
, &st
)) {
970 *size
= xsize_t(st
.st_size
);
972 /* mmap() is forbidden on empty files */
973 error(_("object file %s is empty"), path
);
977 map
= xmmap(NULL
, *size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
984 void *map_sha1_file(struct repository
*r
,
985 const unsigned char *sha1
, unsigned long *size
)
987 return map_sha1_file_1(r
, NULL
, sha1
, size
);
990 static int unpack_sha1_short_header(git_zstream
*stream
,
991 unsigned char *map
, unsigned long mapsize
,
992 void *buffer
, unsigned long bufsiz
)
994 /* Get the data stream */
995 memset(stream
, 0, sizeof(*stream
));
996 stream
->next_in
= map
;
997 stream
->avail_in
= mapsize
;
998 stream
->next_out
= buffer
;
999 stream
->avail_out
= bufsiz
;
1001 git_inflate_init(stream
);
1002 return git_inflate(stream
, 0);
1005 int unpack_sha1_header(git_zstream
*stream
,
1006 unsigned char *map
, unsigned long mapsize
,
1007 void *buffer
, unsigned long bufsiz
)
1009 int status
= unpack_sha1_short_header(stream
, map
, mapsize
,
1015 /* Make sure we have the terminating NUL */
1016 if (!memchr(buffer
, '\0', stream
->next_out
- (unsigned char *)buffer
))
1021 static int unpack_sha1_header_to_strbuf(git_zstream
*stream
, unsigned char *map
,
1022 unsigned long mapsize
, void *buffer
,
1023 unsigned long bufsiz
, struct strbuf
*header
)
1027 status
= unpack_sha1_short_header(stream
, map
, mapsize
, buffer
, bufsiz
);
1032 * Check if entire header is unpacked in the first iteration.
1034 if (memchr(buffer
, '\0', stream
->next_out
- (unsigned char *)buffer
))
1038 * buffer[0..bufsiz] was not large enough. Copy the partial
1039 * result out to header, and then append the result of further
1040 * reading the stream.
1042 strbuf_add(header
, buffer
, stream
->next_out
- (unsigned char *)buffer
);
1043 stream
->next_out
= buffer
;
1044 stream
->avail_out
= bufsiz
;
1047 status
= git_inflate(stream
, 0);
1048 strbuf_add(header
, buffer
, stream
->next_out
- (unsigned char *)buffer
);
1049 if (memchr(buffer
, '\0', stream
->next_out
- (unsigned char *)buffer
))
1051 stream
->next_out
= buffer
;
1052 stream
->avail_out
= bufsiz
;
1053 } while (status
!= Z_STREAM_END
);
1057 static void *unpack_sha1_rest(git_zstream
*stream
, void *buffer
, unsigned long size
, const unsigned char *sha1
)
1059 int bytes
= strlen(buffer
) + 1;
1060 unsigned char *buf
= xmallocz(size
);
1064 n
= stream
->total_out
- bytes
;
1067 memcpy(buf
, (char *) buffer
+ bytes
, n
);
1069 if (bytes
<= size
) {
1071 * The above condition must be (bytes <= size), not
1072 * (bytes < size). In other words, even though we
1073 * expect no more output and set avail_out to zero,
1074 * the input zlib stream may have bytes that express
1075 * "this concludes the stream", and we *do* want to
1078 * Otherwise we would not be able to test that we
1079 * consumed all the input to reach the expected size;
1080 * we also want to check that zlib tells us that all
1081 * went well with status == Z_STREAM_END at the end.
1083 stream
->next_out
= buf
+ bytes
;
1084 stream
->avail_out
= size
- bytes
;
1085 while (status
== Z_OK
)
1086 status
= git_inflate(stream
, Z_FINISH
);
1088 if (status
== Z_STREAM_END
&& !stream
->avail_in
) {
1089 git_inflate_end(stream
);
1094 error(_("corrupt loose object '%s'"), sha1_to_hex(sha1
));
1095 else if (stream
->avail_in
)
1096 error(_("garbage at end of loose object '%s'"),
1103 * We used to just use "sscanf()", but that's actually way
1104 * too permissive for what we want to check. So do an anal
1105 * object header parse by hand.
1107 static int parse_sha1_header_extended(const char *hdr
, struct object_info
*oi
,
1110 const char *type_buf
= hdr
;
1112 int type
, type_len
= 0;
1115 * The type can be of any size but is followed by
1127 type
= type_from_string_gently(type_buf
, type_len
, 1);
1129 strbuf_add(oi
->type_name
, type_buf
, type_len
);
1131 * Set type to 0 if its an unknown object and
1132 * we're obtaining the type using '--allow-unknown-type'
1135 if ((flags
& OBJECT_INFO_ALLOW_UNKNOWN_TYPE
) && (type
< 0))
1138 die(_("invalid object type"));
1143 * The length must follow immediately, and be in canonical
1144 * decimal format (ie "010" is not valid).
1146 size
= *hdr
++ - '0';
1151 unsigned long c
= *hdr
- '0';
1155 size
= size
* 10 + c
;
1163 * The length must be followed by a zero byte
1165 return *hdr
? -1 : type
;
1168 int parse_sha1_header(const char *hdr
, unsigned long *sizep
)
1170 struct object_info oi
= OBJECT_INFO_INIT
;
1173 return parse_sha1_header_extended(hdr
, &oi
, 0);
1176 static int sha1_loose_object_info(struct repository
*r
,
1177 const unsigned char *sha1
,
1178 struct object_info
*oi
, int flags
)
1181 unsigned long mapsize
;
1184 char hdr
[MAX_HEADER_LEN
];
1185 struct strbuf hdrbuf
= STRBUF_INIT
;
1186 unsigned long size_scratch
;
1188 if (oi
->delta_base_sha1
)
1189 hashclr(oi
->delta_base_sha1
);
1192 * If we don't care about type or size, then we don't
1193 * need to look inside the object at all. Note that we
1194 * do not optimize out the stat call, even if the
1195 * caller doesn't care about the disk-size, since our
1196 * return value implicitly indicates whether the
1197 * object even exists.
1199 if (!oi
->typep
&& !oi
->type_name
&& !oi
->sizep
&& !oi
->contentp
) {
1202 if (stat_sha1_file(r
, sha1
, &st
, &path
) < 0)
1205 *oi
->disk_sizep
= st
.st_size
;
1209 map
= map_sha1_file(r
, sha1
, &mapsize
);
1214 oi
->sizep
= &size_scratch
;
1217 *oi
->disk_sizep
= mapsize
;
1218 if ((flags
& OBJECT_INFO_ALLOW_UNKNOWN_TYPE
)) {
1219 if (unpack_sha1_header_to_strbuf(&stream
, map
, mapsize
, hdr
, sizeof(hdr
), &hdrbuf
) < 0)
1220 status
= error(_("unable to unpack %s header with --allow-unknown-type"),
1222 } else if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
1223 status
= error(_("unable to unpack %s header"),
1227 else if (hdrbuf
.len
) {
1228 if ((status
= parse_sha1_header_extended(hdrbuf
.buf
, oi
, flags
)) < 0)
1229 status
= error(_("unable to parse %s header with --allow-unknown-type"),
1231 } else if ((status
= parse_sha1_header_extended(hdr
, oi
, flags
)) < 0)
1232 status
= error(_("unable to parse %s header"), sha1_to_hex(sha1
));
1234 if (status
>= 0 && oi
->contentp
) {
1235 *oi
->contentp
= unpack_sha1_rest(&stream
, hdr
,
1237 if (!*oi
->contentp
) {
1238 git_inflate_end(&stream
);
1242 git_inflate_end(&stream
);
1244 munmap(map
, mapsize
);
1245 if (status
&& oi
->typep
)
1246 *oi
->typep
= status
;
1247 if (oi
->sizep
== &size_scratch
)
1249 strbuf_release(&hdrbuf
);
1250 oi
->whence
= OI_LOOSE
;
1251 return (status
< 0) ? status
: 0;
1254 int fetch_if_missing
= 1;
1256 int oid_object_info_extended(struct repository
*r
, const struct object_id
*oid
,
1257 struct object_info
*oi
, unsigned flags
)
1259 static struct object_info blank_oi
= OBJECT_INFO_INIT
;
1260 struct pack_entry e
;
1262 const struct object_id
*real
= oid
;
1263 int already_retried
= 0;
1265 if (flags
& OBJECT_INFO_LOOKUP_REPLACE
)
1266 real
= lookup_replace_object(r
, oid
);
1268 if (is_null_oid(real
))
1274 if (!(flags
& OBJECT_INFO_SKIP_CACHED
)) {
1275 struct cached_object
*co
= find_cached_object(real
);
1278 *(oi
->typep
) = co
->type
;
1280 *(oi
->sizep
) = co
->size
;
1282 *(oi
->disk_sizep
) = 0;
1283 if (oi
->delta_base_sha1
)
1284 hashclr(oi
->delta_base_sha1
);
1286 strbuf_addstr(oi
->type_name
, type_name(co
->type
));
1288 *oi
->contentp
= xmemdupz(co
->buf
, co
->size
);
1289 oi
->whence
= OI_CACHED
;
1295 if (find_pack_entry(r
, real
, &e
))
1298 if (flags
& OBJECT_INFO_IGNORE_LOOSE
)
1301 /* Most likely it's a loose object. */
1302 if (!sha1_loose_object_info(r
, real
->hash
, oi
, flags
))
1305 /* Not a loose object; someone else may have just packed it. */
1306 if (!(flags
& OBJECT_INFO_QUICK
)) {
1307 reprepare_packed_git(r
);
1308 if (find_pack_entry(r
, real
, &e
))
1312 /* Check if it is a missing object */
1313 if (fetch_if_missing
&& repository_format_partial_clone
&&
1314 !already_retried
&& r
== the_repository
) {
1316 * TODO Investigate having fetch_object() return
1317 * TODO error/success and stopping the music here.
1318 * TODO Pass a repository struct through fetch_object,
1319 * such that arbitrary repositories work.
1321 fetch_objects(repository_format_partial_clone
, real
, 1);
1322 already_retried
= 1;
1329 if (oi
== &blank_oi
)
1331 * We know that the caller doesn't actually need the
1332 * information below, so return early.
1335 rtype
= packed_object_info(r
, e
.p
, e
.offset
, oi
);
1337 mark_bad_packed_object(e
.p
, real
->hash
);
1338 return oid_object_info_extended(r
, real
, oi
, 0);
1339 } else if (oi
->whence
== OI_PACKED
) {
1340 oi
->u
.packed
.offset
= e
.offset
;
1341 oi
->u
.packed
.pack
= e
.p
;
1342 oi
->u
.packed
.is_delta
= (rtype
== OBJ_REF_DELTA
||
1343 rtype
== OBJ_OFS_DELTA
);
1349 /* returns enum object_type or negative */
1350 int oid_object_info(struct repository
*r
,
1351 const struct object_id
*oid
,
1352 unsigned long *sizep
)
1354 enum object_type type
;
1355 struct object_info oi
= OBJECT_INFO_INIT
;
1359 if (oid_object_info_extended(r
, oid
, &oi
,
1360 OBJECT_INFO_LOOKUP_REPLACE
) < 0)
1365 static void *read_object(const unsigned char *sha1
, enum object_type
*type
,
1366 unsigned long *size
)
1368 struct object_id oid
;
1369 struct object_info oi
= OBJECT_INFO_INIT
;
1373 oi
.contentp
= &content
;
1375 hashcpy(oid
.hash
, sha1
);
1377 if (oid_object_info_extended(the_repository
, &oid
, &oi
, 0) < 0)
1382 int pretend_object_file(void *buf
, unsigned long len
, enum object_type type
,
1383 struct object_id
*oid
)
1385 struct cached_object
*co
;
1387 hash_object_file(buf
, len
, type_name(type
), oid
);
1388 if (has_sha1_file(oid
->hash
) || find_cached_object(oid
))
1390 ALLOC_GROW(cached_objects
, cached_object_nr
+ 1, cached_object_alloc
);
1391 co
= &cached_objects
[cached_object_nr
++];
1394 co
->buf
= xmalloc(len
);
1395 memcpy(co
->buf
, buf
, len
);
1396 oidcpy(&co
->oid
, oid
);
1401 * This function dies on corrupt objects; the callers who want to
1402 * deal with them should arrange to call read_object() and give error
1403 * messages themselves.
1405 void *read_object_file_extended(const struct object_id
*oid
,
1406 enum object_type
*type
,
1407 unsigned long *size
,
1411 const struct packed_git
*p
;
1414 const struct object_id
*repl
= lookup_replace
?
1415 lookup_replace_object(the_repository
, oid
) : oid
;
1418 data
= read_object(repl
->hash
, type
, size
);
1422 if (errno
&& errno
!= ENOENT
)
1423 die_errno(_("failed to read object %s"), oid_to_hex(oid
));
1425 /* die if we replaced an object with one that does not exist */
1427 die(_("replacement %s not found for %s"),
1428 oid_to_hex(repl
), oid_to_hex(oid
));
1430 if (!stat_sha1_file(the_repository
, repl
->hash
, &st
, &path
))
1431 die(_("loose object %s (stored in %s) is corrupt"),
1432 oid_to_hex(repl
), path
);
1434 if ((p
= has_packed_and_bad(repl
->hash
)) != NULL
)
1435 die(_("packed object %s (stored in %s) is corrupt"),
1436 oid_to_hex(repl
), p
->pack_name
);
1441 void *read_object_with_reference(const struct object_id
*oid
,
1442 const char *required_type_name
,
1443 unsigned long *size
,
1444 struct object_id
*actual_oid_return
)
1446 enum object_type type
, required_type
;
1448 unsigned long isize
;
1449 struct object_id actual_oid
;
1451 required_type
= type_from_string(required_type_name
);
1452 oidcpy(&actual_oid
, oid
);
1454 int ref_length
= -1;
1455 const char *ref_type
= NULL
;
1457 buffer
= read_object_file(&actual_oid
, &type
, &isize
);
1460 if (type
== required_type
) {
1462 if (actual_oid_return
)
1463 oidcpy(actual_oid_return
, &actual_oid
);
1466 /* Handle references */
1467 else if (type
== OBJ_COMMIT
)
1469 else if (type
== OBJ_TAG
)
1470 ref_type
= "object ";
1475 ref_length
= strlen(ref_type
);
1477 if (ref_length
+ the_hash_algo
->hexsz
> isize
||
1478 memcmp(buffer
, ref_type
, ref_length
) ||
1479 get_oid_hex((char *) buffer
+ ref_length
, &actual_oid
)) {
1484 /* Now we have the ID of the referred-to object in
1485 * actual_oid. Check again. */
1489 static void write_object_file_prepare(const void *buf
, unsigned long len
,
1490 const char *type
, struct object_id
*oid
,
1491 char *hdr
, int *hdrlen
)
1495 /* Generate the header */
1496 *hdrlen
= xsnprintf(hdr
, *hdrlen
, "%s %"PRIuMAX
, type
, (uintmax_t)len
)+1;
1499 the_hash_algo
->init_fn(&c
);
1500 the_hash_algo
->update_fn(&c
, hdr
, *hdrlen
);
1501 the_hash_algo
->update_fn(&c
, buf
, len
);
1502 the_hash_algo
->final_fn(oid
->hash
, &c
);
1506 * Move the just written object into its final resting place.
1508 int finalize_object_file(const char *tmpfile
, const char *filename
)
1512 if (object_creation_mode
== OBJECT_CREATION_USES_RENAMES
)
1514 else if (link(tmpfile
, filename
))
1518 * Coda hack - coda doesn't like cross-directory links,
1519 * so we fall back to a rename, which will mean that it
1520 * won't be able to check collisions, but that's not a
1523 * The same holds for FAT formatted media.
1525 * When this succeeds, we just return. We have nothing
1528 if (ret
&& ret
!= EEXIST
) {
1530 if (!rename(tmpfile
, filename
))
1534 unlink_or_warn(tmpfile
);
1536 if (ret
!= EEXIST
) {
1537 return error_errno(_("unable to write sha1 filename %s"), filename
);
1539 /* FIXME!!! Collision check here ? */
1543 if (adjust_shared_perm(filename
))
1544 return error(_("unable to set permission to '%s'"), filename
);
1548 static int write_buffer(int fd
, const void *buf
, size_t len
)
1550 if (write_in_full(fd
, buf
, len
) < 0)
1551 return error_errno(_("file write error"));
1555 int hash_object_file(const void *buf
, unsigned long len
, const char *type
,
1556 struct object_id
*oid
)
1558 char hdr
[MAX_HEADER_LEN
];
1559 int hdrlen
= sizeof(hdr
);
1560 write_object_file_prepare(buf
, len
, type
, oid
, hdr
, &hdrlen
);
1564 /* Finalize a file on disk, and close it. */
1565 static void close_sha1_file(int fd
)
1567 if (fsync_object_files
)
1568 fsync_or_die(fd
, "sha1 file");
1570 die_errno(_("error when closing sha1 file"));
1573 /* Size of directory component, including the ending '/' */
1574 static inline int directory_size(const char *filename
)
1576 const char *s
= strrchr(filename
, '/');
1579 return s
- filename
+ 1;
1583 * This creates a temporary file in the same directory as the final
1586 * We want to avoid cross-directory filename renames, because those
1587 * can have problems on various filesystems (FAT, NFS, Coda).
1589 static int create_tmpfile(struct strbuf
*tmp
, const char *filename
)
1591 int fd
, dirlen
= directory_size(filename
);
1594 strbuf_add(tmp
, filename
, dirlen
);
1595 strbuf_addstr(tmp
, "tmp_obj_XXXXXX");
1596 fd
= git_mkstemp_mode(tmp
->buf
, 0444);
1597 if (fd
< 0 && dirlen
&& errno
== ENOENT
) {
1599 * Make sure the directory exists; note that the contents
1600 * of the buffer are undefined after mkstemp returns an
1601 * error, so we have to rewrite the whole buffer from
1605 strbuf_add(tmp
, filename
, dirlen
- 1);
1606 if (mkdir(tmp
->buf
, 0777) && errno
!= EEXIST
)
1608 if (adjust_shared_perm(tmp
->buf
))
1612 strbuf_addstr(tmp
, "/tmp_obj_XXXXXX");
1613 fd
= git_mkstemp_mode(tmp
->buf
, 0444);
1618 static int write_loose_object(const struct object_id
*oid
, char *hdr
,
1619 int hdrlen
, const void *buf
, unsigned long len
,
1623 unsigned char compressed
[4096];
1626 struct object_id parano_oid
;
1627 static struct strbuf tmp_file
= STRBUF_INIT
;
1628 static struct strbuf filename
= STRBUF_INIT
;
1630 strbuf_reset(&filename
);
1631 sha1_file_name(the_repository
, &filename
, oid
->hash
);
1633 fd
= create_tmpfile(&tmp_file
, filename
.buf
);
1635 if (errno
== EACCES
)
1636 return error(_("insufficient permission for adding an object to repository database %s"), get_object_directory());
1638 return error_errno(_("unable to create temporary file"));
1642 git_deflate_init(&stream
, zlib_compression_level
);
1643 stream
.next_out
= compressed
;
1644 stream
.avail_out
= sizeof(compressed
);
1645 the_hash_algo
->init_fn(&c
);
1647 /* First header.. */
1648 stream
.next_in
= (unsigned char *)hdr
;
1649 stream
.avail_in
= hdrlen
;
1650 while (git_deflate(&stream
, 0) == Z_OK
)
1652 the_hash_algo
->update_fn(&c
, hdr
, hdrlen
);
1654 /* Then the data itself.. */
1655 stream
.next_in
= (void *)buf
;
1656 stream
.avail_in
= len
;
1658 unsigned char *in0
= stream
.next_in
;
1659 ret
= git_deflate(&stream
, Z_FINISH
);
1660 the_hash_algo
->update_fn(&c
, in0
, stream
.next_in
- in0
);
1661 if (write_buffer(fd
, compressed
, stream
.next_out
- compressed
) < 0)
1662 die(_("unable to write sha1 file"));
1663 stream
.next_out
= compressed
;
1664 stream
.avail_out
= sizeof(compressed
);
1665 } while (ret
== Z_OK
);
1667 if (ret
!= Z_STREAM_END
)
1668 die(_("unable to deflate new object %s (%d)"), oid_to_hex(oid
),
1670 ret
= git_deflate_end_gently(&stream
);
1672 die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid
),
1674 the_hash_algo
->final_fn(parano_oid
.hash
, &c
);
1675 if (!oideq(oid
, ¶no_oid
))
1676 die(_("confused by unstable object source data for %s"),
1679 close_sha1_file(fd
);
1684 utb
.modtime
= mtime
;
1685 if (utime(tmp_file
.buf
, &utb
) < 0)
1686 warning_errno(_("failed utime() on %s"), tmp_file
.buf
);
1689 return finalize_object_file(tmp_file
.buf
, filename
.buf
);
1692 static int freshen_loose_object(const struct object_id
*oid
)
1694 return check_and_freshen(oid
, 1);
1697 static int freshen_packed_object(const struct object_id
*oid
)
1699 struct pack_entry e
;
1700 if (!find_pack_entry(the_repository
, oid
, &e
))
1704 if (!freshen_file(e
.p
->pack_name
))
1710 int write_object_file(const void *buf
, unsigned long len
, const char *type
,
1711 struct object_id
*oid
)
1713 char hdr
[MAX_HEADER_LEN
];
1714 int hdrlen
= sizeof(hdr
);
1716 /* Normally if we have it in the pack then we do not bother writing
1717 * it out into .git/objects/??/?{38} file.
1719 write_object_file_prepare(buf
, len
, type
, oid
, hdr
, &hdrlen
);
1720 if (freshen_packed_object(oid
) || freshen_loose_object(oid
))
1722 return write_loose_object(oid
, hdr
, hdrlen
, buf
, len
, 0);
1725 int hash_object_file_literally(const void *buf
, unsigned long len
,
1726 const char *type
, struct object_id
*oid
,
1730 int hdrlen
, status
= 0;
1732 /* type string, SP, %lu of the length plus NUL must fit this */
1733 hdrlen
= strlen(type
) + MAX_HEADER_LEN
;
1734 header
= xmalloc(hdrlen
);
1735 write_object_file_prepare(buf
, len
, type
, oid
, header
, &hdrlen
);
1737 if (!(flags
& HASH_WRITE_OBJECT
))
1739 if (freshen_packed_object(oid
) || freshen_loose_object(oid
))
1741 status
= write_loose_object(oid
, header
, hdrlen
, buf
, len
, 0);
1748 int force_object_loose(const struct object_id
*oid
, time_t mtime
)
1752 enum object_type type
;
1753 char hdr
[MAX_HEADER_LEN
];
1757 if (has_loose_object(oid
))
1759 buf
= read_object(oid
->hash
, &type
, &len
);
1761 return error(_("cannot read sha1_file for %s"), oid_to_hex(oid
));
1762 hdrlen
= xsnprintf(hdr
, sizeof(hdr
), "%s %"PRIuMAX
, type_name(type
), (uintmax_t)len
) + 1;
1763 ret
= write_loose_object(oid
, hdr
, hdrlen
, buf
, len
, mtime
);
1769 int has_sha1_file_with_flags(const unsigned char *sha1
, int flags
)
1771 struct object_id oid
;
1772 if (!startup_info
->have_repository
)
1774 hashcpy(oid
.hash
, sha1
);
1775 return oid_object_info_extended(the_repository
, &oid
, NULL
,
1776 flags
| OBJECT_INFO_SKIP_CACHED
) >= 0;
1779 int has_object_file(const struct object_id
*oid
)
1781 return has_sha1_file(oid
->hash
);
1784 int has_object_file_with_flags(const struct object_id
*oid
, int flags
)
1786 return has_sha1_file_with_flags(oid
->hash
, flags
);
1789 static void check_tree(const void *buf
, size_t size
)
1791 struct tree_desc desc
;
1792 struct name_entry entry
;
1794 init_tree_desc(&desc
, buf
, size
);
1795 while (tree_entry(&desc
, &entry
))
1797 * tree_entry() will die() on malformed entries */
1801 static void check_commit(const void *buf
, size_t size
)
1804 memset(&c
, 0, sizeof(c
));
1805 if (parse_commit_buffer(the_repository
, &c
, buf
, size
, 0))
1806 die(_("corrupt commit"));
1809 static void check_tag(const void *buf
, size_t size
)
1812 memset(&t
, 0, sizeof(t
));
1813 if (parse_tag_buffer(the_repository
, &t
, buf
, size
))
1814 die(_("corrupt tag"));
1817 static int index_mem(struct index_state
*istate
,
1818 struct object_id
*oid
, void *buf
, size_t size
,
1819 enum object_type type
,
1820 const char *path
, unsigned flags
)
1822 int ret
, re_allocated
= 0;
1823 int write_object
= flags
& HASH_WRITE_OBJECT
;
1829 * Convert blobs to git internal format
1831 if ((type
== OBJ_BLOB
) && path
) {
1832 struct strbuf nbuf
= STRBUF_INIT
;
1833 if (convert_to_git(istate
, path
, buf
, size
, &nbuf
,
1834 get_conv_flags(flags
))) {
1835 buf
= strbuf_detach(&nbuf
, &size
);
1839 if (flags
& HASH_FORMAT_CHECK
) {
1840 if (type
== OBJ_TREE
)
1841 check_tree(buf
, size
);
1842 if (type
== OBJ_COMMIT
)
1843 check_commit(buf
, size
);
1844 if (type
== OBJ_TAG
)
1845 check_tag(buf
, size
);
1849 ret
= write_object_file(buf
, size
, type_name(type
), oid
);
1851 ret
= hash_object_file(buf
, size
, type_name(type
), oid
);
1857 static int index_stream_convert_blob(struct index_state
*istate
,
1858 struct object_id
*oid
,
1864 const int write_object
= flags
& HASH_WRITE_OBJECT
;
1865 struct strbuf sbuf
= STRBUF_INIT
;
1868 assert(would_convert_to_git_filter_fd(istate
, path
));
1870 convert_to_git_filter_fd(istate
, path
, fd
, &sbuf
,
1871 get_conv_flags(flags
));
1874 ret
= write_object_file(sbuf
.buf
, sbuf
.len
, type_name(OBJ_BLOB
),
1877 ret
= hash_object_file(sbuf
.buf
, sbuf
.len
, type_name(OBJ_BLOB
),
1879 strbuf_release(&sbuf
);
1883 static int index_pipe(struct index_state
*istate
, struct object_id
*oid
,
1884 int fd
, enum object_type type
,
1885 const char *path
, unsigned flags
)
1887 struct strbuf sbuf
= STRBUF_INIT
;
1890 if (strbuf_read(&sbuf
, fd
, 4096) >= 0)
1891 ret
= index_mem(istate
, oid
, sbuf
.buf
, sbuf
.len
, type
, path
, flags
);
1894 strbuf_release(&sbuf
);
1898 #define SMALL_FILE_SIZE (32*1024)
1900 static int index_core(struct index_state
*istate
,
1901 struct object_id
*oid
, int fd
, size_t size
,
1902 enum object_type type
, const char *path
,
1908 ret
= index_mem(istate
, oid
, "", size
, type
, path
, flags
);
1909 } else if (size
<= SMALL_FILE_SIZE
) {
1910 char *buf
= xmalloc(size
);
1911 ssize_t read_result
= read_in_full(fd
, buf
, size
);
1912 if (read_result
< 0)
1913 ret
= error_errno(_("read error while indexing %s"),
1914 path
? path
: "<unknown>");
1915 else if (read_result
!= size
)
1916 ret
= error(_("short read while indexing %s"),
1917 path
? path
: "<unknown>");
1919 ret
= index_mem(istate
, oid
, buf
, size
, type
, path
, flags
);
1922 void *buf
= xmmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1923 ret
= index_mem(istate
, oid
, buf
, size
, type
, path
, flags
);
1930 * This creates one packfile per large blob unless bulk-checkin
1931 * machinery is "plugged".
1933 * This also bypasses the usual "convert-to-git" dance, and that is on
1934 * purpose. We could write a streaming version of the converting
1935 * functions and insert that before feeding the data to fast-import
1936 * (or equivalent in-core API described above). However, that is
1937 * somewhat complicated, as we do not know the size of the filter
1938 * result, which we need to know beforehand when writing a git object.
1939 * Since the primary motivation for trying to stream from the working
1940 * tree file and to avoid mmaping it in core is to deal with large
1941 * binary blobs, they generally do not want to get any conversion, and
1942 * callers should avoid this code path when filters are requested.
1944 static int index_stream(struct object_id
*oid
, int fd
, size_t size
,
1945 enum object_type type
, const char *path
,
1948 return index_bulk_checkin(oid
, fd
, size
, type
, path
, flags
);
1951 int index_fd(struct index_state
*istate
, struct object_id
*oid
,
1952 int fd
, struct stat
*st
,
1953 enum object_type type
, const char *path
, unsigned flags
)
1958 * Call xsize_t() only when needed to avoid potentially unnecessary
1959 * die() for large files.
1961 if (type
== OBJ_BLOB
&& path
&& would_convert_to_git_filter_fd(istate
, path
))
1962 ret
= index_stream_convert_blob(istate
, oid
, fd
, path
, flags
);
1963 else if (!S_ISREG(st
->st_mode
))
1964 ret
= index_pipe(istate
, oid
, fd
, type
, path
, flags
);
1965 else if (st
->st_size
<= big_file_threshold
|| type
!= OBJ_BLOB
||
1966 (path
&& would_convert_to_git(istate
, path
)))
1967 ret
= index_core(istate
, oid
, fd
, xsize_t(st
->st_size
),
1970 ret
= index_stream(oid
, fd
, xsize_t(st
->st_size
), type
, path
,
1976 int index_path(struct index_state
*istate
, struct object_id
*oid
,
1977 const char *path
, struct stat
*st
, unsigned flags
)
1980 struct strbuf sb
= STRBUF_INIT
;
1983 switch (st
->st_mode
& S_IFMT
) {
1985 fd
= open(path
, O_RDONLY
);
1987 return error_errno("open(\"%s\")", path
);
1988 if (index_fd(istate
, oid
, fd
, st
, OBJ_BLOB
, path
, flags
) < 0)
1989 return error(_("%s: failed to insert into database"),
1993 if (strbuf_readlink(&sb
, path
, st
->st_size
))
1994 return error_errno("readlink(\"%s\")", path
);
1995 if (!(flags
& HASH_WRITE_OBJECT
))
1996 hash_object_file(sb
.buf
, sb
.len
, blob_type
, oid
);
1997 else if (write_object_file(sb
.buf
, sb
.len
, blob_type
, oid
))
1998 rc
= error(_("%s: failed to insert into database"), path
);
1999 strbuf_release(&sb
);
2002 return resolve_gitlink_ref(path
, "HEAD", oid
);
2004 return error(_("%s: unsupported file type"), path
);
2009 int read_pack_header(int fd
, struct pack_header
*header
)
2011 if (read_in_full(fd
, header
, sizeof(*header
)) != sizeof(*header
))
2012 /* "eof before pack header was fully read" */
2013 return PH_ERROR_EOF
;
2015 if (header
->hdr_signature
!= htonl(PACK_SIGNATURE
))
2016 /* "protocol error (pack signature mismatch detected)" */
2017 return PH_ERROR_PACK_SIGNATURE
;
2018 if (!pack_version_ok(header
->hdr_version
))
2019 /* "protocol error (pack version unsupported)" */
2020 return PH_ERROR_PROTOCOL
;
2024 void assert_oid_type(const struct object_id
*oid
, enum object_type expect
)
2026 enum object_type type
= oid_object_info(the_repository
, oid
, NULL
);
2028 die(_("%s is not a valid object"), oid_to_hex(oid
));
2030 die(_("%s is not a valid '%s' object"), oid_to_hex(oid
),
2034 int for_each_file_in_obj_subdir(unsigned int subdir_nr
,
2035 struct strbuf
*path
,
2036 each_loose_object_fn obj_cb
,
2037 each_loose_cruft_fn cruft_cb
,
2038 each_loose_subdir_fn subdir_cb
,
2041 size_t origlen
, baselen
;
2045 struct object_id oid
;
2047 if (subdir_nr
> 0xff)
2048 BUG("invalid loose object subdirectory: %x", subdir_nr
);
2050 origlen
= path
->len
;
2051 strbuf_complete(path
, '/');
2052 strbuf_addf(path
, "%02x", subdir_nr
);
2054 dir
= opendir(path
->buf
);
2056 if (errno
!= ENOENT
)
2057 r
= error_errno(_("unable to open %s"), path
->buf
);
2058 strbuf_setlen(path
, origlen
);
2062 oid
.hash
[0] = subdir_nr
;
2063 strbuf_addch(path
, '/');
2064 baselen
= path
->len
;
2066 while ((de
= readdir(dir
))) {
2068 if (is_dot_or_dotdot(de
->d_name
))
2071 namelen
= strlen(de
->d_name
);
2072 strbuf_setlen(path
, baselen
);
2073 strbuf_add(path
, de
->d_name
, namelen
);
2074 if (namelen
== the_hash_algo
->hexsz
- 2 &&
2075 !hex_to_bytes(oid
.hash
+ 1, de
->d_name
,
2076 the_hash_algo
->rawsz
- 1)) {
2078 r
= obj_cb(&oid
, path
->buf
, data
);
2086 r
= cruft_cb(de
->d_name
, path
->buf
, data
);
2093 strbuf_setlen(path
, baselen
- 1);
2094 if (!r
&& subdir_cb
)
2095 r
= subdir_cb(subdir_nr
, path
->buf
, data
);
2097 strbuf_setlen(path
, origlen
);
2102 int for_each_loose_file_in_objdir_buf(struct strbuf
*path
,
2103 each_loose_object_fn obj_cb
,
2104 each_loose_cruft_fn cruft_cb
,
2105 each_loose_subdir_fn subdir_cb
,
2111 for (i
= 0; i
< 256; i
++) {
2112 r
= for_each_file_in_obj_subdir(i
, path
, obj_cb
, cruft_cb
,
2121 int for_each_loose_file_in_objdir(const char *path
,
2122 each_loose_object_fn obj_cb
,
2123 each_loose_cruft_fn cruft_cb
,
2124 each_loose_subdir_fn subdir_cb
,
2127 struct strbuf buf
= STRBUF_INIT
;
2130 strbuf_addstr(&buf
, path
);
2131 r
= for_each_loose_file_in_objdir_buf(&buf
, obj_cb
, cruft_cb
,
2133 strbuf_release(&buf
);
2138 struct loose_alt_odb_data
{
2139 each_loose_object_fn
*cb
;
2143 static int loose_from_alt_odb(struct alternate_object_database
*alt
,
2146 struct loose_alt_odb_data
*data
= vdata
;
2147 struct strbuf buf
= STRBUF_INIT
;
2150 strbuf_addstr(&buf
, alt
->path
);
2151 r
= for_each_loose_file_in_objdir_buf(&buf
,
2152 data
->cb
, NULL
, NULL
,
2154 strbuf_release(&buf
);
2158 int for_each_loose_object(each_loose_object_fn cb
, void *data
,
2159 enum for_each_object_flags flags
)
2161 struct loose_alt_odb_data alt
;
2164 r
= for_each_loose_file_in_objdir(get_object_directory(),
2165 cb
, NULL
, NULL
, data
);
2169 if (flags
& FOR_EACH_OBJECT_LOCAL_ONLY
)
2174 return foreach_alt_odb(loose_from_alt_odb
, &alt
);
2177 static int check_stream_sha1(git_zstream
*stream
,
2181 const unsigned char *expected_sha1
)
2184 unsigned char real_sha1
[GIT_MAX_RAWSZ
];
2185 unsigned char buf
[4096];
2186 unsigned long total_read
;
2189 the_hash_algo
->init_fn(&c
);
2190 the_hash_algo
->update_fn(&c
, hdr
, stream
->total_out
);
2193 * We already read some bytes into hdr, but the ones up to the NUL
2194 * do not count against the object's content size.
2196 total_read
= stream
->total_out
- strlen(hdr
) - 1;
2199 * This size comparison must be "<=" to read the final zlib packets;
2200 * see the comment in unpack_sha1_rest for details.
2202 while (total_read
<= size
&&
2204 (status
== Z_BUF_ERROR
&& !stream
->avail_out
))) {
2205 stream
->next_out
= buf
;
2206 stream
->avail_out
= sizeof(buf
);
2207 if (size
- total_read
< stream
->avail_out
)
2208 stream
->avail_out
= size
- total_read
;
2209 status
= git_inflate(stream
, Z_FINISH
);
2210 the_hash_algo
->update_fn(&c
, buf
, stream
->next_out
- buf
);
2211 total_read
+= stream
->next_out
- buf
;
2213 git_inflate_end(stream
);
2215 if (status
!= Z_STREAM_END
) {
2216 error(_("corrupt loose object '%s'"), sha1_to_hex(expected_sha1
));
2219 if (stream
->avail_in
) {
2220 error(_("garbage at end of loose object '%s'"),
2221 sha1_to_hex(expected_sha1
));
2225 the_hash_algo
->final_fn(real_sha1
, &c
);
2226 if (!hasheq(expected_sha1
, real_sha1
)) {
2227 error(_("sha1 mismatch for %s (expected %s)"), path
,
2228 sha1_to_hex(expected_sha1
));
2235 int read_loose_object(const char *path
,
2236 const struct object_id
*expected_oid
,
2237 enum object_type
*type
,
2238 unsigned long *size
,
2243 unsigned long mapsize
;
2245 char hdr
[MAX_HEADER_LEN
];
2249 map
= map_sha1_file_1(the_repository
, path
, NULL
, &mapsize
);
2251 error_errno(_("unable to mmap %s"), path
);
2255 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0) {
2256 error(_("unable to unpack header of %s"), path
);
2260 *type
= parse_sha1_header(hdr
, size
);
2262 error(_("unable to parse header of %s"), path
);
2263 git_inflate_end(&stream
);
2267 if (*type
== OBJ_BLOB
&& *size
> big_file_threshold
) {
2268 if (check_stream_sha1(&stream
, hdr
, *size
, path
, expected_oid
->hash
) < 0)
2271 *contents
= unpack_sha1_rest(&stream
, hdr
, *size
, expected_oid
->hash
);
2273 error(_("unable to unpack contents of %s"), path
);
2274 git_inflate_end(&stream
);
2277 if (check_object_signature(expected_oid
, *contents
,
2278 *size
, type_name(*type
))) {
2279 error(_("sha1 mismatch for %s (expected %s)"), path
,
2280 oid_to_hex(expected_oid
));
2286 ret
= 0; /* everything checks out */
2290 munmap(map
, mapsize
);