Skip to content

Commit 913e1ac

Browse files
committed
Minimal backport to support libnfs 6 on v0.23
This is based on the following commits in master: - 31e583e - 58e3b83 also changed cpp_std=c++20 to facilitate the use of span
1 parent b080ca8 commit 913e1ac

File tree

6 files changed

+72
-8
lines changed

6 files changed

+72
-8
lines changed

meson.build

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ project(
66
default_options: [
77
'c_std=c11',
88
'build.c_std=c11',
9-
'cpp_std=c++17',
10-
'build.cpp_std=c++17',
9+
'cpp_std=c++20',
10+
'build.cpp_std=c++20',
1111
'warning_level=3',
1212

1313
# If we build those libraries as Meson subproject, they shall be

src/lib/nfs/Connection.cxx

+29-4
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,25 @@ NfsConnection::CancellableCallback::Stat(nfs_context *ctx,
103103

104104
inline void
105105
NfsConnection::CancellableCallback::Read(nfs_context *ctx, struct nfsfh *fh,
106-
uint64_t offset, size_t size)
106+
uint64_t offset,
107+
#ifdef LIBNFS_API_2
108+
std::span<std::byte> dest
109+
#else
110+
std::size_t size
111+
#endif
112+
)
107113
{
108114
assert(connection.GetEventLoop().IsInside());
109115

110-
int result = nfs_pread_async(ctx, fh, offset, size, Callback, this);
116+
int result = nfs_pread_async(ctx, fh,
117+
#ifdef LIBNFS_API_2
118+
dest.data(), dest.size(),
119+
#endif
120+
offset,
121+
#ifndef LIBNFS_API_2
122+
size,
123+
#endif
124+
Callback, this);
111125
if (result < 0)
112126
throw FormatRuntimeError("nfs_pread_async() failed: %s",
113127
nfs_get_error(ctx));
@@ -329,15 +343,26 @@ NfsConnection::Stat(struct nfsfh *fh, NfsCallback &callback)
329343
}
330344

331345
void
332-
NfsConnection::Read(struct nfsfh *fh, uint64_t offset, size_t size,
346+
NfsConnection::Read(struct nfsfh *fh, uint64_t offset,
347+
#ifdef LIBNFS_API_2
348+
std::span<std::byte> dest,
349+
#else
350+
std::size_t size,
351+
#endif
333352
NfsCallback &callback)
334353
{
335354
assert(GetEventLoop().IsInside());
336355
assert(!callbacks.Contains(callback));
337356

338357
auto &c = callbacks.Add(callback, *this, false);
339358
try {
340-
c.Read(context, fh, offset, size);
359+
c.Read(context, fh, offset,
360+
#ifdef LIBNFS_API_2
361+
dest
362+
#else
363+
size
364+
#endif
365+
);
341366
} catch (...) {
342367
callbacks.Remove(c);
343368
throw;

src/lib/nfs/Connection.hxx

+13-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ class NfsConnection {
7171
void Open(nfs_context *context, const char *path, int flags);
7272
void Stat(nfs_context *context, struct nfsfh *fh);
7373
void Read(nfs_context *context, struct nfsfh *fh,
74-
uint64_t offset, size_t size);
74+
uint64_t offset,
75+
#ifdef LIBNFS_API_2
76+
std::span<std::byte> dest
77+
#else
78+
std::size_t size
79+
#endif
80+
);
7581

7682
/**
7783
* Cancel the operation and schedule a call to
@@ -193,7 +199,12 @@ public:
193199
/**
194200
* Throws std::runtime_error on error.
195201
*/
196-
void Read(struct nfsfh *fh, uint64_t offset, size_t size,
202+
void Read(struct nfsfh *fh, uint64_t offset,
203+
#ifdef LIBNFS_API_2
204+
std::span<std::byte> dest,
205+
#else
206+
std::size_t size,
207+
#endif
197208
NfsCallback &callback);
198209

199210
void Cancel(NfsCallback &callback) noexcept;

src/lib/nfs/FileReader.cxx

+13
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,28 @@ NfsFileReader::Read(uint64_t offset, size_t size)
129129
{
130130
assert(state == State::IDLE);
131131

132+
#ifdef LIBNFS_API_2
133+
assert(!read_buffer);
134+
// TOOD read into caller-provided buffer
135+
read_buffer = std::make_unique<std::byte[]>(size);
136+
connection->Read(fh, offset, {read_buffer.get(), size}, *this);
137+
#else
132138
connection->Read(fh, offset, size, *this);
139+
#endif
140+
133141
state = State::READ;
134142
}
135143

136144
void
137145
NfsFileReader::CancelRead() noexcept
138146
{
139147
if (state == State::READ) {
148+
#ifdef LIBNFS_API_2
149+
assert(read_buffer);
150+
read_buffer.release();
151+
#endif
140152
connection->Cancel(*this);
153+
141154
state = State::IDLE;
142155
}
143156
}

src/lib/nfs/FileReader.hxx

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232

3333
#include <sys/stat.h>
3434

35+
#ifdef LIBNFS_API_2
36+
#include <memory>
37+
#endif
38+
3539
struct nfsfh;
3640
class NfsConnection;
3741

@@ -68,6 +72,10 @@ class NfsFileReader : NfsLease, NfsCallback {
6872
*/
6973
InjectEvent defer_open;
7074

75+
#ifdef LIBNFS_API_2
76+
std::unique_ptr<std::byte[]> read_buffer;
77+
#endif
78+
7179
public:
7280
NfsFileReader() noexcept;
7381
~NfsFileReader() noexcept;

src/lib/nfs/meson.build

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ if not nfs_dep.found()
44
subdir_done()
55
endif
66

7+
if nfs_dep.version().version_compare('>=6')
8+
# libnfs has no version macro therefore we must detect the API
9+
# version 2 at configure time
10+
nfs_dep = declare_dependency(compile_args: '-DLIBNFS_API_2',
11+
dependencies: nfs_dep)
12+
endif
13+
714
nfs = static_library(
815
'nfs',
916
'Connection.cxx',

0 commit comments

Comments
 (0)