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.
This commit is contained in:
Matthias Clasen 2024-07-08 10:11:37 -04:00
parent 39913c2001
commit 1887504666

View File

@ -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;
}