d94989d34c8faf138e198011936c3a66e05f03f2
[platform/core/appfw/app-installers.git] / src / pkg_initdb / option_checker.cc
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.
4
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>
9
10 #include <tzplatform_config.h>
11
12 #include <string>
13 #include <iostream>
14 #include <vector>
15
16 #include "pkg_initdb/option_checker.h"
17
18 namespace bpo = boost::program_options;
19
20 namespace {
21
22 const uid_t kRootUserUid = 0;
23 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
24
25 bool IsGlobalUser(uid_t uid) {
26   return uid == kRootUserUid || uid == kGlobalUserUid;
27 }
28
29 }  // namespace
30
31 bool OptionChecker::Init(int argc, char* argv[]) {
32   try {
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;
44       return false;
45     }
46     if (opt_map_.count("partial-rw"))
47       partial_rw_ = true;
48     if (opt_map_.count("ro"))
49       ro_only_ = true;
50     if (opt_map_.count("rw"))
51       rw_only_ = true;
52     if (opt_map_.count("keep-db"))
53       keep_db_ = true;
54     if (opt_map_.count("recover-db"))
55       recover_db_ = true;
56     bpo::notify(opt_map_);
57     uid_ = opt_map_["uid"].as<int>();
58   } catch (...) {
59     std::cerr << "Exception occurred: "
60               << boost::current_exception_diagnostic_information()
61               << std::endl;
62     return false;
63   }
64   return ValidateOptions();
65 }
66
67 bool OptionChecker::GetKeepDb() {
68   return keep_db_;
69 }
70
71 uid_t OptionChecker::GetUid() {
72   return uid_;
73 }
74
75 bool OptionChecker::IsGlobal() {
76   return IsGlobalUser(uid_);
77 }
78
79 bool OptionChecker::IsRWOnly() {
80   return rw_only_;
81 }
82
83 bool OptionChecker::IsROOnly() {
84   return ro_only_;
85 }
86
87 bool OptionChecker::IsPartialRW() {
88   return partial_rw_;
89 }
90
91 bool OptionChecker::ValidateOptions() {
92   if (ro_only_ && rw_only_) {
93     std::cerr << "Conflicting options : 'ro' and 'rw";
94     return false;
95   } else if (rw_only_ && partial_rw_) {
96     std::cerr << "Conflicting options : 'rw' and 'partial-rw";
97     return false;
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";
101     return false;
102   } else if (recover_db_ && partial_rw_) {
103     std::cerr << "Conflicting options : 'recover-db' and 'partial-rw'";
104     return false;
105   }
106
107   return true;
108 }
109
110 std::vector<std::string> OptionChecker::GetParams(bool is_preload_pkg,
111     const std::string& pkgid) {
112   std::vector<std::string> params;
113   if (is_preload_pkg)
114     params.emplace_back("--preload");
115
116   if (partial_rw_)
117     params.emplace_back("--partial-rw");
118
119   if (!IsGlobalUser(uid_)) {
120     params.emplace_back("--uid");
121     params.emplace_back(std::to_string(uid_));
122   }
123
124   if (recover_db_)
125     params.emplace_back("--recover-db");
126   else
127     params.emplace_back("-y");
128   params.emplace_back(pkgid);
129
130   return params;
131 }