]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: optimize tab files parsing
authorKarel Zak <kzak@redhat.com>
Tue, 26 Feb 2013 13:16:02 +0000 (14:16 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 26 Feb 2013 13:16:53 +0000 (14:16 +0100)
 - ignore empty files
 - ignore empty tables

Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/mountP.h
libmount/src/tab_parse.c
libmount/src/utils.c

index 8eb677cdce8d71d6f8ed3e9a5417f11c9aafd2c0..1b4ba687bd30c8f87e7fcaca3e018651036d5a4d 100644 (file)
@@ -132,6 +132,8 @@ extern int endswith(const char *s, const char *sx)
 extern int startswith(const char *s, const char *sx)
                        __attribute__((nonnull));
 
+extern int is_file_empty(const char *name);
+
 extern int mkdir_p(const char *path, mode_t mode);
 
 extern int mnt_is_readonly(const char *path)
index a8b7b79d4f43514a0e90a558704862de69a91cfe..b4b647075e0a2e378fca516f23568396507a1cea 100644 (file)
@@ -957,6 +957,7 @@ int mnt_table_parse_mtab(struct libmnt_table *tb, const char *filename)
 {
        int rc;
        const char *utab = NULL;
+       struct libmnt_table *u_tb;
 
        if (mnt_has_regular_mtab(&filename, NULL)) {
 
@@ -980,34 +981,33 @@ int mnt_table_parse_mtab(struct libmnt_table *tb, const char *filename)
                return mnt_table_parse_file(tb, _PATH_PROC_MOUNTS);
        }
 
+       if (mnt_table_get_nents(tb) == 0)
+               return 0;                       /* empty, ignore utab */
        /*
         * try to read user specific information from /run/mount/utabs
         */
        utab = mnt_get_utab_path();
-       if (utab) {
-               struct libmnt_table *u_tb = mnt_new_table();
-               if (u_tb) {
-                       u_tb->fmt = MNT_FMT_UTAB;
-                       mnt_table_set_parser_fltrcb(u_tb, tb->fltrcb, tb->fltrcb_data);
-
-                       if (mnt_table_parse_file(u_tb, utab) != 0) {
-                               mnt_free_table(u_tb);
-                               u_tb = NULL;
-                       }
-               }
+       if (!utab || is_file_empty(utab))
+               return 0;
 
-               if (u_tb) {
-                       struct libmnt_fs *u_fs;
-                       struct libmnt_iter itr;
+       u_tb = mnt_new_table();
+       if (!u_tb)
+               return -ENOMEM;
 
-                       mnt_reset_iter(&itr, MNT_ITER_BACKWARD);
+       u_tb->fmt = MNT_FMT_UTAB;
+       mnt_table_set_parser_fltrcb(u_tb, tb->fltrcb, tb->fltrcb_data);
 
-                       /*  merge user options into mountinfo from kernel */
-                       while(mnt_table_next_fs(u_tb, &itr, &u_fs) == 0)
-                               mnt_table_merge_user_fs(tb, u_fs);
+       if (mnt_table_parse_file(u_tb, utab) == 0) {
+               struct libmnt_fs *u_fs;
+               struct libmnt_iter itr;
 
-                       mnt_free_table(u_tb);
-               }
+               mnt_reset_iter(&itr, MNT_ITER_BACKWARD);
+
+               /*  merge user options into mountinfo from kernel */
+               while(mnt_table_next_fs(u_tb, &itr, &u_fs) == 0)
+                       mnt_table_merge_user_fs(tb, u_fs);
        }
+
+       mnt_free_table(u_tb);
        return 0;
 }
index 1c37062e62e6abf26d5acb5d2389b2ee98824b39..1d4fd0e4b3d36ce79d350d8ef8fa71f68eda93f4 100644 (file)
@@ -54,6 +54,17 @@ int startswith(const char *s, const char *sx)
         return !strncmp(s, sx, off);
 }
 
+/*
+ * Return 1 if the file does not accessible of empty
+ */
+int is_file_empty(const char *name)
+{
+       struct stat st;
+       assert(name);
+
+       return (stat(name, &st) != 0 || st.st_size == 0);
+}
+
 int mnt_parse_offset(const char *str, size_t len, uintmax_t *res)
 {
        char *p;