]>
Commit | Line | Data |
---|---|---|
fb36b133 ZJS |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | #pragma once | |
3 | ||
e2857b3d ZJS |
4 | #include <stdio.h> |
5 | ||
fb36b133 ZJS |
6 | /* This needs to be after sys/mount.h */ |
7 | #include <libmount.h> | |
8 | ||
9 | #include "macro.h" | |
10 | ||
11 | DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table*, mnt_free_table); | |
12 | DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter*, mnt_free_iter); | |
e2857b3d ZJS |
13 | |
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 | ||
2f2d81d9 ZJS |
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 | ||
e2857b3d ZJS |
35 | if (source) |
36 | r = mnt_table_parse_stream(table, source, path); | |
2f2d81d9 ZJS |
37 | else if (path) |
38 | r = mnt_table_parse_file(table, path); | |
e2857b3d | 39 | else |
2f2d81d9 | 40 | r = mnt_table_parse_mtab(table, NULL); |
e2857b3d ZJS |
41 | if (r < 0) |
42 | return r; | |
43 | ||
44 | *ret_table = TAKE_PTR(table); | |
45 | *ret_iter = TAKE_PTR(iter); | |
46 | return 0; | |
47 | } |