summaryrefslogtreecommitdiffstats
path: root/libmount/src
diff options
context:
space:
mode:
authorKarel Zak2016-04-22 13:59:06 +0200
committerKarel Zak2016-04-22 13:59:06 +0200
commitdeb1c903272d45e8a1975b1da151d4deb3defe3b (patch)
tree72bc20ea7cad9056bc6ae669debd02a8ed89f7b0 /libmount/src
parenttests: move getopt to separate directory (diff)
downloadkernel-qcow2-util-linux-deb1c903272d45e8a1975b1da151d4deb3defe3b.tar.gz
kernel-qcow2-util-linux-deb1c903272d45e8a1975b1da151d4deb3defe3b.tar.xz
kernel-qcow2-util-linux-deb1c903272d45e8a1975b1da151d4deb3defe3b.zip
libmount: remove duplicate code
For petty long time we have strdup_to_struct_member() macro to avoid duplicate code when strdup() strings in setter functions. Let's use it for libmount. Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libmount/src')
-rw-r--r--libmount/src/fs.c78
-rw-r--r--libmount/src/tab.c26
2 files changed, 12 insertions, 92 deletions
diff --git a/libmount/src/fs.c b/libmount/src/fs.c
index 2bab7d6af..df00504ba 100644
--- a/libmount/src/fs.c
+++ b/libmount/src/fs.c
@@ -518,27 +518,15 @@ const char *mnt_fs_get_target(struct libmnt_fs *fs)
/**
* mnt_fs_set_target:
* @fs: fstab/mtab/mountinfo entry
- * @target: mountpoint
+ * @tgt: mountpoint
*
- * This function creates a private copy (strdup()) of @target.
+ * This function creates a private copy (strdup()) of @tgt.
*
* Returns: 0 on success or negative number in case of error.
*/
-int mnt_fs_set_target(struct libmnt_fs *fs, const char *target)
+int mnt_fs_set_target(struct libmnt_fs *fs, const char *tgt)
{
- char *p = NULL;
-
- if (!fs)
- return -EINVAL;
- if (target) {
- p = strdup(target);
- if (!p)
- return -ENOMEM;
- }
- free(fs->target);
- fs->target = p;
-
- return 0;
+ return strdup_to_struct_member(fs, target, tgt);
}
static int mnt_fs_get_flags(struct libmnt_fs *fs)
@@ -980,19 +968,7 @@ const char *mnt_fs_get_attributes(struct libmnt_fs *fs)
*/
int mnt_fs_set_attributes(struct libmnt_fs *fs, const char *optstr)
{
- char *p = NULL;
-
- if (!fs)
- return -EINVAL;
- if (optstr) {
- p = strdup(optstr);
- if (!p)
- return -ENOMEM;
- }
- free(fs->attrs);
- fs->attrs = p;
-
- return 0;
+ return strdup_to_struct_member(fs, attrs, optstr);
}
/**
@@ -1098,24 +1074,13 @@ const char *mnt_fs_get_root(struct libmnt_fs *fs)
/**
* mnt_fs_set_root:
* @fs: mountinfo entry
- * @root: path
+ * @path: root path
*
* Returns: 0 on success or negative number in case of error.
*/
-int mnt_fs_set_root(struct libmnt_fs *fs, const char *root)
+int mnt_fs_set_root(struct libmnt_fs *fs, const char *path)
{
- char *p = NULL;
-
- if (!fs)
- return -EINVAL;
- if (root) {
- p = strdup(root);
- if (!p)
- return -ENOMEM;
- }
- free(fs->root);
- fs->root = p;
- return 0;
+ return strdup_to_struct_member(fs, root, path);
}
/**
@@ -1196,18 +1161,7 @@ const char *mnt_fs_get_bindsrc(struct libmnt_fs *fs)
*/
int mnt_fs_set_bindsrc(struct libmnt_fs *fs, const char *src)
{
- char *p = NULL;
-
- if (!fs)
- return -EINVAL;
- if (src) {
- p = strdup(src);
- if (!p)
- return -ENOMEM;
- }
- free(fs->bindsrc);
- fs->bindsrc = p;
- return 0;
+ return strdup_to_struct_member(fs, bindsrc, src);
}
/**
@@ -1326,19 +1280,7 @@ const char *mnt_fs_get_comment(struct libmnt_fs *fs)
*/
int mnt_fs_set_comment(struct libmnt_fs *fs, const char *comm)
{
- char *p = NULL;
-
- if (!fs)
- return -EINVAL;
- if (comm) {
- p = strdup(comm);
- if (!p)
- return -ENOMEM;
- }
-
- free(fs->comment);
- fs->comment = p;
- return 0;
+ return strdup_to_struct_member(fs, comment, comm);
}
/**
diff --git a/libmount/src/tab.c b/libmount/src/tab.c
index a8d835e04..b09c79f0d 100644
--- a/libmount/src/tab.c
+++ b/libmount/src/tab.c
@@ -287,18 +287,7 @@ const char *mnt_table_get_intro_comment(struct libmnt_table *tb)
*/
int mnt_table_set_intro_comment(struct libmnt_table *tb, const char *comm)
{
- char *p = NULL;
-
- if (!tb)
- return -EINVAL;
- if (comm) {
- p = strdup(comm);
- if (!p)
- return -ENOMEM;
- }
- free(tb->comm_intro);
- tb->comm_intro = p;
- return 0;
+ return strdup_to_struct_member(tb, comm_intro, comm);
}
/**
@@ -339,18 +328,7 @@ const char *mnt_table_get_trailing_comment(struct libmnt_table *tb)
*/
int mnt_table_set_trailing_comment(struct libmnt_table *tb, const char *comm)
{
- char *p = NULL;
-
- if (!tb)
- return -EINVAL;
- if (comm) {
- p = strdup(comm);
- if (!p)
- return -ENOMEM;
- }
- free(tb->comm_tail);
- tb->comm_tail = p;
- return 0;
+ return strdup_to_struct_member(tb, comm_tail, comm);
}
/**