mpd/src/input/plugins/SmbclientInputPlugin.cxx

167 lines
3.7 KiB
C++
Raw Normal View History

2013-12-29 00:36:57 +01:00
/*
2017-01-03 20:48:59 +01:00
* Copyright 2003-2017 The Music Player Daemon Project
2013-12-29 00:36:57 +01:00
* 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 "config.h"
#include "SmbclientInputPlugin.hxx"
#include "lib/smbclient/Init.hxx"
#include "lib/smbclient/Mutex.hxx"
2014-01-24 16:18:21 +01:00
#include "../InputStream.hxx"
#include "../InputPlugin.hxx"
#include "PluginUnavailable.hxx"
#include "system/Error.hxx"
#include "util/StringCompare.hxx"
2013-12-29 00:36:57 +01:00
#include <libsmbclient.h>
#include <stdexcept>
class SmbclientInputStream final : public InputStream {
2013-12-29 00:36:57 +01:00
SMBCCTX *ctx;
int fd;
public:
SmbclientInputStream(const char *_uri,
Mutex &_mutex, Cond &_cond,
2013-12-29 00:36:57 +01:00
SMBCCTX *_ctx, int _fd, const struct stat &st)
2014-05-11 18:25:55 +02:00
:InputStream(_uri, _mutex, _cond),
2013-12-29 00:36:57 +01:00
ctx(_ctx), fd(_fd) {
seekable = true;
size = st.st_size;
SetReady();
2013-12-29 00:36:57 +01:00
}
~SmbclientInputStream() {
smbclient_mutex.lock();
2013-12-29 00:36:57 +01:00
smbc_close(fd);
smbc_free_context(ctx, 1);
smbclient_mutex.unlock();
2013-12-29 00:36:57 +01:00
}
/* virtual methods from InputStream */
2013-12-29 00:36:57 +01:00
bool IsEOF() noexcept override {
return offset >= size;
2013-12-29 00:36:57 +01:00
}
size_t Read(void *ptr, size_t size) override;
void Seek(offset_type offset) override;
2013-12-29 00:36:57 +01:00
};
/*
* InputPlugin methods
*
*/
static void
input_smbclient_init(EventLoop &, const ConfigBlock &)
2013-12-29 00:36:57 +01:00
{
try {
SmbclientInit();
} catch (const std::runtime_error &e) {
// TODO: use std::throw_with_nested()?
throw PluginUnavailable(e.what());
}
2013-12-29 00:36:57 +01:00
// TODO: create one global SMBCCTX here?
// TODO: evaluate ConfigBlock, call smbc_setOption*()
2013-12-29 00:36:57 +01:00
}
static InputStreamPtr
2013-12-29 00:36:57 +01:00
input_smbclient_open(const char *uri,
Mutex &mutex, Cond &cond)
2013-12-29 00:36:57 +01:00
{
if (!StringStartsWith(uri, "smb://"))
return nullptr;
const std::lock_guard<Mutex> protect(smbclient_mutex);
2013-12-29 00:36:57 +01:00
SMBCCTX *ctx = smbc_new_context();
if (ctx == nullptr)
throw MakeErrno("smbc_new_context() failed");
2013-12-29 00:36:57 +01:00
SMBCCTX *ctx2 = smbc_init_context(ctx);
if (ctx2 == nullptr) {
int e = errno;
2013-12-29 00:36:57 +01:00
smbc_free_context(ctx, 1);
throw MakeErrno(e, "smbc_init_context() failed");
2013-12-29 00:36:57 +01:00
}
ctx = ctx2;
int fd = smbc_open(uri, O_RDONLY, 0);
if (fd < 0) {
int e = errno;
2013-12-29 00:36:57 +01:00
smbc_free_context(ctx, 1);
throw MakeErrno(e, "smbc_open() failed");
2013-12-29 00:36:57 +01:00
}
struct stat st;
if (smbc_fstat(fd, &st) < 0) {
int e = errno;
2013-12-29 00:36:57 +01:00
smbc_free_context(ctx, 1);
throw MakeErrno(e, "smbc_fstat() failed");
2013-12-29 00:36:57 +01:00
}
return std::make_unique<SmbclientInputStream>(uri, mutex, cond,
ctx, fd, st);
2013-12-29 00:36:57 +01:00
}
size_t
SmbclientInputStream::Read(void *ptr, size_t read_size)
2013-12-29 00:36:57 +01:00
{
2017-11-13 17:12:21 +01:00
ssize_t nbytes;
{
const ScopeUnlock unlock(mutex);
2017-11-13 17:12:21 +01:00
const std::lock_guard<Mutex> lock(smbclient_mutex);
nbytes = smbc_read(fd, ptr, read_size);
}
if (nbytes < 0)
throw MakeErrno("smbc_read() failed");
2013-12-29 00:36:57 +01:00
2015-05-29 22:37:49 +02:00
offset += nbytes;
return nbytes;
2013-12-29 00:36:57 +01:00
}
void
SmbclientInputStream::Seek(offset_type new_offset)
2013-12-29 00:36:57 +01:00
{
2017-11-13 17:12:21 +01:00
off_t result;
{
const ScopeUnlock unlock(mutex);
2017-11-13 17:12:21 +01:00
const std::lock_guard<Mutex> lock(smbclient_mutex);
result = smbc_lseek(fd, new_offset, SEEK_SET);
}
if (result < 0)
throw MakeErrno("smbc_lseek() failed");
offset = result;
2013-12-29 00:36:57 +01:00
}
const InputPlugin input_plugin_smbclient = {
"smbclient",
input_smbclient_init,
nullptr,
input_smbclient_open,
};