[Replicant] [libsamsung-ipc] [PATCH 7/7] partitions: android: Add open_android_modem_partition_by_name

Denis 'GNUtoo' Carikli GNUtoo at cyberdimension.org
Thu Mar 4 12:42:27 UTC 2021


Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo at cyberdimension.org>
---
 samsung-ipc/partitions/android/android.c | 63 ++++++++++++++++++++++++
 samsung-ipc/partitions/android/android.h |  2 +
 2 files changed, 65 insertions(+)

diff --git a/samsung-ipc/partitions/android/android.c b/samsung-ipc/partitions/android/android.c
index fb922c5..9ec6ed4 100644
--- a/samsung-ipc/partitions/android/android.c
+++ b/samsung-ipc/partitions/android/android.c
@@ -23,12 +23,20 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
 
 #include <sys/stat.h>
 #include <sys/types.h>
 
 #include <samsung-ipc.h>
 
+static char const * const partitions_dirnames[] = {
+	"/dev/disk/by-partlabel/", /* GNU/Linux */
+	"/dev/block/by-name/",     /* Android */
+	NULL
+};
+
 int open_android_modem_partition(struct ipc_client *client,
 				 char const * const *path_names)
 {
@@ -54,3 +62,58 @@ int open_android_modem_partition(struct ipc_client *client,
 	errno = ENOENT;
 	return -1;
 }
+
+int open_android_modem_partition_by_name(struct ipc_client *client,
+					 const char *name, char **out_path)
+{
+	int i;
+	int rc;
+
+	for (i = 0; partitions_dirnames[i] != NULL; i++) {
+		char *path = NULL;
+		int fd;
+		size_t len;
+
+		len = strlen(partitions_dirnames[i]) + strlen(name) + 1;
+		path = calloc(1, len);
+		if (path == NULL) {
+			rc = errno;
+			ipc_client_log(client,
+				       "%s: calloc failed with error %d: %s",
+				       __func__, rc, strerror(rc));
+			return -errno;
+		}
+
+		strncpy(path, partitions_dirnames[i],
+			strlen(partitions_dirnames[i]));
+		strcat(path, name);
+
+		ipc_client_log(client, "%s: Trying to open %s",
+			       __func__, path);
+
+		fd = open(path, O_RDONLY);
+		if (fd == -1) {
+			rc = -errno;
+			if (out_path)
+				*out_path = path;
+			else
+				free(path);
+
+			if (rc == -ENOENT)
+				continue;
+
+			errno = -rc;
+			return -1;
+		}
+
+		if (out_path)
+			*out_path = path;
+		else
+			free(path);
+
+		return fd;
+	}
+
+	errno = ENOENT;
+	return -1;
+}
diff --git a/samsung-ipc/partitions/android/android.h b/samsung-ipc/partitions/android/android.h
index 4d7700c..e2f23f7 100644
--- a/samsung-ipc/partitions/android/android.h
+++ b/samsung-ipc/partitions/android/android.h
@@ -24,4 +24,6 @@
 #define __ANDROID_PARTITIONS_H__
 int open_android_modem_partition(struct ipc_client *client,
 				 char const * const *path_names);
+int open_android_modem_partition_by_name(struct ipc_client *client,
+					 const char *name, char **out_path);
 #endif /* __ANDROID_PARTITIONS_H__ */
-- 
2.30.1



More information about the Replicant mailing list