Mapper.cxx 2.95 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 "fs/AllocatedPath.hxx"
27
#include "fs/Traits.hxx"
28
#include "fs/Charset.hxx"
29
#include "fs/CheckFile.hxx"
30

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

37 38
#include <assert.h>

39 40 41 42
/**
 * The absolute path of the playlist directory encoded in the
 * filesystem character set.
 */
43
static AllocatedPath playlist_dir_fs = AllocatedPath::Null();
44

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

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

52
	CheckDirectoryReadable(playlist_dir_fs);
53
}
54

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

void mapper_finish(void)
{
}

66 67
#ifdef ENABLE_DATABASE

68
AllocatedPath
69
map_uri_fs(const char *uri)
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(const char *path_fs)
90
{
91
	if (PathTraitsFS::IsSeparator(path_fs[0])) {
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 100 101 102
		path_fs = music_dir_fs.RelativeFS(path_fs);
		if (path_fs == nullptr || *path_fs == 0)
			return std::string();
	}
103

104
	return PathToUTF8(path_fs);
105
}
106

107 108
#endif

109
const AllocatedPath &
110 111
map_spl_path(void)
{
112
	return playlist_dir_fs;
113 114
}

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

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

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

129
	return AllocatedPath::Build(playlist_dir_fs, filename_fs);
130
}