]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: Use filter arg of scandir* to pickup /etc/fstab.d/*.fstab files
authorMasatake YAMATO <yamato@redhat.com>
Thu, 19 Jan 2012 04:28:41 +0000 (13:28 +0900)
committerKarel Zak <kzak@redhat.com>
Mon, 23 Jan 2012 12:53:40 +0000 (13:53 +0100)
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 <yamato@redhat.com>
libmount/src/tab_parse.c

index 1b0324ef4a0270c0a23a1236092567b234665b28..bbdea553c7829fca138abf6dd4513f25bc036dd7 100644 (file)
@@ -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;