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"
43 #define EMPTY_TREE_SHA256_BIN_LITERAL \
44 "\x6e\xf1\x9b\x41\x22\x5c\x53\x69\xf1\xc1" \
45 "\x04\xd4\x5d\x8d\x85\xef\xa9\xb0\x57\xb5" \
46 "\x3b\x14\xb4\xb9\xb9\x39\xdd\x74\xde\xcc" \
49 #define EMPTY_BLOB_SHA1_BIN_LITERAL \
50 "\xe6\x9d\xe2\x9b\xb2\xd1\xd6\x43\x4b\x8b" \
51 "\x29\xae\x77\x5a\xd8\xc2\xe4\x8c\x53\x91"
52 #define EMPTY_BLOB_SHA256_BIN_LITERAL \
53 "\x47\x3a\x0f\x4c\x3b\xe8\xa9\x36\x81\xa2" \
54 "\x67\xe3\xb1\xe9\xa7\xdc\xda\x11\x85\x43" \
55 "\x6f\xe1\x41\xf7\x74\x91\x20\xa3\x03\x72" \
58 const unsigned char null_sha1
[GIT_MAX_RAWSZ
];
59 const struct object_id null_oid
;
60 static const struct object_id empty_tree_oid
= {
61 EMPTY_TREE_SHA1_BIN_LITERAL
63 static const struct object_id empty_blob_oid
= {
64 EMPTY_BLOB_SHA1_BIN_LITERAL
66 static const struct object_id empty_tree_oid_sha256
= {
67 EMPTY_TREE_SHA256_BIN_LITERAL
69 static const struct object_id empty_blob_oid_sha256
= {
70 EMPTY_BLOB_SHA256_BIN_LITERAL
73 static void git_hash_sha1_init(git_hash_ctx
*ctx
)
75 git_SHA1_Init(&ctx
->sha1
);
78 static void git_hash_sha1_update(git_hash_ctx
*ctx
, const void *data
, size_t len
)
80 git_SHA1_Update(&ctx
->sha1
, data
, len
);
83 static void git_hash_sha1_final(unsigned char *hash
, git_hash_ctx
*ctx
)
85 git_SHA1_Final(hash
, &ctx
->sha1
);
89 static void git_hash_sha256_init(git_hash_ctx
*ctx
)
91 git_SHA256_Init(&ctx
->sha256
);
94 static void git_hash_sha256_update(git_hash_ctx
*ctx
, const void *data
, size_t len
)
96 git_SHA256_Update(&ctx
->sha256
, data
, len
);
99 static void git_hash_sha256_final(unsigned char *hash
, git_hash_ctx
*ctx
)
101 git_SHA256_Final(hash
, &ctx
->sha256
);
104 static void git_hash_unknown_init(git_hash_ctx
*ctx
)
106 BUG("trying to init unknown hash");
109 static void git_hash_unknown_update(git_hash_ctx
*ctx
, const void *data
, size_t len
)
111 BUG("trying to update unknown hash");
114 static void git_hash_unknown_final(unsigned char *hash
, git_hash_ctx
*ctx
)
116 BUG("trying to finalize unknown hash");
119 const struct git_hash_algo hash_algos
[GIT_HASH_NALGOS
] = {
126 git_hash_unknown_init
,
127 git_hash_unknown_update
,
128 git_hash_unknown_final
,
134 /* "sha1", big-endian */
140 git_hash_sha1_update
,
147 /* "s256", big-endian */
152 git_hash_sha256_init
,
153 git_hash_sha256_update
,
154 git_hash_sha256_final
,
155 &empty_tree_oid_sha256
,
156 &empty_blob_oid_sha256
,
160 const char *empty_tree_oid_hex(void)
162 static char buf
[GIT_MAX_HEXSZ
+ 1];
163 return oid_to_hex_r(buf
, the_hash_algo
->empty_tree
);
166 const char *empty_blob_oid_hex(void)
168 static char buf
[GIT_MAX_HEXSZ
+ 1];
169 return oid_to_hex_r(buf
, the_hash_algo
->empty_blob
);
172 int hash_algo_by_name(const char *name
)
176 return GIT_HASH_UNKNOWN
;
177 for (i
= 1; i
< GIT_HASH_NALGOS
; i
++)
178 if (!strcmp(name
, hash_algos
[i
].name
))
180 return GIT_HASH_UNKNOWN
;
183 int hash_algo_by_id(uint32_t format_id
)
186 for (i
= 1; i
< GIT_HASH_NALGOS
; i
++)
187 if (format_id
== hash_algos
[i
].format_id
)
189 return GIT_HASH_UNKNOWN
;
194 * This is meant to hold a *small* number of objects that you would
195 * want read_object_file() to be able to return, but yet you do not want
196 * to write them into the object store (e.g. a browse-only
199 static struct cached_object
{
200 struct object_id oid
;
201 enum object_type type
;
205 static int cached_object_nr
, cached_object_alloc
;
207 static struct cached_object empty_tree
= {
208 { EMPTY_TREE_SHA1_BIN_LITERAL
},
214 static struct cached_object
*find_cached_object(const struct object_id
*oid
)
217 struct cached_object
*co
= cached_objects
;
219 for (i
= 0; i
< cached_object_nr
; i
++, co
++) {
220 if (oideq(&co
->oid
, oid
))
223 if (oideq(oid
, the_hash_algo
->empty_tree
))
229 static int get_conv_flags(unsigned flags
)
231 if (flags
& HASH_RENORMALIZE
)
232 return CONV_EOL_RENORMALIZE
;
233 else if (flags
& HASH_WRITE_OBJECT
)
234 return global_conv_flags_eol
| CONV_WRITE_OBJECT
;
240 int mkdir_in_gitdir(const char *path
)
242 if (mkdir(path
, 0777)) {
243 int saved_errno
= errno
;
245 struct strbuf sb
= STRBUF_INIT
;
250 * Are we looking at a path in a symlinked worktree
251 * whose original repository does not yet have it?
252 * e.g. .git/rr-cache pointing at its original
253 * repository in which the user hasn't performed any
254 * conflict resolution yet?
256 if (lstat(path
, &st
) || !S_ISLNK(st
.st_mode
) ||
257 strbuf_readlink(&sb
, path
, st
.st_size
) ||
258 !is_absolute_path(sb
.buf
) ||
259 mkdir(sb
.buf
, 0777)) {
266 return adjust_shared_perm(path
);
269 enum scld_error
safe_create_leading_directories(char *path
)
271 char *next_component
= path
+ offset_1st_component(path
);
272 enum scld_error ret
= SCLD_OK
;
274 while (ret
== SCLD_OK
&& next_component
) {
276 char *slash
= next_component
, slash_character
;
278 while (*slash
&& !is_dir_sep(*slash
))
284 next_component
= slash
+ 1;
285 while (is_dir_sep(*next_component
))
287 if (!*next_component
)
290 slash_character
= *slash
;
292 if (!stat(path
, &st
)) {
294 if (!S_ISDIR(st
.st_mode
)) {
298 } else if (mkdir(path
, 0777)) {
299 if (errno
== EEXIST
&&
300 !stat(path
, &st
) && S_ISDIR(st
.st_mode
))
301 ; /* somebody created it since we checked */
302 else if (errno
== ENOENT
)
304 * Either mkdir() failed because
305 * somebody just pruned the containing
306 * directory, or stat() failed because
307 * the file that was in our way was
308 * just removed. Either way, inform
309 * the caller that it might be worth
315 } else if (adjust_shared_perm(path
)) {
318 *slash
= slash_character
;
323 enum scld_error
safe_create_leading_directories_const(const char *path
)
326 /* path points to cache entries, so xstrdup before messing with it */
327 char *buf
= xstrdup(path
);
328 enum scld_error result
= safe_create_leading_directories(buf
);
336 int raceproof_create_file(const char *path
, create_file_fn fn
, void *cb
)
339 * The number of times we will try to remove empty directories
340 * in the way of path. This is only 1 because if another
341 * process is racily creating directories that conflict with
342 * us, we don't want to fight against them.
344 int remove_directories_remaining
= 1;
347 * The number of times that we will try to create the
348 * directories containing path. We are willing to attempt this
349 * more than once, because another process could be trying to
350 * clean up empty directories at the same time as we are
351 * trying to create them.
353 int create_directories_remaining
= 3;
355 /* A scratch copy of path, filled lazily if we need it: */
356 struct strbuf path_copy
= STRBUF_INIT
;
369 if (errno
== EISDIR
&& remove_directories_remaining
-- > 0) {
371 * A directory is in the way. Maybe it is empty; try
375 strbuf_addstr(&path_copy
, path
);
377 if (!remove_dir_recursively(&path_copy
, REMOVE_DIR_EMPTY_ONLY
))
379 } else if (errno
== ENOENT
&& create_directories_remaining
-- > 0) {
381 * Maybe the containing directory didn't exist, or
382 * maybe it was just deleted by a process that is
383 * racing with us to clean up empty directories. Try
386 enum scld_error scld_result
;
389 strbuf_addstr(&path_copy
, path
);
392 scld_result
= safe_create_leading_directories(path_copy
.buf
);
393 if (scld_result
== SCLD_OK
)
395 } while (scld_result
== SCLD_VANISHED
&& create_directories_remaining
-- > 0);
399 strbuf_release(&path_copy
);
404 static void fill_loose_path(struct strbuf
*buf
, const struct object_id
*oid
)
407 for (i
= 0; i
< the_hash_algo
->rawsz
; i
++) {
408 static char hex
[] = "0123456789abcdef";
409 unsigned int val
= oid
->hash
[i
];
410 strbuf_addch(buf
, hex
[val
>> 4]);
411 strbuf_addch(buf
, hex
[val
& 0xf]);
413 strbuf_addch(buf
, '/');
417 static const char *odb_loose_path(struct object_directory
*odb
,
419 const struct object_id
*oid
)
422 strbuf_addstr(buf
, odb
->path
);
423 strbuf_addch(buf
, '/');
424 fill_loose_path(buf
, oid
);
428 const char *loose_object_path(struct repository
*r
, struct strbuf
*buf
,
429 const struct object_id
*oid
)
431 return odb_loose_path(r
->objects
->odb
, buf
, oid
);
435 * Return non-zero iff the path is usable as an alternate object database.
437 static int alt_odb_usable(struct raw_object_store
*o
,
439 const char *normalized_objdir
)
441 struct object_directory
*odb
;
443 /* Detect cases where alternate disappeared */
444 if (!is_directory(path
->buf
)) {
445 error(_("object directory %s does not exist; "
446 "check .git/objects/info/alternates"),
452 * Prevent the common mistake of listing the same
453 * thing twice, or object directory itself.
455 for (odb
= o
->odb
; odb
; odb
= odb
->next
) {
456 if (!fspathcmp(path
->buf
, odb
->path
))
459 if (!fspathcmp(path
->buf
, normalized_objdir
))
466 * Prepare alternate object database registry.
468 * The variable alt_odb_list points at the list of struct
469 * object_directory. The elements on this list come from
470 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
471 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
472 * whose contents is similar to that environment variable but can be
473 * LF separated. Its base points at a statically allocated buffer that
474 * contains "/the/directory/corresponding/to/.git/objects/...", while
475 * its name points just after the slash at the end of ".git/objects/"
476 * in the example above, and has enough space to hold 40-byte hex
477 * SHA1, an extra slash for the first level indirection, and the
480 static void read_info_alternates(struct repository
*r
,
481 const char *relative_base
,
483 static int link_alt_odb_entry(struct repository
*r
, const char *entry
,
484 const char *relative_base
, int depth
, const char *normalized_objdir
)
486 struct object_directory
*ent
;
487 struct strbuf pathbuf
= STRBUF_INIT
;
489 if (!is_absolute_path(entry
) && relative_base
) {
490 strbuf_realpath(&pathbuf
, relative_base
, 1);
491 strbuf_addch(&pathbuf
, '/');
493 strbuf_addstr(&pathbuf
, entry
);
495 if (strbuf_normalize_path(&pathbuf
) < 0 && relative_base
) {
496 error(_("unable to normalize alternate object path: %s"),
498 strbuf_release(&pathbuf
);
503 * The trailing slash after the directory name is given by
504 * this function at the end. Remove duplicates.
506 while (pathbuf
.len
&& pathbuf
.buf
[pathbuf
.len
- 1] == '/')
507 strbuf_setlen(&pathbuf
, pathbuf
.len
- 1);
509 if (!alt_odb_usable(r
->objects
, &pathbuf
, normalized_objdir
)) {
510 strbuf_release(&pathbuf
);
514 ent
= xcalloc(1, sizeof(*ent
));
515 ent
->path
= xstrdup(pathbuf
.buf
);
517 /* add the alternate entry */
518 *r
->objects
->odb_tail
= ent
;
519 r
->objects
->odb_tail
= &(ent
->next
);
522 /* recursively add alternates */
523 read_info_alternates(r
, pathbuf
.buf
, depth
+ 1);
525 strbuf_release(&pathbuf
);
529 static const char *parse_alt_odb_entry(const char *string
,
537 if (*string
== '#') {
538 /* comment; consume up to next separator */
539 end
= strchrnul(string
, sep
);
540 } else if (*string
== '"' && !unquote_c_style(out
, string
, &end
)) {
542 * quoted path; unquote_c_style has copied the
543 * data for us and set "end". Broken quoting (e.g.,
544 * an entry that doesn't end with a quote) falls
545 * back to the unquoted case below.
548 /* normal, unquoted path */
549 end
= strchrnul(string
, sep
);
550 strbuf_add(out
, string
, end
- string
);
558 static void link_alt_odb_entries(struct repository
*r
, const char *alt
,
559 int sep
, const char *relative_base
, int depth
)
561 struct strbuf objdirbuf
= STRBUF_INIT
;
562 struct strbuf entry
= STRBUF_INIT
;
568 error(_("%s: ignoring alternate object stores, nesting too deep"),
573 strbuf_add_absolute_path(&objdirbuf
, r
->objects
->odb
->path
);
574 if (strbuf_normalize_path(&objdirbuf
) < 0)
575 die(_("unable to normalize object directory: %s"),
579 alt
= parse_alt_odb_entry(alt
, sep
, &entry
);
582 link_alt_odb_entry(r
, entry
.buf
,
583 relative_base
, depth
, objdirbuf
.buf
);
585 strbuf_release(&entry
);
586 strbuf_release(&objdirbuf
);
589 static void read_info_alternates(struct repository
*r
,
590 const char *relative_base
,
594 struct strbuf buf
= STRBUF_INIT
;
596 path
= xstrfmt("%s/info/alternates", relative_base
);
597 if (strbuf_read_file(&buf
, path
, 1024) < 0) {
598 warn_on_fopen_errors(path
);
603 link_alt_odb_entries(r
, buf
.buf
, '\n', relative_base
, depth
);
604 strbuf_release(&buf
);
608 void add_to_alternates_file(const char *reference
)
610 struct lock_file lock
= LOCK_INIT
;
611 char *alts
= git_pathdup("objects/info/alternates");
615 hold_lock_file_for_update(&lock
, alts
, LOCK_DIE_ON_ERROR
);
616 out
= fdopen_lock_file(&lock
, "w");
618 die_errno(_("unable to fdopen alternates lockfile"));
620 in
= fopen(alts
, "r");
622 struct strbuf line
= STRBUF_INIT
;
624 while (strbuf_getline(&line
, in
) != EOF
) {
625 if (!strcmp(reference
, line
.buf
)) {
629 fprintf_or_die(out
, "%s\n", line
.buf
);
632 strbuf_release(&line
);
635 else if (errno
!= ENOENT
)
636 die_errno(_("unable to read alternates file"));
639 rollback_lock_file(&lock
);
641 fprintf_or_die(out
, "%s\n", reference
);
642 if (commit_lock_file(&lock
))
643 die_errno(_("unable to move new alternates file into place"));
644 if (the_repository
->objects
->loaded_alternates
)
645 link_alt_odb_entries(the_repository
, reference
,
651 void add_to_alternates_memory(const char *reference
)
654 * Make sure alternates are initialized, or else our entry may be
655 * overwritten when they are.
657 prepare_alt_odb(the_repository
);
659 link_alt_odb_entries(the_repository
, reference
,
664 * Compute the exact path an alternate is at and returns it. In case of
665 * error NULL is returned and the human readable error is added to `err`
666 * `path` may be relative and should point to $GIT_DIR.
667 * `err` must not be null.
669 char *compute_alternate_path(const char *path
, struct strbuf
*err
)
671 char *ref_git
= NULL
;
672 const char *repo
, *ref_git_s
;
675 ref_git_s
= real_path_if_valid(path
);
678 strbuf_addf(err
, _("path '%s' does not exist"), path
);
682 * Beware: read_gitfile(), real_path() and mkpath()
683 * return static buffer
685 ref_git
= xstrdup(ref_git_s
);
687 repo
= read_gitfile(ref_git
);
689 repo
= read_gitfile(mkpath("%s/.git", ref_git
));
692 ref_git
= xstrdup(repo
);
695 if (!repo
&& is_directory(mkpath("%s/.git/objects", ref_git
))) {
696 char *ref_git_git
= mkpathdup("%s/.git", ref_git
);
698 ref_git
= ref_git_git
;
699 } else if (!is_directory(mkpath("%s/objects", ref_git
))) {
700 struct strbuf sb
= STRBUF_INIT
;
702 if (get_common_dir(&sb
, ref_git
)) {
704 _("reference repository '%s' as a linked "
705 "checkout is not supported yet."),
710 strbuf_addf(err
, _("reference repository '%s' is not a "
711 "local repository."), path
);
715 if (!access(mkpath("%s/shallow", ref_git
), F_OK
)) {
716 strbuf_addf(err
, _("reference repository '%s' is shallow"),
722 if (!access(mkpath("%s/info/grafts", ref_git
), F_OK
)) {
724 _("reference repository '%s' is grafted"),
732 FREE_AND_NULL(ref_git
);
738 int foreach_alt_odb(alt_odb_fn fn
, void *cb
)
740 struct object_directory
*ent
;
743 prepare_alt_odb(the_repository
);
744 for (ent
= the_repository
->objects
->odb
->next
; ent
; ent
= ent
->next
) {
752 void prepare_alt_odb(struct repository
*r
)
754 if (r
->objects
->loaded_alternates
)
757 link_alt_odb_entries(r
, r
->objects
->alternate_db
, PATH_SEP
, NULL
, 0);
759 read_info_alternates(r
, r
->objects
->odb
->path
, 0);
760 r
->objects
->loaded_alternates
= 1;
763 /* Returns 1 if we have successfully freshened the file, 0 otherwise. */
764 static int freshen_file(const char *fn
)
767 t
.actime
= t
.modtime
= time(NULL
);
768 return !utime(fn
, &t
);
772 * All of the check_and_freshen functions return 1 if the file exists and was
773 * freshened (if freshening was requested), 0 otherwise. If they return
774 * 0, you should not assume that it is safe to skip a write of the object (it
775 * either does not exist on disk, or has a stale mtime and may be subject to
778 int check_and_freshen_file(const char *fn
, int freshen
)
780 if (access(fn
, F_OK
))
782 if (freshen
&& !freshen_file(fn
))
787 static int check_and_freshen_odb(struct object_directory
*odb
,
788 const struct object_id
*oid
,
791 static struct strbuf path
= STRBUF_INIT
;
792 odb_loose_path(odb
, &path
, oid
);
793 return check_and_freshen_file(path
.buf
, freshen
);
796 static int check_and_freshen_local(const struct object_id
*oid
, int freshen
)
798 return check_and_freshen_odb(the_repository
->objects
->odb
, oid
, freshen
);
801 static int check_and_freshen_nonlocal(const struct object_id
*oid
, int freshen
)
803 struct object_directory
*odb
;
805 prepare_alt_odb(the_repository
);
806 for (odb
= the_repository
->objects
->odb
->next
; odb
; odb
= odb
->next
) {
807 if (check_and_freshen_odb(odb
, oid
, freshen
))
813 static int check_and_freshen(const struct object_id
*oid
, int freshen
)
815 return check_and_freshen_local(oid
, freshen
) ||
816 check_and_freshen_nonlocal(oid
, freshen
);
819 int has_loose_object_nonlocal(const struct object_id
*oid
)
821 return check_and_freshen_nonlocal(oid
, 0);
824 static int has_loose_object(const struct object_id
*oid
)
826 return check_and_freshen(oid
, 0);
829 static void mmap_limit_check(size_t length
)
831 static size_t limit
= 0;
833 limit
= git_env_ulong("GIT_MMAP_LIMIT", 0);
838 die(_("attempting to mmap %"PRIuMAX
" over limit %"PRIuMAX
),
839 (uintmax_t)length
, (uintmax_t)limit
);
842 void *xmmap_gently(void *start
, size_t length
,
843 int prot
, int flags
, int fd
, off_t offset
)
847 mmap_limit_check(length
);
848 ret
= mmap(start
, length
, prot
, flags
, fd
, offset
);
849 if (ret
== MAP_FAILED
) {
852 release_pack_memory(length
);
853 ret
= mmap(start
, length
, prot
, flags
, fd
, offset
);
858 void *xmmap(void *start
, size_t length
,
859 int prot
, int flags
, int fd
, off_t offset
)
861 void *ret
= xmmap_gently(start
, length
, prot
, flags
, fd
, offset
);
862 if (ret
== MAP_FAILED
)
863 die_errno(_("mmap failed"));
868 * With an in-core object data in "map", rehash it to make sure the
869 * object name actually matches "oid" to detect object corruption.
870 * With "map" == NULL, try reading the object named with "oid" using
871 * the streaming interface and rehash it to do the same.
873 int check_object_signature(const struct object_id
*oid
, void *map
,
874 unsigned long size
, const char *type
)
876 struct object_id real_oid
;
877 enum object_type obj_type
;
878 struct git_istream
*st
;
880 char hdr
[MAX_HEADER_LEN
];
884 hash_object_file(map
, size
, type
, &real_oid
);
885 return !oideq(oid
, &real_oid
) ? -1 : 0;
888 st
= open_istream(oid
, &obj_type
, &size
, NULL
);
892 /* Generate the header */
893 hdrlen
= xsnprintf(hdr
, sizeof(hdr
), "%s %"PRIuMAX
, type_name(obj_type
), (uintmax_t)size
) + 1;
896 the_hash_algo
->init_fn(&c
);
897 the_hash_algo
->update_fn(&c
, hdr
, hdrlen
);
900 ssize_t readlen
= read_istream(st
, buf
, sizeof(buf
));
908 the_hash_algo
->update_fn(&c
, buf
, readlen
);
910 the_hash_algo
->final_fn(real_oid
.hash
, &c
);
912 return !oideq(oid
, &real_oid
) ? -1 : 0;
915 int git_open_cloexec(const char *name
, int flags
)
918 static int o_cloexec
= O_CLOEXEC
;
920 fd
= open(name
, flags
| o_cloexec
);
921 if ((o_cloexec
& O_CLOEXEC
) && fd
< 0 && errno
== EINVAL
) {
922 /* Try again w/o O_CLOEXEC: the kernel might not support it */
923 o_cloexec
&= ~O_CLOEXEC
;
924 fd
= open(name
, flags
| o_cloexec
);
927 #if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
929 static int fd_cloexec
= FD_CLOEXEC
;
931 if (!o_cloexec
&& 0 <= fd
&& fd_cloexec
) {
932 /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */
933 int flags
= fcntl(fd
, F_GETFD
);
934 if (fcntl(fd
, F_SETFD
, flags
| fd_cloexec
))
943 * Find "oid" as a loose object in the local repository or in an alternate.
944 * Returns 0 on success, negative on failure.
946 * The "path" out-parameter will give the path of the object we found (if any).
947 * Note that it may point to static storage and is only valid until another
948 * call to stat_loose_object().
950 static int stat_loose_object(struct repository
*r
, const struct object_id
*oid
,
951 struct stat
*st
, const char **path
)
953 struct object_directory
*odb
;
954 static struct strbuf buf
= STRBUF_INIT
;
957 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
958 *path
= odb_loose_path(odb
, &buf
, oid
);
959 if (!lstat(*path
, st
))
967 * Like stat_loose_object(), but actually open the object and return the
968 * descriptor. See the caveats on the "path" parameter above.
970 static int open_loose_object(struct repository
*r
,
971 const struct object_id
*oid
, const char **path
)
974 struct object_directory
*odb
;
975 int most_interesting_errno
= ENOENT
;
976 static struct strbuf buf
= STRBUF_INIT
;
979 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
980 *path
= odb_loose_path(odb
, &buf
, oid
);
981 fd
= git_open(*path
);
985 if (most_interesting_errno
== ENOENT
)
986 most_interesting_errno
= errno
;
988 errno
= most_interesting_errno
;
992 static int quick_has_loose(struct repository
*r
,
993 const struct object_id
*oid
)
995 struct object_directory
*odb
;
998 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
999 if (oid_array_lookup(odb_loose_cache(odb
, oid
), oid
) >= 0)
1006 * Map the loose object at "path" if it is not NULL, or the path found by
1007 * searching for a loose object named "oid".
1009 static void *map_loose_object_1(struct repository
*r
, const char *path
,
1010 const struct object_id
*oid
, unsigned long *size
)
1016 fd
= git_open(path
);
1018 fd
= open_loose_object(r
, oid
, &path
);
1023 if (!fstat(fd
, &st
)) {
1024 *size
= xsize_t(st
.st_size
);
1026 /* mmap() is forbidden on empty files */
1027 error(_("object file %s is empty"), path
);
1031 map
= xmmap(NULL
, *size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1038 void *map_loose_object(struct repository
*r
,
1039 const struct object_id
*oid
,
1040 unsigned long *size
)
1042 return map_loose_object_1(r
, NULL
, oid
, size
);
1045 static int unpack_loose_short_header(git_zstream
*stream
,
1046 unsigned char *map
, unsigned long mapsize
,
1047 void *buffer
, unsigned long bufsiz
)
1049 /* Get the data stream */
1050 memset(stream
, 0, sizeof(*stream
));
1051 stream
->next_in
= map
;
1052 stream
->avail_in
= mapsize
;
1053 stream
->next_out
= buffer
;
1054 stream
->avail_out
= bufsiz
;
1056 git_inflate_init(stream
);
1057 return git_inflate(stream
, 0);
1060 int unpack_loose_header(git_zstream
*stream
,
1061 unsigned char *map
, unsigned long mapsize
,
1062 void *buffer
, unsigned long bufsiz
)
1064 int status
= unpack_loose_short_header(stream
, map
, mapsize
,
1070 /* Make sure we have the terminating NUL */
1071 if (!memchr(buffer
, '\0', stream
->next_out
- (unsigned char *)buffer
))
1076 static int unpack_loose_header_to_strbuf(git_zstream
*stream
, unsigned char *map
,
1077 unsigned long mapsize
, void *buffer
,
1078 unsigned long bufsiz
, struct strbuf
*header
)
1082 status
= unpack_loose_short_header(stream
, map
, mapsize
, buffer
, bufsiz
);
1087 * Check if entire header is unpacked in the first iteration.
1089 if (memchr(buffer
, '\0', stream
->next_out
- (unsigned char *)buffer
))
1093 * buffer[0..bufsiz] was not large enough. Copy the partial
1094 * result out to header, and then append the result of further
1095 * reading the stream.
1097 strbuf_add(header
, buffer
, stream
->next_out
- (unsigned char *)buffer
);
1098 stream
->next_out
= buffer
;
1099 stream
->avail_out
= bufsiz
;
1102 status
= git_inflate(stream
, 0);
1103 strbuf_add(header
, buffer
, stream
->next_out
- (unsigned char *)buffer
);
1104 if (memchr(buffer
, '\0', stream
->next_out
- (unsigned char *)buffer
))
1106 stream
->next_out
= buffer
;
1107 stream
->avail_out
= bufsiz
;
1108 } while (status
!= Z_STREAM_END
);
1112 static void *unpack_loose_rest(git_zstream
*stream
,
1113 void *buffer
, unsigned long size
,
1114 const struct object_id
*oid
)
1116 int bytes
= strlen(buffer
) + 1;
1117 unsigned char *buf
= xmallocz(size
);
1121 n
= stream
->total_out
- bytes
;
1124 memcpy(buf
, (char *) buffer
+ bytes
, n
);
1126 if (bytes
<= size
) {
1128 * The above condition must be (bytes <= size), not
1129 * (bytes < size). In other words, even though we
1130 * expect no more output and set avail_out to zero,
1131 * the input zlib stream may have bytes that express
1132 * "this concludes the stream", and we *do* want to
1135 * Otherwise we would not be able to test that we
1136 * consumed all the input to reach the expected size;
1137 * we also want to check that zlib tells us that all
1138 * went well with status == Z_STREAM_END at the end.
1140 stream
->next_out
= buf
+ bytes
;
1141 stream
->avail_out
= size
- bytes
;
1142 while (status
== Z_OK
)
1143 status
= git_inflate(stream
, Z_FINISH
);
1145 if (status
== Z_STREAM_END
&& !stream
->avail_in
) {
1146 git_inflate_end(stream
);
1151 error(_("corrupt loose object '%s'"), oid_to_hex(oid
));
1152 else if (stream
->avail_in
)
1153 error(_("garbage at end of loose object '%s'"),
1160 * We used to just use "sscanf()", but that's actually way
1161 * too permissive for what we want to check. So do an anal
1162 * object header parse by hand.
1164 static int parse_loose_header_extended(const char *hdr
, struct object_info
*oi
,
1167 const char *type_buf
= hdr
;
1169 int type
, type_len
= 0;
1172 * The type can be of any size but is followed by
1184 type
= type_from_string_gently(type_buf
, type_len
, 1);
1186 strbuf_add(oi
->type_name
, type_buf
, type_len
);
1188 * Set type to 0 if its an unknown object and
1189 * we're obtaining the type using '--allow-unknown-type'
1192 if ((flags
& OBJECT_INFO_ALLOW_UNKNOWN_TYPE
) && (type
< 0))
1195 die(_("invalid object type"));
1200 * The length must follow immediately, and be in canonical
1201 * decimal format (ie "010" is not valid).
1203 size
= *hdr
++ - '0';
1208 unsigned long c
= *hdr
- '0';
1212 size
= size
* 10 + c
;
1220 * The length must be followed by a zero byte
1222 return *hdr
? -1 : type
;
1225 int parse_loose_header(const char *hdr
, unsigned long *sizep
)
1227 struct object_info oi
= OBJECT_INFO_INIT
;
1230 return parse_loose_header_extended(hdr
, &oi
, 0);
1233 static int loose_object_info(struct repository
*r
,
1234 const struct object_id
*oid
,
1235 struct object_info
*oi
, int flags
)
1238 unsigned long mapsize
;
1241 char hdr
[MAX_HEADER_LEN
];
1242 struct strbuf hdrbuf
= STRBUF_INIT
;
1243 unsigned long size_scratch
;
1245 if (oi
->delta_base_sha1
)
1246 hashclr(oi
->delta_base_sha1
);
1249 * If we don't care about type or size, then we don't
1250 * need to look inside the object at all. Note that we
1251 * do not optimize out the stat call, even if the
1252 * caller doesn't care about the disk-size, since our
1253 * return value implicitly indicates whether the
1254 * object even exists.
1256 if (!oi
->typep
&& !oi
->type_name
&& !oi
->sizep
&& !oi
->contentp
) {
1259 if (!oi
->disk_sizep
&& (flags
& OBJECT_INFO_QUICK
))
1260 return quick_has_loose(r
, oid
) ? 0 : -1;
1261 if (stat_loose_object(r
, oid
, &st
, &path
) < 0)
1264 *oi
->disk_sizep
= st
.st_size
;
1268 map
= map_loose_object(r
, oid
, &mapsize
);
1273 oi
->sizep
= &size_scratch
;
1276 *oi
->disk_sizep
= mapsize
;
1277 if ((flags
& OBJECT_INFO_ALLOW_UNKNOWN_TYPE
)) {
1278 if (unpack_loose_header_to_strbuf(&stream
, map
, mapsize
, hdr
, sizeof(hdr
), &hdrbuf
) < 0)
1279 status
= error(_("unable to unpack %s header with --allow-unknown-type"),
1281 } else if (unpack_loose_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
1282 status
= error(_("unable to unpack %s header"),
1286 else if (hdrbuf
.len
) {
1287 if ((status
= parse_loose_header_extended(hdrbuf
.buf
, oi
, flags
)) < 0)
1288 status
= error(_("unable to parse %s header with --allow-unknown-type"),
1290 } else if ((status
= parse_loose_header_extended(hdr
, oi
, flags
)) < 0)
1291 status
= error(_("unable to parse %s header"), oid_to_hex(oid
));
1293 if (status
>= 0 && oi
->contentp
) {
1294 *oi
->contentp
= unpack_loose_rest(&stream
, hdr
,
1296 if (!*oi
->contentp
) {
1297 git_inflate_end(&stream
);
1301 git_inflate_end(&stream
);
1303 munmap(map
, mapsize
);
1304 if (status
&& oi
->typep
)
1305 *oi
->typep
= status
;
1306 if (oi
->sizep
== &size_scratch
)
1308 strbuf_release(&hdrbuf
);
1309 oi
->whence
= OI_LOOSE
;
1310 return (status
< 0) ? status
: 0;
1313 int fetch_if_missing
= 1;
1315 int oid_object_info_extended(struct repository
*r
, const struct object_id
*oid
,
1316 struct object_info
*oi
, unsigned flags
)
1318 static struct object_info blank_oi
= OBJECT_INFO_INIT
;
1319 struct pack_entry e
;
1321 const struct object_id
*real
= oid
;
1322 int already_retried
= 0;
1324 if (flags
& OBJECT_INFO_LOOKUP_REPLACE
)
1325 real
= lookup_replace_object(r
, oid
);
1327 if (is_null_oid(real
))
1333 if (!(flags
& OBJECT_INFO_SKIP_CACHED
)) {
1334 struct cached_object
*co
= find_cached_object(real
);
1337 *(oi
->typep
) = co
->type
;
1339 *(oi
->sizep
) = co
->size
;
1341 *(oi
->disk_sizep
) = 0;
1342 if (oi
->delta_base_sha1
)
1343 hashclr(oi
->delta_base_sha1
);
1345 strbuf_addstr(oi
->type_name
, type_name(co
->type
));
1347 *oi
->contentp
= xmemdupz(co
->buf
, co
->size
);
1348 oi
->whence
= OI_CACHED
;
1354 if (find_pack_entry(r
, real
, &e
))
1357 if (flags
& OBJECT_INFO_IGNORE_LOOSE
)
1360 /* Most likely it's a loose object. */
1361 if (!loose_object_info(r
, real
, oi
, flags
))
1364 /* Not a loose object; someone else may have just packed it. */
1365 if (!(flags
& OBJECT_INFO_QUICK
)) {
1366 reprepare_packed_git(r
);
1367 if (find_pack_entry(r
, real
, &e
))
1371 /* Check if it is a missing object */
1372 if (fetch_if_missing
&& repository_format_partial_clone
&&
1373 !already_retried
&& r
== the_repository
) {
1375 * TODO Investigate having fetch_object() return
1376 * TODO error/success and stopping the music here.
1377 * TODO Pass a repository struct through fetch_object,
1378 * such that arbitrary repositories work.
1380 fetch_objects(repository_format_partial_clone
, real
, 1);
1381 already_retried
= 1;
1388 if (oi
== &blank_oi
)
1390 * We know that the caller doesn't actually need the
1391 * information below, so return early.
1394 rtype
= packed_object_info(r
, e
.p
, e
.offset
, oi
);
1396 mark_bad_packed_object(e
.p
, real
->hash
);
1397 return oid_object_info_extended(r
, real
, oi
, 0);
1398 } else if (oi
->whence
== OI_PACKED
) {
1399 oi
->u
.packed
.offset
= e
.offset
;
1400 oi
->u
.packed
.pack
= e
.p
;
1401 oi
->u
.packed
.is_delta
= (rtype
== OBJ_REF_DELTA
||
1402 rtype
== OBJ_OFS_DELTA
);
1408 /* returns enum object_type or negative */
1409 int oid_object_info(struct repository
*r
,
1410 const struct object_id
*oid
,
1411 unsigned long *sizep
)
1413 enum object_type type
;
1414 struct object_info oi
= OBJECT_INFO_INIT
;
1418 if (oid_object_info_extended(r
, oid
, &oi
,
1419 OBJECT_INFO_LOOKUP_REPLACE
) < 0)
1424 static void *read_object(struct repository
*r
,
1425 const struct object_id
*oid
, enum object_type
*type
,
1426 unsigned long *size
)
1428 struct object_info oi
= OBJECT_INFO_INIT
;
1432 oi
.contentp
= &content
;
1434 if (oid_object_info_extended(r
, oid
, &oi
, 0) < 0)
1439 int pretend_object_file(void *buf
, unsigned long len
, enum object_type type
,
1440 struct object_id
*oid
)
1442 struct cached_object
*co
;
1444 hash_object_file(buf
, len
, type_name(type
), oid
);
1445 if (has_object_file(oid
) || find_cached_object(oid
))
1447 ALLOC_GROW(cached_objects
, cached_object_nr
+ 1, cached_object_alloc
);
1448 co
= &cached_objects
[cached_object_nr
++];
1451 co
->buf
= xmalloc(len
);
1452 memcpy(co
->buf
, buf
, len
);
1453 oidcpy(&co
->oid
, oid
);
1458 * This function dies on corrupt objects; the callers who want to
1459 * deal with them should arrange to call read_object() and give error
1460 * messages themselves.
1462 void *read_object_file_extended(struct repository
*r
,
1463 const struct object_id
*oid
,
1464 enum object_type
*type
,
1465 unsigned long *size
,
1469 const struct packed_git
*p
;
1472 const struct object_id
*repl
= lookup_replace
?
1473 lookup_replace_object(r
, oid
) : oid
;
1476 data
= read_object(r
, repl
, type
, size
);
1480 if (errno
&& errno
!= ENOENT
)
1481 die_errno(_("failed to read object %s"), oid_to_hex(oid
));
1483 /* die if we replaced an object with one that does not exist */
1485 die(_("replacement %s not found for %s"),
1486 oid_to_hex(repl
), oid_to_hex(oid
));
1488 if (!stat_loose_object(r
, repl
, &st
, &path
))
1489 die(_("loose object %s (stored in %s) is corrupt"),
1490 oid_to_hex(repl
), path
);
1492 if ((p
= has_packed_and_bad(r
, repl
->hash
)) != NULL
)
1493 die(_("packed object %s (stored in %s) is corrupt"),
1494 oid_to_hex(repl
), p
->pack_name
);
1499 void *read_object_with_reference(const struct object_id
*oid
,
1500 const char *required_type_name
,
1501 unsigned long *size
,
1502 struct object_id
*actual_oid_return
)
1504 enum object_type type
, required_type
;
1506 unsigned long isize
;
1507 struct object_id actual_oid
;
1509 required_type
= type_from_string(required_type_name
);
1510 oidcpy(&actual_oid
, oid
);
1512 int ref_length
= -1;
1513 const char *ref_type
= NULL
;
1515 buffer
= read_object_file(&actual_oid
, &type
, &isize
);
1518 if (type
== required_type
) {
1520 if (actual_oid_return
)
1521 oidcpy(actual_oid_return
, &actual_oid
);
1524 /* Handle references */
1525 else if (type
== OBJ_COMMIT
)
1527 else if (type
== OBJ_TAG
)
1528 ref_type
= "object ";
1533 ref_length
= strlen(ref_type
);
1535 if (ref_length
+ the_hash_algo
->hexsz
> isize
||
1536 memcmp(buffer
, ref_type
, ref_length
) ||
1537 get_oid_hex((char *) buffer
+ ref_length
, &actual_oid
)) {
1542 /* Now we have the ID of the referred-to object in
1543 * actual_oid. Check again. */
1547 static void write_object_file_prepare(const void *buf
, unsigned long len
,
1548 const char *type
, struct object_id
*oid
,
1549 char *hdr
, int *hdrlen
)
1553 /* Generate the header */
1554 *hdrlen
= xsnprintf(hdr
, *hdrlen
, "%s %"PRIuMAX
, type
, (uintmax_t)len
)+1;
1557 the_hash_algo
->init_fn(&c
);
1558 the_hash_algo
->update_fn(&c
, hdr
, *hdrlen
);
1559 the_hash_algo
->update_fn(&c
, buf
, len
);
1560 the_hash_algo
->final_fn(oid
->hash
, &c
);
1564 * Move the just written object into its final resting place.
1566 int finalize_object_file(const char *tmpfile
, const char *filename
)
1570 if (object_creation_mode
== OBJECT_CREATION_USES_RENAMES
)
1572 else if (link(tmpfile
, filename
))
1576 * Coda hack - coda doesn't like cross-directory links,
1577 * so we fall back to a rename, which will mean that it
1578 * won't be able to check collisions, but that's not a
1581 * The same holds for FAT formatted media.
1583 * When this succeeds, we just return. We have nothing
1586 if (ret
&& ret
!= EEXIST
) {
1588 if (!rename(tmpfile
, filename
))
1592 unlink_or_warn(tmpfile
);
1594 if (ret
!= EEXIST
) {
1595 return error_errno(_("unable to write file %s"), filename
);
1597 /* FIXME!!! Collision check here ? */
1601 if (adjust_shared_perm(filename
))
1602 return error(_("unable to set permission to '%s'"), filename
);
1606 static int write_buffer(int fd
, const void *buf
, size_t len
)
1608 if (write_in_full(fd
, buf
, len
) < 0)
1609 return error_errno(_("file write error"));
1613 int hash_object_file(const void *buf
, unsigned long len
, const char *type
,
1614 struct object_id
*oid
)
1616 char hdr
[MAX_HEADER_LEN
];
1617 int hdrlen
= sizeof(hdr
);
1618 write_object_file_prepare(buf
, len
, type
, oid
, hdr
, &hdrlen
);
1622 /* Finalize a file on disk, and close it. */
1623 static void close_loose_object(int fd
)
1625 if (fsync_object_files
)
1626 fsync_or_die(fd
, "loose object file");
1628 die_errno(_("error when closing loose object file"));
1631 /* Size of directory component, including the ending '/' */
1632 static inline int directory_size(const char *filename
)
1634 const char *s
= strrchr(filename
, '/');
1637 return s
- filename
+ 1;
1641 * This creates a temporary file in the same directory as the final
1644 * We want to avoid cross-directory filename renames, because those
1645 * can have problems on various filesystems (FAT, NFS, Coda).
1647 static int create_tmpfile(struct strbuf
*tmp
, const char *filename
)
1649 int fd
, dirlen
= directory_size(filename
);
1652 strbuf_add(tmp
, filename
, dirlen
);
1653 strbuf_addstr(tmp
, "tmp_obj_XXXXXX");
1654 fd
= git_mkstemp_mode(tmp
->buf
, 0444);
1655 if (fd
< 0 && dirlen
&& errno
== ENOENT
) {
1657 * Make sure the directory exists; note that the contents
1658 * of the buffer are undefined after mkstemp returns an
1659 * error, so we have to rewrite the whole buffer from
1663 strbuf_add(tmp
, filename
, dirlen
- 1);
1664 if (mkdir(tmp
->buf
, 0777) && errno
!= EEXIST
)
1666 if (adjust_shared_perm(tmp
->buf
))
1670 strbuf_addstr(tmp
, "/tmp_obj_XXXXXX");
1671 fd
= git_mkstemp_mode(tmp
->buf
, 0444);
1676 static int write_loose_object(const struct object_id
*oid
, char *hdr
,
1677 int hdrlen
, const void *buf
, unsigned long len
,
1681 unsigned char compressed
[4096];
1684 struct object_id parano_oid
;
1685 static struct strbuf tmp_file
= STRBUF_INIT
;
1686 static struct strbuf filename
= STRBUF_INIT
;
1688 loose_object_path(the_repository
, &filename
, oid
);
1690 fd
= create_tmpfile(&tmp_file
, filename
.buf
);
1692 if (errno
== EACCES
)
1693 return error(_("insufficient permission for adding an object to repository database %s"), get_object_directory());
1695 return error_errno(_("unable to create temporary file"));
1699 git_deflate_init(&stream
, zlib_compression_level
);
1700 stream
.next_out
= compressed
;
1701 stream
.avail_out
= sizeof(compressed
);
1702 the_hash_algo
->init_fn(&c
);
1704 /* First header.. */
1705 stream
.next_in
= (unsigned char *)hdr
;
1706 stream
.avail_in
= hdrlen
;
1707 while (git_deflate(&stream
, 0) == Z_OK
)
1709 the_hash_algo
->update_fn(&c
, hdr
, hdrlen
);
1711 /* Then the data itself.. */
1712 stream
.next_in
= (void *)buf
;
1713 stream
.avail_in
= len
;
1715 unsigned char *in0
= stream
.next_in
;
1716 ret
= git_deflate(&stream
, Z_FINISH
);
1717 the_hash_algo
->update_fn(&c
, in0
, stream
.next_in
- in0
);
1718 if (write_buffer(fd
, compressed
, stream
.next_out
- compressed
) < 0)
1719 die(_("unable to write loose object file"));
1720 stream
.next_out
= compressed
;
1721 stream
.avail_out
= sizeof(compressed
);
1722 } while (ret
== Z_OK
);
1724 if (ret
!= Z_STREAM_END
)
1725 die(_("unable to deflate new object %s (%d)"), oid_to_hex(oid
),
1727 ret
= git_deflate_end_gently(&stream
);
1729 die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid
),
1731 the_hash_algo
->final_fn(parano_oid
.hash
, &c
);
1732 if (!oideq(oid
, ¶no_oid
))
1733 die(_("confused by unstable object source data for %s"),
1736 close_loose_object(fd
);
1741 utb
.modtime
= mtime
;
1742 if (utime(tmp_file
.buf
, &utb
) < 0)
1743 warning_errno(_("failed utime() on %s"), tmp_file
.buf
);
1746 return finalize_object_file(tmp_file
.buf
, filename
.buf
);
1749 static int freshen_loose_object(const struct object_id
*oid
)
1751 return check_and_freshen(oid
, 1);
1754 static int freshen_packed_object(const struct object_id
*oid
)
1756 struct pack_entry e
;
1757 if (!find_pack_entry(the_repository
, oid
, &e
))
1761 if (!freshen_file(e
.p
->pack_name
))
1767 int write_object_file(const void *buf
, unsigned long len
, const char *type
,
1768 struct object_id
*oid
)
1770 char hdr
[MAX_HEADER_LEN
];
1771 int hdrlen
= sizeof(hdr
);
1773 /* Normally if we have it in the pack then we do not bother writing
1774 * it out into .git/objects/??/?{38} file.
1776 write_object_file_prepare(buf
, len
, type
, oid
, hdr
, &hdrlen
);
1777 if (freshen_packed_object(oid
) || freshen_loose_object(oid
))
1779 return write_loose_object(oid
, hdr
, hdrlen
, buf
, len
, 0);
1782 int hash_object_file_literally(const void *buf
, unsigned long len
,
1783 const char *type
, struct object_id
*oid
,
1787 int hdrlen
, status
= 0;
1789 /* type string, SP, %lu of the length plus NUL must fit this */
1790 hdrlen
= strlen(type
) + MAX_HEADER_LEN
;
1791 header
= xmalloc(hdrlen
);
1792 write_object_file_prepare(buf
, len
, type
, oid
, header
, &hdrlen
);
1794 if (!(flags
& HASH_WRITE_OBJECT
))
1796 if (freshen_packed_object(oid
) || freshen_loose_object(oid
))
1798 status
= write_loose_object(oid
, header
, hdrlen
, buf
, len
, 0);
1805 int force_object_loose(const struct object_id
*oid
, time_t mtime
)
1809 enum object_type type
;
1810 char hdr
[MAX_HEADER_LEN
];
1814 if (has_loose_object(oid
))
1816 buf
= read_object(the_repository
, oid
, &type
, &len
);
1818 return error(_("cannot read object for %s"), oid_to_hex(oid
));
1819 hdrlen
= xsnprintf(hdr
, sizeof(hdr
), "%s %"PRIuMAX
, type_name(type
), (uintmax_t)len
) + 1;
1820 ret
= write_loose_object(oid
, hdr
, hdrlen
, buf
, len
, mtime
);
1826 int repo_has_object_file_with_flags(struct repository
*r
,
1827 const struct object_id
*oid
, int flags
)
1829 if (!startup_info
->have_repository
)
1831 return oid_object_info_extended(r
, oid
, NULL
,
1832 flags
| OBJECT_INFO_SKIP_CACHED
) >= 0;
1835 int repo_has_object_file(struct repository
*r
,
1836 const struct object_id
*oid
)
1838 return repo_has_object_file_with_flags(r
, oid
, 0);
1841 static void check_tree(const void *buf
, size_t size
)
1843 struct tree_desc desc
;
1844 struct name_entry entry
;
1846 init_tree_desc(&desc
, buf
, size
);
1847 while (tree_entry(&desc
, &entry
))
1849 * tree_entry() will die() on malformed entries */
1853 static void check_commit(const void *buf
, size_t size
)
1856 memset(&c
, 0, sizeof(c
));
1857 if (parse_commit_buffer(the_repository
, &c
, buf
, size
, 0))
1858 die(_("corrupt commit"));
1861 static void check_tag(const void *buf
, size_t size
)
1864 memset(&t
, 0, sizeof(t
));
1865 if (parse_tag_buffer(the_repository
, &t
, buf
, size
))
1866 die(_("corrupt tag"));
1869 static int index_mem(struct index_state
*istate
,
1870 struct object_id
*oid
, void *buf
, size_t size
,
1871 enum object_type type
,
1872 const char *path
, unsigned flags
)
1874 int ret
, re_allocated
= 0;
1875 int write_object
= flags
& HASH_WRITE_OBJECT
;
1881 * Convert blobs to git internal format
1883 if ((type
== OBJ_BLOB
) && path
) {
1884 struct strbuf nbuf
= STRBUF_INIT
;
1885 if (convert_to_git(istate
, path
, buf
, size
, &nbuf
,
1886 get_conv_flags(flags
))) {
1887 buf
= strbuf_detach(&nbuf
, &size
);
1891 if (flags
& HASH_FORMAT_CHECK
) {
1892 if (type
== OBJ_TREE
)
1893 check_tree(buf
, size
);
1894 if (type
== OBJ_COMMIT
)
1895 check_commit(buf
, size
);
1896 if (type
== OBJ_TAG
)
1897 check_tag(buf
, size
);
1901 ret
= write_object_file(buf
, size
, type_name(type
), oid
);
1903 ret
= hash_object_file(buf
, size
, type_name(type
), oid
);
1909 static int index_stream_convert_blob(struct index_state
*istate
,
1910 struct object_id
*oid
,
1916 const int write_object
= flags
& HASH_WRITE_OBJECT
;
1917 struct strbuf sbuf
= STRBUF_INIT
;
1920 assert(would_convert_to_git_filter_fd(istate
, path
));
1922 convert_to_git_filter_fd(istate
, path
, fd
, &sbuf
,
1923 get_conv_flags(flags
));
1926 ret
= write_object_file(sbuf
.buf
, sbuf
.len
, type_name(OBJ_BLOB
),
1929 ret
= hash_object_file(sbuf
.buf
, sbuf
.len
, type_name(OBJ_BLOB
),
1931 strbuf_release(&sbuf
);
1935 static int index_pipe(struct index_state
*istate
, struct object_id
*oid
,
1936 int fd
, enum object_type type
,
1937 const char *path
, unsigned flags
)
1939 struct strbuf sbuf
= STRBUF_INIT
;
1942 if (strbuf_read(&sbuf
, fd
, 4096) >= 0)
1943 ret
= index_mem(istate
, oid
, sbuf
.buf
, sbuf
.len
, type
, path
, flags
);
1946 strbuf_release(&sbuf
);
1950 #define SMALL_FILE_SIZE (32*1024)
1952 static int index_core(struct index_state
*istate
,
1953 struct object_id
*oid
, int fd
, size_t size
,
1954 enum object_type type
, const char *path
,
1960 ret
= index_mem(istate
, oid
, "", size
, type
, path
, flags
);
1961 } else if (size
<= SMALL_FILE_SIZE
) {
1962 char *buf
= xmalloc(size
);
1963 ssize_t read_result
= read_in_full(fd
, buf
, size
);
1964 if (read_result
< 0)
1965 ret
= error_errno(_("read error while indexing %s"),
1966 path
? path
: "<unknown>");
1967 else if (read_result
!= size
)
1968 ret
= error(_("short read while indexing %s"),
1969 path
? path
: "<unknown>");
1971 ret
= index_mem(istate
, oid
, buf
, size
, type
, path
, flags
);
1974 void *buf
= xmmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1975 ret
= index_mem(istate
, oid
, buf
, size
, type
, path
, flags
);
1982 * This creates one packfile per large blob unless bulk-checkin
1983 * machinery is "plugged".
1985 * This also bypasses the usual "convert-to-git" dance, and that is on
1986 * purpose. We could write a streaming version of the converting
1987 * functions and insert that before feeding the data to fast-import
1988 * (or equivalent in-core API described above). However, that is
1989 * somewhat complicated, as we do not know the size of the filter
1990 * result, which we need to know beforehand when writing a git object.
1991 * Since the primary motivation for trying to stream from the working
1992 * tree file and to avoid mmaping it in core is to deal with large
1993 * binary blobs, they generally do not want to get any conversion, and
1994 * callers should avoid this code path when filters are requested.
1996 static int index_stream(struct object_id
*oid
, int fd
, size_t size
,
1997 enum object_type type
, const char *path
,
2000 return index_bulk_checkin(oid
, fd
, size
, type
, path
, flags
);
2003 int index_fd(struct index_state
*istate
, struct object_id
*oid
,
2004 int fd
, struct stat
*st
,
2005 enum object_type type
, const char *path
, unsigned flags
)
2010 * Call xsize_t() only when needed to avoid potentially unnecessary
2011 * die() for large files.
2013 if (type
== OBJ_BLOB
&& path
&& would_convert_to_git_filter_fd(istate
, path
))
2014 ret
= index_stream_convert_blob(istate
, oid
, fd
, path
, flags
);
2015 else if (!S_ISREG(st
->st_mode
))
2016 ret
= index_pipe(istate
, oid
, fd
, type
, path
, flags
);
2017 else if (st
->st_size
<= big_file_threshold
|| type
!= OBJ_BLOB
||
2018 (path
&& would_convert_to_git(istate
, path
)))
2019 ret
= index_core(istate
, oid
, fd
, xsize_t(st
->st_size
),
2022 ret
= index_stream(oid
, fd
, xsize_t(st
->st_size
), type
, path
,
2028 int index_path(struct index_state
*istate
, struct object_id
*oid
,
2029 const char *path
, struct stat
*st
, unsigned flags
)
2032 struct strbuf sb
= STRBUF_INIT
;
2035 switch (st
->st_mode
& S_IFMT
) {
2037 fd
= open(path
, O_RDONLY
);
2039 return error_errno("open(\"%s\")", path
);
2040 if (index_fd(istate
, oid
, fd
, st
, OBJ_BLOB
, path
, flags
) < 0)
2041 return error(_("%s: failed to insert into database"),
2045 if (strbuf_readlink(&sb
, path
, st
->st_size
))
2046 return error_errno("readlink(\"%s\")", path
);
2047 if (!(flags
& HASH_WRITE_OBJECT
))
2048 hash_object_file(sb
.buf
, sb
.len
, blob_type
, oid
);
2049 else if (write_object_file(sb
.buf
, sb
.len
, blob_type
, oid
))
2050 rc
= error(_("%s: failed to insert into database"), path
);
2051 strbuf_release(&sb
);
2054 return resolve_gitlink_ref(path
, "HEAD", oid
);
2056 return error(_("%s: unsupported file type"), path
);
2061 int read_pack_header(int fd
, struct pack_header
*header
)
2063 if (read_in_full(fd
, header
, sizeof(*header
)) != sizeof(*header
))
2064 /* "eof before pack header was fully read" */
2065 return PH_ERROR_EOF
;
2067 if (header
->hdr_signature
!= htonl(PACK_SIGNATURE
))
2068 /* "protocol error (pack signature mismatch detected)" */
2069 return PH_ERROR_PACK_SIGNATURE
;
2070 if (!pack_version_ok(header
->hdr_version
))
2071 /* "protocol error (pack version unsupported)" */
2072 return PH_ERROR_PROTOCOL
;
2076 void assert_oid_type(const struct object_id
*oid
, enum object_type expect
)
2078 enum object_type type
= oid_object_info(the_repository
, oid
, NULL
);
2080 die(_("%s is not a valid object"), oid_to_hex(oid
));
2082 die(_("%s is not a valid '%s' object"), oid_to_hex(oid
),
2086 int for_each_file_in_obj_subdir(unsigned int subdir_nr
,
2087 struct strbuf
*path
,
2088 each_loose_object_fn obj_cb
,
2089 each_loose_cruft_fn cruft_cb
,
2090 each_loose_subdir_fn subdir_cb
,
2093 size_t origlen
, baselen
;
2097 struct object_id oid
;
2099 if (subdir_nr
> 0xff)
2100 BUG("invalid loose object subdirectory: %x", subdir_nr
);
2102 origlen
= path
->len
;
2103 strbuf_complete(path
, '/');
2104 strbuf_addf(path
, "%02x", subdir_nr
);
2106 dir
= opendir(path
->buf
);
2108 if (errno
!= ENOENT
)
2109 r
= error_errno(_("unable to open %s"), path
->buf
);
2110 strbuf_setlen(path
, origlen
);
2114 oid
.hash
[0] = subdir_nr
;
2115 strbuf_addch(path
, '/');
2116 baselen
= path
->len
;
2118 while ((de
= readdir(dir
))) {
2120 if (is_dot_or_dotdot(de
->d_name
))
2123 namelen
= strlen(de
->d_name
);
2124 strbuf_setlen(path
, baselen
);
2125 strbuf_add(path
, de
->d_name
, namelen
);
2126 if (namelen
== the_hash_algo
->hexsz
- 2 &&
2127 !hex_to_bytes(oid
.hash
+ 1, de
->d_name
,
2128 the_hash_algo
->rawsz
- 1)) {
2130 r
= obj_cb(&oid
, path
->buf
, data
);
2138 r
= cruft_cb(de
->d_name
, path
->buf
, data
);
2145 strbuf_setlen(path
, baselen
- 1);
2146 if (!r
&& subdir_cb
)
2147 r
= subdir_cb(subdir_nr
, path
->buf
, data
);
2149 strbuf_setlen(path
, origlen
);
2154 int for_each_loose_file_in_objdir_buf(struct strbuf
*path
,
2155 each_loose_object_fn obj_cb
,
2156 each_loose_cruft_fn cruft_cb
,
2157 each_loose_subdir_fn subdir_cb
,
2163 for (i
= 0; i
< 256; i
++) {
2164 r
= for_each_file_in_obj_subdir(i
, path
, obj_cb
, cruft_cb
,
2173 int for_each_loose_file_in_objdir(const char *path
,
2174 each_loose_object_fn obj_cb
,
2175 each_loose_cruft_fn cruft_cb
,
2176 each_loose_subdir_fn subdir_cb
,
2179 struct strbuf buf
= STRBUF_INIT
;
2182 strbuf_addstr(&buf
, path
);
2183 r
= for_each_loose_file_in_objdir_buf(&buf
, obj_cb
, cruft_cb
,
2185 strbuf_release(&buf
);
2190 int for_each_loose_object(each_loose_object_fn cb
, void *data
,
2191 enum for_each_object_flags flags
)
2193 struct object_directory
*odb
;
2195 prepare_alt_odb(the_repository
);
2196 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
) {
2197 int r
= for_each_loose_file_in_objdir(odb
->path
, cb
, NULL
,
2202 if (flags
& FOR_EACH_OBJECT_LOCAL_ONLY
)
2209 static int append_loose_object(const struct object_id
*oid
, const char *path
,
2212 oid_array_append(data
, oid
);
2216 struct oid_array
*odb_loose_cache(struct object_directory
*odb
,
2217 const struct object_id
*oid
)
2219 int subdir_nr
= oid
->hash
[0];
2220 struct strbuf buf
= STRBUF_INIT
;
2222 if (subdir_nr
< 0 ||
2223 subdir_nr
>= ARRAY_SIZE(odb
->loose_objects_subdir_seen
))
2224 BUG("subdir_nr out of range");
2226 if (odb
->loose_objects_subdir_seen
[subdir_nr
])
2227 return &odb
->loose_objects_cache
[subdir_nr
];
2229 strbuf_addstr(&buf
, odb
->path
);
2230 for_each_file_in_obj_subdir(subdir_nr
, &buf
,
2231 append_loose_object
,
2233 &odb
->loose_objects_cache
[subdir_nr
]);
2234 odb
->loose_objects_subdir_seen
[subdir_nr
] = 1;
2235 strbuf_release(&buf
);
2236 return &odb
->loose_objects_cache
[subdir_nr
];
2239 void odb_clear_loose_cache(struct object_directory
*odb
)
2243 for (i
= 0; i
< ARRAY_SIZE(odb
->loose_objects_cache
); i
++)
2244 oid_array_clear(&odb
->loose_objects_cache
[i
]);
2245 memset(&odb
->loose_objects_subdir_seen
, 0,
2246 sizeof(odb
->loose_objects_subdir_seen
));
2249 static int check_stream_oid(git_zstream
*stream
,
2253 const struct object_id
*expected_oid
)
2256 struct object_id real_oid
;
2257 unsigned char buf
[4096];
2258 unsigned long total_read
;
2261 the_hash_algo
->init_fn(&c
);
2262 the_hash_algo
->update_fn(&c
, hdr
, stream
->total_out
);
2265 * We already read some bytes into hdr, but the ones up to the NUL
2266 * do not count against the object's content size.
2268 total_read
= stream
->total_out
- strlen(hdr
) - 1;
2271 * This size comparison must be "<=" to read the final zlib packets;
2272 * see the comment in unpack_loose_rest for details.
2274 while (total_read
<= size
&&
2276 (status
== Z_BUF_ERROR
&& !stream
->avail_out
))) {
2277 stream
->next_out
= buf
;
2278 stream
->avail_out
= sizeof(buf
);
2279 if (size
- total_read
< stream
->avail_out
)
2280 stream
->avail_out
= size
- total_read
;
2281 status
= git_inflate(stream
, Z_FINISH
);
2282 the_hash_algo
->update_fn(&c
, buf
, stream
->next_out
- buf
);
2283 total_read
+= stream
->next_out
- buf
;
2285 git_inflate_end(stream
);
2287 if (status
!= Z_STREAM_END
) {
2288 error(_("corrupt loose object '%s'"), oid_to_hex(expected_oid
));
2291 if (stream
->avail_in
) {
2292 error(_("garbage at end of loose object '%s'"),
2293 oid_to_hex(expected_oid
));
2297 the_hash_algo
->final_fn(real_oid
.hash
, &c
);
2298 if (!oideq(expected_oid
, &real_oid
)) {
2299 error(_("hash mismatch for %s (expected %s)"), path
,
2300 oid_to_hex(expected_oid
));
2307 int read_loose_object(const char *path
,
2308 const struct object_id
*expected_oid
,
2309 enum object_type
*type
,
2310 unsigned long *size
,
2315 unsigned long mapsize
;
2317 char hdr
[MAX_HEADER_LEN
];
2321 map
= map_loose_object_1(the_repository
, path
, NULL
, &mapsize
);
2323 error_errno(_("unable to mmap %s"), path
);
2327 if (unpack_loose_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0) {
2328 error(_("unable to unpack header of %s"), path
);
2332 *type
= parse_loose_header(hdr
, size
);
2334 error(_("unable to parse header of %s"), path
);
2335 git_inflate_end(&stream
);
2339 if (*type
== OBJ_BLOB
&& *size
> big_file_threshold
) {
2340 if (check_stream_oid(&stream
, hdr
, *size
, path
, expected_oid
) < 0)
2343 *contents
= unpack_loose_rest(&stream
, hdr
, *size
, expected_oid
);
2345 error(_("unable to unpack contents of %s"), path
);
2346 git_inflate_end(&stream
);
2349 if (check_object_signature(expected_oid
, *contents
,
2350 *size
, type_name(*type
))) {
2351 error(_("hash mismatch for %s (expected %s)"), path
,
2352 oid_to_hex(expected_oid
));
2358 ret
= 0; /* everything checks out */
2362 munmap(map
, mapsize
);