mirror of
https://git.proxmox.com/git/libgit2
synced 2025-08-12 03:28:09 +00:00
odb: Add internal git_odb__hashfd
This commit is contained in:
parent
9b8d56087c
commit
18e5b8547d
@ -345,7 +345,7 @@ int git_indexer_run(git_indexer *idx, git_indexer_stats *stats)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* FIXME: Parse the object instead of hashing it */
|
/* FIXME: Parse the object instead of hashing it */
|
||||||
error = git_odb__hash_obj(&oid, &obj);
|
error = git_odb__hashobj(&oid, &obj);
|
||||||
if (error < GIT_SUCCESS) {
|
if (error < GIT_SUCCESS) {
|
||||||
error = git__rethrow(error, "Failed to hash object");
|
error = git__rethrow(error, "Failed to hash object");
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
50
src/odb.c
50
src/odb.c
@ -33,13 +33,13 @@ static int format_object_header(char *hdr, size_t n, size_t obj_len, git_otype o
|
|||||||
const char *type_str = git_object_type2string(obj_type);
|
const char *type_str = git_object_type2string(obj_type);
|
||||||
int len = p_snprintf(hdr, n, "%s %"PRIuZ, type_str, obj_len);
|
int len = p_snprintf(hdr, n, "%s %"PRIuZ, type_str, obj_len);
|
||||||
|
|
||||||
if (len < 0 || ((size_t) len) >= n)
|
if (len < 0 || len >= (int)n)
|
||||||
return git__throw(GIT_ERROR, "Cannot format object header. Length is out of bounds");
|
return git__throw(GIT_ERROR, "Cannot format object header. Length is out of bounds");
|
||||||
|
|
||||||
return len+1;
|
return len+1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int git_odb__hash_obj(git_oid *id, git_rawobj *obj)
|
int git_odb__hashobj(git_oid *id, git_rawobj *obj)
|
||||||
{
|
{
|
||||||
git_buf_vec vec[2];
|
git_buf_vec vec[2];
|
||||||
char header[64];
|
char header[64];
|
||||||
@ -113,22 +113,13 @@ void git_odb_object_free(git_odb_object *object)
|
|||||||
git_cached_obj_decref((git_cached_obj *)object, &free_odb_object);
|
git_cached_obj_decref((git_cached_obj *)object, &free_odb_object);
|
||||||
}
|
}
|
||||||
|
|
||||||
int git_odb_hashfile(git_oid *out, const char *path, git_otype type)
|
int git_odb__hashfd(git_oid *out, git_file fd, size_t size, git_otype type)
|
||||||
{
|
{
|
||||||
int fd, hdr_len;
|
int hdr_len;
|
||||||
char hdr[64], buffer[2048];
|
char hdr[64], buffer[2048];
|
||||||
git_off_t size;
|
|
||||||
git_hash_ctx *ctx;
|
git_hash_ctx *ctx;
|
||||||
|
|
||||||
if ((fd = p_open(path, O_RDONLY)) < 0)
|
hdr_len = format_object_header(hdr, sizeof(hdr), size, type);
|
||||||
return git__throw(GIT_ENOTFOUND, "Could not open '%s'", path);
|
|
||||||
|
|
||||||
if ((size = git_futils_filesize(fd)) < 0 || !git__is_sizet(size)) {
|
|
||||||
p_close(fd);
|
|
||||||
return git__throw(GIT_EOSERR, "'%s' appears to be corrupted", path);
|
|
||||||
}
|
|
||||||
|
|
||||||
hdr_len = format_object_header(hdr, sizeof(hdr), (size_t)size, type);
|
|
||||||
if (hdr_len < 0)
|
if (hdr_len < 0)
|
||||||
return git__throw(GIT_ERROR, "Failed to format blob header. Length is out of bounds");
|
return git__throw(GIT_ERROR, "Failed to format blob header. Length is out of bounds");
|
||||||
|
|
||||||
@ -137,28 +128,43 @@ int git_odb_hashfile(git_oid *out, const char *path, git_otype type)
|
|||||||
git_hash_update(ctx, hdr, hdr_len);
|
git_hash_update(ctx, hdr, hdr_len);
|
||||||
|
|
||||||
while (size > 0) {
|
while (size > 0) {
|
||||||
ssize_t read_len;
|
ssize_t read_len = read(fd, buffer, sizeof(buffer));
|
||||||
|
|
||||||
read_len = read(fd, buffer, sizeof(buffer));
|
|
||||||
|
|
||||||
if (read_len < 0) {
|
if (read_len < 0) {
|
||||||
p_close(fd);
|
|
||||||
git_hash_free_ctx(ctx);
|
git_hash_free_ctx(ctx);
|
||||||
return git__throw(GIT_EOSERR, "Can't read full file '%s'", path);
|
return git__throw(GIT_EOSERR, "Error when reading file: %s", strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
git_hash_update(ctx, buffer, read_len);
|
git_hash_update(ctx, buffer, read_len);
|
||||||
size -= read_len;
|
size -= read_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
p_close(fd);
|
|
||||||
|
|
||||||
git_hash_final(out, ctx);
|
git_hash_final(out, ctx);
|
||||||
git_hash_free_ctx(ctx);
|
git_hash_free_ctx(ctx);
|
||||||
|
|
||||||
return GIT_SUCCESS;
|
return GIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int git_odb_hashfile(git_oid *out, const char *path, git_otype type)
|
||||||
|
{
|
||||||
|
int fd, error;
|
||||||
|
git_off_t size;
|
||||||
|
|
||||||
|
if ((fd = p_open(path, O_RDONLY)) < 0)
|
||||||
|
return git__throw(GIT_ENOTFOUND, "Could not open '%s'", path);
|
||||||
|
|
||||||
|
if ((size = git_futils_filesize(fd)) < 0 || !git__is_sizet(size)) {
|
||||||
|
p_close(fd);
|
||||||
|
return git__throw(GIT_EOSERR,
|
||||||
|
"File size overflow. The object is too big to fit in 32-bit mode");
|
||||||
|
}
|
||||||
|
|
||||||
|
error = git_odb__hashfd(out, fd, (size_t)size, type);
|
||||||
|
|
||||||
|
p_close(fd);
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
int git_odb_hash(git_oid *id, const void *data, size_t len, git_otype type)
|
int git_odb_hash(git_oid *id, const void *data, size_t len, git_otype type)
|
||||||
{
|
{
|
||||||
git_rawobj raw;
|
git_rawobj raw;
|
||||||
@ -169,7 +175,7 @@ int git_odb_hash(git_oid *id, const void *data, size_t len, git_otype type)
|
|||||||
raw.len = len;
|
raw.len = len;
|
||||||
raw.type = type;
|
raw.type = type;
|
||||||
|
|
||||||
return git_odb__hash_obj(id, &raw);
|
return git_odb__hashobj(id, &raw);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
#include "cache.h"
|
#include "cache.h"
|
||||||
|
#include "posix.h"
|
||||||
|
|
||||||
#define GIT_OBJECTS_DIR "objects/"
|
#define GIT_OBJECTS_DIR "objects/"
|
||||||
#define GIT_OBJECT_DIR_MODE 0777
|
#define GIT_OBJECT_DIR_MODE 0777
|
||||||
@ -38,6 +39,7 @@ struct git_odb {
|
|||||||
git_cache cache;
|
git_cache cache;
|
||||||
};
|
};
|
||||||
|
|
||||||
int git_odb__hash_obj(git_oid *id, git_rawobj *obj);
|
int git_odb__hashobj(git_oid *id, git_rawobj *obj);
|
||||||
|
int git_odb__hashfd(git_oid *out, git_file fd, size_t size, git_otype type);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user