block: make file as /run/external-storage/id for mounted sdcard 82/97882/7
authorpr.jung <pr.jung@samsung.com>
Tue, 15 Nov 2016 01:56:33 +0000 (10:56 +0900)
committerJung <pr.jung@samsung.com>
Fri, 18 Nov 2016 06:30:55 +0000 (22:30 -0800)
- make file before sending DeviceChanged signal for mount.
- remove file before sending DeviceBlocked signal for unmount

Change-Id: I365f228cb44b8f07da09b23c2968669d24bc88b7
Signed-off-by: pr.jung <pr.jung@samsung.com>
src/block/block.c

index 85be46b..d88bad3 100644 (file)
@@ -85,6 +85,9 @@
 
 #define BLOCK_CONF_FILE         "/etc/deviced/block.conf"
 
+#define EXTERNAL_STORAGE_PATH  "/run/external-storage"
+#define PATH_LEN               55
+
 /* Minimum value of block id */
 #define BLOCK_ID_MIN 10
 /* For 2.4 Backward Compatibility */
@@ -276,6 +279,39 @@ static int block_get_new_id(void)
        return -ENOENT;
 }
 
+static void remove_file(int id)
+{
+       char file_name[PATH_LEN];
+       int ret;
+
+       if (id < 0)
+               return;
+
+       snprintf(file_name, sizeof(file_name), EXTERNAL_STORAGE_PATH"/%d", id);
+
+       ret = remove(file_name);
+       if (ret < 0)
+               _E("Fail to remove %s. errno: %d", file_name, errno);
+}
+
+static void create_file(int id, char *mount_point)
+{
+       FILE *fp;
+       char file_name[PATH_LEN];
+
+       if (id < 0)
+               return;
+
+       snprintf(file_name, PATH_LEN, EXTERNAL_STORAGE_PATH"/%d", id);
+
+       fp = fopen(file_name, "w+");
+       if (fp) {
+               fprintf(fp, "%s", mount_point);
+               fclose(fp);
+       } else
+               _E("Fail to open %s", file_name);
+}
+
 static void signal_device_blocked(struct block_device *bdev)
 {
        struct block_data *data;
@@ -320,7 +356,6 @@ static void signal_device_blocked(struct block_device *bdev)
        snprintf(str_id, sizeof(str_id), "%d", data->id);
        arr[12] = str_id;
 
-
        broadcast_block_edbus_signal(DEVICED_PATH_BLOCK_MANAGER,
                        DEVICED_INTERFACE_BLOCK_MANAGER,
                        BLOCK_DEVICE_BLOCKED,
@@ -675,6 +710,9 @@ static void free_block_device(struct block_device *bdev)
        if (thread_id < 0 || thread_id >= THREAD_MAX)
                return;
 
+       /* Remove file for block device /run/external-storage/id */
+       remove_file(bdev->data->id);
+
        pthread_mutex_lock(&(th_manager[thread_id].mutex));
 
        th_manager[thread_id].num_dev--;
@@ -1937,6 +1975,10 @@ static int add_block_device(struct udev_device *dev, const char *devnode)
        pthread_mutex_lock(&(th_manager[thread_id].mutex));
        th_manager[thread_id].num_dev++;
        DD_LIST_APPEND(th_manager[thread_id].block_dev_list, bdev);
+
+       /* Create file for block device /run/external-storage/id */
+       create_file(bdev->data->id, bdev->data->mount_point);
+
        pthread_mutex_unlock(&(th_manager[thread_id].mutex));
 
        ret = add_operation(bdev, BLOCK_DEV_INSERT, NULL, (void *)data);
@@ -2313,6 +2355,12 @@ static DBusMessage *request_mount_block(E_DBus_Object *obj,
                goto out;
        }
 
+       if (bdev->data->state == BLOCK_MOUNT) {
+               _I("%s is already mounted", bdev->data->devnode);
+               ret = -EALREADY;
+               goto out;
+       }
+
        /* if requester want to use a specific mount point */
        if (mount_point && strncmp(mount_point, "", 1) != 0) {
                ret = change_mount_point(bdev, mount_point);
@@ -2320,6 +2368,9 @@ static DBusMessage *request_mount_block(E_DBus_Object *obj,
                        ret = -EPERM;
                        goto out;
                }
+
+               /* Modify /run/external-storage/id file */
+               create_file(bdev->data->id, bdev->data->mount_point);
        }
 
        ret = add_operation(bdev, BLOCK_DEV_MOUNT, msg, NULL);
@@ -2826,6 +2877,7 @@ static int mount_root_path_tmpfs(void)
 
 static void block_init(void *data)
 {
+       struct stat buf;
        int ret;
        int i;
 
@@ -2867,6 +2919,19 @@ static void block_init(void *data)
                pthread_mutex_init(&(th_manager[i].mutex), NULL);
                pthread_cond_init(&(th_manager[i].cond), NULL);
        }
+
+       ret = stat(EXTERNAL_STORAGE_PATH, &buf);
+       if (ret < 0) {
+               ret = mkdir(EXTERNAL_STORAGE_PATH, 644);
+               if (ret < 0)
+                       _E("Failed to make directory: %d", errno);
+       } else if (!S_ISDIR(buf.st_mode)) {
+               remove(EXTERNAL_STORAGE_PATH);
+               ret = mkdir(EXTERNAL_STORAGE_PATH, 644);
+               if (ret < 0)
+                       _E("Failed to make directory: %d", errno);
+       } else
+               chmod(EXTERNAL_STORAGE_PATH, 644);
 }
 
 static void block_exit(void *data)