From 9ecdf48fc316b057acbaf744bb32a18e281a7924 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 26 Jul 2010 16:24:29 +0200 Subject: [PATCH] libmount: add mnt_copy_fs() and mnt_fs_set_root() Signed-off-by: Karel Zak --- shlibs/mount/src/fs.c | 87 +++++++++++++++++++++++++++++++++++++ shlibs/mount/src/mount.h.in | 4 +- shlibs/mount/src/mount.sym | 2 + 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/shlibs/mount/src/fs.c b/shlibs/mount/src/fs.c index 0c9e06ea..f0eae744 100644 --- a/shlibs/mount/src/fs.c +++ b/shlibs/mount/src/fs.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "nls.h" #include "mountP.h" @@ -61,6 +62,68 @@ void mnt_free_fs(mnt_fs *fs) free(fs); } +static inline int cpy_str_item(void *new, void *old, size_t offset) +{ + char **o = (char **) (old + offset); + char **n = (char **) (new + offset); + + if (!*o) + return 0; /* source (old) is empty */ + + *n = strdup(*o); + if (!*n) + return -1; + return 0; +} + +/** + * mnt_copy_fs: + * @fs: source FS + * + * This function does not copy userdata (se mnt_fs_set_userdata()). A new copy is + * not linked with any existing mnt_tab. + * + * Returns: copy of @fs + */ +mnt_fs *mnt_copy_fs(mnt_fs *fs) +{ + mnt_fs *n = mnt_new_fs(); + + if (!n) + return NULL; + + n->id = fs->id; + n->parent = fs->parent; + n->devno = fs->devno; + + if (cpy_str_item(n, fs, offsetof(struct _mnt_fs, source))) + goto err; + if (cpy_str_item(n, fs, offsetof(struct _mnt_fs, tagname))) + goto err; + if (cpy_str_item(n, fs, offsetof(struct _mnt_fs, tagval))) + goto err; + if (cpy_str_item(n, fs, offsetof(struct _mnt_fs, root))) + goto err; + if (cpy_str_item(n, fs, offsetof(struct _mnt_fs, target))) + goto err; + if (cpy_str_item(n, fs, offsetof(struct _mnt_fs, fstype))) + goto err; + if (cpy_str_item(n, fs, offsetof(struct _mnt_fs, optstr))) + goto err; + if (cpy_str_item(n, fs, offsetof(struct _mnt_fs, vfs_optstr))) + goto err; + if (cpy_str_item(n, fs, offsetof(struct _mnt_fs, fs_optstr))) + goto err; + n->freq = fs->freq; + n->passno = fs->passno; + n->flags = fs->flags; + + return n; +err: + mnt_free_fs(n); + return NULL; +} + /** * mnt_fs_get_userdata: * @fs: mnt_file instance @@ -482,6 +545,30 @@ const char *mnt_fs_get_root(mnt_fs *fs) return fs ? fs->root : NULL; } +/** + * mnt_fs_set_root: + * @fs: mountinfo entry + * @root: path + * + * Returns: 0 on success or -1 in case of error. + */ +int mnt_fs_set_root(mnt_fs *fs, const char *root) +{ + char *p = NULL; + + assert(fs); + if (!fs) + return -1; + if (root) { + p = strdup(root); + if (!p) + return -1; + } + free(fs->root); + fs->root = p; + return 0; +} + /** * mnt_fs_get_id: * @fs: /proc/self/mountinfo entry diff --git a/shlibs/mount/src/mount.h.in b/shlibs/mount/src/mount.h.in index af1dd38a..33f8d172 100644 --- a/shlibs/mount/src/mount.h.in +++ b/shlibs/mount/src/mount.h.in @@ -146,7 +146,7 @@ extern int mnt_optstr_set_option(char **optstr, const char *name, const char *value); extern int mnt_optstr_remove_option(char **optstr, const char *name); -extern int mnt_split_optstr(char *optstr, char **user, char **vfs, char **fs); +extern int mnt_split_optstr(const char *optstr, char **user, char **vfs, char **fs); /* iter.c */ enum { @@ -224,6 +224,7 @@ extern int mnt_lock_file(mnt_lock *ml); /* fs.c */ extern mnt_fs *mnt_new_fs(void); extern void mnt_free_fs(mnt_fs *ent); +extern mnt_fs *mnt_copy_fs(mnt_fs *fs); extern void *mnt_fs_get_userdata(mnt_fs *fs); extern int mnt_fs_set_userdata(mnt_fs *fs, void *data); extern const char *mnt_fs_get_source(mnt_fs *ent); @@ -244,6 +245,7 @@ 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 const char *mnt_fs_get_root(mnt_fs *fs); +extern int mnt_fs_set_root(mnt_fs *fs, const char *root); 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); diff --git a/shlibs/mount/src/mount.sym b/shlibs/mount/src/mount.sym index 7824597c..d37bb8b1 100644 --- a/shlibs/mount/src/mount.sym +++ b/shlibs/mount/src/mount.sym @@ -10,6 +10,7 @@ global: mnt_cache_find_tag; mnt_cache_find_tag_value; mnt_cache_read_tags; + mnt_copy_fs; mnt_free_cache; mnt_free_fs; mnt_free_iter; @@ -42,6 +43,7 @@ global: mnt_fs_set_fstype; mnt_fs_set_optstr; mnt_fs_set_passno; + mnt_fs_set_root; mnt_fs_set_source; mnt_fs_set_target; mnt_fs_set_userdata; -- 2.39.5