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,6 +115,46 @@ namespace {
|
|||
return copied;
|
||||
}
|
||||
|
||||
// Whether `path` is `ancestor` or lies inside it, element by element.
|
||||
bool Within(const fs::path& path, const fs::path& ancestor) {
|
||||
return std::mismatch(ancestor.begin(), ancestor.end(), path.begin(), path.end()).first == ancestor.end();
|
||||
}
|
||||
|
||||
// Whether `outer` is `inner` or holds it, as written or where their
|
||||
// links lead: a project whose bin/ is a link elsewhere holds its files/
|
||||
// as written, and not once the link is followed.
|
||||
bool Holds(const fs::path& outer, const fs::path& inner) {
|
||||
std::error_code ec;
|
||||
if (Within(fs::absolute(inner, ec).lexically_normal(), fs::absolute(outer, ec).lexically_normal())) return true;
|
||||
const fs::path realOuter = fs::weakly_canonical(outer, ec);
|
||||
if (ec || realOuter.empty()) return false;
|
||||
const fs::path realInner = fs::weakly_canonical(inner, ec);
|
||||
if (ec || realInner.empty()) return false;
|
||||
return Within(realInner, realOuter);
|
||||
}
|
||||
|
||||
// Adds `relative` to the files a directory is to hold, copied from
|
||||
// `source`, unless that would put two files at one path, or a file where
|
||||
// another needs a directory: one share entry over another, or two icons
|
||||
// of one size. SyncOwned would otherwise copy whichever came last, or
|
||||
// fail halfway with a filesystem error.
|
||||
std::string Place(std::map<fs::path, fs::path>& files, const fs::path& relative, const fs::path& source) {
|
||||
if (auto it = files.find(relative); it != files.end()) {
|
||||
return std::format("{} and {} both go to share/{}", it->second.string(), source.string(), relative.string());
|
||||
}
|
||||
// What lies inside a path sorts right after it, element by element.
|
||||
if (auto it = files.upper_bound(relative); it != files.end() && Within(it->first, relative)) {
|
||||
return std::format("{} would make share/{} a file, where {} needs it to be a directory", source.string(), relative.string(), it->second.string());
|
||||
}
|
||||
for (fs::path parent = relative.parent_path(); !parent.empty(); parent = parent.parent_path()) {
|
||||
if (auto it = files.find(parent); it != files.end()) {
|
||||
return std::format("{} needs share/{} to be a directory, where {} makes it a file", source.string(), parent.string(), it->second.string());
|
||||
}
|
||||
}
|
||||
files.emplace(relative, source);
|
||||
return "";
|
||||
}
|
||||
|
||||
// Where an icon goes under share/: an .svg as scalable, a .png in the
|
||||
// directory of its own size, read from its header.
|
||||
fs::path IconPath(const fs::path& icon, std::string_view appId) {
|
||||
|
|
@ -324,19 +364,72 @@ std::string Crafter::WriteFlatpakFiles(const FlatpakFiles& d) {
|
|||
const fs::path desktopEntry = fs::path("applications") / std::format("{}.desktop", d.appId);
|
||||
if (!d.desktopFile.empty()) {
|
||||
if (!fs::exists(d.desktopFile)) return std::format("{}: the desktop file {} does not exist", d.appId, d.desktopFile.string());
|
||||
if (const std::string icon = DesktopIcon(d.desktopFile); !icon.empty() && icon != d.appId) {
|
||||
return std::format("{}: its Icon= is {}, and has to be the app-id, {}, which its icons are installed as", d.desktopFile.string(), icon, d.appId);
|
||||
}
|
||||
share.emplace(desktopEntry, d.desktopFile);
|
||||
if (const std::string clash = Place(share, desktopEntry, d.desktopFile); !clash.empty()) return std::format("{}: {}", d.appId, clash);
|
||||
}
|
||||
if (!d.metainfo.empty()) {
|
||||
const fs::path metainfo = fs::path("metainfo") / std::format("{}.metainfo.xml", d.appId);
|
||||
share.emplace(metainfo, d.metainfo);
|
||||
if (const std::string clash = Place(share, metainfo, d.metainfo); !clash.empty()) return std::format("{}: {}", d.appId, clash);
|
||||
}
|
||||
for (const fs::path& icon : d.icons) {
|
||||
const fs::path relative = IconPath(icon, d.appId);
|
||||
if (share.contains(relative)) return std::format("{}: two icons go to {}", d.appId, relative.string());
|
||||
share.emplace(relative, icon);
|
||||
if (const std::string clash = Place(share, relative, icon); !clash.empty()) return std::format("{}: {}", d.appId, clash);
|
||||
}
|
||||
// A share entry's file goes where it says, unless the place is
|
||||
// another file's (Place), a field's included. Whatever it is, a second
|
||||
// desktop entry or the metainfo of an add-on, say, flatpak checks
|
||||
// some of what it exports when install or bundle export the app, as
|
||||
// for any app: an icon that is not a square image, or a D-Bus
|
||||
// service that is not a key file, stops the export, the app's own
|
||||
// desktop entry gets a warning where desktop-file-validate rejects it
|
||||
// or its Icon= is not the app's, and a second desktop entry, a MIME
|
||||
// package or metainfo goes as it is. The app's own desktop entry and
|
||||
// the icons the field places are checked here too, at build time.
|
||||
for (const FlatpakShareEntry& entry : d.share) {
|
||||
fs::path target = entry.target.lexically_normal();
|
||||
if (!target.empty() && !target.has_filename()) target = target.parent_path();
|
||||
if (entry.target.is_absolute() || target.empty() || target == "." || *target.begin() == "..") {
|
||||
return std::format("{}: the share target '{}' of {} has to be a path inside /app/share, relative to it", d.appId, entry.target.string(), entry.source.string());
|
||||
}
|
||||
std::error_code ec;
|
||||
if (fs::is_directory(entry.source, ec)) {
|
||||
// A directory that holds what the build writes would be
|
||||
// copied into itself, growing each time the app is built or
|
||||
// exported: files/, where share/'s copies land, and the
|
||||
// app's directory, where install and bundle put another copy
|
||||
// of files/ and the exported app; the project's own directory
|
||||
// holds both. One that lies in them is the build's own
|
||||
// output, which the next build, install or bundle writes
|
||||
// again, and no file of the project's.
|
||||
for (const fs::path& output : {files, d.appDir}) {
|
||||
if (output.empty()) continue;
|
||||
if (Holds(entry.source, output)) {
|
||||
return std::format("{}: {} holds {}, which the build writes, so a share entry would copy it into itself, growing each time the app is built or exported: name the files or directories in it instead", d.appId, entry.source.string(), output.string());
|
||||
}
|
||||
if (Holds(output, entry.source)) {
|
||||
return std::format("{}: {} lies in {}, which the build writes and writes again: a share entry names a file of the project's", d.appId, entry.source.string(), output.string());
|
||||
}
|
||||
}
|
||||
// Written with a trailing separator, "data/", the directory is
|
||||
// taken without it, for each file's path inside it.
|
||||
const fs::path dir = entry.source.has_filename() ? entry.source : entry.source.parent_path();
|
||||
for (const fs::directory_entry& file : fs::recursive_directory_iterator(dir)) {
|
||||
if (!file.is_regular_file()) continue;
|
||||
if (const std::string clash = Place(share, target / file.path().lexically_relative(dir), file.path()); !clash.empty()) return std::format("{}: {}", d.appId, clash);
|
||||
}
|
||||
} else if (fs::is_regular_file(entry.source, ec)) {
|
||||
if (const std::string clash = Place(share, target, entry.source); !clash.empty()) return std::format("{}: {}", d.appId, clash);
|
||||
} else {
|
||||
return std::format("{}: {} does not exist, so it cannot go to share/{}", d.appId, entry.source.string(), target.string());
|
||||
}
|
||||
}
|
||||
// The app's desktop entry, applications/<app-id>.desktop, whether
|
||||
// cfg.flatpak.desktopFile or a share entry placed it: its Icon= has
|
||||
// to be the app-id, which the app's icons are installed under.
|
||||
const auto entry = share.find(desktopEntry);
|
||||
if (entry != share.end()) {
|
||||
if (const std::string icon = DesktopIcon(entry->second); !icon.empty() && icon != d.appId) {
|
||||
return std::format("{}: its Icon= is {}, and has to be the app-id, {}, which its icons are installed as", entry->second.string(), icon, d.appId);
|
||||
}
|
||||
}
|
||||
SyncOwned(files / "share", share);
|
||||
// desktop-file-validate checks the entry here, where it was written,
|
||||
|
|
@ -344,12 +437,12 @@ std::string Crafter::WriteFlatpakFiles(const FlatpakFiles& d) {
|
|||
// write that copied the entry and then failed would leave it
|
||||
// unchecked for every write after. Where the tool exists: the Sdk has
|
||||
// it, a development box may not.
|
||||
if (share.contains(desktopEntry) && RunCommandChecked("command -v desktop-file-validate").exitCode == 0) {
|
||||
if (entry != share.end() && RunCommandChecked("command -v desktop-file-validate").exitCode == 0) {
|
||||
const CommandResult r = RunCommandChecked(std::format("desktop-file-validate {}", ShellQuote((files / "share" / desktopEntry).string())));
|
||||
if (r.exitCode != 0) {
|
||||
std::error_code ec;
|
||||
fs::remove(files / "share" / desktopEntry, ec);
|
||||
return std::format("{} does not pass desktop-file-validate:\n{}", d.desktopFile.string(), Trim(r.output));
|
||||
return std::format("{} does not pass desktop-file-validate:\n{}", entry->second.string(), Trim(r.output));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue