From: Kichan Kwon Date: Fri, 18 Mar 2016 07:23:04 +0000 (+0900) Subject: Make function to get the root path of (internal, external) storage X-Git-Tag: accepted/tizen/common/20160324.173209~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F81%2F62781%2F6;p=platform%2Fcore%2Fsystem%2Fresourced.git Make function to get the root path of (internal, external) storage - With making this, modify some functions to use new function. Change-Id: Iadeabf9374f1fbd1d9430527c53524b30714cfb2 Signed-off-by: Kichan Kwon --- diff --git a/packaging/resourced.spec b/packaging/resourced.spec index 457b520..4b4a1cb 100644 --- a/packaging/resourced.spec +++ b/packaging/resourced.spec @@ -85,6 +85,7 @@ BuildRequires: pkgconfig(leveldb) BuildRequires: pkgconfig(eventsystem) BuildRequires: pkgconfig(capi-system-info) BuildRequires: pkgconfig(libtzplatform-config) +BuildRequires: pkgconfig(storage) #only for data types BuildRequires: pkgconfig(tapi) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3df8bc0..f5fd19d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -210,6 +210,7 @@ SET(REQUIRES_LIST ${REQUIRES_LIST} libsystemd leveldb eventsystem + storage ) INCLUDE(FindPkgConfig) diff --git a/src/block/block.c b/src/block/block.c index 8134408..d3dcd22 100755 --- a/src/block/block.c +++ b/src/block/block.c @@ -44,9 +44,11 @@ #include "block.h" #include "proc-common.h" #include "appinfo-list.h" +#include "storage-helper.h" #define BLOCK_CONF_FILE "/etc/resourced/block.conf" #define BLOCK_CONF_SECTION "MONITOR" +#define BLOCK_CONF_ACTIVATED "TRUE" static GSList *block_monitor_list; @@ -59,6 +61,7 @@ static void free_exclude_key(gpointer data) static int load_block_config(struct parse_result *result, void *user_data) { struct block_monitor_info *bmi; + char *monitoring_path; if (!result) return RESOURCED_ERROR_NO_DATA; @@ -69,15 +72,23 @@ static int load_block_config(struct parse_result *result, void *user_data) if (!strstr(result->section, BLOCK_CONF_SECTION)) return RESOURCED_ERROR_NO_DATA; - if (MATCH(result->name, "path")) { - bmi = calloc(1, sizeof(struct block_monitor_info)); - if (!bmi) { - _E("Failed to create monitor info"); - return RESOURCED_ERROR_OUT_OF_MEMORY; + if (MATCH(result->name, "activate")) { + if (!strncmp(result->value, BLOCK_CONF_ACTIVATED, + sizeof(BLOCK_CONF_ACTIVATED))) { + bmi = calloc(1, sizeof(struct block_monitor_info)); + if (!bmi) { + _E("Failed to create monitor info"); + return RESOURCED_ERROR_OUT_OF_MEMORY; + } + if (get_storage_root_path(INTERNAL, &monitoring_path) != RESOURCED_ERROR_NONE) { + _E("Failed to find monitoring path"); + return RESOURCED_ERROR_FAIL; + } + _D("Start to monitor %s", monitoring_path); + strncpy(bmi->path, monitoring_path, sizeof(bmi->path)); + free(monitoring_path); + block_monitor_list = g_slist_prepend(block_monitor_list, bmi); } - strncpy(bmi->path, result->value, MAX_PATH_LENGTH-1); - block_monitor_list = g_slist_prepend(block_monitor_list, bmi); - } else if (MATCH(result->name, "mode")) { bmi = (struct block_monitor_info *)g_slist_nth_data(block_monitor_list, 0); SET_CONF(bmi->mode, convert_fanotify_mode(result->value)); diff --git a/src/block/block.conf b/src/block/block.conf index 1cdd27c..57260d0 100644 --- a/src/block/block.conf +++ b/src/block/block.conf @@ -1,8 +1,9 @@ +# activate {TRUE, FALSE} # mode {DISABLE, ACCESS, READ, WRITE} # logging { DLOG = 0x1, FILE = 0x2, DB = 0x4} # When adding new monitor, "configend=NULL" should be included for distributing other categories. -[MONITOR1] -path=/opt/usr/media +[MONITOR] +activate=TRUE mode=WRITE include=DOWNLOADABLE exclude=SLP_debug diff --git a/src/common/storage-helper.c b/src/common/storage-helper.c index ba41c23..d960203 100644 --- a/src/common/storage-helper.c +++ b/src/common/storage-helper.c @@ -29,6 +29,7 @@ #include "storage-helper.h" #include "trace.h" #include +#include #define PATH_MAX 256 #define INTERNAL_MEMORY_PATH "/opt/usr" @@ -54,33 +55,82 @@ bool is_mounted(const char* path) return ret; } -resourced_ret_c storage_get_size(int type, struct statvfs *buf) +struct rd_storage { + int id; + int type; +}; + +static bool get_storage_id(int sid, storage_type_e type, storage_state_e state, + const char *path, void *userData) { - int ret; - char path[PATH_MAX] = ""; - char errbuf[PATH_MAX]; + struct rd_storage *target = (struct rd_storage*)userData; + + if (type == target->type && state == STORAGE_STATE_MOUNTED) { + target->id = sid; + return false; + } + return true; +} + +resourced_ret_c get_storage_root_path(int type, char **path) +{ + struct rd_storage target; if (type == INTERNAL) - snprintf(path, sizeof(path),"%s", INTERNAL_MEMORY_PATH); + target.type = STORAGE_TYPE_INTERNAL; else if (type == EXTERNAL) - snprintf(path, sizeof(path), "%s", EXTERNAL_MEMORY_PATH); + target.type = STORAGE_TYPE_EXTERNAL; else { - _E("Unsupported storage type:%d", type); + _E("Invalid storage type"); return RESOURCED_ERROR_INVALID_PARAMETER; } + if (storage_foreach_device_supported(get_storage_id, + &target) != STORAGE_ERROR_NONE) { + _E("Failed to get storage ID"); + return RESOURCED_ERROR_FAIL; + } + + if (storage_get_root_directory(target.id, path) + != STORAGE_ERROR_NONE) { + _E("Failed to get root path of storage"); + return RESOURCED_ERROR_FAIL; + } + + return RESOURCED_ERROR_NONE; +} + +resourced_ret_c storage_get_size(int type, struct statvfs *buf) +{ + int ret; + char *path; + char errbuf[PATH_MAX]; + + if (get_storage_root_path(type, &path) != RESOURCED_ERROR_NONE) { + _E("Failed to get storage path"); + goto fail; + } + _I("Path:%s", path); if (type == EXTERNAL) { - if (!is_mounted(EXTERNAL_MEMORY_PATH)) { + if (!is_mounted(path)) { memset(buf, 0, sizeof(struct statvfs)); - return RESOURCED_ERROR_NONE; + goto success; } } ret = statvfs(path, buf); if (ret) { _E("statvfs() failed. Path:%s err:%s", path, strerror_r(errno, errbuf, sizeof(errbuf))); - return RESOURCED_ERROR_FAIL; + goto fail; } + goto success; + +fail: + free(path); + return RESOURCED_ERROR_FAIL; + +success: + free(path); return RESOURCED_ERROR_NONE; } diff --git a/src/common/storage-helper.h b/src/common/storage-helper.h index 39209dd..e99fc21 100644 --- a/src/common/storage-helper.h +++ b/src/common/storage-helper.h @@ -37,6 +37,8 @@ enum storage_type { bool is_mounted(const char* path); +resourced_ret_c get_storage_root_path(int type, char **path); + /** * @desc gets storage details * @param type-INTERNAL/EXTERNAL, buf-storage details