summaryrefslogtreecommitdiff
path: root/src/fileops.c
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2012-09-11 23:38:16 +0200
committerVicent Marti <tanoku@gmail.com>2012-09-11 23:38:16 +0200
commit412293dcc95369f0a42ae9a94f30fe7328a5491c (patch)
tree200d4bb5fa8cc3f8f037486e2128479048b8384e /src/fileops.c
parent5a409c44baf2c7c2bd36fb8e8c2d5b2ce5b1908b (diff)
parentc859184bb459d9801a394dc44f5b0b0e55453263 (diff)
downloadlibgit2-412293dcc95369f0a42ae9a94f30fe7328a5491c.tar.gz
Merge branch 'diff-crlf-filters' into development
Diffstat (limited to 'src/fileops.c')
-rw-r--r--src/fileops.c48
1 files changed, 26 insertions, 22 deletions
diff --git a/src/fileops.c b/src/fileops.c
index 95eacb5f1..8ccf063d5 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -115,10 +115,33 @@ mode_t git_futils_canonical_mode(mode_t raw_mode)
return 0;
}
-int git_futils_readbuffer_updated(git_buf *buf, const char *path, time_t *mtime, int *updated)
+int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
+{
+ ssize_t read_size;
+
+ git_buf_clear(buf);
+
+ if (git_buf_grow(buf, len + 1) < 0)
+ return -1;
+
+ /* p_read loops internally to read len bytes */
+ read_size = p_read(fd, buf->ptr, len);
+
+ if (read_size != (ssize_t)len) {
+ giterr_set(GITERR_OS, "Failed to read descriptor");
+ return -1;
+ }
+
+ buf->ptr[read_size] = '\0';
+ buf->size = read_size;
+
+ return 0;
+}
+
+int git_futils_readbuffer_updated(
+ git_buf *buf, const char *path, time_t *mtime, int *updated)
{
git_file fd;
- size_t len;
struct stat st;
assert(buf && path && *path);
@@ -147,30 +170,11 @@ int git_futils_readbuffer_updated(git_buf *buf, const char *path, time_t *mtime,
if (mtime != NULL)
*mtime = st.st_mtime;
- len = (size_t) st.st_size;
-
- git_buf_clear(buf);
-
- if (git_buf_grow(buf, len + 1) < 0) {
+ if (git_futils_readbuffer_fd(buf, fd, (size_t)st.st_size) < 0) {
p_close(fd);
return -1;
}
- buf->ptr[len] = '\0';
-
- while (len > 0) {
- ssize_t read_size = p_read(fd, buf->ptr, len);
-
- if (read_size < 0) {
- p_close(fd);
- giterr_set(GITERR_OS, "Failed to read descriptor for '%s'", path);
- return -1;
- }
-
- len -= read_size;
- buf->size += read_size;
- }
-
p_close(fd);
if (updated != NULL)