From 52f836284118d7ed41c3bd6c30b9b6907c5334b4 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 23 Jan 2012 20:20:36 +0100 Subject: libmount: support dirs for mnt_table_parse_fstab(), add mnt_table_parse_dir() Signed-off-by: Karel Zak --- libmount/src/libmount.h.in | 2 ++ libmount/src/libmount.sym | 1 + libmount/src/tab_parse.c | 63 +++++++++++++++++++++++++++------------------- 3 files changed, 40 insertions(+), 26 deletions(-) (limited to 'libmount') diff --git a/libmount/src/libmount.h.in b/libmount/src/libmount.h.in index 3e5a45f83..3cc1a79e7 100644 --- a/libmount/src/libmount.h.in +++ b/libmount/src/libmount.h.in @@ -277,6 +277,8 @@ extern struct libmnt_table *mnt_new_table_from_dir(const char *dirname); extern int mnt_table_parse_stream(struct libmnt_table *tb, FILE *f, const char *filename); extern int mnt_table_parse_file(struct libmnt_table *tb, const char *filename); +extern int mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname); + extern int mnt_table_parse_fstab(struct libmnt_table *tb, const char *filename); extern int mnt_table_parse_mtab(struct libmnt_table *tb, const char *filename); extern int mnt_table_set_parser_errcb(struct libmnt_table *tb, diff --git a/libmount/src/libmount.sym b/libmount/src/libmount.sym index 42b2e3092..d4e005692 100644 --- a/libmount/src/libmount.sym +++ b/libmount/src/libmount.sym @@ -224,4 +224,5 @@ global: mnt_fs_is_pseudofs; mnt_fs_is_swaparea; mnt_get_library_features; + mnt_table_parse_dir; } MOUNT_2.20; diff --git a/libmount/src/tab_parse.c b/libmount/src/tab_parse.c index 369325db1..aba4d68fa 100644 --- a/libmount/src/tab_parse.c +++ b/libmount/src/tab_parse.c @@ -460,7 +460,7 @@ static int mnt_table_parse_dir_filter(const struct dirent *d) } #ifdef HAVE_SCANDIRAT -static int mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname) +static int __mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname) { int n = 0, i; int dd; @@ -499,7 +499,7 @@ static int mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname) return 0; } #else -static int mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname) +static int __mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname) { int n = 0, i, r = 0; DIR *dir = NULL; @@ -543,6 +543,23 @@ out: } #endif +/** + * mnt_table_parse_dir: + * @tb: mount table + * @dirname: directory + * + * The directory: + * - files are sorted by strverscmp(3) + * - files that starts with "." are ignored (e.g. ".10foo.fstab") + * - files without the ".fstab" extension are ignored + * + * Returns: 0 on success or negative number in case of error. + */ +int mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname) +{ + return __mnt_table_parse_dir(tb, dirname); +} + struct libmnt_table *__mnt_new_table_from_file(const char *filename, int fmt) { struct libmnt_table *tb; @@ -583,7 +600,7 @@ struct libmnt_table *mnt_new_table_from_file(const char *filename) /** * mnt_new_table_from_dir - * @dirname: for example /etc/fstab.d + * @dirname: directory with *.fstab files * * Returns: newly allocated tab on success and NULL in case of error. */ @@ -632,14 +649,8 @@ int mnt_table_set_parser_errcb(struct libmnt_table *tb, * @tb: table * @filename: overwrites default (/etc/fstab or $LIBMOUNT_FSTAB) or NULL * - * This function parses /etc/fstab or /etc/fstab.d and appends new lines to the - * @tab. If the system contains classic fstab file and also fstab.d directory - * then the fstab file is parsed before the fstab.d directory. - * - * The fstab.d directory: - * - files are sorted by strverscmp(3) - * - files that starts with "." are ignored (e.g. ".10foo.fstab") - * - files without the ".fstab" extension are ignored + * 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(). * @@ -647,7 +658,8 @@ int mnt_table_set_parser_errcb(struct libmnt_table *tb, */ int mnt_table_parse_fstab(struct libmnt_table *tb, const char *filename) { - FILE *f; + struct stat st; + int rc = 0; assert(tb); @@ -656,24 +668,23 @@ int mnt_table_parse_fstab(struct libmnt_table *tb, const char *filename) if (!filename) filename = mnt_get_fstab_path(); - tb->fmt = MNT_FMT_FSTAB; + if (!filename || stat(filename, &st)) + return -EINVAL; - f = fopen(filename, "r"); - if (f) { - int rc = mnt_table_parse_stream(tb, f, filename); - fclose(f); + tb->fmt = MNT_FMT_FSTAB; - if (rc) - return rc; + if (S_ISREG(st.st_mode)) + rc = mnt_table_parse_file(tb, filename); + else if (S_ISDIR(st.st_mode)) + rc = mnt_table_parse_dir(tb, filename); + else + rc = -EINVAL; - if (strcmp(filename, _PATH_MNTTAB)) - /* /etc/fstab.d sould be used together with /etc/fstab only */ - return 0; + if (rc == 0 && strcmp(filename, _PATH_MNTTAB) == 0) { + if (!access(_PATH_MNTTAB_DIR, R_OK)) + rc = mnt_table_parse_dir(tb, _PATH_MNTTAB_DIR); } - - if (!access(_PATH_MNTTAB_DIR, R_OK)) - return mnt_table_parse_dir(tb, _PATH_MNTTAB_DIR); - return 0; + return rc; } /* -- cgit v1.2.3-55-g7522