From 9f10eff3905a99fcfb2795ace13ff26f46235132 Mon Sep 17 00:00:00 2001 From: Leonardo da Cunha Date: Fri, 15 Dec 2023 07:53:28 -0800 Subject: [PATCH] util: Added function to find specific UUID in UUID list. Finds a given UUID in the UUID list returned by identify UUID. Signed-off-by: Leonardo da Cunha --- src/libnvme.map | 5 +++++ src/nvme/util.c | 19 +++++++++++++++++++ src/nvme/util.h | 9 +++++++++ 3 files changed, 33 insertions(+) diff --git a/src/libnvme.map b/src/libnvme.map index 742f6356..de36ba14 100644 --- a/src/libnvme.map +++ b/src/libnvme.map @@ -1,4 +1,9 @@ # SPDX-License-Identifier: LGPL-2.1-or-later +LIBNVME_1_8 { + global: + nvme_uuid_find; +}; + LIBNVME_1_7 { global: nvme_init_copy_range_f2; diff --git a/src/nvme/util.c b/src/nvme/util.c index 45512ff9..78ce14f8 100644 --- a/src/nvme/util.c +++ b/src/nvme/util.c @@ -951,6 +951,25 @@ int nvme_uuid_random(unsigned char uuid[NVME_UUID_LEN]) return 0; } +int nvme_uuid_find(struct nvme_id_uuid_list *uuid_list, const unsigned char uuid[NVME_UUID_LEN]) +{ + const unsigned char uuid_end[NVME_UUID_LEN] = {0}; + + if ((!uuid_list) || (!uuid)) { + errno = EINVAL; + return -1; + } + + for (int i = 0; i < NVME_ID_UUID_LIST_MAX; i++) { + if (memcmp(uuid, &uuid_list->entry[i].uuid, NVME_UUID_LEN) == 0) + return i + 1; + if (memcmp(uuid_end, &uuid_list->entry[i].uuid, NVME_UUID_LEN) == 0) + break; + } + errno = ENOENT; + return -1; +} + #ifdef HAVE_NETDB static bool _nvme_ipaddrs_eq(struct sockaddr *addr1, struct sockaddr *addr2) { diff --git a/src/nvme/util.h b/src/nvme/util.h index 16d5b9ca..517c696c 100644 --- a/src/nvme/util.h +++ b/src/nvme/util.h @@ -666,6 +666,15 @@ int nvme_uuid_from_string(const char *str, unsigned char uuid[NVME_UUID_LEN]); */ int nvme_uuid_random(unsigned char uuid[NVME_UUID_LEN]); +/** + * nvme_uuid_find - Find UUID position on UUID list + * @uuid_list: UUID list returned by identify UUID + * @uuid: Binary encoded input UUID + * + * Return: The array position where given UUID is present, or -1 on failure with errno set. + */ +int nvme_uuid_find(struct nvme_id_uuid_list *uuid_list, const unsigned char uuid[NVME_UUID_LEN]); + /** * nvme_ipaddrs_eq - Check if 2 IP addresses are equal. * @addr1: IP address (can be IPv4 or IPv6)