assert(m);
- table = mnt_new_table();
- iter = mnt_new_iter(MNT_ITER_FORWARD);
- if (!table || !iter)
- return log_oom();
-
- r = mnt_table_parse_mtab(table, NULL);
+ r = libmount_parse(NULL, NULL, &table, &iter);
if (r < 0)
return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
/* Returns all mount points obtained from /proc/self/mountinfo in *list,
* and the number of mount points as return value. */
- table = mnt_new_table();
- iter = mnt_new_iter(MNT_ITER_FORWARD);
- if (!table || !iter)
- return log_oom();
-
- r = mnt_table_parse_mtab(table, NULL);
+ r = libmount_parse(NULL, NULL, &table, &iter);
if (r < 0)
return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once
+#include <stdio.h>
+
/* This needs to be after sys/mount.h */
#include <libmount.h>
DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table*, mnt_free_table);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter*, mnt_free_iter);
+
+static inline int libmount_parse(
+ const char *path,
+ FILE *source,
+ struct libmnt_table **ret_table,
+ struct libmnt_iter **ret_iter) {
+
+ _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
+ _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
+ int r;
+
+ /* Older libmount seems to require this. */
+ assert(!source || path);
+
+ table = mnt_new_table();
+ iter = mnt_new_iter(MNT_ITER_FORWARD);
+ if (!table || !iter)
+ return -ENOMEM;
+
+ if (source)
+ r = mnt_table_parse_stream(table, source, path);
+ else
+ r = mnt_table_parse_mtab(table, path);
+ if (r < 0)
+ return r;
+
+ *ret_table = TAKE_PTR(table);
+ *ret_iter = TAKE_PTR(iter);
+ return 0;
+}
again = false;
- table = mnt_new_table();
- iter = mnt_new_iter(MNT_ITER_FORWARD);
- if (!table || !iter)
- return -ENOMEM;
-
- r = mnt_table_parse_mtab(table, NULL);
+ r = libmount_parse(NULL, NULL, &table, &iter);
if (r < 0)
return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
if (!todo)
return -ENOMEM;
- table = mnt_new_table();
- iter = mnt_new_iter(MNT_ITER_FORWARD);
- if (!table || !iter)
- return -ENOMEM;
-
rewind(proc_self_mountinfo);
- r = mnt_table_parse_stream(table, proc_self_mountinfo, "/proc/self/mountinfo");
+ r = libmount_parse("/proc/self/mountinfo", proc_self_mountinfo, &table, &iter);
if (r < 0)
return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
assert(head);
- table = mnt_new_table();
- iter = mnt_new_iter(MNT_ITER_FORWARD);
- if (!table || !iter)
- return log_oom();
-
- r = mnt_table_parse_mtab(table, mountinfo);
+ r = libmount_parse(mountinfo, NULL, &table, &iter);
if (r < 0)
return log_error_errno(r, "Failed to parse %s: %m", mountinfo);
_cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
_cleanup_fclose_ FILE *f = NULL;
- assert_se(table = mnt_new_table());
- assert_se(iter = mnt_new_iter(MNT_ITER_FORWARD));
-
f = fmemopen((char*) string, strlen(string), "re");
assert_se(f);
- assert_se(mnt_table_parse_stream(table, f, title) >= 0);
+ assert_se(libmount_parse(title, f, &table, &iter) >= 0);
struct libmnt_fs *fs;
const char *source, *target;