1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-27 07:42:10 +03:00

Use truncate(2) where appropriate.

When truncating files by name, use truncate(2).  Windows hasn't got it,
so keep our previous coding based on ftruncate(2) as a fallback.

Discussion: https://postgr.es/m/16663-fe97ccf9932fc800%40postgresql.org
This commit is contained in:
Thomas Munro
2020-12-01 15:34:57 +13:00
parent 9f35f94373
commit 57faaf376e
3 changed files with 29 additions and 12 deletions

View File

@@ -622,6 +622,33 @@ pg_flush_data(int fd, off_t offset, off_t nbytes)
#endif
}
/*
* Truncate a file to a given length by name.
*/
int
pg_truncate(const char *path, off_t length)
{
#ifdef WIN32
int save_errno;
int ret;
int fd;
fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
if (fd >= 0)
{
ret = ftruncate(fd, 0);
save_errno = errno;
CloseTransientFile(fd);
errno = save_errno;
}
else
ret = -1;
return ret;
#else
return truncate(path, length);
#endif
}
/*
* fsync_fname -- fsync a file or directory, handling errors properly