]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/dbus-util: several cleanups for bus_read_mount_options() 40395/head
authorMike Yuan <me@yhndnzj.com>
Tue, 6 Jan 2026 21:02:17 +0000 (22:02 +0100)
committerMike Yuan <me@yhndnzj.com>
Mon, 19 Jan 2026 16:49:29 +0000 (17:49 +0100)
* Make sure ret_options is initialized on success.
* Return empty mount options as-is rather than NULL-ing it -
  dbus property parser for RootImageOptions relies on it
  for resetting options for a specific partition designator.
* Format partition:options properly with strextendf, currently
  multiple ":" will be emitted.
* Allow separator to be unset if in_out_format_str is not
  needed.

src/core/dbus-service.c
src/core/dbus-util.c

index 74a294e4945f98bcaf3408e07905809a7a25f696..f95b5259ff906e5b489f5a87ac8700b90c5bb732 100644 (file)
@@ -194,7 +194,8 @@ static int bus_service_method_mount(sd_bus_message *message, void *userdata, sd_
                 return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Destination path must be absolute and normalized");
 
         if (is_image) {
-                r = bus_read_mount_options(message, reterr_error, &options, NULL, "");
+                r = bus_read_mount_options(message, reterr_error, &options,
+                                           /* in_out_format_str = */ NULL, /* separator = */ NULL);
                 if (r < 0)
                         return r;
         }
index f8d14d4209dcac3738b068eb47322db585da8f6b..3ba469cf0b5f8ac41ce144ee629897e3aea422f7 100644 (file)
@@ -267,49 +267,50 @@ int bus_read_mount_options(
 
         _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
         _cleanup_free_ char *format_str = NULL;
-        const char *mount_options, *partition;
         int r;
 
         assert(message);
         assert(ret_options);
-        assert(separator);
+        assert(!in_out_format_str == !separator);
 
         r = sd_bus_message_enter_container(message, 'a', "(ss)");
         if (r < 0)
                 return r;
 
+        const char *partition, *mount_options;
         while ((r = sd_bus_message_read(message, "(ss)", &partition, &mount_options)) > 0) {
-                _cleanup_free_ char *escaped = NULL;
                 PartitionDesignator partition_designator;
 
                 if (chars_intersect(mount_options, WHITESPACE))
                         return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS,
-                                                "Invalid mount options string, contains whitespace character(s): %s", mount_options);
+                                                 "Invalid mount options string, contains whitespace character(s): %s", mount_options);
 
                 partition_designator = partition_designator_from_string(partition);
                 if (partition_designator < 0)
                         return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid partition name %s", partition);
 
-                /* Need to store the options with the escapes, so that they can be parsed again */
-                escaped = shell_escape(mount_options, ":");
-                if (!escaped)
-                        return -ENOMEM;
-
-                if (!isempty(escaped)) {
-                        if (!strextend_with_separator(&format_str, separator, partition, ":", escaped))
+                if (!options) {
+                        options = new0(MountOptions, 1);
+                        if (!options)
                                 return -ENOMEM;
+                }
+
+                r = free_and_strdup(&options->options[partition_designator], mount_options);
+                if (r < 0)
+                        return r;
 
-                        if (!options) {
-                                options = new0(MountOptions, 1);
-                                if (!options)
-                                        return -ENOMEM;
-                        }
+                if (in_out_format_str && !isempty(mount_options)) {
+                        /* Need to store the options with the escapes, so that they can be parsed again */
+                        _cleanup_free_ char *escaped = NULL;
 
-                        r = free_and_strdup(&options->options[partition_designator], mount_options);
+                        escaped = shell_escape(mount_options, ":");
+                        if (!escaped)
+                                return -ENOMEM;
+
+                        r = strextendf_with_separator(&format_str, separator, "%s:%s", partition, escaped);
                         if (r < 0)
                                 return r;
-                } else if (options)
-                        options->options[partition_designator] = mfree(options->options[partition_designator]);
+                }
         }
         if (r < 0)
                 return r;
@@ -318,11 +319,10 @@ int bus_read_mount_options(
         if (r < 0)
                 return r;
 
-        if (options) {
-                if (in_out_format_str && !strextend_with_separator(in_out_format_str, separator, format_str))
-                        return -ENOMEM;
-                *ret_options = TAKE_PTR(options);
-        }
+        if (in_out_format_str && !strextend_with_separator(in_out_format_str, separator, format_str))
+                return -ENOMEM;
+
+        *ret_options = TAKE_PTR(options);
 
         return 0;
 }