diff options
author | Carlos Martín Nieto <cmn@dwim.me> | 2017-11-19 09:46:02 +0100 |
---|---|---|
committer | Carlos Martín Nieto <cmn@dwim.me> | 2017-12-26 17:32:37 +0000 |
commit | 3ccc1a4deb67212a3e3c69e91fb7a04975819b31 (patch) | |
tree | 712bd7837514a629492466afb48bf72d9e709a2d | |
parent | c0bfda87bee12be2f5c72c6f9863823f813130e9 (diff) | |
download | libgit2-3ccc1a4deb67212a3e3c69e91fb7a04975819b31.tar.gz |
futils: add a function to truncate a file
We want to do this in order to get FETCH_HEAD to be empty when we start updating
it due to fetching from the remote.
-rw-r--r-- | src/fileops.c | 10 | ||||
-rw-r--r-- | src/fileops.h | 5 |
2 files changed, 15 insertions, 0 deletions
diff --git a/src/fileops.c b/src/fileops.c index ad3f67e2b..58988c2d2 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -102,6 +102,16 @@ int git_futils_open_ro(const char *path) return fd; } +int git_futils_truncate(const char *path, int mode) +{ + int fd = p_open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode); + if (fd < 0) + return git_path_set_error(errno, path, "open"); + + close(fd); + return 0; +} + git_off_t git_futils_filesize(git_file fd) { struct stat sb; diff --git a/src/fileops.h b/src/fileops.h index fd5441243..57b9d173e 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -248,6 +248,11 @@ extern int git_futils_cp_r( extern int git_futils_open_ro(const char *path); /** + * Truncate a file, creating it if it doesn't exist. + */ +extern int git_futils_truncate(const char *path, int mode); + +/** * Get the filesize in bytes of a file */ extern git_off_t git_futils_filesize(git_file fd); |