lib/smbclient/Context: new wrapper for SMBCCTX
This commit is contained in:
parent
b04c6fbd72
commit
697531a948
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
#include "SmbclientInputPlugin.hxx"
|
#include "SmbclientInputPlugin.hxx"
|
||||||
#include "lib/smbclient/Init.hxx"
|
#include "lib/smbclient/Init.hxx"
|
||||||
|
#include "lib/smbclient/Context.hxx"
|
||||||
#include "lib/smbclient/Mutex.hxx"
|
#include "lib/smbclient/Mutex.hxx"
|
||||||
#include "../InputStream.hxx"
|
#include "../InputStream.hxx"
|
||||||
#include "../InputPlugin.hxx"
|
#include "../InputPlugin.hxx"
|
||||||
@ -29,15 +30,17 @@
|
|||||||
#include <libsmbclient.h>
|
#include <libsmbclient.h>
|
||||||
|
|
||||||
class SmbclientInputStream final : public InputStream {
|
class SmbclientInputStream final : public InputStream {
|
||||||
SMBCCTX *ctx;
|
SmbclientContext ctx;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SmbclientInputStream(const char *_uri,
|
SmbclientInputStream(const char *_uri,
|
||||||
Mutex &_mutex,
|
Mutex &_mutex,
|
||||||
SMBCCTX *_ctx, int _fd, const struct stat &st)
|
SmbclientContext &&_ctx,
|
||||||
|
int _fd, const struct stat &st)
|
||||||
:InputStream(_uri, _mutex),
|
:InputStream(_uri, _mutex),
|
||||||
ctx(_ctx), fd(_fd) {
|
ctx(std::move(_ctx)), fd(_fd)
|
||||||
|
{
|
||||||
seekable = true;
|
seekable = true;
|
||||||
size = st.st_size;
|
size = st.st_size;
|
||||||
SetReady();
|
SetReady();
|
||||||
@ -46,7 +49,6 @@ public:
|
|||||||
~SmbclientInputStream() override {
|
~SmbclientInputStream() override {
|
||||||
const std::lock_guard<Mutex> lock(smbclient_mutex);
|
const std::lock_guard<Mutex> lock(smbclient_mutex);
|
||||||
smbc_close(fd);
|
smbc_close(fd);
|
||||||
smbc_free_context(ctx, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* virtual methods from InputStream */
|
/* virtual methods from InputStream */
|
||||||
@ -83,38 +85,22 @@ static InputStreamPtr
|
|||||||
input_smbclient_open(const char *uri,
|
input_smbclient_open(const char *uri,
|
||||||
Mutex &mutex)
|
Mutex &mutex)
|
||||||
{
|
{
|
||||||
|
auto ctx = SmbclientContext::New();
|
||||||
|
|
||||||
const std::lock_guard<Mutex> protect(smbclient_mutex);
|
const std::lock_guard<Mutex> protect(smbclient_mutex);
|
||||||
|
|
||||||
SMBCCTX *ctx = smbc_new_context();
|
|
||||||
if (ctx == nullptr)
|
|
||||||
throw MakeErrno("smbc_new_context() failed");
|
|
||||||
|
|
||||||
SMBCCTX *ctx2 = smbc_init_context(ctx);
|
|
||||||
if (ctx2 == nullptr) {
|
|
||||||
int e = errno;
|
|
||||||
smbc_free_context(ctx, 1);
|
|
||||||
throw MakeErrno(e, "smbc_init_context() failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx = ctx2;
|
|
||||||
|
|
||||||
int fd = smbc_open(uri, O_RDONLY, 0);
|
int fd = smbc_open(uri, O_RDONLY, 0);
|
||||||
if (fd < 0) {
|
if (fd < 0)
|
||||||
int e = errno;
|
throw MakeErrno("smbc_open() failed");
|
||||||
smbc_free_context(ctx, 1);
|
|
||||||
throw MakeErrno(e, "smbc_open() failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (smbc_fstat(fd, &st) < 0) {
|
if (smbc_fstat(fd, &st) < 0)
|
||||||
int e = errno;
|
throw MakeErrno("smbc_fstat() failed");
|
||||||
smbc_free_context(ctx, 1);
|
|
||||||
throw MakeErrno(e, "smbc_fstat() failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
return std::make_unique<MaybeBufferedInputStream>
|
return std::make_unique<MaybeBufferedInputStream>
|
||||||
(std::make_unique<SmbclientInputStream>(uri, mutex,
|
(std::make_unique<SmbclientInputStream>(uri, mutex,
|
||||||
ctx, fd, st));
|
std::move(ctx),
|
||||||
|
fd, st));
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
|
40
src/lib/smbclient/Context.cxx
Normal file
40
src/lib/smbclient/Context.cxx
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2003-2020 The Music Player Daemon Project
|
||||||
|
* http://www.musicpd.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "Context.hxx"
|
||||||
|
#include "system/Error.hxx"
|
||||||
|
|
||||||
|
#include <cerrno>
|
||||||
|
|
||||||
|
SmbclientContext
|
||||||
|
SmbclientContext::New()
|
||||||
|
{
|
||||||
|
SMBCCTX *ctx = smbc_new_context();
|
||||||
|
if (ctx == nullptr)
|
||||||
|
throw MakeErrno("smbc_new_context() failed");
|
||||||
|
|
||||||
|
SMBCCTX *ctx2 = smbc_init_context(ctx);
|
||||||
|
if (ctx2 == nullptr) {
|
||||||
|
int e = errno;
|
||||||
|
smbc_free_context(ctx, 1);
|
||||||
|
throw MakeErrno(e, "smbc_init_context() failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
return SmbclientContext(ctx2);
|
||||||
|
}
|
59
src/lib/smbclient/Context.hxx
Normal file
59
src/lib/smbclient/Context.hxx
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2003-2020 The Music Player Daemon Project
|
||||||
|
* http://www.musicpd.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MPD_SMBCLIENT_CONTEXT_HXX
|
||||||
|
#define MPD_SMBCLIENT_CONTEXT_HXX
|
||||||
|
|
||||||
|
#include <libsmbclient.h>
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper for `SMBCCTX*`.
|
||||||
|
*/
|
||||||
|
class SmbclientContext {
|
||||||
|
SMBCCTX *ctx = nullptr;
|
||||||
|
|
||||||
|
explicit SmbclientContext(SMBCCTX *_ctx) noexcept
|
||||||
|
:ctx(_ctx) {}
|
||||||
|
|
||||||
|
public:
|
||||||
|
SmbclientContext() = default;
|
||||||
|
|
||||||
|
~SmbclientContext() noexcept {
|
||||||
|
if (ctx != nullptr)
|
||||||
|
smbc_free_context(ctx, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
SmbclientContext(SmbclientContext &&src) noexcept
|
||||||
|
:ctx(std::exchange(src.ctx, nullptr)) {}
|
||||||
|
|
||||||
|
SmbclientContext &operator=(SmbclientContext &&src) noexcept {
|
||||||
|
using std::swap;
|
||||||
|
swap(ctx, src.ctx);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throws on error.
|
||||||
|
*/
|
||||||
|
static SmbclientContext New();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -9,6 +9,7 @@ smbclient = static_library(
|
|||||||
'Domain.cxx',
|
'Domain.cxx',
|
||||||
'Mutex.cxx',
|
'Mutex.cxx',
|
||||||
'Init.cxx',
|
'Init.cxx',
|
||||||
|
'Context.cxx',
|
||||||
include_directories: inc,
|
include_directories: inc,
|
||||||
dependencies: [
|
dependencies: [
|
||||||
smbclient_dep,
|
smbclient_dep,
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
#include "storage/StorageInterface.hxx"
|
#include "storage/StorageInterface.hxx"
|
||||||
#include "storage/FileInfo.hxx"
|
#include "storage/FileInfo.hxx"
|
||||||
#include "lib/smbclient/Init.hxx"
|
#include "lib/smbclient/Init.hxx"
|
||||||
|
#include "lib/smbclient/Context.hxx"
|
||||||
#include "lib/smbclient/Mutex.hxx"
|
#include "lib/smbclient/Mutex.hxx"
|
||||||
#include "fs/Traits.hxx"
|
#include "fs/Traits.hxx"
|
||||||
#include "thread/Mutex.hxx"
|
#include "thread/Mutex.hxx"
|
||||||
@ -52,16 +53,11 @@ public:
|
|||||||
class SmbclientStorage final : public Storage {
|
class SmbclientStorage final : public Storage {
|
||||||
const std::string base;
|
const std::string base;
|
||||||
|
|
||||||
SMBCCTX *const ctx;
|
SmbclientContext ctx = SmbclientContext::New();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SmbclientStorage(const char *_base, SMBCCTX *_ctx)
|
explicit SmbclientStorage(const char *_base)
|
||||||
:base(_base), ctx(_ctx) {}
|
:base(_base) {}
|
||||||
|
|
||||||
~SmbclientStorage() override {
|
|
||||||
const std::lock_guard<Mutex> lock(smbclient_mutex);
|
|
||||||
smbc_free_context(ctx, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* virtual methods from class Storage */
|
/* virtual methods from class Storage */
|
||||||
StorageFileInfo GetInfo(std::string_view uri_utf8, bool follow) override;
|
StorageFileInfo GetInfo(std::string_view uri_utf8, bool follow) override;
|
||||||
@ -184,18 +180,7 @@ CreateSmbclientStorageURI([[maybe_unused]] EventLoop &event_loop, const char *ba
|
|||||||
|
|
||||||
SmbclientInit();
|
SmbclientInit();
|
||||||
|
|
||||||
const std::lock_guard<Mutex> protect(smbclient_mutex);
|
return std::make_unique<SmbclientStorage>(base);
|
||||||
SMBCCTX *ctx = smbc_new_context();
|
|
||||||
if (ctx == nullptr)
|
|
||||||
throw MakeErrno("smbc_new_context() failed");
|
|
||||||
|
|
||||||
SMBCCTX *ctx2 = smbc_init_context(ctx);
|
|
||||||
if (ctx2 == nullptr) {
|
|
||||||
AtScopeExit(ctx) { smbc_free_context(ctx, 1); };
|
|
||||||
throw MakeErrno("smbc_new_context() failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
return std::make_unique<SmbclientStorage>(base, ctx2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const StoragePlugin smbclient_storage_plugin = {
|
const StoragePlugin smbclient_storage_plugin = {
|
||||||
|
Loading…
Reference in New Issue
Block a user