summaryrefslogtreecommitdiff
path: root/src/fileops.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fileops.c')
-rw-r--r--src/fileops.c57
1 files changed, 32 insertions, 25 deletions
diff --git a/src/fileops.c b/src/fileops.c
index 57d2ce9c3..cfc22bb53 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -153,13 +153,15 @@ int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
}
int git_futils_readbuffer_updated(
- git_buf *buf, const char *path, time_t *mtime, size_t *size, int *updated)
+ git_buf *out, const char *path, git_oid *checksum, int *updated)
{
+ int error;
git_file fd;
struct stat st;
- bool changed = false;
+ git_buf buf = GIT_BUF_INIT;
+ git_oid checksum_new;
- assert(buf && path && *path);
+ assert(out && path && *path);
if (updated != NULL)
*updated = 0;
@@ -178,45 +180,50 @@ int git_futils_readbuffer_updated(
return -1;
}
- /*
- * If we were given a time and/or a size, we only want to read the file
- * if it has been modified.
- */
- if (size && *size != (size_t)st.st_size)
- changed = true;
- if (mtime && *mtime != (time_t)st.st_mtime)
- changed = true;
- if (!size && !mtime)
- changed = true;
-
- if (!changed) {
- return 0;
- }
-
- if (mtime != NULL)
- *mtime = st.st_mtime;
- if (size != NULL)
- *size = (size_t)st.st_size;
-
if ((fd = git_futils_open_ro(path)) < 0)
return fd;
- if (git_futils_readbuffer_fd(buf, fd, (size_t)st.st_size) < 0) {
+ if (git_futils_readbuffer_fd(&buf, fd, (size_t)st.st_size) < 0) {
p_close(fd);
return -1;
}
p_close(fd);
+ if ((error = git_hash_buf(&checksum_new, buf.ptr, buf.size)) < 0) {
+ git_buf_free(&buf);
+ return error;
+ }
+
+ /*
+ * If we were given a checksum, we only want to use it if it's different
+ */
+ if (checksum && !git_oid__cmp(checksum, &checksum_new)) {
+ git_buf_free(&buf);
+ if (updated)
+ *updated = 0;
+
+ return 0;
+ }
+
+ /*
+ * If we're here, the file did change, or the user didn't have an old version
+ */
+ if (checksum)
+ git_oid_cpy(checksum, &checksum_new);
+
if (updated != NULL)
*updated = 1;
+ git_buf_swap(out, &buf);
+ git_buf_free(&buf);
+
return 0;
}
int git_futils_readbuffer(git_buf *buf, const char *path)
{
- return git_futils_readbuffer_updated(buf, path, NULL, NULL, NULL);
+ return git_futils_readbuffer_updated(buf, path, NULL, NULL);
}
int git_futils_writebuffer(