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* __TAC_DIR = __STR(TAC_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(__TAC_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 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
92 if (ret != PMINFO_R_OK) {
93 fprintf(stderr, "Failed to get root path\n");
98 ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
99 if (ret != PMINFO_R_OK) {
100 fprintf(stderr, "Failed to get exec name\n");
103 execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
105 std::vector<std::string> parserData;
106 for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
107 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
108 parserData.push_back(nugetPackage);
110 std::sort(parserData.begin(), parserData.end());
111 parserData.erase(unique(parserData.begin(), parserData.end()), parserData.end());
113 for (auto& nuget : parserData) {
115 std::string name = nuget.substr(0, nuget.find('/'));
116 std::string version = nuget.substr(nuget.rfind('/') + 1);
117 std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
118 "VALUES ('" + std::string(pkgId) + "', '" + nuget + "', '" + name + "', '" + version + "');";
119 dbInsert(tac_db, TAC_APP_LIST_RESTORE_DB, sql);
120 restore_nuget.push_back(concatPath(__TAC_DIR, nuget));
127 tac_error_e restoreTACDB()
129 if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
130 fprintf(stderr, "Failed to remove of %s\n", TAC_APP_LIST_RESTORE_DB);
131 return TAC_ERROR_UNKNOWN;
134 std::string dbRestoreJournal = TAC_APP_LIST_RESTORE_DB + std::string("-journal");
135 if (!removeFile(dbRestoreJournal)) {
136 fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
137 return TAC_ERROR_UNKNOWN;
140 if (initializePathManager(std::string(), std::string(), std::string())) {
141 fprintf(stderr, "Fail to initialize PathManger\n");
142 return TAC_ERROR_UNKNOWN;
145 tac_db = dbCreate(TAC_APP_LIST_RESTORE_DB);
147 fprintf(stderr, "Sqlite create error\n");
148 return TAC_ERROR_UNKNOWN;
151 pkgmgrinfo_appinfo_metadata_filter_h handle;
152 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
153 if (ret != PMINFO_R_OK) {
154 return TAC_ERROR_UNKNOWN;
157 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
158 if (ret != PMINFO_R_OK) {
159 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
160 return TAC_ERROR_UNKNOWN;
163 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, restoreDBCb, NULL);
164 if (ret != PMINFO_R_OK) {
165 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
166 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
167 return TAC_ERROR_UNKNOWN;
169 fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
171 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
178 uid_t g_uid = 301; // app_fw
179 gid_t g_gid = 301; // app_fw
181 if (!copyFile(TAC_APP_LIST_RESTORE_DB, TAC_APP_LIST_DB)) {
182 fprintf(stderr, "Failed to copy of %s\n", TAC_APP_LIST_DB);
183 return TAC_ERROR_UNKNOWN;
185 if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
186 fprintf(stderr, "Failed to remove of %s\n", TAC_APP_LIST_RESTORE_DB);
187 return TAC_ERROR_UNKNOWN;
189 if (chown(TAC_APP_LIST_DB, g_uid, g_gid) == -1) {
190 fprintf(stderr, "Failed to change owner and group name\n");
193 std::string dbJournal = TAC_APP_LIST_DB + std::string("-journal");
194 if (!copyFile(dbRestoreJournal, dbJournal)) {
195 fprintf(stderr, "Failed to copy of %s\n", dbJournal.c_str());
196 return TAC_ERROR_UNKNOWN;
198 if (!removeFile(dbRestoreJournal)) {
199 fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
200 return TAC_ERROR_UNKNOWN;
202 if (chown(dbJournal.c_str(), g_uid, g_gid) == -1) {
203 fprintf(stderr, "Failed to change owner and group name\n");
208 return TAC_ERROR_NONE;
211 tac_error_e resetTACPackage(const std::string& pkgId)
214 if (getRootPath(pkgId, pkgRoot) < 0) {
215 return TAC_ERROR_INVALID_PACKAGE;
218 std::vector<std::string> tacNativeImage;
219 std::string binDir = concatPath(pkgRoot, "bin");
220 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
221 if (bf::exists(tacDir)) {
223 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
224 std::string symPath = symlinkAssembly.path().string();
225 if (bf::is_symlink(symPath)) {
226 if (isNativeImage(symPath)) {
227 tacNativeImage.push_back(symPath);
231 for (auto& path : tacNativeImage) {
232 if (!removeFile(path)) {
233 fprintf(stderr, "Failed to remove of %s\n", path.c_str());
234 return TAC_ERROR_UNKNOWN;
237 tacNativeImage.clear();
238 } catch (const bf::filesystem_error& error) {
239 fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
240 return TAC_ERROR_UNKNOWN;
243 return TAC_ERROR_NONE;
246 tac_error_e disableTACPackage(const std::string& pkgId)
249 if (getRootPath(pkgId, pkgRoot) < 0) {
250 return TAC_ERROR_INVALID_PACKAGE;
253 std::string binDir = concatPath(pkgRoot, "bin");
254 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
255 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
256 if (bf::exists(tacDir)) {
258 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
259 std::string symPath = symlinkAssembly.path().string();
260 std::string fileName = symlinkAssembly.path().filename().string();
261 if (bf::is_symlink(symPath)) {
262 std::string originPath = bf::read_symlink(symPath).string();
263 if (!isNativeImage(symPath)) {
264 std::string dllPath = concatPath(binDir, fileName);
265 if (!copyFile(originPath, dllPath)) {
266 fprintf(stderr, "Failed to copy of %s\n", dllPath.c_str());
267 return TAC_ERROR_UNKNOWN;
269 updateAssemblyInfo(binDir.c_str(), concatPath(binDir, fileName).c_str());
271 std::string niPath = concatPath(binNIDir, fileName);
272 if (!copyFile(originPath, niPath)) {
273 fprintf(stderr, "Failed to copy of %s\n", niPath.c_str());
274 return TAC_ERROR_UNKNOWN;
276 updateAssemblyInfo(binDir.c_str(), niPath.c_str());
280 if (!removeAll(tacDir)) {
281 fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
282 return TAC_ERROR_UNKNOWN;
284 } catch (const bf::filesystem_error& error) {
285 fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
286 return TAC_ERROR_UNKNOWN;
289 return TAC_ERROR_NONE;
292 tac_error_e enableTACPackage(const std::string& pkgId)
294 std::string rootPath;
295 if (getRootPath(pkgId, rootPath) < 0) {
296 return TAC_ERROR_INVALID_PACKAGE;
299 std::string execName;
300 if (getExecName(pkgId, execName) < 0) {
301 return TAC_ERROR_INVALID_PACKAGE;
304 std::string metaValue;
305 if (getMetadataValue(pkgId, TAC_METADATA_KEY, metaValue) < 0) {
306 return TAC_ERROR_INVALID_PACKAGE;
309 if (initializePathManager(std::string(), std::string(), std::string())) {
310 fprintf(stderr, "Fail to initialize PathManger\n");
311 return TAC_ERROR_UNKNOWN;
314 if (!strcmp(metaValue.c_str(), "true")) {
315 std::string binDir = concatPath(rootPath, "bin");
316 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
317 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
318 if (!bf::exists(tacDir)) {
319 if (!createDir(tacDir)) {
320 fprintf(stderr, "Cannot create directory: %s\n", tacDir.c_str());
321 return TAC_ERROR_UNKNOWN;
323 updateAssemblyInfo(binDir.c_str(), tacDir.c_str());
325 std::vector<std::string> enableNuget;
326 for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
327 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
328 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
329 std::string nugetPath = concatPath(__TAC_DIR, nugetPackage);
330 if (bf::exists(nugetPath)) {
331 std::string originPath = concatPath(nugetPath, assemblyName);
332 if (bf::exists(originPath)) {
333 enableNuget.push_back(originPath);
338 for (auto& originPath : enableNuget) {
339 if (bf::exists(originPath)) {
340 std::string fileName = originPath.substr(originPath.rfind('/') + 1);
341 std::string NIFileName = fileName.substr(0, fileName.rfind(".dll")) + ".ni.dll";
342 if (bf::exists(binNIDir)) {
343 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
344 if (bf::exists(originNIPath)) {
345 bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName));
346 fprintf(stderr, "%s symbolic link file generated successfully.\n", concatPath(tacDir, NIFileName).c_str());
347 updateAssemblyInfo(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true);
349 if (!removeFile(concatPath(binNIDir, NIFileName))) {
350 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
351 return TAC_ERROR_UNKNOWN;
355 bf::create_symlink(originPath, concatPath(tacDir, fileName));
356 fprintf(stderr, "%s symbolic link file generated successfully.\n", concatPath(tacDir, fileName).c_str());
357 updateAssemblyInfo(tacDir.c_str(), concatPath(tacDir, fileName).c_str(), true);
359 if (!removeFile(concatPath(binDir, fileName))) {
360 fprintf(stderr, "Failed to remove of %s\n", concatPath(binDir, fileName).c_str());
361 return TAC_ERROR_UNKNOWN;
365 if (enableNuget.empty()) {
366 if (!removeAll(tacDir)) {
367 fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
373 fprintf(stderr, "The metadata key is missing or the metadata value is false of [%s]\n", pkgId.c_str());
375 return TAC_ERROR_NONE;
378 //Parser the .deps.json file to get nuget information.
379 std::vector<std::string> depsJsonParser(std::string rootPath, std::string execName, std::string tpaList)
381 std::vector<std::string> tpaAssemblies;
382 splitPath(tpaList, tpaAssemblies);
384 std::vector<std::string> parserData;
385 std::string depsJsonName = execName.substr(0, execName.rfind(".dll")) + ".deps.json";
386 std::string depsJsonPath = concatPath(rootPath, depsJsonName);
388 if (bf::exists(depsJsonPath)) {
389 std::ifstream ifs(depsJsonPath);
390 Json::CharReaderBuilder reader;
394 if (!Json::parseFromStream(reader, ifs, &root, &error)) {
395 _ERR("Failed to parse of deps.json");
397 tpaAssemblies.clear();
400 const Json::Value runtimeTargetName = root["runtimeTarget"]["name"];
401 const Json::Value nugetPackages = root["targets"][runtimeTargetName.asString().c_str()];
402 std::vector<std::string> appDependencies;
403 for (auto& nuget : nugetPackages.getMemberNames()) {
404 if (strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) != NULL ||
405 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) != NULL) {
406 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
407 if (assemblies != Json::nullValue) {
408 const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
409 for (auto& dependency : dependencies.getMemberNames()) {
410 appDependencies.push_back(dependency);
415 for (auto& nuget : nugetPackages.getMemberNames()) {
416 //Skip the nuget package related to Tizen
417 if (strstr(nuget.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
418 strstr(nuget.c_str(), TIZEN_DOTNET_SDK_NUGET) == NULL &&
419 strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) == NULL &&
420 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) == NULL) {
421 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
422 if (assemblies != Json::nullValue) {
423 const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
424 bool hasDependency = false;
425 for (auto& dependency : dependencies.getMemberNames()) {
426 //Skip the nugget package that is dependent on another nuget package
427 if (strstr(dependency.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
428 strstr(dependency.c_str(), NET_STANDARD_LIBRARY_NUGET) == NULL) {
429 hasDependency = true;
430 for (auto& ad : appDependencies) {
431 if (!strcmp(ad.c_str(), dependency.c_str())) {
432 hasDependency = true;
435 hasDependency = false;
438 if (hasDependency) break;
441 if (!hasDependency) {
442 bool isExistTpaAssembly = false;
443 for (auto& assembly : assemblies.getMemberNames()) {
444 std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
445 //Skip the assembly present in the TPA list
446 for (auto& tpa : tpaAssemblies) {
447 if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) {
448 isExistTpaAssembly = true;
452 if (isExistTpaAssembly) break;
454 if (!isExistTpaAssembly) {
455 for (auto& assembly : assemblies.getMemberNames()) {
456 std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
457 parserData.push_back(nuget + ":" + assemblyName);
458 _INFO("Nuget : [%s] / Assembly : [%s]", nuget.c_str(), assemblyName.c_str());
465 appDependencies.clear();
469 } catch (const Json::LogicError& error) {
470 _ERR("Failed to parse Json: %s", error.what());
472 tpaAssemblies.clear();