]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: remove duplicate code
authorKarel Zak <kzak@redhat.com>
Fri, 22 Apr 2016 11:59:06 +0000 (13:59 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 22 Apr 2016 11:59:06 +0000 (13:59 +0200)
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>
include/strutils.h
libfdisk/src/ask.c
libmount/src/fs.c
libmount/src/tab.c

index e9f7921758d3aa4c6befb7b5d690d60fa155406c..9f4339f41e21dc6268f848c5e9e673a64d1947bf 100644 (file)
@@ -7,6 +7,7 @@
 #include <sys/types.h>
 #include <ctype.h>
 #include <stdio.h>
+#include <errno.h>
 
 /* default strtoxx_or_err() exit code */
 #ifndef STRTOXX_EXIT_CODE
@@ -60,20 +61,24 @@ static inline void xstrncpy(char *dest, const char *src, size_t n)
        dest[n-1] = 0;
 }
 
-static inline char *strdup_to_offset(void *stru, size_t offset, const char *str)
+static inline int strdup_to_offset(void *stru, size_t offset, const char *str)
 {
        char *n = NULL;
-       char **o = (char **) ((char *) stru + offset);
+       char **o;
 
+       if (!stru)
+               return -EINVAL;
+
+       o = (char **) ((char *) stru + offset);
        if (str) {
                n = strdup(str);
                if (!n)
-                       return NULL;
+                       return -ENOMEM;
        }
 
        free(*o);
        *o = n;
-       return n;
+       return 0;
 }
 
 #define strdup_to_struct_member(_s, _m, _str) \
index fd0555e307bd7eca605446df83607ad6252d8541..cbf5e97384f4446e7e95107565daef869c830d72 100644 (file)
@@ -106,7 +106,7 @@ const char *fdisk_ask_get_query(struct fdisk_ask *ask)
 int fdisk_ask_set_query(struct fdisk_ask *ask, const char *str)
 {
        assert(ask);
-       return !strdup_to_struct_member(ask, query, str) ? -ENOMEM : 0;
+       return strdup_to_struct_member(ask, query, str);
 }
 
 /**
index 2bab7d6af6db5ff74d80b0049fa3b21264ec8391..df00504baa5ef5d2dca00710ab7b51f5ea1cf3f0 100644 (file)
@@ -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);
 }
 
 /**
index a8d835e042401118fb4c0baee0db5122b9b9def7..b09c79f0d6705cd33999779a5dd745ee921eab89 100644 (file)
@@ -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);
 }
 
 /**