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 <boost/exception/diagnostic_information.hpp>
6 #include <boost/filesystem/operations.hpp>
7 #include <boost/program_options.hpp>
8 #include <boost/system/error_code.hpp>
10 #include <tzplatform_config.h>
16 #include "pkg_initdb/option_checker.h"
18 namespace bpo = boost::program_options;
22 const uid_t kRootUserUid = 0;
23 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
25 bool IsGlobalUser(uid_t uid) {
26 return uid == kRootUserUid || uid == kGlobalUserUid;
31 bool OptionChecker::Init(int argc, char* argv[]) {
33 options_.add_options()
34 ("uid,u", bpo::value<int>()->default_value(kRootUserUid), "user id")
35 ("partial-rw", "rw-partition only")
36 ("ro", "readonly packages only")
37 ("rw", "rw packages only")
38 ("keep-db", "keep current database")
39 ("help,h", "display this help message")
40 ("recover-db", "register pkg db only");
41 bpo::store(bpo::parse_command_line(argc, argv, options_), opt_map_);
42 if (opt_map_.count("help")) {
43 std::cerr << options_ << std::endl;
46 if (opt_map_.count("partial-rw"))
48 if (opt_map_.count("ro"))
50 if (opt_map_.count("rw"))
52 if (opt_map_.count("keep-db"))
54 if (opt_map_.count("recover-db"))
56 bpo::notify(opt_map_);
57 uid_ = opt_map_["uid"].as<int>();
59 std::cerr << "Exception occurred: "
60 << boost::current_exception_diagnostic_information()
64 return ValidateOptions();
67 bool OptionChecker::GetKeepDb() {
71 uid_t OptionChecker::GetUid() {
75 bool OptionChecker::IsGlobal() {
76 return IsGlobalUser(uid_);
79 bool OptionChecker::IsRWOnly() {
83 bool OptionChecker::IsROOnly() {
87 bool OptionChecker::IsPartialRW() {
91 bool OptionChecker::ValidateOptions() {
92 if (ro_only_ && rw_only_) {
93 std::cerr << "Conflicting options : 'ro' and 'rw";
95 } else if (rw_only_ && partial_rw_) {
96 std::cerr << "Conflicting options : 'rw' and 'partial-rw";
98 } else if (!IsGlobalUser(uid_) && (ro_only_ || rw_only_ || partial_rw_)) {
99 std::cerr << "Conflicting options : 'rw' or 'partial-rw'"
100 << " only affect on global user";
102 } else if (recover_db_ && partial_rw_) {
103 std::cerr << "Conflicting options : 'recover-db' and 'partial-rw'";
110 std::vector<std::string> OptionChecker::GetParams(bool is_preload_pkg,
111 const std::string& pkgid) {
112 std::vector<std::string> params;
114 params.emplace_back("--preload");
117 params.emplace_back("--partial-rw");
119 if (!IsGlobalUser(uid_)) {
120 params.emplace_back("--uid");
121 params.emplace_back(std::to_string(uid_));
125 params.emplace_back("--recover-db");
127 params.emplace_back("-y");
128 params.emplace_back(pkgid);