From 8400a03fdea31a98aaf06805f194d281ce71a405 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Wed, 6 Nov 2024 11:00:58 +0100 Subject: [PATCH] More accurate flatpak info parser --- common/utils/flatpak.cpp | 9 +++++++-- common/utils/flatpak.h | 9 ++++++++- dashboard/adb.cpp | 2 +- dashboard/steam_app.cpp | 2 +- server/active_runtime.cpp | 2 +- server/main.cpp | 2 +- server/start_application.cpp | 2 +- 7 files changed, 20 insertions(+), 8 deletions(-) diff --git a/common/utils/flatpak.cpp b/common/utils/flatpak.cpp index 466db655..7a41fae6 100644 --- a/common/utils/flatpak.cpp +++ b/common/utils/flatpak.cpp @@ -29,14 +29,19 @@ bool wivrn::is_flatpak() return std::filesystem::exists(info_path); } -std::optional wivrn::flatpak_key(std::string key) +std::optional wivrn::flatpak_key(std::string section, std::string key) { + section = "[" + section + "]"; key += "="; std::string line; std::ifstream info(info_path); + bool in_section = false; while (std::getline(info, line)) { - if (line.starts_with(key)) + if (line.starts_with("[")) + in_section = section == line; + + if (in_section and line.starts_with(key)) return line.substr(key.size()); } return {}; diff --git a/common/utils/flatpak.h b/common/utils/flatpak.h index 0e1a686c..7e8fd1b6 100644 --- a/common/utils/flatpak.h +++ b/common/utils/flatpak.h @@ -24,6 +24,13 @@ namespace wivrn { + +namespace flatpak::section +{ +inline const char * instance = "Instance"; +inline const char * session_bus_policy = "Session Bus Policy"; +}; // namespace flatpak::section + bool is_flatpak(); -std::optional flatpak_key(std::string key); +std::optional flatpak_key(std::string section, std::string key); } // namespace wivrn diff --git a/dashboard/adb.cpp b/dashboard/adb.cpp index 6585327c..4b31f07b 100644 --- a/dashboard/adb.cpp +++ b/dashboard/adb.cpp @@ -33,7 +33,7 @@ std::unique_ptr escape_sandbox(const std::string & executable, Args && auto process = std::make_unique(); QStringList args; - if (wivrn::flatpak_key("org.freedesktop.Flatpak") == "talk") + if (wivrn::flatpak_key(wivrn::flatpak::section::session_bus_policy, "org.freedesktop.Flatpak") == "talk") { process->setProgram("flatpak-spawn"); args.push_back("--host"); diff --git a/dashboard/steam_app.cpp b/dashboard/steam_app.cpp index 7b14385c..6d6f2eb2 100644 --- a/dashboard/steam_app.cpp +++ b/dashboard/steam_app.cpp @@ -35,7 +35,7 @@ std::string read_vr_manifest() std::filesystem::path vrmanifest = std::string{home ? home : ""} + "/.steam/steam/config/steamapps.vrmanifest"; - if (wivrn::flatpak_key("org.freedesktop.Flatpak") == "talk") + if (wivrn::flatpak_key(wivrn::flatpak::section::session_bus_policy, "org.freedesktop.Flatpak") == "talk") { QProcess flatpak_spawn; flatpak_spawn.start("flatpak-spawn", {"--host", "cat", QString::fromStdString(vrmanifest)}); diff --git a/server/active_runtime.cpp b/server/active_runtime.cpp index 73939e46..ecef0328 100644 --- a/server/active_runtime.cpp +++ b/server/active_runtime.cpp @@ -31,7 +31,7 @@ std::filesystem::path active_runtime::manifest_path() { const std::filesystem::path install_location = "share/openxr/1/openxr_wivrn.json"; // Check if in a flatpak - if (auto path = flatpak_key("app-path")) + if (auto path = flatpak_key(flatpak::section::instance, "app-path")) return *path / install_location.relative_path(); // Check if running from build directory diff --git a/server/main.cpp b/server/main.cpp index 7bbf02bc..f33f2e9d 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -133,7 +133,7 @@ void display_child_status(int wstatus, const std::string & name) static std::filesystem::path flatpak_app_path() { - if (auto value = flatpak_key("app-path")) + if (auto value = flatpak_key(flatpak::section::instance, "app-path")) { std::filesystem::path path = *value; while (path != "" and path != path.parent_path() and path.filename() != "io.github.wivrn.wivrn") diff --git a/server/start_application.cpp b/server/start_application.cpp index 576d9903..e2943df4 100644 --- a/server/start_application.cpp +++ b/server/start_application.cpp @@ -235,7 +235,7 @@ int wivrn::exec_application(configuration config) std::string executable; std::vector args; - if (flatpak_key("org.freedesktop.Flatpak") == "talk") + if (flatpak_key(flatpak::section::session_bus_policy, "org.freedesktop.Flatpak") == "talk") { executable = "flatpak-spawn"; args.push_back("flatpak-spawn");