Mapper.cxx 2.99 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 = AllocatedPath::Null();
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
void
62
mapper_finish() noexcept
63 64 65
{
}

66 67
#ifdef ENABLE_DATABASE

68
AllocatedPath
69
map_uri_fs(const char *uri) noexcept
70
{
71
	assert(uri != nullptr);
72 73
	assert(*uri != '/');

74 75 76 77
	if (instance->storage == nullptr)
		return AllocatedPath::Null();

	const auto music_dir_fs = instance->storage->MapFS("");
78
	if (music_dir_fs.IsNull())
79
		return AllocatedPath::Null();
80

81
	const auto uri_fs = AllocatedPath::FromUTF8(uri);
82
	if (uri_fs.IsNull())
83
		return AllocatedPath::Null();
84

85
	return AllocatedPath::Build(music_dir_fs, uri_fs);
86 87
}

88
std::string
89
map_fs_to_utf8(Path path_fs) noexcept
90
{
91
	if (path_fs.IsAbsolute()) {
92 93 94 95 96 97 98
		if (instance->storage == nullptr)
			return std::string();

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

99
		auto relative = music_dir_fs.Relative(path_fs);
100
		if (relative == nullptr || StringIsEmpty(relative))
101
			return std::string();
102 103

		path_fs = Path::FromFS(relative);
104
	}
105

106
	return path_fs.ToUTF8();
107
}
108

109 110
#endif

111
const AllocatedPath &
112
map_spl_path() noexcept
113
{
114
	return playlist_dir_fs;
115 116
}

117
AllocatedPath
118
map_spl_utf8_to_fs(const char *name) noexcept
119
{
120
	if (playlist_dir_fs.IsNull())
121
		return AllocatedPath::Null();
122

Max Kellermann's avatar
Max Kellermann committed
123 124 125
	std::string filename_utf8 = name;
	filename_utf8.append(PLAYLIST_FILE_SUFFIX);

126 127
	const auto filename_fs =
		AllocatedPath::FromUTF8(filename_utf8.c_str());
128
	if (filename_fs.IsNull())
129
		return AllocatedPath::Null();
130

131
	return AllocatedPath::Build(playlist_dir_fs, filename_fs);
132
}