+}
+
+void copySmackAndOwnership(const std::string& fromPath, const std::string& toPath, bool isSymlink)
+{
+ char* label = NULL;
+ struct stat info;
+
+ if (isSymlink) {
+ // change smack label for symbolic link.
+ if (smack_lgetlabel(fromPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
+ if (smack_lsetlabel(toPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
+ _SERR("Fail to set smack label");
+ }
+ free(label);
+ }
+
+ // change owner and groupsfor symbolic link.
+ // change mode is skipped for symlink because permission of symlink file is meaningless.
+ if (!lstat(fromPath.c_str(), &info)) {
+ if (lchown(toPath.c_str(), info.st_uid, info.st_gid) == -1)
+ _SERR("Failed to change owner and group name");
+ }
+ } else {
+ // change smack label
+ if (smack_getlabel(fromPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
+ if (smack_setlabel(toPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
+ _SERR("Fail to set smack label");
+ }
+ free(label);
+ }
+
+ // change owner, groups and mode for generated ni file.
+ if (!stat(fromPath.c_str(), &info)) {
+ if (chown(toPath.c_str(), info.st_uid, info.st_gid) == -1)
+ _SERR("Failed to change owner and group name");
+ if (chmod(toPath.c_str(), info.st_mode) == -1)
+ _SERR("Failed to change mode");
+ }
+ }
+}
+
+static bool setOwnership(const bf::path& path, uid_t uid, gid_t gid)
+{
+ int fd = open(path.c_str(), O_RDONLY);
+ if (fd < 0) {
+ _ERR("Can't open directory: %s", path.c_str());
+ return false;
+ }
+ int ret = fchown(fd, uid, gid);
+ close(fd);
+ if (ret != 0) {
+ _ERR("Failed to change owner of: %s", path.c_str());
+ return false;
+ }
+ return true;
+}
+
+static bool setDirPermissions(const bf::path& path, bf::perms permissions)
+{
+ bs::error_code error;
+ bf::permissions(path, permissions, error);
+ if (error) {
+ _ERR("Failed to set permissions for directory: %s, %s", path.c_str(), error.message().c_str());
+ return false;
+ }
+ return true;
+}
+
+static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permissions, uid_t uid, gid_t gid)
+{
+ if (!setOwnership(path, uid, gid)) {
+ _ERR("Failed to change owner: %s, (uid: %d, gid: %d)", path.c_str(), uid, gid);
+ return false;
+ }
+ if (!setDirPermissions(path, permissions)) {
+ _ERR("Failed to change permission: %s, (%d)", path.c_str(), permissions);
+ return false;
+ }
+ return true;
+}
+
+static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2)
+{
+ if (!exist(path)) {
+ _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
+ return false;
+ }
+ bs::error_code error;
+ bf::perms permissions = bf::status(path, error).permissions();
+ if (error) {
+ _ERR("Failed to copy ownership and permissions : %s", error.message().c_str());
+ return false;
+ }
+ struct stat stats;
+ if (stat(path.c_str(), &stats) != 0) {
+ return false;
+ }
+ if (!setDirOwnershipAndPermissions(path2, permissions, stats.st_uid, stats.st_gid)) {
+ _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
+ return false;
+ }
+ return true;
+}
+
+bool exist(const bf::path& path)
+{
+ bs::error_code error;
+ int ret = bf::exists(path, error);
+ if (error) {
+ if ((error.value() != bs::errc::success) && (error.value() != bs::errc::no_such_file_or_directory)) {
+ _ERR("Failed to check %s exists : %s", path.c_str(), error.message().c_str());
+ }
+ }
+ return ret;
+}
+
+bool createDir(const bf::path& path)
+{
+ if (exist(path)) {
+ return true;
+ }
+ bs::error_code error;
+ bf::create_directories(path, error);
+ if (error) {
+ _ERR("Failed to create directory: %s", error.message().c_str());
+ return false;
+ }
+ return true;
+}
+
+bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags)
+{
+ try {
+ // Check whether the function call is valid
+ if (!exist(path1) || !bf::is_directory(path1)) {
+ _ERR("Source directory %s does not exist or is not a directory", path1.c_str());
+ return false;
+ }
+ if (!exist(path2)) {
+ // Create the destination directory
+ if (!createDir(path2)) {
+ _ERR("Unable to create destination directory %s", path2.c_str());
+ return false;
+ }
+ if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
+ copyOwnershipAndPermissions(path1, path2);
+ }
+ } else {
+ if (!(flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE))) {
+ _ERR("Destination directory %s already exists", path2.c_str());
+ return false;
+ }
+ if (flags & (FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)) {
+ copyOwnershipAndPermissions(path1, path2);
+ }
+ }
+ } catch (const bf::filesystem_error& error) {
+ _ERR("Failed to copy directory: %s", error.what());
+ return false;
+ }
+
+ // Iterate through the source directory
+ try {
+ for (bf::directory_iterator file(path1); file != bf::directory_iterator(); ++file) {
+ bf::path current(file->path());
+ bf::path target = path2 / current.filename();
+ if (bf::is_symlink(bf::symlink_status(current))) {
+ if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) && exist(target)) {
+ continue;
+ }
+ bs::error_code error;
+ bf::copy_symlink(current, target, error);
+ if (error) {
+ _ERR("Failed to copy symlink: %s, %s", current.c_str(), error.message().c_str());
+ return false;
+ }
+ } else if (bf::is_directory(current)) {
+ // Found directory: Recursion
+ if (!copyDir(current, target, flags)) {
+ return false;
+ }
+ } else {
+ if ((flags & FS_MERGE_SKIP) && exist(target)) {
+ continue;
+ }
+ bf::path destination = target;
+ if (flags & FS_COMMIT_COPY_FILE) {
+ destination = bf::unique_path(target.parent_path() / "%%%%-%%%%-%%%%-%%%%");
+ }
+ if (flags & FS_MERGE_OVERWRITE) {
+ bf::copy_file(current, destination, bf::copy_option::overwrite_if_exists);
+ } else {
+ bf::copy_file(current, destination);
+ }
+ if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
+ copyOwnershipAndPermissions(current, destination);
+ }
+ if (flags & FS_COMMIT_COPY_FILE) {
+ if (flags & FS_MERGE_OVERWRITE) {
+ bf::remove(target);
+ }
+ bf::rename(destination, target);
+ }
+ }
+ }
+ } catch (const bf::filesystem_error& error) {
+ _ERR("Failed to copy directory: %s", error.what());
+ return false;
+ }
+
+ return true;
+}
+
+bool copyFile(const bf::path& path1, const bf::path& path2)
+{
+ bs::error_code error;
+ if (!exist(path1)) {
+ return false;
+ }
+ bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
+ if (error) {
+ _ERR("copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
+ return false;
+ }
+ return true;
+}
+
+bool moveFile(const bf::path& path1, const bf::path& path2)
+{
+ if (!exist(path1) || exist(path2)) {
+ return false;
+ }
+ bs::error_code error;
+ bf::rename(path1, path2, error);
+ if (error) {
+ _ERR("Cannot move file: %s. Will copy/remove... with error [%s]", path1.c_str(), error.message().c_str());
+ bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
+ if (error) {
+ _ERR("Cannot copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
+ return false;
+ }
+ bf::remove_all(path1, error);
+ if (error) {
+ _ERR("Cannot remove old file when coping: %s with error [%s]", path1.c_str(), error.message().c_str());
+ return false;
+ }
+ }
+ return true;
+}
+
+bool removeFile(const bf::path& path)
+{
+ if (!exist(path)) {
+ return true;
+ }
+ bs::error_code error;
+ bf::remove(path, error);
+ if (error) {
+ _ERR("Cannot remove: %s, %s", path.c_str(), error.message().c_str());
+ return false;
+ }
+ return true;
+}
+
+bool removeAll(const bf::path& path)
+{
+ if (!exist(path)) {
+ return true;
+ }
+ bs::error_code error;
+ bf::remove_all(path, error);
+ if (error) {
+ _ERR("Cannot remove: %s, %s", path.c_str(), error.message().c_str());
+ return false;
+ }
+ return true;
+}
+
+void setCmdName(const std::string& name)
+{
+ #define PRC_NAME_LENGTH 16
+
+ char processName[PRC_NAME_LENGTH] = {0, };
+
+ if (name.empty())
+ return;
+
+ memset(processName, '\0', PRC_NAME_LENGTH);
+ snprintf(processName, PRC_NAME_LENGTH, "%s", name.c_str());
+ prctl(PR_SET_NAME, processName);
+}
+
+std::string getFileName(const std::string& path)
+{
+ std::string ret(path);
+ size_t index = ret.find_last_of(PATH_SEPARATOR);
+ return index == std::string::npos ? ret : ret.substr(index + 1);
+}
+
+std::string SHA256(const std::string& path)
+{
+ std::string output = "";
+ FILE *file = fopen(path.c_str(), "rb");
+ if (!file) {
+ return output;
+ }
+
+ unsigned char hash[SHA256_DIGEST_LENGTH];
+ SHA256_CTX sha256;
+ SHA256_Init(&sha256);
+ int bytesRead = 0;
+ const int bufSize = 32768;
+ char *buffer = (char*)malloc(bufSize);
+ if (!buffer) {
+ fclose(file);
+ return output;
+ }
+
+ while ((bytesRead = fread(buffer, 1, bufSize, file))) {
+ SHA256_Update(&sha256, buffer, bytesRead);
+ }
+ SHA256_Final(hash, &sha256);
+
+ std::stringstream ss;
+ for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
+ ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
+ }
+ output = ss.str();
+
+ fclose(file);
+ free(buffer);
+
+ return output;
+}
+
+int pkgmgrGetPkgInfo(const std::string& pkgId, pkgmgrinfo_pkginfo_h* handle)
+{
+ uid_t uid = 0;
+ int ret = 0;
+
+ if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
+ _ERR("Failed to get UID");
+ return -1;
+ }
+
+ ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, handle);
+ if (ret != PMINFO_R_OK) {
+ _ERR("Failed to get pkginfo (%d)", ret);
+ return -1;
+ }
+
+ return 0;
+}
+
+int pkgmgrGetAppInfo(const std::string& appId, pkgmgrinfo_appinfo_h* handle)
+{
+ uid_t uid = 0;
+ int ret = 0;
+
+ if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
+ _ERR("Failed to get UID");
+ return -1;
+ }
+
+ ret = pkgmgrinfo_appinfo_get_usr_appinfo(appId.c_str(), uid, handle);
+ if (ret != PMINFO_R_OK) {
+ _ERR("Failed to get appinfo (%d)", ret);
+ return -1;
+ }
+
+ return 0;
+}
+
+int pkgmgrMDFilterForeach(pkgmgrinfo_appinfo_metadata_filter_h handle,
+ pkgmgrinfo_app_list_cb app_cb,
+ void *user_data)
+{
+ uid_t uid = 0;
+ int ret = 0;
+
+ if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
+ _ERR("Failed to get UID");
+ return -1;
+ }
+
+
+ ret = pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle, app_cb, user_data, uid);
+ if (ret != PMINFO_R_OK) {
+ _ERR("Failed to execute the metadata filter query (%d)", ret);
+ return -1;
+ }
+
+ return 0;
+}
+
+void printHWClockLog(const char* format, ...)
+{
+ char buf[1024] = {0,};
+ va_list ap;
+
+ va_start(ap, format);
+ vsnprintf(buf, sizeof(buf), format, ap);
+ va_end(ap);
+
+ prctl(PR_TASK_PERF_USER_TRACE, buf, strlen(buf));
+}
+