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 <json/json.h>
19 #include <pkgmgr-info.h>
20 #include <pkgmgr_installer_info.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* __DOTNET_DIR = __STR(DOTNET_DIR);
39 static sqlite3 *tac_db = NULL;
40 std::vector<std::string> restore_nuget;
42 static void cleanupDirectory()
44 std::vector<std::string> removeNuget;
46 for (auto& nuget : bf::recursive_directory_iterator(__DOTNET_DIR)) {
48 std::string nugetPath = nuget.path().string();
49 for (auto& restore : restore_nuget) {
50 if (!bf::is_directory(nugetPath)) {
53 if (!strcmp(nugetPath.c_str(), restore.c_str()) ||
54 !strcmp(nugetPath.c_str(), restore.substr(0, restore.rfind('/')).c_str())) {
60 removeNuget.push_back(nugetPath);
64 for (auto& rm : removeNuget) {
66 fprintf(stderr, "Failed to remove of %s\n", rm.c_str());
70 } catch (const bf::filesystem_error& error) {
71 fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
76 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
77 static int restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
85 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
86 if (ret != PMINFO_R_OK) {
87 fprintf(stderr, "Failed to get pkgid\n");
91 enableTACPackage(std::string(pkgId));
93 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
94 if (ret != PMINFO_R_OK) {
95 fprintf(stderr, "Failed to get root path\n");
100 ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
101 if (ret != PMINFO_R_OK) {
102 fprintf(stderr, "Failed to get exec name\n");
105 execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
107 std::vector<std::string> parserData;
108 std::string binDir = concatPath(rootPath, "bin");
109 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
110 for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
111 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
112 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
113 if (bf::exists(tacDir) && bf::exists(concatPath(tacDir, assemblyName))) {
114 parserData.push_back(nugetPackage);
117 std::sort(parserData.begin(), parserData.end());
118 parserData.erase(unique(parserData.begin(), parserData.end()), parserData.end());
120 for (auto& nuget : parserData) {
122 std::string name = nuget.substr(0, nuget.find('/'));
123 std::string version = nuget.substr(nuget.rfind('/') + 1);
124 char *sql = sqlite3_mprintf(
125 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
126 "VALUES (%Q, %Q, %Q, %Q);", pkgId, nuget.c_str(), name.c_str(), version.c_str());
127 dbInsert(tac_db, TAC_APP_LIST_RESTORE_DB, sql);
128 restore_nuget.push_back(concatPath(__DOTNET_DIR, nuget));
136 tac_error_e restoreTACDB()
138 if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
139 fprintf(stderr, "Failed to remove of %s\n", TAC_APP_LIST_RESTORE_DB);
140 return TAC_ERROR_UNKNOWN;
143 std::string dbRestoreJournal = TAC_APP_LIST_RESTORE_DB + std::string("-journal");
144 if (!removeFile(dbRestoreJournal)) {
145 fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
146 return TAC_ERROR_UNKNOWN;
149 if (initializePathManager(std::string(), std::string(), std::string())) {
150 fprintf(stderr, "Fail to initialize PathManger\n");
151 return TAC_ERROR_UNKNOWN;
154 tac_db = dbCreate(TAC_APP_LIST_RESTORE_DB, CREATE_TAC_DB_TABLE);
156 fprintf(stderr, "Sqlite create error\n");
157 return TAC_ERROR_UNKNOWN;
160 pkgmgrinfo_appinfo_metadata_filter_h handle;
161 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
162 if (ret != PMINFO_R_OK) {
163 return TAC_ERROR_UNKNOWN;
166 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
167 if (ret != PMINFO_R_OK) {
168 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
169 return TAC_ERROR_UNKNOWN;
172 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, restoreDBCb, NULL);
173 if (ret != PMINFO_R_OK) {
174 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
175 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
176 return TAC_ERROR_UNKNOWN;
178 fprintf(stdout, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
180 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
187 uid_t g_uid = 301; // app_fw
188 gid_t g_gid = 301; // app_fw
190 if (!copyFile(TAC_APP_LIST_RESTORE_DB, TAC_APP_LIST_DB)) {
191 fprintf(stderr, "Failed to copy of %s\n", TAC_APP_LIST_DB);
192 return TAC_ERROR_UNKNOWN;
194 if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
195 fprintf(stderr, "Failed to remove of %s\n", TAC_APP_LIST_RESTORE_DB);
196 return TAC_ERROR_UNKNOWN;
198 if (chown(TAC_APP_LIST_DB, g_uid, g_gid) == -1) {
199 fprintf(stderr, "Failed to change owner and group name\n");
202 std::string dbJournal = TAC_APP_LIST_DB + std::string("-journal");
203 if (!copyFile(dbRestoreJournal, dbJournal)) {
204 fprintf(stderr, "Failed to copy of %s\n", dbJournal.c_str());
205 return TAC_ERROR_UNKNOWN;
207 if (!removeFile(dbRestoreJournal)) {
208 fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
209 return TAC_ERROR_UNKNOWN;
211 if (chown(dbJournal.c_str(), g_uid, g_gid) == -1) {
212 fprintf(stderr, "Failed to change owner and group name\n");
217 return TAC_ERROR_NONE;
220 tac_error_e resetTACPackage(const std::string& pkgId)
223 if (getRootPath(pkgId, pkgRoot) < 0) {
224 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
225 return TAC_ERROR_INVALID_PACKAGE;
228 std::vector<std::string> tacNativeImage;
229 std::string binDir = concatPath(pkgRoot, "bin");
230 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
231 if (bf::exists(tacDir)) {
233 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
234 std::string symPath = symlinkAssembly.path().string();
235 if (bf::is_symlink(symPath)) {
236 if (isNativeImage(symPath)) {
237 tacNativeImage.push_back(symPath);
241 for (auto& path : tacNativeImage) {
242 if (!removeFile(path)) {
243 fprintf(stderr, "Failed to remove of %s\n", path.c_str());
244 return TAC_ERROR_UNKNOWN;
247 tacNativeImage.clear();
248 } catch (const bf::filesystem_error& error) {
249 fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
250 return TAC_ERROR_UNKNOWN;
253 return TAC_ERROR_NONE;
256 tac_error_e disableTACPackage(const std::string& pkgId)
259 if (getRootPath(pkgId, pkgRoot) < 0) {
260 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
261 return TAC_ERROR_INVALID_PACKAGE;
264 std::string binDir = concatPath(pkgRoot, "bin");
265 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
266 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
267 if (bf::exists(tacDir)) {
269 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
270 std::string symPath = symlinkAssembly.path().string();
271 std::string fileName = symlinkAssembly.path().filename().string();
272 if (bf::is_symlink(symPath)) {
273 std::string originPath = bf::read_symlink(symPath).string();
274 if (!isNativeImage(symPath)) {
275 std::string dllPath = concatPath(binDir, fileName);
276 if (!copyFile(originPath, dllPath)) {
277 fprintf(stderr, "Failed to copy of %s\n", dllPath.c_str());
278 return TAC_ERROR_UNKNOWN;
280 copySmackAndOwnership(binDir.c_str(), concatPath(binDir, fileName).c_str());
282 std::string niPath = concatPath(binNIDir, fileName);
283 if (!copyFile(originPath, niPath)) {
284 fprintf(stderr, "Failed to copy of %s\n", niPath.c_str());
285 return TAC_ERROR_UNKNOWN;
287 copySmackAndOwnership(binDir.c_str(), niPath.c_str());
291 if (!removeAll(tacDir)) {
292 fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
293 return TAC_ERROR_UNKNOWN;
295 } catch (const bf::filesystem_error& error) {
296 fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
297 return TAC_ERROR_UNKNOWN;
300 return TAC_ERROR_NONE;
303 tac_error_e enableTACPackage(const std::string& pkgId)
305 std::string rootPath;
306 if (getRootPath(pkgId, rootPath) < 0) {
307 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
308 return TAC_ERROR_INVALID_PACKAGE;
311 std::string execName;
312 if (getExecName(pkgId, execName) < 0) {
313 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
314 return TAC_ERROR_INVALID_PACKAGE;
317 std::string metaValue;
318 if (getMetadataValue(pkgId, TAC_METADATA_KEY, metaValue) < 0) {
319 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
320 return TAC_ERROR_INVALID_PACKAGE;
323 if (initializePathManager(std::string(), std::string(), std::string())) {
324 fprintf(stderr, "Fail to initialize PathManger\n");
325 return TAC_ERROR_UNKNOWN;
328 if (!strcmp(metaValue.c_str(), "true")) {
329 std::string binDir = concatPath(rootPath, "bin");
330 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
331 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
332 if (!bf::exists(tacDir)) {
333 if (!createDir(tacDir)) {
334 fprintf(stderr, "Cannot create directory: %s\n", tacDir.c_str());
335 return TAC_ERROR_UNKNOWN;
337 copySmackAndOwnership(binDir.c_str(), tacDir.c_str());
339 std::vector<std::string> enableNuget;
340 for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
341 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
342 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
343 std::string nugetPath = concatPath(__DOTNET_DIR, nugetPackage);
344 if (bf::exists(nugetPath)) {
345 std::string originPath = concatPath(nugetPath, assemblyName);
346 if (bf::exists(originPath)) {
347 enableNuget.push_back(originPath);
352 for (auto& originPath : enableNuget) {
353 if (bf::exists(originPath)) {
354 std::string fileName = originPath.substr(originPath.rfind('/') + 1);
355 std::string NIFileName = fileName.substr(0, fileName.rfind(".dll")) + ".ni.dll";
356 if (bf::exists(binNIDir)) {
357 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
358 if (bf::exists(originNIPath)) {
359 bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName));
360 fprintf(stdout, "%s symbolic link file generated successfully.\n", concatPath(tacDir, NIFileName).c_str());
361 copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true);
363 if (!removeFile(concatPath(binNIDir, NIFileName))) {
364 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
365 return TAC_ERROR_UNKNOWN;
369 bf::create_symlink(originPath, concatPath(tacDir, fileName));
370 fprintf(stdout, "%s symbolic link file generated successfully.\n", concatPath(tacDir, fileName).c_str());
371 copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, fileName).c_str(), true);
373 if (!removeFile(concatPath(binDir, fileName))) {
374 fprintf(stderr, "Failed to remove of %s\n", concatPath(binDir, fileName).c_str());
375 return TAC_ERROR_UNKNOWN;
379 if (enableNuget.empty()) {
380 if (!removeAll(tacDir)) {
381 fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
387 fprintf(stderr, "The metadata key is missing or the metadata value is false of [%s]\n", pkgId.c_str());
389 return TAC_ERROR_NONE;
392 //Parser the .deps.json file to get nuget information.
393 std::vector<std::string> depsJsonParser(const std::string& rootPath, const std::string& execName, const std::string& tpaList)
395 std::vector<std::string> tpaAssemblies;
396 splitPath(tpaList, tpaAssemblies);
398 std::vector<std::string> parserData;
399 std::string depsJsonName = execName.substr(0, execName.rfind(".dll")) + ".deps.json";
400 std::string depsJsonPath = concatPath(rootPath, depsJsonName);
402 if (bf::exists(depsJsonPath)) {
403 std::ifstream ifs(depsJsonPath);
404 Json::CharReaderBuilder reader;
408 if (!Json::parseFromStream(reader, ifs, &root, &error)) {
409 _ERR("Failed to parse of deps.json");
411 tpaAssemblies.clear();
414 const Json::Value runtimeTargetName = root["runtimeTarget"]["name"];
415 const Json::Value nugetPackages = root["targets"][runtimeTargetName.asString().c_str()];
416 std::vector<std::string> appDependencies;
417 for (auto& nuget : nugetPackages.getMemberNames()) {
418 if (strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) != NULL ||
419 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) != NULL) {
420 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
421 if (assemblies != Json::nullValue) {
422 const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
423 for (auto& dependency : dependencies.getMemberNames()) {
424 appDependencies.push_back(dependency);
429 for (auto& nuget : nugetPackages.getMemberNames()) {
430 //Skip the nuget package related to Tizen
431 if (strstr(nuget.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
432 strstr(nuget.c_str(), TIZEN_DOTNET_SDK_NUGET) == NULL &&
433 strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) == NULL &&
434 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) == NULL) {
435 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
436 if (assemblies != Json::nullValue) {
437 const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
438 bool hasDependency = false;
439 for (auto& dependency : dependencies.getMemberNames()) {
440 //Skip the nugget package that is dependent on another nuget package
441 if (strstr(dependency.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
442 strstr(dependency.c_str(), NET_STANDARD_LIBRARY_NUGET) == NULL) {
443 hasDependency = true;
444 for (auto& ad : appDependencies) {
445 if (!strcmp(ad.c_str(), dependency.c_str())) {
446 hasDependency = true;
449 hasDependency = false;
452 if (hasDependency) break;
455 if (!hasDependency) {
456 bool isExistTpaAssembly = false;
457 for (auto& assembly : assemblies.getMemberNames()) {
458 std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
459 //Skip the assembly present in the TPA list
460 for (auto& tpaPath : tpaAssemblies) {
461 std::string tpaName = replaceAll(tpaPath.substr(tpaPath.rfind('/') + 1), ".ni.dll", ".dll");
462 if (!strcmp(tpaName.c_str(), assemblyName.c_str())) {
463 isExistTpaAssembly = true;
467 if (isExistTpaAssembly) break;
469 if (!isExistTpaAssembly) {
470 for (auto& assembly : assemblies.getMemberNames()) {
471 std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
472 parserData.push_back(nuget + ":" + assemblyName);
473 _INFO("Nuget : [%s] / Assembly : [%s]", nuget.c_str(), assemblyName.c_str());
480 appDependencies.clear();
484 } catch (const Json::LogicError& error) {
485 _ERR("Failed to parse Json: %s", error.what());
487 tpaAssemblies.clear();