Configured.cxx 2.25 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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
#include "config/Domain.hxx"
26 27 28
#include "fs/StandardDirectory.hxx"
#include "fs/CheckFile.hxx"
#include "util/UriUtil.hxx"
29
#include "util/RuntimeError.hxx"
30 31 32

#include <assert.h>

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

40 41 42 43
	return storage;
}

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

	return path;
}

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

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

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

72
	return CreateConfiguredStorageLocal(config);
73
}
74 75

bool
76
IsStorageConfigured(const ConfigData &config) noexcept
77
{
78
	return config.GetParam(ConfigOption::MUSIC_DIR) != nullptr;
79
}