]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mount: Add more helpers
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 19 Oct 2023 14:38:47 +0000 (16:38 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 20 Oct 2023 12:09:30 +0000 (14:09 +0200)
src/core/dbus-mount.c
src/core/mount.c
src/core/mount.h

index 89f7d4fa0f9699c128456693103086f29f0fc320..7dbbdd07f50870609fea6dd2dfb9c129fcd67149 100644 (file)
@@ -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);
 
index 123c87ea1af6b15a00b98809dd5d56d5a14b9f84..140286792a25a07f1db20dc37c903ea730b72dbc 100644 (file)
@@ -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",
index 2c48d32b07dea8a90d8e74fcbde9e872b6cc9dec..6712c16811f226d7ca030fc6cb7ba489ff8e2558 100644 (file)
@@ -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_;