Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */ 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 40 : DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table*, mnt_free_table); 12 40 : DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter*, mnt_free_iter); 13 : 14 19 : 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 19 : _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL; 21 19 : _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL; 22 : int r; 23 : 24 : /* Older libmount seems to require this. */ 25 19 : assert(!source || path); 26 : 27 19 : table = mnt_new_table(); 28 19 : iter = mnt_new_iter(MNT_ITER_FORWARD); 29 19 : if (!table || !iter) 30 0 : 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 19 : if (source) 36 4 : r = mnt_table_parse_stream(table, source, path); 37 15 : else if (path) 38 3 : r = mnt_table_parse_file(table, path); 39 : else 40 12 : r = mnt_table_parse_mtab(table, NULL); 41 19 : if (r < 0) 42 0 : return r; 43 : 44 19 : *ret_table = TAKE_PTR(table); 45 19 : *ret_iter = TAKE_PTR(iter); 46 19 : return 0; 47 : }