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.
19 #include "db_manager.h"
27 #include <json/json.h>
28 #include <pkgmgr-info.h>
29 #include <pkgmgr_installer_info.h>
34 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
36 const std::string mOptUsrDotnet = "/opt/usr/dotnet";
37 const std::string mTizenNET = "Tizen.NET";
38 const std::string mTizenNETSdk = "Tizen.NET.Sdk";
39 const std::string mNETStandardLibrary = "NETStandard.Library";
40 const std::string mDepsJson = ".deps.json";
41 const std::string tacAppListDB = mOptUsrDotnet + "/.TAC.App.list.db";
42 const std::string tacAppListDBJournal = mOptUsrDotnet + "/.TAC.App.list.db-journal";
43 const std::string tacAppListRestoreDB = mOptUsrDotnet + "/.TAC.App.list.restore.db";
44 const std::string tacAppListRestoreDBJournal = mOptUsrDotnet + "/.TAC.App.list.restore.db-journal";
45 const std::string mdKey = "http://tizen.org/metadata/prefer_nuget_cache";
46 const std::string mdValue = "true";
48 static sqlite3 *tac_db = NULL;
49 std::vector<std::string> restoreNuget;
51 static void help(const char *argv0)
53 const char* helpDesc =
54 "Usage: %s [args] <root paths or pkg name>\n"
55 " --help - Display this screen\n"
56 " --restore-db - Restore TAC Database\n"
58 printf(helpDesc, argv0, argv0, argv0, argv0, argv0);
61 void cleanupDirectory()
63 std::vector<std::string> removeNuget;
64 for (auto& nuget : bf::recursive_directory_iterator(bf::path(mOptUsrDotnet))) {
66 for (auto& restore : restoreNuget) {
67 if (!bf::is_directory(nuget.path())) {
70 if (strstr(nuget.path().c_str(), restore.c_str()) != NULL) {
76 removeNuget.push_back(nuget.path().string());
80 for (auto& rm : removeNuget) {
83 _ERR("Failed to remove of %s", rm.c_str());
90 void restoreTACDB(std::string pkgId, std::string depsJsonPath, std::string execName)
92 std::ifstream ifs(depsJsonPath);
93 Json::CharReaderBuilder reader;
97 if (!Json::parseFromStream(reader, ifs, &root, &error)) {
98 _INFO("Failed to parse of deps.json");
102 const Json::Value runtimeTargetName = root["runtimeTarget"]["name"];
103 std::string runtimeTarget_name = runtimeTargetName.asString();
104 const Json::Value nugetPackages = root["targets"][runtimeTarget_name.c_str()];
105 for (auto& nuget : nugetPackages.getMemberNames()) {
106 if (strstr(nuget.c_str(), mTizenNET.c_str()) != NULL ||
107 strstr(nuget.c_str(), mTizenNETSdk.c_str()) != NULL ||
108 strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) != NULL ||
109 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) != NULL) {
112 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
113 if (assemblies != Json::nullValue) {
114 const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
115 bool isDependency = false;
116 for (auto& dependency : dependencies.getMemberNames()) {
117 if (strstr(dependency.c_str(), mTizenNET.c_str()) != NULL ||
118 strstr(dependency.c_str(), mNETStandardLibrary.c_str()) != NULL) {
125 _INFO("PackageId : [%s] / Nuget package : [%s]", pkgId.c_str(), nuget.c_str());
126 std::string name = nuget.substr(0, nuget.find('/'));
127 std::string version = nuget.substr(nuget.rfind('/') + 1);
128 std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
129 "VALUES ('" + pkgId + "', '" + nuget + "', '" + name + "', '" + version + "');";
130 dbInsert(tac_db, tacAppListRestoreDB, sql);
131 restoreNuget.push_back(mOptUsrDotnet + "/" + name);
140 static int restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
146 std::string rootPath;
147 std::string execName;
149 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
150 if (ret != PMINFO_R_OK) {
151 fprintf(stderr, "Failed to get pkgid\n");
155 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
156 if (ret != PMINFO_R_OK) {
157 fprintf(stderr, "Failed to get root path\n");
162 ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
163 if (ret != PMINFO_R_OK) {
164 fprintf(stderr, "Failed to get exec name\n");
167 execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
169 std::string depsJsonName = execName.substr(0, execName.rfind(".dll")) + mDepsJson;
170 std::string depsJsonPath = rootPath + "/" + depsJsonName;
171 if (bf::exists(depsJsonPath)) {
172 restoreTACDB(pkgId, depsJsonPath, execName);
179 if (bf::exists(tacAppListRestoreDB)) {
180 if (!removeFile(tacAppListRestoreDB)) {
181 _ERR("Failed to remove of %s", tacAppListRestoreDB.c_str());
185 if (bf::exists(tacAppListRestoreDBJournal)) {
186 if (!removeFile(tacAppListRestoreDBJournal)) {
187 _ERR("Failed to remove of %s", tacAppListRestoreDBJournal.c_str());
192 tac_db = dbCreate(tacAppListRestoreDB);
194 if (!dbOpen(tac_db, tacAppListRestoreDB)) {
202 pkgmgrinfo_appinfo_metadata_filter_h handle;
204 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
205 if (ret != PMINFO_R_OK) {
209 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, mdKey.c_str(), mdValue.c_str());
210 if (ret != PMINFO_R_OK) {
211 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
215 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, restoreDBCb, NULL);
216 if (ret != PMINFO_R_OK) {
217 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
218 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
221 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
228 if (bf::exists(tacAppListRestoreDB)) {
229 if (!copyFile(tacAppListRestoreDB, tacAppListDB)) {
230 _ERR("Failed to move of %s", tacAppListDB.c_str());
233 if (!removeFile(tacAppListRestoreDB)) {
234 _ERR("Failed to remove of %s", tacAppListRestoreDB.c_str());
239 if (bf::exists(tacAppListRestoreDBJournal)) {
240 if (!copyFile(tacAppListRestoreDBJournal, tacAppListDBJournal)) {
241 _ERR("Failed to move of %s", tacAppListDBJournal.c_str());
244 if (!removeFile(tacAppListRestoreDBJournal)) {
245 _ERR("Failed to remove of %s", tacAppListRestoreDBJournal.c_str());
254 // step 1. Remove original DB
255 // step 2. Parsing the .deps.json for all apps
256 // step 3. Create new DB
257 // step 4. Cleanup unnecessary TAC directory
258 int main(int argc, char* argv[])
260 if (cmdOptionExists(argv, argv + argc, "--help")) {
262 } else if (cmdOptionExists(argv, argv + argc, "--restore-db")) {