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>
20 #include <json/json.h>
24 #include "ni_common.h"
25 #include "tac_common.h"
26 #include "db_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;
41 std::vector<std::string> enableNuget;
43 void cleanupDirectory()
45 std::vector<std::string> removeNuget;
46 for (auto& nuget : bf::recursive_directory_iterator(__TAC_DIR)) {
48 for (auto& restore : restoreNuget) {
49 if (!bf::is_directory(nuget.path())) {
52 if (strstr(nuget.path().c_str(), restore.c_str()) != NULL) {
58 removeNuget.push_back(nuget.path().string());
62 for (auto& rm : removeNuget) {
64 _ERR("Failed to remove of %s", rm.c_str());
70 void depsJsonParser(std::string pkgId, std::string depsJsonPath, std::string execName, bool isRestore)
72 std::ifstream ifs(depsJsonPath);
73 Json::CharReaderBuilder reader;
77 if (!Json::parseFromStream(reader, ifs, &root, &error)) {
78 _INFO("Failed to parse of deps.json");
82 const Json::Value runtimeTargetName = root["runtimeTarget"]["name"];
83 const Json::Value nugetPackages = root["targets"][runtimeTargetName.asString().c_str()];
84 for (auto& nuget : nugetPackages.getMemberNames()) {
85 if (strstr(nuget.c_str(), TIZEN_DOTNET_NUGET) != NULL ||
86 strstr(nuget.c_str(), TIZEN_DOTNET_SDK_NUGET) != NULL ||
87 strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) != NULL ||
88 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) != NULL) {
91 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
92 if (assemblies != Json::nullValue) {
93 const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
94 bool isDependency = false;
95 for (auto& dependency : dependencies.getMemberNames()) {
96 if (strstr(dependency.c_str(), TIZEN_DOTNET_NUGET) != NULL ||
97 strstr(dependency.c_str(), NET_STANDARD_LIBRARY_NUGET) != NULL) {
104 _INFO("PackageId : [%s] / Nuget package : [%s]", pkgId.c_str(), nuget.c_str());
105 std::string name = nuget.substr(0, nuget.find('/'));
106 std::string version = nuget.substr(nuget.rfind('/') + 1);
107 std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
108 "VALUES ('" + pkgId + "', '" + nuget + "', '" + name + "', '" + version + "');";
111 dbInsert(tac_db, TAC_APP_LIST_RESTORE_DB, sql);
112 restoreNuget.push_back(concatPath(__TAC_DIR, name));
115 std::string nugetPath = concatPath(__TAC_DIR, nuget);
116 if (bf::exists(nugetPath)) {
117 for (auto& assembly : assemblies.getMemberNames()) {
118 std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
119 std::string originPath = concatPath(nugetPath, assemblyName);
120 if (bf::exists(originPath)) {
121 enableNuget.push_back(originPath);
134 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
135 static int restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
140 std::string rootPath;
141 std::string execName;
143 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
144 if (ret != PMINFO_R_OK) {
145 fprintf(stderr, "Failed to get pkgid\n");
149 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
150 if (ret != PMINFO_R_OK) {
151 fprintf(stderr, "Failed to get root path\n");
156 ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
157 if (ret != PMINFO_R_OK) {
158 fprintf(stderr, "Failed to get exec name\n");
161 execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
163 std::string depsJsonName = execName.substr(0, execName.rfind(".dll")) + ".deps.json";
164 std::string depsJsonPath = concatPath(rootPath, depsJsonName);
165 if (bf::exists(depsJsonPath)) {
166 depsJsonParser(pkgId, depsJsonPath, execName, true);
171 tac_error_e restoreTACDB()
173 if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
174 fprintf(stderr, "Failed to remove of %s", TAC_APP_LIST_RESTORE_DB);
175 return TAC_ERROR_UNKNOWN;
178 std::string dbRestoreJournal = TAC_APP_LIST_RESTORE_DB + std::string("-journal");
179 if (!removeFile(dbRestoreJournal)) {
180 fprintf(stderr, "Failed to remove of %s", dbRestoreJournal.c_str());
181 return TAC_ERROR_UNKNOWN;
184 tac_db = dbCreate(TAC_APP_LIST_RESTORE_DB);
186 if (!dbOpen(tac_db, TAC_APP_LIST_RESTORE_DB)) {
187 return TAC_ERROR_UNKNOWN;
190 return TAC_ERROR_UNKNOWN;
193 pkgmgrinfo_appinfo_metadata_filter_h handle;
194 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
195 if (ret != PMINFO_R_OK) {
196 return TAC_ERROR_UNKNOWN;
199 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
200 if (ret != PMINFO_R_OK) {
201 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
202 return TAC_ERROR_UNKNOWN;
205 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, restoreDBCb, NULL);
206 if (ret != PMINFO_R_OK) {
207 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
208 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
209 return TAC_ERROR_UNKNOWN;
211 fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
213 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
220 if (!copyFile(TAC_APP_LIST_RESTORE_DB, TAC_APP_LIST_DB)) {
221 fprintf(stderr, "Failed to move of %s", TAC_APP_LIST_DB);
222 return TAC_ERROR_UNKNOWN;
224 if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
225 fprintf(stderr, "Failed to remove of %s", TAC_APP_LIST_RESTORE_DB);
226 return TAC_ERROR_UNKNOWN;
229 std::string dbJournal = TAC_APP_LIST_DB + std::string("-journal");
230 if (!copyFile(dbRestoreJournal, dbJournal)) {
231 fprintf(stderr, "Failed to move of %s", dbJournal.c_str());
232 return TAC_ERROR_UNKNOWN;
234 if (!removeFile(dbRestoreJournal)) {
235 fprintf(stderr, "Failed to remove of %s", dbRestoreJournal.c_str());
236 return TAC_ERROR_UNKNOWN;
240 return TAC_ERROR_NONE;
243 tac_error_e resetTACPackage(const std::string& pkgId)
246 if (getRootPath(pkgId, pkgRoot) < 0) {
247 return TAC_ERROR_INVALID_PACKAGE;
250 std::vector<std::string> tacNativeImage;
251 std::string binDir = concatPath(pkgRoot, "bin");
252 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
253 if (bf::exists(tacDir)) {
254 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
255 std::string symPath = symlinkAssembly.path().string();
256 if (bf::is_symlink(symPath)) {
257 if (isNativeImage(symPath)) {
258 tacNativeImage.push_back(symPath);
262 for (auto& path : tacNativeImage) {
263 if (!removeFile(path)) {
264 fprintf(stderr, "Failed to remove of %s", path.c_str());
265 return TAC_ERROR_UNKNOWN;
269 tacNativeImage.clear();
270 return TAC_ERROR_NONE;
273 tac_error_e createTACPackage(const std::string& pkgId)
276 if (getRootPath(pkgId, pkgRoot) < 0) {
277 return TAC_ERROR_INVALID_PACKAGE;
280 std::string binDir = concatPath(pkgRoot, "bin");
281 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
282 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
283 if (bf::exists(tacDir)) {
284 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
285 std::string symPath = symlinkAssembly.path().string();
286 if (bf::is_symlink(symPath)) {
287 if (!isNativeImage(symPath)) {
288 std::string originPath = bf::read_symlink(symPath).string();
289 std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
290 if (!bf::exists(originNiPath)) {
291 if(createNiDll(originPath, false) != NI_ERROR_NONE) {
292 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
293 return TAC_ERROR_UNKNOWN;
296 std::string symNIPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
297 if (!bf::exists(symNIPath)) {
298 bf::create_symlink(originNiPath, symNIPath);
299 fprintf(stderr, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
300 updateAssemblyInfo(tacDir.c_str(), symNIPath.c_str(), true);
302 std::string NIFileName = symNIPath.substr(symNIPath.rfind('/') + 1);
303 if (!removeFile(concatPath(binNIDir, NIFileName))) {
304 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
305 return TAC_ERROR_UNKNOWN;
312 return TAC_ERROR_NONE;
315 tac_error_e regenerateTAC()
317 const std::string tacDir[] = {__TAC_DIR};
318 removeNiUnderDirs(tacDir, 1);
320 auto convert = [](const std::string& path, std::string name) {
321 if (strstr(path.c_str(), TAC_APP_LIST_DB) != NULL ||
322 strstr(path.c_str(), TAC_APP_LIST_RESTORE_DB) != NULL ||
323 strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
325 if(createNiDll(path, false) != NI_ERROR_NONE) {
326 fprintf(stderr, "Failed to create NI file [%s]\n", path.c_str());
330 scanFilesInDir(tacDir[0], convert, -1);
331 return TAC_ERROR_NONE;
334 tac_error_e disableTACPackage(const std::string& pkgId)
337 if (getRootPath(pkgId, pkgRoot) < 0) {
338 return TAC_ERROR_INVALID_PACKAGE;
341 std::string binDir = concatPath(pkgRoot, "bin");
342 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
343 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
344 if (bf::exists(tacDir)) {
345 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
346 std::string symPath = symlinkAssembly.path().string();
347 std::string fileName = symlinkAssembly.path().filename().string();
348 if (bf::is_symlink(symPath)) {
349 std::string originPath = bf::read_symlink(symPath).string();
350 if (!isNativeImage(symPath)) {
351 if (!copyFile(originPath, concatPath(binDir, fileName))) {
352 fprintf(stderr, "Failed to copy of %s\n", concatPath(binDir, fileName).c_str());
353 return TAC_ERROR_UNKNOWN;
355 updateAssemblyInfo(binDir.c_str(), concatPath(binDir, fileName).c_str());
357 if (!copyFile(originPath, concatPath(binNIDir, fileName))) {
358 fprintf(stderr, "Failed to copy of %s\n", concatPath(binNIDir, fileName).c_str());
359 return TAC_ERROR_UNKNOWN;
361 updateAssemblyInfo(binDir.c_str(), concatPath(binNIDir, fileName).c_str());
366 if (!removeAll(tacDir)) {
367 fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
368 return TAC_ERROR_UNKNOWN;
370 return TAC_ERROR_NONE;
373 tac_error_e enableTACPackage(const std::string& pkgId)
376 if (getRootPath(pkgId, pkgRoot) < 0) {
377 return TAC_ERROR_INVALID_PACKAGE;
380 std::string execName;
381 if (getExecName(pkgId, execName) < 0) {
382 return TAC_ERROR_INVALID_PACKAGE;
385 std::string metaValue;
386 if (getMetadataValue(pkgId, TAC_METADATA_KEY, metaValue) < 0) {
387 return TAC_ERROR_INVALID_PACKAGE;
390 if (!strcmp(metaValue.c_str(), "true")) {
391 std::string binDir = concatPath(pkgRoot, "bin");
392 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
393 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
394 if (!bf::exists(tacDir)) {
395 if (!createDir(tacDir)) {
396 fprintf(stderr, "Cannot create directory: %s\n", tacDir.c_str());
397 return TAC_ERROR_UNKNOWN;
399 updateAssemblyInfo(binDir.c_str(), tacDir.c_str());
401 std::string depsJsonName = execName.substr(0, execName.rfind(".dll")) + ".deps.json";
402 std::string depsJsonPath = concatPath(pkgRoot, depsJsonName);
403 if (bf::exists(depsJsonPath)) {
404 depsJsonParser(pkgId, depsJsonPath, execName, false);
407 for (auto& originPath : enableNuget) {
408 if (bf::exists(originPath)) {
409 std::string fileName = originPath.substr(originPath.rfind('/') + 1);
410 std::string NIFileName = fileName.substr(0, fileName.rfind(".dll")) + ".ni.dll";
411 if (bf::exists(binNIDir)) {
412 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
413 if (bf::exists(originNIPath)) {
414 bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName));
415 fprintf(stderr, "%s symbolic link file generated successfully.\n", concatPath(tacDir, NIFileName).c_str());
416 updateAssemblyInfo(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true);
418 if (!removeFile(concatPath(binNIDir, NIFileName))) {
419 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
420 return TAC_ERROR_UNKNOWN;
424 bf::create_symlink(originPath, concatPath(tacDir, fileName));
425 fprintf(stderr, "%s symbolic link file generated successfully.\n", concatPath(tacDir, fileName).c_str());
426 updateAssemblyInfo(tacDir.c_str(), concatPath(tacDir, fileName).c_str(), true);
428 if (!removeFile(concatPath(binDir, fileName))) {
429 fprintf(stderr, "Failed to remove of %s\n", concatPath(binDir, fileName).c_str());
430 return TAC_ERROR_UNKNOWN;
434 if (enableNuget.empty()) {
435 if (!removeAll(tacDir)) {
436 _ERR("Failed to remove of %s", tacDir.c_str());
441 fprintf(stderr, "The metadata key is missing or the metadata value is false of [%s]\n", pkgId.c_str());
443 return TAC_ERROR_NONE;