Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / utils / paths.cc
1 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/utils/paths.h"
6
7 #include <manifest_parser/utils/logging.h>
8
9 #include <pwd.h>
10 #include <tzplatform_config.h>
11 #include <storage-internal.h>
12
13 #include <filesystem>
14 #include <vector>
15
16 #include "common/utils/user_util.h"
17
18 namespace ci = common_installer;
19 namespace fs = std::filesystem;
20
21 namespace {
22
23 const char kImageDir[] = ".image";
24 const char kBckExtension[] = ".bck";
25
26 std::filesystem::path GetBackupPath(const std::filesystem::path& pkg_path) {
27   fs::path backup_path = pkg_path;
28   backup_path += kBckExtension;
29   return backup_path;
30 }
31
32 }  // namespace
33
34 namespace common_installer {
35
36 std::filesystem::path GetBackupPathForPackagePath(
37     const std::filesystem::path& pkg_path) {
38   return GetBackupPath(pkg_path);
39 }
40
41 std::filesystem::path GetBackupPathForManifestFile(
42     const std::filesystem::path& manifest_path) {
43   return GetBackupPath(manifest_path);
44 }
45
46 std::filesystem::path GetBackupPathForIconFile(
47     const std::filesystem::path& icon_path) {
48   return GetBackupPath(icon_path);
49 }
50
51 std::string GetIconFileBackupExtension() {
52   return kBckExtension;
53 }
54
55 std::filesystem::path GetBackupPathForZipFile(const fs::path& zip_path) {
56   return GetBackupPath(zip_path);
57 }
58
59 std::filesystem::path GetMountLocation(const fs::path& pkg_path) {
60   return pkg_path / ".pkg";
61 }
62
63 std::filesystem::path GetZipPackageLocation(
64     const std::filesystem::path& pkg_path,
65     const std::string& pkgid) {
66   return pkg_path / kImageDir / pkgid;
67 }
68
69 std::filesystem::path GetExternalCardPath() {
70   char* sdpath = nullptr;
71   int storage_id = 0;
72   int ret;
73
74   ret = storage_get_primary_sdcard(&storage_id, &sdpath);
75   if (ret != STORAGE_ERROR_NONE) {
76     free(sdpath);
77     return fs::path();
78   }
79
80   if (sdpath) {
81     std::string mount_path(sdpath);
82     free(sdpath);
83     return fs::path(mount_path);
84   }
85
86   return fs::path();
87 }
88
89 std::filesystem::path GetExternalTepPath(RequestMode request_mode,
90                                            uid_t uid) {
91   fs::path result;
92   fs::path ext_mount_path = GetExternalCardPath();
93   if (fs::is_empty(ext_mount_path))
94     return result;
95   result = ext_mount_path / "tep";
96   if (request_mode == RequestMode::USER)
97     result /= ci::GetUsernameByUid(uid);
98   return result;
99 }
100
101 std::filesystem::path GetInternalTepPath(
102     const std::filesystem::path& pkg_path) {
103   return pkg_path / "tep";
104 }
105
106 std::filesystem::path GetIconPath(const fs::path& base_path,
107                       const std::string& pkgid,
108                       const fs::path& icon_filename,
109                       const fs::path& root_path) {
110   std::vector<fs::path> paths;
111   fs::path system_path = base_path / icon_filename;
112   fs::path small_system_path = base_path / "default" / "small" / icon_filename;
113   fs::path res_path = root_path / pkgid / "res" / "icons" / icon_filename;
114
115   paths.push_back(system_path);
116   paths.push_back(small_system_path);
117   paths.push_back(res_path);
118
119   for (auto& path : paths) {
120     if (fs::exists(path))
121       return path;
122   }
123
124   return {};
125 }
126
127 bool IsExtendedStorageAvailable() {
128   bool is_available = false;
129   int ret = storage_foreach_device_supported(
130       [](int, storage_type_e type, storage_state_e state,
131           const char* path, void* user_data) -> bool {
132         if (type != STORAGE_TYPE_EXTENDED_INTERNAL)
133           return true;
134         if (state != STORAGE_STATE_MOUNTED)
135           return true;
136         bool* available = static_cast<bool*>(user_data);
137         if (fs::exists(path))
138           *available = true;
139         // Stop iteration
140         return false;
141       },
142       &is_available);
143   if (ret != STORAGE_ERROR_NONE) {
144     LOG(ERROR) << "Failed to call storage_foreach_device_supported()";
145     return false;
146   }
147   return is_available;
148 }
149
150 }  // namespace common_installer