From 4ebeea6dad91d67bc99feab3d90adaf7983d9f90 Mon Sep 17 00:00:00 2001 From: Jiwoong Im Date: Wed, 26 Jul 2017 17:14:48 +0900 Subject: Add command for os upgrade in preference_tool Change-Id: I4f5725148f4ab308a9db5273269e220a00cea16a Signed-off-by: Jiwoong Im --- preference/preference_tool.c | 181 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) diff --git a/preference/preference_tool.c b/preference/preference_tool.c index 14ff562..c8be42a 100755 --- a/preference/preference_tool.c +++ b/preference/preference_tool.c @@ -694,6 +694,10 @@ static void _print_help(void) printf("preference_tool get \n"); printf("ex) preference_tool get "); printf("org.tizen.preferencetest test_key\n\n"); + + printf("[Convert preference file name from plain text]\n"); + printf("preference_tool convert_file_name \n"); + printf("ex) preference_tool convert_file_name /opt/usr/apps\n\n"); } static void _preference_set_key(const char *pkgid, const char *key, @@ -786,6 +790,175 @@ static void write_history(const char *str) fclose(fp); } +static int _add_key_info_to_file(const char *key_path, const char *key) +{ + FILE *fp = NULL; + size_t keyname_len; + char *buf = NULL; + int read_size; + int ret; + long file_size; + + fp = fopen(key_path, "r+"); + if (fp == NULL) { + printf("fopen() failed.(%d/%s).fp is null.\n", + errno, strerror(errno)); + return -1; + } + + fseek(fp, 0, SEEK_END); + file_size = ftell(fp); + rewind(fp); + + buf = (char *)calloc(1, file_size); + if (buf == NULL) { + printf("Out of memory\n"); + ret = -1; + goto out; + } + + /* write keyname and size */ + read_size = fread(buf, 1, file_size, fp); + if ((read_size <= 0)) { + printf("preference read key-val data error(%d/%s)\n", + errno, strerror(errno)); + ret = -1; + goto out; + } + + rewind(fp); + + keyname_len = strlen(key); + ret = fwrite((void *)&keyname_len, sizeof(int), 1, fp); + if (ret <= 0) { + printf("preference write key name length error(%d/%s)\n", + errno, strerror(errno)); + ret = -1; + goto out; + } + + ret = fwrite((void *)key, keyname_len, 1, fp); + if (ret <= 0) { + printf("preference write key name error(%d/%s)\n", + errno, strerror(errno)); + ret = -1; + goto out; + } + ret = fwrite((void *)buf, read_size, 1, fp); + if (ret <= 0) { + printf("preference write key-val data error(%d/%s)\n", + errno, strerror(errno)); + ret = -1; + goto out; + } + + ret = 0; + +out: + if (fp) + fclose(fp); + if (buf) + free(buf); + + return ret; +} + +static int _convert_pref_file(const char *pref_dir) +{ + DIR *dir; + struct dirent *ent; + char old_file[BUF_LEN]; + char new_file[BUF_LEN]; + gchar *convert_key; + char _key[PREFERENCE_KEY_PATH_LEN] = {0,}; + char *chrptr = NULL; + + dir = opendir(pref_dir); + if (!dir) + return -1; + + while ((ent = readdir(dir))) { + if (ent->d_type != DT_REG) + continue; + + snprintf(old_file, sizeof(old_file), "%s/%s", pref_dir, + ent->d_name); + + strncpy(_key, ent->d_name, PREFERENCE_KEY_PATH_LEN - 1); + + chrptr = strchr((const char*)_key, DELIMITER); + if (chrptr) { + chrptr = strchr((const char*)_key, DELIMITER); + while (chrptr) { + _key[chrptr-_key] = '/'; + chrptr = strchr((const char*)chrptr + 1, DELIMITER); + } + } + + convert_key = g_compute_checksum_for_string(G_CHECKSUM_SHA1, + _key, strlen(_key)); + if (convert_key == NULL) { + printf("fail to convert key\n"); + closedir(dir); + return -1; + } + + snprintf(new_file, sizeof(new_file), "%s/%s", pref_dir, + convert_key); + g_free(convert_key); + + if (rename(old_file, new_file) < 0) { + printf("rename %s to %s failed.(%d/%s)\n", + old_file, new_file, + errno, strerror(errno)); + } + + if (_add_key_info_to_file(new_file, _key) < 0) + printf("convert %s file failed\n", new_file); + } + + closedir(dir); + return 0; +} + +static int _convert_file_name(const char *app_dir) +{ + DIR *dir; + struct dirent *ent; + char buf[BUF_LEN]; + int res; + + dir = opendir(app_dir); + if (!dir) { + printf("failed to open app dir (%s)\n", app_dir); + return -1; + } + + while ((ent = readdir(dir))) { + if (ent->d_type != DT_DIR) + continue; + + if (strcmp(ent->d_name, ".") == 0 || + strcmp(ent->d_name, "..") == 0) + continue; + + snprintf(buf, sizeof(buf), "%s/%s/data/.pref", + app_dir, ent->d_name); + if (access(buf, F_OK) == -1) + continue; + + res = _convert_pref_file(buf); + if (res < 0) { + printf("_convert_pref_file error (%s)\n", buf); + closedir(dir); + return -1; + } + } + + closedir(dir); + return 0; +} + int main(int argc, char *argv[]) { int res; @@ -849,6 +1022,14 @@ int main(int argc, char *argv[]) _preference_set_key(argv[2], argv[3], argv[4], argv[5]); else _print_help(); + } else if (strcmp(argv[1], "convert_file_name") == 0) { + res = _convert_file_name(argv[2]); + + if (res < 0) { + printf("converting preference file name failed (%s)\n", + argv[2]); + return -1; + } } else { _print_help(); } -- cgit v1.2.3