1
0
Fork 0

core/anonymous-file: Support 0-sized files

There is nothing to allocate for a 0-sized files, and indeed
posix_fallocate() will error out if the passed len isn't greater
than 0.

Now that anonymous files are used to back the memory selection
source, this fixes unsetting the selection when the screen is
locked.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3752>
This commit is contained in:
Florian Müllner 2024-05-14 17:33:00 +02:00 committed by Marge Bot
parent 09a6ff6eb7
commit da0bd303ad

View file

@ -136,6 +136,9 @@ create_anonymous_file (off_t size)
return -1;
}
if (size == 0)
return fd;
#if defined(HAVE_POSIX_FALLOCATE)
do
{
@ -187,7 +190,6 @@ meta_anonymous_file_new (size_t size,
const uint8_t *data)
{
MetaAnonymousFile *file;
void *map;
file = g_malloc0 (sizeof *file);
if (!file)
@ -201,13 +203,17 @@ meta_anonymous_file_new (size_t size,
if (file->fd == -1)
goto err_free;
map = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, file->fd, 0);
if (map == MAP_FAILED)
goto err_close;
if (size > 0)
{
void *map;
memcpy (map, data, size);
map = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, file->fd, 0);
if (map == MAP_FAILED)
goto err_close;
munmap (map, size);
memcpy (map, data, size);
munmap (map, size);
}
#if defined(HAVE_MEMFD_CREATE)
/* try to put seals on the file to make it read-only so that we can
@ -310,6 +316,9 @@ meta_anonymous_file_open_fd (MetaAnonymousFile *file,
if (fd == -1)
return fd;
if (file->size == 0)
return fd;
src = mmap (NULL, file->size, PROT_READ, MAP_PRIVATE, file->fd, 0);
if (src == MAP_FAILED)
{