From fbb3eb85d328f0de2dce212e3336ba8655195de4 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 21 Jun 2010 23:15:54 +0200 Subject: [PATCH] libmount: improve parser return codes Signed-off-by: Karel Zak --- shlibs/mount/src/tab.c | 11 +++++++++++ shlibs/mount/src/tab_parse.c | 25 +++++++++++++++---------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/shlibs/mount/src/tab.c b/shlibs/mount/src/tab.c index c7665ee4..4c06d8be 100644 --- a/shlibs/mount/src/tab.c +++ b/shlibs/mount/src/tab.c @@ -762,6 +762,13 @@ error: } #ifdef TEST_PROGRAM + +static int parser_errcb(mnt_tab *tb, const char *filename, int line, int flag) +{ + fprintf(stderr, "%s:%d: parse error\n", filename, line); + return 0; +} + mnt_tab *create_tab(const char *file) { mnt_tab *tb; @@ -771,6 +778,9 @@ mnt_tab *create_tab(const char *file) tb = mnt_new_tab(file); if (!tb) goto err; + + mnt_tab_set_parser_errcb(tb, parser_errcb); + if (mnt_tab_parse_file(tb) != 0) goto err; return tb; @@ -792,6 +802,7 @@ int test_parse(struct mtest *ts, int argc, char *argv[]) itr = mnt_new_iter(MNT_ITER_FORWARD); if (!itr) goto err; + while(mnt_tab_next_fs(tb, itr, &fs) == 0) mnt_fs_print_debug(fs, stdout); err: diff --git a/shlibs/mount/src/tab_parse.c b/shlibs/mount/src/tab_parse.c index 4942692a..d7cb1b77 100644 --- a/shlibs/mount/src/tab_parse.c +++ b/shlibs/mount/src/tab_parse.c @@ -330,6 +330,7 @@ static int mnt_tab_parse_next(mnt_tab *tb, FILE *f, mnt_fs *fs, int *nlines) /* parse /etc/{fs,m}tab */ if (mnt_tab_parse_file_line(fs, s) != 0) goto err; + } else if (tb->fmt == MNT_FMT_MOUNTINFO) { /* parse /proc/self/mountinfo */ if (mnt_parse_mountinfo_line(fs, s) != 0) @@ -340,11 +341,11 @@ static int mnt_tab_parse_next(mnt_tab *tb, FILE *f, mnt_fs *fs, int *nlines) if (!fs->optstr && (fs->vfs_optstr || fs->fs_optstr)) { fs->optstr = merge_optstr(fs->vfs_optstr, fs->fs_optstr); if (!fs->optstr) - goto err; + return -1; } DBG(DEBUG_TAB, fprintf(stderr, - "libmount: %s: %d: SOURCE:%s, MNTPOINT:%s, TYPE:%s, " + "libmount: %s:%d: SOURCE:%s, MNTPOINT:%s, TYPE:%s, " "OPTS:%s, FREQ:%d, PASSNO:%d\n", tb->filename, *nlines, fs->source, fs->target, fs->fstype, @@ -352,10 +353,13 @@ static int mnt_tab_parse_next(mnt_tab *tb, FILE *f, mnt_fs *fs, int *nlines) return 0; err: - if (tb->errcb) - return tb->errcb(tb, NULL, *nlines, 0); + DBG(DEBUG_TAB, fprintf(stderr, + "libmount: %s:%d: parse error\n", tb->filename, *nlines)); - return 0; + if (tb->errcb && tb->errcb(tb, tb->filename, *nlines, 0)) + return -1; /* fatal error */ + + return 1; /* recoverable error */ } /** @@ -402,19 +406,20 @@ int mnt_tab_parse_file(mnt_tab *tb) while (!feof(f)) { int rc; mnt_fs *fs = mnt_new_fs(); + if (!fs) goto error; rc = mnt_tab_parse_next(tb, f, fs, &nlines); if (!rc) rc = mnt_tab_add_fs(tb, fs); - else if (feof(f)) { - mnt_free_fs(fs); - break; - } if (rc) { mnt_free_fs(fs); - goto error; + if (rc == 1) + continue; /* recoverable error */ + if (feof(f)) + break; + goto error; /* fatal error */ } } -- 2.39.5