return true;
}
-static const std::string global_parser_memdb_path =
- "file:parserdb?mode=memory&cache=shared";
-
-static const std::string cert_memdb_path =
- "file:certdb?mode=memory&cache=shared";
-
} // namespace
namespace pkgmgr_server {
std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>>
DBHandleProvider::provider_map_;
-bool DBHandleProvider::is_global_memdb_set_ = false;
-std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
- DBHandleProvider::global_parser_memdb_handle_(nullptr,
- sqlite3_close_v2);
-std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
- DBHandleProvider::cert_memdb_handle_(nullptr, sqlite3_close_v2);
std::string DBHandleProvider::global_parser_filedb_path_;
std::string DBHandleProvider::cert_filedb_path_;
std::unordered_set<pid_t> DBHandleProvider::writer_pid_list_;
std::recursive_mutex DBHandleProvider::lock_;
std::shared_mutex DBHandleProvider::pid_list_lock_;
-DBHandleProvider::DBHandleProvider(uid_t uid)
- : uid_(uid),
- is_user_memdb_set_(false),
- parser_memdb_handle_(nullptr, sqlite3_close_v2) {
+DBHandleProvider::DBHandleProvider(uid_t uid) : uid_(uid) {
char* tmp_path;
if (global_parser_filedb_path_.empty()) {
tmp_path = getUserPkgParserDBPathUID(uid_);
user_parser_filedb_path_ = tmp_path;
free(tmp_path);
-
- user_parser_memdb_path_ = "file:parserdb" +
- std::to_string(static_cast<int>(uid_)) +
- "?mode=memory&cache=shared";
}
DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
return ret;
}
-std::vector<std::pair<std::string, uid_t>> DBHandleProvider::GetParserDBPath(
- pid_t pid, bool write) {
+std::vector<std::pair<std::string, uid_t>> DBHandleProvider::GetParserDBPath() {
std::unique_lock<std::recursive_mutex> u(lock_);
std::vector<std::pair<std::string, uid_t>> db_path_list;
- if (is_user_memdb_set_ != is_global_memdb_set_)
- is_global_memdb_set_ ? SetMemoryMode(pid) : UnsetMemoryMode(pid);
-
- if (IsMemoryDBActive(pid, write)) {
- if (uid_ > REGULAR_USER)
- db_path_list.emplace_back(std::make_pair(user_parser_memdb_path_, uid_));
+ if (uid_ > REGULAR_USER)
+ db_path_list.emplace_back(std::make_pair(user_parser_filedb_path_, uid_));
- db_path_list.emplace_back(
- std::make_pair(global_parser_memdb_path, GetGlobalUID()));
- } else {
- if (uid_ > REGULAR_USER)
- db_path_list.emplace_back(std::make_pair(user_parser_filedb_path_, uid_));
-
- db_path_list.emplace_back(
- std::make_pair(global_parser_filedb_path_, GetGlobalUID()));
- }
+ db_path_list.emplace_back(
+ std::make_pair(global_parser_filedb_path_, GetGlobalUID()));
if (db_path_list.size() == 1) {
LOG(DEBUG) << "global db path : " << db_path_list[0].first;
return db_path_list;
}
-std::string DBHandleProvider::GetCertDBPath(pid_t pid, bool write) {
- std::unique_lock<std::recursive_mutex> u(lock_);
- if (is_user_memdb_set_ != is_global_memdb_set_)
- is_global_memdb_set_ ? SetMemoryMode(pid) : UnsetMemoryMode(pid);
-
- if (IsMemoryDBActive(pid, write))
- return cert_memdb_path;
- else
- return cert_filedb_path_;
-}
-
-sqlite3* DBHandleProvider::CreateMemoryDBHandle(const std::string& filedb_path,
- const std::string& memorydb_path) {
- sqlite3* memorydb = nullptr;
- sqlite3* filedb = nullptr;
- LOG(INFO) << "Create memory db handle : " << memorydb_path;
- int ret = sqlite3_open_v2(memorydb_path.c_str(), &memorydb,
- SQLITE_OPEN_READONLY | SQLITE_OPEN_URI, nullptr);
- if (ret != SQLITE_OK) {
- LOG(ERROR) << "Failed to open memory DB " << ret << ": " << memorydb_path;
- return nullptr;
- }
-
- ret = sqlite3_open_v2(filedb_path.c_str(), &filedb, SQLITE_OPEN_READONLY,
- nullptr);
- if (ret != SQLITE_OK) {
- LOG(ERROR) << "Failed to open file DB " << ret << ": " << filedb_path;
- sqlite3_close_v2(memorydb);
- return nullptr;
- }
-
- sqlite3_backup* backup =
- sqlite3_backup_init(memorydb, "main", filedb, "main");
- if (backup == nullptr) {
- LOG(ERROR) << "Failed to backup for memory DB";
- sqlite3_close_v2(memorydb);
- sqlite3_close_v2(filedb);
- return nullptr;
- }
-
- sqlite3_backup_step(backup, -1);
- sqlite3_backup_finish(backup);
- sqlite3_close_v2(filedb);
- LOG(INFO) << "Create memory db handle done";
- return memorydb;
-}
-
-void DBHandleProvider::SetMemoryMode(pid_t pid) {
- std::unique_lock<std::recursive_mutex> u(lock_);
- if (is_global_memdb_set_ && is_user_memdb_set_)
- return;
-
- sqlite3* parser_db =
- CreateMemoryDBHandle(user_parser_filedb_path_, user_parser_memdb_path_);
- if (parser_db != nullptr)
- parser_memdb_handle_.reset(parser_db);
-
- if (is_user_memdb_set_ == is_global_memdb_set_) {
- sqlite3* global_parser_file_db = CreateMemoryDBHandle(
- global_parser_filedb_path_, global_parser_memdb_path);
- if (global_parser_file_db)
- global_parser_memdb_handle_.reset(global_parser_file_db);
-
- sqlite3* cert_db =
- CreateMemoryDBHandle(cert_filedb_path_, cert_memdb_path);
- if (cert_db != nullptr)
- cert_memdb_handle_.reset(cert_db);
-
- InsertPID(pid);
- }
-
- is_user_memdb_set_ = true;
- is_global_memdb_set_ = true;
- LOG(INFO) << "Set Memory mode : Memory";
-}
-
-void DBHandleProvider::UnsetMemoryMode(pid_t pid) {
- std::unique_lock<std::recursive_mutex> u(lock_);
- if (!is_global_memdb_set_ && !is_user_memdb_set_)
- return;
-
- parser_memdb_handle_.reset(nullptr);
- cert_memdb_handle_.reset(nullptr);
- global_parser_memdb_handle_.reset(nullptr);
-
- if (!ErasePID(pid))
- LOG(ERROR) << "Given pid is not exists in pid list : " << pid;
-
- is_user_memdb_set_ = false;
- is_global_memdb_set_ = false;
-
- LOG(DEBUG) << "Set Memory mode : File";
-}
-
-bool DBHandleProvider::IsMemoryDBActive(pid_t pid, bool write) {
- std::unique_lock<std::shared_mutex> u(pid_list_lock_);
- if (!is_user_memdb_set_)
- return false;
-
- if (write)
- return false;
-
- if (writer_pid_list_.find(pid) != writer_pid_list_.end())
- return false;
-
- return true;
+std::string DBHandleProvider::GetCertDBPath() {
+ return cert_filedb_path_;
}
void DBHandleProvider::TrimCache() {
pkg_app_map_.clear();
const char* dbpath = sqlite3_db_filename(db.GetRaw(), "main");
- bool is_inmemory_db = false;
- if (dbpath == nullptr || strlen(dbpath) == 0) {
- LOG(INFO) << "database is inmemory db";
- is_inmemory_db = true;
- }
timespec start_time = { 0, };
timespec end_time = { 0, };
- if (!is_inmemory_db && !GetModifiedTime(dbpath, &start_time))
+ if (!GetModifiedTime(dbpath, &start_time))
return PMINFO_R_ERROR;
pkgmgrinfo_filter_x tmp_filter = { 0, };
AddPackage(std::move(key), std::move(val));
}
- if (!is_inmemory_db && !GetModifiedTime(dbpath, &end_time))
+ if (!GetModifiedTime(dbpath, &end_time))
return PMINFO_R_ERROR;
if (start_time.tv_sec != end_time.tv_sec ||
std::vector<std::shared_ptr<application_x>> app_list;
ret = internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
- if (!is_inmemory_db && !GetModifiedTime(dbpath, &end_time))
+ if (!GetModifiedTime(dbpath, &end_time))
return PMINFO_R_ERROR;
if (start_time.tv_sec != end_time.tv_sec ||
static DBHandleProvider& GetInst(uid_t uid);
static bool IsCrashedWriteRequest();
static bool IsWriter(pid_t pid);
- std::vector<std::pair<std::string, uid_t>> GetParserDBPath(int pid,
- bool write);
- std::string GetCertDBPath(int pid, bool write);
- void SetMemoryMode(pid_t pid);
- void UnsetMemoryMode(pid_t pid);
+ std::vector<std::pair<std::string, uid_t>> GetParserDBPath();
+ std::string GetCertDBPath();
int UpdateCache(const tizen_base::Database& db, pid_t pid, uid_t uid,
bool write, const std::string& locale);
std::vector<std::shared_ptr<package_x>> GetPackages(
private:
explicit DBHandleProvider(uid_t uid);
- sqlite3* CreateMemoryDBHandle(const std::string& filedb_path,
- const std::string& memorydb_path);
- bool IsMemoryDBActive(pid_t pid, bool write);
void ReleaseCache();
void AddPackage(std::string package, std::shared_ptr<package_x> info);
void AddApplication(std::string app, std::shared_ptr<application_x> info);
private:
static std::unordered_map<uid_t,
std::unique_ptr<DBHandleProvider>> provider_map_;
- static bool is_global_memdb_set_;
- static std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
- global_parser_memdb_handle_;
- static std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
- cert_memdb_handle_;
static std::string global_parser_filedb_path_;
static std::string cert_filedb_path_;
static std::unordered_set<pid_t> writer_pid_list_;
static std::shared_mutex pid_list_lock_;
uid_t uid_;
- bool is_user_memdb_set_;
- std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
- parser_memdb_handle_;
std::string user_parser_memdb_path_;
std::string user_parser_filedb_path_;
bool released_ = true;