Merge branch 'fix-fallocate-fail' into 'main'

wayland: Work around fallocate bugs

See merge request GNOME/gtk!7429
This commit is contained in:
Matthias Clasen
2024-07-08 17:24:16 +00:00

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