LocalStorage.cxx 4.35 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 "LocalStorage.hxx"
21
#include "storage/StoragePlugin.hxx"
22
#include "storage/StorageInterface.hxx"
23
#include "storage/FileInfo.hxx"
24
#include "fs/FileInfo.hxx"
25 26
#include "fs/AllocatedPath.hxx"
#include "fs/DirectoryReader.hxx"
27
#include "util/StringCompare.hxx"
28 29 30 31 32 33 34 35 36 37 38

#include <string>

class LocalDirectoryReader final : public StorageDirectoryReader {
	AllocatedPath base_fs;

	DirectoryReader reader;

	std::string name_utf8;

public:
Max Kellermann's avatar
Max Kellermann committed
39
	explicit LocalDirectoryReader(AllocatedPath &&_base_fs)
40 41 42
		:base_fs(std::move(_base_fs)), reader(base_fs) {}

	/* virtual methods from class StorageDirectoryReader */
43
	const char *Read() noexcept override;
44
	StorageFileInfo GetInfo(bool follow) override;
45 46 47 48
};

class LocalStorage final : public Storage {
	const AllocatedPath base_fs;
49
	const std::string base_utf8;
50 51

public:
52
	explicit LocalStorage(Path _base_fs)
53
		:base_fs(_base_fs), base_utf8(base_fs.ToUTF8Throw()) {
54 55 56
		assert(!base_fs.IsNull());
		assert(!base_utf8.empty());
	}
57 58

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

61
	std::unique_ptr<StorageDirectoryReader> OpenDirectory(std::string_view uri_utf8) override;
62

63
	[[nodiscard]] std::string MapUTF8(std::string_view uri_utf8) const noexcept override;
64

65
	[[nodiscard]] AllocatedPath MapFS(std::string_view uri_utf8) const noexcept override;
66

67
	[[nodiscard]] std::string_view MapToRelativeUTF8(std::string_view uri_utf8) const noexcept override;
68

69
private:
70
	[[nodiscard]] AllocatedPath MapFSOrThrow(std::string_view uri_utf8) const;
71
};
72

73 74
static StorageFileInfo
Stat(Path path, bool follow)
75
{
76 77
	const FileInfo src(path, follow);
	StorageFileInfo info;
78

79
	if (src.IsRegular())
80
		info.type = StorageFileInfo::Type::REGULAR;
81
	else if (src.IsDirectory())
82
		info.type = StorageFileInfo::Type::DIRECTORY;
83
	else
84
		info.type = StorageFileInfo::Type::OTHER;
85

86 87
	info.size = src.GetSize();
	info.mtime = src.GetModificationTime();
88
#ifdef _WIN32
89 90 91 92 93
	info.device = info.inode = 0;
#else
	info.device = src.GetDevice();
	info.inode = src.GetInode();
#endif
94
	return info;
95 96 97
}

std::string
98
LocalStorage::MapUTF8(std::string_view uri_utf8) const noexcept
99
{
100
	if (uri_utf8.empty())
101 102
		return base_utf8;

103
	return PathTraitsUTF8::Build(base_utf8, uri_utf8);
104 105 106
}

AllocatedPath
107
LocalStorage::MapFSOrThrow(std::string_view uri_utf8) const
108
{
109
	if (uri_utf8.empty())
110 111
		return base_fs;

112
	return base_fs / AllocatedPath::FromUTF8Throw(uri_utf8);
113 114 115
}

AllocatedPath
116
LocalStorage::MapFS(std::string_view uri_utf8) const noexcept
117
{
118 119
	try {
		return MapFSOrThrow(uri_utf8);
120
	} catch (...) {
121
		return nullptr;
122
	}
123 124
}

125 126
std::string_view
LocalStorage::MapToRelativeUTF8(std::string_view uri_utf8) const noexcept
127
{
128
	return PathTraitsUTF8::Relative(base_utf8, uri_utf8);
129 130
}

131
StorageFileInfo
132
LocalStorage::GetInfo(std::string_view uri_utf8, bool follow)
133
{
134
	return Stat(MapFSOrThrow(uri_utf8), follow);
135 136
}

137
std::unique_ptr<StorageDirectoryReader>
138
LocalStorage::OpenDirectory(std::string_view uri_utf8)
139
{
140
	return std::make_unique<LocalDirectoryReader>(MapFSOrThrow(uri_utf8));
141 142 143
}

const char *
144
LocalDirectoryReader::Read() noexcept
145 146 147
{
	while (reader.ReadEntry()) {
		const Path name_fs = reader.GetEntry();
148
		if (PathTraitsFS::IsSpecialFilename(name_fs.c_str()))
149 150
			continue;

151 152 153 154 155
		try {
			name_utf8 = name_fs.ToUTF8Throw();
			return name_utf8.c_str();
		} catch (...) {
		}
156 157 158 159 160
	}

	return nullptr;
}

161 162
StorageFileInfo
LocalDirectoryReader::GetInfo(bool follow)
163
{
164
	return Stat(base_fs / reader.GetEntry(), follow);
165
}
166

167
std::unique_ptr<Storage>
168
CreateLocalStorage(Path base_fs)
169
{
170
	return std::make_unique<LocalStorage>(base_fs);
171
}
172

173
constexpr StoragePlugin local_storage_plugin = {
174 175
	"local",
	nullptr,
176
	nullptr,
177
};