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>
13 #include "pkg_initdb/option_checker.h"
17 const uid_t kRootUserUid = 0;
18 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
19 const struct option long_opts[] = {
20 { "uid", required_argument, nullptr, 'u' },
21 { "partial-rw", no_argument, nullptr, 'p' },
22 { "ro", no_argument, nullptr, 'o' },
23 { "rw", no_argument, nullptr, 'w' },
24 { "keep-db", no_argument, nullptr, 'k' },
25 { "help", no_argument, nullptr, 'h' },
26 { "recover-db", no_argument, nullptr, 'r' },
29 const char kHelpMessage[] = R"(Allowed options:
30 -u [ --uid ] arg (=0) user id
31 --partial-rw rw-partition only
32 --ro readonly packages only
34 --keep-db keep current database
35 -h [ --help ] display this help message
36 --recover-db register pkg db only
39 bool IsGlobalUser(uid_t uid) {
40 return uid == kRootUserUid || uid == kGlobalUserUid;
44 std::cerr << kHelpMessage;
49 bool OptionChecker::Init(int argc, char* argv[]) {
52 int opt = getopt_long(argc, argv, "uh", long_opts, nullptr);
59 std::cerr << "The required argument for option '--uid' is missing\n";
62 uid_ = std::stoi(optarg);
83 std::cerr << "Unrecognised option";
88 return ValidateOptions();
91 bool OptionChecker::GetKeepDb() {
95 uid_t OptionChecker::GetUid() {
99 bool OptionChecker::IsGlobal() {
100 return IsGlobalUser(uid_);
103 bool OptionChecker::IsRWOnly() {
107 bool OptionChecker::IsROOnly() {
111 bool OptionChecker::IsPartialRW() {
115 bool OptionChecker::ValidateOptions() {
116 if (ro_only_ && rw_only_) {
117 std::cerr << "Conflicting options : 'ro' and 'rw";
119 } else if (rw_only_ && partial_rw_) {
120 std::cerr << "Conflicting options : 'rw' and 'partial-rw";
122 } else if (!IsGlobalUser(uid_) && (ro_only_ || rw_only_ || partial_rw_)) {
123 std::cerr << "Conflicting options : 'rw' or 'partial-rw'"
124 << " only affect on global user";
126 } else if (recover_db_ && partial_rw_) {
127 std::cerr << "Conflicting options : 'recover-db' and 'partial-rw'";
134 std::vector<std::string> OptionChecker::GetParams(bool is_preload_pkg,
135 const std::string& pkgid) {
136 std::vector<std::string> params;
138 params.emplace_back("--preload");
141 params.emplace_back("--partial-rw");
143 if (!IsGlobalUser(uid_)) {
144 params.emplace_back("--uid");
145 params.emplace_back(std::to_string(uid_));
149 params.emplace_back("--recover-db");
151 params.emplace_back("-y");
152 params.emplace_back(pkgid);