LocalStorage.cxx 4.46 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 39 40 41 42

#include <string>

class LocalDirectoryReader final : public StorageDirectoryReader {
	AllocatedPath base_fs;

	DirectoryReader reader;

	std::string name_utf8;

public:
	LocalDirectoryReader(AllocatedPath &&_base_fs)
		: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(const char *uri_utf8, bool follow) override;
60

61
	std::unique_ptr<StorageDirectoryReader> OpenDirectory(const char *uri_utf8) override;
62

63
	std::string MapUTF8(const char *uri_utf8) const noexcept override;
64

65
	AllocatedPath MapFS(const char *uri_utf8) const noexcept override;
66

67
	const char *MapToRelativeUTF8(const char *uri_utf8) const noexcept override;
68

69
private:
70
	AllocatedPath MapFSOrThrow(const char *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(const char *uri_utf8) const noexcept
99 100 101
{
	assert(uri_utf8 != nullptr);

102
	if (StringIsEmpty(uri_utf8))
103 104 105 106 107 108
		return base_utf8;

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

AllocatedPath
109
LocalStorage::MapFSOrThrow(const char *uri_utf8) const
110 111 112
{
	assert(uri_utf8 != nullptr);

113
	if (StringIsEmpty(uri_utf8))
114 115
		return base_fs;

116
	return base_fs / AllocatedPath::FromUTF8Throw(uri_utf8);
117 118 119
}

AllocatedPath
120
LocalStorage::MapFS(const char *uri_utf8) const noexcept
121
{
122 123
	try {
		return MapFSOrThrow(uri_utf8);
124
	} catch (...) {
125
		return nullptr;
126
	}
127 128
}

129
const char *
130
LocalStorage::MapToRelativeUTF8(const char *uri_utf8) const noexcept
131 132 133 134
{
	return PathTraitsUTF8::Relative(base_utf8.c_str(), uri_utf8);
}

135 136
StorageFileInfo
LocalStorage::GetInfo(const char *uri_utf8, bool follow)
137
{
138
	return Stat(MapFSOrThrow(uri_utf8), follow);
139 140
}

141
std::unique_ptr<StorageDirectoryReader>
142
LocalStorage::OpenDirectory(const char *uri_utf8)
143
{
144
	return std::make_unique<LocalDirectoryReader>(MapFSOrThrow(uri_utf8));
145 146 147 148
}

gcc_pure
static bool
149
SkipNameFS(PathTraitsFS::const_pointer_type name_fs) noexcept
150 151 152 153 154 155 156
{
	return name_fs[0] == '.' &&
		(name_fs[1] == 0 ||
		 (name_fs[1] == '.' && name_fs[2] == 0));
}

const char *
157
LocalDirectoryReader::Read() noexcept
158 159 160 161 162 163
{
	while (reader.ReadEntry()) {
		const Path name_fs = reader.GetEntry();
		if (SkipNameFS(name_fs.c_str()))
			continue;

164 165 166 167 168
		try {
			name_utf8 = name_fs.ToUTF8Throw();
			return name_utf8.c_str();
		} catch (...) {
		}
169 170 171 172 173
	}

	return nullptr;
}

174 175
StorageFileInfo
LocalDirectoryReader::GetInfo(bool follow)
176
{
177
	return Stat(base_fs / reader.GetEntry(), follow);
178
}
179

180
std::unique_ptr<Storage>
181
CreateLocalStorage(Path base_fs)
182
{
183
	return std::make_unique<LocalStorage>(base_fs);
184
}
185 186 187 188 189

const StoragePlugin local_storage_plugin = {
	"local",
	nullptr,
};