Mapper.cxx 2.83 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "fs/AllocatedPath.hxx"
27
#include "fs/CheckFile.hxx"
28
#include "util/StringCompare.hxx"
29

30
#ifdef ENABLE_DATABASE
31 32 33
#include "storage/StorageInterface.hxx"
#include "Instance.hxx"
#include "Main.hxx"
34 35
#endif

36 37
#include <assert.h>

38 39 40 41
/**
 * The absolute path of the playlist directory encoded in the
 * filesystem character set.
 */
42
static AllocatedPath playlist_dir_fs = nullptr;
43

44
static void
45
mapper_set_playlist_dir(AllocatedPath &&path)
46
{
47 48
	assert(!path.IsNull());

49
	playlist_dir_fs = std::move(path);
50

51
	CheckDirectoryReadable(playlist_dir_fs);
52
}
53

54
void
55
mapper_init(AllocatedPath &&_playlist_dir)
56
{
57 58
	if (!_playlist_dir.IsNull())
		mapper_set_playlist_dir(std::move(_playlist_dir));
59 60
}

61 62
#ifdef ENABLE_DATABASE

63
AllocatedPath
64
map_uri_fs(const char *uri) noexcept
65
{
66
	assert(uri != nullptr);
67 68
	assert(*uri != '/');

69
	if (instance->storage == nullptr)
70
		return nullptr;
71 72

	const auto music_dir_fs = instance->storage->MapFS("");
73
	if (music_dir_fs.IsNull())
74
		return nullptr;
75

76
	const auto uri_fs = AllocatedPath::FromUTF8(uri);
77
	if (uri_fs.IsNull())
78
		return nullptr;
79

80
	return music_dir_fs / uri_fs;
81 82
}

83
std::string
84
map_fs_to_utf8(Path path_fs) noexcept
85
{
86
	if (path_fs.IsAbsolute()) {
87 88 89 90 91 92 93
		if (instance->storage == nullptr)
			return std::string();

		const auto music_dir_fs = instance->storage->MapFS("");
		if (music_dir_fs.IsNull())
			return std::string();

94
		auto relative = music_dir_fs.Relative(path_fs);
95
		if (relative == nullptr || StringIsEmpty(relative))
96
			return std::string();
97 98

		path_fs = Path::FromFS(relative);
99
	}
100

101
	return path_fs.ToUTF8();
102
}
103

104 105
#endif

106
const AllocatedPath &
107
map_spl_path() noexcept
108
{
109
	return playlist_dir_fs;
110 111
}

112
AllocatedPath
113
map_spl_utf8_to_fs(const char *name) noexcept
114
{
115
	if (playlist_dir_fs.IsNull())
116
		return nullptr;
117

Max Kellermann's avatar
Max Kellermann committed
118 119 120
	std::string filename_utf8 = name;
	filename_utf8.append(PLAYLIST_FILE_SUFFIX);

121 122
	const auto filename_fs =
		AllocatedPath::FromUTF8(filename_utf8.c_str());
123
	if (filename_fs.IsNull())
124
		return nullptr;
125

126
	return playlist_dir_fs / filename_fs;
127
}