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
1
tests/FlatpakShare/fixture/data/LICENSE
Normal file
1
tests/FlatpakShare/fixture/data/LICENSE
Normal file
|
|
@ -0,0 +1 @@
|
|||
A licence text for crafter-build's FlatpakShare test.
|
||||
7
tests/FlatpakShare/fixture/data/app.metainfo.xml
Normal file
7
tests/FlatpakShare/fixture/data/app.metainfo.xml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<component type="console-application">
|
||||
<id>net.example.Share</id>
|
||||
<name>Share</name>
|
||||
<summary>A fixture for crafter-build's tests</summary>
|
||||
<metadata_license>CC0-1.0</metadata_license>
|
||||
</component>
|
||||
1
tests/FlatpakShare/fixture/data/process/lens.md
Normal file
1
tests/FlatpakShare/fixture/data/process/lens.md
Normal file
|
|
@ -0,0 +1 @@
|
|||
the lens as first written
|
||||
1
tests/FlatpakShare/fixture/data/process/steps/one.md
Normal file
1
tests/FlatpakShare/fixture/data/process/steps/one.md
Normal file
|
|
@ -0,0 +1 @@
|
|||
a step in a subdirectory
|
||||
19
tests/FlatpakShare/fixture/main.cpp
Normal file
19
tests/FlatpakShare/fixture/main.cpp
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
import std;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
// Finds its data the way Crafter.Review finds its process/ directory: at
|
||||
// <its directory>/../share/<name>, which is /app/share installed and
|
||||
// files/share under -r.
|
||||
int main() {
|
||||
const fs::path lens = fs::read_symlink("/proc/self/exe").parent_path() / ".." / "share" / "demo" / "process" / "lens.md";
|
||||
std::ifstream in(lens, std::ios::binary);
|
||||
if (!in) {
|
||||
std::println(std::cerr, "no {}", lens.string());
|
||||
return 1;
|
||||
}
|
||||
std::print("{}", std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>()));
|
||||
return 0;
|
||||
}
|
||||
253
tests/FlatpakShare/main.cpp
Normal file
253
tests/FlatpakShare/main.cpp
Normal file
|
|
@ -0,0 +1,253 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
#include <stdlib.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;
|
||||
}
|
||||
}
|
||||
|
||||
std::string ReadFile(const fs::path& path) {
|
||||
std::ifstream in(path, std::ios::binary);
|
||||
return std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
|
||||
}
|
||||
|
||||
std::string BuildIt(Configuration& cfg) {
|
||||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
return Build(cfg, depResults, depMutex).result;
|
||||
}
|
||||
|
||||
std::map<fs::path, fs::file_time_type> Mtimes(const fs::path& root) {
|
||||
std::map<fs::path, fs::file_time_type> times;
|
||||
for (const fs::directory_entry& entry : fs::recursive_directory_iterator(root)) {
|
||||
if (entry.is_regular_file()) times.emplace(entry.path(), entry.last_write_time());
|
||||
}
|
||||
return times;
|
||||
}
|
||||
}
|
||||
|
||||
// cfg.flatpak.share puts files the program reads under /app/share rather
|
||||
// than next to it, where cfg.files goes: a directory with everything in it
|
||||
// and single files, each at the path under share/ it names. Crafter.Review
|
||||
// finds its process/ directory at <its directory>/../share/crafter-review,
|
||||
// and the Flatpak's files/share is the writer's own, which it prunes, so
|
||||
// before this there was no way to put it there. The fixture program reads
|
||||
// its data the same way, and is run from files/bin as -r runs it. The
|
||||
// entries are kept while listed: a build that changed nothing writes
|
||||
// nothing, an edited source is copied again, a removed one goes, and so does
|
||||
// an entry no longer listed. The build only places them: what is exported of
|
||||
// them is flatpak build-finish's to decide, and build-export's to check, as
|
||||
// for any app's files. A target outside share/, a missing source, and two
|
||||
// files for one path, or a file where another needs a directory, are
|
||||
// refused, naming what clashed, a field's own file among them. So is a
|
||||
// directory that holds what the build writes, files/ or the app's
|
||||
// directory, where install and bundle put another copy of files/, which
|
||||
// would be copied into itself, as written and where links lead, so the
|
||||
// directory of a project whose bin/ is a link elsewhere as well; and one
|
||||
// that lies in them, the build's own output. The
|
||||
// app's desktop entry is held to the field's checks whichever placed it, so
|
||||
// one a share entry places with another Icon= is refused. Anything else is
|
||||
// placed like any other file: a second desktop entry, an add-on's metainfo,
|
||||
// the legacy appdata/, any icon, of the app's or not, and the app's D-Bus
|
||||
// services, MIME packages, search providers and KRunner plugins, which no
|
||||
// field of their own carries.
|
||||
int main() {
|
||||
const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-share";
|
||||
std::error_code ec;
|
||||
fs::remove_all(scratch, ec);
|
||||
fs::create_directories(scratch);
|
||||
const fs::path info = scratch / "flatpak-info";
|
||||
std::ofstream(info) << "[Application]\nname=net.catcrafts.CrafterBuild\nruntime=runtime/org.freedesktop.Sdk/x86_64/26.08\n";
|
||||
setenv("CRAFTER_BUILD_FLATPAK_INFO", info.string().c_str(), 1);
|
||||
if (NormalizeTarget(GetToolchain().hostTriple) != "x86_64-linux-gnu") {
|
||||
std::println(std::cerr, "SKIP: the staged runtime is x86_64 and the host is {}", GetToolchain().hostTriple);
|
||||
return 77;
|
||||
}
|
||||
if (!GetToolchain().libcxxDir.empty()) {
|
||||
std::println(std::cerr, "SKIP: CRAFTER_BUILD_LIBCXX_DIR is set, and a build against it is no Flatpak");
|
||||
return 77;
|
||||
}
|
||||
|
||||
const fs::path fixture = fs::current_path() / "tests" / "FlatpakShare" / "fixture";
|
||||
fs::remove_all(fixture / "bin", ec);
|
||||
fs::remove_all(fixture / "build", ec);
|
||||
// The data is edited and deleted below, so it is staged.
|
||||
const fs::path data = scratch / "data";
|
||||
fs::copy(fixture / "data", data, fs::copy_options::recursive);
|
||||
|
||||
Configuration app;
|
||||
app.path = fixture;
|
||||
app.name = "share-app";
|
||||
app.outputName = "share";
|
||||
app.target = HostTarget();
|
||||
app.packaging = Packaging::Flatpak;
|
||||
app.flatpak.appId = "net.example.Share";
|
||||
app.flatpak.metainfo = data / "app.metainfo.xml";
|
||||
const FlatpakShareEntry process = { data / "process", "demo/process" };
|
||||
const FlatpakShareEntry license = { data / "LICENSE", "licenses/net.example.Share/LICENSE" };
|
||||
app.flatpak.share = { process, license };
|
||||
{
|
||||
std::array<fs::path, 0> ifaces = {};
|
||||
std::array<fs::path, 1> impls = { "main" };
|
||||
app.GetInterfacesAndImplementations(ifaces, impls);
|
||||
}
|
||||
if (const std::string failed = BuildIt(app); !failed.empty()) {
|
||||
std::println(std::cerr, "FAIL: build failed: {}", failed);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const fs::path root = app.BinDir();
|
||||
const fs::path share = root / "files" / "share";
|
||||
Check(ReadFile(share / "demo" / "process" / "lens.md") == ReadFile(data / "process" / "lens.md"), "a directory entry is copied under its target");
|
||||
Check(fs::is_regular_file(share / "demo" / "process" / "steps" / "one.md"), "with everything in it");
|
||||
Check(ReadFile(share / "licenses" / "net.example.Share" / "LICENSE") == ReadFile(data / "LICENSE"), "a file entry is copied to its target");
|
||||
Check(fs::is_regular_file(share / "metainfo" / "net.example.Share.metainfo.xml"), "the metainfo is still placed beside them");
|
||||
Check(!fs::exists(root / "export"), "the build writes no export/, which build-finish does");
|
||||
|
||||
const auto run = RunCommandWithTimeout(ShellQuote(ExpectedOutput(app).string()), std::chrono::seconds(30));
|
||||
Check(run.exitCode == 0 && run.output == "the lens as first written\n", std::format("the program finds its data at <its directory>/../share (exit {}): {}", run.exitCode, run.output));
|
||||
|
||||
const std::map<fs::path, fs::file_time_type> before = Mtimes(share);
|
||||
Check(BuildIt(app).empty(), "the second build succeeds");
|
||||
Check(Mtimes(share) == before, "a build that changed nothing leaves files/share alone");
|
||||
|
||||
std::ofstream(data / "process" / "lens.md", std::ios::trunc) << "the lens as edited\n";
|
||||
fs::remove(data / "process" / "steps" / "one.md");
|
||||
Check(BuildIt(app).empty(), "the build after the edits succeeds");
|
||||
Check(ReadFile(share / "demo" / "process" / "lens.md") == "the lens as edited\n", "an edited source is copied again");
|
||||
Check(!fs::exists(share / "demo" / "process" / "steps"), "a removed source goes, and the directory it leaves empty with it");
|
||||
|
||||
app.flatpak.share = { process };
|
||||
Check(BuildIt(app).empty(), "the build without the licence entry succeeds");
|
||||
Check(!fs::exists(share / "licenses"), "an entry no longer listed goes");
|
||||
Check(fs::exists(share / "demo" / "process" / "lens.md"), "the one still listed stays");
|
||||
|
||||
// A directory entry that holds an icon of the app, among others.
|
||||
const fs::path theme = data / "theme" / "hicolor" / "scalable";
|
||||
fs::create_directories(theme / "apps");
|
||||
fs::create_directories(theme / "actions");
|
||||
std::ofstream(theme / "apps" / "net.example.Share.svg") << "<svg/>\n";
|
||||
std::ofstream(theme / "actions" / "demo-action.svg") << "<svg/>\n";
|
||||
|
||||
struct Refusal {
|
||||
std::string_view what;
|
||||
std::vector<FlatpakShareEntry> share;
|
||||
std::string_view reason;
|
||||
};
|
||||
// The app's directory, where install and bundle make build-dir/files of
|
||||
// files/ again, as they would have by now.
|
||||
fs::create_directories(FlatpakAppDir(app) / "build-dir" / "files");
|
||||
const std::array<Refusal, 12> refusals = {{
|
||||
{"an absolute target", {{data / "LICENSE", "/etc/LICENSE"}}, "has to be a path inside /app/share"},
|
||||
{"a target leaving share/", {{data / "LICENSE", "../bin/LICENSE"}}, "has to be a path inside /app/share"},
|
||||
{"share/ itself", {{data / "LICENSE", "."}}, "has to be a path inside /app/share"},
|
||||
{"a missing source", {{data / "NOTICE", "licenses/NOTICE"}}, "does not exist"},
|
||||
{"two files for one path", {{data / "LICENSE", "x/LICENSE"}, {data / "app.metainfo.xml", "x/LICENSE"}}, "both go to share/x/LICENSE"},
|
||||
{"a file where a directory goes", {process, {data / "LICENSE", "demo/process"}}, "would make share/demo/process a file"},
|
||||
{"a directory where a file went", {{data / "LICENSE", "demo/process"}, process}, "needs share/demo/process to be a directory"},
|
||||
{"a file over the metainfo cfg.flatpak.metainfo places", {{data / "LICENSE", "metainfo/net.example.Share.metainfo.xml"}}, "both go to share/metainfo/net.example.Share.metainfo.xml"},
|
||||
{"the project's directory, which holds files/share", {{fixture, "project"}}, "so a share entry would copy it into itself"},
|
||||
{"the bin dir's files/", {{root / "files", "build"}}, "so a share entry would copy it into itself"},
|
||||
{"the project's build/, which holds the app's directory", {{fixture / "build", "build"}}, "so a share entry would copy it into itself"},
|
||||
{"a directory inside the app's directory", {{FlatpakAppDir(app) / "build-dir", "app"}}, "which the build writes and writes again"},
|
||||
}};
|
||||
for (const Refusal& refusal : refusals) {
|
||||
app.flatpak.share = refusal.share;
|
||||
const std::string result = BuildIt(app);
|
||||
Check(result.contains(refusal.reason), std::format("{} is refused, saying '{}': {}", refusal.what, refusal.reason, result));
|
||||
}
|
||||
|
||||
// A project whose bin/ is a link elsewhere holds its files/ as written
|
||||
// and not once the link is followed, and its own directory is refused
|
||||
// all the same: its walk would otherwise copy build/, and with it the
|
||||
// app's build-dir, deeper with every install.
|
||||
const fs::path linked = scratch / "linked";
|
||||
fs::create_directories(linked);
|
||||
fs::create_directories(scratch / "linked-bin");
|
||||
fs::copy_file(fixture / "main.cpp", linked / "main.cpp");
|
||||
fs::create_directory_symlink(scratch / "linked-bin", linked / "bin");
|
||||
Configuration other;
|
||||
other.path = linked;
|
||||
other.name = "share-linked";
|
||||
other.outputName = "linked";
|
||||
other.target = HostTarget();
|
||||
other.packaging = Packaging::Flatpak;
|
||||
other.flatpak.appId = "net.example.ShareLinked";
|
||||
other.flatpak.share = { {linked, "project"} };
|
||||
{
|
||||
std::array<fs::path, 0> ifaces = {};
|
||||
std::array<fs::path, 1> impls = { "main" };
|
||||
other.GetInterfacesAndImplementations(ifaces, impls);
|
||||
}
|
||||
const std::string linkedRefused = BuildIt(other);
|
||||
Check(linkedRefused.contains("so a share entry would copy it into itself"), std::format("the directory of a project whose bin/ is a link is refused: {}", linkedRefused));
|
||||
|
||||
// The app's desktop entry is checked where it lands, whichever placed
|
||||
// it: one a share entry puts at applications/<app-id>.desktop is held to
|
||||
// its Icon= as the field's is.
|
||||
std::ofstream(data / "wrong-icon.desktop") << "[Desktop Entry]\nType=Application\nName=Share\nExec=share\nIcon=other\n";
|
||||
app.flatpak.share = { {data / "wrong-icon.desktop", "applications/net.example.Share.desktop"} };
|
||||
const std::string wrongIcon = BuildIt(app);
|
||||
Check(wrongIcon.contains("has to be the app-id"), std::format("a share entry's desktop entry of the app with another Icon= is refused: {}", wrongIcon));
|
||||
|
||||
// Anything else is placed as it is, for flatpak to export and check as
|
||||
// it does any app's: a second desktop entry, an add-on's metainfo, the
|
||||
// legacy appdata/, the app's icon where the icons field puts none,
|
||||
// inside a directory entry too, an icon of something else, an action
|
||||
// icon say, and the app's symbolic one or one named with a '+'.
|
||||
std::ofstream(data / "editor.desktop") << "[Desktop Entry]\nType=Application\nName=Share Editor\nExec=share --edit\nIcon=net.example.Share\n";
|
||||
app.flatpak.share = {
|
||||
{data / "editor.desktop", "applications/net.example.Share.Editor.desktop"},
|
||||
{data / "app.metainfo.xml", "metainfo/net.example.Share.Plugin.metainfo.xml"},
|
||||
{data / "app.metainfo.xml", "appdata/net.example.Share.appdata.xml"},
|
||||
{data / "LICENSE", "icons/hicolor/256x256/apps/net.example.Share.png"},
|
||||
{data / "theme", "icons"},
|
||||
{data / "LICENSE", "icons/hicolor/scalable/apps/net.example.SharePlus.svg"},
|
||||
{data / "LICENSE", "icons/hicolor/scalable/apps/net.example.Share-symbolic.svg"},
|
||||
{data / "LICENSE", "icons/hicolor/scalable/apps/net.example.Share+x.svg"},
|
||||
};
|
||||
const std::string placed = BuildIt(app);
|
||||
Check(placed.empty(), std::format("a second desktop entry, other metainfo and any icon build: {}", placed));
|
||||
const fs::path apps = share / "icons" / "hicolor" / "scalable" / "apps";
|
||||
for (const fs::path& place : {fs::path("applications/net.example.Share.Editor.desktop"), fs::path("metainfo/net.example.Share.Plugin.metainfo.xml"), fs::path("appdata/net.example.Share.appdata.xml"), fs::path("icons/hicolor/256x256/apps/net.example.Share.png"), fs::path("icons/hicolor/scalable/apps/net.example.Share.svg"), fs::path("icons/hicolor/scalable/actions/demo-action.svg")}) {
|
||||
Check(fs::is_regular_file(share / place), std::format("share/{} is placed", place.string()));
|
||||
}
|
||||
Check(fs::is_regular_file(apps / "net.example.SharePlus.svg") && fs::is_regular_file(apps / "net.example.Share-symbolic.svg") && fs::is_regular_file(apps / "net.example.Share+x.svg"), "and so are the app's icons the icons field does not make");
|
||||
|
||||
// So are the files of the app's that no field carries: D-Bus
|
||||
// activation, a MIME type, a search provider, a KRunner plugin.
|
||||
const std::array<fs::path, 4> others = {
|
||||
"dbus-1/services/net.example.Share.service",
|
||||
"mime/packages/net.example.Share.xml",
|
||||
"gnome-shell/search-providers/net.example.Share.search-provider.ini",
|
||||
"krunner/dbusplugins/net.example.Share.desktop",
|
||||
};
|
||||
app.flatpak.share.clear();
|
||||
for (const fs::path& place : others) app.flatpak.share.push_back({data / "LICENSE", place});
|
||||
const std::string othersBuilt = BuildIt(app);
|
||||
Check(othersBuilt.empty(), std::format("the app's D-Bus service, MIME package, search provider and KRunner plugin build: {}", othersBuilt));
|
||||
for (const fs::path& place : others) Check(fs::is_regular_file(share / place), std::format("share/{} is placed", place.string()));
|
||||
|
||||
// A source directory written with a trailing separator, "process/", is
|
||||
// the directory: each file lands at its path inside it, under the target.
|
||||
app.flatpak.share = { {data / "process" / "", "slashed"} };
|
||||
Check(BuildIt(app).empty() && fs::is_regular_file(share / "slashed" / "lens.md"), "a source directory written with a trailing separator is copied under its target");
|
||||
|
||||
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