Skip to content

Commit 8c03279

Browse files
Moved random_uuid helper into util.c
Only used in one place. Define there.
1 parent f6ed5bf commit 8c03279

3 files changed

Lines changed: 41 additions & 33 deletions

File tree

libnvme/src/nvme/util.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,47 @@ __public int libnvme_uuid_from_string(const char *str, unsigned char uuid[NVME_U
770770

771771
}
772772

773+
#if defined(_WIN32) || defined(_WIN64)
774+
775+
#include <bcrypt.h>
776+
777+
/* Windows-specific UUID generation using BCryptGenRandom */
778+
static inline int random_uuid(unsigned char *uuid, size_t len)
779+
{
780+
NTSTATUS status;
781+
782+
status = BCryptGenRandom(NULL, uuid, (ULONG)len,
783+
BCRYPT_USE_SYSTEM_PREFERRED_RNG);
784+
if (!BCRYPT_SUCCESS(status))
785+
return -EIO;
786+
787+
return 0;
788+
}
789+
790+
#else
791+
792+
/* Linux-specific UUID generation using /dev/urandom */
793+
static inline int random_uuid(unsigned char *uuid, size_t len)
794+
{
795+
int f, ret = 0;
796+
ssize_t n;
797+
798+
f = open("/dev/urandom", O_RDONLY);
799+
if (f < 0)
800+
return -errno;
801+
802+
n = read(f, uuid, len);
803+
if (n < 0)
804+
ret = -errno;
805+
else if ((size_t)n != len)
806+
ret = -EIO;
807+
808+
close(f);
809+
return ret;
810+
}
811+
812+
#endif
813+
773814
__public int libnvme_random_uuid(unsigned char uuid[NVME_UUID_LEN])
774815
{
775816
int ret;

libnvme/src/platform/linux.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,3 @@ typedef int libnvme_fd_t;
4242

4343
/* Platform initialization - no-op on Linux */
4444
static inline void libnvme_init(void) {}
45-
46-
/* Platform-specific UUID generation using /dev/urandom */
47-
static inline int random_uuid(unsigned char *uuid, size_t len)
48-
{
49-
int f, ret = 0;
50-
ssize_t n;
51-
52-
f = open("/dev/urandom", O_RDONLY);
53-
if (f < 0)
54-
return -errno;
55-
56-
n = read(f, uuid, len);
57-
if (n < 0)
58-
ret = -errno;
59-
else if ((size_t)n != len)
60-
ret = -EIO;
61-
62-
close(f);
63-
return ret;
64-
}

libnvme/src/platform/windows.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,6 @@ static inline void libnvme_init(void)
4444
_setmode(_fileno(stderr), O_BINARY);
4545
}
4646

47-
/* Platform-specific UUID generation using BCryptGenRandom */
48-
static inline int random_uuid(unsigned char *uuid, size_t len)
49-
{
50-
NTSTATUS status;
51-
52-
status = BCryptGenRandom(NULL, uuid, (ULONG)len,
53-
BCRYPT_USE_SYSTEM_PREFERRED_RNG);
54-
if (!BCRYPT_SUCCESS(status))
55-
return -EIO;
56-
57-
return 0;
58-
}
59-
6047

6148
/* time.h POSIX compatibility */
6249

0 commit comments

Comments
 (0)