Commit c616ba2d by Edward Thomson

filebuf: configurable hash type

`git_filebuf` hashes contents as its written; allow for SHA1 or SHA256
as that hash. Currently, most callers simply use SHA1 as they do not yet
know about SHA256 themselves.
parent 51f3e0a4
...@@ -1116,7 +1116,12 @@ static int write_on_eof( ...@@ -1116,7 +1116,12 @@ static int write_on_eof(
/* /*
* This is pretty much the parsing, except we write out anything we don't have * This is pretty much the parsing, except we write out anything we don't have
*/ */
static int config_file_write(config_file_backend *cfg, const char *orig_key, const char *key, const git_regexp *preg, const char *value) static int config_file_write(
config_file_backend *cfg,
const char *orig_key,
const char *key,
const git_regexp *preg,
const char *value)
{ {
char *orig_section = NULL, *section = NULL, *orig_name, *name, *ldot; char *orig_section = NULL, *section = NULL, *orig_name, *name, *ldot;
...@@ -1124,15 +1129,18 @@ static int config_file_write(config_file_backend *cfg, const char *orig_key, con ...@@ -1124,15 +1129,18 @@ static int config_file_write(config_file_backend *cfg, const char *orig_key, con
git_config_parser parser = GIT_CONFIG_PARSER_INIT; git_config_parser parser = GIT_CONFIG_PARSER_INIT;
git_filebuf file = GIT_FILEBUF_INIT; git_filebuf file = GIT_FILEBUF_INIT;
struct write_data write_data; struct write_data write_data;
int error; int filebuf_hash, error;
filebuf_hash = git_filebuf_hash_flags(git_oid_algorithm(GIT_OID_SHA1));
GIT_ASSERT(filebuf_hash);
memset(&write_data, 0, sizeof(write_data)); memset(&write_data, 0, sizeof(write_data));
if (cfg->locked) { if (cfg->locked) {
error = git_str_puts(&contents, git_str_cstr(&cfg->locked_content) == NULL ? "" : git_str_cstr(&cfg->locked_content)); error = git_str_puts(&contents, git_str_cstr(&cfg->locked_content) == NULL ? "" : git_str_cstr(&cfg->locked_content));
} else { } else {
if ((error = git_filebuf_open(&file, cfg->file.path, GIT_FILEBUF_HASH_CONTENTS, if ((error = git_filebuf_open(&file, cfg->file.path,
GIT_CONFIG_FILE_MODE)) < 0) filebuf_hash, GIT_CONFIG_FILE_MODE)) < 0)
goto done; goto done;
/* We need to read in our own config file */ /* We need to read in our own config file */
......
...@@ -3668,19 +3668,23 @@ int git_indexwriter_init( ...@@ -3668,19 +3668,23 @@ int git_indexwriter_init(
git_indexwriter *writer, git_indexwriter *writer,
git_index *index) git_index *index)
{ {
int error; int filebuf_hash, error;
GIT_REFCOUNT_INC(index); GIT_REFCOUNT_INC(index);
writer->index = index; writer->index = index;
filebuf_hash = git_filebuf_hash_flags(git_oid_algorithm(GIT_OID_SHA1));
GIT_ASSERT(filebuf_hash);
if (!index->index_file_path) if (!index->index_file_path)
return create_index_error(-1, return create_index_error(-1,
"failed to write index: The index is in-memory only"); "failed to write index: The index is in-memory only");
if ((error = git_filebuf_open( if ((error = git_filebuf_open(&writer->file,
&writer->file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS, GIT_INDEX_FILE_MODE)) < 0) { index->index_file_path,
git_filebuf_hash_flags(filebuf_hash),
GIT_INDEX_FILE_MODE)) < 0) {
if (error == GIT_ELOCKED) if (error == GIT_ELOCKED)
git_error_set(GIT_ERROR_INDEX, "the index is locked; this might be due to a concurrent or crashed process"); git_error_set(GIT_ERROR_INDEX, "the index is locked; this might be due to a concurrent or crashed process");
......
...@@ -1232,6 +1232,7 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats) ...@@ -1232,6 +1232,7 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats)
git_filebuf index_file = {0}; git_filebuf index_file = {0};
void *packfile_trailer; void *packfile_trailer;
size_t checksum_size; size_t checksum_size;
int filebuf_hash;
bool mismatch; bool mismatch;
if (!idx->parsed_header) { if (!idx->parsed_header) {
...@@ -1240,6 +1241,7 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats) ...@@ -1240,6 +1241,7 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats)
} }
checksum_size = git_hash_size(indexer_hash_algorithm(idx)); checksum_size = git_hash_size(indexer_hash_algorithm(idx));
filebuf_hash = git_filebuf_hash_flags(indexer_hash_algorithm(idx));
GIT_ASSERT(checksum_size); GIT_ASSERT(checksum_size);
/* Test for this before resolve_deltas(), as it plays with idx->off */ /* Test for this before resolve_deltas(), as it plays with idx->off */
...@@ -1314,8 +1316,7 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats) ...@@ -1314,8 +1316,7 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats)
return -1; return -1;
if (git_filebuf_open(&index_file, filename.ptr, if (git_filebuf_open(&index_file, filename.ptr,
GIT_FILEBUF_HASH_CONTENTS | filebuf_hash | (idx->do_fsync ? GIT_FILEBUF_FSYNC : 0),
(idx->do_fsync ? GIT_FILEBUF_FSYNC : 0),
idx->mode) < 0) idx->mode) < 0)
goto on_error; goto on_error;
......
...@@ -302,11 +302,16 @@ int git_filebuf_open_withsize(git_filebuf *file, const char *path, int flags, mo ...@@ -302,11 +302,16 @@ int git_filebuf_open_withsize(git_filebuf *file, const char *path, int flags, mo
} }
/* If we are hashing on-write, allocate a new hash context */ /* If we are hashing on-write, allocate a new hash context */
if (flags & GIT_FILEBUF_HASH_CONTENTS) { if (flags & GIT_FILEBUF_HASH_SHA1) {
file->compute_digest = 1; file->compute_digest = 1;
if (git_hash_ctx_init(&file->digest, GIT_HASH_ALGORITHM_SHA1) < 0) if (git_hash_ctx_init(&file->digest, GIT_HASH_ALGORITHM_SHA1) < 0)
goto cleanup; goto cleanup;
} else if (flags & GIT_FILEBUF_HASH_SHA256) {
file->compute_digest = 1;
if (git_hash_ctx_init(&file->digest, GIT_HASH_ALGORITHM_SHA256) < 0)
goto cleanup;
} }
compression = flags >> GIT_FILEBUF_DEFLATE_SHIFT; compression = flags >> GIT_FILEBUF_DEFLATE_SHIFT;
......
...@@ -17,13 +17,14 @@ ...@@ -17,13 +17,14 @@
# define GIT_FILEBUF_THREADS # define GIT_FILEBUF_THREADS
#endif #endif
#define GIT_FILEBUF_HASH_CONTENTS (1 << 0) #define GIT_FILEBUF_HASH_SHA1 (1 << 0)
#define GIT_FILEBUF_APPEND (1 << 2) #define GIT_FILEBUF_HASH_SHA256 (1 << 1)
#define GIT_FILEBUF_APPEND (1 << 2)
#define GIT_FILEBUF_CREATE_LEADING_DIRS (1 << 3) #define GIT_FILEBUF_CREATE_LEADING_DIRS (1 << 3)
#define GIT_FILEBUF_TEMPORARY (1 << 4) #define GIT_FILEBUF_TEMPORARY (1 << 4)
#define GIT_FILEBUF_DO_NOT_BUFFER (1 << 5) #define GIT_FILEBUF_DO_NOT_BUFFER (1 << 5)
#define GIT_FILEBUF_FSYNC (1 << 6) #define GIT_FILEBUF_FSYNC (1 << 6)
#define GIT_FILEBUF_DEFLATE_SHIFT (7) #define GIT_FILEBUF_DEFLATE_SHIFT (7)
#define GIT_FILELOCK_EXTENSION ".lock\0" #define GIT_FILELOCK_EXTENSION ".lock\0"
#define GIT_FILELOCK_EXTLENGTH 6 #define GIT_FILELOCK_EXTLENGTH 6
...@@ -91,4 +92,16 @@ int git_filebuf_hash(unsigned char *out, git_filebuf *file); ...@@ -91,4 +92,16 @@ int git_filebuf_hash(unsigned char *out, git_filebuf *file);
int git_filebuf_flush(git_filebuf *file); int git_filebuf_flush(git_filebuf *file);
int git_filebuf_stats(time_t *mtime, size_t *size, git_filebuf *file); int git_filebuf_stats(time_t *mtime, size_t *size, git_filebuf *file);
GIT_INLINE(int) git_filebuf_hash_flags(git_hash_algorithm_t algorithm)
{
switch (algorithm) {
case GIT_HASH_ALGORITHM_SHA1:
return GIT_FILEBUF_HASH_SHA1;
case GIT_HASH_ALGORITHM_SHA256:
return GIT_FILEBUF_HASH_SHA256;
default:
return 0;
}
}
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment