summaryrefslogtreecommitdiff
path: root/csum-file.c
diff options
context:
space:
mode:
Diffstat (limited to 'csum-file.c')
-rw-r--r--csum-file.c71
1 files changed, 43 insertions, 28 deletions
diff --git a/csum-file.c b/csum-file.c
index c203ebf11b..6e21e3cac8 100644
--- a/csum-file.c
+++ b/csum-file.c
@@ -8,12 +8,11 @@
* able to verify hasn't been messed with afterwards.
*/
-#define USE_THE_REPOSITORY_VARIABLE
-
#include "git-compat-util.h"
-#include "progress.h"
#include "csum-file.h"
+#include "git-zlib.h"
#include "hash.h"
+#include "progress.h"
static void verify_buffer_or_die(struct hashfile *f,
const void *buf,
@@ -23,7 +22,7 @@ static void verify_buffer_or_die(struct hashfile *f,
if (ret < 0)
die_errno("%s: sha1 file read error", f->name);
- if (ret != count)
+ if ((size_t)ret != count)
die("%s: sha1 file truncated", f->name);
if (memcmp(buf, f->check_buffer, count))
die("sha1 file '%s' validation error", f->name);
@@ -50,7 +49,7 @@ void hashflush(struct hashfile *f)
if (offset) {
if (!f->skip_hash)
- the_hash_algo->unsafe_update_fn(&f->ctx, f->buffer, offset);
+ git_hash_update(&f->ctx, f->buffer, offset);
flush(f, f->buffer, offset);
f->offset = 0;
}
@@ -71,14 +70,14 @@ int finalize_hashfile(struct hashfile *f, unsigned char *result,
hashflush(f);
if (f->skip_hash)
- hashclr(f->buffer, the_repository->hash_algo);
+ hashclr(f->buffer, f->algop);
else
- the_hash_algo->unsafe_final_fn(f->buffer, &f->ctx);
+ git_hash_final(f->buffer, &f->ctx);
if (result)
- hashcpy(result, f->buffer, the_repository->hash_algo);
+ hashcpy(result, f->buffer, f->algop);
if (flags & CSUM_HASH_IN_STREAM)
- flush(f, f->buffer, the_hash_algo->rawsz);
+ flush(f, f->buffer, f->algop->rawsz);
if (flags & CSUM_FSYNC)
fsync_component_or_die(component, f->fd, f->name);
if (flags & CSUM_CLOSE) {
@@ -128,7 +127,7 @@ void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
* f->offset is necessarily zero.
*/
if (!f->skip_hash)
- the_hash_algo->unsafe_update_fn(&f->ctx, buf, nr);
+ git_hash_update(&f->ctx, buf, nr);
flush(f, buf, nr);
} else {
/*
@@ -147,21 +146,23 @@ void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
}
}
-struct hashfile *hashfd_check(const char *name)
+struct hashfile *hashfd_check(const struct git_hash_algo *algop,
+ const char *name)
{
int sink, check;
struct hashfile *f;
sink = xopen("/dev/null", O_WRONLY);
check = xopen(name, O_RDONLY);
- f = hashfd(sink, name);
+ f = hashfd(algop, sink, name);
f->check_fd = check;
f->check_buffer = xmalloc(f->buffer_len);
return f;
}
-static struct hashfile *hashfd_internal(int fd, const char *name,
+static struct hashfile *hashfd_internal(const struct git_hash_algo *algop,
+ int fd, const char *name,
struct progress *tp,
size_t buffer_len)
{
@@ -174,7 +175,9 @@ static struct hashfile *hashfd_internal(int fd, const char *name,
f->name = name;
f->do_crc = 0;
f->skip_hash = 0;
- the_hash_algo->unsafe_init_fn(&f->ctx);
+
+ f->algop = unsafe_hash_algo(algop);
+ f->algop->init_fn(&f->ctx);
f->buffer_len = buffer_len;
f->buffer = xmalloc(buffer_len);
@@ -183,17 +186,19 @@ static struct hashfile *hashfd_internal(int fd, const char *name,
return f;
}
-struct hashfile *hashfd(int fd, const char *name)
+struct hashfile *hashfd(const struct git_hash_algo *algop,
+ int fd, const char *name)
{
/*
* Since we are not going to use a progress meter to
* measure the rate of data passing through this hashfile,
* use a larger buffer size to reduce fsync() calls.
*/
- return hashfd_internal(fd, name, NULL, 128 * 1024);
+ return hashfd_internal(algop, fd, name, NULL, 128 * 1024);
}
-struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp)
+struct hashfile *hashfd_throughput(const struct git_hash_algo *algop,
+ int fd, const char *name, struct progress *tp)
{
/*
* Since we are expecting to report progress of the
@@ -201,14 +206,21 @@ struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp
* size so the progress indicators arrive at a more
* frequent rate.
*/
- return hashfd_internal(fd, name, tp, 8 * 1024);
+ return hashfd_internal(algop, fd, name, tp, 8 * 1024);
+}
+
+void hashfile_checkpoint_init(struct hashfile *f,
+ struct hashfile_checkpoint *checkpoint)
+{
+ memset(checkpoint, 0, sizeof(*checkpoint));
+ f->algop->init_fn(&checkpoint->ctx);
}
void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
{
hashflush(f);
checkpoint->offset = f->total;
- the_hash_algo->unsafe_clone_fn(&checkpoint->ctx, &f->ctx);
+ git_hash_clone(&checkpoint->ctx, &f->ctx);
}
int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
@@ -219,7 +231,7 @@ int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint
lseek(f->fd, offset, SEEK_SET) != offset)
return -1;
f->total = offset;
- the_hash_algo->unsafe_clone_fn(&f->ctx, &checkpoint->ctx);
+ git_hash_clone(&f->ctx, &checkpoint->ctx);
f->offset = 0; /* hashflush() was called in checkpoint */
return 0;
}
@@ -236,18 +248,21 @@ uint32_t crc32_end(struct hashfile *f)
return f->crc32;
}
-int hashfile_checksum_valid(const unsigned char *data, size_t total_len)
+int hashfile_checksum_valid(const struct git_hash_algo *algop,
+ const unsigned char *data, size_t total_len)
{
unsigned char got[GIT_MAX_RAWSZ];
- git_hash_ctx ctx;
- size_t data_len = total_len - the_hash_algo->rawsz;
+ struct git_hash_ctx ctx;
+ size_t data_len = total_len - algop->rawsz;
+
+ algop = unsafe_hash_algo(algop);
- if (total_len < the_hash_algo->rawsz)
+ if (total_len < algop->rawsz)
return 0; /* say "too short"? */
- the_hash_algo->unsafe_init_fn(&ctx);
- the_hash_algo->unsafe_update_fn(&ctx, data, data_len);
- the_hash_algo->unsafe_final_fn(got, &ctx);
+ algop->init_fn(&ctx);
+ git_hash_update(&ctx, data, data_len);
+ git_hash_final(got, &ctx);
- return hasheq(got, data + data_len, the_repository->hash_algo);
+ return hasheq(got, data + data_len, algop);
}