Skip to content

Commit 346c568

Browse files
compat (fd): Adds cross-platform-compatible file descriptor type
Creates and uses new libnvme_fd_t to handle differences in file descriptor types across platforms. Also defines platform-specific values for invalid file descriptors and test file descriptors. These values are defined in a new lib-compat.h that can be used to host other library-specific cross-platform utilities in the future. Signed-off-by: Broc Going <[email protected]> Signed-off-by: Brandon Capener <[email protected]>
1 parent d801e1e commit 346c568

15 files changed

Lines changed: 51 additions & 27 deletions

File tree

libnvme/src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ headers = [
2929
'nvme/endian.h',
3030
'nvme/filters.h',
3131
'nvme/ioctl.h',
32+
'nvme/lib-compat.h',
3233
'nvme/lib-types.h',
3334
'nvme/lib.h',
3435
'nvme/linux.h',

libnvme/src/nvme/ioctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ __public int libnvme_update_block_size(struct libnvme_transport_handle *hdl,
9999
int block_size)
100100
{
101101
int ret;
102-
int fd = libnvme_transport_handle_get_fd(hdl);
102+
libnvme_fd_t fd = libnvme_transport_handle_get_fd(hdl);
103103

104104
ret = ioctl(fd, BLKBSZSET, &block_size);
105105
if (ret < 0)

libnvme/src/nvme/lib-compat.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2+
/*
3+
* This file is part of libnvme.
4+
* Copyright (c) 2026 Micron Technology, Inc.
5+
*
6+
* Cross-platform compatibility definitions, types, and utilities.
7+
*
8+
* Authors: Brandon Capener <[email protected]>
9+
*/
10+
#pragma once
11+
12+
#if defined(_WIN32)
13+
14+
#define WIN32_LEAN_AND_MEAN /* keeps windows.h from including winsock */
15+
#include <windows.h>
16+
17+
typedef HANDLE libnvme_fd_t;
18+
#define LIBNVME_INVALID_FD INVALID_HANDLE_VALUE
19+
#define LIBNVME_TEST_FD ((HANDLE)0xFD)
20+
21+
#else
22+
23+
typedef int libnvme_fd_t;
24+
#define LIBNVME_INVALID_FD -1
25+
#define LIBNVME_TEST_FD 0xFD
26+
27+
#endif

libnvme/src/nvme/lib.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ __public int libnvme_open(struct libnvme_global_ctx *ctx, const char *name,
243243

244244
if (!strncmp(name, "NVME_TEST_FD", 12)) {
245245
hdl->type = LIBNVME_TRANSPORT_HANDLE_TYPE_DIRECT;
246-
hdl->fd = 0xFD;
246+
hdl->fd = LIBNVME_TEST_FD;
247247

248248
if (!strcmp(name, "NVME_TEST_FD64"))
249249
hdl->ioctl_admin64 = true;
@@ -287,7 +287,7 @@ __public void libnvme_close(struct libnvme_transport_handle *hdl)
287287
}
288288
}
289289

290-
__public int libnvme_transport_handle_get_fd(struct libnvme_transport_handle *hdl)
290+
__public libnvme_fd_t libnvme_transport_handle_get_fd(struct libnvme_transport_handle *hdl)
291291
{
292292
return hdl->fd;
293293
}

libnvme/src/nvme/lib.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <stdbool.h>
1212
#include <stdio.h>
1313

14+
#include <nvme/lib-compat.h>
1415
#include <nvme/lib-types.h>
1516

1617
enum libnvme_log_level {
@@ -94,9 +95,10 @@ void libnvme_close(struct libnvme_transport_handle *hdl);
9495
* If the device handle is for a ioctl based device,
9596
* libnvme_transport_handle_get_fd will return a valid file descriptor.
9697
*
97-
* Return: File descriptor for an IOCTL based transport handle, otherwise -1.
98+
* Return: File descriptor for an IOCTL based transport handle,
99+
* otherwise LIBNVME_INVALID_FD.
98100
*/
99-
int libnvme_transport_handle_get_fd(struct libnvme_transport_handle *hdl);
101+
libnvme_fd_t libnvme_transport_handle_get_fd(struct libnvme_transport_handle *hdl);
100102

101103
/**
102104
* libnvme_transport_handle_get_name - Return name of the device

libnvme/src/nvme/private.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <ccan/list/list.h>
1414

1515
#include "nvme/nvme-types.h"
16+
#include "nvme/lib-compat.h"
1617
#include "nvme/lib-types.h"
1718

1819
#include <nvme/tree.h>
@@ -156,7 +157,7 @@ struct libnvme_transport_handle {
156157
__u32 timeout;
157158

158159
/* direct */
159-
int fd;
160+
libnvme_fd_t fd;
160161
struct stat stat;
161162
bool ioctl_admin64;
162163
bool ioctl_io64;

libnvme/test/ioctl/ana.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "mock.h"
1414
#include "util.h"
1515

16-
#define TEST_FD 0xFD
1716
#define PDU_SIZE NVME_LOG_PAGE_PDU_SIZE
1817

1918
static struct libnvme_transport_handle *test_hdl;
@@ -631,7 +630,7 @@ int main(void)
631630
struct libnvme_global_ctx *ctx =
632631
libnvme_create_global_ctx(stdout, LIBNVME_DEFAULT_LOGLEVEL);
633632

634-
set_mock_fd(TEST_FD);
633+
set_mock_fd(LIBNVME_TEST_FD);
635634
check(!libnvme_open(ctx, "NVME_TEST_FD", &test_hdl),
636635
"opening test link failed");
637636

libnvme/test/ioctl/discovery.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "mock.h"
1414
#include "util.h"
1515

16-
#define TEST_FD 0xFD
1716
#define HEADER_LEN 20
1817

1918
static struct libnvme_transport_handle *test_hdl;
@@ -441,7 +440,7 @@ int main(void)
441440
struct libnvme_global_ctx *ctx =
442441
libnvme_create_global_ctx(stdout, LIBNVME_DEFAULT_LOGLEVEL);
443442

444-
set_mock_fd(TEST_FD);
443+
set_mock_fd(LIBNVME_TEST_FD);
445444
check(!libnvme_open(ctx, "NVME_TEST_FD", &test_hdl),
446445
"opening test link failed");
447446

libnvme/test/ioctl/features.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "mock.h"
99
#include "util.h"
1010

11-
#define TEST_FD 0xFD
1211
#define TEST_TIMEOUT 1234
1312
#define TEST_NSID 0x89ABCDEF
1413
#define TEST_CDW11 0x11111111
@@ -1625,7 +1624,7 @@ int main(void)
16251624
struct libnvme_global_ctx *ctx =
16261625
libnvme_create_global_ctx(stdout, LIBNVME_DEFAULT_LOGLEVEL);
16271626

1628-
set_mock_fd(TEST_FD);
1627+
set_mock_fd(LIBNVME_TEST_FD);
16291628
check(!libnvme_open(ctx, "NVME_TEST_FD64", &test_hdl),
16301629
"opening test link failed");
16311630

libnvme/test/ioctl/identify.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "mock.h"
99
#include "util.h"
1010

11-
#define TEST_FD 0xFD
1211
#define TEST_NSID 0x12345678
1312
#define TEST_NVMSETID 0xABCD
1413
#define TEST_UUID 123
@@ -647,7 +646,7 @@ int main(void)
647646
struct libnvme_global_ctx * ctx =
648647
libnvme_create_global_ctx(stdout, LIBNVME_DEFAULT_LOGLEVEL);
649648

650-
set_mock_fd(TEST_FD);
649+
set_mock_fd(LIBNVME_TEST_FD);
651650
check(!libnvme_open(ctx, "NVME_TEST_FD", &test_hdl),
652651
"opening test link failed");
653652

0 commit comments

Comments
 (0)