libmount Reference Manual | ||||
---|---|---|---|---|
Top | Description |
struct libmnt_table; void mnt_free_table (struct libmnt_table *tb
); struct libmnt_table * mnt_new_table (void
); int mnt_reset_table (struct libmnt_table *tb
); void mnt_ref_table (struct libmnt_table *tb
); void mnt_unref_table (struct libmnt_table *tb
); struct libmnt_table * mnt_new_table_from_dir (const char *dirname
); struct libmnt_table * mnt_new_table_from_file (const char *filename
); int mnt_table_add_fs (struct libmnt_table *tb
,struct libmnt_fs *fs
); int mnt_table_append_intro_comment (struct libmnt_table *tb
,const char *comm
); int mnt_table_append_trailing_comment (struct libmnt_table *tb
,const char *comm
); void mnt_table_enable_comments (struct libmnt_table *tb
,int enable
); struct libmnt_fs * mnt_table_find_devno (struct libmnt_table *tb
,dev_t devno
,int direction
); struct libmnt_fs * mnt_table_find_mountpoint (struct libmnt_table *tb
,const char *path
,int direction
); int mnt_table_find_next_fs (struct libmnt_table *tb
,struct libmnt_iter *itr
,int (*match_func) (struct libmnt_fs *, void *)
,void *userdata
,struct libmnt_fs **fs
); struct libmnt_fs * mnt_table_find_pair (struct libmnt_table *tb
,const char *source
,const char *target
,int direction
); struct libmnt_fs * mnt_table_find_source (struct libmnt_table *tb
,const char *source
,int direction
); struct libmnt_fs * mnt_table_find_srcpath (struct libmnt_table *tb
,const char *path
,int direction
); struct libmnt_fs * mnt_table_find_tag (struct libmnt_table *tb
,const char *tag
,const char *val
,int direction
); struct libmnt_fs * mnt_table_find_target (struct libmnt_table *tb
,const char *path
,int direction
); int mnt_table_first_fs (struct libmnt_table *tb
,struct libmnt_fs **fs
); struct libmnt_cache * mnt_table_get_cache (struct libmnt_table *tb
); const char * mnt_table_get_intro_comment (struct libmnt_table *tb
); int mnt_table_get_nents (struct libmnt_table *tb
); int mnt_table_get_root_fs (struct libmnt_table *tb
,struct libmnt_fs **root
); const char * mnt_table_get_trailing_comment (struct libmnt_table *tb
); void * mnt_table_get_userdata (struct libmnt_table *tb
); int mnt_table_is_empty (struct libmnt_table *tb
); int mnt_table_is_fs_mounted (struct libmnt_table *tb
,struct libmnt_fs *fstab_fs
); int mnt_table_last_fs (struct libmnt_table *tb
,struct libmnt_fs **fs
); int mnt_table_next_child_fs (struct libmnt_table *tb
,struct libmnt_iter *itr
,struct libmnt_fs *parent
,struct libmnt_fs **chld
); int mnt_table_next_fs (struct libmnt_table *tb
,struct libmnt_iter *itr
,struct libmnt_fs **fs
); int mnt_table_parse_dir (struct libmnt_table *tb
,const char *dirname
); int mnt_table_parse_file (struct libmnt_table *tb
,const char *filename
); int mnt_table_parse_fstab (struct libmnt_table *tb
,const char *filename
); int mnt_table_parse_mtab (struct libmnt_table *tb
,const char *filename
); int mnt_table_parse_stream (struct libmnt_table *tb
,FILE *f
,const char *filename
); int mnt_table_parse_swaps (struct libmnt_table *tb
,const char *filename
); int mnt_table_remove_fs (struct libmnt_table *tb
,struct libmnt_fs *fs
); int mnt_table_set_cache (struct libmnt_table *tb
,struct libmnt_cache *mpc
); int mnt_table_set_intro_comment (struct libmnt_table *tb
,const char *comm
); int mnt_table_set_iter (struct libmnt_table *tb
,struct libmnt_iter *itr
,struct libmnt_fs *fs
); int mnt_table_set_parser_errcb (struct libmnt_table *tb
,int (*cb) (struct libmnt_table *tb, const char *filename, int line)
); int mnt_table_set_trailing_comment (struct libmnt_table *tb
,const char *comm
); int mnt_table_set_userdata (struct libmnt_table *tb
,void *data
); int mnt_table_with_comments (struct libmnt_table *tb
);
Note that mnt_table_find_* functions are mount(8) compatible. These functions try to find an entry in more iterations, where the first attempt is always based on comparison with unmodified (non-canonicalized or un-evaluated) paths or tags. For example a fstab with two entries:
1 2 |
LABEL=foo /foo auto rw /dev/foo /foo auto rw |
where both lines are used for the *same* device, then
1 |
mnt_table_find_source(tb, "/dev/foo", &fs); |
will returns the second line, and
1 |
mnt_table_find_source(tb, "LABEL=foo", &fs); |
will returns the first entry, and
1 |
mnt_table_find_source(tb, "UUID=anyuuid", &fs); |
will return the first entry (if UUID matches with the device).
struct libmnt_table;
List of struct libmnt_fs entries (parsed fstab/mtab/mountinfo)
void mnt_free_table (struct libmnt_table *tb
);
Deallocates the table. This function does not care about reference count. Don't
use this function directly -- it's better to use use mnt_unref_table()
.
The table entries (filesystems) are unrefrenced by mnt_reset_table()
and
cache by mnt_unref_cache()
.
|
tab pointer |
struct libmnt_table * mnt_new_table (void
);
The tab is a container for struct libmnt_fs entries that usually represents a fstab, mtab or mountinfo file from your system.
See also mnt_table_parse_file()
.
Returns : |
newly allocated tab struct. |
int mnt_reset_table (struct libmnt_table *tb
);
Removes all entries (filesystems) from the table. The filesystems with zero reference count will be deallocated.
|
tab pointer |
Returns : |
0 on success or negative number in case of error. |
void mnt_ref_table (struct libmnt_table *tb
);
Increments reference counter.
|
table pointer |
void mnt_unref_table (struct libmnt_table *tb
);
De-increments reference counter, on zero the tb
is automatically
deallocated by mnt_free_table()
.
|
table pointer |
struct libmnt_table * mnt_new_table_from_dir (const char *dirname
);
|
directory with *.fstab files |
Returns : |
newly allocated tab on success and NULL in case of error. |
struct libmnt_table * mnt_new_table_from_file (const char *filename
);
Same as mnt_new_table()
+ mnt_table_parse_file()
. Use this function for private
files only. This function does not allow using the error callback, so you
cannot provide any feedback to end-users about broken records in files (e.g.
fstab).
|
/etc/{m,fs}tab or /proc/self/mountinfo path |
Returns : |
newly allocated tab on success and NULL in case of error. |
int mnt_table_add_fs (struct libmnt_table *tb
,struct libmnt_fs *fs
);
Adds a new entry to tab and increment fs
reference counter. Don't forget to
use mnt_unref_fs()
after mnt_table_add_fs()
you want to keep the fs
referenced by the table only.
|
tab pointer |
|
new entry |
Returns : |
0 on success or negative number in case of error. |
int mnt_table_append_intro_comment (struct libmnt_table *tb
,const char *comm
);
int mnt_table_append_trailing_comment (struct libmnt_table *tb
,const char *comm
);
Appends to the trailing table comment.
|
pointer to tab |
|
comment of NULL |
Returns : |
0 on success or negative number in case of error. |
void mnt_table_enable_comments (struct libmnt_table *tb
,int enable
);
Enables parsing of comments.
The initial (intro) file comment is accessible by
mnt_table_get_intro_comment()
. The intro and the comment of the first fstab
entry has to be separated by blank line. The filesystem comments are
accessible by mnt_fs_get_comment()
. The trailing fstab comment is accessible
by mnt_table_get_trailing_comment()
.
1 2 3 4 5 6 7 8 9 |
# # Intro comment # # this comments belongs to the first fs LABEL=foo /mnt/foo auto defaults 1 2 # this comments belongs to the second fs LABEL=bar /mnt/bar auto defaults 1 2 # tailing comment |
|
pointer to tab |
|
TRUE or FALSE |
struct libmnt_fs * mnt_table_find_devno (struct libmnt_table *tb
,dev_t devno
,int direction
);
Note that zero could be a valid device number for the root pseudo filesystem (e.g. tmpfs).
|
/proc/self/mountinfo |
|
device number |
|
MNT_ITER_{FORWARD,BACKWARD} |
Returns : |
a tab entry or NULL. |
struct libmnt_fs * mnt_table_find_mountpoint (struct libmnt_table *tb
,const char *path
,int direction
);
Same as mnt_get_mountpoint()
, except this function does not rely on
st_dev numbers.
|
tab pointer |
|
directory |
|
MNT_ITER_{FORWARD,BACKWARD} |
Returns : |
a tab entry or NULL. |
int mnt_table_find_next_fs (struct libmnt_table *tb
,struct libmnt_iter *itr
,int (*match_func) (struct libmnt_fs *, void *)
,void *userdata
,struct libmnt_fs **fs
);
This function allows searching in tb
.
|
table |
|
iterator |
|
function returning 1 or 0 |
|
extra data for match_func |
|
returns pointer to the next matching table entry |
Returns : |
negative number in case of error, 1 at end of table or 0 o success. |
struct libmnt_fs * mnt_table_find_pair (struct libmnt_table *tb
,const char *source
,const char *target
,int direction
);
This function is implemented by mnt_fs_match_source()
and
mnt_fs_match_target()
functions. It means that this is more expensive than
others mnt_table_find_* function, because every tab
entry is fully evaluated.
|
tab pointer |
|
TAG or path |
|
mountpoint |
|
MNT_ITER_{FORWARD,BACKWARD} |
Returns : |
a tab entry or NULL. |
struct libmnt_fs * mnt_table_find_source (struct libmnt_table *tb
,const char *source
,int direction
);
This is a high-level API for mnt_table_find_{srcpath,tag}. You needn't care
about the source
format (device, LABEL, UUID, ...). This function parses
the source
and calls mnt_table_find_tag()
or mnt_table_find_srcpath()
.
|
tab pointer |
|
TAG or path |
|
MNT_ITER_{FORWARD,BACKWARD} |
Returns : |
a tab entry or NULL. |
struct libmnt_fs * mnt_table_find_srcpath (struct libmnt_table *tb
,const char *path
,int direction
);
Try to lookup an entry in the given tab, four iterations are possible, the first
with path
, the second with realpath(path
), the third with tags (LABEL, UUID, ..)
from path
and the fourth with realpath(path
) against realpath(entry->srcpath).
The 2nd, 3rd and 4th iterations are not performed when the tb
cache is not
set (see mnt_table_set_cache()
).
Note that NULL is a valid source path; it will be replaced with "none". The "none" is used in /proc/{mounts,self/mountinfo} for pseudo filesystems.
|
tab pointer |
|
source path (devname or dirname) or NULL |
|
MNT_ITER_{FORWARD,BACKWARD} |
Returns : |
a tab entry or NULL. |
struct libmnt_fs * mnt_table_find_tag (struct libmnt_table *tb
,const char *tag
,const char *val
,int direction
);
Try to lookup an entry in the given tab, the first attempt is to lookup by tag
and
val
, for the second attempt the tag is evaluated (converted to the device
name) and mnt_table_find_srcpath()
is performed. The second attempt is not
performed when tb
cache is not set (see mnt_table_set_cache()
).
|
tab pointer |
|
tag name (e.g "LABEL", "UUID", ...) |
|
tag value |
|
MNT_ITER_{FORWARD,BACKWARD} |
Returns : |
a tab entry or NULL. |
struct libmnt_fs * mnt_table_find_target (struct libmnt_table *tb
,const char *path
,int direction
);
Try to lookup an entry in the given tab, three iterations are possible, the first
with path
, the second with realpath(path
) and the third with realpath(path
)
against realpath(fs->target). The 2nd and 3rd iterations are not performed
when the tb
cache is not set (see mnt_table_set_cache()
).
|
tab pointer |
|
mountpoint directory |
|
MNT_ITER_{FORWARD,BACKWARD} |
Returns : |
a tab entry or NULL. |
int mnt_table_first_fs (struct libmnt_table *tb
,struct libmnt_fs **fs
);
|
tab pointer |
|
returns the first tab entry |
Returns : |
0 on success, negative number in case of error or 1 at the end of list. |
struct libmnt_cache * mnt_table_get_cache (struct libmnt_table *tb
);
|
pointer to tab |
Returns : |
pointer to struct libmnt_cache instance or NULL. |
const char * mnt_table_get_intro_comment (struct libmnt_table *tb
);
|
pointer to tab |
Returns : |
initial comment in tb |
int mnt_table_get_nents (struct libmnt_table *tb
);
|
pointer to tab |
Returns : |
number of entries in table. |
int mnt_table_get_root_fs (struct libmnt_table *tb
,struct libmnt_fs **root
);
The function uses the parent ID from the mountinfo file to determine the root filesystem (the filesystem with the smallest ID). The function is designed mostly for applications where it is necessary to sort mountpoints by IDs to get the tree of the mountpoints (e.g. findmnt default output).
If you're not sure, then use
mnt_table_find_target(tb, "/", MNT_ITER_BACKWARD);
this is more robust and usable for arbitrary tab files (including fstab).
|
mountinfo file (/proc/self/mountinfo) |
|
returns pointer to the root filesystem (/) |
Returns : |
0 on success or negative number in case of error. |
const char * mnt_table_get_trailing_comment (struct libmnt_table *tb
);
|
pointer to tab |
Returns : |
table trailing comment |
void * mnt_table_get_userdata (struct libmnt_table *tb
);
|
pointer to tab |
Returns : |
pointer to user's data. |
int mnt_table_is_empty (struct libmnt_table *tb
);
|
pointer to tab |
Returns : |
1 if the table is without filesystems, or 0. |
int mnt_table_is_fs_mounted (struct libmnt_table *tb
,struct libmnt_fs *fstab_fs
);
int mnt_table_last_fs (struct libmnt_table *tb
,struct libmnt_fs **fs
);
|
tab pointer |
|
returns the last tab entry |
Returns : |
0 on success, negative number in case of error or 1 at the end of list. |
int mnt_table_next_child_fs (struct libmnt_table *tb
,struct libmnt_iter *itr
,struct libmnt_fs *parent
,struct libmnt_fs **chld
);
Note that filesystems are returned in the order of mounting (according to IDs in /proc/self/mountinfo).
|
mountinfo file (/proc/self/mountinfo) |
|
iterator |
|
parental FS |
|
returns the next child filesystem |
Returns : |
0 on success, negative number in case of error or 1 at the end of list. |
int mnt_table_next_fs (struct libmnt_table *tb
,struct libmnt_iter *itr
,struct libmnt_fs **fs
);
|
tab pointer | ||
|
iterator | ||
|
returns the next tab entry | ||
Returns : |
0 on success, negative number in case of error or 1 at the end of list.
Example:
|
int mnt_table_parse_dir (struct libmnt_table *tb
,const char *dirname
);
The directory:
files are sorted by strverscmp(3)
files that start with "." are ignored (e.g. ".10foo.fstab")
files without the ".fstab" extension are ignored
|
mount table |
|
directory |
Returns : |
0 on success or negative number in case of error. |
int mnt_table_parse_file (struct libmnt_table *tb
,const char *filename
);
Parses the whole table (e.g. /etc/mtab) and appends new records to the tab
.
The libmount parser ignores broken (syntax error) lines, these lines are
reported to the caller by the errcb()
function (see mnt_table_set_parser_errcb()
).
|
tab pointer |
|
file |
Returns : |
0 on success, negative number in case of error. |
int mnt_table_parse_fstab (struct libmnt_table *tb
,const char *filename
);
This function parses /etc/fstab and appends new lines to the tab
. If the
filename
is a directory, then mnt_table_parse_dir()
is called.
See also mnt_table_set_parser_errcb()
.
|
table |
|
overwrites default (/etc/fstab or $LIBMOUNT_FSTAB) or NULL |
Returns : |
0 on success or negative number in case of error. |
int mnt_table_parse_mtab (struct libmnt_table *tb
,const char *filename
);
This function parses /etc/mtab or /proc/self/mountinfo + /run/mount/utabs or /proc/mounts.
See also mnt_table_set_parser_errcb()
.
|
table |
|
overwrites default (/etc/mtab or $LIBMOUNT_MTAB) or NULL |
Returns : |
0 on success or negative number in case of error. |
int mnt_table_parse_stream (struct libmnt_table *tb
,FILE *f
,const char *filename
);
|
tab pointer |
|
file stream |
|
filename used for debug and error messages |
Returns : |
0 on success, negative number in case of error. |
int mnt_table_parse_swaps (struct libmnt_table *tb
,const char *filename
);
This function parses /proc/swaps and appends new lines to the tab
.
See also mnt_table_set_parser_errcb()
.
|
table |
|
overwrites default (/proc/swaps or $LIBMOUNT_SWAPS) or NULL |
Returns : |
0 on success or negative number in case of error. |
int mnt_table_remove_fs (struct libmnt_table *tb
,struct libmnt_fs *fs
);
Removes the fs
from the table and de-increment reference counter of the fs
. The
filesystem with zero reference counter will be deallocated. Don't forget to use
mnt_ref_fs()
before call mnt_table_remove_fs()
if you want to use fs
later.
|
tab pointer |
|
new entry |
Returns : |
0 on success or negative number in case of error. |
int mnt_table_set_cache (struct libmnt_table *tb
,struct libmnt_cache *mpc
);
Sets up a cache for canonicalized paths and evaluated tags (LABEL/UUID). The cache is recommended for mnt_table_find_*() functions.
The cache could be shared between more tabs. Be careful when you share the same cache between more threads -- currently the cache does not provide any locking method.
This function increments cache refrence counter. It's recomented to use
mnt_unref_cache()
after mnt_table_set_cache()
if you want to keep the cache
referenced by tb
only.
See also mnt_new_cache()
.
|
pointer to tab |
|
pointer to struct libmnt_cache instance |
Returns : |
0 on success or negative number in case of error. |
int mnt_table_set_intro_comment (struct libmnt_table *tb
,const char *comm
);
int mnt_table_set_iter (struct libmnt_table *tb
,struct libmnt_iter *itr
,struct libmnt_fs *fs
);
Sets iter
to the position of fs
in the file tb
.
|
tab pointer |
|
iterator |
|
tab entry |
Returns : |
0 on success, negative number in case of error. |
int mnt_table_set_parser_errcb (struct libmnt_table *tb
,int (*cb) (struct libmnt_table *tb, const char *filename, int line)
);
The error callback function is called by table parser (mnt_table_parse_file()
)
in case of a syntax error. The callback function could be used for error
evaluation, libmount will continue/stop parsing according to callback return
codes:
<0 : fatal error (abort parsing) 0 : success (parsing continues) >0 : recoverable error (the line is ignored, parsing continues).
|
pointer to table |
|
pointer to callback function |
Returns : |
0 on success or negative number in case of error. |
int mnt_table_set_trailing_comment (struct libmnt_table *tb
,const char *comm
);
Sets the trailing comment in table.
|
pointer to tab |
|
comment string |
Returns : |
0 on success or negative number in case of error. |
int mnt_table_set_userdata (struct libmnt_table *tb
,void *data
);
Sets pointer to the private user data.
|
pointer to tab |
|
pointer to user data |
Returns : |
0 on success or negative number in case of error. |