Add -f option on sp_initdb tool 99/132999/4
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 8 Jun 2017 23:37:00 +0000 (08:37 +0900)
committerHwanKyu Jhun <h.jhun@samsung.com>
Thu, 8 Jun 2017 23:50:33 +0000 (23:50 +0000)
When upgrading the platform, the syspopup script executes
the sp_initdb tool with -f option.
If the syspopup db file already exists, the tool doesn't initialize it.

Change-Id: I355dd204983c28ed08ca0a0e917e408d11c773f9
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
scripts/502.syspopup_upgrade.sh
tool/CMakeLists.txt
tool/sp_initdb.c

index 32fdfe21ffc7c29569df0df679a5cd243d6b5693..af102a74a605bd24fd5f76997a523f8935318a7d 100755 (executable)
@@ -8,4 +8,4 @@
 PATH=/bin:/usr/bin:/sbin:/usr/sbin
 
 # Initialize DB
-sp_initdb
+sp_initdb -f
index ccff37bb03de3ccb6e5f185f46a0ee2fd8c5734b..8d4a7c2dfe922ba90a2affcd9085d032961a5380 100644 (file)
@@ -5,6 +5,7 @@ pkg_check_modules(PKGS REQUIRED
        capi-system-info
        sqlite3
        libsmack
+       glib-2.0
        )
 
 FOREACH(FLAGS ${PKGS_CFLAGS})
index a8bbcf8a291ef60e8bf94ce8d5e073bda9a7fe9c..7f1fbc24607b7a17bd6de1149b8676dfe6970880 100755 (executable)
@@ -21,6 +21,7 @@
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/smack.h>
+#include <glib.h>
 #include <sqlite3.h>
 #include <tzplatform_config.h>
 #include <system_info.h>
@@ -265,15 +266,40 @@ static int __init_db(void)
 int main(int argc, char **argv)
 {
        int ret;
+       GOptionContext *context;
+       GError *error = NULL;
+       gpointer opt_force = NULL;
+       GOptionEntry entries[] = {
+               {"force", 'f', 0, G_OPTION_ARG_NONE, &opt_force, "Remove an existing db file and initialize it", NULL},
+               {NULL}
+       };
 
        if (getuid() != ROOT_UID) {
                printf("This binary should be run as root user\n");
                return -1;
        }
 
-       ret = __unlink_db();
-       if (ret < 0)
+       context = g_option_context_new(NULL);
+       g_option_context_add_main_entries(context, entries, NULL);
+       if (!g_option_context_parse(context, &argc, &argv, &error)) {
+               printf("%s: %s\n", argv[0], error->message);
+               g_option_context_free(context);
+               g_clear_error(&error);
                return -1;
+       }
+       g_option_context_free(context);
+
+       if (opt_force) {
+               ret = __unlink_db();
+               if (ret < 0)
+                       return -1;
+       } else {
+               ret = access(PATH_DB, F_OK);
+               if (ret == 0) {
+                       printf("The db(%s) file already exists.\n", PATH_DB);
+                       return 0;
+               }
+       }
 
        return __init_db();
 }