From 45d6713e8781e3102fa49c4284637dc5fcf5f656 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 22 Jun 2010 11:52:36 +0200 Subject: [PATCH] libmount: add mnt_tab_parse_stream() Signed-off-by: Karel Zak --- shlibs/mount/src/mount.h.in | 1 + shlibs/mount/src/mount.sym | 1 + shlibs/mount/src/tab_parse.c | 84 +++++++++++++++++++++--------------- 3 files changed, 52 insertions(+), 34 deletions(-) diff --git a/shlibs/mount/src/mount.h.in b/shlibs/mount/src/mount.h.in index 8f1ecaad..ac1aef40 100644 --- a/shlibs/mount/src/mount.h.in +++ b/shlibs/mount/src/mount.h.in @@ -265,6 +265,7 @@ extern int mnt_fs_print_debug(mnt_fs *ent, FILE *file); /* tab-parse.c */ extern mnt_tab *mnt_new_tab_from_file(const char *filename); +extern int mnt_tab_parse_stream(mnt_tab *tb, FILE *f, const char *filename); extern int mnt_tab_parse_file(mnt_tab *tb, const char *filename); extern int mnt_tab_set_parser_errcb(mnt_tab *tb, int (*cb)(mnt_tab *tb, const char *filename, int line, int flag)); diff --git a/shlibs/mount/src/mount.sym b/shlibs/mount/src/mount.sym index 7cd79498..8f930967 100644 --- a/shlibs/mount/src/mount.sym +++ b/shlibs/mount/src/mount.sym @@ -121,6 +121,7 @@ global: mnt_tab_next_child_fs; mnt_tab_next_fs; mnt_tab_parse_file; + mnt_tab_parse_stream; mnt_tab_remove_fs; mnt_tab_set_cache; mnt_tab_set_parser_errcb; diff --git a/shlibs/mount/src/tab_parse.c b/shlibs/mount/src/tab_parse.c index 69443ca7..d82be5ab 100644 --- a/shlibs/mount/src/tab_parse.c +++ b/shlibs/mount/src/tab_parse.c @@ -364,44 +364,21 @@ err: } /** - * mnt_tab_parse_file: + * mnt_tab_parse_stream: * @tb: tab pointer - * @filename: file - * - * Parses whole table (e.g. /etc/fstab). - * - * - * - * mnt_tab *tb = mnt_new_tab(); - * int rc; - * - * rc = mnt_tab_parse_file(tb, "/etc/fstab"); - * if (!rc) - * mnt_fprintf_tab(tb, stdout, NULL); - * mnt_free_tab(tb); - * - * - * - * The libmount parser ignores broken (with syntax error) lines, these lines are - * reported to caller by errcb() function (see mnt_tab_set_parser_errcb()). + * @f: file stream + * @filename: filename used for debug and error messages * * Returns: 0 on success, -1 in case of error. */ -int mnt_tab_parse_file(mnt_tab *tb, const char *filename) +int mnt_tab_parse_stream(mnt_tab *tb, FILE *f, const char *filename) { - FILE *f; int nlines = 0; assert(tb); + assert(f); assert(filename); - if (!filename) - return -1; - - f = fopen(filename, "r"); - if (!f) - return -1; - DBG(DEBUG_TAB, fprintf(stderr, "libmount: tab %p: start parsing %s\n", tb, filename)); @@ -427,22 +404,61 @@ int mnt_tab_parse_file(mnt_tab *tb, const char *filename) DBG(DEBUG_TAB, fprintf(stderr, "libmount: tab %p: stop parsing %s\n", tb, filename)); - fclose(f); return 0; error: DBG(DEBUG_TAB, fprintf(stderr, "libmount: tab %p: error parsing %s\n", tb, filename)); - fclose(f); return -1; } /** - * mnt_new_tab_parse: + * mnt_tab_parse_file: + * @tb: tab pointer + * @filename: file + * + * Parses whole table (e.g. /etc/fstab). + * + * + * + * mnt_tab *tb = mnt_new_tab(); + * int rc; + * + * rc = mnt_tab_parse_file(tb, "/etc/fstab"); + * if (!rc) + * mnt_fprintf_tab(tb, stdout, NULL); + * mnt_free_tab(tb); + * + * + * + * The libmount parser ignores broken (syntax error) lines, these lines are + * reported to caller by errcb() function (see mnt_tab_set_parser_errcb()). + * + * Returns: 0 on success, -1 in case of error. + */ +int mnt_tab_parse_file(mnt_tab *tb, const char *filename) +{ + FILE *f; + int rc = -1; + + assert(tb); + assert(filename); + + if (!filename || !tb) + return -1; + + f = fopen(filename, "r"); + if (f) { + rc = mnt_tab_parse_stream(tb, f, filename); + fclose(f); + } + return rc; +} + +/** + * mnt_new_tab_from_file: * @filename: /etc/{m,fs}tab or /proc/self/mountinfo path * - * Same as mnt_new_tab() + mnt_tab_parse_file(). Note that this function does - * not provide details (by mnt_tab_strerror()) about failed parsing -- so you - * should not to use this function for user-writeable files like /etc/fstab. + * Same as mnt_new_tab() + mnt_tab_parse_file(). * * Returns: newly allocated tab on success and NULL in case of error. */ -- 2.39.5