From: Daan De Meyer Date: Thu, 19 Oct 2023 14:38:47 +0000 (+0200) Subject: mount: Add more helpers X-Git-Tag: v255-rc1~182^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8dbab37dec924021b9b3398c91e7e0ecf01c9709;p=thirdparty%2Fsystemd.git mount: Add more helpers --- diff --git a/src/core/dbus-mount.c b/src/core/dbus-mount.c index 89f7d4fa0f9..7dbbdd07f50 100644 --- a/src/core/dbus-mount.c +++ b/src/core/dbus-mount.c @@ -22,21 +22,13 @@ static int property_get_what( _cleanup_free_ char *escaped = NULL; Mount *m = ASSERT_PTR(userdata); - const char *s = NULL; assert(bus); assert(reply); - if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.what) - s = m->parameters_proc_self_mountinfo.what; - else if (m->from_fragment && m->parameters_fragment.what) - s = m->parameters_fragment.what; - - if (s) { - escaped = utf8_escape_invalid(s); - if (!escaped) - return -ENOMEM; - } + escaped = mount_get_what_escaped(m); + if (!escaped) + return -ENOMEM; return sd_bus_message_append_basic(reply, 's', escaped); } @@ -52,33 +44,17 @@ static int property_get_options( _cleanup_free_ char *escaped = NULL; Mount *m = ASSERT_PTR(userdata); - const char *s = NULL; assert(bus); assert(reply); - if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.options) - s = m->parameters_proc_self_mountinfo.options; - else if (m->from_fragment && m->parameters_fragment.options) - s = m->parameters_fragment.options; - - if (s) { - escaped = utf8_escape_invalid(s); - if (!escaped) - return -ENOMEM; - } + escaped = mount_get_options_escaped(m); + if (!escaped) + return -ENOMEM; return sd_bus_message_append_basic(reply, 's', escaped); } -static const char *mount_get_fstype(const Mount *m) { - if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.fstype) - return m->parameters_proc_self_mountinfo.fstype; - else if (m->from_fragment && m->parameters_fragment.fstype) - return m->parameters_fragment.fstype; - return NULL; -} - static BUS_DEFINE_PROPERTY_GET(property_get_type, "s", Mount, mount_get_fstype); static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, mount_result, MountResult); diff --git a/src/core/mount.c b/src/core/mount.c index 123c87ea1af..140286792a2 100644 --- a/src/core/mount.c +++ b/src/core/mount.c @@ -34,6 +34,7 @@ #include "strv.h" #include "unit-name.h" #include "unit.h" +#include "utf8.h" #define RETRY_UMOUNT_MAX 32 @@ -2345,6 +2346,58 @@ static int mount_subsystem_ratelimited(Manager *m) { return sd_event_source_is_ratelimited(m->mount_event_source); } +char* mount_get_what_escaped(const Mount *m) { + _cleanup_free_ char *escaped = NULL; + const char *s = NULL; + + assert(m); + + if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.what) + s = m->parameters_proc_self_mountinfo.what; + else if (m->from_fragment && m->parameters_fragment.what) + s = m->parameters_fragment.what; + + if (s) { + escaped = utf8_escape_invalid(s); + if (!escaped) + return NULL; + } + + return escaped ? TAKE_PTR(escaped) : strdup(""); +} + +char* mount_get_options_escaped(const Mount *m) { + _cleanup_free_ char *escaped = NULL; + const char *s = NULL; + + assert(m); + + if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.options) + s = m->parameters_proc_self_mountinfo.options; + else if (m->from_fragment && m->parameters_fragment.options) + s = m->parameters_fragment.options; + + if (s) { + escaped = utf8_escape_invalid(s); + if (!escaped) + return NULL; + } + + return escaped ? TAKE_PTR(escaped) : strdup(""); +} + +const char* mount_get_fstype(const Mount *m) { + assert(m); + + if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.fstype) + return m->parameters_proc_self_mountinfo.fstype; + + if (m->from_fragment && m->parameters_fragment.fstype) + return m->parameters_fragment.fstype; + + return NULL; +} + static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = { [MOUNT_EXEC_MOUNT] = "ExecMount", [MOUNT_EXEC_UNMOUNT] = "ExecUnmount", diff --git a/src/core/mount.h b/src/core/mount.h index 2c48d32b07d..6712c16811f 100644 --- a/src/core/mount.h +++ b/src/core/mount.h @@ -97,6 +97,10 @@ void mount_fd_event(Manager *m, int events); int mount_invalidate_state_by_path(Manager *manager, const char *path); +char* mount_get_what_escaped(const Mount *m); +char* mount_get_options_escaped(const Mount *m); +const char* mount_get_fstype(const Mount *m); + const char* mount_exec_command_to_string(MountExecCommand i) _const_; MountExecCommand mount_exec_command_from_string(const char *s) _pure_;