From 18875046669cc49b3af1bce137869e6a8f4a7bb4 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 8 Jul 2024 10:11:37 -0400 Subject: [PATCH] wayland: Fall back to ftruncate We are seeing posix_fallocate fail with ENOENT occasionally. This shouldn't happen according to the docs, but it does. Fall back to ftruncate if it does. It gives us less guarantees, but it makes the ci not fail so much. --- gdk/wayland/cursor/os-compatibility.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/gdk/wayland/cursor/os-compatibility.c b/gdk/wayland/cursor/os-compatibility.c index a10d070b9b..0e2cd19798 100644 --- a/gdk/wayland/cursor/os-compatibility.c +++ b/gdk/wayland/cursor/os-compatibility.c @@ -70,13 +70,10 @@ create_tmpfile_cloexec(char *tmpname) #ifdef HAVE_MKOSTEMP fd = mkostemp(tmpname, O_CLOEXEC); - if (fd >= 0) - unlink(tmpname); #else fd = mkstemp(tmpname); if (fd >= 0) { fd = set_cloexec_or_close(fd); - unlink(tmpname); } #endif @@ -116,7 +113,7 @@ os_create_anonymous_file(off_t size) { static const char template[] = "/wayland-cursor-shared-XXXXXX"; const char *path; - char *name; + char *name = NULL; int fd; int ret; @@ -140,7 +137,7 @@ os_create_anonymous_file(off_t size) return -1; } - name = malloc(strlen(path) + sizeof(template)); + name = alloca(strlen(path) + sizeof(template)); if (!name) return -1; @@ -151,29 +148,27 @@ os_create_anonymous_file(off_t size) if (fd < 0) { g_warning ("os_create_anonymous_file(): create_tmpfile_cloexec(\"%s\") failed", name); - free(name); return -1; } - - free(name); } #ifdef HAVE_POSIX_FALLOCATE ret = posix_fallocate(fd, 0, size); - if (ret != 0) { - g_warning ("os_create_anonymous_file(): posix_fallocate(%d, 0, %ld) failed: %s", fd, size, strerror (errno)); - close(fd); - errno = ret; - return -1; - } -#else + if (ret == 0) { + goto allocated; + } +#endif + ret = ftruncate(fd, size); if (ret < 0) { g_warning ("os_create_anonymous_file(): ftruncate() failed"); close(fd); return -1; } -#endif + +allocated: + if (fd >= 0 && name) + unlink (name); return fd; }