25RamFile::RamFile(
const char *name,
const void *inData,
int size, RamFile::Flags flags)
29 auto guard = qScopeGuard([
this] {
34 m_fd = FileDescriptor(memfd_create(name, MFD_CLOEXEC | MFD_ALLOW_SEALING));
35 if (!m_fd.isValid()) {
36 qCWarning(KWIN_CORE).nospace() << name <<
": Can't create memfd: " << strerror(errno);
40 if (ftruncate(m_fd.get(), size) < 0) {
41 qCWarning(KWIN_CORE).nospace() << name <<
": Failed to ftruncate memfd: " << strerror(errno);
45 void *data = mmap(
nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd.get(), 0);
46 if (data == MAP_FAILED) {
47 qCWarning(KWIN_CORE).nospace() << name <<
": mmap failed: " << strerror(errno);
51 m_tmp = std::make_unique<QTemporaryFile>();
53 qCWarning(KWIN_CORE).nospace() << name <<
": Can't open temporary file";
57 if (unlink(m_tmp->fileName().toUtf8().constData()) != 0) {
58 qCWarning(KWIN_CORE).nospace() << name <<
": Failed to remove temporary file from filesystem: " << strerror(errno);
61 if (!m_tmp->resize(size)) {
62 qCWarning(KWIN_CORE).nospace() << name <<
": Failed to resize temporary file";
66 uchar *data = m_tmp->map(0, size);
68 qCWarning(KWIN_CORE).nospace() << name <<
": map failed";
73 memcpy(data, inData, size);
81 int seals = F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_SEAL;
82 if (flags.testFlag(RamFile::Flag::SealWrite)) {
83 seals |= F_SEAL_WRITE;
86 if (fcntl(fd(), F_ADD_SEALS, seals) != 0) {
87 qCDebug(KWIN_CORE).nospace() << name <<
": Failed to seal RamFile: " << strerror(errno);
93RamFile::RamFile(
RamFile &&other) Q_DECL_NOEXCEPT
94 : m_size(std::exchange(other.m_size, 0))
95 , m_flags(std::exchange(other.m_flags, RamFile::Flags{}))
99 , m_tmp(std::exchange(other.m_tmp, {}))
107 m_size = std::exchange(other.m_size, 0);
108 m_flags = std::exchange(other.m_flags, RamFile::Flags{});
112 m_tmp = std::exchange(other.m_tmp, {});
122void RamFile::cleanup()
131bool RamFile::isValid()
const
136RamFile::Flags RamFile::effectiveFlags()
const
140 const int seals = fcntl(fd(), F_GET_SEALS);
142 if (seals & F_SEAL_WRITE) {
143 flags.setFlag(Flag::SealWrite);
150int RamFile::fd()
const
155 return m_tmp->handle();
159int RamFile::size()
const
Creates a file in memory.