2 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "anbox/system_configuration.h"
20 #include "anbox/utils.h"
21 #include "anbox/build/config.h"
23 #include "external/xdg/xdg.h"
27 namespace fs = boost::filesystem;
30 static std::string runtime_dir() {
31 static std::string path;
33 const auto snap_user_common = anbox::utils::get_env_value("SNAP_USER_COMMON");
34 if (!snap_user_common.empty())
35 path = (fs::path(snap_user_common) / "runtime").string();
37 path = anbox::utils::get_env_value("XDG_RUNTIME_DIR");
39 BOOST_THROW_EXCEPTION(std::runtime_error("No runtime directory specified"));
46 void anbox::SystemConfiguration::set_data_path(const std::string &path) {
50 fs::path anbox::SystemConfiguration::data_dir() const {
54 std::string anbox::SystemConfiguration::rootfs_dir() const {
55 return (data_path / "rootfs").string();
58 std::string anbox::SystemConfiguration::overlay_dir() const {
59 return (data_path / "rootfs-overlay").string();
62 std::string anbox::SystemConfiguration::combined_rootfs_dir() const {
63 return (data_path / "combined-rootfs").string();
66 std::string anbox::SystemConfiguration::log_dir() const {
67 return (data_path / "logs").string();
70 std::string anbox::SystemConfiguration::container_config_dir() const {
71 return (data_path / "containers").string();
74 std::string anbox::SystemConfiguration::container_socket_path() const {
75 static std::string path;
77 const auto snap_common = anbox::utils::get_env_value("SNAP_COMMON");
78 if (!snap_common.empty())
79 path = (fs::path(snap_common) / "sockets" / "anbox-container.socket").string();
81 path = "/run/anbox-container.socket";
86 std::string anbox::SystemConfiguration::container_devices_dir() const {
87 return (data_path / "devices").string();
90 std::string anbox::SystemConfiguration::container_state_dir() const {
91 return (data_path / "state").string();
94 std::string anbox::SystemConfiguration::socket_dir() const {
95 static std::string dir = anbox::utils::string_format("%s/anbox/sockets", runtime_dir());
99 std::string anbox::SystemConfiguration::input_device_dir() const {
100 static std::string dir = anbox::utils::string_format("%s/anbox/input", runtime_dir());
104 std::string anbox::SystemConfiguration::application_item_dir() const {
105 static auto dir = xdg::data().home() / "applications" / "anbox";
106 if (anbox::utils::get_env_value("ANBOX_NO_DESKTOP_SUBDIR").length() > 0)
107 dir = xdg::data().home() / "applications";
111 std::string anbox::SystemConfiguration::resource_dir() const {
112 return resource_path.string();
115 anbox::SystemConfiguration& anbox::SystemConfiguration::instance() {
116 static SystemConfiguration config;
120 anbox::SystemConfiguration::SystemConfiguration() {
121 auto detect_resource_path = [] () -> fs::path {
122 const auto snap_path = utils::get_env_value("SNAP");
123 if (!snap_path.empty()) {
124 return fs::path(snap_path) / "usr" / "share" / "anbox";
126 return anbox::build::default_resource_path;
130 resource_path = detect_resource_path();
131 data_path = anbox::build::default_data_path;