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 static const char *odb_loose_path(struct object_directory
*odb
,
351 const unsigned char *sha1
)
354 strbuf_addstr(buf
, odb
->path
);
355 strbuf_addch(buf
, '/');
356 fill_sha1_path(buf
, sha1
);
360 const char *loose_object_path(struct repository
*r
, struct strbuf
*buf
,
361 const unsigned char *sha1
)
363 return odb_loose_path(r
->objects
->odb
, buf
, sha1
);
367 * Return non-zero iff the path is usable as an alternate object database.
369 static int alt_odb_usable(struct raw_object_store
*o
,
371 const char *normalized_objdir
)
373 struct object_directory
*odb
;
375 /* Detect cases where alternate disappeared */
376 if (!is_directory(path
->buf
)) {
377 error(_("object directory %s does not exist; "
378 "check .git/objects/info/alternates"),
384 * Prevent the common mistake of listing the same
385 * thing twice, or object directory itself.
387 for (odb
= o
->odb
; odb
; odb
= odb
->next
) {
388 if (!fspathcmp(path
->buf
, odb
->path
))
391 if (!fspathcmp(path
->buf
, normalized_objdir
))
398 * Prepare alternate object database registry.
400 * The variable alt_odb_list points at the list of struct
401 * object_directory. The elements on this list come from
402 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
403 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
404 * whose contents is similar to that environment variable but can be
405 * LF separated. Its base points at a statically allocated buffer that
406 * contains "/the/directory/corresponding/to/.git/objects/...", while
407 * its name points just after the slash at the end of ".git/objects/"
408 * in the example above, and has enough space to hold 40-byte hex
409 * SHA1, an extra slash for the first level indirection, and the
412 static void read_info_alternates(struct repository
*r
,
413 const char *relative_base
,
415 static int link_alt_odb_entry(struct repository
*r
, const char *entry
,
416 const char *relative_base
, int depth
, const char *normalized_objdir
)
418 struct object_directory
*ent
;
419 struct strbuf pathbuf
= STRBUF_INIT
;
421 if (!is_absolute_path(entry
) && relative_base
) {
422 strbuf_realpath(&pathbuf
, relative_base
, 1);
423 strbuf_addch(&pathbuf
, '/');
425 strbuf_addstr(&pathbuf
, entry
);
427 if (strbuf_normalize_path(&pathbuf
) < 0 && relative_base
) {
428 error(_("unable to normalize alternate object path: %s"),
430 strbuf_release(&pathbuf
);
435 * The trailing slash after the directory name is given by
436 * this function at the end. Remove duplicates.
438 while (pathbuf
.len
&& pathbuf
.buf
[pathbuf
.len
- 1] == '/')
439 strbuf_setlen(&pathbuf
, pathbuf
.len
- 1);
441 if (!alt_odb_usable(r
->objects
, &pathbuf
, normalized_objdir
)) {
442 strbuf_release(&pathbuf
);
446 ent
= xcalloc(1, sizeof(*ent
));
447 ent
->path
= xstrdup(pathbuf
.buf
);
449 /* add the alternate entry */
450 *r
->objects
->odb_tail
= ent
;
451 r
->objects
->odb_tail
= &(ent
->next
);
454 /* recursively add alternates */
455 read_info_alternates(r
, pathbuf
.buf
, depth
+ 1);
457 strbuf_release(&pathbuf
);
461 static const char *parse_alt_odb_entry(const char *string
,
469 if (*string
== '#') {
470 /* comment; consume up to next separator */
471 end
= strchrnul(string
, sep
);
472 } else if (*string
== '"' && !unquote_c_style(out
, string
, &end
)) {
474 * quoted path; unquote_c_style has copied the
475 * data for us and set "end". Broken quoting (e.g.,
476 * an entry that doesn't end with a quote) falls
477 * back to the unquoted case below.
480 /* normal, unquoted path */
481 end
= strchrnul(string
, sep
);
482 strbuf_add(out
, string
, end
- string
);
490 static void link_alt_odb_entries(struct repository
*r
, const char *alt
,
491 int sep
, const char *relative_base
, int depth
)
493 struct strbuf objdirbuf
= STRBUF_INIT
;
494 struct strbuf entry
= STRBUF_INIT
;
500 error(_("%s: ignoring alternate object stores, nesting too deep"),
505 strbuf_add_absolute_path(&objdirbuf
, r
->objects
->odb
->path
);
506 if (strbuf_normalize_path(&objdirbuf
) < 0)
507 die(_("unable to normalize object directory: %s"),
511 alt
= parse_alt_odb_entry(alt
, sep
, &entry
);
514 link_alt_odb_entry(r
, entry
.buf
,
515 relative_base
, depth
, objdirbuf
.buf
);
517 strbuf_release(&entry
);
518 strbuf_release(&objdirbuf
);
521 static void read_info_alternates(struct repository
*r
,
522 const char *relative_base
,
526 struct strbuf buf
= STRBUF_INIT
;
528 path
= xstrfmt("%s/info/alternates", relative_base
);
529 if (strbuf_read_file(&buf
, path
, 1024) < 0) {
530 warn_on_fopen_errors(path
);
535 link_alt_odb_entries(r
, buf
.buf
, '\n', relative_base
, depth
);
536 strbuf_release(&buf
);
540 void add_to_alternates_file(const char *reference
)
542 struct lock_file lock
= LOCK_INIT
;
543 char *alts
= git_pathdup("objects/info/alternates");
547 hold_lock_file_for_update(&lock
, alts
, LOCK_DIE_ON_ERROR
);
548 out
= fdopen_lock_file(&lock
, "w");
550 die_errno(_("unable to fdopen alternates lockfile"));
552 in
= fopen(alts
, "r");
554 struct strbuf line
= STRBUF_INIT
;
556 while (strbuf_getline(&line
, in
) != EOF
) {
557 if (!strcmp(reference
, line
.buf
)) {
561 fprintf_or_die(out
, "%s\n", line
.buf
);
564 strbuf_release(&line
);
567 else if (errno
!= ENOENT
)
568 die_errno(_("unable to read alternates file"));
571 rollback_lock_file(&lock
);
573 fprintf_or_die(out
, "%s\n", reference
);
574 if (commit_lock_file(&lock
))
575 die_errno(_("unable to move new alternates file into place"));
576 if (the_repository
->objects
->loaded_alternates
)
577 link_alt_odb_entries(the_repository
, reference
,
583 void add_to_alternates_memory(const char *reference
)
586 * Make sure alternates are initialized, or else our entry may be
587 * overwritten when they are.
589 prepare_alt_odb(the_repository
);
591 link_alt_odb_entries(the_repository
, reference
,
596 * Compute the exact path an alternate is at and returns it. In case of
597 * error NULL is returned and the human readable error is added to `err`
598 * `path` may be relative and should point to $GIT_DIR.
599 * `err` must not be null.
601 char *compute_alternate_path(const char *path
, struct strbuf
*err
)
603 char *ref_git
= NULL
;
604 const char *repo
, *ref_git_s
;
607 ref_git_s
= real_path_if_valid(path
);
610 strbuf_addf(err
, _("path '%s' does not exist"), path
);
614 * Beware: read_gitfile(), real_path() and mkpath()
615 * return static buffer
617 ref_git
= xstrdup(ref_git_s
);
619 repo
= read_gitfile(ref_git
);
621 repo
= read_gitfile(mkpath("%s/.git", ref_git
));
624 ref_git
= xstrdup(repo
);
627 if (!repo
&& is_directory(mkpath("%s/.git/objects", ref_git
))) {
628 char *ref_git_git
= mkpathdup("%s/.git", ref_git
);
630 ref_git
= ref_git_git
;
631 } else if (!is_directory(mkpath("%s/objects", ref_git
))) {
632 struct strbuf sb
= STRBUF_INIT
;
634 if (get_common_dir(&sb
, ref_git
)) {
636 _("reference repository '%s' as a linked "
637 "checkout is not supported yet."),
642 strbuf_addf(err
, _("reference repository '%s' is not a "
643 "local repository."), path
);
647 if (!access(mkpath("%s/shallow", ref_git
), F_OK
)) {
648 strbuf_addf(err
, _("reference repository '%s' is shallow"),
654 if (!access(mkpath("%s/info/grafts", ref_git
), F_OK
)) {
656 _("reference repository '%s' is grafted"),
664 FREE_AND_NULL(ref_git
);
670 int foreach_alt_odb(alt_odb_fn fn
, void *cb
)
672 struct object_directory
*ent
;
675 prepare_alt_odb(the_repository
);
676 for (ent
= the_repository
->objects
->odb
->next
; ent
; ent
= ent
->next
) {
684 void prepare_alt_odb(struct repository
*r
)
686 if (r
->objects
->loaded_alternates
)
689 link_alt_odb_entries(r
, r
->objects
->alternate_db
, PATH_SEP
, NULL
, 0);
691 read_info_alternates(r
, r
->objects
->odb
->path
, 0);
692 r
->objects
->loaded_alternates
= 1;
695 /* Returns 1 if we have successfully freshened the file, 0 otherwise. */
696 static int freshen_file(const char *fn
)
699 t
.actime
= t
.modtime
= time(NULL
);
700 return !utime(fn
, &t
);
704 * All of the check_and_freshen functions return 1 if the file exists and was
705 * freshened (if freshening was requested), 0 otherwise. If they return
706 * 0, you should not assume that it is safe to skip a write of the object (it
707 * either does not exist on disk, or has a stale mtime and may be subject to
710 int check_and_freshen_file(const char *fn
, int freshen
)
712 if (access(fn
, F_OK
))
714 if (freshen
&& !freshen_file(fn
))
719 static int check_and_freshen_odb(struct object_directory
*odb
,
720 const struct object_id
*oid
,
723 static struct strbuf path
= STRBUF_INIT
;
724 odb_loose_path(odb
, &path
, oid
->hash
);
725 return check_and_freshen_file(path
.buf
, freshen
);
728 static int check_and_freshen_local(const struct object_id
*oid
, int freshen
)
730 return check_and_freshen_odb(the_repository
->objects
->odb
, oid
, freshen
);
733 static int check_and_freshen_nonlocal(const struct object_id
*oid
, int freshen
)
735 struct object_directory
*odb
;
737 prepare_alt_odb(the_repository
);
738 for (odb
= the_repository
->objects
->odb
->next
; odb
; odb
= odb
->next
) {
739 if (check_and_freshen_odb(odb
, oid
, freshen
))
745 static int check_and_freshen(const struct object_id
*oid
, int freshen
)
747 return check_and_freshen_local(oid
, freshen
) ||
748 check_and_freshen_nonlocal(oid
, freshen
);
751 int has_loose_object_nonlocal(const struct object_id
*oid
)
753 return check_and_freshen_nonlocal(oid
, 0);
756 static int has_loose_object(const struct object_id
*oid
)
758 return check_and_freshen(oid
, 0);
761 static void mmap_limit_check(size_t length
)
763 static size_t limit
= 0;
765 limit
= git_env_ulong("GIT_MMAP_LIMIT", 0);
770 die(_("attempting to mmap %"PRIuMAX
" over limit %"PRIuMAX
),
771 (uintmax_t)length
, (uintmax_t)limit
);
774 void *xmmap_gently(void *start
, size_t length
,
775 int prot
, int flags
, int fd
, off_t offset
)
779 mmap_limit_check(length
);
780 ret
= mmap(start
, length
, prot
, flags
, fd
, offset
);
781 if (ret
== MAP_FAILED
) {
784 release_pack_memory(length
);
785 ret
= mmap(start
, length
, prot
, flags
, fd
, offset
);
790 void *xmmap(void *start
, size_t length
,
791 int prot
, int flags
, int fd
, off_t offset
)
793 void *ret
= xmmap_gently(start
, length
, prot
, flags
, fd
, offset
);
794 if (ret
== MAP_FAILED
)
795 die_errno(_("mmap failed"));
800 * With an in-core object data in "map", rehash it to make sure the
801 * object name actually matches "sha1" to detect object corruption.
802 * With "map" == NULL, try reading the object named with "sha1" using
803 * the streaming interface and rehash it to do the same.
805 int check_object_signature(const struct object_id
*oid
, void *map
,
806 unsigned long size
, const char *type
)
808 struct object_id real_oid
;
809 enum object_type obj_type
;
810 struct git_istream
*st
;
812 char hdr
[MAX_HEADER_LEN
];
816 hash_object_file(map
, size
, type
, &real_oid
);
817 return !oideq(oid
, &real_oid
) ? -1 : 0;
820 st
= open_istream(oid
, &obj_type
, &size
, NULL
);
824 /* Generate the header */
825 hdrlen
= xsnprintf(hdr
, sizeof(hdr
), "%s %"PRIuMAX
, type_name(obj_type
), (uintmax_t)size
) + 1;
828 the_hash_algo
->init_fn(&c
);
829 the_hash_algo
->update_fn(&c
, hdr
, hdrlen
);
832 ssize_t readlen
= read_istream(st
, buf
, sizeof(buf
));
840 the_hash_algo
->update_fn(&c
, buf
, readlen
);
842 the_hash_algo
->final_fn(real_oid
.hash
, &c
);
844 return !oideq(oid
, &real_oid
) ? -1 : 0;
847 int git_open_cloexec(const char *name
, int flags
)
850 static int o_cloexec
= O_CLOEXEC
;
852 fd
= open(name
, flags
| o_cloexec
);
853 if ((o_cloexec
& O_CLOEXEC
) && fd
< 0 && errno
== EINVAL
) {
854 /* Try again w/o O_CLOEXEC: the kernel might not support it */
855 o_cloexec
&= ~O_CLOEXEC
;
856 fd
= open(name
, flags
| o_cloexec
);
859 #if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
861 static int fd_cloexec
= FD_CLOEXEC
;
863 if (!o_cloexec
&& 0 <= fd
&& fd_cloexec
) {
864 /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */
865 int flags
= fcntl(fd
, F_GETFD
);
866 if (fcntl(fd
, F_SETFD
, flags
| fd_cloexec
))
875 * Find "sha1" as a loose object in the local repository or in an alternate.
876 * Returns 0 on success, negative on failure.
878 * The "path" out-parameter will give the path of the object we found (if any).
879 * Note that it may point to static storage and is only valid until another
880 * call to stat_sha1_file().
882 static int stat_sha1_file(struct repository
*r
, const unsigned char *sha1
,
883 struct stat
*st
, const char **path
)
885 struct object_directory
*odb
;
886 static struct strbuf buf
= STRBUF_INIT
;
889 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
890 *path
= odb_loose_path(odb
, &buf
, sha1
);
891 if (!lstat(*path
, st
))
899 * Like stat_sha1_file(), but actually open the object and return the
900 * descriptor. See the caveats on the "path" parameter above.
902 static int open_sha1_file(struct repository
*r
,
903 const unsigned char *sha1
, const char **path
)
906 struct object_directory
*odb
;
907 int most_interesting_errno
= ENOENT
;
908 static struct strbuf buf
= STRBUF_INIT
;
911 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
912 *path
= odb_loose_path(odb
, &buf
, sha1
);
913 fd
= git_open(*path
);
917 if (most_interesting_errno
== ENOENT
)
918 most_interesting_errno
= errno
;
920 errno
= most_interesting_errno
;
924 static int quick_has_loose(struct repository
*r
,
925 const unsigned char *sha1
)
927 struct object_id oid
;
928 struct object_directory
*odb
;
930 hashcpy(oid
.hash
, sha1
);
933 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
934 if (oid_array_lookup(odb_loose_cache(odb
, &oid
), &oid
) >= 0)
941 * Map the loose object at "path" if it is not NULL, or the path found by
942 * searching for a loose object named "sha1".
944 static void *map_sha1_file_1(struct repository
*r
, const char *path
,
945 const unsigned char *sha1
, unsigned long *size
)
953 fd
= open_sha1_file(r
, sha1
, &path
);
958 if (!fstat(fd
, &st
)) {
959 *size
= xsize_t(st
.st_size
);
961 /* mmap() is forbidden on empty files */
962 error(_("object file %s is empty"), path
);
966 map
= xmmap(NULL
, *size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
973 void *map_sha1_file(struct repository
*r
,
974 const unsigned char *sha1
, unsigned long *size
)
976 return map_sha1_file_1(r
, NULL
, sha1
, size
);
979 static int unpack_sha1_short_header(git_zstream
*stream
,
980 unsigned char *map
, unsigned long mapsize
,
981 void *buffer
, unsigned long bufsiz
)
983 /* Get the data stream */
984 memset(stream
, 0, sizeof(*stream
));
985 stream
->next_in
= map
;
986 stream
->avail_in
= mapsize
;
987 stream
->next_out
= buffer
;
988 stream
->avail_out
= bufsiz
;
990 git_inflate_init(stream
);
991 return git_inflate(stream
, 0);
994 int unpack_sha1_header(git_zstream
*stream
,
995 unsigned char *map
, unsigned long mapsize
,
996 void *buffer
, unsigned long bufsiz
)
998 int status
= unpack_sha1_short_header(stream
, map
, mapsize
,
1004 /* Make sure we have the terminating NUL */
1005 if (!memchr(buffer
, '\0', stream
->next_out
- (unsigned char *)buffer
))
1010 static int unpack_sha1_header_to_strbuf(git_zstream
*stream
, unsigned char *map
,
1011 unsigned long mapsize
, void *buffer
,
1012 unsigned long bufsiz
, struct strbuf
*header
)
1016 status
= unpack_sha1_short_header(stream
, map
, mapsize
, buffer
, bufsiz
);
1021 * Check if entire header is unpacked in the first iteration.
1023 if (memchr(buffer
, '\0', stream
->next_out
- (unsigned char *)buffer
))
1027 * buffer[0..bufsiz] was not large enough. Copy the partial
1028 * result out to header, and then append the result of further
1029 * reading the stream.
1031 strbuf_add(header
, buffer
, stream
->next_out
- (unsigned char *)buffer
);
1032 stream
->next_out
= buffer
;
1033 stream
->avail_out
= bufsiz
;
1036 status
= git_inflate(stream
, 0);
1037 strbuf_add(header
, buffer
, stream
->next_out
- (unsigned char *)buffer
);
1038 if (memchr(buffer
, '\0', stream
->next_out
- (unsigned char *)buffer
))
1040 stream
->next_out
= buffer
;
1041 stream
->avail_out
= bufsiz
;
1042 } while (status
!= Z_STREAM_END
);
1046 static void *unpack_sha1_rest(git_zstream
*stream
, void *buffer
, unsigned long size
, const unsigned char *sha1
)
1048 int bytes
= strlen(buffer
) + 1;
1049 unsigned char *buf
= xmallocz(size
);
1053 n
= stream
->total_out
- bytes
;
1056 memcpy(buf
, (char *) buffer
+ bytes
, n
);
1058 if (bytes
<= size
) {
1060 * The above condition must be (bytes <= size), not
1061 * (bytes < size). In other words, even though we
1062 * expect no more output and set avail_out to zero,
1063 * the input zlib stream may have bytes that express
1064 * "this concludes the stream", and we *do* want to
1067 * Otherwise we would not be able to test that we
1068 * consumed all the input to reach the expected size;
1069 * we also want to check that zlib tells us that all
1070 * went well with status == Z_STREAM_END at the end.
1072 stream
->next_out
= buf
+ bytes
;
1073 stream
->avail_out
= size
- bytes
;
1074 while (status
== Z_OK
)
1075 status
= git_inflate(stream
, Z_FINISH
);
1077 if (status
== Z_STREAM_END
&& !stream
->avail_in
) {
1078 git_inflate_end(stream
);
1083 error(_("corrupt loose object '%s'"), sha1_to_hex(sha1
));
1084 else if (stream
->avail_in
)
1085 error(_("garbage at end of loose object '%s'"),
1092 * We used to just use "sscanf()", but that's actually way
1093 * too permissive for what we want to check. So do an anal
1094 * object header parse by hand.
1096 static int parse_sha1_header_extended(const char *hdr
, struct object_info
*oi
,
1099 const char *type_buf
= hdr
;
1101 int type
, type_len
= 0;
1104 * The type can be of any size but is followed by
1116 type
= type_from_string_gently(type_buf
, type_len
, 1);
1118 strbuf_add(oi
->type_name
, type_buf
, type_len
);
1120 * Set type to 0 if its an unknown object and
1121 * we're obtaining the type using '--allow-unknown-type'
1124 if ((flags
& OBJECT_INFO_ALLOW_UNKNOWN_TYPE
) && (type
< 0))
1127 die(_("invalid object type"));
1132 * The length must follow immediately, and be in canonical
1133 * decimal format (ie "010" is not valid).
1135 size
= *hdr
++ - '0';
1140 unsigned long c
= *hdr
- '0';
1144 size
= size
* 10 + c
;
1152 * The length must be followed by a zero byte
1154 return *hdr
? -1 : type
;
1157 int parse_sha1_header(const char *hdr
, unsigned long *sizep
)
1159 struct object_info oi
= OBJECT_INFO_INIT
;
1162 return parse_sha1_header_extended(hdr
, &oi
, 0);
1165 static int sha1_loose_object_info(struct repository
*r
,
1166 const unsigned char *sha1
,
1167 struct object_info
*oi
, int flags
)
1170 unsigned long mapsize
;
1173 char hdr
[MAX_HEADER_LEN
];
1174 struct strbuf hdrbuf
= STRBUF_INIT
;
1175 unsigned long size_scratch
;
1177 if (oi
->delta_base_sha1
)
1178 hashclr(oi
->delta_base_sha1
);
1181 * If we don't care about type or size, then we don't
1182 * need to look inside the object at all. Note that we
1183 * do not optimize out the stat call, even if the
1184 * caller doesn't care about the disk-size, since our
1185 * return value implicitly indicates whether the
1186 * object even exists.
1188 if (!oi
->typep
&& !oi
->type_name
&& !oi
->sizep
&& !oi
->contentp
) {
1191 if (!oi
->disk_sizep
&& (flags
& OBJECT_INFO_QUICK
))
1192 return quick_has_loose(r
, sha1
) ? 0 : -1;
1193 if (stat_sha1_file(r
, sha1
, &st
, &path
) < 0)
1196 *oi
->disk_sizep
= st
.st_size
;
1200 map
= map_sha1_file(r
, sha1
, &mapsize
);
1205 oi
->sizep
= &size_scratch
;
1208 *oi
->disk_sizep
= mapsize
;
1209 if ((flags
& OBJECT_INFO_ALLOW_UNKNOWN_TYPE
)) {
1210 if (unpack_sha1_header_to_strbuf(&stream
, map
, mapsize
, hdr
, sizeof(hdr
), &hdrbuf
) < 0)
1211 status
= error(_("unable to unpack %s header with --allow-unknown-type"),
1213 } else if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
1214 status
= error(_("unable to unpack %s header"),
1218 else if (hdrbuf
.len
) {
1219 if ((status
= parse_sha1_header_extended(hdrbuf
.buf
, oi
, flags
)) < 0)
1220 status
= error(_("unable to parse %s header with --allow-unknown-type"),
1222 } else if ((status
= parse_sha1_header_extended(hdr
, oi
, flags
)) < 0)
1223 status
= error(_("unable to parse %s header"), sha1_to_hex(sha1
));
1225 if (status
>= 0 && oi
->contentp
) {
1226 *oi
->contentp
= unpack_sha1_rest(&stream
, hdr
,
1228 if (!*oi
->contentp
) {
1229 git_inflate_end(&stream
);
1233 git_inflate_end(&stream
);
1235 munmap(map
, mapsize
);
1236 if (status
&& oi
->typep
)
1237 *oi
->typep
= status
;
1238 if (oi
->sizep
== &size_scratch
)
1240 strbuf_release(&hdrbuf
);
1241 oi
->whence
= OI_LOOSE
;
1242 return (status
< 0) ? status
: 0;
1245 int fetch_if_missing
= 1;
1247 int oid_object_info_extended(struct repository
*r
, const struct object_id
*oid
,
1248 struct object_info
*oi
, unsigned flags
)
1250 static struct object_info blank_oi
= OBJECT_INFO_INIT
;
1251 struct pack_entry e
;
1253 const struct object_id
*real
= oid
;
1254 int already_retried
= 0;
1256 if (flags
& OBJECT_INFO_LOOKUP_REPLACE
)
1257 real
= lookup_replace_object(r
, oid
);
1259 if (is_null_oid(real
))
1265 if (!(flags
& OBJECT_INFO_SKIP_CACHED
)) {
1266 struct cached_object
*co
= find_cached_object(real
);
1269 *(oi
->typep
) = co
->type
;
1271 *(oi
->sizep
) = co
->size
;
1273 *(oi
->disk_sizep
) = 0;
1274 if (oi
->delta_base_sha1
)
1275 hashclr(oi
->delta_base_sha1
);
1277 strbuf_addstr(oi
->type_name
, type_name(co
->type
));
1279 *oi
->contentp
= xmemdupz(co
->buf
, co
->size
);
1280 oi
->whence
= OI_CACHED
;
1286 if (find_pack_entry(r
, real
, &e
))
1289 if (flags
& OBJECT_INFO_IGNORE_LOOSE
)
1292 /* Most likely it's a loose object. */
1293 if (!sha1_loose_object_info(r
, real
->hash
, oi
, flags
))
1296 /* Not a loose object; someone else may have just packed it. */
1297 if (!(flags
& OBJECT_INFO_QUICK
)) {
1298 reprepare_packed_git(r
);
1299 if (find_pack_entry(r
, real
, &e
))
1303 /* Check if it is a missing object */
1304 if (fetch_if_missing
&& repository_format_partial_clone
&&
1305 !already_retried
&& r
== the_repository
) {
1307 * TODO Investigate having fetch_object() return
1308 * TODO error/success and stopping the music here.
1309 * TODO Pass a repository struct through fetch_object,
1310 * such that arbitrary repositories work.
1312 fetch_objects(repository_format_partial_clone
, real
, 1);
1313 already_retried
= 1;
1320 if (oi
== &blank_oi
)
1322 * We know that the caller doesn't actually need the
1323 * information below, so return early.
1326 rtype
= packed_object_info(r
, e
.p
, e
.offset
, oi
);
1328 mark_bad_packed_object(e
.p
, real
->hash
);
1329 return oid_object_info_extended(r
, real
, oi
, 0);
1330 } else if (oi
->whence
== OI_PACKED
) {
1331 oi
->u
.packed
.offset
= e
.offset
;
1332 oi
->u
.packed
.pack
= e
.p
;
1333 oi
->u
.packed
.is_delta
= (rtype
== OBJ_REF_DELTA
||
1334 rtype
== OBJ_OFS_DELTA
);
1340 /* returns enum object_type or negative */
1341 int oid_object_info(struct repository
*r
,
1342 const struct object_id
*oid
,
1343 unsigned long *sizep
)
1345 enum object_type type
;
1346 struct object_info oi
= OBJECT_INFO_INIT
;
1350 if (oid_object_info_extended(r
, oid
, &oi
,
1351 OBJECT_INFO_LOOKUP_REPLACE
) < 0)
1356 static void *read_object(const unsigned char *sha1
, enum object_type
*type
,
1357 unsigned long *size
)
1359 struct object_id oid
;
1360 struct object_info oi
= OBJECT_INFO_INIT
;
1364 oi
.contentp
= &content
;
1366 hashcpy(oid
.hash
, sha1
);
1368 if (oid_object_info_extended(the_repository
, &oid
, &oi
, 0) < 0)
1373 int pretend_object_file(void *buf
, unsigned long len
, enum object_type type
,
1374 struct object_id
*oid
)
1376 struct cached_object
*co
;
1378 hash_object_file(buf
, len
, type_name(type
), oid
);
1379 if (has_sha1_file(oid
->hash
) || find_cached_object(oid
))
1381 ALLOC_GROW(cached_objects
, cached_object_nr
+ 1, cached_object_alloc
);
1382 co
= &cached_objects
[cached_object_nr
++];
1385 co
->buf
= xmalloc(len
);
1386 memcpy(co
->buf
, buf
, len
);
1387 oidcpy(&co
->oid
, oid
);
1392 * This function dies on corrupt objects; the callers who want to
1393 * deal with them should arrange to call read_object() and give error
1394 * messages themselves.
1396 void *read_object_file_extended(const struct object_id
*oid
,
1397 enum object_type
*type
,
1398 unsigned long *size
,
1402 const struct packed_git
*p
;
1405 const struct object_id
*repl
= lookup_replace
?
1406 lookup_replace_object(the_repository
, oid
) : oid
;
1409 data
= read_object(repl
->hash
, type
, size
);
1413 if (errno
&& errno
!= ENOENT
)
1414 die_errno(_("failed to read object %s"), oid_to_hex(oid
));
1416 /* die if we replaced an object with one that does not exist */
1418 die(_("replacement %s not found for %s"),
1419 oid_to_hex(repl
), oid_to_hex(oid
));
1421 if (!stat_sha1_file(the_repository
, repl
->hash
, &st
, &path
))
1422 die(_("loose object %s (stored in %s) is corrupt"),
1423 oid_to_hex(repl
), path
);
1425 if ((p
= has_packed_and_bad(repl
->hash
)) != NULL
)
1426 die(_("packed object %s (stored in %s) is corrupt"),
1427 oid_to_hex(repl
), p
->pack_name
);
1432 void *read_object_with_reference(const struct object_id
*oid
,
1433 const char *required_type_name
,
1434 unsigned long *size
,
1435 struct object_id
*actual_oid_return
)
1437 enum object_type type
, required_type
;
1439 unsigned long isize
;
1440 struct object_id actual_oid
;
1442 required_type
= type_from_string(required_type_name
);
1443 oidcpy(&actual_oid
, oid
);
1445 int ref_length
= -1;
1446 const char *ref_type
= NULL
;
1448 buffer
= read_object_file(&actual_oid
, &type
, &isize
);
1451 if (type
== required_type
) {
1453 if (actual_oid_return
)
1454 oidcpy(actual_oid_return
, &actual_oid
);
1457 /* Handle references */
1458 else if (type
== OBJ_COMMIT
)
1460 else if (type
== OBJ_TAG
)
1461 ref_type
= "object ";
1466 ref_length
= strlen(ref_type
);
1468 if (ref_length
+ the_hash_algo
->hexsz
> isize
||
1469 memcmp(buffer
, ref_type
, ref_length
) ||
1470 get_oid_hex((char *) buffer
+ ref_length
, &actual_oid
)) {
1475 /* Now we have the ID of the referred-to object in
1476 * actual_oid. Check again. */
1480 static void write_object_file_prepare(const void *buf
, unsigned long len
,
1481 const char *type
, struct object_id
*oid
,
1482 char *hdr
, int *hdrlen
)
1486 /* Generate the header */
1487 *hdrlen
= xsnprintf(hdr
, *hdrlen
, "%s %"PRIuMAX
, type
, (uintmax_t)len
)+1;
1490 the_hash_algo
->init_fn(&c
);
1491 the_hash_algo
->update_fn(&c
, hdr
, *hdrlen
);
1492 the_hash_algo
->update_fn(&c
, buf
, len
);
1493 the_hash_algo
->final_fn(oid
->hash
, &c
);
1497 * Move the just written object into its final resting place.
1499 int finalize_object_file(const char *tmpfile
, const char *filename
)
1503 if (object_creation_mode
== OBJECT_CREATION_USES_RENAMES
)
1505 else if (link(tmpfile
, filename
))
1509 * Coda hack - coda doesn't like cross-directory links,
1510 * so we fall back to a rename, which will mean that it
1511 * won't be able to check collisions, but that's not a
1514 * The same holds for FAT formatted media.
1516 * When this succeeds, we just return. We have nothing
1519 if (ret
&& ret
!= EEXIST
) {
1521 if (!rename(tmpfile
, filename
))
1525 unlink_or_warn(tmpfile
);
1527 if (ret
!= EEXIST
) {
1528 return error_errno(_("unable to write sha1 filename %s"), filename
);
1530 /* FIXME!!! Collision check here ? */
1534 if (adjust_shared_perm(filename
))
1535 return error(_("unable to set permission to '%s'"), filename
);
1539 static int write_buffer(int fd
, const void *buf
, size_t len
)
1541 if (write_in_full(fd
, buf
, len
) < 0)
1542 return error_errno(_("file write error"));
1546 int hash_object_file(const void *buf
, unsigned long len
, const char *type
,
1547 struct object_id
*oid
)
1549 char hdr
[MAX_HEADER_LEN
];
1550 int hdrlen
= sizeof(hdr
);
1551 write_object_file_prepare(buf
, len
, type
, oid
, hdr
, &hdrlen
);
1555 /* Finalize a file on disk, and close it. */
1556 static void close_sha1_file(int fd
)
1558 if (fsync_object_files
)
1559 fsync_or_die(fd
, "sha1 file");
1561 die_errno(_("error when closing sha1 file"));
1564 /* Size of directory component, including the ending '/' */
1565 static inline int directory_size(const char *filename
)
1567 const char *s
= strrchr(filename
, '/');
1570 return s
- filename
+ 1;
1574 * This creates a temporary file in the same directory as the final
1577 * We want to avoid cross-directory filename renames, because those
1578 * can have problems on various filesystems (FAT, NFS, Coda).
1580 static int create_tmpfile(struct strbuf
*tmp
, const char *filename
)
1582 int fd
, dirlen
= directory_size(filename
);
1585 strbuf_add(tmp
, filename
, dirlen
);
1586 strbuf_addstr(tmp
, "tmp_obj_XXXXXX");
1587 fd
= git_mkstemp_mode(tmp
->buf
, 0444);
1588 if (fd
< 0 && dirlen
&& errno
== ENOENT
) {
1590 * Make sure the directory exists; note that the contents
1591 * of the buffer are undefined after mkstemp returns an
1592 * error, so we have to rewrite the whole buffer from
1596 strbuf_add(tmp
, filename
, dirlen
- 1);
1597 if (mkdir(tmp
->buf
, 0777) && errno
!= EEXIST
)
1599 if (adjust_shared_perm(tmp
->buf
))
1603 strbuf_addstr(tmp
, "/tmp_obj_XXXXXX");
1604 fd
= git_mkstemp_mode(tmp
->buf
, 0444);
1609 static int write_loose_object(const struct object_id
*oid
, char *hdr
,
1610 int hdrlen
, const void *buf
, unsigned long len
,
1614 unsigned char compressed
[4096];
1617 struct object_id parano_oid
;
1618 static struct strbuf tmp_file
= STRBUF_INIT
;
1619 static struct strbuf filename
= STRBUF_INIT
;
1621 loose_object_path(the_repository
, &filename
, oid
->hash
);
1623 fd
= create_tmpfile(&tmp_file
, filename
.buf
);
1625 if (errno
== EACCES
)
1626 return error(_("insufficient permission for adding an object to repository database %s"), get_object_directory());
1628 return error_errno(_("unable to create temporary file"));
1632 git_deflate_init(&stream
, zlib_compression_level
);
1633 stream
.next_out
= compressed
;
1634 stream
.avail_out
= sizeof(compressed
);
1635 the_hash_algo
->init_fn(&c
);
1637 /* First header.. */
1638 stream
.next_in
= (unsigned char *)hdr
;
1639 stream
.avail_in
= hdrlen
;
1640 while (git_deflate(&stream
, 0) == Z_OK
)
1642 the_hash_algo
->update_fn(&c
, hdr
, hdrlen
);
1644 /* Then the data itself.. */
1645 stream
.next_in
= (void *)buf
;
1646 stream
.avail_in
= len
;
1648 unsigned char *in0
= stream
.next_in
;
1649 ret
= git_deflate(&stream
, Z_FINISH
);
1650 the_hash_algo
->update_fn(&c
, in0
, stream
.next_in
- in0
);
1651 if (write_buffer(fd
, compressed
, stream
.next_out
- compressed
) < 0)
1652 die(_("unable to write sha1 file"));
1653 stream
.next_out
= compressed
;
1654 stream
.avail_out
= sizeof(compressed
);
1655 } while (ret
== Z_OK
);
1657 if (ret
!= Z_STREAM_END
)
1658 die(_("unable to deflate new object %s (%d)"), oid_to_hex(oid
),
1660 ret
= git_deflate_end_gently(&stream
);
1662 die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid
),
1664 the_hash_algo
->final_fn(parano_oid
.hash
, &c
);
1665 if (!oideq(oid
, ¶no_oid
))
1666 die(_("confused by unstable object source data for %s"),
1669 close_sha1_file(fd
);
1674 utb
.modtime
= mtime
;
1675 if (utime(tmp_file
.buf
, &utb
) < 0)
1676 warning_errno(_("failed utime() on %s"), tmp_file
.buf
);
1679 return finalize_object_file(tmp_file
.buf
, filename
.buf
);
1682 static int freshen_loose_object(const struct object_id
*oid
)
1684 return check_and_freshen(oid
, 1);
1687 static int freshen_packed_object(const struct object_id
*oid
)
1689 struct pack_entry e
;
1690 if (!find_pack_entry(the_repository
, oid
, &e
))
1694 if (!freshen_file(e
.p
->pack_name
))
1700 int write_object_file(const void *buf
, unsigned long len
, const char *type
,
1701 struct object_id
*oid
)
1703 char hdr
[MAX_HEADER_LEN
];
1704 int hdrlen
= sizeof(hdr
);
1706 /* Normally if we have it in the pack then we do not bother writing
1707 * it out into .git/objects/??/?{38} file.
1709 write_object_file_prepare(buf
, len
, type
, oid
, hdr
, &hdrlen
);
1710 if (freshen_packed_object(oid
) || freshen_loose_object(oid
))
1712 return write_loose_object(oid
, hdr
, hdrlen
, buf
, len
, 0);
1715 int hash_object_file_literally(const void *buf
, unsigned long len
,
1716 const char *type
, struct object_id
*oid
,
1720 int hdrlen
, status
= 0;
1722 /* type string, SP, %lu of the length plus NUL must fit this */
1723 hdrlen
= strlen(type
) + MAX_HEADER_LEN
;
1724 header
= xmalloc(hdrlen
);
1725 write_object_file_prepare(buf
, len
, type
, oid
, header
, &hdrlen
);
1727 if (!(flags
& HASH_WRITE_OBJECT
))
1729 if (freshen_packed_object(oid
) || freshen_loose_object(oid
))
1731 status
= write_loose_object(oid
, header
, hdrlen
, buf
, len
, 0);
1738 int force_object_loose(const struct object_id
*oid
, time_t mtime
)
1742 enum object_type type
;
1743 char hdr
[MAX_HEADER_LEN
];
1747 if (has_loose_object(oid
))
1749 buf
= read_object(oid
->hash
, &type
, &len
);
1751 return error(_("cannot read sha1_file for %s"), oid_to_hex(oid
));
1752 hdrlen
= xsnprintf(hdr
, sizeof(hdr
), "%s %"PRIuMAX
, type_name(type
), (uintmax_t)len
) + 1;
1753 ret
= write_loose_object(oid
, hdr
, hdrlen
, buf
, len
, mtime
);
1759 int has_sha1_file_with_flags(const unsigned char *sha1
, int flags
)
1761 struct object_id oid
;
1762 if (!startup_info
->have_repository
)
1764 hashcpy(oid
.hash
, sha1
);
1765 return oid_object_info_extended(the_repository
, &oid
, NULL
,
1766 flags
| OBJECT_INFO_SKIP_CACHED
) >= 0;
1769 int has_object_file(const struct object_id
*oid
)
1771 return has_sha1_file(oid
->hash
);
1774 int has_object_file_with_flags(const struct object_id
*oid
, int flags
)
1776 return has_sha1_file_with_flags(oid
->hash
, flags
);
1779 static void check_tree(const void *buf
, size_t size
)
1781 struct tree_desc desc
;
1782 struct name_entry entry
;
1784 init_tree_desc(&desc
, buf
, size
);
1785 while (tree_entry(&desc
, &entry
))
1787 * tree_entry() will die() on malformed entries */
1791 static void check_commit(const void *buf
, size_t size
)
1794 memset(&c
, 0, sizeof(c
));
1795 if (parse_commit_buffer(the_repository
, &c
, buf
, size
, 0))
1796 die(_("corrupt commit"));
1799 static void check_tag(const void *buf
, size_t size
)
1802 memset(&t
, 0, sizeof(t
));
1803 if (parse_tag_buffer(the_repository
, &t
, buf
, size
))
1804 die(_("corrupt tag"));
1807 static int index_mem(struct index_state
*istate
,
1808 struct object_id
*oid
, void *buf
, size_t size
,
1809 enum object_type type
,
1810 const char *path
, unsigned flags
)
1812 int ret
, re_allocated
= 0;
1813 int write_object
= flags
& HASH_WRITE_OBJECT
;
1819 * Convert blobs to git internal format
1821 if ((type
== OBJ_BLOB
) && path
) {
1822 struct strbuf nbuf
= STRBUF_INIT
;
1823 if (convert_to_git(istate
, path
, buf
, size
, &nbuf
,
1824 get_conv_flags(flags
))) {
1825 buf
= strbuf_detach(&nbuf
, &size
);
1829 if (flags
& HASH_FORMAT_CHECK
) {
1830 if (type
== OBJ_TREE
)
1831 check_tree(buf
, size
);
1832 if (type
== OBJ_COMMIT
)
1833 check_commit(buf
, size
);
1834 if (type
== OBJ_TAG
)
1835 check_tag(buf
, size
);
1839 ret
= write_object_file(buf
, size
, type_name(type
), oid
);
1841 ret
= hash_object_file(buf
, size
, type_name(type
), oid
);
1847 static int index_stream_convert_blob(struct index_state
*istate
,
1848 struct object_id
*oid
,
1854 const int write_object
= flags
& HASH_WRITE_OBJECT
;
1855 struct strbuf sbuf
= STRBUF_INIT
;
1858 assert(would_convert_to_git_filter_fd(istate
, path
));
1860 convert_to_git_filter_fd(istate
, path
, fd
, &sbuf
,
1861 get_conv_flags(flags
));
1864 ret
= write_object_file(sbuf
.buf
, sbuf
.len
, type_name(OBJ_BLOB
),
1867 ret
= hash_object_file(sbuf
.buf
, sbuf
.len
, type_name(OBJ_BLOB
),
1869 strbuf_release(&sbuf
);
1873 static int index_pipe(struct index_state
*istate
, struct object_id
*oid
,
1874 int fd
, enum object_type type
,
1875 const char *path
, unsigned flags
)
1877 struct strbuf sbuf
= STRBUF_INIT
;
1880 if (strbuf_read(&sbuf
, fd
, 4096) >= 0)
1881 ret
= index_mem(istate
, oid
, sbuf
.buf
, sbuf
.len
, type
, path
, flags
);
1884 strbuf_release(&sbuf
);
1888 #define SMALL_FILE_SIZE (32*1024)
1890 static int index_core(struct index_state
*istate
,
1891 struct object_id
*oid
, int fd
, size_t size
,
1892 enum object_type type
, const char *path
,
1898 ret
= index_mem(istate
, oid
, "", size
, type
, path
, flags
);
1899 } else if (size
<= SMALL_FILE_SIZE
) {
1900 char *buf
= xmalloc(size
);
1901 ssize_t read_result
= read_in_full(fd
, buf
, size
);
1902 if (read_result
< 0)
1903 ret
= error_errno(_("read error while indexing %s"),
1904 path
? path
: "<unknown>");
1905 else if (read_result
!= size
)
1906 ret
= error(_("short read while indexing %s"),
1907 path
? path
: "<unknown>");
1909 ret
= index_mem(istate
, oid
, buf
, size
, type
, path
, flags
);
1912 void *buf
= xmmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1913 ret
= index_mem(istate
, oid
, buf
, size
, type
, path
, flags
);
1920 * This creates one packfile per large blob unless bulk-checkin
1921 * machinery is "plugged".
1923 * This also bypasses the usual "convert-to-git" dance, and that is on
1924 * purpose. We could write a streaming version of the converting
1925 * functions and insert that before feeding the data to fast-import
1926 * (or equivalent in-core API described above). However, that is
1927 * somewhat complicated, as we do not know the size of the filter
1928 * result, which we need to know beforehand when writing a git object.
1929 * Since the primary motivation for trying to stream from the working
1930 * tree file and to avoid mmaping it in core is to deal with large
1931 * binary blobs, they generally do not want to get any conversion, and
1932 * callers should avoid this code path when filters are requested.
1934 static int index_stream(struct object_id
*oid
, int fd
, size_t size
,
1935 enum object_type type
, const char *path
,
1938 return index_bulk_checkin(oid
, fd
, size
, type
, path
, flags
);
1941 int index_fd(struct index_state
*istate
, struct object_id
*oid
,
1942 int fd
, struct stat
*st
,
1943 enum object_type type
, const char *path
, unsigned flags
)
1948 * Call xsize_t() only when needed to avoid potentially unnecessary
1949 * die() for large files.
1951 if (type
== OBJ_BLOB
&& path
&& would_convert_to_git_filter_fd(istate
, path
))
1952 ret
= index_stream_convert_blob(istate
, oid
, fd
, path
, flags
);
1953 else if (!S_ISREG(st
->st_mode
))
1954 ret
= index_pipe(istate
, oid
, fd
, type
, path
, flags
);
1955 else if (st
->st_size
<= big_file_threshold
|| type
!= OBJ_BLOB
||
1956 (path
&& would_convert_to_git(istate
, path
)))
1957 ret
= index_core(istate
, oid
, fd
, xsize_t(st
->st_size
),
1960 ret
= index_stream(oid
, fd
, xsize_t(st
->st_size
), type
, path
,
1966 int index_path(struct index_state
*istate
, struct object_id
*oid
,
1967 const char *path
, struct stat
*st
, unsigned flags
)
1970 struct strbuf sb
= STRBUF_INIT
;
1973 switch (st
->st_mode
& S_IFMT
) {
1975 fd
= open(path
, O_RDONLY
);
1977 return error_errno("open(\"%s\")", path
);
1978 if (index_fd(istate
, oid
, fd
, st
, OBJ_BLOB
, path
, flags
) < 0)
1979 return error(_("%s: failed to insert into database"),
1983 if (strbuf_readlink(&sb
, path
, st
->st_size
))
1984 return error_errno("readlink(\"%s\")", path
);
1985 if (!(flags
& HASH_WRITE_OBJECT
))
1986 hash_object_file(sb
.buf
, sb
.len
, blob_type
, oid
);
1987 else if (write_object_file(sb
.buf
, sb
.len
, blob_type
, oid
))
1988 rc
= error(_("%s: failed to insert into database"), path
);
1989 strbuf_release(&sb
);
1992 return resolve_gitlink_ref(path
, "HEAD", oid
);
1994 return error(_("%s: unsupported file type"), path
);
1999 int read_pack_header(int fd
, struct pack_header
*header
)
2001 if (read_in_full(fd
, header
, sizeof(*header
)) != sizeof(*header
))
2002 /* "eof before pack header was fully read" */
2003 return PH_ERROR_EOF
;
2005 if (header
->hdr_signature
!= htonl(PACK_SIGNATURE
))
2006 /* "protocol error (pack signature mismatch detected)" */
2007 return PH_ERROR_PACK_SIGNATURE
;
2008 if (!pack_version_ok(header
->hdr_version
))
2009 /* "protocol error (pack version unsupported)" */
2010 return PH_ERROR_PROTOCOL
;
2014 void assert_oid_type(const struct object_id
*oid
, enum object_type expect
)
2016 enum object_type type
= oid_object_info(the_repository
, oid
, NULL
);
2018 die(_("%s is not a valid object"), oid_to_hex(oid
));
2020 die(_("%s is not a valid '%s' object"), oid_to_hex(oid
),
2024 int for_each_file_in_obj_subdir(unsigned int subdir_nr
,
2025 struct strbuf
*path
,
2026 each_loose_object_fn obj_cb
,
2027 each_loose_cruft_fn cruft_cb
,
2028 each_loose_subdir_fn subdir_cb
,
2031 size_t origlen
, baselen
;
2035 struct object_id oid
;
2037 if (subdir_nr
> 0xff)
2038 BUG("invalid loose object subdirectory: %x", subdir_nr
);
2040 origlen
= path
->len
;
2041 strbuf_complete(path
, '/');
2042 strbuf_addf(path
, "%02x", subdir_nr
);
2044 dir
= opendir(path
->buf
);
2046 if (errno
!= ENOENT
)
2047 r
= error_errno(_("unable to open %s"), path
->buf
);
2048 strbuf_setlen(path
, origlen
);
2052 oid
.hash
[0] = subdir_nr
;
2053 strbuf_addch(path
, '/');
2054 baselen
= path
->len
;
2056 while ((de
= readdir(dir
))) {
2058 if (is_dot_or_dotdot(de
->d_name
))
2061 namelen
= strlen(de
->d_name
);
2062 strbuf_setlen(path
, baselen
);
2063 strbuf_add(path
, de
->d_name
, namelen
);
2064 if (namelen
== the_hash_algo
->hexsz
- 2 &&
2065 !hex_to_bytes(oid
.hash
+ 1, de
->d_name
,
2066 the_hash_algo
->rawsz
- 1)) {
2068 r
= obj_cb(&oid
, path
->buf
, data
);
2076 r
= cruft_cb(de
->d_name
, path
->buf
, data
);
2083 strbuf_setlen(path
, baselen
- 1);
2084 if (!r
&& subdir_cb
)
2085 r
= subdir_cb(subdir_nr
, path
->buf
, data
);
2087 strbuf_setlen(path
, origlen
);
2092 int for_each_loose_file_in_objdir_buf(struct strbuf
*path
,
2093 each_loose_object_fn obj_cb
,
2094 each_loose_cruft_fn cruft_cb
,
2095 each_loose_subdir_fn subdir_cb
,
2101 for (i
= 0; i
< 256; i
++) {
2102 r
= for_each_file_in_obj_subdir(i
, path
, obj_cb
, cruft_cb
,
2111 int for_each_loose_file_in_objdir(const char *path
,
2112 each_loose_object_fn obj_cb
,
2113 each_loose_cruft_fn cruft_cb
,
2114 each_loose_subdir_fn subdir_cb
,
2117 struct strbuf buf
= STRBUF_INIT
;
2120 strbuf_addstr(&buf
, path
);
2121 r
= for_each_loose_file_in_objdir_buf(&buf
, obj_cb
, cruft_cb
,
2123 strbuf_release(&buf
);
2128 int for_each_loose_object(each_loose_object_fn cb
, void *data
,
2129 enum for_each_object_flags flags
)
2131 struct object_directory
*odb
;
2133 prepare_alt_odb(the_repository
);
2134 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
) {
2135 int r
= for_each_loose_file_in_objdir(odb
->path
, cb
, NULL
,
2140 if (flags
& FOR_EACH_OBJECT_LOCAL_ONLY
)
2147 static int append_loose_object(const struct object_id
*oid
, const char *path
,
2150 oid_array_append(data
, oid
);
2154 struct oid_array
*odb_loose_cache(struct object_directory
*odb
,
2155 const struct object_id
*oid
)
2157 int subdir_nr
= oid
->hash
[0];
2158 struct strbuf buf
= STRBUF_INIT
;
2160 if (subdir_nr
< 0 ||
2161 subdir_nr
>= ARRAY_SIZE(odb
->loose_objects_subdir_seen
))
2162 BUG("subdir_nr out of range");
2164 if (odb
->loose_objects_subdir_seen
[subdir_nr
])
2165 return &odb
->loose_objects_cache
[subdir_nr
];
2167 strbuf_addstr(&buf
, odb
->path
);
2168 for_each_file_in_obj_subdir(subdir_nr
, &buf
,
2169 append_loose_object
,
2171 &odb
->loose_objects_cache
[subdir_nr
]);
2172 odb
->loose_objects_subdir_seen
[subdir_nr
] = 1;
2173 strbuf_release(&buf
);
2174 return &odb
->loose_objects_cache
[subdir_nr
];
2177 void odb_clear_loose_cache(struct object_directory
*odb
)
2181 for (i
= 0; i
< ARRAY_SIZE(odb
->loose_objects_cache
); i
++)
2182 oid_array_clear(&odb
->loose_objects_cache
[i
]);
2183 memset(&odb
->loose_objects_subdir_seen
, 0,
2184 sizeof(odb
->loose_objects_subdir_seen
));
2187 static int check_stream_sha1(git_zstream
*stream
,
2191 const unsigned char *expected_sha1
)
2194 unsigned char real_sha1
[GIT_MAX_RAWSZ
];
2195 unsigned char buf
[4096];
2196 unsigned long total_read
;
2199 the_hash_algo
->init_fn(&c
);
2200 the_hash_algo
->update_fn(&c
, hdr
, stream
->total_out
);
2203 * We already read some bytes into hdr, but the ones up to the NUL
2204 * do not count against the object's content size.
2206 total_read
= stream
->total_out
- strlen(hdr
) - 1;
2209 * This size comparison must be "<=" to read the final zlib packets;
2210 * see the comment in unpack_sha1_rest for details.
2212 while (total_read
<= size
&&
2214 (status
== Z_BUF_ERROR
&& !stream
->avail_out
))) {
2215 stream
->next_out
= buf
;
2216 stream
->avail_out
= sizeof(buf
);
2217 if (size
- total_read
< stream
->avail_out
)
2218 stream
->avail_out
= size
- total_read
;
2219 status
= git_inflate(stream
, Z_FINISH
);
2220 the_hash_algo
->update_fn(&c
, buf
, stream
->next_out
- buf
);
2221 total_read
+= stream
->next_out
- buf
;
2223 git_inflate_end(stream
);
2225 if (status
!= Z_STREAM_END
) {
2226 error(_("corrupt loose object '%s'"), sha1_to_hex(expected_sha1
));
2229 if (stream
->avail_in
) {
2230 error(_("garbage at end of loose object '%s'"),
2231 sha1_to_hex(expected_sha1
));
2235 the_hash_algo
->final_fn(real_sha1
, &c
);
2236 if (!hasheq(expected_sha1
, real_sha1
)) {
2237 error(_("sha1 mismatch for %s (expected %s)"), path
,
2238 sha1_to_hex(expected_sha1
));
2245 int read_loose_object(const char *path
,
2246 const struct object_id
*expected_oid
,
2247 enum object_type
*type
,
2248 unsigned long *size
,
2253 unsigned long mapsize
;
2255 char hdr
[MAX_HEADER_LEN
];
2259 map
= map_sha1_file_1(the_repository
, path
, NULL
, &mapsize
);
2261 error_errno(_("unable to mmap %s"), path
);
2265 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0) {
2266 error(_("unable to unpack header of %s"), path
);
2270 *type
= parse_sha1_header(hdr
, size
);
2272 error(_("unable to parse header of %s"), path
);
2273 git_inflate_end(&stream
);
2277 if (*type
== OBJ_BLOB
&& *size
> big_file_threshold
) {
2278 if (check_stream_sha1(&stream
, hdr
, *size
, path
, expected_oid
->hash
) < 0)
2281 *contents
= unpack_sha1_rest(&stream
, hdr
, *size
, expected_oid
->hash
);
2283 error(_("unable to unpack contents of %s"), path
);
2284 git_inflate_end(&stream
);
2287 if (check_object_signature(expected_oid
, *contents
,
2288 *size
, type_name(*type
))) {
2289 error(_("sha1 mismatch for %s (expected %s)"), path
,
2290 oid_to_hex(expected_oid
));
2296 ret
= 0; /* everything checks out */
2300 munmap(map
, mapsize
);