Configured.cxx 2.21 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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 "Configured.hxx"
#include "Registry.hxx"
22
#include "StorageInterface.hxx"
23
#include "plugins/LocalStorage.hxx"
24
#include "config/Data.hxx"
25 26
#include "fs/StandardDirectory.hxx"
#include "fs/CheckFile.hxx"
27
#include "util/RuntimeError.hxx"
Max Kellermann's avatar
Max Kellermann committed
28
#include "util/UriExtract.hxx"
29

30
static std::unique_ptr<Storage>
31
CreateConfiguredStorageUri(EventLoop &event_loop, const char *uri)
32
{
33
	auto storage = CreateStorageURI(event_loop, uri);
34 35 36
	if (storage == nullptr)
		throw FormatRuntimeError("Unrecognized storage URI: %s", uri);

37 38 39 40
	return storage;
}

static AllocatedPath
41
GetConfiguredMusicDirectory(const ConfigData &config)
42
{
43
	AllocatedPath path = config.GetPath(ConfigOption::MUSIC_DIR);
44
	if (path.IsNull())
45 46 47 48 49
		path = GetUserMusicDir();

	return path;
}

50
static std::unique_ptr<Storage>
51
CreateConfiguredStorageLocal(const ConfigData &config)
52
{
53
	AllocatedPath path = GetConfiguredMusicDirectory(config);
54 55 56 57 58 59 60 61
	if (path.IsNull())
		return nullptr;

	path.ChopSeparators();
	CheckDirectoryReadable(path);
	return CreateLocalStorage(path);
}

62
std::unique_ptr<Storage>
63
CreateConfiguredStorage(const ConfigData &config, EventLoop &event_loop)
64
{
65
	auto uri = config.GetString(ConfigOption::MUSIC_DIR);
66
	if (uri != nullptr && uri_has_scheme(uri))
67
		return CreateConfiguredStorageUri(event_loop, uri);
68

69
	return CreateConfiguredStorageLocal(config);
70
}
71 72

bool
73
IsStorageConfigured(const ConfigData &config) noexcept
74
{
75
	return config.GetParam(ConfigOption::MUSIC_DIR) != nullptr;
76
}