feat: install a Flatpak's data under /app/share
A program packaged as a Flatpak could only have its files beside it in files/bin, where cfg.files goes. One that finds its data at <its directory>/../share/<name>, as Crafter.Review finds its process/ directory, had no way to get it there: files/share belongs to the writer, which removes anything it did not put there, and a module of flatpak-builder's would have placed it only in the installed app, never under -r. cfg.flatpak.share lists files and directories of the project and the path under /app/share each goes to. The writer copies them into files/share with the desktop entry, metainfo and icons, keeps them while they are listed and removes them once they are not, as it does those. -r runs the program from files/bin, so the same ../share lookup finds them there. What is exported of them is flatpak build-finish's to decide, by its own rules, as for any app's files: the app's D-Bus services, MIME types, search providers and KRunner plugins, which have no field of their own, are placed like any other file. A library's are not passed on to the programs built on it, since data a library needs at run time already goes through cfg.files. A dependency that packages its own executable has the sources rebased by the loader, as its cfg.files are. Share entries change files/share and not what is compiled, so they are not keyed into VariantId, which its test checks. Every file bound for files/share, the desktop entry, metainfo and icons included, now goes through one placement check, which refuses two files for one path, a field's file among them, and a file where another needs a directory, naming both. A target that is absolute or leaves share/ is refused too. Any other place is open to a share entry, a second desktop entry, an add-on's metainfo and any icon included, as for any app's files: flatpak build-export refuses an exported icon that is not a square image and a D-Bus service that is not a key file, warns of the app's own desktop entry where desktop-file-validate rejects it or its Icon= is not the app's, and exports a second desktop entry, a MIME package or metainfo as it is. The app's own desktop entry, applications/<app-id>.desktop, is held to cfg.flatpak.desktopFile's checks whichever placed it: its Icon= and desktop-file-validate are keyed to that place now, not to the field. A source directory that holds what the build writes is refused as well, since it would be copied into itself, growing each time the app is built or exported: files/, and the app's directory, where install and bundle put another copy of files/ and the exported app, compared as written and where links lead, so that the project's own directory is refused where its bin/ is a link elsewhere too. So is one that lies in them, the build's own output, which the next build, install or bundle writes again. The loader leaves an empty source empty, as it does an empty desktop file. FlatpakShareLarge holds the writer to comparing an entry with its copy at constant memory, which the program's data, gigabytes of it in a game, needs on every build: it rewrites a files/ holding a 32 MiB entry, with nothing changed, in a child process whose address space may grow by 16 MiB, with a stand-in llvm-readelf, since the real one, which a write starts for the libraries, would not fit under that limit, and a change in the entry's last byte, in the short block a comparison reads last, is still copied.
This commit is contained in:
parent
52a767fadc
commit
e0ac8ee600
17 changed files with 557 additions and 20 deletions
115
tests/FlatpakShareLarge/main.cpp
Normal file
115
tests/FlatpakShareLarge/main.cpp
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
#include <sys/resource.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
import std;
|
||||
import Crafter.Build;
|
||||
namespace fs = std::filesystem;
|
||||
using namespace Crafter;
|
||||
|
||||
namespace {
|
||||
std::int32_t Failures = 0;
|
||||
|
||||
void Check(bool cond, std::string_view msg) {
|
||||
if (!cond) {
|
||||
std::println(std::cerr, "FAIL: {}", msg);
|
||||
++Failures;
|
||||
}
|
||||
}
|
||||
|
||||
// The process's address space now, in bytes: the first field of
|
||||
// /proc/self/statm, in pages.
|
||||
std::uint64_t AddressSpace() {
|
||||
std::ifstream statm("/proc/self/statm");
|
||||
std::uint64_t pages = 0;
|
||||
statm >> pages;
|
||||
return pages * static_cast<std::uint64_t>(sysconf(_SC_PAGESIZE));
|
||||
}
|
||||
}
|
||||
|
||||
// A share entry can be a program's data, gigabytes of it, and the writer
|
||||
// compares each with its copy in files/share on every build, to leave an
|
||||
// unchanged one alone. It compared them by reading both files whole, so a
|
||||
// build with nothing to copy held twice the largest entry in memory, and
|
||||
// failed where that did not fit, every time after the first. A files/ with
|
||||
// a 32 MiB share entry is written, then written again with nothing changed
|
||||
// in a child process whose address space may grow by no more than 16 MiB:
|
||||
// that rewrite has to succeed. A change in the entry's last byte, in the
|
||||
// short block a comparison reads last, is still copied. The writer is
|
||||
// called directly, with a stand-in llvm-readelf: a whole Build() would start
|
||||
// compiles and threads, and the real llvm-readelf a write starts for the
|
||||
// libraries, which the same limit would hold as well.
|
||||
int main() {
|
||||
const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-share-large";
|
||||
std::error_code ec;
|
||||
fs::remove_all(scratch, ec);
|
||||
fs::create_directories(scratch / "app" / "files" / "bin");
|
||||
fs::create_directories(scratch / "build");
|
||||
// The program, and a stand-in for llvm-readelf that says it needs
|
||||
// nothing: the writer reads the program's libraries on every write, and
|
||||
// the real llvm-readelf, started under the child's limit below, does not
|
||||
// fit in it. The libraries are not what is measured here.
|
||||
std::ofstream(scratch / "app" / "files" / "bin" / "large") << "a program\n";
|
||||
const fs::path readelf = scratch / "readelf";
|
||||
std::ofstream(readelf) << "#!/bin/sh\necho 'NeededLibraries ['\necho ']'\n";
|
||||
fs::permissions(readelf, fs::perms::owner_all);
|
||||
// Not a whole number of blocks, so the last one is short.
|
||||
constexpr std::uintmax_t Size = (32 * 1024 * 1024) + 3;
|
||||
const fs::path data = scratch / "data.bin";
|
||||
std::ofstream(data, std::ios::binary).close();
|
||||
fs::resize_file(data, Size);
|
||||
|
||||
const Toolchain& tc = GetToolchain();
|
||||
FlatpakFiles d;
|
||||
d.root = scratch / "app";
|
||||
d.record = scratch / "build" / "flatpak-record";
|
||||
d.appId = "net.example.ShareLarge";
|
||||
d.command = "large";
|
||||
d.share = { {data, "large/data.bin"} };
|
||||
d.clangxx = tc.clangxx;
|
||||
d.readelf = readelf;
|
||||
d.programEntries = {"large"};
|
||||
|
||||
const std::string first = WriteFlatpakFiles(d);
|
||||
const fs::path copy = d.root / "files" / "share" / "large" / "data.bin";
|
||||
Check(first.empty() && fs::file_size(copy, ec) == Size, std::format("the share entry is copied: {}", first));
|
||||
|
||||
// The rewrite, in a child under the limit: its exit status is whether
|
||||
// the writer finished. No thread runs here to be lost in the fork, and
|
||||
// nothing buffered is written twice.
|
||||
std::fflush(nullptr);
|
||||
const pid_t child = fork();
|
||||
if (child == 0) {
|
||||
rlimit limit{};
|
||||
getrlimit(RLIMIT_AS, &limit);
|
||||
limit.rlim_cur = std::min<rlim_t>(AddressSpace() + (16 * 1024 * 1024), limit.rlim_max);
|
||||
if (setrlimit(RLIMIT_AS, &limit) != 0) _exit(2);
|
||||
const std::string again = WriteFlatpakFiles(d);
|
||||
if (!again.empty()) std::println(std::cerr, "the rewrite failed: {}", again);
|
||||
_exit(again.empty() ? 0 : 1);
|
||||
}
|
||||
std::int32_t status = 0;
|
||||
if (child > 0) waitpid(child, &status, 0);
|
||||
Check(child > 0 && WIFEXITED(status) && WEXITSTATUS(status) == 0, std::format("rewriting it with nothing changed fits in 16 MiB more than the process has (status {})", status));
|
||||
|
||||
// The comparison still sees a change in the last, short block.
|
||||
{
|
||||
std::fstream out(data, std::ios::binary | std::ios::in | std::ios::out);
|
||||
out.seekp(static_cast<std::streamoff>(Size - 1));
|
||||
out.put('x');
|
||||
}
|
||||
const std::string changed = WriteFlatpakFiles(d);
|
||||
std::ifstream in(copy, std::ios::binary);
|
||||
in.seekg(static_cast<std::streamoff>(Size - 1));
|
||||
Check(changed.empty() && in.get() == 'x', std::format("a change in the entry's last byte is copied: {}", changed));
|
||||
in.close();
|
||||
|
||||
fs::remove_all(scratch, ec);
|
||||
if (Failures > 0) {
|
||||
std::println(std::cerr, "{} assertions failed", Failures);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in a new issue