storage/nfs: optimize OpenFile()
This commit is contained in:
@@ -10,6 +10,8 @@
|
||||
|
||||
#include <nfsc/libnfs.h> // for struct nfs_stat_64
|
||||
|
||||
#include <fmt/core.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
@@ -18,16 +20,37 @@
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h> // for S_ISREG()
|
||||
|
||||
using std::string_view_literals::operator""sv;
|
||||
|
||||
NfsFileReader::NfsFileReader() noexcept
|
||||
:defer_open(nfs_get_event_loop(), BIND_THIS_METHOD(OnDeferredOpen))
|
||||
{
|
||||
}
|
||||
|
||||
NfsFileReader::NfsFileReader(NfsConnection &_connection,
|
||||
std::string_view _path) noexcept
|
||||
:state(State::DEFER),
|
||||
path(_path),
|
||||
connection(&_connection),
|
||||
defer_open(_connection.GetEventLoop(), BIND_THIS_METHOD(OnDeferredOpen))
|
||||
{
|
||||
defer_open.Schedule();
|
||||
}
|
||||
|
||||
NfsFileReader::~NfsFileReader() noexcept
|
||||
{
|
||||
assert(state == State::INITIAL);
|
||||
}
|
||||
|
||||
std::string
|
||||
NfsFileReader::GetAbsoluteUri() const noexcept
|
||||
{
|
||||
return fmt::format("nfs://{}{}/{}"sv,
|
||||
connection != nullptr ? connection->GetServer() : server,
|
||||
connection != nullptr ? connection->GetExportName() : export_name,
|
||||
path);
|
||||
}
|
||||
|
||||
void
|
||||
NfsFileReader::Close() noexcept
|
||||
{
|
||||
@@ -293,12 +316,13 @@ NfsFileReader::OnDeferredOpen() noexcept
|
||||
{
|
||||
assert(state == State::DEFER);
|
||||
|
||||
try {
|
||||
connection = &nfs_get_connection(server, export_name);
|
||||
} catch (...) {
|
||||
OnNfsFileError(std::current_exception());
|
||||
return;
|
||||
}
|
||||
if (connection == nullptr)
|
||||
try {
|
||||
connection = &nfs_get_connection(server, export_name);
|
||||
} catch (...) {
|
||||
OnNfsFileError(std::current_exception());
|
||||
return;
|
||||
}
|
||||
|
||||
connection->AddLease(*this);
|
||||
state = State::MOUNT;
|
||||
|
||||
@@ -45,7 +45,7 @@ class NfsFileReader : NfsLease, NfsCallback {
|
||||
|
||||
std::string server, export_name, path;
|
||||
|
||||
NfsConnection *connection;
|
||||
NfsConnection *connection = nullptr;
|
||||
|
||||
nfsfh *fh;
|
||||
|
||||
@@ -60,12 +60,17 @@ class NfsFileReader : NfsLease, NfsCallback {
|
||||
|
||||
public:
|
||||
NfsFileReader() noexcept;
|
||||
explicit NfsFileReader(NfsConnection &_connection,
|
||||
std::string_view _path) noexcept;
|
||||
~NfsFileReader() noexcept;
|
||||
|
||||
auto &GetEventLoop() const noexcept {
|
||||
return defer_open.GetEventLoop();
|
||||
}
|
||||
|
||||
[[nodiscard]] [[gnu::pure]]
|
||||
std::string GetAbsoluteUri() const noexcept;
|
||||
|
||||
void Close() noexcept;
|
||||
void DeferClose() noexcept;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user