Commit fb2198db by Edward Thomson

futils_filesize: use `uint64_t` for object size

Instead of using a signed type (`off_t`) use `uint64_t` for the maximum
size of files.
parent 4334b177
...@@ -330,8 +330,10 @@ static int diff_file_content_load_workdir_file( ...@@ -330,8 +330,10 @@ static int diff_file_content_load_workdir_file(
if (fd < 0) if (fd < 0)
return fd; return fd;
if (!fc->file->size && if (!fc->file->size)
!(fc->file->size = git_futils_filesize(fd))) error = git_futils_filesize(&fc->file->size, fd);
if (error < 0 || !fc->file->size)
goto cleanup; goto cleanup;
if ((diff_opts->flags & GIT_DIFF_SHOW_BINARY) == 0 && if ((diff_opts->flags & GIT_DIFF_SHOW_BINARY) == 0 &&
......
...@@ -112,7 +112,7 @@ int git_futils_truncate(const char *path, int mode) ...@@ -112,7 +112,7 @@ int git_futils_truncate(const char *path, int mode)
return 0; return 0;
} }
git_off_t git_futils_filesize(git_file fd) int git_futils_filesize(uint64_t *out, git_file fd)
{ {
struct stat sb; struct stat sb;
...@@ -121,7 +121,13 @@ git_off_t git_futils_filesize(git_file fd) ...@@ -121,7 +121,13 @@ git_off_t git_futils_filesize(git_file fd)
return -1; return -1;
} }
return sb.st_size; if (sb.st_size < 0) {
git_error_set(GIT_ERROR_INVALID, "invalid file size");
return -1;
}
*out = sb.st_size;
return 0;
} }
mode_t git_futils_canonical_mode(mode_t raw_mode) mode_t git_futils_canonical_mode(mode_t raw_mode)
...@@ -309,16 +315,14 @@ int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len) ...@@ -309,16 +315,14 @@ int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len)
int git_futils_mmap_ro_file(git_map *out, const char *path) int git_futils_mmap_ro_file(git_map *out, const char *path)
{ {
git_file fd = git_futils_open_ro(path); git_file fd = git_futils_open_ro(path);
git_off_t len; uint64_t len;
int result; int result;
if (fd < 0) if (fd < 0)
return fd; return fd;
if ((len = git_futils_filesize(fd)) < 0) { if ((result = git_futils_filesize(&len, fd)) < 0)
result = -1;
goto out; goto out;
}
if (!git__is_sizet(len)) { if (!git__is_sizet(len)) {
git_error_set(GIT_ERROR_OS, "file `%s` too large to mmap", path); git_error_set(GIT_ERROR_OS, "file `%s` too large to mmap", path);
......
...@@ -255,7 +255,7 @@ extern int git_futils_truncate(const char *path, int mode); ...@@ -255,7 +255,7 @@ extern int git_futils_truncate(const char *path, int mode);
/** /**
* Get the filesize in bytes of a file * Get the filesize in bytes of a file
*/ */
extern git_off_t git_futils_filesize(git_file fd); extern int git_futils_filesize(uint64_t *out, git_file fd);
#define GIT_PERMS_IS_EXEC(MODE) (((MODE) & 0111) != 0) #define GIT_PERMS_IS_EXEC(MODE) (((MODE) & 0111) != 0)
#define GIT_PERMS_CANONICAL(MODE) (GIT_PERMS_IS_EXEC(MODE) ? 0755 : 0644) #define GIT_PERMS_CANONICAL(MODE) (GIT_PERMS_IS_EXEC(MODE) ? 0755 : 0644)
......
...@@ -320,20 +320,26 @@ int git_odb__hashlink(git_oid *out, const char *path) ...@@ -320,20 +320,26 @@ int git_odb__hashlink(git_oid *out, const char *path)
int git_odb_hashfile(git_oid *out, const char *path, git_object_t type) int git_odb_hashfile(git_oid *out, const char *path, git_object_t type)
{ {
git_object_size_t size; uint64_t size;
int result, fd = git_futils_open_ro(path); int fd, error = 0;
if (fd < 0)
if ((fd = git_futils_open_ro(path)) < 0)
return fd; return fd;
if ((size = git_futils_filesize(fd)) < 0 || !git__is_sizet(size)) { if ((error = git_futils_filesize(&size, fd)) < 0)
goto done;
if (!git__is_sizet(size)) {
git_error_set(GIT_ERROR_OS, "file size overflow for 32-bit systems"); git_error_set(GIT_ERROR_OS, "file size overflow for 32-bit systems");
p_close(fd); error = -1;
return -1; goto done;
} }
result = git_odb__hashfd(out, fd, (size_t)size, type); error = git_odb__hashfd(out, fd, (size_t)size, type);
done:
p_close(fd); p_close(fd);
return result; return error;
} }
int git_odb_hash(git_oid *id, const void *data, size_t len, git_object_t type) int git_odb_hash(git_oid *id, const void *data, size_t len, git_object_t type)
......
...@@ -2545,7 +2545,7 @@ int git_repository_hashfile( ...@@ -2545,7 +2545,7 @@ int git_repository_hashfile(
int error; int error;
git_filter_list *fl = NULL; git_filter_list *fl = NULL;
git_file fd = -1; git_file fd = -1;
git_off_t len; uint64_t len;
git_buf full_path = GIT_BUF_INIT; git_buf full_path = GIT_BUF_INIT;
assert(out && path && repo); /* as_path can be NULL */ assert(out && path && repo); /* as_path can be NULL */
...@@ -2582,11 +2582,8 @@ int git_repository_hashfile( ...@@ -2582,11 +2582,8 @@ int git_repository_hashfile(
goto cleanup; goto cleanup;
} }
len = git_futils_filesize(fd); if ((error = git_futils_filesize(&len, fd)) < 0)
if (len < 0) {
error = (int)len;
goto cleanup; goto cleanup;
}
if (!git__is_sizet(len)) { if (!git__is_sizet(len)) {
git_error_set(GIT_ERROR_OS, "file size overflow for 32-bit systems"); git_error_set(GIT_ERROR_OS, "file size overflow for 32-bit systems");
......
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