X Tutup
Skip to content

Commit 988172c

Browse files
committed
core: split read_mount_options helper out for reuse
1 parent c5a7055 commit 988172c

File tree

3 files changed

+81
-70
lines changed

3 files changed

+81
-70
lines changed

src/core/dbus-execute.c

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,74 +1497,6 @@ static BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(capability, "t", uint64_t, uint6
14971497
static BUS_DEFINE_SET_TRANSIENT_TO_STRING_ALLOC(namespace_flag, "t", uint64_t, unsigned long, "%" PRIu64, namespace_flags_to_string);
14981498
static BUS_DEFINE_SET_TRANSIENT_TO_STRING(mount_flags, "t", uint64_t, unsigned long, "%" PRIu64, mount_propagation_flags_to_string_with_check);
14991499

1500-
/* ret_format_str is an accumulator, so if it has any pre-existing content, new options will be appended to it */
1501-
static int read_mount_options(sd_bus_message *message, sd_bus_error *error, MountOptions **ret_options, char **ret_format_str, const char *separator) {
1502-
_cleanup_(mount_options_free_allp) MountOptions *options = NULL;
1503-
_cleanup_free_ char *format_str = NULL;
1504-
const char *mount_options, *partition;
1505-
int r;
1506-
1507-
assert(message);
1508-
assert(ret_options);
1509-
assert(ret_format_str);
1510-
assert(separator);
1511-
1512-
r = sd_bus_message_enter_container(message, 'a', "(ss)");
1513-
if (r < 0)
1514-
return r;
1515-
1516-
while ((r = sd_bus_message_read(message, "(ss)", &partition, &mount_options)) > 0) {
1517-
_cleanup_free_ char *previous = NULL, *escaped = NULL;
1518-
_cleanup_free_ MountOptions *o = NULL;
1519-
PartitionDesignator partition_designator;
1520-
1521-
if (chars_intersect(mount_options, WHITESPACE))
1522-
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1523-
"Invalid mount options string, contains whitespace character(s): %s", mount_options);
1524-
1525-
partition_designator = partition_designator_from_string(partition);
1526-
if (partition_designator < 0)
1527-
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid partition name %s", partition);
1528-
1529-
/* Need to store them in the unit with the escapes, so that they can be parsed again */
1530-
escaped = shell_escape(mount_options, ":");
1531-
if (!escaped)
1532-
return -ENOMEM;
1533-
1534-
previous = TAKE_PTR(format_str);
1535-
format_str = strjoin(previous, previous ? separator : "", partition, ":", escaped);
1536-
if (!format_str)
1537-
return -ENOMEM;
1538-
1539-
o = new(MountOptions, 1);
1540-
if (!o)
1541-
return -ENOMEM;
1542-
*o = (MountOptions) {
1543-
.partition_designator = partition_designator,
1544-
.options = strdup(mount_options),
1545-
};
1546-
if (!o->options)
1547-
return -ENOMEM;
1548-
LIST_APPEND(mount_options, options, TAKE_PTR(o));
1549-
}
1550-
if (r < 0)
1551-
return r;
1552-
1553-
r = sd_bus_message_exit_container(message);
1554-
if (r < 0)
1555-
return r;
1556-
1557-
if (!LIST_IS_EMPTY(options)) {
1558-
char *final = strjoin(*ret_format_str, !isempty(*ret_format_str) ? separator : "", format_str);
1559-
if (!final)
1560-
return -ENOMEM;
1561-
free_and_replace(*ret_format_str, final);
1562-
LIST_JOIN(mount_options, *ret_options, options);
1563-
}
1564-
1565-
return 0;
1566-
}
1567-
15681500
int bus_exec_context_set_transient_property(
15691501
Unit *u,
15701502
ExecContext *c,
@@ -1599,7 +1531,7 @@ int bus_exec_context_set_transient_property(
15991531
_cleanup_(mount_options_free_allp) MountOptions *options = NULL;
16001532
_cleanup_free_ char *format_str = NULL;
16011533

1602-
r = read_mount_options(message, error, &options, &format_str, " ");
1534+
r = bus_read_mount_options(message, error, &options, &format_str, " ");
16031535
if (r < 0)
16041536
return r;
16051537

@@ -3407,7 +3339,7 @@ int bus_exec_context_set_transient_property(
34073339
return -ENOMEM;
34083340
free_and_replace(format_str, tuple);
34093341

3410-
r = read_mount_options(message, error, &options, &format_str, ":");
3342+
r = bus_read_mount_options(message, error, &options, &format_str, ":");
34113343
if (r < 0)
34123344
return r;
34133345

src/core/dbus-util.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "bus-polkit.h"
44
#include "bus-util.h"
55
#include "dbus-util.h"
6+
#include "escape.h"
67
#include "parse-util.h"
78
#include "path-util.h"
89
#include "unit-printf.h"
@@ -186,3 +187,78 @@ int bus_verify_manage_units_async_full(
186187
&u->manager->polkit_registry,
187188
error);
188189
}
190+
191+
/* ret_format_str is an accumulator, so if it has any pre-existing content, new options will be appended to it */
192+
int bus_read_mount_options(
193+
sd_bus_message *message,
194+
sd_bus_error *error,
195+
MountOptions **ret_options,
196+
char **ret_format_str,
197+
const char *separator) {
198+
199+
_cleanup_(mount_options_free_allp) MountOptions *options = NULL;
200+
_cleanup_free_ char *format_str = NULL;
201+
const char *mount_options, *partition;
202+
int r;
203+
204+
assert(message);
205+
assert(ret_options);
206+
assert(separator);
207+
208+
r = sd_bus_message_enter_container(message, 'a', "(ss)");
209+
if (r < 0)
210+
return r;
211+
212+
while ((r = sd_bus_message_read(message, "(ss)", &partition, &mount_options)) > 0) {
213+
_cleanup_free_ char *previous = NULL, *escaped = NULL;
214+
_cleanup_free_ MountOptions *o = NULL;
215+
PartitionDesignator partition_designator;
216+
217+
if (chars_intersect(mount_options, WHITESPACE))
218+
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
219+
"Invalid mount options string, contains whitespace character(s): %s", mount_options);
220+
221+
partition_designator = partition_designator_from_string(partition);
222+
if (partition_designator < 0)
223+
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid partition name %s", partition);
224+
225+
/* Need to store the options with the escapes, so that they can be parsed again */
226+
escaped = shell_escape(mount_options, ":");
227+
if (!escaped)
228+
return -ENOMEM;
229+
230+
previous = TAKE_PTR(format_str);
231+
format_str = strjoin(previous, previous ? separator : "", partition, ":", escaped);
232+
if (!format_str)
233+
return -ENOMEM;
234+
235+
o = new(MountOptions, 1);
236+
if (!o)
237+
return -ENOMEM;
238+
*o = (MountOptions) {
239+
.partition_designator = partition_designator,
240+
.options = strdup(mount_options),
241+
};
242+
if (!o->options)
243+
return -ENOMEM;
244+
LIST_APPEND(mount_options, options, TAKE_PTR(o));
245+
}
246+
if (r < 0)
247+
return r;
248+
249+
r = sd_bus_message_exit_container(message);
250+
if (r < 0)
251+
return r;
252+
253+
if (!LIST_IS_EMPTY(options)) {
254+
if (ret_format_str) {
255+
char *final = strjoin(*ret_format_str, !isempty(*ret_format_str) ? separator : "", format_str);
256+
if (!final)
257+
return -ENOMEM;
258+
free_and_replace(*ret_format_str, final);
259+
}
260+
LIST_JOIN(mount_options, *ret_options, options);
261+
}
262+
263+
return 0;
264+
}

src/core/dbus-util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "sd-bus.h"
55

6+
#include "dissect-image.h"
67
#include "unit.h"
78

89
int bus_property_get_triggered_unit(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error);
@@ -249,3 +250,5 @@ static inline int bus_set_transient_usec_fix_0(Unit *u, const char *name, usec_t
249250
return bus_set_transient_usec_internal(u, name, p, true, message, flags, error);
250251
}
251252
int bus_verify_manage_units_async_full(Unit *u, const char *verb, int capability, const char *polkit_message, bool interactive, sd_bus_message *call, sd_bus_error *error);
253+
254+
int bus_read_mount_options(sd_bus_message *message, sd_bus_error *error, MountOptions **ret_options, char **ret_format_str, const char *separator);

0 commit comments

Comments
 (0)
X Tutup