summaryrefslogtreecommitdiff
path: root/glnx-fdio.h
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2017-06-12 17:00:53 -0400
committerColin Walters <walters@verbum.org>2017-06-13 11:43:29 -0400
commit05abf2143f967cd85b1b1b777ee412a581979cf1 (patch)
treec556a50df731405cab5b1adb045a3dd38ca17477 /glnx-fdio.h
parentf5ba01cf65309164adb06067b39cf28c071e1ccb (diff)
downloadlibglnx-05abf2143f967cd85b1b1b777ee412a581979cf1.tar.gz
fdio: Add glnx_try_fallocate()
The glibc `posix_fallocate()` implementation has a bad fallback, and further we need to handle `EOPNOTSUPP` for musl. https://github.com/flatpak/flatpak/issues/802
Diffstat (limited to 'glnx-fdio.h')
-rw-r--r--glnx-fdio.h32
1 files changed, 32 insertions, 0 deletions
diff --git a/glnx-fdio.h b/glnx-fdio.h
index dda32d1..e52c823 100644
--- a/glnx-fdio.h
+++ b/glnx-fdio.h
@@ -167,6 +167,38 @@ int glnx_renameat2_exchange (int olddirfd, const char *oldpath,
int newdirfd, const char *newpath);
/**
+ * glnx_try_fallocate:
+ * @fd: File descriptor
+ * @size: Size
+ * @error: Error
+ *
+ * Wrapper for Linux fallocate(). Explicitly ignores a @size of zero.
+ * Also, will silently do nothing if the underlying filesystem doesn't
+ * support it. Use this instead of posix_fallocate(), since the glibc fallback
+ * is bad: https://sourceware.org/bugzilla/show_bug.cgi?id=18515
+ */
+static inline gboolean
+glnx_try_fallocate (int fd,
+ off_t offset,
+ off_t size,
+ GError **error)
+{
+ /* This is just nicer than throwing an error */
+ if (size == 0)
+ return TRUE;
+
+ if (fallocate (fd, 0, offset, size) < 0)
+ {
+ if (errno == ENOSYS || errno == EOPNOTSUPP)
+ ; /* Ignore */
+ else
+ return glnx_throw_errno_prefix (error, "fallocate");
+ }
+
+ return TRUE;
+}
+
+/**
* glnx_fstat:
* @fd: FD to stat
* @buf: (out caller-allocates): Return location for stat details