summaryrefslogtreecommitdiff
path: root/src/fileops.c
diff options
context:
space:
mode:
authorRamsay Jones <ramsay@ramsay1.demon.co.uk>2008-12-18 22:56:14 +0000
committerShawn O. Pearce <spearce@spearce.org>2008-12-19 07:21:20 -0800
commit75d584305598407be4c53d42c85eca3cd58c973c (patch)
tree150bce88353a5b322b1600a5989e126cf8288562 /src/fileops.c
parentdef425bf8568b9c1e20879bf5be6f9c52b7361c4 (diff)
downloadlibgit2-75d584305598407be4c53d42c85eca3cd58c973c.tar.gz
Add a file reading routine along with an io buffer type
In particular, the gitfo_read_file() routine can be used to slurp the complete file contents into an gitfo_buf structure. The buffer content will be allocated by malloc() and may be released by the gitfo_free_buf() routine. The io buffer type can be initialised on the stack with the GITFO_BUF_INIT macro. Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'src/fileops.c')
-rw-r--r--src/fileops.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/fileops.c b/src/fileops.c
index e5f6ef800..caa7d9e0c 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -49,6 +49,43 @@ off_t gitfo_size(git_file fd)
return sb.st_size;
}
+int gitfo_read_file(gitfo_buf *obj, const char *path)
+{
+ git_file fd;
+ off_t len;
+ void *buff;
+
+ assert(obj && path && *path);
+
+ if ((fd = gitfo_open(path, O_RDONLY)) < 0)
+ return GIT_ERROR; /* TODO: error handling */
+
+ if (((len = gitfo_size(fd)) < 0) || ((buff = malloc(len)) == NULL)) {
+ gitfo_close(fd);
+ return GIT_ERROR; /* TODO: error handling */
+ }
+
+ if (gitfo_read(fd, buff, len) < 0) {
+ gitfo_close(fd);
+ free(buff);
+ return GIT_ERROR; /* TODO: error handling */
+ }
+
+ gitfo_close(fd);
+
+ obj->data = buff;
+ obj->len = len;
+
+ return GIT_SUCCESS;
+}
+
+void gitfo_free_buf(gitfo_buf *obj)
+{
+ assert(obj);
+ free(obj->data);
+ obj->data = NULL;
+}
+
/* cached diskio */
struct gitfo_cache {
git_file fd;