1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <stdio.h>
5
6 /* This needs to be after sys/mount.h */
7 #include <libmount.h>
8
9 #include "macro.h"
10
11 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct libmnt_table*, mnt_free_table, NULL);
12 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct libmnt_iter*, mnt_free_iter, NULL);
13
libmount_parse(const char * path,FILE * source,struct libmnt_table ** ret_table,struct libmnt_iter ** ret_iter)14 static inline int libmount_parse(
15 const char *path,
16 FILE *source,
17 struct libmnt_table **ret_table,
18 struct libmnt_iter **ret_iter) {
19
20 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
21 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
22 int r;
23
24 /* Older libmount seems to require this. */
25 assert(!source || path);
26
27 table = mnt_new_table();
28 iter = mnt_new_iter(MNT_ITER_FORWARD);
29 if (!table || !iter)
30 return -ENOMEM;
31
32 /* If source or path are specified, we use on the functions which ignore utab.
33 * Only if both are empty, we use mnt_table_parse_mtab(). */
34
35 if (source)
36 r = mnt_table_parse_stream(table, source, path);
37 else if (path)
38 r = mnt_table_parse_file(table, path);
39 else
40 r = mnt_table_parse_mtab(table, NULL);
41 if (r < 0)
42 return r;
43
44 *ret_table = TAKE_PTR(table);
45 *ret_iter = TAKE_PTR(iter);
46 return 0;
47 }
48