SmbclientStorage.cxx 5.05 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 "SmbclientStorage.hxx"
22
#include "storage/StoragePlugin.hxx"
23 24 25
#include "storage/StorageInterface.hxx"
#include "storage/FileInfo.hxx"
#include "lib/smbclient/Init.hxx"
26
#include "lib/smbclient/Mutex.hxx"
27
#include "fs/Traits.hxx"
28
#include "thread/Mutex.hxx"
29
#include "system/Error.hxx"
30
#include "util/StringCompare.hxx"
31
#include "util/ScopeExit.hxx"
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

#include <libsmbclient.h>

class SmbclientDirectoryReader final : public StorageDirectoryReader {
	const std::string base;
	const unsigned handle;

	const char *name;

public:
	SmbclientDirectoryReader(std::string &&_base, unsigned _handle)
		:base(std::move(_base)), handle(_handle) {}

	virtual ~SmbclientDirectoryReader();

	/* virtual methods from class StorageDirectoryReader */
48
	const char *Read() override;
49
	StorageFileInfo GetInfo(bool follow) override;
50 51 52 53 54 55 56 57 58 59 60 61
};

class SmbclientStorage final : public Storage {
	const std::string base;

	SMBCCTX *const ctx;

public:
	SmbclientStorage(const char *_base, SMBCCTX *_ctx)
		:base(_base), ctx(_ctx) {}

	virtual ~SmbclientStorage() {
62
		smbclient_mutex.lock();
63
		smbc_free_context(ctx, 1);
64
		smbclient_mutex.unlock();
65 66 67
	}

	/* virtual methods from class Storage */
68
	StorageFileInfo GetInfo(const char *uri_utf8, bool follow) override;
69

70
	StorageDirectoryReader *OpenDirectory(const char *uri_utf8) override;
71

72
	std::string MapUTF8(const char *uri_utf8) const noexcept override;
73

74
	const char *MapToRelativeUTF8(const char *uri_utf8) const noexcept override;
75 76 77
};

std::string
78
SmbclientStorage::MapUTF8(const char *uri_utf8) const noexcept
79 80 81
{
	assert(uri_utf8 != nullptr);

82
	if (StringIsEmpty(uri_utf8))
83 84 85 86 87
		return base;

	return PathTraitsUTF8::Build(base.c_str(), uri_utf8);
}

88
const char *
89
SmbclientStorage::MapToRelativeUTF8(const char *uri_utf8) const noexcept
90 91 92 93
{
	return PathTraitsUTF8::Relative(base.c_str(), uri_utf8);
}

94 95
static StorageFileInfo
GetInfo(const char *path)
96 97
{
	struct stat st;
98 99

	{
100
		const std::lock_guard<Mutex> protect(smbclient_mutex);
101 102
		if (smbc_stat(path, &st) != 0)
			throw MakeErrno("Failed to access file");
103 104
	}

105
	StorageFileInfo info;
106
	if (S_ISREG(st.st_mode))
107
		info.type = StorageFileInfo::Type::REGULAR;
108
	else if (S_ISDIR(st.st_mode))
109
		info.type = StorageFileInfo::Type::DIRECTORY;
110
	else
111
		info.type = StorageFileInfo::Type::OTHER;
112 113

	info.size = st.st_size;
114
	info.mtime = std::chrono::system_clock::from_time_t(st.st_mtime);
115 116
	info.device = st.st_dev;
	info.inode = st.st_ino;
117
	return info;
118 119
}

120 121
StorageFileInfo
SmbclientStorage::GetInfo(const char *uri_utf8, gcc_unused bool follow)
122 123
{
	const std::string mapped = MapUTF8(uri_utf8);
124
	return ::GetInfo(mapped.c_str());
125 126 127
}

StorageDirectoryReader *
128
SmbclientStorage::OpenDirectory(const char *uri_utf8)
129 130
{
	std::string mapped = MapUTF8(uri_utf8);
131 132 133 134

	int handle;

	{
135
		const std::lock_guard<Mutex> protect(smbclient_mutex);
136 137 138
		handle = smbc_opendir(mapped.c_str());
		if (handle < 0)
			throw MakeErrno("Failed to open directory");
139 140 141 142 143 144 145
	}

	return new SmbclientDirectoryReader(std::move(mapped.c_str()), handle);
}

gcc_pure
static bool
146
SkipNameFS(const char *name) noexcept
147 148 149 150 151 152 153 154
{
	return name[0] == '.' &&
		(name[1] == 0 ||
		 (name[1] == '.' && name[2] == 0));
}

SmbclientDirectoryReader::~SmbclientDirectoryReader()
{
155
	smbclient_mutex.lock();
156
	smbc_close(handle);
157
	smbclient_mutex.unlock();
158 159 160 161 162
}

const char *
SmbclientDirectoryReader::Read()
{
163
	const std::lock_guard<Mutex> protect(smbclient_mutex);
164

165 166 167 168 169 170 171 172 173 174
	struct smbc_dirent *e;
	while ((e = smbc_readdir(handle)) != nullptr) {
		name = e->name;
		if (!SkipNameFS(name))
			return name;
	}

	return nullptr;
}

175 176
StorageFileInfo
SmbclientDirectoryReader::GetInfo(gcc_unused bool follow)
177 178
{
	const std::string path = PathTraitsUTF8::Build(base.c_str(), name);
179
	return ::GetInfo(path.c_str());
180 181
}

182
static Storage *
183
CreateSmbclientStorageURI(gcc_unused EventLoop &event_loop, const char *base)
184
{
185
	if (strncmp(base, "smb://", 6) != 0)
186 187
		return nullptr;

188
	SmbclientInit();
189

190
	const std::lock_guard<Mutex> protect(smbclient_mutex);
191
	SMBCCTX *ctx = smbc_new_context();
192 193
	if (ctx == nullptr)
		throw MakeErrno("smbc_new_context() failed");
194 195 196

	SMBCCTX *ctx2 = smbc_init_context(ctx);
	if (ctx2 == nullptr) {
197 198
		AtScopeExit(ctx) { smbc_free_context(ctx, 1); };
		throw MakeErrno("smbc_new_context() failed");
199 200 201 202
	}

	return new SmbclientStorage(base, ctx2);
}
203 204 205 206 207

const StoragePlugin smbclient_storage_plugin = {
	"smbclient",
	CreateSmbclientStorageURI,
};