--- /dev/null
+/*
+ * Vconf migration tool (2.4 -> 3.0)
+ *
+ * Copyright (C) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License)
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <dirent.h>
+#include <inttypes.h>
+#include <unistd.h>
+
+#include <glib.h>
+#include <vconf.h>
+
+#define BUF_LEN 1024
+
+static void _get_vconf_keys(const char *path, keylist_t *keylist)
+{
+ int type = 0;
+ FILE *fp = NULL;
+ int read_size = 0;
+ char *keyname;
+ int i;
+
+ fp = fopen(path, "r+");
+ if (fp == NULL) {
+ fprintf(stderr, "unable to open key path[%s] : %d", path, errno);
+ return;
+ }
+
+ read_size = fread((void *)&type, sizeof(int), 1, fp);
+ if ((read_size <= 0) || (read_size > sizeof(int))) {
+ if (!ferror(fp)) {
+ fprintf(stderr, "number of read items for type is 0 with false ferror. err : %d", errno);
+ errno = ENODATA;
+ }
+ goto out_func;
+ }
+
+ keyname = strrchr(path, '/');
+
+ while (*(keyname - 1) != '/')
+ keyname--;
+
+ i = 0;
+ while (keyname[i] != '\0') {
+ if (keyname[i] == '+')
+ keyname[i] = '/';
+ i++;
+ }
+
+ /* read data value */
+ switch (type) {
+ case VCONF_TYPE_INT:
+ {
+ int value_int = 0;
+ read_size = fread((void *)&value_int, sizeof(int), 1, fp);
+ if ((read_size <= 0) || (read_size > sizeof(int))) {
+ if (!ferror(fp))
+ fprintf(stderr, "number of read items for value is wrong. err : %d", errno);
+ goto out_func;
+ } else {
+ vconf_keylist_add_int(keylist, keyname, value_int);
+ }
+
+ break;
+ }
+ case VCONF_TYPE_DOUBLE:
+ {
+ double value_dbl = 0;
+ read_size = fread((void *)&value_dbl, sizeof(double), 1, fp);
+ if ((read_size <= 0) || (read_size > sizeof(double))) {
+ if (!ferror(fp))
+ fprintf(stderr, "number of read items for value is wrong. err : %d", errno);
+ goto out_func;
+ } else {
+ vconf_keylist_add_dbl(keylist, keyname, value_dbl);
+ }
+
+ break;
+ }
+ case VCONF_TYPE_BOOL:
+ {
+ int value_int = 0;
+ read_size = fread((void *)&value_int, sizeof(int), 1, fp);
+ if ((read_size <= 0) || (read_size > sizeof(int))) {
+ if (!ferror(fp))
+ fprintf(stderr, "number of read items for value is wrong. err : %d", errno);
+ goto out_func;
+ } else {
+ vconf_keylist_add_bool(keylist, keyname, value_int);
+ }
+
+ break;
+ }
+ case VCONF_TYPE_STRING:
+ {
+ char file_buf[BUF_LEN] = {0,};
+ char *value = NULL;
+ int value_size = 0;
+
+ while (fgets(file_buf, sizeof(file_buf), fp)) {
+ if (value) {
+ value_size = value_size + strlen(file_buf);
+ value = (char *) realloc(value, value_size);
+ if (value == NULL)
+ break;
+ strncat(value, file_buf, strlen(file_buf));
+ } else {
+ value_size = strlen(file_buf) + 1;
+ value = (char *)malloc(value_size);
+ if (value == NULL)
+ break;
+ memset(value, 0x00, value_size);
+ strncpy(value, file_buf, strlen(file_buf));
+ }
+ }
+
+ if (ferror(fp)) {
+ fprintf(stderr, "fgets error for getting string key : %d", errno);
+ } else {
+ if (value)
+ vconf_keylist_add_str(keylist, keyname, value);
+ else
+ vconf_keylist_add_str(keylist, keyname, "");
+ }
+ if (value)
+ free(value);
+
+ break;
+ }
+ }
+
+out_func:
+ fclose(fp);
+}
+
+static int _load_vconf_dir(const char *directory)
+{
+ DIR *dir;
+ struct dirent file_info;
+ struct dirent *result;
+ char buf[BUF_LEN];
+ keylist_t *keylist = vconf_keylist_new();
+
+ dir = opendir(directory);
+ if (!dir)
+ return -1;
+
+ while (readdir_r(dir, &file_info, &result) == 0) {
+ if (result == NULL)
+ break;
+ if (file_info.d_type != DT_REG)
+ continue;
+
+ snprintf(buf, sizeof(buf), "%s/%s", directory,
+ file_info.d_name);
+
+ _get_vconf_keys(buf, keylist);
+ }
+
+ vconf_set(keylist);
+
+ vconf_keylist_free(keylist);
+ closedir(dir);
+
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+ if (_load_vconf_dir("/opt/var/kdb/db") < 0)
+ fprintf(stderr, "fail to migrate db backend keys : %d", errno);
+
+ if (_load_vconf_dir("/opt/var/kdb/file") < 0)
+ fprintf(stderr, "fail to migrate db backend keys : %d", errno);
+
+ return EXIT_SUCCESS;
+}