Remove boost dependency
[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 <tzplatform_config.h>
6 #include <getopt.h>
7 #include <unistd.h>
8
9 #include <string>
10 #include <iostream>
11 #include <vector>
12
13 #include "pkg_initdb/option_checker.h"
14
15 namespace {
16
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' },
27   { 0, 0, 0, 0 }
28 };
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
33   --rw                  rw packages only
34   --keep-db             keep current database
35   -h [ --help ]         display this help message
36   --recover-db          register pkg db only
37 )";
38
39 bool IsGlobalUser(uid_t uid) {
40   return uid == kRootUserUid || uid == kGlobalUserUid;
41 }
42
43 void PrintHelp() {
44   std::cerr << kHelpMessage;
45 }
46
47 }  // namespace
48
49 bool OptionChecker::Init(int argc, char* argv[]) {
50   uid_ = kRootUserUid;
51   while (true) {
52     int opt = getopt_long(argc, argv, "uh", long_opts, nullptr);
53     if (opt == -1)
54       break;
55
56     switch (opt) {
57       case 'u':
58         if (!optarg) {
59           std::cerr << "The required argument for option '--uid' is missing\n";
60           return false;
61         }
62         uid_ = std::stoi(optarg);
63         break;
64       case 'p':
65         partial_rw_ = true;
66         break;
67       case 'o':
68         ro_only_ = true;
69         break;
70       case 'w':
71         rw_only_ = true;
72         break;
73       case 'k':
74         keep_db_ = true;
75         break;
76       case 'h':
77         PrintHelp();
78         break;
79       case 'r':
80         recover_db_ = true;
81         break;
82       default:
83         std::cerr << "Unrecognised option";
84         PrintHelp();
85         return false;
86     }
87   }
88   return ValidateOptions();
89 }
90
91 bool OptionChecker::GetKeepDb() {
92   return keep_db_;
93 }
94
95 uid_t OptionChecker::GetUid() {
96   return uid_;
97 }
98
99 bool OptionChecker::IsGlobal() {
100   return IsGlobalUser(uid_);
101 }
102
103 bool OptionChecker::IsRWOnly() {
104   return rw_only_;
105 }
106
107 bool OptionChecker::IsROOnly() {
108   return ro_only_;
109 }
110
111 bool OptionChecker::IsPartialRW() {
112   return partial_rw_;
113 }
114
115 bool OptionChecker::ValidateOptions() {
116   if (ro_only_ && rw_only_) {
117     std::cerr << "Conflicting options : 'ro' and 'rw";
118     return false;
119   } else if (rw_only_ && partial_rw_) {
120     std::cerr << "Conflicting options : 'rw' and 'partial-rw";
121     return false;
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";
125     return false;
126   } else if (recover_db_ && partial_rw_) {
127     std::cerr << "Conflicting options : 'recover-db' and 'partial-rw'";
128     return false;
129   }
130
131   return true;
132 }
133
134 std::vector<std::string> OptionChecker::GetParams(bool is_preload_pkg,
135     const std::string& pkgid) {
136   std::vector<std::string> params;
137   if (is_preload_pkg)
138     params.emplace_back("--preload");
139
140   if (partial_rw_)
141     params.emplace_back("--partial-rw");
142
143   if (!IsGlobalUser(uid_)) {
144     params.emplace_back("--uid");
145     params.emplace_back(std::to_string(uid_));
146   }
147
148   if (recover_db_)
149     params.emplace_back("--recover-db");
150   else
151     params.emplace_back("-y");
152   params.emplace_back(pkgid);
153
154   return params;
155 }