Commit 3ccc1a4deb67212a3e3c69e91fb7a04975819b31

Carlos Martín Nieto 2017-11-19T09:46:02

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.

diff --git a/src/fileops.c b/src/fileops.c
index ad3f67e..58988c2 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 fd54412..57b9d17 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);