Add initdb options and constraints 59/165659/1
authorJunghyun Yeon <jungh.yeon@samsung.com>
Tue, 19 Dec 2017 05:12:42 +0000 (14:12 +0900)
committerJunghyun Yeon <jungh.yeon@samsung.com>
Wed, 3 Jan 2018 05:55:20 +0000 (14:55 +0900)
- "--rw" will only handle RW packages.
- "--keep-db" will preserve existing databases.
- Add constraints for some conflicting options.

Change-Id: If3db2636bc5cca49d3e1396d345a79fe2fefdf17
Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
src/pkg_initdb/pkg_initdb.cc

index 2246d37..0e4fddf 100644 (file)
@@ -137,46 +137,72 @@ int main(int argc, char *argv[]) {
   bpo::options_description options("Allowed options");
   bpo::variables_map opt_map;
   uid_t uid;
+  bool partial_rw = false;
+  bool ro_only = false;
   bool rw_only = false;
-  bool initial = false;
+  bool keep_db = false;
   try {
     options.add_options()
         ("uid,u", bpo::value<int>()->default_value(kRootUserUid), "user id")
         ("partial-rw", "rw-partition only")
-        ("ro", "readonly package only")
+        ("ro", "readonly packages only")
+        ("rw", "rw packages only")
+        ("keep-db", "keep current database")
         ("help,h", "display this help message");
     bpo::store(bpo::parse_command_line(argc, argv, options), opt_map);
     if (opt_map.count("help")) {
       std::cerr << options << std::endl;
       return -1;
     }
+
     if (opt_map.count("partial-rw"))
-      rw_only = true;
+      partial_rw = true;
     if (opt_map.count("ro"))
-      initial = true;
+      ro_only = true;
+    if (opt_map.count("rw"))
+      rw_only = true;
+    if (opt_map.count("keep-db"))
+      keep_db = true;
     bpo::notify(opt_map);
     uid = opt_map["uid"].as<int>();
   } catch (...) {
     std::cerr << "Exception occurred: "
-              << boost::current_exception_diagnostic_information() << std::endl;
+              << boost::current_exception_diagnostic_information()
+              << std::endl;
     return -1;
   }
 
-  RemoveOldDatabases(uid);
-
-  int ret = pkgmgr_parser_create_and_initialize_db(uid);
-  if (ret < 0) {
-    std::cerr << "Cannot create db" << std::endl;
+  if (ro_only && rw_only) {
+    std::cerr << "Conflicting options : 'ro' and 'rw";
+    return -1;
+  } else if (rw_only && partial_rw) {
+    std::cerr << "Conflicting options : 'rw' and 'partial-rw";
+    return -1;
+  } else if (!IsGlobal(uid) && (ro_only || rw_only || partial_rw)) {
+    std::cerr << "Conflicting options : 'rw' or 'partial-rw'"
+              << " only affect on global user";
     return -1;
   }
 
+  if (!keep_db) {
+    RemoveOldDatabases(uid);
+
+    int ret = pkgmgr_parser_create_and_initialize_db(uid);
+    if (ret < 0) {
+      std::cerr << "Cannot create db" << std::endl;
+      return -1;
+    }
+  }
+
   if (IsGlobal(uid)) {
     // RO location
-    bf::path ro_dir(tzplatform_getenv(TZ_SYS_RO_PACKAGES));
-    InitdbLoadDirectory(uid, ro_dir, true, rw_only);
+    if (!rw_only) {
+      bf::path ro_dir(tzplatform_getenv(TZ_SYS_RO_PACKAGES));
+      InitdbLoadDirectory(uid, ro_dir, true, partial_rw);
+    }
 
-    if (initial)
-      return ret;
+    if (ro_only)
+      return 0;
 
     // RW location
     bf::path rw_dir(tzplatform_getenv(TZ_SYS_RW_PACKAGES));
@@ -189,5 +215,5 @@ int main(int argc, char *argv[]) {
     tzplatform_reset_user();
   }
 
-  return ret;
+  return 0;
 }