summaryrefslogtreecommitdiff
path: root/object-file.c
diff options
context:
space:
mode:
Diffstat (limited to 'object-file.c')
-rw-r--r--object-file.c499
1 files changed, 126 insertions, 373 deletions
diff --git a/object-file.c b/object-file.c
index b1a3463852..f3810871f0 100644
--- a/object-file.c
+++ b/object-file.c
@@ -8,6 +8,7 @@
*/
#define USE_THE_REPOSITORY_VARIABLE
+#define DISABLE_SIGN_COMPARE_WARNINGS
#include "git-compat-util.h"
#include "abspath.h"
@@ -44,299 +45,34 @@
/* The maximum size for an object header. */
#define MAX_HEADER_LEN 32
-
-#define EMPTY_TREE_SHA1_BIN_LITERAL \
- "\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60" \
- "\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04"
-#define EMPTY_TREE_SHA256_BIN_LITERAL \
- "\x6e\xf1\x9b\x41\x22\x5c\x53\x69\xf1\xc1" \
- "\x04\xd4\x5d\x8d\x85\xef\xa9\xb0\x57\xb5" \
- "\x3b\x14\xb4\xb9\xb9\x39\xdd\x74\xde\xcc" \
- "\x53\x21"
-
-#define EMPTY_BLOB_SHA1_BIN_LITERAL \
- "\xe6\x9d\xe2\x9b\xb2\xd1\xd6\x43\x4b\x8b" \
- "\x29\xae\x77\x5a\xd8\xc2\xe4\x8c\x53\x91"
-#define EMPTY_BLOB_SHA256_BIN_LITERAL \
- "\x47\x3a\x0f\x4c\x3b\xe8\xa9\x36\x81\xa2" \
- "\x67\xe3\xb1\xe9\xa7\xdc\xda\x11\x85\x43" \
- "\x6f\xe1\x41\xf7\x74\x91\x20\xa3\x03\x72" \
- "\x18\x13"
-
-static const struct object_id empty_tree_oid = {
- .hash = EMPTY_TREE_SHA1_BIN_LITERAL,
- .algo = GIT_HASH_SHA1,
-};
-static const struct object_id empty_blob_oid = {
- .hash = EMPTY_BLOB_SHA1_BIN_LITERAL,
- .algo = GIT_HASH_SHA1,
-};
-static const struct object_id null_oid_sha1 = {
- .hash = {0},
- .algo = GIT_HASH_SHA1,
-};
-static const struct object_id empty_tree_oid_sha256 = {
- .hash = EMPTY_TREE_SHA256_BIN_LITERAL,
- .algo = GIT_HASH_SHA256,
-};
-static const struct object_id empty_blob_oid_sha256 = {
- .hash = EMPTY_BLOB_SHA256_BIN_LITERAL,
- .algo = GIT_HASH_SHA256,
-};
-static const struct object_id null_oid_sha256 = {
- .hash = {0},
- .algo = GIT_HASH_SHA256,
-};
-
-static void git_hash_sha1_init(git_hash_ctx *ctx)
-{
- git_SHA1_Init(&ctx->sha1);
-}
-
-static void git_hash_sha1_clone(git_hash_ctx *dst, const git_hash_ctx *src)
-{
- git_SHA1_Clone(&dst->sha1, &src->sha1);
-}
-
-static void git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
-{
- git_SHA1_Update(&ctx->sha1, data, len);
-}
-
-static void git_hash_sha1_final(unsigned char *hash, git_hash_ctx *ctx)
-{
- git_SHA1_Final(hash, &ctx->sha1);
-}
-
-static void git_hash_sha1_final_oid(struct object_id *oid, git_hash_ctx *ctx)
-{
- git_SHA1_Final(oid->hash, &ctx->sha1);
- memset(oid->hash + GIT_SHA1_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA1_RAWSZ);
- oid->algo = GIT_HASH_SHA1;
-}
-
-static void git_hash_sha1_init_unsafe(git_hash_ctx *ctx)
-{
- git_SHA1_Init_unsafe(&ctx->sha1_unsafe);
-}
-
-static void git_hash_sha1_clone_unsafe(git_hash_ctx *dst, const git_hash_ctx *src)
-{
- git_SHA1_Clone_unsafe(&dst->sha1_unsafe, &src->sha1_unsafe);
-}
-
-static void git_hash_sha1_update_unsafe(git_hash_ctx *ctx, const void *data,
- size_t len)
-{
- git_SHA1_Update_unsafe(&ctx->sha1_unsafe, data, len);
-}
-
-static void git_hash_sha1_final_unsafe(unsigned char *hash, git_hash_ctx *ctx)
-{
- git_SHA1_Final_unsafe(hash, &ctx->sha1_unsafe);
-}
-
-static void git_hash_sha1_final_oid_unsafe(struct object_id *oid, git_hash_ctx *ctx)
-{
- git_SHA1_Final_unsafe(oid->hash, &ctx->sha1_unsafe);
- memset(oid->hash + GIT_SHA1_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA1_RAWSZ);
- oid->algo = GIT_HASH_SHA1;
-}
-
-static void git_hash_sha256_init(git_hash_ctx *ctx)
-{
- git_SHA256_Init(&ctx->sha256);
-}
-
-static void git_hash_sha256_clone(git_hash_ctx *dst, const git_hash_ctx *src)
-{
- git_SHA256_Clone(&dst->sha256, &src->sha256);
-}
-
-static void git_hash_sha256_update(git_hash_ctx *ctx, const void *data, size_t len)
-{
- git_SHA256_Update(&ctx->sha256, data, len);
-}
-
-static void git_hash_sha256_final(unsigned char *hash, git_hash_ctx *ctx)
-{
- git_SHA256_Final(hash, &ctx->sha256);
-}
-
-static void git_hash_sha256_final_oid(struct object_id *oid, git_hash_ctx *ctx)
-{
- git_SHA256_Final(oid->hash, &ctx->sha256);
- /*
- * This currently does nothing, so the compiler should optimize it out,
- * but keep it in case we extend the hash size again.
- */
- memset(oid->hash + GIT_SHA256_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA256_RAWSZ);
- oid->algo = GIT_HASH_SHA256;
-}
-
-static void git_hash_unknown_init(git_hash_ctx *ctx UNUSED)
-{
- BUG("trying to init unknown hash");
-}
-
-static void git_hash_unknown_clone(git_hash_ctx *dst UNUSED,
- const git_hash_ctx *src UNUSED)
-{
- BUG("trying to clone unknown hash");
-}
-
-static void git_hash_unknown_update(git_hash_ctx *ctx UNUSED,
- const void *data UNUSED,
- size_t len UNUSED)
-{
- BUG("trying to update unknown hash");
-}
-
-static void git_hash_unknown_final(unsigned char *hash UNUSED,
- git_hash_ctx *ctx UNUSED)
-{
- BUG("trying to finalize unknown hash");
-}
-
-static void git_hash_unknown_final_oid(struct object_id *oid UNUSED,
- git_hash_ctx *ctx UNUSED)
-{
- BUG("trying to finalize unknown hash");
-}
-
-const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
- {
- .name = NULL,
- .format_id = 0x00000000,
- .rawsz = 0,
- .hexsz = 0,
- .blksz = 0,
- .init_fn = git_hash_unknown_init,
- .clone_fn = git_hash_unknown_clone,
- .update_fn = git_hash_unknown_update,
- .final_fn = git_hash_unknown_final,
- .final_oid_fn = git_hash_unknown_final_oid,
- .unsafe_init_fn = git_hash_unknown_init,
- .unsafe_clone_fn = git_hash_unknown_clone,
- .unsafe_update_fn = git_hash_unknown_update,
- .unsafe_final_fn = git_hash_unknown_final,
- .unsafe_final_oid_fn = git_hash_unknown_final_oid,
- .empty_tree = NULL,
- .empty_blob = NULL,
- .null_oid = NULL,
- },
- {
- .name = "sha1",
- .format_id = GIT_SHA1_FORMAT_ID,
- .rawsz = GIT_SHA1_RAWSZ,
- .hexsz = GIT_SHA1_HEXSZ,
- .blksz = GIT_SHA1_BLKSZ,
- .init_fn = git_hash_sha1_init,
- .clone_fn = git_hash_sha1_clone,
- .update_fn = git_hash_sha1_update,
- .final_fn = git_hash_sha1_final,
- .final_oid_fn = git_hash_sha1_final_oid,
- .unsafe_init_fn = git_hash_sha1_init_unsafe,
- .unsafe_clone_fn = git_hash_sha1_clone_unsafe,
- .unsafe_update_fn = git_hash_sha1_update_unsafe,
- .unsafe_final_fn = git_hash_sha1_final_unsafe,
- .unsafe_final_oid_fn = git_hash_sha1_final_oid_unsafe,
- .empty_tree = &empty_tree_oid,
- .empty_blob = &empty_blob_oid,
- .null_oid = &null_oid_sha1,
- },
- {
- .name = "sha256",
- .format_id = GIT_SHA256_FORMAT_ID,
- .rawsz = GIT_SHA256_RAWSZ,
- .hexsz = GIT_SHA256_HEXSZ,
- .blksz = GIT_SHA256_BLKSZ,
- .init_fn = git_hash_sha256_init,
- .clone_fn = git_hash_sha256_clone,
- .update_fn = git_hash_sha256_update,
- .final_fn = git_hash_sha256_final,
- .final_oid_fn = git_hash_sha256_final_oid,
- .unsafe_init_fn = git_hash_sha256_init,
- .unsafe_clone_fn = git_hash_sha256_clone,
- .unsafe_update_fn = git_hash_sha256_update,
- .unsafe_final_fn = git_hash_sha256_final,
- .unsafe_final_oid_fn = git_hash_sha256_final_oid,
- .empty_tree = &empty_tree_oid_sha256,
- .empty_blob = &empty_blob_oid_sha256,
- .null_oid = &null_oid_sha256,
- }
-};
-
-const struct object_id *null_oid(void)
-{
- return the_hash_algo->null_oid;
-}
-
-const char *empty_tree_oid_hex(const struct git_hash_algo *algop)
-{
- static char buf[GIT_MAX_HEXSZ + 1];
- return oid_to_hex_r(buf, algop->empty_tree);
-}
-
-int hash_algo_by_name(const char *name)
-{
- int i;
- if (!name)
- return GIT_HASH_UNKNOWN;
- for (i = 1; i < GIT_HASH_NALGOS; i++)
- if (!strcmp(name, hash_algos[i].name))
- return i;
- return GIT_HASH_UNKNOWN;
-}
-
-int hash_algo_by_id(uint32_t format_id)
-{
- int i;
- for (i = 1; i < GIT_HASH_NALGOS; i++)
- if (format_id == hash_algos[i].format_id)
- return i;
- return GIT_HASH_UNKNOWN;
-}
-
-int hash_algo_by_length(int len)
-{
- int i;
- for (i = 1; i < GIT_HASH_NALGOS; i++)
- if (len == hash_algos[i].rawsz)
- return i;
- return GIT_HASH_UNKNOWN;
-}
-
/*
* This is meant to hold a *small* number of objects that you would
* want repo_read_object_file() to be able to return, but yet you do not want
* to write them into the object store (e.g. a browse-only
* application).
*/
-static struct cached_object {
+static struct cached_object_entry {
struct object_id oid;
- enum object_type type;
- const void *buf;
- unsigned long size;
+ struct cached_object {
+ enum object_type type;
+ const void *buf;
+ unsigned long size;
+ } value;
} *cached_objects;
static int cached_object_nr, cached_object_alloc;
-static struct cached_object empty_tree = {
- .oid = {
- .hash = EMPTY_TREE_SHA1_BIN_LITERAL,
- },
- .type = OBJ_TREE,
- .buf = "",
-};
-
-static struct cached_object *find_cached_object(const struct object_id *oid)
+static const struct cached_object *find_cached_object(const struct object_id *oid)
{
+ static const struct cached_object empty_tree = {
+ .type = OBJ_TREE,
+ .buf = "",
+ };
int i;
- struct cached_object *co = cached_objects;
+ const struct cached_object_entry *co = cached_objects;
for (i = 0; i < cached_object_nr; i++, co++) {
if (oideq(&co->oid, oid))
- return co;
+ return &co->value;
}
if (oideq(oid, the_hash_algo->empty_tree))
return &empty_tree;
@@ -381,7 +117,7 @@ int mkdir_in_gitdir(const char *path)
}
strbuf_release(&sb);
}
- return adjust_shared_perm(path);
+ return adjust_shared_perm(the_repository, path);
}
static enum scld_error safe_create_leading_directories_1(char *path, int share)
@@ -430,7 +166,7 @@ static enum scld_error safe_create_leading_directories_1(char *path, int share)
ret = SCLD_VANISHED;
else
ret = SCLD_FAILED;
- } else if (share && adjust_shared_perm(path)) {
+ } else if (share && adjust_shared_perm(the_repository, path)) {
ret = SCLD_PERMS;
}
*slash = slash_character;
@@ -469,14 +205,14 @@ int odb_mkstemp(struct strbuf *temp_filename, const char *pattern)
* restrictive except to remove write permission.
*/
int mode = 0444;
- git_path_buf(temp_filename, "objects/%s", pattern);
+ repo_git_path_replace(the_repository, temp_filename, "objects/%s", pattern);
fd = git_mkstemp_mode(temp_filename->buf, mode);
if (0 <= fd)
return fd;
/* slow path */
/* some mkstemp implementations erase temp_filename on failure */
- git_path_buf(temp_filename, "objects/%s", pattern);
+ repo_git_path_replace(the_repository, temp_filename, "objects/%s", pattern);
safe_create_leading_directories(temp_filename->buf);
return xmkstemp_mode(temp_filename->buf, mode);
}
@@ -710,7 +446,7 @@ static void read_info_alternates(struct repository *r,
void add_to_alternates_file(const char *reference)
{
struct lock_file lock = LOCK_INIT;
- char *alts = git_pathdup("objects/info/alternates");
+ char *alts = repo_git_path(the_repository, "objects/info/alternates");
FILE *in, *out;
int found = 0;
@@ -1173,7 +909,7 @@ int stream_object_signature(struct repository *r, const struct object_id *oid)
unsigned long size;
enum object_type obj_type;
struct git_istream *st;
- git_hash_ctx c;
+ struct git_hash_ctx c;
char hdr[MAX_HEADER_LEN];
int hdrlen;
@@ -1186,7 +922,7 @@ int stream_object_signature(struct repository *r, const struct object_id *oid)
/* Sha1.. */
r->hash_algo->init_fn(&c);
- r->hash_algo->update_fn(&c, hdr, hdrlen);
+ git_hash_update(&c, hdr, hdrlen);
for (;;) {
char buf[1024 * 16];
ssize_t readlen = read_istream(st, buf, sizeof(buf));
@@ -1197,9 +933,9 @@ int stream_object_signature(struct repository *r, const struct object_id *oid)
}
if (!readlen)
break;
- r->hash_algo->update_fn(&c, buf, readlen);
+ git_hash_update(&c, buf, readlen);
}
- r->hash_algo->final_oid_fn(&real_oid, &c);
+ git_hash_final_oid(&real_oid, &c);
close_istream(st);
return !oideq(oid, &real_oid) ? -1 : 0;
}
@@ -1349,7 +1085,7 @@ enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
obj_read_unlock();
status = git_inflate(stream, 0);
obj_read_lock();
- if (status < Z_OK)
+ if (status != Z_OK && status != Z_STREAM_END)
return ULHR_BAD;
/*
@@ -1372,20 +1108,19 @@ enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
* reading the stream.
*/
strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
- stream->next_out = buffer;
- stream->avail_out = bufsiz;
do {
+ stream->next_out = buffer;
+ stream->avail_out = bufsiz;
+
obj_read_unlock();
status = git_inflate(stream, 0);
obj_read_lock();
strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
return 0;
- stream->next_out = buffer;
- stream->avail_out = bufsiz;
- } while (status != Z_STREAM_END);
- return ULHR_TOO_LONG;
+ } while (status == Z_OK);
+ return ULHR_BAD;
}
static void *unpack_loose_rest(git_zstream *stream,
@@ -1424,18 +1159,17 @@ static void *unpack_loose_rest(git_zstream *stream,
obj_read_lock();
}
}
- if (status == Z_STREAM_END && !stream->avail_in) {
- git_inflate_end(stream);
- return buf;
- }
- if (status < 0)
+ if (status != Z_STREAM_END) {
error(_("corrupt loose object '%s'"), oid_to_hex(oid));
- else if (stream->avail_in)
+ FREE_AND_NULL(buf);
+ } else if (stream->avail_in) {
error(_("garbage at end of loose object '%s'"),
oid_to_hex(oid));
- free(buf);
- return NULL;
+ FREE_AND_NULL(buf);
+ }
+
+ return buf;
}
/*
@@ -1567,6 +1301,8 @@ static int loose_object_info(struct repository *r,
if (!oi->contentp)
break;
+ if (hdrbuf.len)
+ BUG("unpacking content with unknown types not yet supported");
*oi->contentp = unpack_loose_rest(&stream, hdr, *oi->sizep, oid);
if (*oi->contentp)
goto cleanup;
@@ -1587,8 +1323,8 @@ static int loose_object_info(struct repository *r,
die(_("loose object %s (stored in %s) is corrupt"),
oid_to_hex(oid), path);
- git_inflate_end(&stream);
cleanup:
+ git_inflate_end(&stream);
munmap(map, mapsize);
if (oi->sizep == &size_scratch)
oi->sizep = NULL;
@@ -1627,7 +1363,7 @@ static int do_oid_object_info_extended(struct repository *r,
struct object_info *oi, unsigned flags)
{
static struct object_info blank_oi = OBJECT_INFO_INIT;
- struct cached_object *co;
+ const struct cached_object *co;
struct pack_entry e;
int rtype;
const struct object_id *real = oid;
@@ -1780,7 +1516,7 @@ static int oid_object_info_convert(struct repository *r,
if (type == -1)
return -1;
if (type != OBJ_BLOB) {
- ret = convert_object_file(&outbuf,
+ ret = convert_object_file(the_repository, &outbuf,
the_hash_algo, input_algo,
content, size, type, !do_die);
free(content);
@@ -1849,7 +1585,7 @@ int oid_object_info(struct repository *r,
int pretend_object_file(void *buf, unsigned long len, enum object_type type,
struct object_id *oid)
{
- struct cached_object *co;
+ struct cached_object_entry *co;
char *co_buf;
hash_object_file(the_hash_algo, buf, len, type, oid);
@@ -1858,11 +1594,11 @@ int pretend_object_file(void *buf, unsigned long len, enum object_type type,
return 0;
ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc);
co = &cached_objects[cached_object_nr++];
- co->size = len;
- co->type = type;
+ co->value.size = len;
+ co->value.type = type;
co_buf = xmalloc(len);
memcpy(co_buf, buf, len);
- co->buf = co_buf;
+ co->value.buf = co_buf;
oidcpy(&co->oid, oid);
return 0;
}
@@ -1938,15 +1674,15 @@ void *read_object_with_reference(struct repository *r,
}
}
-static void hash_object_body(const struct git_hash_algo *algo, git_hash_ctx *c,
+static void hash_object_body(const struct git_hash_algo *algo, struct git_hash_ctx *c,
const void *buf, unsigned long len,
struct object_id *oid,
char *hdr, int *hdrlen)
{
algo->init_fn(c);
- algo->update_fn(c, hdr, *hdrlen);
- algo->update_fn(c, buf, len);
- algo->final_oid_fn(oid, c);
+ git_hash_update(c, hdr, *hdrlen);
+ git_hash_update(c, buf, len);
+ git_hash_final_oid(oid, c);
}
static void write_object_file_prepare(const struct git_hash_algo *algo,
@@ -1954,7 +1690,7 @@ static void write_object_file_prepare(const struct git_hash_algo *algo,
enum object_type type, struct object_id *oid,
char *hdr, int *hdrlen)
{
- git_hash_ctx c;
+ struct git_hash_ctx c;
/* Generate the header */
*hdrlen = format_object_header(hdr, *hdrlen, type, len);
@@ -1968,60 +1704,65 @@ static void write_object_file_prepare_literally(const struct git_hash_algo *algo
const char *type, struct object_id *oid,
char *hdr, int *hdrlen)
{
- git_hash_ctx c;
+ struct git_hash_ctx c;
*hdrlen = format_object_header_literally(hdr, *hdrlen, type, len);
hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen);
}
-static int check_collision(const char *filename_a, const char *filename_b)
+#define CHECK_COLLISION_DEST_VANISHED -2
+
+static int check_collision(const char *source, const char *dest)
{
- char buf_a[4096], buf_b[4096];
- int fd_a = -1, fd_b = -1;
+ char buf_source[4096], buf_dest[4096];
+ int fd_source = -1, fd_dest = -1;
int ret = 0;
- fd_a = open(filename_a, O_RDONLY);
- if (fd_a < 0) {
- ret = error_errno(_("unable to open %s"), filename_a);
+ fd_source = open(source, O_RDONLY);
+ if (fd_source < 0) {
+ ret = error_errno(_("unable to open %s"), source);
goto out;
}
- fd_b = open(filename_b, O_RDONLY);
- if (fd_b < 0) {
- ret = error_errno(_("unable to open %s"), filename_b);
+ fd_dest = open(dest, O_RDONLY);
+ if (fd_dest < 0) {
+ if (errno != ENOENT)
+ ret = error_errno(_("unable to open %s"), dest);
+ else
+ ret = CHECK_COLLISION_DEST_VANISHED;
goto out;
}
while (1) {
ssize_t sz_a, sz_b;
- sz_a = read_in_full(fd_a, buf_a, sizeof(buf_a));
+ sz_a = read_in_full(fd_source, buf_source, sizeof(buf_source));
if (sz_a < 0) {
- ret = error_errno(_("unable to read %s"), filename_a);
+ ret = error_errno(_("unable to read %s"), source);
goto out;
}
- sz_b = read_in_full(fd_b, buf_b, sizeof(buf_b));
+ sz_b = read_in_full(fd_dest, buf_dest, sizeof(buf_dest));
if (sz_b < 0) {
- ret = error_errno(_("unable to read %s"), filename_b);
+ ret = error_errno(_("unable to read %s"), dest);
goto out;
}
- if (sz_a != sz_b || memcmp(buf_a, buf_b, sz_a)) {
+ if (sz_a != sz_b || memcmp(buf_source, buf_dest, sz_a)) {
ret = error(_("files '%s' and '%s' differ in contents"),
- filename_a, filename_b);
+ source, dest);
goto out;
}
- if (sz_a < sizeof(buf_a))
+ if (sz_a < sizeof(buf_source))
break;
}
out:
- if (fd_a > -1)
- close(fd_a);
- if (fd_b > -1)
- close(fd_b);
+ if (fd_source > -1)
+ close(fd_source);
+ if (fd_dest > -1)
+ close(fd_dest);
return ret;
}
@@ -2036,8 +1777,11 @@ int finalize_object_file(const char *tmpfile, const char *filename)
int finalize_object_file_flags(const char *tmpfile, const char *filename,
enum finalize_object_file_flags flags)
{
- struct stat st;
- int ret = 0;
+ unsigned retries = 0;
+ int ret;
+
+retry:
+ ret = 0;
if (object_creation_mode == OBJECT_CREATION_USES_RENAMES)
goto try_rename;
@@ -2058,6 +1802,8 @@ int finalize_object_file_flags(const char *tmpfile, const char *filename,
* left to unlink.
*/
if (ret && ret != EEXIST) {
+ struct stat st;
+
try_rename:
if (!stat(filename, &st))
ret = EEXIST;
@@ -2073,14 +1819,22 @@ int finalize_object_file_flags(const char *tmpfile, const char *filename,
errno = saved_errno;
return error_errno(_("unable to write file %s"), filename);
}
- if (!(flags & FOF_SKIP_COLLISION_CHECK) &&
- check_collision(tmpfile, filename))
+ if (!(flags & FOF_SKIP_COLLISION_CHECK)) {
+ ret = check_collision(tmpfile, filename);
+ if (ret == CHECK_COLLISION_DEST_VANISHED) {
+ if (retries++ > 5)
+ return error(_("unable to write repeatedly vanishing file %s"),
+ filename);
+ goto retry;
+ }
+ else if (ret)
return -1;
+ }
unlink_or_warn(tmpfile);
}
out:
- if (adjust_shared_perm(filename))
+ if (adjust_shared_perm(the_repository, filename))
return error(_("unable to set permission to '%s'"), filename);
return 0;
}
@@ -2156,7 +1910,7 @@ static int create_tmpfile(struct strbuf *tmp, const char *filename)
strbuf_add(tmp, filename, dirlen - 1);
if (mkdir(tmp->buf, 0777) && errno != EEXIST)
return -1;
- if (adjust_shared_perm(tmp->buf))
+ if (adjust_shared_perm(the_repository, tmp->buf))
return -1;
/* Try again */
@@ -2181,7 +1935,7 @@ static int start_loose_object_common(struct strbuf *tmp_file,
const char *filename, unsigned flags,
git_zstream *stream,
unsigned char *buf, size_t buflen,
- git_hash_ctx *c, git_hash_ctx *compat_c,
+ struct git_hash_ctx *c, struct git_hash_ctx *compat_c,
char *hdr, int hdrlen)
{
struct repository *repo = the_repository;
@@ -2215,9 +1969,9 @@ static int start_loose_object_common(struct strbuf *tmp_file,
stream->avail_in = hdrlen;
while (git_deflate(stream, 0) == Z_OK)
; /* nothing */
- algo->update_fn(c, hdr, hdrlen);
+ git_hash_update(c, hdr, hdrlen);
if (compat && compat_c)
- compat->update_fn(compat_c, hdr, hdrlen);
+ git_hash_update(compat_c, hdr, hdrlen);
return fd;
}
@@ -2226,21 +1980,20 @@ static int start_loose_object_common(struct strbuf *tmp_file,
* Common steps for the inner git_deflate() loop for writing loose
* objects. Returns what git_deflate() returns.
*/
-static int write_loose_object_common(git_hash_ctx *c, git_hash_ctx *compat_c,
+static int write_loose_object_common(struct git_hash_ctx *c, struct git_hash_ctx *compat_c,
git_zstream *stream, const int flush,
unsigned char *in0, const int fd,
unsigned char *compressed,
const size_t compressed_len)
{
struct repository *repo = the_repository;
- const struct git_hash_algo *algo = repo->hash_algo;
const struct git_hash_algo *compat = repo->compat_hash_algo;
int ret;
ret = git_deflate(stream, flush ? Z_FINISH : 0);
- algo->update_fn(c, in0, stream->next_in - in0);
+ git_hash_update(c, in0, stream->next_in - in0);
if (compat && compat_c)
- compat->update_fn(compat_c, in0, stream->next_in - in0);
+ git_hash_update(compat_c, in0, stream->next_in - in0);
if (write_in_full(fd, compressed, stream->next_out - compressed) < 0)
die_errno(_("unable to write loose object file"));
stream->next_out = compressed;
@@ -2255,21 +2008,20 @@ static int write_loose_object_common(git_hash_ctx *c, git_hash_ctx *compat_c,
* - End the compression of zlib stream.
* - Get the calculated oid to "oid".
*/
-static int end_loose_object_common(git_hash_ctx *c, git_hash_ctx *compat_c,
+static int end_loose_object_common(struct git_hash_ctx *c, struct git_hash_ctx *compat_c,
git_zstream *stream, struct object_id *oid,
struct object_id *compat_oid)
{
struct repository *repo = the_repository;
- const struct git_hash_algo *algo = repo->hash_algo;
const struct git_hash_algo *compat = repo->compat_hash_algo;
int ret;
ret = git_deflate_end_gently(stream);
if (ret != Z_OK)
return ret;
- algo->final_oid_fn(oid, c);
+ git_hash_final_oid(oid, c);
if (compat && compat_c)
- compat->final_oid_fn(compat_oid, compat_c);
+ git_hash_final_oid(compat_oid, compat_c);
return Z_OK;
}
@@ -2281,7 +2033,7 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
int fd, ret;
unsigned char compressed[4096];
git_zstream stream;
- git_hash_ctx c;
+ struct git_hash_ctx c;
struct object_id parano_oid;
static struct strbuf tmp_file = STRBUF_INIT;
static struct strbuf filename = STRBUF_INIT;
@@ -2361,7 +2113,7 @@ int stream_loose_object(struct input_stream *in_stream, size_t len,
int fd, ret, err = 0, flush = 0;
unsigned char compressed[4096];
git_zstream stream;
- git_hash_ctx c, compat_c;
+ struct git_hash_ctx c, compat_c;
struct strbuf tmp_file = STRBUF_INIT;
struct strbuf filename = STRBUF_INIT;
int dirlen;
@@ -2481,7 +2233,7 @@ int write_object_file_flags(const void *buf, unsigned long len,
hash_object_file(compat, buf, len, type, &compat_oid);
else {
struct strbuf converted = STRBUF_INIT;
- convert_object_file(&converted, algo, compat,
+ convert_object_file(the_repository, &converted, algo, compat,
buf, len, type, 0);
hash_object_file(compat, converted.buf, converted.len,
type, &compat_oid);
@@ -2521,7 +2273,8 @@ int write_object_file_literally(const void *buf, unsigned long len,
&compat_oid);
else if (compat_type != -1) {
struct strbuf converted = STRBUF_INIT;
- convert_object_file(&converted, algo, compat,
+ convert_object_file(the_repository,
+ &converted, algo, compat,
buf, len, compat_type, 0);
hash_object_file(compat, converted.buf, converted.len,
compat_type, &compat_oid);
@@ -2652,7 +2405,7 @@ static int index_mem(struct index_state *istate,
opts.strict = 1;
opts.error_func = hash_format_check_report;
- if (fsck_buffer(null_oid(), type, buf, size, &opts))
+ if (fsck_buffer(null_oid(the_hash_algo), type, buf, size, &opts))
die(_("refusing to create malformed object"));
fsck_finish(&opts);
}
@@ -2677,7 +2430,7 @@ static int index_stream_convert_blob(struct index_state *istate,
struct strbuf sbuf = STRBUF_INIT;
assert(path);
- assert(would_convert_to_git_filter_fd(istate, path));
+ ASSERT(would_convert_to_git_filter_fd(istate, path));
convert_to_git_filter_fd(istate, path, fd, &sbuf,
get_conv_flags(flags));
@@ -2774,7 +2527,8 @@ int index_fd(struct index_state *istate, struct object_id *oid,
ret = index_stream_convert_blob(istate, oid, fd, path, flags);
else if (!S_ISREG(st->st_mode))
ret = index_pipe(istate, oid, fd, type, path, flags);
- else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
+ else if (st->st_size <= repo_settings_get_big_file_threshold(the_repository) ||
+ type != OBJ_BLOB ||
(path && would_convert_to_git(istate, path)))
ret = index_core(istate, oid, fd, xsize_t(st->st_size),
type, path, flags);
@@ -3021,14 +2775,14 @@ static int check_stream_oid(git_zstream *stream,
const char *path,
const struct object_id *expected_oid)
{
- git_hash_ctx c;
+ struct git_hash_ctx c;
struct object_id real_oid;
unsigned char buf[4096];
unsigned long total_read;
int status = Z_OK;
the_hash_algo->init_fn(&c);
- the_hash_algo->update_fn(&c, hdr, stream->total_out);
+ git_hash_update(&c, hdr, stream->total_out);
/*
* We already read some bytes into hdr, but the ones up to the NUL
@@ -3048,10 +2802,9 @@ static int check_stream_oid(git_zstream *stream,
if (size - total_read < stream->avail_out)
stream->avail_out = size - total_read;
status = git_inflate(stream, Z_FINISH);
- the_hash_algo->update_fn(&c, buf, stream->next_out - buf);
+ git_hash_update(&c, buf, stream->next_out - buf);
total_read += stream->next_out - buf;
}
- git_inflate_end(stream);
if (status != Z_STREAM_END) {
error(_("corrupt loose object '%s'"), oid_to_hex(expected_oid));
@@ -3063,7 +2816,7 @@ static int check_stream_oid(git_zstream *stream,
return -1;
}
- the_hash_algo->final_oid_fn(&real_oid, &c);
+ git_hash_final_oid(&real_oid, &c);
if (!oideq(expected_oid, &real_oid)) {
error(_("hash mismatch for %s (expected %s)"), path,
oid_to_hex(expected_oid));
@@ -3098,35 +2851,35 @@ int read_loose_object(const char *path,
if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr),
NULL) != ULHR_OK) {
error(_("unable to unpack header of %s"), path);
- git_inflate_end(&stream);
- goto out;
+ goto out_inflate;
}
if (parse_loose_header(hdr, oi) < 0) {
error(_("unable to parse header of %s"), path);
- git_inflate_end(&stream);
- goto out;
+ goto out_inflate;
}
- if (*oi->typep == OBJ_BLOB && *size > big_file_threshold) {
+ if (*oi->typep == OBJ_BLOB &&
+ *size > repo_settings_get_big_file_threshold(the_repository)) {
if (check_stream_oid(&stream, hdr, *size, path, expected_oid) < 0)
- goto out;
+ goto out_inflate;
} else {
*contents = unpack_loose_rest(&stream, hdr, *size, expected_oid);
if (!*contents) {
error(_("unable to unpack contents of %s"), path);
- git_inflate_end(&stream);
- goto out;
+ goto out_inflate;
}
hash_object_file_literally(the_repository->hash_algo,
*contents, *size,
oi->type_name->buf, real_oid);
if (!oideq(expected_oid, real_oid))
- goto out;
+ goto out_inflate;
}
ret = 0; /* everything checks out */
+out_inflate:
+ git_inflate_end(&stream);
out:
if (map)
munmap(map, mapsize);