+
+std::vector<std::string> getLibrariesInfo(const std::string& rootPath)
+{
+ std::vector<std::string> LibrariesInfo;
+ std::string binDir = concatPath(rootPath, "bin");
+ if (!exist(binDir))
+ return LibrariesInfo;
+
+ auto convert = [&LibrariesInfo](const std::string& filepath, const std::string& filename) {
+ if (filename.find(".so", filename.size() - 3) != std::string::npos || filepath.rfind(".so.") != std::string::npos) {
+ std::string buffer = SHA256(filepath);
+ LibrariesInfo.push_back(filepath + ":" + buffer);
+ _INFO("Library : [%s] / SHA256 : [%s]", filename.c_str(), buffer.c_str());
+ }
+ };
+ scanFilesInDirectory(binDir, convert, -1);
+
+ return LibrariesInfo;
+}
+
+// callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
+static int tlc_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
+{
+ char *pkgId = NULL;
+ char *root = NULL;
+ std::string rootPath;
+
+ int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
+ if (ret != PMINFO_R_OK) {
+ _SERR("Failed to get pkgid");
+ return -1;
+ }
+
+ ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
+ if (ret != PMINFO_R_OK) {
+ _SERR("Failed to get root path");
+ return -1;
+ }
+ rootPath = std::string(root);
+
+ for (auto& librarySha : getLibrariesInfo(rootPath)) {
+ std::string library = librarySha.substr(0, librarySha.find(':'));
+ if (exist(library)) {
+ std::string fileSha = library.substr(library.rfind('/') + 1) + ".." + librarySha.substr(librarySha.find(':') + 1);
+ char *sql = sqlite3_mprintf("INSERT INTO TLC (PKGID, LIBRARY) VALUES (%Q, %Q);", pkgId, fileSha.c_str());
+ insertDB(tlc_db, sql);
+ restore_library.push_back(fileSha);
+ sqlite3_free(sql);
+ }
+ }
+ return 0;
+}
+
+tac_error_e tlc_restoreDB()
+{
+ if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
+ _SERR("Failed to remove of %s", TLC_APP_LIST_RESTORE_DB);
+ return TAC_ERROR_UNKNOWN;
+ }
+
+ std::string dbRestoreJournal = TLC_APP_LIST_RESTORE_DB + std::string("-journal");
+ if (!removeFile(dbRestoreJournal)) {
+ _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
+ return TAC_ERROR_UNKNOWN;
+ }
+
+ tlc_db = createDB(TLC_APP_LIST_RESTORE_DB, CREATE_TLC_DB_TABLE);
+ if (!tlc_db) {
+ _SERR("Sqlite create error");
+ return TAC_ERROR_UNKNOWN;
+ }
+ sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
+
+ pkgmgrinfo_appinfo_metadata_filter_h handle;
+ int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
+ if (ret != PMINFO_R_OK) {
+ return TAC_ERROR_UNKNOWN;
+ }
+
+ ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE_TRUE);
+ if (ret != PMINFO_R_OK) {
+ pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
+ return TAC_ERROR_UNKNOWN;
+ }
+
+ ret = pkgmgrMDFilterForeach(handle, tlc_restoreDBCb, NULL);
+ if (ret != 0) {
+ pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
+ return TAC_ERROR_UNKNOWN;
+ }
+
+ pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
+
+ if (tlc_db) {
+ closeDB(tlc_db);
+ tlc_db = NULL;
+ }
+
+ if (!copyFile(TLC_APP_LIST_RESTORE_DB, TLC_APP_LIST_DB)) {
+ _SERR("Failed to copy of %s", TLC_APP_LIST_DB);
+ return TAC_ERROR_UNKNOWN;
+ }
+ if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
+ _SERR("Failed to remove of %s", TLC_APP_LIST_RESTORE_DB);
+ return TAC_ERROR_UNKNOWN;
+ }
+
+ std::string dbJournal = TLC_APP_LIST_DB + std::string("-journal");
+ if (!copyFile(dbRestoreJournal, dbJournal)) {
+ _SERR("Failed to copy of %s", dbJournal.c_str());
+ return TAC_ERROR_UNKNOWN;
+ }
+ if (!removeFile(dbRestoreJournal)) {
+ _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
+ return TAC_ERROR_UNKNOWN;
+ }
+
+ auto convert = [](const std::string& path, const std::string& filename) {
+ bool isExist = false;
+ for (auto& library : restore_library) {
+ if (!strcmp(filename.c_str(), library.c_str())) {
+ isExist = true;
+ break;
+ }
+ }
+ if (!isExist) {
+ if (!removeFile(path)) {
+ _ERR("Failed to remove of %s", path.c_str());
+ }
+ }
+ };
+
+ scanFilesInDirectory(TLC_LIBRARIES_DIR, convert, 0);
+
+ return TAC_ERROR_NONE;
+}