[Ocfs2-tools-devel] [PATCH 2/5] debugfs: Add utility functions to manage a list of strings

Sunil Mushran sunil.mushran at oracle.com
Wed Oct 8 16:05:44 PDT 2008


Patch adds utility functions to manage a list of strings. This
will be used in the upcoming patch that allows users to specify
a list of locknames for the fs_locks and dlm_locks commands.

Signed-off-by: Sunil Mushran <sunil.mushran at oracle.com>
---
 debugfs.ocfs2/include/utils.h |   10 ++++++
 debugfs.ocfs2/utils.c         |   65 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 0 deletions(-)

diff --git a/debugfs.ocfs2/include/utils.h b/debugfs.ocfs2/include/utils.h
index 1d71c11..7b0da94 100644
--- a/debugfs.ocfs2/include/utils.h
+++ b/debugfs.ocfs2/include/utils.h
@@ -32,6 +32,11 @@ typedef struct _rdump_opts {
 	int verbose;
 } rdump_opts;
 
+struct strings {
+	char *s_str;
+	struct list_head s_list;
+};
+
 void get_incompat_flag(uint32_t flag, GString *str);
 void get_tunefs_flag(uint32_t incompat_flag, uint16_t flag, GString *str);
 void get_compat_flag(uint32_t flag, GString *str);
@@ -60,4 +65,9 @@ errcode_t get_debugfs_path(char *debugfs_path, int len);
 errcode_t open_debugfs_file(const char *debugfs_path, const char *dirname,
 			    const char *uuid, const char *filename, FILE **fd);
 
+void init_stringlist(struct list_head *strlist);
+void free_stringlist(struct list_head *strlist);
+errcode_t add_to_stringlist(char *str, struct list_head *strlist);
+int del_from_stringlist(char *str, struct list_head *strlist);
+
 #endif		/* __UTILS_H__ */
diff --git a/debugfs.ocfs2/utils.c b/debugfs.ocfs2/utils.c
index 1c302c9..b09b069 100644
--- a/debugfs.ocfs2/utils.c
+++ b/debugfs.ocfs2/utils.c
@@ -911,3 +911,68 @@ errcode_t open_debugfs_file(const char *debugfs_path, const char *dirname,
 out:
 	return ret;
 }
+
+void init_stringlist(struct list_head *strlist)
+{
+	INIT_LIST_HEAD(strlist);
+}
+
+errcode_t add_to_stringlist(char *str, struct list_head *strlist)
+{
+	struct strings *s;
+
+	if (!str || !strlen(str))
+		return 0;
+
+	s = calloc(1, sizeof(struct strings));
+	if (!s)
+		return OCFS2_ET_NO_MEMORY;
+
+	INIT_LIST_HEAD(&s->s_list);
+	s->s_str = strdup(str);
+	if (!s->s_str) {
+		free(s);
+		return OCFS2_ET_NO_MEMORY;
+	}
+
+	list_add_tail(&s->s_list, strlist);
+
+	return 0;
+}
+
+void free_stringlist(struct list_head *strlist)
+{
+	struct strings *s;
+	struct list_head *iter, *iter2;
+
+	if (list_empty(strlist))
+		return;
+
+	list_for_each_safe(iter, iter2, strlist) {
+		s = list_entry(iter, struct strings, s_list);
+		list_del(iter);
+		free(s->s_str);
+		free(s);
+	}
+}
+
+int del_from_stringlist(char *str, struct list_head *strlist)
+{
+	struct strings *s;
+	struct list_head *iter, *iter2;
+
+	if (!list_empty(strlist)) {
+		list_for_each_safe(iter, iter2, strlist) {
+			s = list_entry(iter, struct strings, s_list);
+			if (!strcmp(str, s->s_str)) {
+				list_del(iter);
+				free(s->s_str);
+				free(s);
+				return 1;
+			}
+		}
+	}
+
+	return 0;
+}
+
-- 
1.5.4.3




More information about the Ocfs2-tools-devel mailing list