]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: add generic function to read table for context
authorKarel Zak <kzak@redhat.com>
Wed, 22 Jun 2011 11:34:47 +0000 (13:34 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 22 Jun 2011 11:34:47 +0000 (13:34 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/context.c
libmount/src/libmount.h.in
libmount/src/libmount.sym

index f3eaae414810101f1b927fb6b2be437e2d9fbb08..681b0575ba9b0e64a2890b16881544cf0204aa4c 100644 (file)
@@ -738,8 +738,10 @@ int mnt_context_get_mtab(struct libmnt_context *cxt, struct libmnt_table **tb)
                cxt->mtab = mnt_new_table();
                if (!cxt->mtab)
                        return -ENOMEM;
+
                if (cxt->table_errcb)
-                       mnt_table_set_parser_errcb(cxt->fstab, cxt->table_errcb);
+                       mnt_table_set_parser_errcb(cxt->mtab, cxt->table_errcb);
+
                rc = mnt_table_parse_mtab(cxt->mtab, cxt->mtab_path);
                if (rc)
                        return rc;
@@ -753,6 +755,54 @@ int mnt_context_get_mtab(struct libmnt_context *cxt, struct libmnt_table **tb)
        return 0;
 }
 
+/**
+ * mnt_context_get_table:
+ * @cxt: mount context
+ * @file: filename (e.g. /proc/self/mountinfo, ...)
+ * @tb: returns the table
+ *
+ * This function allocates a new table and parses the @file. The parser error
+ * callback and cache for tags and paths is set according to the @cxt setting.
+ * See also mnt_table_parse_file().
+ *
+ * It's strongly recommended use mnt_context_get_mtab() and
+ * mnt_context_get_fstab() functions for mtab and fstab files. This function
+ * does not care about LIBMOUNT_* env.variables and does not merge userspace
+ * options.
+ *
+ * The result will NOT be deallocated by mnt_free_context(@cxt).
+ *
+ * Returns: 0 on success, negative number in case of error.
+ */
+int mnt_context_get_table(struct libmnt_context *cxt,
+                         const char *filename, struct libmnt_table **tb)
+{
+       struct libmnt_cache *cache;
+       int rc;
+
+       if (!cxt || !tb)
+               return -EINVAL;
+
+       *tb = mnt_new_table();
+       if (!*tb)
+               return -ENOMEM;
+
+       if (cxt->table_errcb)
+               mnt_table_set_parser_errcb(*tb, cxt->table_errcb);
+
+       rc = mnt_table_parse_file(*tb, filename);
+       if (rc) {
+               mnt_free_table(*tb);
+               return rc;
+       }
+
+       cache = mnt_context_get_cache(cxt);
+       if (cache)
+               mnt_table_set_cache(*tb, cache);
+
+       return 0;
+}
+
 /**
  * mnt_context_set_tables_errcb
  * @cxt: mount context
index b9d4db69db5bd718a9acc03d4a8ac1c69d4831c7..a224fb0f848abd6acd2a0204da95d460e0ba7329 100644 (file)
@@ -420,6 +420,9 @@ extern int mnt_context_get_fstab(struct libmnt_context *cxt,
                                 struct libmnt_table **tb);
 extern int mnt_context_get_mtab(struct libmnt_context *cxt,
                                struct libmnt_table **tb);
+extern int mnt_context_get_table(struct libmnt_context *cxt,
+                               const char *filename,
+                               struct libmnt_table **tb);
 extern int mnt_context_set_cache(struct libmnt_context *cxt,
                                 struct libmnt_cache *cache);
 extern struct libmnt_cache *mnt_context_get_cache(struct libmnt_context *cxt);
index 889309379b2ba13b13863087578e3e310f682e60..d8c01c0187cf32bba9d213ec8ac96c8ec56af312 100644 (file)
@@ -61,6 +61,7 @@ global:
        mnt_context_set_optsmode;
        mnt_context_set_source;
        mnt_context_set_syscall_status;
+       mnt_context_get_table;
        mnt_context_set_tables_errcb;
        mnt_context_set_target;
        mnt_context_set_user_mflags;