Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal backport to support libnfs 6 on v0.23 #2198

Open
wants to merge 2 commits into
base: v0.23.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ project(
default_options: [
'c_std=c11',
'build.c_std=c11',
'cpp_std=c++17',
'build.cpp_std=c++17',
'cpp_std=c++20',
'build.cpp_std=c++20',
'warning_level=3',

# If we build those libraries as Meson subproject, they shall be
Expand Down
33 changes: 29 additions & 4 deletions src/lib/nfs/Connection.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,25 @@ NfsConnection::CancellableCallback::Stat(nfs_context *ctx,

inline void
NfsConnection::CancellableCallback::Read(nfs_context *ctx, struct nfsfh *fh,
uint64_t offset, size_t size)
uint64_t offset,
#ifdef LIBNFS_API_2
std::span<std::byte> dest
#else
std::size_t size
#endif
)
{
assert(connection.GetEventLoop().IsInside());

int result = nfs_pread_async(ctx, fh, offset, size, Callback, this);
int result = nfs_pread_async(ctx, fh,
#ifdef LIBNFS_API_2
dest.data(), dest.size(),
#endif
offset,
#ifndef LIBNFS_API_2
size,
#endif
Callback, this);
if (result < 0)
throw FormatRuntimeError("nfs_pread_async() failed: %s",
nfs_get_error(ctx));
Expand Down Expand Up @@ -329,15 +343,26 @@ NfsConnection::Stat(struct nfsfh *fh, NfsCallback &callback)
}

void
NfsConnection::Read(struct nfsfh *fh, uint64_t offset, size_t size,
NfsConnection::Read(struct nfsfh *fh, uint64_t offset,
#ifdef LIBNFS_API_2
std::span<std::byte> dest,
#else
std::size_t size,
#endif
NfsCallback &callback)
{
assert(GetEventLoop().IsInside());
assert(!callbacks.Contains(callback));

auto &c = callbacks.Add(callback, *this, false);
try {
c.Read(context, fh, offset, size);
c.Read(context, fh, offset,
#ifdef LIBNFS_API_2
dest
#else
size
#endif
);
} catch (...) {
callbacks.Remove(c);
throw;
Expand Down
15 changes: 13 additions & 2 deletions src/lib/nfs/Connection.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ class NfsConnection {
void Open(nfs_context *context, const char *path, int flags);
void Stat(nfs_context *context, struct nfsfh *fh);
void Read(nfs_context *context, struct nfsfh *fh,
uint64_t offset, size_t size);
uint64_t offset,
#ifdef LIBNFS_API_2
std::span<std::byte> dest
#else
std::size_t size
#endif
);

/**
* Cancel the operation and schedule a call to
Expand Down Expand Up @@ -193,7 +199,12 @@ public:
/**
* Throws std::runtime_error on error.
*/
void Read(struct nfsfh *fh, uint64_t offset, size_t size,
void Read(struct nfsfh *fh, uint64_t offset,
#ifdef LIBNFS_API_2
std::span<std::byte> dest,
#else
std::size_t size,
#endif
NfsCallback &callback);

void Cancel(NfsCallback &callback) noexcept;
Expand Down
13 changes: 13 additions & 0 deletions src/lib/nfs/FileReader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,28 @@ NfsFileReader::Read(uint64_t offset, size_t size)
{
assert(state == State::IDLE);

#ifdef LIBNFS_API_2
assert(!read_buffer);
// TOOD read into caller-provided buffer
read_buffer = std::make_unique<std::byte[]>(size);
connection->Read(fh, offset, {read_buffer.get(), size}, *this);
#else
connection->Read(fh, offset, size, *this);
#endif

state = State::READ;
}

void
NfsFileReader::CancelRead() noexcept
{
if (state == State::READ) {
#ifdef LIBNFS_API_2
assert(read_buffer);
read_buffer.release();
#endif
connection->Cancel(*this);

state = State::IDLE;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/lib/nfs/FileReader.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@

#include <sys/stat.h>

#ifdef LIBNFS_API_2
#include <memory>
#endif

struct nfsfh;
class NfsConnection;

Expand Down Expand Up @@ -68,6 +72,10 @@ class NfsFileReader : NfsLease, NfsCallback {
*/
InjectEvent defer_open;

#ifdef LIBNFS_API_2
std::unique_ptr<std::byte[]> read_buffer;
#endif

public:
NfsFileReader() noexcept;
~NfsFileReader() noexcept;
Expand Down
9 changes: 8 additions & 1 deletion src/lib/nfs/meson.build
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
nfs_dep = dependency('libnfs', version: ['>= 4', '< 6'], required: get_option('nfs'))
nfs_dep = dependency('libnfs', version: '>= 4', required: get_option('nfs'))
conf.set('ENABLE_NFS', nfs_dep.found())
if not nfs_dep.found()
subdir_done()
endif

if nfs_dep.version().version_compare('>=6')
# libnfs has no version macro therefore we must detect the API
# version 2 at configure time
nfs_dep = declare_dependency(compile_args: '-DLIBNFS_API_2',
dependencies: nfs_dep)
endif

nfs = static_library(
'nfs',
'Connection.cxx',
Expand Down
Loading