2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <pkgmgr-info.h>
24 #include <pkgmgr_installer_info.h>
25 #include <sys/smack.h>
30 #include <unordered_map>
38 #include "path_manager.h"
40 static bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length)
42 return static_cast<int>(a.length()) - length >= aOffset &&
43 static_cast<int>(b.length()) - length >= bOffset &&
44 std::equal(b.begin() + bOffset, b.begin() + bOffset + length, a.begin() + aOffset,
45 [](unsigned char a, unsigned char b)
46 { return std::tolower(a) == std::tolower(b); });
49 bool isManagedAssembly(const std::string& fileName)
51 return (iCompare(fileName, fileName.size()-4, ".dll", 0, 4) ||
52 iCompare(fileName, fileName.size()-4, ".exe", 0, 4)) &&
53 !isNativeImage(fileName);
56 bool isNativeImage(const std::string& fileName)
58 return iCompare(fileName, fileName.size()-7, ".ni", 0, 3);
61 bool cmdOptionExists(char** begin, char** end, const std::string& option)
63 return std::find(begin, end, option) != end;
66 std::string readSelfPath()
69 ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
72 return std::string(buff);
78 std::string concatPath(const std::string& path1, const std::string& path2)
80 std::string path(path1);
81 if (path.back() == PATH_SEPARATOR) {
84 path += PATH_SEPARATOR;
91 void splitPath(const std::string& path, std::vector<std::string>& out)
93 std::istringstream ss(path);
96 while (std::getline(ss, token, ':')) {
101 std::string absolutePath(const std::string& path)
104 char realPath[PATH_MAX];
105 if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
106 absPath.assign(realPath);
111 int getRootPath(std::string pkgId, std::string& rootPath)
117 if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
118 _ERR("Failed to get UID");
122 pkgmgrinfo_pkginfo_h handle;
124 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
125 if (ret != PMINFO_R_OK) {
129 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
130 if (ret != PMINFO_R_OK) {
135 ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
136 if (ret != PMINFO_R_OK) {
137 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
141 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
145 std::string baseName(const std::string& path)
147 auto pos = path.find_last_of(PATH_SEPARATOR);
148 if (pos != std::string::npos)
149 return path.substr(0, pos);
151 return std::string(".");
155 bool isFileExist(const std::string& path)
158 return stat(path.c_str(), &sb) == 0;
161 std::string stripNiDLL(const std::string& path)
163 std::string niPath(path);
164 if (path.size() < 5) return niPath;
165 if (!strncasecmp(path.c_str() + path.size() - 4, ".dll", 4))
166 niPath = path.substr(0, path.size()-4);
167 else if (!strncasecmp(path.c_str() + path.size() - 4, ".exe", 4))
168 niPath = path.substr(0, path.size()-4);
170 if (!strncasecmp(niPath.c_str() + niPath.size() - 3, ".ni", 3))
171 return niPath.substr(0, niPath.size()-3);
176 void assembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList)
178 std::map<std::string, std::string> assemblyList;
179 std::map<std::string, std::string> tmpList;
181 auto reader = [&assemblyList, &tmpList] (const std::string& path, const char* name) {
182 if (isManagedAssembly(path) || isNativeImage(path)) {
183 std::string dllName = stripNiDLL(name);
184 std::pair<std::map<std::string, std::string>::iterator, bool> ret;
185 ret = tmpList.insert(std::pair<std::string, std::string>(dllName, path));
186 if (ret.second == false) {
187 if (isNativeImage(path))
188 tmpList[dllName] = path;
193 for (auto directory : directories) {
194 scanFilesInDir(directory.c_str(), reader, 1);
195 // merge scaned dll list to tpa list.
196 // if the dll is already exist in the list, that is skipped.
197 assemblyList.insert(tmpList.begin(), tmpList.end());
200 std::map<std::string, std::string>::iterator it;
201 for (it = assemblyList.begin(); it != assemblyList.end(); it++)
202 tpaList += it->second + ':';
204 if (tpaList.back() == ':')
208 void scanFilesInDir(const std::string& directory, FileReader reader, unsigned int depth)
211 struct dirent* entry;
214 if (strstr(directory.c_str(), TAC_SYMLINK_SUB_DIR) != NULL)
215 return; // skip nitool --regen-all-app (--r2r)
217 dir = opendir(directory.c_str());
222 std::vector<std::string> innerDirectories;
224 while ((entry = readdir(dir)) != nullptr) {
226 std::string path = concatPath(directory, entry->d_name);
227 switch (entry->d_type) {
235 if (stat(path.c_str(), &sb) == -1)
238 if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
244 reader(path, entry->d_name);
245 else if (depth > 1 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
246 innerDirectories.push_back(path);
250 for (auto& d : innerDirectories)
251 scanFilesInDir(d.c_str(), reader, depth - 1);
256 void updateAssemblyInfo(const std::string& getPath, const std::string& setPath)
260 // change smack label
261 if (smack_getlabel(getPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
262 if (smack_setlabel(setPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
263 fprintf(stderr, "Fail to set smack label\n");
268 // change owner and groups for generated ni file.
270 if (!stat(getPath.c_str(), &info)) {
271 if (chown(setPath.c_str(), info.st_uid, info.st_gid) == -1)
272 fprintf(stderr, "Failed to change owner and group name\n");
276 static bool setOwnership(const bf::path& path, uid_t uid, gid_t gid) {
277 int fd = open(path.c_str(), O_RDONLY);
279 _ERR("Can't open directory: %s", path.c_str());
282 int ret = fchown(fd, uid, gid);
285 _ERR("Failed to change owner of: %s", path.c_str());
291 static bool setDirPermissions(const bf::path& path, bf::perms permissions) {
292 bs::error_code error;
293 bf::permissions(path, permissions, error);
295 _ERR("Failed to set permissions for directory: %s, %s", path.c_str(), error.message().c_str());
301 static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permissions, uid_t uid, gid_t gid) {
302 if (!setOwnership(path, uid, gid)) {
303 _ERR("Failed to change owner: %s, (uid: %d, gid: %d)", path.c_str(), uid, gid);
306 if (!setDirPermissions(path, permissions)) {
307 _ERR("Failed to change permission: %s, (%d)", path.c_str(), permissions);
313 static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2) {
314 if (!bf::exists(path)) {
315 _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
318 bf::perms permissions = bf::status(path).permissions();
320 if (stat(path.c_str(), &stats) != 0) {
323 if (!setDirOwnershipAndPermissions(path2, permissions, stats.st_uid, stats.st_gid)) {
324 _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
330 bool createDir(const bf::path& path) {
331 if (bf::exists(path)) {
334 bs::error_code error;
335 bf::create_directories(path, error);
337 _ERR("Failed to create directory: %s", error.message().c_str());
343 bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags) {
345 // Check whether the function call is valid
346 if (!bf::exists(path1) || !bf::is_directory(path1)) {
347 _ERR("Source directory %s does not exist or is not a directory", path1.c_str());
350 if (!bf::exists(path2)) {
351 // Create the destination directory
352 if (!createDir(path2)) {
353 _ERR("Unable to create destination directory %s", path2.c_str());
356 if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
357 copyOwnershipAndPermissions(path1, path2);
360 if (!(flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE))) {
361 _ERR("Destination directory %s already exists", path2.c_str());
364 if (flags & (FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)) {
365 copyOwnershipAndPermissions(path1, path2);
368 } catch (const bf::filesystem_error& error) {
369 _ERR("Failed to copy directory: %s", error.what());
373 // Iterate through the source directory
374 for (bf::directory_iterator file(path1); file != bf::directory_iterator(); ++file) {
376 bf::path current(file->path());
377 bf::path target = path2 / current.filename();
378 if (bf::is_symlink(symlink_status(current))) {
379 if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) && bf::exists(target)) {
382 bs::error_code error;
383 bf::copy_symlink(current, target, error);
385 _ERR("Failed to copy symlink: %s, %s", current.c_str(), error.message().c_str());
388 } else if (bf::is_directory(current)) {
389 // Found directory: Recursion
390 if (!copyDir(current, target, flags)) {
394 if ((flags & FS_MERGE_SKIP) && bf::exists(target)) {
397 bf::path destination = target;
398 if (flags & FS_COMMIT_COPY_FILE) {
399 destination = bf::unique_path(target.parent_path() / "%%%%-%%%%-%%%%-%%%%");
401 if (flags & FS_MERGE_OVERWRITE) {
402 bf::copy_file(current, destination, bf::copy_option::overwrite_if_exists);
404 bf::copy_file(current, destination);
406 if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
407 copyOwnershipAndPermissions(current, destination);
409 if (flags & FS_COMMIT_COPY_FILE) {
410 if (flags & FS_MERGE_OVERWRITE) {
413 bf::rename(destination, target);
416 } catch (const bf::filesystem_error& error) {
417 _ERR("Failed to copy directory: %s", error.what());
424 bool copyFile(const bf::path& path1, const bf::path& path2) {
425 bs::error_code error;
426 bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
428 _ERR("copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
434 bool moveFile(const bf::path& path1, const bf::path& path2) {
435 if (bf::exists(path2)) {
438 bs::error_code error;
439 bf::rename(path1, path2, error);
441 _ERR("Cannot move file: %s. Will copy/remove... with error [%s]", path1.c_str(), error.message().c_str());
442 bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
444 _ERR("Cannot copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
447 bf::remove_all(path1, error);
449 _ERR("Cannot remove old file when coping: %s with error [%s]", path1.c_str(), error.message().c_str());
456 bool removeFile(const bf::path& path) {
457 if (!bf::exists(path)) {
460 bs::error_code error;
461 bf::remove(path, error);
463 _ERR("Cannot remove: %s, %s", path.c_str(), error.message().c_str());
469 bool removeAll(const bf::path& path) {
473 bs::error_code error;
474 bf::remove_all(path, error);
476 _ERR("Cannot remove: %s, %s", path.c_str(), error.message().c_str());