Skip to content

Commit f94f092

Browse files
Removed public libnvme_fstat.
Only lib-windows.c and ocp-nvme.c used libnvme_fstat, and ocp-nvme.c was easily refactored to use the existing libnvme_transport_handle_is_blkdev check instead. - Refactored ocp-nvme.c to not need libnvme_fsat - Removed public libnvme_fstat - Made implementation in lib-windows.c private and renamed.
1 parent 55b3711 commit f94f092

5 files changed

Lines changed: 78 additions & 96 deletions

File tree

libnvme/src/libnvme.ld

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ LIBNVME_3 {
3838
libnvme_free_nbft;
3939
libnvme_free_ns;
4040
libnvme_free_subsystem;
41-
libnvme_fstat;
4241
libnvme_gen_dhchap_key;
4342
libnvme_generate_hostid;
4443
libnvme_generate_hostnqn;

libnvme/src/nvme/lib-windows.c

Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,80 @@
1111
#include "compiler-attributes.h"
1212

1313

14-
static int __nvme_transport_handle_open_direct(struct libnvme_transport_handle *hdl, const char *name)
14+
/* fstat implementation for device HANDLE */
15+
static int __libnvme_fd_fstat(libnvme_fd_t fd, struct stat *buf)
16+
{
17+
BY_HANDLE_FILE_INFORMATION file_info;
18+
ULARGE_INTEGER ull;
19+
DWORD file_type;
20+
21+
if (!buf) {
22+
errno = EINVAL;
23+
return -1;
24+
}
25+
26+
/* Check for invalid handle */
27+
if (fd == INVALID_HANDLE_VALUE || fd == NULL) {
28+
errno = EBADF;
29+
return -1;
30+
}
31+
32+
/*
33+
* GetFileInformationByHandle() does not work for all device HANDLEs
34+
* (e.g. raw \\\\.\\PhysicalDriveN). For those, fall back to file type.
35+
*/
36+
if (!GetFileInformationByHandle(fd, &file_info)) {
37+
file_type = GetFileType(fd);
38+
if (file_type == FILE_TYPE_DISK || file_type == FILE_TYPE_CHAR) {
39+
memset(buf, 0, sizeof(*buf));
40+
buf->st_mode = S_IFBLK | 0600;
41+
buf->st_nlink = 1;
42+
return 0;
43+
}
44+
45+
errno = EBADF;
46+
return -1;
47+
}
48+
49+
/* Fill in the stat structure */
50+
memset(buf, 0, sizeof(*buf));
51+
52+
/* Convert Windows file attributes to stat mode */
53+
if (file_info.dwFileAttributes & FILE_ATTRIBUTE_DEVICE)
54+
/* Windows device files should be marked as block devices */
55+
/* This is used by libnvme_verify_chr to check device type */
56+
buf->st_mode = S_IFBLK | 0600;
57+
else if (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
58+
buf->st_mode = S_IFDIR | 0755;
59+
else
60+
buf->st_mode = S_IFREG | 0644;
61+
62+
/* File size */
63+
buf->st_size = (((off_t)file_info.nFileSizeHigh << 32)
64+
| file_info.nFileSizeLow);
65+
66+
/* Number of hard links */
67+
buf->st_nlink = file_info.nNumberOfLinks;
68+
69+
/* Convert FILETIME to time_t for timestamps */
70+
/* Windows FILETIME is 100-nanosecond intervals since Jan 1, 1601 */
71+
/* Unix time_t is seconds since Jan 1, 1970 */
72+
ull.LowPart = file_info.ftLastWriteTime.dwLowDateTime;
73+
ull.HighPart = file_info.ftLastWriteTime.dwHighDateTime;
74+
buf->st_mtime = (time_t)((ull.QuadPart / 10000000ULL) - 11644473600ULL);
75+
76+
ull.LowPart = file_info.ftLastAccessTime.dwLowDateTime;
77+
ull.HighPart = file_info.ftLastAccessTime.dwHighDateTime;
78+
buf->st_atime = (time_t)((ull.QuadPart / 10000000ULL) - 11644473600ULL);
79+
80+
ull.LowPart = file_info.ftCreationTime.dwLowDateTime;
81+
ull.HighPart = file_info.ftCreationTime.dwHighDateTime;
82+
buf->st_ctime = (time_t)((ull.QuadPart / 10000000ULL) - 11644473600ULL);
83+
84+
return 0;
85+
}
86+
87+
static int __libnvme_transport_handle_open_direct(struct libnvme_transport_handle *hdl, const char *name)
1588
{
1689
char device_path[MAX_PATH];
1790
HANDLE h;
@@ -53,7 +126,7 @@ static int __nvme_transport_handle_open_direct(struct libnvme_transport_handle *
53126

54127
hdl->fd = h;
55128

56-
libnvme_fstat(hdl->fd, &hdl->stat);
129+
__libnvme_fd_fstat(hdl->fd, &hdl->stat);
57130

58131
/* Windows doesn't distinguish 32/64-bit ioctl, assume 64-bit capable */
59132
hdl->ioctl_admin64 = true;
@@ -96,7 +169,7 @@ __public int libnvme_open(struct libnvme_global_ctx *ctx, const char *name,
96169
libnvme_close(hdl);
97170
return -ENOTSUP;
98171
} else {
99-
ret = __nvme_transport_handle_open_direct(hdl, name);
172+
ret = __libnvme_transport_handle_open_direct(hdl, name);
100173
}
101174

102175
if (ret) {
@@ -132,76 +205,3 @@ __public void libnvme_close(struct libnvme_transport_handle *hdl)
132205
break;
133206
}
134207
}
135-
136-
/* Platform-specific fstat wrapper for libnvme_fd_t */
137-
__public int libnvme_fstat(libnvme_fd_t fd, struct stat *buf)
138-
{
139-
BY_HANDLE_FILE_INFORMATION file_info;
140-
ULARGE_INTEGER ull;
141-
DWORD file_type;
142-
143-
if (!buf) {
144-
errno = EINVAL;
145-
return -1;
146-
}
147-
148-
/* Check for invalid handle */
149-
if (fd == INVALID_HANDLE_VALUE || fd == NULL) {
150-
errno = EBADF;
151-
return -1;
152-
}
153-
154-
/*
155-
* GetFileInformationByHandle() does not work for all device HANDLEs
156-
* (e.g. raw \\\\.\\PhysicalDriveN). For those, fall back to file type.
157-
*/
158-
if (!GetFileInformationByHandle(fd, &file_info)) {
159-
file_type = GetFileType(fd);
160-
if (file_type == FILE_TYPE_DISK || file_type == FILE_TYPE_CHAR) {
161-
memset(buf, 0, sizeof(*buf));
162-
buf->st_mode = S_IFBLK | 0600;
163-
buf->st_nlink = 1;
164-
return 0;
165-
}
166-
167-
errno = EBADF;
168-
return -1;
169-
}
170-
171-
/* Fill in the stat structure */
172-
memset(buf, 0, sizeof(*buf));
173-
174-
/* Convert Windows file attributes to stat mode */
175-
if (file_info.dwFileAttributes & FILE_ATTRIBUTE_DEVICE)
176-
/* Windows device files should be marked as block devices */
177-
/* This is used by libnvme_verify_chr to check device type */
178-
buf->st_mode = S_IFBLK | 0600;
179-
else if (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
180-
buf->st_mode = S_IFDIR | 0755;
181-
else
182-
buf->st_mode = S_IFREG | 0644;
183-
184-
/* File size */
185-
buf->st_size = (((off_t)file_info.nFileSizeHigh << 32)
186-
| file_info.nFileSizeLow);
187-
188-
/* Number of hard links */
189-
buf->st_nlink = file_info.nNumberOfLinks;
190-
191-
/* Convert FILETIME to time_t for timestamps */
192-
/* Windows FILETIME is 100-nanosecond intervals since Jan 1, 1601 */
193-
/* Unix time_t is seconds since Jan 1, 1970 */
194-
ull.LowPart = file_info.ftLastWriteTime.dwLowDateTime;
195-
ull.HighPart = file_info.ftLastWriteTime.dwHighDateTime;
196-
buf->st_mtime = (time_t)((ull.QuadPart / 10000000ULL) - 11644473600ULL);
197-
198-
ull.LowPart = file_info.ftLastAccessTime.dwLowDateTime;
199-
ull.HighPart = file_info.ftLastAccessTime.dwHighDateTime;
200-
buf->st_atime = (time_t)((ull.QuadPart / 10000000ULL) - 11644473600ULL);
201-
202-
ull.LowPart = file_info.ftCreationTime.dwLowDateTime;
203-
ull.HighPart = file_info.ftCreationTime.dwHighDateTime;
204-
buf->st_ctime = (time_t)((ull.QuadPart / 10000000ULL) - 11644473600ULL);
205-
206-
return 0;
207-
}

libnvme/src/platform/linux.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ typedef int libnvme_fd_t;
4040
#define O_BINARY 0
4141
#endif
4242

43-
/* Platform-specific fstat wrapper for libnvme_fd_t */
44-
static inline int libnvme_fstat(libnvme_fd_t fd, struct stat *buf)
45-
{
46-
return fstat(fd, buf);
47-
}
48-
4943
/* Platform initialization - no-op on Linux */
5044
static inline void libnvme_init(void) {}
5145

libnvme/src/platform/windows.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,6 @@ static inline int sigaction(int signum, const struct sigaction *act,
220220
/* _mkdir is defined in <direct.h> */
221221
#define mkdir(path, mode) _mkdir(path)
222222

223-
/* Platform-specific fstat wrapper for libnvme_fd_t */
224-
int libnvme_fstat(libnvme_fd_t fd, struct stat *buf);
225-
226223

227224
/* mman.h memory mapping stubs - not supported on Windows */
228225

plugins/ocp/ocp-nvme.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,7 @@ int ocp_set_latency_monitor_feature(int argc, char **argv, struct command *acmd,
349349
if (err)
350350
return err;
351351

352-
err = libnvme_fstat(libnvme_transport_handle_get_fd(hdl), &nvme_stat);
353-
if (err < 0)
354-
return err;
355-
356-
if (S_ISBLK(nvme_stat.st_mode)) {
352+
if (libnvme_transport_handle_is_blkdev(hdl)) {
357353
err = libnvme_get_nsid(hdl, &nsid);
358354
if (err < 0) {
359355
perror("invalid-namespace-id");
@@ -1463,11 +1459,7 @@ static int ocp_telemetry_log(int argc, char **argv, struct command *acmd, struct
14631459
if (opt.telemetry_type == 0)
14641460
opt.telemetry_type = "host";
14651461

1466-
err = libnvme_fstat(libnvme_transport_handle_get_fd(hdl), &nvme_stat);
1467-
if (err < 0)
1468-
return err;
1469-
1470-
if (S_ISBLK(nvme_stat.st_mode)) {
1462+
if (libnvme_transport_handle_is_blkdev(hdl)) {
14711463
err = libnvme_get_nsid(hdl, &nsid);
14721464
if (err < 0)
14731465
return err;

0 commit comments

Comments
 (0)