TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / system_configuration.cpp
1 /*
2  * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
3  *
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.
7  *
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.
12  *
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/>.
15  *
16  */
17
18
19 #include "anbox/system_configuration.h"
20 #include "anbox/utils.h"
21 #include "anbox/build/config.h"
22
23 #include "external/xdg/xdg.h"
24
25 #include <cstring>
26
27 namespace fs = boost::filesystem;
28
29 namespace {
30 static std::string runtime_dir() {
31   static std::string path;
32   if (path.empty()) {
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();
36     else {
37       path = anbox::utils::get_env_value("XDG_RUNTIME_DIR");
38       if (path.empty())
39         BOOST_THROW_EXCEPTION(std::runtime_error("No runtime directory specified"));
40     }
41   }
42   return path;
43 }
44 }
45
46 void anbox::SystemConfiguration::set_data_path(const std::string &path) {
47   data_path = path;
48 }
49
50 fs::path anbox::SystemConfiguration::data_dir() const {
51   return data_path;
52 }
53
54 std::string anbox::SystemConfiguration::rootfs_dir() const {
55   return (data_path / "rootfs").string();
56 }
57
58 std::string anbox::SystemConfiguration::overlay_dir() const {
59   return (data_path / "rootfs-overlay").string();
60 }
61
62 std::string anbox::SystemConfiguration::combined_rootfs_dir() const {
63   return (data_path / "combined-rootfs").string();
64 }
65
66 std::string anbox::SystemConfiguration::log_dir() const {
67   return (data_path / "logs").string();
68 }
69
70 std::string anbox::SystemConfiguration::container_config_dir() const {
71   return (data_path / "containers").string();
72 }
73
74 std::string anbox::SystemConfiguration::container_socket_path() const {
75   static std::string path;
76   if (path.empty()) {
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();
80     else
81       path = "/run/anbox-container.socket";
82   }
83   return path;
84 }
85
86 std::string anbox::SystemConfiguration::container_devices_dir() const {
87   return (data_path / "devices").string();
88 }
89
90 std::string anbox::SystemConfiguration::container_state_dir() const {
91   return (data_path / "state").string();
92 }
93
94 std::string anbox::SystemConfiguration::socket_dir() const {
95   static std::string dir = anbox::utils::string_format("%s/anbox/sockets", runtime_dir());
96   return dir;
97 }
98
99 std::string anbox::SystemConfiguration::input_device_dir() const {
100   static std::string dir = anbox::utils::string_format("%s/anbox/input", runtime_dir());
101   return dir;
102 }
103
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";
108   return dir.string();
109 }
110
111 std::string anbox::SystemConfiguration::resource_dir() const {
112   return resource_path.string();
113 }
114
115 anbox::SystemConfiguration& anbox::SystemConfiguration::instance() {
116   static SystemConfiguration config;
117   return config;
118 }
119
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";
125     } else {
126       return anbox::build::default_resource_path;
127     }
128   };
129
130   resource_path = detect_resource_path();
131   data_path = anbox::build::default_data_path;
132 }