commit 4c9b88110ffd0deb6eed9861f320246d8354bae2 from: Stefan Sperling date: Tue Mar 19 13:46:32 2024 UTC introduce got_opentemp_truncatefd() commit - 30a624fb1ef8d2d9706a604cbf65dcdacf072e72 commit + 4c9b88110ffd0deb6eed9861f320246d8354bae2 blob - 8c49224c3f3618922f155e0671044215eba5d031 blob + f593c2654e3b83e7aaf1b5fa2a6a2f3311675648 --- include/got_opentemp.h +++ include/got_opentemp.h @@ -42,3 +42,6 @@ const struct got_error *got_opentemp_named_fd(char **, /* Truncate a file. This is useful for re-using open temporary files. */ const struct got_error *got_opentemp_truncate(FILE *); + +/* Truncate a file via a file descriptor. */ +const struct got_error *got_opentemp_truncatefd(int); blob - af76736e74e8d84085430ebf22b10d5375f8d887 blob + a0d0c2213dfee3f56549ae2f92dc2415d3d9744d --- lib/opentemp.c +++ lib/opentemp.c @@ -132,3 +132,13 @@ got_opentemp_truncate(FILE *f) return got_error_from_errno("fseeko"); return NULL; } + +const struct got_error * +got_opentemp_truncatefd(int fd) +{ + if (ftruncate(fd, 0L) == -1) + return got_error_from_errno("ftruncate"); + if (lseek(fd, 0L, SEEK_SET) == -1) + return got_error_from_errno("lseek"); + return NULL; +}