Fix duplicated code for BLOCK_DEV_FORMAT operation 32/281132/7
authorSangYoun Kwak <sy.kwak@samsung.com>
Tue, 13 Sep 2022 06:59:34 +0000 (15:59 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Tue, 13 Sep 2022 09:25:10 +0000 (18:25 +0900)
Change-Id: Ifeb2f2f307eacacc4098d898bba6886148af39c6
Signed-off-by: SangYoun Kwak <sy.kwak@samsung.com>
src/block/block.c

index 8115a98..6d0121b 100644 (file)
@@ -3173,50 +3173,24 @@ static GVariant *request_private_unmount_block(GDBusConnection *conn,
        return request_unmount_block(conn, sender, path, iface, name, param, invocation, user_data, true);
 }
 
-static GVariant *request_format_block(GDBusConnection *conn,
-               const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
-               GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
+static int process_request_format_block(const gchar *sender, GDBusMethodInvocation *invocation,
+               struct block_device *bdev, int option, const char *type)
 {
-       struct block_device *bdev;
        struct format_data *fdata;
        pid_t pid;
-       int id;
-       int option;
-       int ret = -EBADMSG;
        int prev_state;
-
-       if (!block_control) {
-               _D("Block module is disabled.");
-               ret = -EPERM;
-               goto out;
-       }
-
-       g_variant_get(param, "(ii)", &id, &option);
-
-       bdev = find_block_device_by_id(id);
-       if (!bdev) {
-               _E("Failed to find (%d) in the device list.", id);
-               goto out;
-       }
-
-       if (bdev->data->block_type == BLOCK_EXTENDEDSD_DEV ||
-           !strncmp(bdev->data->fs_type, LUKS_NAME, strlen(LUKS_NAME))) {
-               _D("Format dbus request for extended internal storage is blocked.");
-               ret = -EPERM;
-               goto out;
-       }
+       int ret = -EBADMSG;
 
        pid = gdbus_connection_get_sender_pid(NULL, sender);
        if (bdev->on_private_op != REQ_NORMAL && pid != bdev->private_pid) {
                _E("Failed to format on private state.");
-               ret = -EPERM;
-               goto out;
+               return -EPERM;
        }
 
-       fdata = get_format_data(NULL, option);
+       fdata = get_format_data(type, option);
        if (!fdata) {
                _E("Failed to get format data.");
-               goto out;
+               return ret;
        }
 
        prev_state = bdev->data->state;
@@ -3229,7 +3203,7 @@ static GVariant *request_format_block(GDBusConnection *conn,
                if (ret < 0) {
                        _E("Failed to add operation(unmount, %s).", bdev->data->devnode);
                        release_format_data(fdata);
-                       goto out;
+                       return ret;
                }
        }
 
@@ -3243,10 +3217,48 @@ static GVariant *request_format_block(GDBusConnection *conn,
        if (prev_state == BLOCK_MOUNT) {
                if (add_operation(bdev, BLOCK_DEV_MOUNT, NULL, NULL) < 0) {
                        _E("Failed to add operation(mount, %s).", bdev->data->devnode);
-                       goto out;
+                       return ret;
                }
        }
 
+       return 0;
+}
+
+static GVariant *request_format_block(GDBusConnection *conn,
+               const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
+               GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
+{
+       struct block_device *bdev;
+       int id;
+       int option;
+       int ret = -EBADMSG;
+
+       if (!block_control) {
+               _D("Block module is disabled.");
+               ret = -EPERM;
+               goto out;
+       }
+
+       g_variant_get(param, "(ii)", &id, &option);
+
+       bdev = find_block_device_by_id(id);
+       if (!bdev) {
+               _E("Failed to find (%d) in the device list.", id);
+               goto out;
+       }
+
+       if (bdev->data->block_type == BLOCK_EXTENDEDSD_DEV ||
+           !strncmp(bdev->data->fs_type, LUKS_NAME, strlen(LUKS_NAME))) {
+               _D("Format dbus request for extended internal storage is blocked.");
+               ret = -EPERM;
+               goto out;
+       }
+
+       ret = process_request_format_block(sender, invocation, bdev, option, NULL);
+       if (ret < 0) {
+               goto out;
+       }
+
        return NULL;
 
 out:
@@ -3258,13 +3270,10 @@ static GVariant *request_format_block_type(GDBusConnection *conn,
                GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
 {
        struct block_device *bdev;
-       struct format_data *fdata;
        char *type = NULL;
-       pid_t pid;
        int id;
        int option;
        int ret = -EBADMSG;
-       int prev_state;
 
        if (!block_control) {
                _D("Block module is disabled.");
@@ -3287,45 +3296,9 @@ static GVariant *request_format_block_type(GDBusConnection *conn,
                goto out;
        }
 
-       pid = gdbus_connection_get_sender_pid(NULL, sender);
-       if (bdev->on_private_op != REQ_NORMAL && pid != bdev->private_pid) {
-               _E("Failed to format on private state.");
-               ret = -EPERM;
-               goto out;
-       }
-
-       fdata = get_format_data(type, option);
-       if (!fdata) {
-               _E("Failed to get format data.");
-               goto out;
-       }
-
-       prev_state = bdev->data->state;
-       if (prev_state == BLOCK_MOUNT) {
-               if (bdev->on_private_op == REQ_PRIVATE) {
-                       bdev->on_private_op = REQ_PRIVATE_FORMAT;
-                       _D("Private operation state(%d).", bdev->on_private_op);
-               }
-               ret = add_operation(bdev, BLOCK_DEV_UNMOUNT, NULL, (void *)UNMOUNT_FORCE);
-               if (ret < 0) {
-                       _E("Failed to add operation(unmount, %s).", bdev->data->devnode);
-                       release_format_data(fdata);
-                       goto out;
-               }
-       }
-
-       ret = add_operation(bdev, BLOCK_DEV_FORMAT, invocation, (void *)fdata);
+       ret = process_request_format_block(sender, invocation, bdev, option, type);
        if (ret < 0) {
-               _E("Failed to add operation(format, %s).", bdev->data->devnode);
-               release_format_data(fdata);
-       }
-
-       /* Maintain previous state of mount/unmount */
-       if (prev_state == BLOCK_MOUNT) {
-               if (add_operation(bdev, BLOCK_DEV_MOUNT, NULL, NULL) < 0) {
-                       _E("Failed to add operation(mount, %s).", bdev->data->devnode);
-                       goto out;
-               }
+               goto out;
        }
 
        g_free(type);