From efe73c3ef120997c14ad95bf1857a044e99872b6 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 2 Mar 2010 22:34:56 +0100 Subject: libmount: read devno, ID and parent ID from /proc/self/mountinfo The patch also removes obsolete test program from tab_parse.c. Signed-off-by: Karel Zak --- shlibs/mount/src/fs.c | 54 ++++++++++++++++++++++++++++++----- shlibs/mount/src/mount.h.in | 3 ++ shlibs/mount/src/mount.sym | 3 ++ shlibs/mount/src/mountP.h | 1 + shlibs/mount/src/tab.c | 10 ++++++- shlibs/mount/src/tab_parse.c | 68 +++++--------------------------------------- 6 files changed, 70 insertions(+), 69 deletions(-) (limited to 'shlibs/mount') diff --git a/shlibs/mount/src/fs.c b/shlibs/mount/src/fs.c index 01c2d0277..fef61b0d6 100644 --- a/shlibs/mount/src/fs.c +++ b/shlibs/mount/src/fs.c @@ -328,7 +328,7 @@ const char *mnt_fs_get_fs_optstr(mnt_fs *fs) /** * mnt_fs_get_vfs_optstr: - * @fs: fstab/mtab/mountinfo entry pointer + * @fs: fstab/mtab entry pointer * * This function works for "mountinfo" files only. * @@ -355,7 +355,7 @@ int mnt_fs_get_freq(mnt_fs *fs) /** * mnt_fs_set_freq: - * @fs: fstab/mtab/mountinfo entry pointer + * @fs: fstab/mtab entry pointer * @freq: dump frequency in days * * Returns 0 on success or -1 in case of error. @@ -363,7 +363,6 @@ int mnt_fs_get_freq(mnt_fs *fs) int mnt_fs_set_freq(mnt_fs *fs, int freq) { assert(fs); - if (!fs) return -1; fs->freq = freq; @@ -372,7 +371,7 @@ int mnt_fs_set_freq(mnt_fs *fs, int freq) /** * mnt_fs_get_passno: - * @fs: fstab/mtab/mountinfo entry pointer + * @fs: fstab/mtab entry pointer * * Returns "pass number on parallel fsck". */ @@ -384,7 +383,7 @@ int mnt_fs_get_passno(mnt_fs *fs) /** * mnt_fs_set_passno: - * @fs: fstab/mtab/mountinfo entry pointer + * @fs: fstab/mtab entry pointer * @passno: pass number * * Returns 0 on success or -1 in case of error. @@ -392,13 +391,50 @@ int mnt_fs_get_passno(mnt_fs *fs) int mnt_fs_set_passno(mnt_fs *fs, int passno) { assert(fs); - if (!fs) return -1; fs->passno = passno; return 0; } +/** + * mnt_fs_get_id: + * @fs: /proc/self/mountinfo entry + * + * Returns: mount ID (unique identifier of the mount) or -1 if ID undefined + * (for example if @fs is not mountinfo entry). + */ +int mnt_fs_get_id(mnt_fs *fs) +{ + assert(fs); + return fs ? fs->id : -1; +} + +/** + * mnt_fs_get_parent_id: + * @fs: /proc/self/mountinfo entry + * + * Returns: parent mount ID or -1 if ID undefined (for example if @fs is not + * mountinfo entry). + */ +int mnt_fs_get_parent_id(mnt_fs *fs) +{ + assert(fs); + return fs ? fs->parent : -1; +} + +/** + * mnt_fs_get_devno: + * @fs: /proc/self/mountinfo + * + * Returns: value of st_dev for files on filesystem or 0 in case of error. + */ +dev_t mnt_fs_get_devno(mnt_fs *fs) +{ + assert(fs); + return fs ? fs->devno : 0; +} + /** * mnt_fs_get_option: * @fs: fstab/mtab/mountinfo entry pointer @@ -415,7 +451,6 @@ int mnt_fs_get_option(mnt_fs *fs, const char *name, return optstr ? mnt_optstr_get_option(optstr, name, value, valsz) : 1; } - /** * mnt_fs_match_target: * @fs: filesystem @@ -689,6 +724,11 @@ int mnt_fs_print_debug(mnt_fs *fs, FILE *file) fprintf(file, "optstr: %s\n", mnt_fs_get_optstr(fs)); fprintf(file, "freq: %d\n", mnt_fs_get_freq(fs)); fprintf(file, "pass: %d\n", mnt_fs_get_passno(fs)); + fprintf(file, "id: %d\n", mnt_fs_get_id(fs)); + fprintf(file, "parent: %d\n", mnt_fs_get_parent_id(fs)); + fprintf(file, "devno: %d:%d\n", major(mnt_fs_get_devno(fs)), + minor(mnt_fs_get_devno(fs))); + return 0; } diff --git a/shlibs/mount/src/mount.h.in b/shlibs/mount/src/mount.h.in index 04bbee963..8e71213aa 100644 --- a/shlibs/mount/src/mount.h.in +++ b/shlibs/mount/src/mount.h.in @@ -227,6 +227,9 @@ extern int mnt_fs_get_freq(mnt_fs *ent); extern int mnt_fs_set_freq(mnt_fs *ent, int freq); extern int mnt_fs_get_passno(mnt_fs *ent); extern int mnt_fs_set_passno(mnt_fs *ent, int passno); +extern int mnt_fs_get_id(mnt_fs *fs); +extern int mnt_fs_get_parent_id(mnt_fs *fs); +extern dev_t mnt_fs_get_devno(mnt_fs *fs); extern int mnt_fs_get_option(mnt_fs *ent, const char *name, char **value, size_t *valsz); diff --git a/shlibs/mount/src/mount.sym b/shlibs/mount/src/mount.sym index f37214a67..3bc32ecdd 100644 --- a/shlibs/mount/src/mount.sym +++ b/shlibs/mount/src/mount.sym @@ -17,12 +17,15 @@ global: mnt_free_optls; mnt_free_tab; mnt_fs_fprintf; + mnt_fs_get_devno; mnt_fs_get_freq; mnt_fs_get_fstype; + mnt_fs_get_id; mnt_fs_get_optfs; mnt_fs_get_option; mnt_fs_get_optstr; mnt_fs_get_optvfs; + mnt_fs_get_parent_id; mnt_fs_get_passno; mnt_fs_get_source; mnt_fs_get_srcpath; diff --git a/shlibs/mount/src/mountP.h b/shlibs/mount/src/mountP.h index 3ba3205f6..e92ba701a 100644 --- a/shlibs/mount/src/mountP.h +++ b/shlibs/mount/src/mountP.h @@ -135,6 +135,7 @@ struct _mnt_fs { int id; /* mountinfo[1]: ID */ int parent; /* moutninfo[2]: parent */ + dev_t devno; /* moutninfo[3]: st_dev */ char *source; /* fstab[1]: mountinfo[10]: * source dev, file, dir or TAG */ diff --git a/shlibs/mount/src/tab.c b/shlibs/mount/src/tab.c index bfd3a6e12..2ae694b5e 100644 --- a/shlibs/mount/src/tab.c +++ b/shlibs/mount/src/tab.c @@ -709,12 +709,20 @@ err: int test_parse(struct mtest *ts, int argc, char *argv[]) { mnt_tab *tb; + mnt_iter *itr; + mnt_fs *fs; tb = create_tab(argv[1]); if (!tb) return -1; - mnt_tab_fprintf(tb, stdout, MNT_MFILE_PRINTFMT); + 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: + mnt_free_iter(itr); mnt_free_tab(tb); return 0; } diff --git a/shlibs/mount/src/tab_parse.c b/shlibs/mount/src/tab_parse.c index feaab74af..f8d6680fd 100644 --- a/shlibs/mount/src/tab_parse.c +++ b/shlibs/mount/src/tab_parse.c @@ -166,6 +166,8 @@ static int mnt_tab_parse_file_line(mnt_fs *fs, char *s) */ static int mnt_parse_mountinfo_line(mnt_fs *fs, char *s) { + unsigned int maj, min; + /* ID */ if (next_number(&s, &fs->id) != 0) return 1; @@ -174,9 +176,12 @@ static int mnt_parse_mountinfo_line(mnt_fs *fs, char *s) if (next_number(&s, &fs->parent) != 0) return 1; - /* : (ignore) */ - if (next_word_skip(&s) != 0) + /* : */ + s = skip_spaces(s); + if (!*s || sscanf(s, "%u:%u", &maj, &min) != 2) return 1; + fs->devno = makedev(maj, min); + next_word_skip(&s); /* MOUNTROOT */ fs->mntroot = next_word(&s); @@ -532,62 +537,3 @@ done: return buf; } -#ifdef LIBMOUNT_TEST_PROGRAM -int test_parse(struct mtest *ts, int argc, char *argv[]) -{ - mnt_tab *tb; - mnt_fs *fs; - mnt_iter *itr; - - if (argc != 2) - goto err; - - tb = mnt_new_tab(argv[1]); - if (!tb) - goto err; - if (mnt_tab_parse_file(tb) != 0) - goto err; - if (mnt_tab_get_nerrs(tb)) { - char buf[BUFSIZ]; - - mnt_tab_strerror(tb, buf, sizeof(buf)); - printf("\t%s\n", buf); - goto err; - } - - itr = mnt_new_iter(MNT_ITER_FORWARD); - if (!itr) - goto err; - while(mnt_tab_next_fs(tb, itr, &fs) == 0) { - const char *tg, *vl; - - if (mnt_fs_get_tag(fs, &tg, &vl) == 0) - printf("%s=%s", tg, vl); - else - printf("%s", mnt_fs_get_srcpath(fs)); - - printf("|%s|%s|%s|%d|%d|\n", - mnt_fs_get_target(fs), - mnt_fs_get_fstype(fs), - mnt_fs_get_optstr(fs), - mnt_fs_get_freq(fs), - mnt_fs_get_passno(fs)); - } - mnt_free_tab(tb); - mnt_free_iter(itr); - - return 0; -err: - return -1; -} - -int main(int argc, char *argv[]) -{ - struct mtest tss[] = { - { "--parse", test_parse, " parse the {fs,m}tab or mountinfo file" }, - { NULL } - }; - return mnt_run_test(tss, argc, argv); -} - -#endif /* LIBMOUNT_TEST_PROGRAM */ -- cgit v1.2.3-55-g7522