SmbclientStorage.cxx 5.19 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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 "SmbclientStorage.hxx"
21
#include "storage/StoragePlugin.hxx"
22 23 24
#include "storage/StorageInterface.hxx"
#include "storage/FileInfo.hxx"
#include "lib/smbclient/Init.hxx"
25
#include "lib/smbclient/Mutex.hxx"
26
#include "fs/Traits.hxx"
27
#include "thread/Mutex.hxx"
28
#include "system/Error.hxx"
29
#include "util/ASCII.hxx"
30
#include "util/StringCompare.hxx"
31
#include "util/ScopeExit.hxx"
32 33 34 35 36 37 38 39 40 41 42 43 44

#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) {}

45
	~SmbclientDirectoryReader() override;
46 47

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

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

	SMBCCTX *const ctx;

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

61
	~SmbclientStorage() override {
62
		const std::lock_guard<Mutex> lock(smbclient_mutex);
63 64 65 66
		smbc_free_context(ctx, 1);
	}

	/* virtual methods from class Storage */
67
	StorageFileInfo GetInfo(std::string_view uri_utf8, bool follow) override;
68

69
	std::unique_ptr<StorageDirectoryReader> OpenDirectory(std::string_view uri_utf8) override;
70

71
	[[nodiscard]] std::string MapUTF8(std::string_view uri_utf8) const noexcept override;
72

73
	[[nodiscard]] std::string_view MapToRelativeUTF8(std::string_view uri_utf8) const noexcept override;
74 75 76
};

std::string
77
SmbclientStorage::MapUTF8(std::string_view uri_utf8) const noexcept
78
{
79
	if (uri_utf8.empty())
80 81
		return base;

82
	return PathTraitsUTF8::Build(base, uri_utf8);
83 84
}

85 86
std::string_view
SmbclientStorage::MapToRelativeUTF8(std::string_view uri_utf8) const noexcept
87
{
88
	return PathTraitsUTF8::Relative(base, uri_utf8);
89 90
}

91 92
static StorageFileInfo
GetInfo(const char *path)
93 94
{
	struct stat st;
95 96

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

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

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

117
StorageFileInfo
118
SmbclientStorage::GetInfo(std::string_view uri_utf8, [[maybe_unused]] bool follow)
119 120
{
	const std::string mapped = MapUTF8(uri_utf8);
121
	return ::GetInfo(mapped.c_str());
122 123
}

124
std::unique_ptr<StorageDirectoryReader>
125
SmbclientStorage::OpenDirectory(std::string_view uri_utf8)
126 127
{
	std::string mapped = MapUTF8(uri_utf8);
128 129 130 131

	int handle;

	{
132
		const std::lock_guard<Mutex> protect(smbclient_mutex);
133 134 135
		handle = smbc_opendir(mapped.c_str());
		if (handle < 0)
			throw MakeErrno("Failed to open directory");
136 137
	}

138
	return std::make_unique<SmbclientDirectoryReader>(std::move(mapped),
139
							  handle);
140 141 142 143
}

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

SmbclientDirectoryReader::~SmbclientDirectoryReader()
{
153
	const std::lock_guard<Mutex> lock(smbclient_mutex);
154 155 156 157
	smbc_close(handle);
}

const char *
158
SmbclientDirectoryReader::Read() noexcept
159
{
160
	const std::lock_guard<Mutex> protect(smbclient_mutex);
161

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

	return nullptr;
}

172
StorageFileInfo
Rosen Penev's avatar
Rosen Penev committed
173
SmbclientDirectoryReader::GetInfo([[maybe_unused]] bool follow)
174
{
175
	const std::string path = PathTraitsUTF8::Build(base, name);
176
	return ::GetInfo(path.c_str());
177 178
}

179
static std::unique_ptr<Storage>
Rosen Penev's avatar
Rosen Penev committed
180
CreateSmbclientStorageURI([[maybe_unused]] EventLoop &event_loop, const char *base)
181
{
182
	if (!StringStartsWithCaseASCII(base, "smb://"))
183 184
		return nullptr;

185
	SmbclientInit();
186

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

	SMBCCTX *ctx2 = smbc_init_context(ctx);
	if (ctx2 == nullptr) {
194 195
		AtScopeExit(ctx) { smbc_free_context(ctx, 1); };
		throw MakeErrno("smbc_new_context() failed");
196 197
	}

198
	return std::make_unique<SmbclientStorage>(base, ctx2);
199
}
200 201 202 203 204

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