1 // Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
5 #include <tzplatform_config.h>
7 #include <pkgmgr_parser_db.h>
8 #include <pkgmgr-info.h>
15 #include "pkg_initdb/init_pkg_db.h"
16 #include "pkg_initdb/manifest_loader.h"
17 #include "pkg_initdb/option_checker.h"
19 #include "common/utils/file_util.h"
20 #include "common/utils/subprocess.h"
22 namespace ci = common_installer;
23 namespace fs = std::filesystem;
27 const uid_t kRootUserUid = 0;
28 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
29 const char kBackendDirectoryPath[] = "/etc/package-manager/backend/";
31 void RemoveDatabase(const std::string& path) {
33 ci::Remove(path + "-journal");
36 void RemoveOldDatabases(uid_t uid, bool is_global) {
37 char *parser_db = getUserPkgParserDBPathUID(uid);
39 if (parser_db == nullptr) {
40 std::cerr << "Failed to get db path, cannot remove old databases";
44 RemoveDatabase(parser_db);
50 char *cert_db = getUserPkgCertDBPath();
52 std::cerr << "Failed to get cert db path" << std::endl;
56 RemoveDatabase(cert_db);
62 bool InitPkgDB::Init(int argc, char *argv[]) {
63 return option_checker_.Init(argc, argv);
66 bool InitPkgDB::RunBackend(const std::string& pkgid,
67 const std::string& type, bool preload) {
68 fs::path backend_path(kBackendDirectoryPath);
70 ci::Subprocess backend(backend_path.string());
71 auto params = option_checker_.GetParams(preload, pkgid);
73 if (option_checker_.GetUid() != kRootUserUid) {
74 std::cerr << "Preload request for non-root user" << std::endl;
77 params.emplace_back("--preload");
80 if (!backend.RunWithArgs(params)) {
81 std::cerr << "Failed to start backend" << std::endl;
84 int status = backend.Wait();
85 if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0) {
86 std::cerr << "Backend operation failed" << std::endl;
93 bool InitPkgDB::LoadDirectory(const std::string& path,
95 ManifestLoader manifest_loader(path);
96 std::list<ManifestInfo> manifest_list = manifest_loader.LoadManifest();
99 for (auto &manifest_info : manifest_list) {
100 std::cerr << "Manifest : " << std::get<0>(manifest_info) <<
101 (option_checker_.IsPartialRW() ? " (rw-only)":" (all)") << std::endl;
102 if (!RunBackend(std::get<1>(manifest_info),
103 std::get<2>(manifest_info), preload))
109 bool InitPkgDB::Run() {
110 if (!HandleDatabase())
114 if (option_checker_.IsGlobal()) {
115 if (!option_checker_.IsRWOnly())
116 if (!LoadDirectory(tzplatform_getenv(TZ_SYS_RO_PACKAGES), true))
119 if (option_checker_.IsROOnly())
122 if (!LoadDirectory(tzplatform_getenv(TZ_SYS_RW_PACKAGES), false))
125 // Specified user location
126 tzplatform_set_user(option_checker_.GetUid());
127 std::string user_dir(tzplatform_getenv(TZ_USER_PACKAGES));
128 tzplatform_reset_user();
129 if (!LoadDirectory(user_dir, false))
136 bool InitPkgDB::HandleDatabase() {
137 if (option_checker_.GetKeepDb())
140 uid_t uid = option_checker_.GetUid();
141 RemoveOldDatabases(uid, option_checker_.IsGlobal());
142 if (pkgmgr_parser_create_and_initialize_db(uid) < 0) {
143 std::cerr << "Cannot create db" << std::endl;