2 * Copyright (c) 2019 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.
18 #include <pkgmgr-info.h>
19 #include <pkgmgr_installer_info.h>
23 #include "ni_common.h"
24 #include "tac_common.h"
25 #include "db_manager.h"
26 #include "path_manager.h"
31 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
34 #define __STR(x) __XSTR(x)
35 static const char* __TAC_DIR = __STR(TAC_DIR);
39 static sqlite3 *tac_db = NULL;
40 std::vector<std::string> restoreNuget;
42 void cleanupDirectory()
44 std::vector<std::string> removeNuget;
45 for (auto& nuget : bf::recursive_directory_iterator(__TAC_DIR)) {
47 std::string nugetPath = nuget.path().string();
48 for (auto& restore : restoreNuget) {
49 if (!bf::is_directory(nugetPath)) {
52 if (strstr(nugetPath.c_str(), restore.c_str()) != NULL) {
58 removeNuget.push_back(nugetPath);
62 for (auto& rm : removeNuget) {
64 _ERR("Failed to remove of %s", rm.c_str());
70 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
71 static int restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
79 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
80 if (ret != PMINFO_R_OK) {
81 fprintf(stderr, "Failed to get pkgid\n");
85 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
86 if (ret != PMINFO_R_OK) {
87 fprintf(stderr, "Failed to get root path\n");
92 ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
93 if (ret != PMINFO_R_OK) {
94 fprintf(stderr, "Failed to get exec name\n");
97 execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
99 std::vector<std::string> parserData;
100 for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
101 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
102 parserData.push_back(nugetPackage);
104 std::sort(parserData.begin(), parserData.end());
105 parserData.erase(unique(parserData.begin(), parserData.end()), parserData.end());
107 for (auto& nuget : parserData) {
109 std::string name = nuget.substr(0, nuget.find('/'));
110 std::string version = nuget.substr(nuget.rfind('/') + 1);
111 std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
112 "VALUES ('" + std::string(pkgId) + "', '" + nuget + "', '" + name + "', '" + version + "');";
113 dbInsert(tac_db, TAC_APP_LIST_RESTORE_DB, sql);
114 restoreNuget.push_back(concatPath(__TAC_DIR, name));
121 tac_error_e restoreTACDB()
123 if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
124 fprintf(stderr, "Failed to remove of %s", TAC_APP_LIST_RESTORE_DB);
125 return TAC_ERROR_UNKNOWN;
128 std::string dbRestoreJournal = TAC_APP_LIST_RESTORE_DB + std::string("-journal");
129 if (!removeFile(dbRestoreJournal)) {
130 fprintf(stderr, "Failed to remove of %s", dbRestoreJournal.c_str());
131 return TAC_ERROR_UNKNOWN;
134 if (initializePathManager(std::string(), std::string(), std::string())) {
135 fprintf(stderr, "Fail to initialize PathManger");
136 return TAC_ERROR_UNKNOWN;
139 tac_db = dbCreate(TAC_APP_LIST_RESTORE_DB);
141 if (!dbOpen(tac_db, TAC_APP_LIST_RESTORE_DB)) {
142 return TAC_ERROR_UNKNOWN;
145 return TAC_ERROR_UNKNOWN;
148 pkgmgrinfo_appinfo_metadata_filter_h handle;
149 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
150 if (ret != PMINFO_R_OK) {
151 return TAC_ERROR_UNKNOWN;
154 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
155 if (ret != PMINFO_R_OK) {
156 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
157 return TAC_ERROR_UNKNOWN;
160 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, restoreDBCb, NULL);
161 if (ret != PMINFO_R_OK) {
162 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
163 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
164 return TAC_ERROR_UNKNOWN;
166 fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
168 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
175 if (!copyFile(TAC_APP_LIST_RESTORE_DB, TAC_APP_LIST_DB)) {
176 fprintf(stderr, "Failed to copy of %s", TAC_APP_LIST_DB);
177 return TAC_ERROR_UNKNOWN;
179 if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
180 fprintf(stderr, "Failed to remove of %s", TAC_APP_LIST_RESTORE_DB);
181 return TAC_ERROR_UNKNOWN;
184 std::string dbJournal = TAC_APP_LIST_DB + std::string("-journal");
185 if (!copyFile(dbRestoreJournal, dbJournal)) {
186 fprintf(stderr, "Failed to copy of %s", dbJournal.c_str());
187 return TAC_ERROR_UNKNOWN;
189 if (!removeFile(dbRestoreJournal)) {
190 fprintf(stderr, "Failed to remove of %s", dbRestoreJournal.c_str());
191 return TAC_ERROR_UNKNOWN;
195 return TAC_ERROR_NONE;
198 tac_error_e resetTACPackage(const std::string& pkgId)
201 if (getRootPath(pkgId, pkgRoot) < 0) {
202 return TAC_ERROR_INVALID_PACKAGE;
205 std::vector<std::string> tacNativeImage;
206 std::string binDir = concatPath(pkgRoot, "bin");
207 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
208 if (bf::exists(tacDir)) {
209 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
210 std::string symPath = symlinkAssembly.path().string();
211 if (bf::is_symlink(symPath)) {
212 if (isNativeImage(symPath)) {
213 tacNativeImage.push_back(symPath);
217 for (auto& path : tacNativeImage) {
218 if (!removeFile(path)) {
219 fprintf(stderr, "Failed to remove of %s", path.c_str());
220 return TAC_ERROR_UNKNOWN;
224 tacNativeImage.clear();
225 return TAC_ERROR_NONE;
228 tac_error_e createTACPackage(const std::string& pkgId)
231 if (getRootPath(pkgId, pkgRoot) < 0) {
232 return TAC_ERROR_INVALID_PACKAGE;
235 std::string binDir = concatPath(pkgRoot, "bin");
236 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
237 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
238 if (bf::exists(tacDir)) {
239 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
240 std::string symPath = symlinkAssembly.path().string();
241 if (bf::is_symlink(symPath)) {
242 if (!isNativeImage(symPath)) {
243 std::string originPath = bf::read_symlink(symPath).string();
244 std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
245 if (!bf::exists(originNiPath)) {
246 if(createNiDll(originPath, false) != NI_ERROR_NONE) {
247 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
248 return TAC_ERROR_UNKNOWN;
251 std::string symNIPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
252 if (!bf::exists(symNIPath)) {
253 bf::create_symlink(originNiPath, symNIPath);
254 fprintf(stderr, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
255 updateAssemblyInfo(tacDir.c_str(), symNIPath.c_str(), true);
257 std::string NIFileName = symNIPath.substr(symNIPath.rfind('/') + 1);
258 if (!removeFile(concatPath(binNIDir, NIFileName))) {
259 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
260 return TAC_ERROR_UNKNOWN;
267 return TAC_ERROR_NONE;
270 tac_error_e regenerateTAC()
272 const std::string tacDir[] = {__TAC_DIR};
273 removeNiUnderDirs(tacDir, 1);
275 auto convert = [](const std::string& path, std::string name) {
276 if (strstr(path.c_str(), TAC_APP_LIST_DB) != NULL ||
277 strstr(path.c_str(), TAC_APP_LIST_RESTORE_DB) != NULL ||
278 strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
280 if(createNiDll(path, false) != NI_ERROR_NONE) {
281 fprintf(stderr, "Failed to create NI file [%s]\n", path.c_str());
285 scanFilesInDir(tacDir[0], convert, -1);
286 return TAC_ERROR_NONE;
289 tac_error_e disableTACPackage(const std::string& pkgId)
292 if (getRootPath(pkgId, pkgRoot) < 0) {
293 return TAC_ERROR_INVALID_PACKAGE;
296 std::string binDir = concatPath(pkgRoot, "bin");
297 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
298 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
299 if (bf::exists(tacDir)) {
300 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
301 std::string symPath = symlinkAssembly.path().string();
302 std::string fileName = symlinkAssembly.path().filename().string();
303 if (bf::is_symlink(symPath)) {
304 std::string originPath = bf::read_symlink(symPath).string();
305 if (!isNativeImage(symPath)) {
306 if (!copyFile(originPath, concatPath(binDir, fileName))) {
307 fprintf(stderr, "Failed to copy of %s\n", concatPath(binDir, fileName).c_str());
308 return TAC_ERROR_UNKNOWN;
310 updateAssemblyInfo(binDir.c_str(), concatPath(binDir, fileName).c_str());
312 if (!copyFile(originPath, concatPath(binNIDir, fileName))) {
313 fprintf(stderr, "Failed to copy of %s\n", concatPath(binNIDir, fileName).c_str());
314 return TAC_ERROR_UNKNOWN;
316 updateAssemblyInfo(binDir.c_str(), concatPath(binNIDir, fileName).c_str());
321 if (!removeAll(tacDir)) {
322 fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
323 return TAC_ERROR_UNKNOWN;
325 return TAC_ERROR_NONE;
328 tac_error_e enableTACPackage(const std::string& pkgId)
330 std::string rootPath;
331 if (getRootPath(pkgId, rootPath) < 0) {
332 return TAC_ERROR_INVALID_PACKAGE;
335 std::string execName;
336 if (getExecName(pkgId, execName) < 0) {
337 return TAC_ERROR_INVALID_PACKAGE;
340 std::string metaValue;
341 if (getMetadataValue(pkgId, TAC_METADATA_KEY, metaValue) < 0) {
342 return TAC_ERROR_INVALID_PACKAGE;
345 if (initializePathManager(std::string(), std::string(), std::string())) {
346 fprintf(stderr, "Fail to initialize PathManger");
347 return TAC_ERROR_UNKNOWN;
350 if (!strcmp(metaValue.c_str(), "true")) {
351 std::string binDir = concatPath(rootPath, "bin");
352 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
353 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
354 if (!bf::exists(tacDir)) {
355 if (!createDir(tacDir)) {
356 fprintf(stderr, "Cannot create directory: %s\n", tacDir.c_str());
357 return TAC_ERROR_UNKNOWN;
359 updateAssemblyInfo(binDir.c_str(), tacDir.c_str());
361 std::vector<std::string> enableNuget;
362 for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
363 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
364 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
365 std::string nugetPath = concatPath(__TAC_DIR, nugetPackage);
366 if (bf::exists(nugetPath)) {
367 std::string originPath = concatPath(nugetPath, assemblyName);
368 if (bf::exists(originPath)) {
369 enableNuget.push_back(originPath);
374 for (auto& originPath : enableNuget) {
375 if (bf::exists(originPath)) {
376 std::string fileName = originPath.substr(originPath.rfind('/') + 1);
377 std::string NIFileName = fileName.substr(0, fileName.rfind(".dll")) + ".ni.dll";
378 if (bf::exists(binNIDir)) {
379 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
380 if (bf::exists(originNIPath)) {
381 bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName));
382 fprintf(stderr, "%s symbolic link file generated successfully.\n", concatPath(tacDir, NIFileName).c_str());
383 updateAssemblyInfo(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true);
385 if (!removeFile(concatPath(binNIDir, NIFileName))) {
386 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
387 return TAC_ERROR_UNKNOWN;
391 bf::create_symlink(originPath, concatPath(tacDir, fileName));
392 fprintf(stderr, "%s symbolic link file generated successfully.\n", concatPath(tacDir, fileName).c_str());
393 updateAssemblyInfo(tacDir.c_str(), concatPath(tacDir, fileName).c_str(), true);
395 if (!removeFile(concatPath(binDir, fileName))) {
396 fprintf(stderr, "Failed to remove of %s\n", concatPath(binDir, fileName).c_str());
397 return TAC_ERROR_UNKNOWN;
401 if (enableNuget.empty()) {
402 if (!removeAll(tacDir)) {
403 _ERR("Failed to remove of %s", tacDir.c_str());
409 fprintf(stderr, "The metadata key is missing or the metadata value is false of [%s]\n", pkgId.c_str());
411 return TAC_ERROR_NONE;