From 2dbdb824f0cfbf5f583e6b94aec753f276ed3368 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Mon, 3 Nov 2008 18:38:57 -0800 Subject: [PATCH] Add git_fsize to the os file API This permits us to get the size of an opened file. Signed-off-by: Shawn O. Pearce --- include/git/os/unix.h | 14 ++++++++++++++ src/os/unix.c | 9 ++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/git/os/unix.h b/include/git/os/unix.h index 56f77b95f..475fe13a5 100644 --- a/include/git/os/unix.h +++ b/include/git/os/unix.h @@ -26,11 +26,16 @@ #ifndef INCLUDE_git_os_abstraction_h__ #define INCLUDE_git_os_abstraction_h__ +/** Force 64 bit off_t size on POSIX. */ +#define _FILE_OFFSET_BITS 64 + +#include #include #include #include #include #include +#include #include /** @@ -95,6 +100,15 @@ GIT_EXTERN(int) git_fread(git_file fd, void *buf, size_t cnt); */ GIT_EXTERN(int) git_fwrite(git_file fd, void *buf, size_t cnt); +/** + * Get the current size of an open file. + * @param fd open descriptor. + * @return + * - On success, >= 0, indicating the file size in bytes. + * - On error, <0. + */ +GIT_EXTERN(off_t) git_fsize(git_file fd); + /** * Close an open file descriptor. * @param fd descriptor to close. diff --git a/src/os/unix.c b/src/os/unix.c index 767f20ca8..8a2088414 100644 --- a/src/os/unix.c +++ b/src/os/unix.c @@ -23,7 +23,6 @@ * Boston, MA 02110-1301, USA. */ -#include #include "git/common.h" int git_fopen(git_file *out, const char *path, int flags) @@ -74,3 +73,11 @@ int git_fwrite(git_file fd, void *buf, size_t cnt) } return GIT_SUCCESS; } + +off_t git_fsize(git_file fd) +{ + struct stat sb; + if (fstat(fd, &sb)) + return -1; + return sb.st_size; +}