After this patch is applied, a commit is performed in step methods.
The StepBackup() is added to save previous data. If the installation
is failed, previous data will be applied to the database.
Change-Id: I0b1290315b00dff6a93c90162daa0e15c2b7b3e6
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
--- /dev/null
+/*
+ * Copyright (c) 2020 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.
+ */
+
+#ifndef ALIAS_APPID_ALIAS_INFO_HH_
+#define ALIAS_APPID_ALIAS_INFO_HH_
+
+#include <string>
+
+namespace plugin {
+
+class AliasInfo {
+ public:
+ AliasInfo(std::string alias_appid, std::string appid)
+ : alias_appid_(std::move(alias_appid)), appid_(std::move(appid)) {
+ }
+
+ virtual ~AliasInfo() = default;
+
+ const std::string& GetAliasAppId() const {
+ return alias_appid_;
+ }
+
+ const std::string& GetAppId() const {
+ return appid_;
+ }
+
+ private:
+ std::string alias_appid_;
+ std::string appid_;
+};
+
+} // namespace plugin
+
+#endif // ALIAS_APPID_ALIAS_INFO_HH_
AppSvcDB::~AppSvcDB() = default;
+std::vector<std::shared_ptr<AliasInfo>> AppSvcDB::Select(
+ const std::string& appid) {
+ static const char query[] = "SELECT alias_appid FROM alias_info "
+ "WHERE appid = ?;";
+ sqlite3_stmt* stmt;
+ __PREPARE_V2(GetHandle(), query, strlen(query), stmt);
+ auto stmt_ptr = std::unique_ptr<sqlite3_stmt, decltype(sqlite3_finalize)*>(
+ stmt, sqlite3_finalize);
+ __BIND_TEXT(GetHandle(), stmt, 1, appid.c_str());
+
+ std::vector<std::shared_ptr<AliasInfo>> infos;
+ while (sqlite3_step(stmt) == SQLITE_ROW) {
+ std::string alias_appid = ColumnText(stmt, 0);
+ if (!alias_appid.empty())
+ infos.emplace_back(new (std::nothrow) AliasInfo(alias_appid, appid));
+ }
+
+ return infos;
+}
+
void AppSvcDB::Insert(const std::string& alias_appid,
const std::string& appid) {
static const char query[] = "INSERT OR REPLACE INTO "
#include <unistd.h>
+#include <memory>
#include <string>
+#include <vector>
+#include "alias-appid/alias_info.hh"
#include "common/database.hh"
namespace plugin {
AppSvcDB(uid_t uid);
virtual ~AppSvcDB();
+ std::vector<std::shared_ptr<AliasInfo>> Select(const std::string& appid);
void Insert(const std::string& alias_appid, const std::string& appid);
void Delete(const std::string& alias_appid, const std::string& appid);
void Delete(const std::string& appid);
_W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid);
PluginManager::GetInst().AddAppEventArgs(pkgid, appid,
EventType::Install, list);
+ PluginManager::GetInst().Do();
return 0;
}
_W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid);
PluginManager::GetInst().AddAppEventArgs(pkgid, appid,
EventType::Uninstall, list);
+ PluginManager::GetInst().Do();
return 0;
}
_W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid);
PluginManager::GetInst().AddAppEventArgs(pkgid, appid,
EventType::Upgrade, list);
+ PluginManager::GetInst().Do();
return 0;
}
_W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid);
PluginManager::GetInst().AddAppEventArgs(pkgid, appid,
EventType::Uninstall, list);
+ PluginManager::GetInst().Do();
return 0;
}
_W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid);
PluginManager::GetInst().AddAppEventArgs(pkgid, appid,
EventType::Uninstall, list);
+ PluginManager::GetInst().Do();
return 0;
}
_W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid);
PluginManager::GetInst().AddAppEventArgs(pkgid, appid,
EventType::Upgrade, list);
+ PluginManager::GetInst().Do();
return 0;
}
return inst;
}
+bool PluginManager::StepBackup(const std::unique_ptr<AppEventArgs>& args) {
+ auto* db = dynamic_cast<AppSvcDB*>(GetDB());
+ if (db == nullptr) {
+ _E("MetadataPlugin is not prepared");
+ return false;
+ }
+
+ try {
+ auto info_arr = db->Select(args->GetAppId());
+ if (info_arr.size() != 0)
+ infos_.insert(infos_.end(), info_arr.begin(), info_arr.end());
+ } catch (Exception& e) {
+ _E("Exception(%d) occurs", e.GetErrorCode());
+ return false;
+ }
+
+ return true;
+}
+
bool PluginManager::StepInstall(const std::unique_ptr<AppEventArgs>& args) {
auto* db = dynamic_cast<AppSvcDB*>(GetDB());
if (db == nullptr) {
return StepInstall(args);
}
+bool PluginManager::StepRestore() {
+ _E("Restore");
+ auto* db = dynamic_cast<AppSvcDB*>(GetDB());
+ if (db == nullptr) {
+ _E("MetadataPlugin is not prepared");
+ return false;
+ }
+
+ for (auto& info : infos_) {
+ try {
+ db->Insert(info->GetAliasAppId(), info->GetAppId());
+ } catch (Exception& e) {
+ _E("Exception(%d) occurs", e.GetErrorCode());
+ return false;
+ }
+ }
+
+ return true;
+}
+
} // namespace plugin
#ifndef ALIAS_APPID_PLUGIN_MANAGER_HH_
#define ALIAS_APPID_PLUGIN_MANAGER_HH_
+#include <vector>
+
+#include "alias-appid/alias_info.hh"
#include "common/metadata_plugin.hh"
namespace plugin {
public:
static PluginManager& GetInst();
+ bool StepBackup(const std::unique_ptr<AppEventArgs>& args) override;
bool StepInstall(const std::unique_ptr<AppEventArgs>& args) override;
bool StepUninstall(const std::unique_ptr<AppEventArgs>& args) override;
bool StepUpgrade(const std::unique_ptr<AppEventArgs>& args) override;
+ bool StepRestore() override;
+ private:
+ std::vector<std::shared_ptr<AliasInfo>> infos_;
};
} // namespace plugin
--- /dev/null
+/*
+ * Copyright (c) 2020 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.
+ */
+
+#ifndef ALLOWED_APPID_ALLOWED_INFO_HH_
+#define ALLOWED_APPID_ALLOWED_INFO_HH_
+
+#include <memory>
+#include <string>
+
+namespace plugin {
+
+class AllowedInfo {
+ public:
+ AllowedInfo(std::string appid, std::string allowed_appid)
+ : appid_(std::move(appid)), allowed_appid_(std::move(allowed_appid)) {
+ }
+
+ virtual ~AllowedInfo() = default;
+
+ const std::string& GetAppId() const {
+ return appid_;
+ }
+
+ const std::string& GetAllowedAppId() const {
+ return allowed_appid_;
+ }
+
+ private:
+ std::string appid_;
+ std::string allowed_appid_;
+};
+
+} // namespace plugin
+
+#endif // ALLOWED_APPID_ALLOWED_INFO_HH_
AppSvcDB::~AppSvcDB() = default;
+std::vector<std::shared_ptr<AllowedInfo>> AppSvcDB::Select(
+ const std::string& appid) {
+ static const char query[] = "SELECT allowed_appid FROM "
+ "allowed_info WHERE appid = ?;";
+ sqlite3_stmt* stmt;
+ __PREPARE_V2(GetHandle(), query, strlen(query), stmt);
+ auto stmt_ptr = std::unique_ptr<sqlite3_stmt, decltype(sqlite3_finalize)*>(
+ stmt, sqlite3_finalize);
+ __BIND_TEXT(GetHandle(), stmt, 1, appid.c_str());
+
+ std::vector<std::shared_ptr<AllowedInfo>> allowed_infos;
+ while (sqlite3_step(stmt) == SQLITE_ROW) {
+ std::string allowed_appid = ColumnText(stmt, 0);
+ if (!allowed_appid.empty()) {
+ allowed_infos.emplace_back(
+ new (std::nothrow) AllowedInfo(appid, allowed_appid));
+ }
+ }
+
+ return allowed_infos;
+}
+
void AppSvcDB::Insert(const std::string& appid,
const std::string& allowed_appid) {
static const char query[] = "INSERT OR REPLACE INTO "
* limitations under the License.
*/
-#ifndef ALIAS_APPID_APPSVC_DB_HH_
-#define ALIAS_APPID_APPSVC_DB_HH_
+#ifndef ALLOWED_APPID_APPSVC_DB_HH_
+#define ALLOWED_APPID_APPSVC_DB_HH_
#include <unistd.h>
+#include <memory>
#include <string>
+#include <vector>
+#include "allowed-appid/allowed_info.hh"
#include "common/database.hh"
namespace plugin {
AppSvcDB(uid_t uid);
virtual ~AppSvcDB();
+ std::vector<std::shared_ptr<AllowedInfo>> Select(const std::string& appid);
void Insert(const std::string& appid, const std::string& allowed_appid);
void Delete(const std::string& appid);
} // namespace plugin
-#endif // ALIAS_APPID_APPSVC_DB_HH_
+#endif // ALLOWED_APPID_APPSVC_DB_HH_
_W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid);
PluginManager::GetInst().AddAppEventArgs(pkgid, appid,
EventType::Install, list);
+ PluginManager::GetInst().Do();
return 0;
}
_W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid);
PluginManager::GetInst().AddAppEventArgs(pkgid, appid,
EventType::Uninstall, list);
+ PluginManager::GetInst().Do();
return 0;
}
_W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid);
PluginManager::GetInst().AddAppEventArgs(pkgid, appid,
EventType::Upgrade, list);
+ PluginManager::GetInst().Do();
return 0;
}
_W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid);
PluginManager::GetInst().AddAppEventArgs(pkgid, appid,
EventType::Uninstall, list);
+ PluginManager::GetInst().Do();
return 0;
}
_W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid);
PluginManager::GetInst().AddAppEventArgs(pkgid, appid,
EventType::Uninstall, list);
+ PluginManager::GetInst().Do();
return 0;
}
_W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid);
PluginManager::GetInst().AddAppEventArgs(pkgid, appid,
EventType::Upgrade, list);
+ PluginManager::GetInst().Do();
return 0;
}
return inst;
}
+bool PluginManager::StepBackup(const std::unique_ptr<AppEventArgs>& args) {
+ auto* db = dynamic_cast<AppSvcDB*>(GetDB());
+ if (db == nullptr) {
+ _E("MetadataPlugin is not prepared");
+ return false;
+ }
+
+ try {
+ auto info_arr = db->Select(args->GetAppId());
+ if (info_arr.size() != 0)
+ infos_.insert(infos_.end(), info_arr.begin(), info_arr.end());
+ } catch (Exception& e) {
+ _E("Execption(%d) occurs", e.GetErrorCode());
+ return false;
+ }
+
+ return true;
+}
+
std::vector<std::string> PluginManager::Split(const std::string& str,
const std::string& delim) {
std::string string(str);
return StepInstall(args);
}
+bool PluginManager::StepRestore() {
+ _E("Restore");
+ auto* db = dynamic_cast<AppSvcDB*>(GetDB());
+ if (db == nullptr) {
+ _E("MetadataPlugin is not prepared");
+ return false;
+ }
+
+ for (auto& info : infos_) {
+ try {
+ db->Insert(info->GetAppId(), info->GetAllowedAppId());
+ } catch (Exception& e) {
+ _E("Exception(%d) occurs", e.GetErrorCode());
+ return false;
+ }
+ }
+
+ return true;
+}
+
} // namespace plugin
* limitations under the License.
*/
-#ifndef ALIAS_APPID_PLUGIN_MANAGER_HH_
-#define ALIAS_APPID_PLUGIN_MANAGER_HH_
+#ifndef ALLOWED_APPID_PLUGIN_MANAGER_HH_
+#define ALLOWED_APPID_PLUGIN_MANAGER_HH_
#include <vector>
#include "common/metadata_plugin.hh"
+#include "allowed-appid/allowed_info.hh"
namespace plugin {
public:
static PluginManager& GetInst();
+ bool StepBackup(const std::unique_ptr<AppEventArgs>& args) override;
bool StepInstall(const std::unique_ptr<AppEventArgs>& args) override;
bool StepUninstall(const std::unique_ptr<AppEventArgs>& args) override;
bool StepUpgrade(const std::unique_ptr<AppEventArgs>& args) override;
+ bool StepRestore() override;
private:
std::vector<std::string> Split(const std::string& str,
const std::string& delim);
+
+ private:
+ std::vector<std::shared_ptr<AllowedInfo>> infos_;
};
} // namespace plugin
-#endif // ALIAS_APPID_PLUGIN_MANAGER_HH_
+#endif // ALLOWED_APPID_PLUGIN_MANAGER_HH_
return db_;
}
+std::string Database::ColumnText(sqlite3_stmt* stmt, int index) {
+ auto* text = reinterpret_cast<const char*>(sqlite3_column_text(stmt, index));
+ if (text)
+ return std::string(text);
+
+ return {};
+}
+
bool Database::IntegrityCheck() {
static const char query[] = "PRAGMA integrity_check";
sqlite3_stmt* stmt;
void EndTransaction();
void Rollback();
sqlite3* GetHandle();
+ std::string ColumnText(sqlite3_stmt* stmt, int index);
private:
static int BusyHandler(void* data, int count);
list_.emplace_back(args);
}
-void MetadataPlugin::Clean() {
+void MetadataPlugin::Do() {
+ _W("Do");
if (!Prepare())
return;
for (auto& args : list_) {
+ bool ret = StepBackup(args);
+ if (!ret) {
+ Post();
+ return;
+ }
+ }
+
+ for (auto& args : list_) {
bool ret = true;
if (args->GetEventType() == EventType::Install) {
ret = StepInstall(args);
Post();
}
+void MetadataPlugin::Clean() {
+ _W("Clean");
+}
+
Database* MetadataPlugin::GetDB() {
return db_.get();
}
return true;
}
+bool MetadataPlugin::StepBackup(const std::unique_ptr<AppEventArgs>& args) {
+ return true;
+}
+
bool MetadataPlugin::StepInstall(const std::unique_ptr<AppEventArgs>& args) {
return true;
}
return true;
}
+bool MetadataPlugin::StepRestore() {
+ return true;
+}
+
void MetadataPlugin::Post() {
try {
db_->EndTransaction();
void MetadataPlugin::Undo() {
_E("Undo");
+ if (!Prepare())
+ return;
+
+ for (auto& args : list_) {
+ if (!StepUninstall(args))
+ _E("StepUninstall() is failed");
+ }
+
+ if (!StepRestore())
+ _E("StepRestore() is failed");
+
+ Post();
}
} // namespace plugin
void AddAppEventArgs(const std::string& pkgid, const std::string& appid,
EventType event_type, GList* list);
+ Database* GetDB();
+
+ void Do();
void Clean();
void Undo();
- Database* GetDB();
virtual bool Prepare();
+ virtual bool StepBackup(const std::unique_ptr<AppEventArgs>& args);
virtual bool StepInstall(const std::unique_ptr<AppEventArgs>& args);
virtual bool StepUninstall(const std::unique_ptr<AppEventArgs>& args);
virtual bool StepUpgrade(const std::unique_ptr<AppEventArgs>& args);
+ virtual bool StepRestore();
virtual void Post();
virtual void Rollback();