SmbclientStorage.cxx 5.09 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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/Context.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

#include <libsmbclient.h>

35 36
class SmbclientStorage;

37
class SmbclientDirectoryReader final : public StorageDirectoryReader {
38
	SmbclientStorage &storage;
39
	const std::string base;
40
	SMBCFILE *const handle;
41 42 43 44

	const char *name;

public:
45
	SmbclientDirectoryReader(SmbclientStorage &_storage,
46 47
				 std::string &&_base,
				 SMBCFILE *_handle) noexcept
48
		:storage(_storage), base(std::move(_base)), handle(_handle) {}
49

50
	~SmbclientDirectoryReader() override;
51 52

	/* virtual methods from class StorageDirectoryReader */
53
	const char *Read() noexcept override;
54
	StorageFileInfo GetInfo(bool follow) override;
55 56 57
};

class SmbclientStorage final : public Storage {
58 59
	friend class SmbclientDirectoryReader;

60 61
	const std::string base;

62 63 64 65 66 67
	/**
	 * This mutex protects all calls into the #SmbclientContext,
	 * which is not thread-safe.
	 */
	Mutex mutex;

68
	SmbclientContext ctx = SmbclientContext::New();
69 70

public:
71 72
	explicit SmbclientStorage(const char *_base)
		:base(_base) {}
73 74

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

77
	std::unique_ptr<StorageDirectoryReader> OpenDirectory(std::string_view uri_utf8) override;
78

Rosen Penev's avatar
Rosen Penev committed
79
	[[nodiscard]] std::string MapUTF8(std::string_view uri_utf8) const noexcept override;
80

Rosen Penev's avatar
Rosen Penev committed
81
	[[nodiscard]] std::string_view MapToRelativeUTF8(std::string_view uri_utf8) const noexcept override;
82 83 84
};

std::string
85
SmbclientStorage::MapUTF8(std::string_view uri_utf8) const noexcept
86
{
87
	if (uri_utf8.empty())
88 89
		return base;

90
	return PathTraitsUTF8::Build(base, uri_utf8);
91 92
}

93 94
std::string_view
SmbclientStorage::MapToRelativeUTF8(std::string_view uri_utf8) const noexcept
95
{
96
	return PathTraitsUTF8::Relative(base, uri_utf8);
97 98
}

99
static StorageFileInfo
100
GetInfo(SmbclientContext &ctx, Mutex &mutex, const char *path)
101 102
{
	struct stat st;
103 104

	{
105
		const std::lock_guard<Mutex> protect(mutex);
106
		if (ctx.Stat(path, st) != 0)
107
			throw MakeErrno("Failed to access file");
108 109
	}

110
	StorageFileInfo info;
111
	if (S_ISREG(st.st_mode))
112
		info.type = StorageFileInfo::Type::REGULAR;
113
	else if (S_ISDIR(st.st_mode))
114
		info.type = StorageFileInfo::Type::DIRECTORY;
115
	else
116
		info.type = StorageFileInfo::Type::OTHER;
117 118

	info.size = st.st_size;
119
	info.mtime = std::chrono::system_clock::from_time_t(st.st_mtime);
120 121
	info.device = st.st_dev;
	info.inode = st.st_ino;
122
	return info;
123 124
}

125
StorageFileInfo
126
SmbclientStorage::GetInfo(std::string_view uri_utf8, [[maybe_unused]] bool follow)
127 128
{
	const std::string mapped = MapUTF8(uri_utf8);
129
	return ::GetInfo(ctx, mutex, mapped.c_str());
130 131
}

132
std::unique_ptr<StorageDirectoryReader>
133
SmbclientStorage::OpenDirectory(std::string_view uri_utf8)
134 135
{
	std::string mapped = MapUTF8(uri_utf8);
136

137
	SMBCFILE *handle;
138 139

	{
140
		const std::lock_guard<Mutex> protect(mutex);
141
		handle = ctx.OpenDirectory(mapped.c_str());
142 143
	}

144 145 146
	if (handle == nullptr)
		throw MakeErrno("Failed to open directory");

147
	return std::make_unique<SmbclientDirectoryReader>(*this,
148
							  std::move(mapped),
149
							  handle);
150 151 152 153
}

gcc_pure
static bool
154
SkipNameFS(PathTraitsFS::const_pointer name) noexcept
155
{
156
	return PathTraitsFS::IsSpecialFilename(name);
157 158 159 160
}

SmbclientDirectoryReader::~SmbclientDirectoryReader()
{
161
	const std::lock_guard<Mutex> lock(storage.mutex);
162
	storage.ctx.CloseDirectory(handle);
163 164 165
}

const char *
166
SmbclientDirectoryReader::Read() noexcept
167
{
168
	const std::lock_guard<Mutex> protect(storage.mutex);
169

170
	while (auto e = storage.ctx.ReadDirectory(handle)) {
171 172 173 174 175 176 177 178
		name = e->name;
		if (!SkipNameFS(name))
			return name;
	}

	return nullptr;
}

179
StorageFileInfo
Rosen Penev's avatar
Rosen Penev committed
180
SmbclientDirectoryReader::GetInfo([[maybe_unused]] bool follow)
181
{
182
	const std::string path = PathTraitsUTF8::Build(base, name);
183
	return ::GetInfo(storage.ctx, storage.mutex, path.c_str());
184 185
}

186
static std::unique_ptr<Storage>
Rosen Penev's avatar
Rosen Penev committed
187
CreateSmbclientStorageURI([[maybe_unused]] EventLoop &event_loop, const char *base)
188
{
189
	if (!StringStartsWithCaseASCII(base, "smb://"))
190 191
		return nullptr;

192
	SmbclientInit();
193

194
	return std::make_unique<SmbclientStorage>(base);
195
}
196 197 198 199 200

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