From 1450acbb28acd83f8b46171d881c0d96f807140c Mon Sep 17 00:00:00 2001 From: Denis 'GNUtoo' Carikli Date: Wed, 17 Feb 2021 00:18:41 +0100 Subject: partitions: android: Add open_android_modem_partition_by_name Signed-off-by: Denis 'GNUtoo' Carikli --- samsung-ipc/partitions/android/android.c | 63 ++++++++++++++++++++++++++++++++ samsung-ipc/partitions/android/android.h | 2 + 2 files changed, 65 insertions(+) (limited to 'samsung-ipc') 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 #include #include +#include +#include #include #include #include +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__ */ -- cgit v1.2.3