*/smbclient: use the new API with SMBCCTX parameter

As a side effect, the input plugin closes the SMB/CIFS connection
after closing the file.

This solves one part of
https://github.com/MusicPlayerDaemon/MPD/issues/916
This commit is contained in:
Max Kellermann
2020-07-20 21:10:58 +02:00
parent 697531a948
commit f6dc9bcad6
5 changed files with 93 additions and 42 deletions

View File

@@ -19,6 +19,7 @@
#include "SmbclientNeighborPlugin.hxx"
#include "lib/smbclient/Init.hxx"
#include "lib/smbclient/Context.hxx"
#include "lib/smbclient/Domain.hxx"
#include "lib/smbclient/Mutex.hxx"
#include "neighbor/NeighborPlugin.hxx"
@@ -56,6 +57,8 @@ class SmbclientNeighborExplorer final : public NeighborExplorer {
}
};
SmbclientContext ctx = SmbclientContext::New();
Thread thread;
mutable Mutex mutex;
@@ -66,7 +69,7 @@ class SmbclientNeighborExplorer final : public NeighborExplorer {
bool quit;
public:
explicit SmbclientNeighborExplorer(NeighborListener &_listener) noexcept
explicit SmbclientNeighborExplorer(NeighborListener &_listener)
:NeighborExplorer(_listener),
thread(BIND_THIS_METHOD(ThreadFunc)) {}
@@ -125,21 +128,24 @@ ReadServer(NeighborExplorer::List &list, const smbc_dirent &e) noexcept
}
static void
ReadServers(NeighborExplorer::List &list, const char *uri) noexcept;
ReadServers(SmbclientContext &ctx, const char *uri,
NeighborExplorer::List &list) noexcept;
static void
ReadWorkgroup(NeighborExplorer::List &list, const std::string &name) noexcept
ReadWorkgroup(SmbclientContext &ctx, const std::string &name,
NeighborExplorer::List &list) noexcept
{
std::string uri = "smb://" + name;
ReadServers(list, uri.c_str());
ReadServers(ctx, uri.c_str(), list);
}
static void
ReadEntry(NeighborExplorer::List &list, const smbc_dirent &e) noexcept
ReadEntry(SmbclientContext &ctx, const smbc_dirent &e,
NeighborExplorer::List &list) noexcept
{
switch (e.smbc_type) {
case SMBC_WORKGROUP:
ReadWorkgroup(list, std::string(e.name, e.namelen));
ReadWorkgroup(ctx, std::string(e.name, e.namelen), list);
break;
case SMBC_SERVER:
@@ -149,20 +155,21 @@ ReadEntry(NeighborExplorer::List &list, const smbc_dirent &e) noexcept
}
static void
ReadServers(NeighborExplorer::List &list, int fd) noexcept
ReadServers(SmbclientContext &ctx, SMBCFILE *handle,
NeighborExplorer::List &list) noexcept
{
smbc_dirent *e;
while ((e = smbc_readdir(fd)) != nullptr)
ReadEntry(list, *e);
while (auto e = ctx.ReadDirectory(handle))
ReadEntry(ctx, *e, list);
}
static void
ReadServers(NeighborExplorer::List &list, const char *uri) noexcept
ReadServers(SmbclientContext &ctx, const char *uri,
NeighborExplorer::List &list) noexcept
{
int fd = smbc_opendir(uri);
if (fd >= 0) {
ReadServers(list, fd);
smbc_closedir(fd);
SMBCFILE *handle = ctx.OpenDirectory(uri);
if (handle != nullptr) {
ReadServers(ctx, handle, list);
ctx.CloseDirectory(handle);
} else
FormatErrno(smbclient_domain, "smbc_opendir('%s') failed",
uri);
@@ -170,11 +177,11 @@ ReadServers(NeighborExplorer::List &list, const char *uri) noexcept
gcc_pure
static NeighborExplorer::List
DetectServers() noexcept
DetectServers(SmbclientContext &ctx) noexcept
{
NeighborExplorer::List list;
const std::lock_guard<Mutex> protect(smbclient_mutex);
ReadServers(list, "smb://");
ReadServers(ctx, "smb://", list);
return list;
}
@@ -198,7 +205,7 @@ SmbclientNeighborExplorer::Run() noexcept
{
const ScopeUnlock unlock(mutex);
found = DetectServers();
found = DetectServers(ctx);
}
const auto found_before_begin = found.before_begin();