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