mirror of
https://gitlab.freedesktop.org/libinput/libei.git
synced 2026-05-01 08:08:19 +02:00
utils: basic helper for creating a file in memory
Needed by the tools for keymap handling. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
parent
575943e744
commit
2a619fd4b4
3 changed files with 128 additions and 0 deletions
|
|
@ -24,6 +24,8 @@ lib_util = static_library('util',
|
|||
'src/util-logger.h',
|
||||
'src/util-logger.c',
|
||||
'src/util-macros.h',
|
||||
'src/util-memfile.h',
|
||||
'src/util-memfile.c',
|
||||
'src/util-color.h',
|
||||
'src/util-object.h',
|
||||
'src/util-sources.h',
|
||||
|
|
|
|||
83
src/util-memfile.c
Normal file
83
src/util-memfile.c
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
/*
|
||||
* Copyright © 2020 Red Hat, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "util-memfile.h"
|
||||
#include "util-mem.h"
|
||||
#include "util-io.h"
|
||||
|
||||
#include "util-object.h"
|
||||
|
||||
struct memfile {
|
||||
struct object object;
|
||||
int fd;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
static void
|
||||
memfile_destroy(struct memfile *memfile)
|
||||
{
|
||||
if (memfile->fd != -1)
|
||||
close(memfile->fd);
|
||||
}
|
||||
|
||||
OBJECT_IMPLEMENT_CREATE(memfile);
|
||||
OBJECT_IMPLEMENT_UNREF(memfile);
|
||||
OBJECT_IMPLEMENT_REF(memfile);
|
||||
OBJECT_IMPLEMENT_GETTER(memfile, fd, int);
|
||||
OBJECT_IMPLEMENT_GETTER(memfile, size, size_t);
|
||||
|
||||
#define _cleanup_memfile_ _cleanup_(memfile_cleanup)
|
||||
|
||||
struct memfile *
|
||||
memfile_new(const char *data, size_t sz) {
|
||||
_cleanup_memfile_ struct memfile *memfile = memfile_create(NULL);
|
||||
|
||||
_cleanup_close_ int fd = memfd_create("memfile", MFD_CLOEXEC|MFD_ALLOW_SEALING);
|
||||
if (fd < 0)
|
||||
return NULL;
|
||||
|
||||
fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK);
|
||||
int rc = posix_fallocate(fd, 0, sz);
|
||||
if (rc < 0)
|
||||
return NULL;
|
||||
|
||||
memfile->fd = fd;
|
||||
memfile->size = sz;
|
||||
fd = -1;
|
||||
|
||||
void *map = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_SHARED, memfile->fd, 0);
|
||||
if (map == MAP_FAILED)
|
||||
return NULL;
|
||||
|
||||
memcpy(map, data, sz);
|
||||
munmap(map, sz);
|
||||
|
||||
return steal(&memfile);
|
||||
}
|
||||
43
src/util-memfile.h
Normal file
43
src/util-memfile.h
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Copyright © 2020 Red Hat, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "config.h"
|
||||
|
||||
struct memfile;
|
||||
|
||||
struct memfile *
|
||||
memfile_new(const char *data, size_t sz);
|
||||
|
||||
struct memfile *
|
||||
memfile_ref(struct memfile *memfile);
|
||||
|
||||
struct memfile *
|
||||
memfile_unref(struct memfile *memfile);
|
||||
|
||||
int
|
||||
memfile_get_fd(struct memfile *memfile);
|
||||
|
||||
size_t
|
||||
memfile_get_size(struct memfile *memfile);
|
||||
Loading…
Add table
Reference in a new issue