diff mbox series

[03/29] Add utility function to detect filesystem

Message ID 20211011112156.44192-4-sbabic@denx.de
State Changes Requested
Headers show
Series DELTA Update | expand

Commit Message

Stefano Babic Oct. 11, 2021, 11:21 a.m. UTC
This can be used to detect a filesystem from a device instead of specify
the expected filesystem.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 fs/diskformat.c        | 28 ++++++++++++++++++++--------
 include/fs_interface.h |  1 +
 2 files changed, 21 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/fs/diskformat.c b/fs/diskformat.c
index 0841f0b..8d58fc3 100644
--- a/fs/diskformat.c
+++ b/fs/diskformat.c
@@ -39,36 +39,48 @@  static struct supported_filesystems fs[] = {
  * Checks if file system fstype already exists on device.
  * return 0 if not exists, 1 if exists, negative values on failure
  */
-int diskformat_fs_exists(char *device, char *fstype)
+
+char *diskformat_fs_detect(char *device)
 {
-	char buf[10];
-	const char *value = buf;
+	const char *value;
+	char *s = NULL;
 	size_t len;
 	blkid_probe pr;
-	int ret = 0;
 
 	pr = blkid_new_probe_from_filename(device);
 
 	if (!pr) {
 		ERROR("%s: failed to create libblkid probe",
 			  device);
-		return -EFAULT;
+		return NULL;
 	}
 
 	while (blkid_do_probe(pr) == 0) {
 		if (blkid_probe_lookup_value(pr, "TYPE", &value, &len)) {
 			ERROR("blkid_probe_lookup_value failed");
-			ret = -EFAULT;
 			break;
 		}
 
-		if (!strncmp(value, fstype, sizeof(buf))) {
-			ret = 1;
+		if (len > 0) {
+			s = strndup(value, len);
 			break;
 		}
 	}
 	blkid_free_probe(pr);
 
+	return s;
+}
+
+int diskformat_fs_exists(char *device, char *fstype)
+{
+	int ret = 0;
+	char *filesystem = diskformat_fs_detect(device);
+
+	if (filesystem) {
+		ret = !strcmp(fstype, filesystem);
+	}
+
+	free(filesystem);
 	return ret;
 }
 
diff --git a/include/fs_interface.h b/include/fs_interface.h
index 25c22e5..581f02a 100644
--- a/include/fs_interface.h
+++ b/include/fs_interface.h
@@ -7,6 +7,7 @@ 
 #ifndef _FS_INTERFACE_H
 #define _FS_INTERFACE_H
 
+char *diskformat_fs_detect(char *device);
 int diskformat_fs_exists(char *device, char *fstype);
 
 int diskformat_mkfs(char *device, char *fstype);