+/*
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "log.h"
+#include "utils.h"
+#include "db_manager.h"
+#include "tac_common.h"
+#include "tac_db.h"
+
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
+
+#define __XSTR(x) #x
+#define __STR(x) __XSTR(x)
+static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
+#undef __STR
+#undef __XSTR
+
+static sqlite3 *tac_db = NULL;
+static sqlite3 *tlc_db = NULL;
+
+// callback function of "sqlite3_exec"
+int sqliteCb(void *count, int argc, char **argv, char **colName)
+{
+ int *c = (int*)count;
+ *c = atoi(argv[0]);
+ return 0;
+}
+
+int tac_createDB()
+{
+ tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
+ if (!tac_db) {
+ _ERR("Sqlite create error. So restore the database.");
+ if (tac_restoreDB() != TAC_ERROR_NONE) {
+ _ERR("Sqlite create error");
+ return -1;
+ }
+ tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
+ if (!tac_db) {
+ _ERR("Sqlite create error");
+ return -1;
+ }
+ }
+ sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
+ return 0;
+}
+
+int tac_openDB()
+{
+ tac_db = openDB(TAC_APP_LIST_DB);
+ if (!tac_db) {
+ _ERR("Sqlite open error. So restore the database.");
+ if (tac_restoreDB() != TAC_ERROR_NONE) {
+ _ERR("Sqlite open error");
+ return -1;
+ }
+ tac_db = openDB(TAC_APP_LIST_DB);
+ if (!tac_db) {
+ _ERR("Sqlite open error");
+ return -1;
+ }
+ }
+ sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
+ return 0;
+}
+
+int tac_insertDB(const std::string& pkgId, const std::string& np, const std::string& tac_name, const std::string& tac_version)
+{
+ char *sql = sqlite3_mprintf(
+ "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
+ "VALUES (%Q, %Q, %Q, %Q);", pkgId.c_str(), np.c_str(), tac_name.c_str(), tac_version.c_str());
+ if (!insertDB(tac_db, sql)) {
+ _ERR("Sqlite insert error");
+ sqlite3_free(sql);
+ return -1;
+ }
+ sqlite3_free(sql);
+ return 0;
+}
+
+int tac_countDB(const std::string& pkgId, const std::string& tac_name, const std::string& unp, int& count)
+{
+ char *sql = NULL;
+ if (unp == "") {
+ sql = sqlite3_mprintf("SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId.c_str(), tac_name.c_str());
+ } else {
+ sql = sqlite3_mprintf("SELECT COUNT(NUGET) FROM TAC WHERE NUGET = %Q;", unp.c_str());
+ }
+ if (sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL) != SQLITE_OK) {
+ _ERR("Sqlite select error");
+ sqlite3_free(sql);
+ return -1;
+ }
+ sqlite3_free(sql);
+ return 0;
+}
+
+std::vector<std::string> tac_selectDB(const std::string& pkgId)
+{
+ char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId.c_str());
+ std::vector<std::string> update_tac = selectDB(tac_db, sql);
+ sqlite3_free(sql);
+ return update_tac;
+}
+
+int tac_updateDB(const std::string& pkgId, const std::string& np, const std::string& tac_name, const std::string& tac_version)
+{
+ char *sql = sqlite3_mprintf(
+ "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;",
+ tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId.c_str(), tac_name.c_str());
+ if (!updateDB(tac_db, sql)) {
+ _ERR("Sqlite update error");
+ sqlite3_free(sql);
+ return -1;
+ }
+ sqlite3_free(sql);
+ return 0;
+}
+
+int tac_deleteDB(const std::string& pkgId, const std::string& unp)
+{
+ char *sql = NULL;
+ if (unp == "") {
+ sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId.c_str());
+ } else {
+ sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q AND NUGET = %Q;", pkgId.c_str(), unp.c_str());
+ }
+ if (!deleteDB(tac_db, sql)) {
+ _ERR("Sqlite delete error");
+ sqlite3_free(sql);
+ return -1;
+ }
+ sqlite3_free(sql);
+ return 0;
+}
+
+void tac_rollbackDB()
+{
+ if (tac_db) {
+ rollbackDB(tac_db);
+ tac_db = NULL;
+ }
+}
+
+bool tac_closeDB()
+{
+ if (tac_db) {
+ closeDB(tac_db);
+ tac_db = NULL;
+ return true;
+ }
+ return false;
+}
+
+int tlc_createDB()
+{
+ if (!createDir(TLC_LIBRARIES_DIR)) {
+ _ERR("Cannot create directory: %s", TLC_LIBRARIES_DIR);
+ return -1;
+ }
+ copySmackAndOwnership(__DOTNET_DIR, TLC_LIBRARIES_DIR);
+
+ tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE);
+ if (!tlc_db) {
+ _ERR("Sqlite create error. So restore the database.");
+ if (tlc_restoreDB() != TAC_ERROR_NONE) {
+ _ERR("Sqlite create error");
+ return -1;
+ }
+ tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE);
+ if (!tlc_db) {
+ _ERR("Sqlite create error");
+ return -1;
+ }
+ }
+ sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
+ return 0;
+}
+
+int tlc_openDB()
+{
+ tlc_db = openDB(TLC_APP_LIST_DB);
+ if (!tlc_db) {
+ _ERR("Sqlite open error. So restore the database.");
+ if (tlc_restoreDB() != TAC_ERROR_NONE) {
+ _ERR("Sqlite open error");
+ return 0;
+ }
+ tlc_db = openDB(TLC_APP_LIST_DB);
+ if (!tlc_db) {
+ _ERR("Sqlite open error");
+ return 0;
+ }
+ }
+ sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
+ return 0;
+}
+
+int tlc_insertDB(const std::string& pkgId, const std::string& fileSha)
+{
+ char *sql = sqlite3_mprintf("INSERT INTO TLC (PKGID, LIBRARY) VALUES (%Q, %Q);", pkgId.c_str(), fileSha.c_str());
+ if (!insertDB(tlc_db, sql)) {
+ _ERR("Sqlite insert error");
+ sqlite3_free(sql);
+ return -1;
+ }
+ sqlite3_free(sql);
+ return 0;
+}
+
+int tlc_countDB(const std::string& pkgId, const std::string& ulp, int& count)
+{
+ char *sql = sqlite3_mprintf("SELECT COUNT(LIBRARY) FROM TLC WHERE LIBRARY = %Q;", ulp.c_str());
+ if (sqlite3_exec(tlc_db, sql, sqliteCb, &count, NULL) != SQLITE_OK) {
+ _ERR("Sqlite select error");
+ sqlite3_free(sql);
+ return -1;
+ }
+ sqlite3_free(sql);
+ return 0;
+}
+
+std::vector<std::string> tlc_selectDB(const std::string& pkgId)
+{
+ char *sql = sqlite3_mprintf("SELECT * FROM TLC WHERE PKGID = %Q;", pkgId.c_str());
+ std::vector<std::string> update_tlc = selectDB(tlc_db, sql);
+ sqlite3_free(sql);
+ return update_tlc;
+}
+
+int tlc_deleteDB(const std::string& pkgId)
+{
+ char *sql = sqlite3_mprintf("DELETE FROM TLC WHERE PKGID = %Q;", pkgId.c_str());
+ if (!deleteDB(tlc_db, sql)) {
+ _ERR("Sqlite delete error");
+ sqlite3_free(sql);
+ return -1;
+ }
+ sqlite3_free(sql);
+ return 0;
+}
+
+void tlc_rollbackDB()
+{
+ if (tlc_db) {
+ rollbackDB(tlc_db);
+ tlc_db = NULL;
+ }
+}
+
+bool tlc_closeDB()
+{
+ if (tlc_db) {
+ closeDB(tlc_db);
+ tlc_db = NULL;
+ return true;
+ }
+ return false;
+}