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;
46 for (auto& nuget : bf::recursive_directory_iterator(__TAC_DIR)) {
48 std::string nugetPath = nuget.path().string();
49 for (auto& restore : restoreNuget) {
50 if (!bf::is_directory(nugetPath)) {
53 if (strstr(nugetPath.c_str(), restore.c_str()) != NULL) {
59 removeNuget.push_back(nugetPath);
63 for (auto& rm : removeNuget) {
65 _ERR("Failed to remove of %s", rm.c_str());
69 } catch (const bf::filesystem_error& error) {
70 _ERR("Failed to recursive directory: %s", error.what());
75 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
76 static int restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
84 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
85 if (ret != PMINFO_R_OK) {
86 fprintf(stderr, "Failed to get pkgid\n");
90 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
91 if (ret != PMINFO_R_OK) {
92 fprintf(stderr, "Failed to get root path\n");
97 ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
98 if (ret != PMINFO_R_OK) {
99 fprintf(stderr, "Failed to get exec name\n");
102 execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
104 std::vector<std::string> parserData;
105 for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
106 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
107 parserData.push_back(nugetPackage);
109 std::sort(parserData.begin(), parserData.end());
110 parserData.erase(unique(parserData.begin(), parserData.end()), parserData.end());
112 for (auto& nuget : parserData) {
114 std::string name = nuget.substr(0, nuget.find('/'));
115 std::string version = nuget.substr(nuget.rfind('/') + 1);
116 std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
117 "VALUES ('" + std::string(pkgId) + "', '" + nuget + "', '" + name + "', '" + version + "');";
118 dbInsert(tac_db, TAC_APP_LIST_RESTORE_DB, sql);
119 restoreNuget.push_back(concatPath(__TAC_DIR, name));
126 tac_error_e restoreTACDB()
128 if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
129 fprintf(stderr, "Failed to remove of %s", TAC_APP_LIST_RESTORE_DB);
130 return TAC_ERROR_UNKNOWN;
133 std::string dbRestoreJournal = TAC_APP_LIST_RESTORE_DB + std::string("-journal");
134 if (!removeFile(dbRestoreJournal)) {
135 fprintf(stderr, "Failed to remove of %s", dbRestoreJournal.c_str());
136 return TAC_ERROR_UNKNOWN;
139 if (initializePathManager(std::string(), std::string(), std::string())) {
140 fprintf(stderr, "Fail to initialize PathManger");
141 return TAC_ERROR_UNKNOWN;
144 tac_db = dbCreate(TAC_APP_LIST_RESTORE_DB);
146 if (!dbOpen(tac_db, TAC_APP_LIST_RESTORE_DB)) {
147 return TAC_ERROR_UNKNOWN;
150 return TAC_ERROR_UNKNOWN;
153 pkgmgrinfo_appinfo_metadata_filter_h handle;
154 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
155 if (ret != PMINFO_R_OK) {
156 return TAC_ERROR_UNKNOWN;
159 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
160 if (ret != PMINFO_R_OK) {
161 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
162 return TAC_ERROR_UNKNOWN;
165 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, restoreDBCb, NULL);
166 if (ret != PMINFO_R_OK) {
167 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
168 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
169 return TAC_ERROR_UNKNOWN;
171 fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
173 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
180 if (!copyFile(TAC_APP_LIST_RESTORE_DB, TAC_APP_LIST_DB)) {
181 fprintf(stderr, "Failed to copy of %s", TAC_APP_LIST_DB);
182 return TAC_ERROR_UNKNOWN;
184 if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
185 fprintf(stderr, "Failed to remove of %s", TAC_APP_LIST_RESTORE_DB);
186 return TAC_ERROR_UNKNOWN;
189 std::string dbJournal = TAC_APP_LIST_DB + std::string("-journal");
190 if (!copyFile(dbRestoreJournal, dbJournal)) {
191 fprintf(stderr, "Failed to copy of %s", dbJournal.c_str());
192 return TAC_ERROR_UNKNOWN;
194 if (!removeFile(dbRestoreJournal)) {
195 fprintf(stderr, "Failed to remove of %s", dbRestoreJournal.c_str());
196 return TAC_ERROR_UNKNOWN;
200 return TAC_ERROR_NONE;
203 tac_error_e resetTACPackage(const std::string& pkgId)
206 if (getRootPath(pkgId, pkgRoot) < 0) {
207 return TAC_ERROR_INVALID_PACKAGE;
210 std::vector<std::string> tacNativeImage;
211 std::string binDir = concatPath(pkgRoot, "bin");
212 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
213 if (bf::exists(tacDir)) {
215 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
216 std::string symPath = symlinkAssembly.path().string();
217 if (bf::is_symlink(symPath)) {
218 if (isNativeImage(symPath)) {
219 tacNativeImage.push_back(symPath);
223 for (auto& path : tacNativeImage) {
224 if (!removeFile(path)) {
225 fprintf(stderr, "Failed to remove of %s", path.c_str());
226 return TAC_ERROR_UNKNOWN;
229 tacNativeImage.clear();
230 } catch (const bf::filesystem_error& error) {
231 _ERR("Failed to recursive directory: %s", error.what());
232 return TAC_ERROR_UNKNOWN;
235 return TAC_ERROR_NONE;
238 tac_error_e createTACPackage(const std::string& pkgId)
241 if (getRootPath(pkgId, pkgRoot) < 0) {
242 return TAC_ERROR_INVALID_PACKAGE;
245 std::string binDir = concatPath(pkgRoot, "bin");
246 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
247 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
248 if (bf::exists(tacDir)) {
250 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
251 std::string symPath = symlinkAssembly.path().string();
252 if (bf::is_symlink(symPath)) {
253 if (!isNativeImage(symPath)) {
254 std::string originPath = bf::read_symlink(symPath).string();
255 std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
256 if (!bf::exists(originNiPath)) {
257 if(createNiDll(originPath, false) != NI_ERROR_NONE) {
258 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
259 return TAC_ERROR_UNKNOWN;
262 std::string symNIPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
263 if (!bf::exists(symNIPath)) {
264 bf::create_symlink(originNiPath, symNIPath);
265 fprintf(stderr, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
266 updateAssemblyInfo(tacDir.c_str(), symNIPath.c_str(), true);
268 std::string NIFileName = symNIPath.substr(symNIPath.rfind('/') + 1);
269 if (!removeFile(concatPath(binNIDir, NIFileName))) {
270 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
271 return TAC_ERROR_UNKNOWN;
277 } catch (const bf::filesystem_error& error) {
278 _ERR("Failed to recursive directory: %s", error.what());
279 return TAC_ERROR_UNKNOWN;
282 return TAC_ERROR_NONE;
285 tac_error_e regenerateTAC()
287 const std::string tacDir[] = {__TAC_DIR};
288 removeNiUnderDirs(tacDir, 1);
289 createNiUnderTAC(tacDir, 1);
290 return TAC_ERROR_NONE;
293 tac_error_e disableTACPackage(const std::string& pkgId)
296 if (getRootPath(pkgId, pkgRoot) < 0) {
297 return TAC_ERROR_INVALID_PACKAGE;
300 std::string binDir = concatPath(pkgRoot, "bin");
301 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
302 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
303 if (bf::exists(tacDir)) {
305 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
306 std::string symPath = symlinkAssembly.path().string();
307 std::string fileName = symlinkAssembly.path().filename().string();
308 if (bf::is_symlink(symPath)) {
309 std::string originPath = bf::read_symlink(symPath).string();
310 if (!isNativeImage(symPath)) {
311 std::string dllPath = concatPath(binDir, fileName);
312 if (!copyFile(originPath, dllPath)) {
313 fprintf(stderr, "Failed to copy of %s\n", dllPath.c_str());
314 return TAC_ERROR_UNKNOWN;
316 updateAssemblyInfo(binDir.c_str(), concatPath(binDir, fileName).c_str());
318 std::string niPath = concatPath(binNIDir, fileName);
319 if (!copyFile(originPath, niPath)) {
320 fprintf(stderr, "Failed to copy of %s\n", niPath.c_str());
321 return TAC_ERROR_UNKNOWN;
323 updateAssemblyInfo(binDir.c_str(), niPath.c_str());
327 if (!removeAll(tacDir)) {
328 fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
329 return TAC_ERROR_UNKNOWN;
331 } catch (const bf::filesystem_error& error) {
332 _ERR("Failed to recursive directory: %s", error.what());
333 return TAC_ERROR_UNKNOWN;
336 return TAC_ERROR_NONE;
339 tac_error_e enableTACPackage(const std::string& pkgId)
341 std::string rootPath;
342 if (getRootPath(pkgId, rootPath) < 0) {
343 return TAC_ERROR_INVALID_PACKAGE;
346 std::string execName;
347 if (getExecName(pkgId, execName) < 0) {
348 return TAC_ERROR_INVALID_PACKAGE;
351 std::string metaValue;
352 if (getMetadataValue(pkgId, TAC_METADATA_KEY, metaValue) < 0) {
353 return TAC_ERROR_INVALID_PACKAGE;
356 if (initializePathManager(std::string(), std::string(), std::string())) {
357 fprintf(stderr, "Fail to initialize PathManger");
358 return TAC_ERROR_UNKNOWN;
361 if (!strcmp(metaValue.c_str(), "true")) {
362 std::string binDir = concatPath(rootPath, "bin");
363 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
364 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
365 if (!bf::exists(tacDir)) {
366 if (!createDir(tacDir)) {
367 fprintf(stderr, "Cannot create directory: %s\n", tacDir.c_str());
368 return TAC_ERROR_UNKNOWN;
370 updateAssemblyInfo(binDir.c_str(), tacDir.c_str());
372 std::vector<std::string> enableNuget;
373 for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
374 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
375 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
376 std::string nugetPath = concatPath(__TAC_DIR, nugetPackage);
377 if (bf::exists(nugetPath)) {
378 std::string originPath = concatPath(nugetPath, assemblyName);
379 if (bf::exists(originPath)) {
380 enableNuget.push_back(originPath);
385 for (auto& originPath : enableNuget) {
386 if (bf::exists(originPath)) {
387 std::string fileName = originPath.substr(originPath.rfind('/') + 1);
388 std::string NIFileName = fileName.substr(0, fileName.rfind(".dll")) + ".ni.dll";
389 if (bf::exists(binNIDir)) {
390 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
391 if (bf::exists(originNIPath)) {
392 bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName));
393 fprintf(stderr, "%s symbolic link file generated successfully.\n", concatPath(tacDir, NIFileName).c_str());
394 updateAssemblyInfo(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true);
396 if (!removeFile(concatPath(binNIDir, NIFileName))) {
397 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
398 return TAC_ERROR_UNKNOWN;
402 bf::create_symlink(originPath, concatPath(tacDir, fileName));
403 fprintf(stderr, "%s symbolic link file generated successfully.\n", concatPath(tacDir, fileName).c_str());
404 updateAssemblyInfo(tacDir.c_str(), concatPath(tacDir, fileName).c_str(), true);
406 if (!removeFile(concatPath(binDir, fileName))) {
407 fprintf(stderr, "Failed to remove of %s\n", concatPath(binDir, fileName).c_str());
408 return TAC_ERROR_UNKNOWN;
412 if (enableNuget.empty()) {
413 if (!removeAll(tacDir)) {
414 _ERR("Failed to remove of %s", tacDir.c_str());
420 fprintf(stderr, "The metadata key is missing or the metadata value is false of [%s]\n", pkgId.c_str());
422 return TAC_ERROR_NONE;