Add command in buxton2ctl to remove trash data 53/119853/1
authorJiwoong Im <jiwoong.im@samsung.com>
Tue, 14 Mar 2017 07:14:14 +0000 (16:14 +0900)
committerJiwoong Im <jiwoong.im@samsung.com>
Mon, 20 Mar 2017 09:33:01 +0000 (18:33 +0900)
- When r / o area is upgraded after key deletion,
  garbage data may exist in normal db.
  To remove garbage data, add remove-garbage-data command in buxton2ctl

Change-Id: I569c4eccba8778c02e355cf28af7729f50df5a09
Signed-off-by: Jiwoong Im <jiwoong.im@samsung.com>
backend/gdbm.c
backend/sqlite.c
client/c_direct.c
client/c_direct.h
client/c_main.c
client/c_proc.h
common/backend.h
common/direct.c
common/direct.h
daemon/daemon.c

index d62be1a..7fc363d 100644 (file)
@@ -273,7 +273,8 @@ static int unset_value(const char *dbpath, const char *key)
        return 0;
 }
 
-static int list_keys(const char *dbpath, char ***keys, unsigned int *klen)
+static int list_keys(const char *dbpath, char ***keys, unsigned int *klen,
+               bool readonly)
 {
        GDBM_FILE db;
        GList *list;
@@ -288,7 +289,7 @@ static int list_keys(const char *dbpath, char ***keys, unsigned int *klen)
                return -1;
        }
 
-       db = open_gdbm(dbpath, true);
+       db = open_gdbm(dbpath, readonly);
        if (!db)
                return -1;
 
index 46fb0f3..477977c 100644 (file)
@@ -390,7 +390,8 @@ end:
        return ret;
 }
 
-static int list_keys(const char *dbpath, char ***keys, unsigned int *klen)
+static int list_keys(const char *dbpath, char ***keys, unsigned int *klen,
+               bool readonly)
 {
        sqlite3 *db;
        GList *list;
@@ -409,7 +410,7 @@ static int list_keys(const char *dbpath, char ***keys, unsigned int *klen)
                return -1;
        }
 
-       db = open_sqlite3(dbpath, true);
+       db = open_sqlite3(dbpath, readonly);
        if (!db)
                return -1;
 
index 1029aac..469b595 100644 (file)
@@ -447,7 +447,7 @@ int c_direct_list(const struct buxton_layer *layer,
        if (r == -1)
                return -1;
 
-       r = direct_list(layer, &keys, NULL);
+       r = direct_list(layer, &keys, NULL, true);
 
        c_exit();
 
@@ -469,3 +469,63 @@ int c_direct_list(const struct buxton_layer *layer,
        return 0;
 }
 
+int c_direct_remove_garbage(const struct buxton_layer *layer,
+               UNUSED const char *key, UNUSED const char *value,
+               UNUSED const char *rpriv, UNUSED const char *wpriv)
+{
+       struct buxton_layer *normal_layer;
+       struct buxton_layer *base_layer;
+       struct buxton_value val;
+       char **keys;
+       unsigned int len;
+       int r;
+       int i;
+       char err_buf[BUFFER_SIZE];
+
+       if (!layer) {
+               errno = EINVAL;
+               strerror_r(errno, err_buf, sizeof(err_buf));
+               bxt_err("Remove garbage data : Layer '%s' : %s",
+                               layer ? buxton_layer_get_name(layer) : "",
+                               err_buf);
+               return -1;
+       }
+
+       r = c_init();
+       if (r == -1)
+               return -1;
+
+       normal_layer = buxton_create_layer(layer->name);
+       buxton_layer_set_type(normal_layer, BUXTON_LAYER_NORMAL);
+       base_layer = buxton_create_layer(layer->name);
+       buxton_layer_set_type(base_layer, BUXTON_LAYER_BASE);
+
+       r = direct_list(normal_layer, &keys, &len, false);
+       if (r == -1) {
+               buxton_free_layer(normal_layer);
+               buxton_free_layer(base_layer);
+               return -1;
+       }
+
+       i = 0;
+       while (i < len) {
+               r = direct_get(base_layer, keys[i], &val);
+               if (r == -1 && errno == ENOENT) {
+                       r = direct_unset(normal_layer, keys[i]);
+                       if (r == -1) {
+                               strerror_r(errno, err_buf, sizeof(err_buf));
+                               bxt_err("Remove garbage data: Layer '%s' Key '%s': %s",
+                                               buxton_layer_get_name(normal_layer), keys[i],
+                                               err_buf);
+                       }
+               }
+               value_free(&val);
+               i++;
+       }
+
+       c_exit();
+       buxton_free_layer(normal_layer);
+       buxton_free_layer(base_layer);
+
+       return 0;
+}
index 0fdc7f8..1a9a178 100644 (file)
@@ -90,4 +90,7 @@ int c_direct_create_double(const struct buxton_layer *layer,
 int c_direct_create_bool(const struct buxton_layer *layer,
                const char *key, const char *value,
                const char *rpriv, const char *wpriv);
+int c_direct_remove_garbage(UNUSED const struct buxton_layer *layer,
+               UNUSED const char *key, UNUSED const char *value,
+               UNUSED const char *rpriv, UNUSED const char *wpriv);
 
index bf99146..dcd63a8 100644 (file)
@@ -246,6 +246,14 @@ static const struct command const commands[] = {
                .func    = c_disable_security,
                .dfunc   = NULL,
        },
+       {
+               .name    = "remove-garbage-data",
+               .summary = "Remove garbage data",
+               .nargs   = 1,
+               .usage   = "LAYER",
+               .func    = c_direct_remove_garbage,
+               .dfunc   = c_direct_remove_garbage,
+       },
 };
 
 static const struct command *find_comm(const char *name)
@@ -317,7 +325,7 @@ static void usage(const char *name)
        printf("  Command:\n");
 
        for (i = 0; i < sizeof(commands) / sizeof(commands[0]); i++)
-               printf("%16s - %s\n", commands[i].name, commands[i].summary);
+               printf("%20s - %s\n", commands[i].name, commands[i].summary);
 
        printf("\n");
        printf("  Example:\n");
index bc8429b..345386f 100644 (file)
@@ -93,3 +93,6 @@ int c_enable_security(UNUSED const struct buxton_layer *layer,
 int c_disable_security(UNUSED const struct buxton_layer *layer,
                UNUSED const char *key, UNUSED const char *value,
                UNUSED const char *rpriv, UNUSED const char *wpriv);
+int c_remove_trash_data(UNUSED const struct buxton_layer *layer,
+               UNUSED const char *key, UNUSED const char *value,
+               UNUSED const char *rpriv, UNUSED const char *wpriv);
index a4d4c43..f3c29c3 100644 (file)
@@ -37,7 +37,7 @@ typedef int (*backend_get_value)(const char *dbpath,
                const char *key, void **data, int *dlen, bool readonly);
 typedef int (*backend_unset_value)(const char *dbpath, const char *key);
 typedef int (*backend_list_keys)(const char *dbpath,
-               char ***keys, unsigned int *klen);
+               char ***keys, unsigned int *klen, bool readonly);
 
 struct backend {
        const char *name;
index 9c9e3ad..9dc113f 100644 (file)
@@ -445,7 +445,7 @@ static int comp_str(const void *pa, const void *pb)
 }
 
 int direct_list(const struct buxton_layer *layer,
-               char ***names, unsigned int *len)
+               char ***names, unsigned int *len, bool is_base)
 {
        int r;
        const struct layer *ly;
@@ -471,11 +471,13 @@ int direct_list(const struct buxton_layer *layer,
                return -1;
        }
 
-       r = get_path(layer->uid, BUXTON_LAYER_BASE, ly, path, sizeof(path));
+       r = get_path(layer->uid, is_base ?
+                       BUXTON_LAYER_BASE : BUXTON_LAYER_NORMAL,
+                       ly, path, sizeof(path));
        if (r == -1)
                return -1;
 
-       r = backend->list_keys(path, names, &_len);
+       r = backend->list_keys(path, names, &_len, is_base);
        if (r == -1) {
                bxt_err("List: list_keys: %d", errno);
                return -1;
index 6953906..ac0ef0a 100644 (file)
@@ -35,7 +35,7 @@ int direct_create(const struct buxton_layer *layer, const char *key,
 int direct_unset(const struct buxton_layer *layer, const char *key);
 
 int direct_list(const struct buxton_layer *layer,
-               char ***names, unsigned int *len);
+               char ***names, unsigned int *len, bool is_base);
 
 int direct_get_priv(const struct buxton_layer *layer,
                const char *key, enum buxton_priv_type type, char **priv);
index a827263..b50ffb2 100644 (file)
@@ -368,7 +368,7 @@ static void proc_list(struct bxt_client *cli,
        assert(rqst);
        assert(resp);
 
-       r = direct_list(rqst->layer, &resp->names, &resp->nmlen);
+       r = direct_list(rqst->layer, &resp->names, &resp->nmlen, true);
        resp->res = (r == -1) ? errno : 0;
 }