Mapper.cxx 6.51 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19 20 21 22 23
 */

/*
 * Maps directory and song objects to file system paths.
 */

24
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
25
#include "Mapper.hxx"
26
#include "Directory.hxx"
27
#include "Song.hxx"
28
#include "DetachedSong.hxx"
29
#include "LightSong.hxx"
30
#include "fs/AllocatedPath.hxx"
31
#include "fs/Traits.hxx"
32
#include "fs/Charset.hxx"
33 34
#include "fs/FileSystem.hxx"
#include "fs/DirectoryReader.hxx"
35 36
#include "util/Domain.hxx"
#include "Log.hxx"
37

Max Kellermann's avatar
Max Kellermann committed
38 39
#include <assert.h>
#include <string.h>
40 41
#include <sys/stat.h>
#include <errno.h>
42

43 44
static constexpr Domain mapper_domain("mapper");

45 46 47
/**
 * The absolute path of the music directory encoded in UTF-8.
 */
Max Kellermann's avatar
Max Kellermann committed
48
static std::string music_dir_utf8;
49
static size_t music_dir_utf8_length;
50

51 52 53 54
/**
 * The absolute path of the music directory encoded in the filesystem
 * character set.
 */
55
static AllocatedPath music_dir_fs = AllocatedPath::Null();
56 57 58 59 60

/**
 * The absolute path of the playlist directory encoded in the
 * filesystem character set.
 */
61
static AllocatedPath playlist_dir_fs = AllocatedPath::Null();
62

63
static void
64
check_directory(const char *path_utf8, const AllocatedPath &path_fs)
65
{
66
	struct stat st;
67
	if (!StatFile(path_fs, st)) {
68 69 70
		FormatErrno(mapper_domain,
			    "Failed to stat directory \"%s\"",
			    path_utf8);
71 72 73 74
		return;
	}

	if (!S_ISDIR(st.st_mode)) {
75 76
		FormatError(mapper_domain,
			    "Not a directory: %s", path_utf8);
77 78
		return;
	}
79 80

#ifndef WIN32
81
	const auto x = AllocatedPath::Build(path_fs, ".");
82
	if (!StatFile(x, st) && errno == EACCES)
83 84 85
		FormatError(mapper_domain,
			    "No permission to traverse (\"execute\") directory: %s",
			    path_utf8);
86
#endif
87

88
	const DirectoryReader reader(path_fs);
89
	if (reader.HasFailed() && errno == EACCES)
90 91
		FormatError(mapper_domain,
			    "No permission to read directory: %s", path_utf8);
92 93
}

94
static void
95
mapper_set_music_dir(AllocatedPath &&path)
96
{
97
	assert(!path.IsNull());
98

99
	music_dir_fs = std::move(path);
100
	music_dir_fs.ChopSeparators();
101

Max Kellermann's avatar
Max Kellermann committed
102 103
	music_dir_utf8 = music_dir_fs.ToUTF8();
	music_dir_utf8_length = music_dir_utf8.length();
104

Max Kellermann's avatar
Max Kellermann committed
105
	check_directory(music_dir_utf8.c_str(), music_dir_fs);
106 107
}

108
static void
109
mapper_set_playlist_dir(AllocatedPath &&path)
110
{
111 112
	assert(!path.IsNull());

113
	playlist_dir_fs = std::move(path);
114

115 116
	const auto utf8 = playlist_dir_fs.ToUTF8();
	check_directory(utf8.c_str(), playlist_dir_fs);
117
}
118

119
void
120
mapper_init(AllocatedPath &&_music_dir, AllocatedPath &&_playlist_dir)
121
{
122 123
	if (!_music_dir.IsNull())
		mapper_set_music_dir(std::move(_music_dir));
124

125 126
	if (!_playlist_dir.IsNull())
		mapper_set_playlist_dir(std::move(_playlist_dir));
127 128 129 130 131 132
}

void mapper_finish(void)
{
}

133
const char *
134
mapper_get_music_directory_utf8(void)
135
{
136 137 138
	return music_dir_utf8.empty()
		? nullptr
		: music_dir_utf8.c_str();
139 140
}

141
const AllocatedPath &
142 143 144
mapper_get_music_directory_fs(void)
{
	return music_dir_fs;
145 146
}

147 148 149
const char *
map_to_relative_path(const char *path_utf8)
{
Max Kellermann's avatar
Max Kellermann committed
150 151
	return !music_dir_utf8.empty() &&
		memcmp(path_utf8, music_dir_utf8.c_str(),
152
		       music_dir_utf8_length) == 0 &&
153
		PathTraitsUTF8::IsSeparator(path_utf8[music_dir_utf8_length])
154
		? path_utf8 + music_dir_utf8_length + 1
155 156 157
		: path_utf8;
}

158
AllocatedPath
159
map_uri_fs(const char *uri)
160
{
161
	assert(uri != nullptr);
162 163
	assert(*uri != '/');

164
	if (music_dir_fs.IsNull())
165
		return AllocatedPath::Null();
166

167
	const auto uri_fs = AllocatedPath::FromUTF8(uri);
168
	if (uri_fs.IsNull())
169
		return AllocatedPath::Null();
170

171
	return AllocatedPath::Build(music_dir_fs, uri_fs);
172 173
}

174
AllocatedPath
175
map_directory_fs(const Directory &directory)
176
{
177
	assert(!music_dir_fs.IsNull());
178

179
	if (directory.IsRoot())
180
		return music_dir_fs;
181

182
	return map_uri_fs(directory.GetPath());
183 184
}

185
AllocatedPath
186
map_directory_child_fs(const Directory &directory, const char *name)
187
{
188
	assert(!music_dir_fs.IsNull());
189

190
	/* check for invalid or unauthorized base names */
191
	if (*name == 0 || strchr(name, '/') != nullptr ||
192
	    strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
193
		return AllocatedPath::Null();
194

195
	const auto parent_fs = map_directory_fs(directory);
196
	if (parent_fs.IsNull())
197
		return AllocatedPath::Null();
198

199
	const auto name_fs = AllocatedPath::FromUTF8(name);
200
	if (name_fs.IsNull())
201
		return AllocatedPath::Null();
202

203
	return AllocatedPath::Build(parent_fs, name_fs);
204 205
}

206 207 208 209 210
/**
 * Map a song object that was created by song_dup_detached().  It does
 * not have a real parent directory, only the dummy object
 * #detached_root.
 */
211
static AllocatedPath
212 213
map_detached_song_fs(const char *uri_utf8)
{
214
	auto uri_fs = AllocatedPath::FromUTF8(uri_utf8);
215
	if (uri_fs.IsNull())
216
		return uri_fs;
217

218
	return AllocatedPath::Build(music_dir_fs, uri_fs);
219 220
}

221
DetachedSong
222
map_song_detach(const LightSong &song)
223
{
224 225
	DetachedSong detached(song);

226
	if (detached.IsInDatabase() && !detached.HasRealURI()) {
227 228 229 230 231 232
		const auto uri = song.GetURI();
		detached.SetRealURI(PathTraitsUTF8::Build(music_dir_utf8.c_str(),
							  uri.c_str()));
	}

	return detached;
233 234
}

235
AllocatedPath
236
map_song_fs(const Song &song)
237
{
238 239 240 241
	return song.parent == nullptr
		? map_detached_song_fs(song.uri)
		: map_directory_child_fs(*song.parent, song.uri);
}
242

243 244 245 246
AllocatedPath
map_song_fs(const DetachedSong &song)
{
	if (song.IsAbsoluteFile())
247
		return AllocatedPath::FromUTF8(song.GetRealURI());
248
	else
249
		return map_uri_fs(song.GetURI());
250 251
}

252
std::string
253
map_fs_to_utf8(const char *path_fs)
254
{
255
	if (PathTraitsFS::IsSeparator(path_fs[0])) {
256 257 258 259
		path_fs = music_dir_fs.RelativeFS(path_fs);
		if (path_fs == nullptr || *path_fs == 0)
			return std::string();
	}
260

261
	return PathToUTF8(path_fs);
262
}
263

264
const AllocatedPath &
265 266
map_spl_path(void)
{
267
	return playlist_dir_fs;
268 269
}

270
AllocatedPath
271
map_spl_utf8_to_fs(const char *name)
272
{
273
	if (playlist_dir_fs.IsNull())
274
		return AllocatedPath::Null();
275

Max Kellermann's avatar
Max Kellermann committed
276 277 278
	std::string filename_utf8 = name;
	filename_utf8.append(PLAYLIST_FILE_SUFFIX);

279 280
	const auto filename_fs =
		AllocatedPath::FromUTF8(filename_utf8.c_str());
281
	if (filename_fs.IsNull())
282
		return AllocatedPath::Null();
283

284
	return AllocatedPath::Build(playlist_dir_fs, filename_fs);
285
}