846eff908f3319648767b1f84a1422f20c9b61bc
[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
43 std::string GetRWPath(const std::string& pkg_id, uid_t uid) {
44   tzplatform_set_user(uid);
45   std::string path = std::string(tzplatform_getenv(TZ_USER_APP)) + "/" + pkg_id;
46   tzplatform_reset_user();
47   return path;
48 }
49
50 std::string GetPath(const std::string& path,
51     const std::string& dir_name) {
52   return path + "/" + dir_name;
53 }
54
55 }  // namespace
56
57 DirectoryInfo::Builder& DirectoryInfo::Builder::SetRootPath(
58     std::string root_path) {
59   root_path_ = std::move(root_path);
60   return *this;
61 }
62
63 DirectoryInfo::Builder& DirectoryInfo::Builder::SetDataPath(
64     std::string data_path) {
65   data_path_ = std::move(data_path);
66   return *this;
67 }
68
69 DirectoryInfo::Builder& DirectoryInfo::Builder::SetCachePath(
70     std::string cache_path) {
71   cache_path_ = std::move(cache_path);
72   return *this;
73 }
74
75 DirectoryInfo::Builder& DirectoryInfo::Builder::SetResourcePath(
76     std::string resource_path) {
77   resource_path_ = std::move(resource_path);
78   return *this;
79 }
80
81 DirectoryInfo::Builder& DirectoryInfo::Builder::SetTepResourcePath(
82     std::string tep_resource_path) {
83   tep_resource_path_ = std::move(tep_resource_path);
84   return *this;
85 }
86
87 DirectoryInfo::Builder& DirectoryInfo::Builder::SetSharedDataPath(
88     std::string shared_data_path) {
89   shared_data_path_ = std::move(shared_data_path);
90   return *this;
91 }
92
93 DirectoryInfo::Builder& DirectoryInfo::Builder::SetSharedResourcePath(
94     std::string shared_resource_path) {
95   shared_resource_path_ = std::move(shared_resource_path);
96   return *this;
97 }
98
99 DirectoryInfo::Builder& DirectoryInfo::Builder::SetSharedTrustedPath(
100     std::string shared_trusted_path) {
101   shared_trusted_path_ = std::move(shared_trusted_path);
102   return *this;
103 }
104
105 DirectoryInfo::Builder::operator DirectoryInfo*() {
106   return new (std::nothrow) DirectoryInfo(std::move(root_path_),
107       std::move(data_path_), std::move(cache_path_),
108       std::move(resource_path_), std::move(tep_resource_path_),
109       std::move(shared_data_path_), std::move(shared_resource_path_),
110       std::move(shared_trusted_path_));
111 }
112
113 DirectoryInfo* DirectoryInfo::Get(const std::string app_id,
114     uid_t uid) {
115   std::unique_ptr<AppInfo> info(AppInfo::Get(app_id, uid));
116   if (info.get() == nullptr)
117     return nullptr;
118
119   std::string root_path = info->GetRootPath();
120   std::string rw_path = GetRWPath(info->GetPkgId(), uid);
121   return Builder().SetRootPath(info->GetRootPath())
122       .SetDataPath(GetPath(rw_path, kDataDir))
123       .SetCachePath(GetPath(rw_path, kCacheDir))
124       .SetResourcePath(GetPath(root_path, kResourceDir))
125       .SetTepResourcePath(GetPath(root_path, kTepResourceDir))
126       .SetSharedDataPath(GetPath(rw_path, kSharedDataDir))
127       .SetSharedResourcePath(GetPath(root_path, kSharedResourceDir))
128       .SetSharedTrustedPath(GetPath(rw_path, kSharedTrustedDir));
129 }
130
131 DirectoryInfo* DirectoryInfo::Get() {
132   char app_id[256] = { 0, };
133   int ret = aul_app_get_appid_bypid(getpid(), app_id, sizeof(app_id));
134   if (ret != AUL_R_OK)
135     return nullptr;
136
137   return Get(app_id, getuid());
138 }
139
140 DirectoryInfo::DirectoryInfo(std::string root_path,
141     std::string data_path,
142     std::string cache_path,
143     std::string resource_path,
144     std::string tep_resource_path,
145     std::string shared_data_path,
146     std::string shared_resource_path,
147     std::string shared_trusted_path)
148     : root_path_(std::move(root_path)),
149       data_path_(std::move(data_path)),
150       cache_path_(std::move(cache_path)),
151       resource_path_(std::move(resource_path)),
152       tep_resource_path_(std::move(tep_resource_path)),
153       shared_data_path_(std::move(shared_data_path)),
154       shared_resource_path_(std::move(shared_resource_path)),
155       shared_trusted_path_(std::move(shared_trusted_path)) {
156 }
157
158 const std::string& DirectoryInfo::GetRootPath() const {
159   return root_path_;
160 }
161
162 const std::string& DirectoryInfo::GetDataPath() const {
163   return data_path_;
164 }
165
166 const std::string& DirectoryInfo::GetCachePath() const {
167   return cache_path_;
168 }
169
170 const std::string& DirectoryInfo::GetResourcePath() const {
171   return resource_path_;
172 }
173
174 const std::string& DirectoryInfo::GetTepResourcePath() const {
175   return tep_resource_path_;
176 }
177
178 const std::string& DirectoryInfo::GetSharedDataPath() const {
179   return shared_data_path_;
180 }
181
182 const std::string& DirectoryInfo::GetSharedResourcePath() const {
183   return shared_resource_path_;
184 }
185
186 const std::string& DirectoryInfo::GetSharedTrustedPath() const {
187   return shared_trusted_path_;
188 }
189
190 }  // namespace aul