SmbclientInputPlugin.cxx 3.56 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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"
22
#include "lib/smbclient/Init.hxx"
23
#include "lib/smbclient/Mutex.hxx"
Max Kellermann's avatar
Max Kellermann committed
24 25
#include "../InputStream.hxx"
#include "../InputPlugin.hxx"
26
#include "PluginUnavailable.hxx"
27
#include "system/Error.hxx"
28
#include "util/StringCompare.hxx"
29 30 31

#include <libsmbclient.h>

32 33
#include <stdexcept>

34
class SmbclientInputStream final : public InputStream {
35 36 37 38
	SMBCCTX *ctx;
	int fd;

public:
39 40
	SmbclientInputStream(const char *_uri,
			     Mutex &_mutex, Cond &_cond,
41
			     SMBCCTX *_ctx, int _fd, const struct stat &st)
42
		:InputStream(_uri, _mutex, _cond),
43
		 ctx(_ctx), fd(_fd) {
44 45 46
		seekable = true;
		size = st.st_size;
		SetReady();
47 48 49
	}

	~SmbclientInputStream() {
50
		smbclient_mutex.lock();
51 52
		smbc_close(fd);
		smbc_free_context(ctx, 1);
53
		smbclient_mutex.unlock();
54 55
	}

56
	/* virtual methods from InputStream */
57

58
	bool IsEOF() noexcept override {
59
		return offset >= size;
60 61
	}

62 63
	size_t Read(void *ptr, size_t size) override;
	void Seek(offset_type offset) override;
64 65 66 67 68 69 70
};

/*
 * InputPlugin methods
 *
 */

71 72
static void
input_smbclient_init(gcc_unused const ConfigBlock &block)
73
{
74 75 76
	try {
		SmbclientInit();
	} catch (const std::runtime_error &e) {
77 78
		// TODO: use std::throw_with_nested()?
		throw PluginUnavailable(e.what());
79
	}
80 81 82

	// TODO: create one global SMBCCTX here?

83
	// TODO: evaluate ConfigBlock, call smbc_setOption*()
84 85 86 87
}

static InputStream *
input_smbclient_open(const char *uri,
88
		     Mutex &mutex, Cond &cond)
89 90 91 92
{
	if (!StringStartsWith(uri, "smb://"))
		return nullptr;

93
	const std::lock_guard<Mutex> protect(smbclient_mutex);
94

95
	SMBCCTX *ctx = smbc_new_context();
96 97
	if (ctx == nullptr)
		throw MakeErrno("smbc_new_context() failed");
98 99 100

	SMBCCTX *ctx2 = smbc_init_context(ctx);
	if (ctx2 == nullptr) {
101
		int e = errno;
102
		smbc_free_context(ctx, 1);
103
		throw MakeErrno(e, "smbc_init_context() failed");
104 105 106 107 108 109
	}

	ctx = ctx2;

	int fd = smbc_open(uri, O_RDONLY, 0);
	if (fd < 0) {
110
		int e = errno;
111
		smbc_free_context(ctx, 1);
112
		throw MakeErrno(e, "smbc_open() failed");
113 114 115 116
	}

	struct stat st;
	if (smbc_fstat(fd, &st) < 0) {
117
		int e = errno;
118
		smbc_free_context(ctx, 1);
119
		throw MakeErrno(e, "smbc_fstat() failed");
120 121
	}

122
	return new SmbclientInputStream(uri, mutex, cond, ctx, fd, st);
123 124
}

125
size_t
126
SmbclientInputStream::Read(void *ptr, size_t read_size)
127
{
128 129 130
	smbclient_mutex.lock();
	ssize_t nbytes = smbc_read(fd, ptr, read_size);
	smbclient_mutex.unlock();
131 132
	if (nbytes < 0)
		throw MakeErrno("smbc_read() failed");
133

134
	offset += nbytes;
135
	return nbytes;
136 137
}

138 139
void
SmbclientInputStream::Seek(offset_type new_offset)
140
{
141
	smbclient_mutex.lock();
142
	off_t result = smbc_lseek(fd, new_offset, SEEK_SET);
143
	smbclient_mutex.unlock();
144 145
	if (result < 0)
		throw MakeErrno("smbc_lseek() failed");
146 147

	offset = result;
148 149 150 151 152 153 154 155
}

const InputPlugin input_plugin_smbclient = {
	"smbclient",
	input_smbclient_init,
	nullptr,
	input_smbclient_open,
};