From 7a8f1efab92ac4ddd336870cb098c85647e9e6b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=8C=AF=E5=8D=8E=20=28WANG=20Zhenhua=29?= Date: Fri, 25 Aug 2017 13:56:11 +0800 Subject: [PATCH] Compatible resource directory determination Previously, the resource directory is generated from `SNAP` if set, `/usr/share/anbox` is used otherwise. This cannot work with binaries installed via `make install` since the loading screen is mostly at `/usr/local/share/anbox/ui/loading-screen.png`. With this patch, anbox can get correct resource directory when it's installed without snap. --- src/anbox/cmds/launch.cpp | 6 ------ src/anbox/config.cpp | 25 +++++++++++++++++++++---- src/anbox/config.h | 7 +++---- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/anbox/cmds/launch.cpp b/src/anbox/cmds/launch.cpp index 7762af3..77a84eb 100644 --- a/src/anbox/cmds/launch.cpp +++ b/src/anbox/cmds/launch.cpp @@ -119,12 +119,6 @@ anbox::cmds::Launch::Launch() auto bus = std::make_shared(bus_type); bus->install_executor(core::dbus::asio::make_executor(bus, rt->service())); - const auto snap_path = utils::get_env_value("SNAP"); - if (!snap_path.empty()) { - const auto resource_path = fs::path(snap_path) / "usr" / "share" / "anbox"; - SystemConfiguration::instance().set_resource_path(resource_path); - } - std::shared_ptr ss; // Instead of relying on the user session init system to start our diff --git a/src/anbox/config.cpp b/src/anbox/config.cpp index a13aa43..eb3d185 100644 --- a/src/anbox/config.cpp +++ b/src/anbox/config.cpp @@ -41,10 +41,6 @@ void anbox::SystemConfiguration::set_data_path(const std::string &path) { data_path = path; } -void anbox::SystemConfiguration::set_resource_path(const fs::path &path) { - resource_path = path; -} - fs::path anbox::SystemConfiguration::data_dir() const { return data_path; } @@ -88,3 +84,24 @@ anbox::SystemConfiguration& anbox::SystemConfiguration::instance() { static SystemConfiguration config; return config; } + +anbox::SystemConfiguration::SystemConfiguration() { + auto gen_resource_path = [] () -> fs::path { + const auto snap_path = utils::get_env_value("SNAP"); + if (!snap_path.empty()) { + return fs::path(snap_path) / "usr/share/anbox"; + } + + const std::string exe = utils::process_get_exe_path(::getpid()); + const std::size_t pos = exe.rfind("/bin/anbox"); + if (pos != std::string::npos) { + const std::string leading_path = exe.substr(0, pos); + return fs::path(leading_path) / "share/anbox"; + } else { + return "/usr/local/share/anbox"; + } + }; + + resource_path = gen_resource_path(); + data_path = "/var/local/lib/anbox"; +} diff --git a/src/anbox/config.h b/src/anbox/config.h index 0fce571..c6b88da 100644 --- a/src/anbox/config.h +++ b/src/anbox/config.h @@ -31,7 +31,6 @@ class SystemConfiguration { virtual ~SystemConfiguration() = default; void set_data_path(const std::string &path); - void set_resource_path(const boost::filesystem::path &path); boost::filesystem::path data_dir() const; std::string rootfs_dir() const; @@ -44,10 +43,10 @@ class SystemConfiguration { std::string resource_dir() const; protected: - SystemConfiguration() = default; + SystemConfiguration(); - boost::filesystem::path data_path = "/var/lib/anbox"; - boost::filesystem::path resource_path = "/usr/share/anbox"; + boost::filesystem::path data_path; + boost::filesystem::path resource_path; }; } // namespace anbox