Change the way to get root directory for external storage 83/97883/4
authorpr.jung <pr.jung@samsung.com>
Tue, 15 Nov 2016 07:52:12 +0000 (16:52 +0900)
committerpr.jung <pr.jung@samsung.com>
Wed, 16 Nov 2016 05:30:27 +0000 (14:30 +0900)
- When block device is mounted, deviced create a file as /run/external-storage/storage_id
- The file has mount point
- libstorage read file to get root directory for external storage instead to use dbus method call

Change-Id: I685b9c6c8c469814be809b1bd4a8dd39204bf768
Signed-off-by: pr.jung <pr.jung@samsung.com>
src/storage-external.c

index 9eba26d..b5654f2 100755 (executable)
@@ -28,6 +28,9 @@
 #include "log.h"
 #include "storage-external-dbus.h"
 
+#define EXTERNAL_STORAGE_PATH "/run/external-storage"
+#define PATH_LEN              55
+
 static dd_list *cb_list[STORAGE_CALLBACK_MAX];
 
 static int storage_ext_get_dev_state(storage_ext_device *dev,
@@ -353,8 +356,10 @@ int storage_ext_unregister_cb(enum storage_cb_type type, struct storage_cb_info
 
 int storage_ext_get_root(int storage_id, char *path, size_t len)
 {
+       FILE *fp;
        storage_ext_device *dev;
-       int ret;
+       char file_name[PATH_LEN];
+       int ret = 0;
 
        if (storage_id < 0)
                return -ENOTSUP;
@@ -362,25 +367,47 @@ int storage_ext_get_root(int storage_id, char *path, size_t len)
        if (!path)
                return -EINVAL;
 
-       dev = calloc(1, sizeof(storage_ext_device));
-       if (!dev) {
+       snprintf(file_name, PATH_LEN, EXTERNAL_STORAGE_PATH"/%d", storage_id);
+
+       if (access(file_name, R_OK) == 0) {
+               fp = fopen(file_name, "r");
+               if (!fp) {
+                       _E("Cannot get the storage with id (%d, ret:%d)", storage_id, ret); //LCOV_EXCL_LINE
+                       ret = -ENODEV;
+                       goto out;
+               }
+
+               ret = fscanf(fp, "%s", path);
+               if (ret <= 0) {
+                       ret = -ENODEV;
+                       _D("Failed to get path");
+                       fclose(fp);
+                       goto out;
+               }
+               fclose(fp);
+       } else {
+               dev = calloc(1, sizeof(storage_ext_device));
+               if (!dev) {
 //LCOV_EXCL_START System Error
-               _E("calloc failed");
-               return -ENOMEM;
+                       _E("calloc failed");
+                       return -ENOMEM;
 //LCOV_EXCL_STOP
-       }
+               }
 
-       ret = storage_ext_get_device_info(storage_id, dev);
-       if (ret < 0) {
-               _E("Cannot get the storage with id (%d, ret:%d)", storage_id, ret); //LCOV_EXCL_LINE
-               goto out;
+               ret = storage_ext_get_device_info(storage_id, dev);
+               if (ret < 0) {
+                       _E("Cannot get the storage with id (%d, ret:%d)", storage_id, ret); //LCOV_EXCL_LINE
+                       storage_ext_release_device(&dev);
+                       goto out;
+               }
+
+               snprintf(path, len, "%s", dev->mount_point);
+               storage_ext_release_device(&dev);
        }
 
-       snprintf(path, len, "%s", dev->mount_point);
        ret = 0;
 
 out:
-       storage_ext_release_device(&dev);
        return ret;
 }