Commit 89f6d84c by Vicent Martí

Merge pull request #1781 from brodie/brodie/stat-before-open

fileops: stat() before open()ing in git_futils_readbuffer_updated()
parents d5904eb8 9ccdb211
...@@ -147,6 +147,7 @@ int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len) ...@@ -147,6 +147,7 @@ int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
int git_futils_readbuffer_updated( int git_futils_readbuffer_updated(
git_buf *buf, const char *path, time_t *mtime, size_t *size, int *updated) git_buf *buf, const char *path, time_t *mtime, size_t *size, int *updated)
{ {
int error = 0;
git_file fd; git_file fd;
struct stat st; struct stat st;
bool changed = false; bool changed = false;
...@@ -156,11 +157,15 @@ int git_futils_readbuffer_updated( ...@@ -156,11 +157,15 @@ int git_futils_readbuffer_updated(
if (updated != NULL) if (updated != NULL)
*updated = 0; *updated = 0;
if ((fd = git_futils_open_ro(path)) < 0) if (p_stat(path, &st) < 0) {
return fd; error = errno;
giterr_set(GITERR_OS, "Failed to stat '%s'", path);
if (error == ENOENT || error == ENOTDIR)
return GIT_ENOTFOUND;
return -1;
}
if (p_fstat(fd, &st) < 0 || S_ISDIR(st.st_mode) || !git__is_sizet(st.st_size+1)) { if (S_ISDIR(st.st_mode) || !git__is_sizet(st.st_size+1)) {
p_close(fd);
giterr_set(GITERR_OS, "Invalid regular file stat for '%s'", path); giterr_set(GITERR_OS, "Invalid regular file stat for '%s'", path);
return -1; return -1;
} }
...@@ -177,7 +182,6 @@ int git_futils_readbuffer_updated( ...@@ -177,7 +182,6 @@ int git_futils_readbuffer_updated(
changed = true; changed = true;
if (!changed) { if (!changed) {
p_close(fd);
return 0; return 0;
} }
...@@ -186,6 +190,9 @@ int git_futils_readbuffer_updated( ...@@ -186,6 +190,9 @@ int git_futils_readbuffer_updated(
if (size != NULL) if (size != NULL)
*size = (size_t)st.st_size; *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); p_close(fd);
return -1; return -1;
......
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