Make function to get the root path of (internal, external) storage 81/62781/6
authorKichan Kwon <k_c.kwon@samsung.com>
Fri, 18 Mar 2016 07:23:04 +0000 (16:23 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Thu, 24 Mar 2016 09:51:19 +0000 (18:51 +0900)
- With making this, modify some functions to use new function.

Change-Id: Iadeabf9374f1fbd1d9430527c53524b30714cfb2
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
packaging/resourced.spec
src/CMakeLists.txt
src/block/block.c
src/block/block.conf
src/common/storage-helper.c
src/common/storage-helper.h

index 457b520..4b4a1cb 100644 (file)
@@ -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)
index 3df8bc0..f5fd19d 100644 (file)
@@ -210,6 +210,7 @@ SET(REQUIRES_LIST ${REQUIRES_LIST}
        libsystemd
        leveldb
        eventsystem
+       storage
   )
 
 INCLUDE(FindPkgConfig)
index 8134408..d3dcd22 100755 (executable)
 #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));
index 1cdd27c..57260d0 100644 (file)
@@ -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
index ba41c23..d960203 100644 (file)
@@ -29,6 +29,7 @@
 #include "storage-helper.h"
 #include "trace.h"
 #include <mntent.h>
+#include <storage.h>
 
 #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;
 }
index 39209dd..e99fc21 100644 (file)
@@ -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