storage/smbclient: store SmbclientStorage reference

This commit is contained in:
Max Kellermann 2020-07-20 22:36:06 +02:00
parent 2a15fafbd7
commit f5a85a816c

View File

@ -33,18 +33,20 @@
#include <libsmbclient.h> #include <libsmbclient.h>
class SmbclientStorage;
class SmbclientDirectoryReader final : public StorageDirectoryReader { class SmbclientDirectoryReader final : public StorageDirectoryReader {
SmbclientContext &ctx; SmbclientStorage &storage;
const std::string base; const std::string base;
SMBCFILE *const handle; SMBCFILE *const handle;
const char *name; const char *name;
public: public:
SmbclientDirectoryReader(SmbclientContext &_ctx, SmbclientDirectoryReader(SmbclientStorage &_storage,
std::string &&_base, std::string &&_base,
SMBCFILE *_handle) noexcept SMBCFILE *_handle) noexcept
:ctx(_ctx), base(std::move(_base)), handle(_handle) {} :storage(_storage), base(std::move(_base)), handle(_handle) {}
~SmbclientDirectoryReader() override; ~SmbclientDirectoryReader() override;
@ -54,6 +56,8 @@ public:
}; };
class SmbclientStorage final : public Storage { class SmbclientStorage final : public Storage {
friend class SmbclientDirectoryReader;
const std::string base; const std::string base;
SmbclientContext ctx = SmbclientContext::New(); SmbclientContext ctx = SmbclientContext::New();
@ -134,7 +138,7 @@ SmbclientStorage::OpenDirectory(std::string_view uri_utf8)
throw MakeErrno("Failed to open directory"); throw MakeErrno("Failed to open directory");
} }
return std::make_unique<SmbclientDirectoryReader>(ctx, return std::make_unique<SmbclientDirectoryReader>(*this,
std::move(mapped), std::move(mapped),
handle); handle);
} }
@ -151,7 +155,7 @@ SkipNameFS(const char *name) noexcept
SmbclientDirectoryReader::~SmbclientDirectoryReader() SmbclientDirectoryReader::~SmbclientDirectoryReader()
{ {
const std::lock_guard<Mutex> lock(smbclient_mutex); const std::lock_guard<Mutex> lock(smbclient_mutex);
ctx.CloseDirectory(handle); storage.ctx.CloseDirectory(handle);
} }
const char * const char *
@ -159,7 +163,7 @@ SmbclientDirectoryReader::Read() noexcept
{ {
const std::lock_guard<Mutex> protect(smbclient_mutex); const std::lock_guard<Mutex> protect(smbclient_mutex);
while (auto e = ctx.ReadDirectory(handle)) { while (auto e = storage.ctx.ReadDirectory(handle)) {
name = e->name; name = e->name;
if (!SkipNameFS(name)) if (!SkipNameFS(name))
return name; return name;
@ -172,7 +176,7 @@ StorageFileInfo
SmbclientDirectoryReader::GetInfo([[maybe_unused]] bool follow) SmbclientDirectoryReader::GetInfo([[maybe_unused]] bool follow)
{ {
const std::string path = PathTraitsUTF8::Build(base, name); const std::string path = PathTraitsUTF8::Build(base, name);
return ::GetInfo(ctx, path.c_str()); return ::GetInfo(storage.ctx, path.c_str());
} }
static std::unique_ptr<Storage> static std::unique_ptr<Storage>