aboutsummaryrefslogtreecommitdiff
path: root/src/backend/storage/file/fd.c
diff options
context:
space:
mode:
authorThomas Munro <tmunro@postgresql.org>2020-12-01 15:34:57 +1300
committerThomas Munro <tmunro@postgresql.org>2020-12-01 15:42:22 +1300
commit57faaf376e1961fa48866c6e5d6926463c6671b1 (patch)
treeb7df65cb3a351507d0c4ea1ba2099c214f4fa37a /src/backend/storage/file/fd.c
parent9f35f943732eaf9121a107f54ea043925e9a5d7b (diff)
downloadpostgresql-57faaf376e1961fa48866c6e5d6926463c6671b1.tar.gz
postgresql-57faaf376e1961fa48866c6e5d6926463c6671b1.zip
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
Diffstat (limited to 'src/backend/storage/file/fd.c')
-rw-r--r--src/backend/storage/file/fd.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index 05abcf72d68..88004c6fae8 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -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