interactive-wayland: fallback to ftruncate() if needed
authorEvgeniy Khramtsov <evgeniy@khramtsov.org>
Wed, 14 Apr 2021 09:56:07 +0000 (12:56 +0300)
committerRan Benita <ran@unusedvar.com>
Wed, 14 Apr 2021 10:44:57 +0000 (13:44 +0300)
Fallback to ftruncate() if the underlying filesystem does not
support posix_fallocate().

Idea by: Jan Beich <jbeich@FreeBSD.org>, Niclas Zeising <zeising@FreeBSD.org>
Inspired by: Wayland cursor/os-compatibility.c
[ran: small adjustments]
Signed-off-by: Ran Benita <ran@unusedvar.com>
tools/interactive-wayland.c

index 3ac7ad7..d23432d 100644 (file)
@@ -130,6 +130,27 @@ create_tmpfile_cloexec(char *tmpname)
 }
 #endif
 
+static int
+os_resize_anonymous_file(int fd, off_t size)
+{
+    int ret;
+#ifdef HAVE_POSIX_FALLOCATE
+    ret = posix_fallocate(fd, 0, size);
+    if (ret == 0)
+        return 0;
+    /*
+     * Filesystems that do support fallocate will return EINVAL
+     * or EOPNOTSUPP, fallback to ftruncate() then.
+     */
+    if (ret != EINVAL && ret != EOPNOTSUPP)
+        return ret;
+#endif
+    ret = ftruncate(fd, size);
+    if (ret != 0)
+        return errno;
+    return 0;
+}
+
 /*
  * Create a new, unique, anonymous file of the given size, and
  * return the file descriptor for it. The file descriptor is set
@@ -148,8 +169,8 @@ create_tmpfile_cloexec(char *tmpname)
  * If the C library implements posix_fallocate(), it is used to
  * guarantee that disk space is available for the file at the
  * given size. If disk space is insufficent, errno is set to ENOSPC.
- * If posix_fallocate() is not supported, program may receive
- * SIGBUS on accessing mmap()'ed file contents instead.
+ * If posix_fallocate() is not supported, program will fallback
+ * to ftruncate() instead.
  */
 static int
 os_create_anonymous_file(off_t size)
@@ -180,20 +201,12 @@ os_create_anonymous_file(off_t size)
     if (fd < 0)
         return -1;
 
-#ifdef HAVE_POSIX_FALLOCATE
-    ret = posix_fallocate(fd, 0, size);
+    ret = os_resize_anonymous_file(fd, size);
     if (ret != 0) {
         close(fd);
         errno = ret;
         return -1;
     }
-#else
-    ret = ftruncate(fd, size);
-    if (ret < 0) {
-        close(fd);
-        return -1;
-    }
-#endif
 
     return fd;
 }