[OpenWrt-Devel] [PATCH fstools 1/2] libblkid-tiny: add functions for allocating & freeing probe struct

Rafał Miłecki zajec5 at gmail.com
Sat Dec 14 16:55:00 EST 2019


From: Rafał Miłecki <rafal at milecki.pl>

This adds blkid_new_probe() and blkid_free_probe() which have to be used
in place of simple struct memory allocation. They will allow extending
probe struct by any extra initialization code and resources release.

Newly introduced probe.c file is based on original libblkid's code.

Signed-off-by: Rafał Miłecki <rafal at milecki.pl>
---
 CMakeLists.txt                |  1 +
 libblkid-tiny/libblkid-tiny.h |  3 +++
 libblkid-tiny/probe.c         | 31 ++++++++++++++++++++++++++++
 probe.c                       | 38 ++++++++++++++++++++---------------
 4 files changed, 57 insertions(+), 16 deletions(-)
 create mode 100644 libblkid-tiny/probe.c

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7f13c4f..a586577 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -22,6 +22,7 @@ ADD_LIBRARY(blkid-tiny SHARED
 		libblkid-tiny/encode.c
 		libblkid-tiny/libblkid-tiny.c
 		libblkid-tiny/mkdev.c
+		libblkid-tiny/probe.c
 		libblkid-tiny/ext.c
 		libblkid-tiny/jffs2.c
 		libblkid-tiny/vfat.c
diff --git a/libblkid-tiny/libblkid-tiny.h b/libblkid-tiny/libblkid-tiny.h
index 419d5db..06ff94b 100644
--- a/libblkid-tiny/libblkid-tiny.h
+++ b/libblkid-tiny/libblkid-tiny.h
@@ -60,6 +60,9 @@ struct blkid_struct_probe
 	char	version[64];
 };
 
+struct blkid_struct_probe *blkid_new_probe(void);
+void blkid_free_probe(struct blkid_struct_probe *pr);
+
 extern int probe_block(char *block, struct blkid_struct_probe *pr);
 extern int mkblkdev(void);
 
diff --git a/libblkid-tiny/probe.c b/libblkid-tiny/probe.c
new file mode 100644
index 0000000..54e22dc
--- /dev/null
+++ b/libblkid-tiny/probe.c
@@ -0,0 +1,31 @@
+/*
+ * Low-level libblkid probing API
+ *
+ * Copyright (C) 2008-2009 Karel Zak <kzak at redhat.com>
+ *
+ * This file may be redistributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+
+#include <stdlib.h>
+
+#include "libblkid-tiny.h"
+
+struct blkid_struct_probe *blkid_new_probe(void)
+{
+	struct blkid_struct_probe *pr;
+
+	pr = calloc(1, sizeof(struct blkid_struct_probe));
+	if (!pr)
+		return NULL;
+
+	return pr;
+}
+
+void blkid_free_probe(struct blkid_struct_probe *pr)
+{
+	if (!pr)
+		return;
+
+	free(pr);
+}
diff --git a/probe.c b/probe.c
index 63d6f8c..7d0e831 100644
--- a/probe.c
+++ b/probe.c
@@ -21,34 +21,40 @@ static struct probe_info *
 probe_path_tiny(const char *path)
 {
 	struct probe_info *info = NULL;
-	struct blkid_struct_probe pr = { };
+	struct blkid_struct_probe *pr;
 	char *type, *dev, *uuid, *label, *version;
 
-	if (probe_block((char *)path, &pr) == 0 && pr.id && !pr.err) {
+	pr = blkid_new_probe();
+	if (!pr)
+		return NULL;
+
+	if (probe_block((char *)path, pr) == 0 && pr->id && !pr->err) {
 		info = calloc_a(sizeof(*info),
-		                &type,    strlen(pr.id->name) + 1,
-		                &dev,     strlen(pr.dev)      + 1,
-		                &uuid,    strlen(pr.uuid)     + 1,
-		                &label,   strlen(pr.label)    + 1,
-		                &version, strlen(pr.version)  + 1);
+		                &type,    strlen(pr->id->name) + 1,
+		                &dev,     strlen(pr->dev)      + 1,
+		                &uuid,    strlen(pr->uuid)     + 1,
+		                &label,   strlen(pr->label)    + 1,
+		                &version, strlen(pr->version)  + 1);
 
 		if (info) {
-			info->type = strcpy(type, pr.id->name);
+			info->type = strcpy(type, pr->id->name);
 
-			if (pr.dev[0])
-				info->dev = strcpy(dev, pr.dev);
+			if (pr->dev[0])
+				info->dev = strcpy(dev, pr->dev);
 
-			if (pr.uuid[0])
-				info->uuid = strcpy(uuid, pr.uuid);
+			if (pr->uuid[0])
+				info->uuid = strcpy(uuid, pr->uuid);
 
-			if (pr.label[0])
-				info->label = strcpy(label, pr.label);
+			if (pr->label[0])
+				info->label = strcpy(label, pr->label);
 
-			if (pr.version[0])
-				info->version = strcpy(version, pr.version);
+			if (pr->version[0])
+				info->version = strcpy(version, pr->version);
 		}
 	}
 
+	blkid_free_probe(pr);
+
 	return info;
 }
 
-- 
2.21.0


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


More information about the openwrt-devel mailing list