Add APIs for getting path related Resource Control
[platform/core/appfw/aul-1.git] / aul / app_info / directory_info.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <sys/types.h>
19 #include <tzplatform_config.h>
20 #include <unistd.h>
21
22 #include <memory>
23
24 #include "aul/app_info/app_info.hh"
25 #include "aul/app_info/directory_info.hh"
26 #include "aul/common/common.hh"
27 #include "aul/common/log_private.hh"
28 #include "include/aul.h"
29
30 namespace aul {
31 namespace {
32
33 constexpr const char kDataDir[] = "data/";
34 constexpr const char kCacheDir[] = "cache/";
35 constexpr const char kResourceDir[] = "res/";
36 constexpr const char kTepResourceDir[] = "tep/mount/";
37 constexpr const char kSharedDataDir[] = "shared/data/";
38 constexpr const char kSharedTrustedDir[] = "shared/trusted/";
39 constexpr const char kSharedResourceDir[] = "shared/res/";
40 constexpr const char kAppRWDir[] = "apps_rw/";
41 constexpr const char kLightUserSwitchModeDefault[] = "default";
42 constexpr const char kResControlAllowedDir[] = "res/mount/allowed/";
43 constexpr const char kResControlGlobalDir[] = "res/mount/global/";
44
45 std::string GetRWPath(const std::string& pkg_id, uid_t uid) {
46   tzplatform_set_user(uid);
47   std::string path = std::string(tzplatform_getenv(TZ_USER_APP)) + "/" + pkg_id;
48   tzplatform_reset_user();
49   return path;
50 }
51
52 std::string GetPath(const std::string& path,
53     const std::string& dir_name) {
54   return path + "/" + dir_name;
55 }
56
57 }  // namespace
58
59 DirectoryInfo::Builder& DirectoryInfo::Builder::SetRootPath(
60     std::string root_path) {
61   root_path_ = std::move(root_path);
62   return *this;
63 }
64
65 DirectoryInfo::Builder& DirectoryInfo::Builder::SetDataPath(
66     std::string data_path) {
67   data_path_ = std::move(data_path);
68   return *this;
69 }
70
71 DirectoryInfo::Builder& DirectoryInfo::Builder::SetCachePath(
72     std::string cache_path) {
73   cache_path_ = std::move(cache_path);
74   return *this;
75 }
76
77 DirectoryInfo::Builder& DirectoryInfo::Builder::SetResourcePath(
78     std::string resource_path) {
79   resource_path_ = std::move(resource_path);
80   return *this;
81 }
82
83 DirectoryInfo::Builder& DirectoryInfo::Builder::SetTepResourcePath(
84     std::string tep_resource_path) {
85   tep_resource_path_ = std::move(tep_resource_path);
86   return *this;
87 }
88
89 DirectoryInfo::Builder& DirectoryInfo::Builder::SetSharedDataPath(
90     std::string shared_data_path) {
91   shared_data_path_ = std::move(shared_data_path);
92   return *this;
93 }
94
95 DirectoryInfo::Builder& DirectoryInfo::Builder::SetSharedResourcePath(
96     std::string shared_resource_path) {
97   shared_resource_path_ = std::move(shared_resource_path);
98   return *this;
99 }
100
101 DirectoryInfo::Builder& DirectoryInfo::Builder::SetSharedTrustedPath(
102     std::string shared_trusted_path) {
103   shared_trusted_path_ = std::move(shared_trusted_path);
104   return *this;
105 }
106
107 DirectoryInfo::Builder& DirectoryInfo::Builder::SetResControlAllowedPath(
108     std::string res_control_allowed_res_path) {
109   res_control_allowed_res_path_ = std::move(res_control_allowed_res_path);
110   return *this;
111 }
112
113 DirectoryInfo::Builder& DirectoryInfo::Builder::SetResControlGlobalPath(
114     std::string res_control_global_res_path) {
115   res_control_global_res_path_ = std::move(res_control_global_res_path);
116   return *this;
117 }
118
119 DirectoryInfo::Builder::operator DirectoryInfo*() {
120   return new (std::nothrow) DirectoryInfo(std::move(root_path_),
121       std::move(data_path_), std::move(cache_path_),
122       std::move(resource_path_), std::move(tep_resource_path_),
123       std::move(shared_data_path_), std::move(shared_resource_path_),
124       std::move(shared_trusted_path_), std::move(res_control_allowed_res_path_),
125       std::move(res_control_global_res_path_));
126 }
127
128 DirectoryInfo* DirectoryInfo::Get(const std::string app_id,
129     uid_t uid) {
130   std::unique_ptr<AppInfo> info(AppInfo::Get(app_id, uid));
131   if (info.get() == nullptr)
132     return nullptr;
133
134   std::string root_path = info->GetRootPath();
135   std::string rw_path = GetRWPath(info->GetPkgId(), uid);
136   return Builder().SetRootPath(info->GetRootPath())
137       .SetDataPath(GetPath(rw_path, kDataDir))
138       .SetCachePath(GetPath(rw_path, kCacheDir))
139       .SetResourcePath(GetPath(root_path, kResourceDir))
140       .SetTepResourcePath(GetPath(root_path, kTepResourceDir))
141       .SetSharedDataPath(GetPath(rw_path, kSharedDataDir))
142       .SetSharedResourcePath(GetPath(root_path, kSharedResourceDir))
143       .SetSharedTrustedPath(GetPath(rw_path, kSharedTrustedDir))
144       .SetResControlAllowedPath(GetPath(root_path, kResControlAllowedDir))
145       .SetResControlGlobalPath(GetPath(root_path, kResControlGlobalDir));
146 }
147
148 DirectoryInfo* DirectoryInfo::Get() {
149   char app_id[256] = { 0, };
150   int ret = aul_app_get_appid_bypid(getpid(), app_id, sizeof(app_id));
151   if (ret != AUL_R_OK)
152     return nullptr;
153
154   return Get(app_id, getuid());
155 }
156
157 DirectoryInfo::DirectoryInfo(std::string root_path,
158     std::string data_path,
159     std::string cache_path,
160     std::string resource_path,
161     std::string tep_resource_path,
162     std::string shared_data_path,
163     std::string shared_resource_path,
164     std::string shared_trusted_path,
165     std::string res_control_allowed_res_path,
166     std::string res_control_global_res_path)
167     : root_path_(std::move(root_path)),
168       data_path_(std::move(data_path)),
169       cache_path_(std::move(cache_path)),
170       resource_path_(std::move(resource_path)),
171       tep_resource_path_(std::move(tep_resource_path)),
172       shared_data_path_(std::move(shared_data_path)),
173       shared_resource_path_(std::move(shared_resource_path)),
174       shared_trusted_path_(std::move(shared_trusted_path)),
175       res_control_allowed_res_path_(std::move(res_control_allowed_res_path)),
176       res_control_global_res_path_(std::move(res_control_global_res_path)) {
177 }
178
179 const std::string& DirectoryInfo::GetRootPath() const {
180   return root_path_;
181 }
182
183 const std::string& DirectoryInfo::GetDataPath() const {
184   return data_path_;
185 }
186
187 const std::string& DirectoryInfo::GetCachePath() const {
188   return cache_path_;
189 }
190
191 const std::string& DirectoryInfo::GetResourcePath() const {
192   return resource_path_;
193 }
194
195 const std::string& DirectoryInfo::GetTepResourcePath() const {
196   return tep_resource_path_;
197 }
198
199 const std::string& DirectoryInfo::GetSharedDataPath() const {
200   return shared_data_path_;
201 }
202
203 const std::string& DirectoryInfo::GetSharedResourcePath() const {
204   return shared_resource_path_;
205 }
206
207 const std::string& DirectoryInfo::GetSharedTrustedPath() const {
208   return shared_trusted_path_;
209 }
210
211 const std::string& DirectoryInfo::GetResControlAllowedResPath() const {
212   return res_control_allowed_res_path_;
213 }
214
215 const std::string& DirectoryInfo::GetResControlGlobalResPath() const {
216   return res_control_global_res_path_;
217 }
218
219 }  // namespace aul