summaryrefslogtreecommitdiff
path: root/src/neighbor
diff options
context:
space:
mode:
authorMax Kellermann <max@musicpd.org>2020-07-20 21:10:58 +0200
committerMax Kellermann <max@musicpd.org>2020-07-20 22:05:05 +0200
commitf6dc9bcad691c7cc7ef77bf0f92b8b5161cdf32f (patch)
tree86a8fd53053a180bc60a5e1f6a8e0bc4201bebe1 /src/neighbor
parent697531a948bc8f21cd56c591bb5b5cd523cd30d1 (diff)
*/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
Diffstat (limited to 'src/neighbor')
-rw-r--r--src/neighbor/plugins/SmbclientNeighborPlugin.cxx43
1 files changed, 25 insertions, 18 deletions
diff --git a/src/neighbor/plugins/SmbclientNeighborPlugin.cxx b/src/neighbor/plugins/SmbclientNeighborPlugin.cxx
index b51a0b39b..ad944b003 100644
--- a/src/neighbor/plugins/SmbclientNeighborPlugin.cxx
+++ b/src/neighbor/plugins/SmbclientNeighborPlugin.cxx
@@ -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();