From: Karel Zak Date: Wed, 6 Oct 2010 21:25:15 +0000 (+0200) Subject: libmount: add mnt_tab_find_pair() X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=059c696f3e6d2982b30fb70c36ae7d1859a5cb91;p=util-linux libmount: add mnt_tab_find_pair() Signed-off-by: Karel Zak --- diff --git a/shlibs/mount/src/mount.h.in b/shlibs/mount/src/mount.h.in index 3890dd4f..43f81080 100644 --- a/shlibs/mount/src/mount.h.in +++ b/shlibs/mount/src/mount.h.in @@ -276,6 +276,8 @@ extern mnt_fs *mnt_tab_find_srcpath(mnt_tab *tb, const char *path, int direction extern mnt_fs *mnt_tab_find_tag(mnt_tab *tb, const char *tag, const char *val, int direction); extern mnt_fs *mnt_tab_find_source(mnt_tab *tb, const char *source, int direction); +extern mnt_fs *mnt_tab_find_pair(mnt_tab *tb, const char *source, + const char *target, int direction); extern int mnt_tab_find_next_fs(mnt_tab *tb, mnt_iter *itr, int (*match_func)(mnt_fs *, void *), void *userdata, diff --git a/shlibs/mount/src/mount.sym b/shlibs/mount/src/mount.sym index 5f527605..f97d0048 100644 --- a/shlibs/mount/src/mount.sym +++ b/shlibs/mount/src/mount.sym @@ -94,6 +94,7 @@ global: mnt_split_optstr; mnt_tab_add_fs; mnt_tab_find_next_fs; + mnt_tab_find_pair; mnt_tab_find_source; mnt_tab_find_srcpath; mnt_tab_find_tag; diff --git a/shlibs/mount/src/tab.c b/shlibs/mount/src/tab.c index b30bc00d..2b680270 100644 --- a/shlibs/mount/src/tab.c +++ b/shlibs/mount/src/tab.c @@ -650,6 +650,45 @@ mnt_fs *mnt_tab_find_source(mnt_tab *tb, const char *source, int direction) return fs; } +/** + * mnt_tab_find_pair + * @tb: tab pointer + * @source: TAG or path + * @target: mountpoint + * @direction: MNT_ITER_{FORWARD,BACKWARD} + * + * This function is implemented by mnt_fs_match_source() and + * mnt_fs_match_target() functions. It means that this is more expensive that + * others mnt_tab_find_* function, because every @tab entry is fully evaluated. + * + * Returns: a tab entry or NULL. + */ +mnt_fs *mnt_tab_find_pair(mnt_tab *tb, const char *source, + const char *target, int direction) +{ + mnt_fs *fs = NULL; + mnt_iter itr; + + assert(tb); + assert(source); + assert(target); + + if (!tb || !source || !target) + return NULL; + + DBG(TAB, mnt_debug_h(tb, "lookup SOURCE: %s TARGET: %s", source, target)); + + mnt_reset_iter(&itr, direction); + while(mnt_tab_next_fs(tb, &itr, &fs) == 0) { + + if (mnt_fs_match_target(fs, target, tb->cache) && + mnt_fs_match_source(fs, source, tb->cache)) + return fs; + } + + return NULL; +} + #ifdef TEST_PROGRAM static int parser_errcb(mnt_tab *tb, const char *filename, int line, int flag) @@ -682,6 +721,7 @@ int test_copy_fs(struct mtest *ts, int argc, char *argv[]) { mnt_tab *tb; mnt_fs *fs; + int rc = -1; tb = create_tab(argv[1]); if (!tb) @@ -689,28 +729,30 @@ int test_copy_fs(struct mtest *ts, int argc, char *argv[]) fs = mnt_tab_find_target(tb, "/", MNT_ITER_FORWARD); if (!fs) - goto err; + goto done; printf("ORIGINAL:\n"); mnt_fs_print_debug(fs, stdout); fs = mnt_copy_fs(fs); if (!fs) - goto err; + goto done; printf("COPY:\n"); mnt_fs_print_debug(fs, stdout); mnt_free_fs(fs); -err: + rc = 0; +done: mnt_free_tab(tb); - return 0; + return rc; } int test_parse(struct mtest *ts, int argc, char *argv[]) { - mnt_tab *tb; - mnt_iter *itr; + mnt_tab *tb = NULL; + mnt_iter *itr = NULL; mnt_fs *fs; + int rc = -1; tb = create_tab(argv[1]); if (!tb) @@ -718,38 +760,40 @@ int test_parse(struct mtest *ts, int argc, char *argv[]) itr = mnt_new_iter(MNT_ITER_FORWARD); if (!itr) - goto err; + goto done; while(mnt_tab_next_fs(tb, itr, &fs) == 0) mnt_fs_print_debug(fs, stdout); -err: + rc = 0; +done: mnt_free_iter(itr); mnt_free_tab(tb); - return 0; + return rc; } int test_find(struct mtest *ts, int argc, char *argv[], int dr) { mnt_tab *tb; mnt_fs *fs = NULL; - mnt_cache *mpc; + mnt_cache *mpc = NULL; const char *file, *find, *what; + int rc = -1; if (argc != 4) { fprintf(stderr, "try --help\n"); - goto err; + return -EINVAL; } file = argv[1], find = argv[2], what = argv[3]; tb = create_tab(file); if (!tb) - goto err; + goto done; /* create a cache for canonicalized paths */ mpc = mnt_new_cache(); if (!mpc) - goto err; + goto done; mnt_tab_set_cache(tb, mpc); if (strcasecmp(find, "source") == 0) @@ -770,12 +814,12 @@ int test_find(struct mtest *ts, int argc, char *argv[], int dr) } printf("|%s|%s\n", mnt_fs_get_target(fs), mnt_fs_get_optstr(fs)); + rc = 0; } +done: mnt_free_tab(tb); mnt_free_cache(mpc); - return 0; -err: - return -1; + return rc; } int test_find_bw(struct mtest *ts, int argc, char *argv[]) @@ -788,12 +832,34 @@ int test_find_fw(struct mtest *ts, int argc, char *argv[]) return test_find(ts, argc, argv, MNT_ITER_FORWARD); } +int test_find_pair(struct mtest *ts, int argc, char *argv[]) +{ + mnt_tab *tb; + mnt_fs *fs; + int rc = -1; + + tb = create_tab(argv[1]); + if (!tb) + return -1; + + fs = mnt_tab_find_pair(tb, argv[2], argv[3], MNT_ITER_FORWARD); + if (!fs) + goto done; + + mnt_fs_print_debug(fs, stdout); + rc = 0; +done: + mnt_free_tab(tb); + return rc; +} + int main(int argc, char *argv[]) { struct mtest tss[] = { { "--parse", test_parse, " parse and print tab" }, { "--find-forward", test_find_fw, " " }, { "--find-backward", test_find_bw, " " }, + { "--find-pair", test_find_pair, " " }, { "--copy-fs", test_copy_fs, " copy root FS from the file" }, { NULL } };