Skip to content

Commit 3ccc1a4

Browse files
committed
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.
1 parent c0bfda8 commit 3ccc1a4

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/fileops.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ int git_futils_open_ro(const char *path)
102102
return fd;
103103
}
104104

105+
int git_futils_truncate(const char *path, int mode)
106+
{
107+
int fd = p_open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode);
108+
if (fd < 0)
109+
return git_path_set_error(errno, path, "open");
110+
111+
close(fd);
112+
return 0;
113+
}
114+
105115
git_off_t git_futils_filesize(git_file fd)
106116
{
107117
struct stat sb;

src/fileops.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ extern int git_futils_cp_r(
247247
*/
248248
extern int git_futils_open_ro(const char *path);
249249

250+
/**
251+
* Truncate a file, creating it if it doesn't exist.
252+
*/
253+
extern int git_futils_truncate(const char *path, int mode);
254+
250255
/**
251256
* Get the filesize in bytes of a file
252257
*/

0 commit comments

Comments
 (0)