From 9deeef8e47bf204298f346a54011c491b2480b0a Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Thu, 19 Jan 2012 13:28:41 +0900 Subject: libmount: Use filter arg of scandir* to pickup /etc/fstab.d/*.fstab files This patch is based on my last patch: [PATCH 1/2] scandirat based mnt_table_parse_dir implementation scandir* used in `mnt_table_parse_dir in libmount/src/tab_parse.c can take filter function as an argument. `mnt_table_parse_dir' picks up fstab files from namelist returned from scandir* for itself. However, some parts of picking-up job can be done in the filter function. This patch introduces a new function `mnt_table_parse_dir_filter' to share the code for picking-up job between two implementations of `mnt_table_parse_dir_filter', scandir based and scandirat based. Signed-off-by: Masatake YAMATO --- libmount/src/tab_parse.c | 63 +++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 36 deletions(-) (limited to 'libmount/src') diff --git a/libmount/src/tab_parse.c b/libmount/src/tab_parse.c index 1b0324ef4..bbdea553c 100644 --- a/libmount/src/tab_parse.c +++ b/libmount/src/tab_parse.c @@ -435,6 +435,30 @@ int mnt_table_parse_file(struct libmnt_table *tb, const char *filename) return rc; } +static int mnt_table_parse_dir_filter(const struct dirent *d) +{ + size_t namesz; + +#ifdef _DIRENT_HAVE_D_TYPE + if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG && + d->d_type != DT_LNK) + return 0; +#endif + if (*d->d_name == '.') + return 0; + +#define MNT_MNTTABDIR_EXTSIZ (sizeof(MNT_MNTTABDIR_EXT) - 1) + + namesz = strlen(d->d_name); + if (!namesz || namesz < MNT_MNTTABDIR_EXTSIZ + 1 || + strcmp(d->d_name + (namesz - MNT_MNTTABDIR_EXTSIZ), + MNT_MNTTABDIR_EXT)) + return 0; + + /* Accept this */ + return 1; +} + #ifdef HAVE_SCANDIRAT static int mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname) { @@ -445,7 +469,8 @@ static int mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname) dd = open(dirname, O_RDONLY|O_CLOEXEC|O_DIRECTORY); if (dd < 0) return -errno; - n = scandirat(dd, ".", &namelist, NULL, versionsort); + + n = scandirat(dd, ".", &namelist, mnt_table_parse_dir_filter, versionsort); if (n <= 0) { close(dd); return 0; @@ -454,25 +479,8 @@ static int mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname) for (i = 0; i < n; i++) { struct dirent *d = namelist[i]; struct stat st; - size_t namesz; FILE *f; -#ifdef _DIRENT_HAVE_D_TYPE - if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG && - d->d_type != DT_LNK) - continue; -#endif - if (*d->d_name == '.') - continue; - -#define MNT_MNTTABDIR_EXTSIZ (sizeof(MNT_MNTTABDIR_EXT) - 1) - - namesz = strlen(d->d_name); - if (!namesz || namesz < MNT_MNTTABDIR_EXTSIZ + 1 || - strcmp(d->d_name + (namesz - MNT_MNTTABDIR_EXTSIZ), - MNT_MNTTABDIR_EXT)) - continue; - if (fstat_at(dd, ".", d->d_name, &st, 0) || !S_ISREG(st.st_mode)) continue; @@ -497,7 +505,7 @@ static int mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname) DIR *dir = NULL; struct dirent **namelist = NULL; - n = scandir(dirname, &namelist, NULL, versionsort); + n = scandir(dirname, &namelist, mnt_table_parse_dir_filter, versionsort); if (n <= 0) return 0; @@ -509,25 +517,8 @@ static int mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname) for (i = 0; i < n; i++) { struct dirent *d = namelist[i]; struct stat st; - size_t namesz; FILE *f; -#ifdef _DIRENT_HAVE_D_TYPE - if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG && - d->d_type != DT_LNK) - continue; -#endif - if (*d->d_name == '.') - continue; - -#define MNT_MNTTABDIR_EXTSIZ (sizeof(MNT_MNTTABDIR_EXT) - 1) - - namesz = strlen(d->d_name); - if (!namesz || namesz < MNT_MNTTABDIR_EXTSIZ + 1 || - strcmp(d->d_name + (namesz - MNT_MNTTABDIR_EXTSIZ), - MNT_MNTTABDIR_EXT)) - continue; - if (fstat_at(dirfd(dir), _PATH_MNTTAB_DIR, d->d_name, &st, 0) || !S_ISREG(st.st_mode)) continue; -- cgit v1.2.3-55-g7522