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

#include <assert.h>

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

41 42 43 44
	return storage;
}

static AllocatedPath
45
GetConfiguredMusicDirectory()
46
{
47 48
	AllocatedPath path = config_get_path(ConfigOption::MUSIC_DIR);
	if (path.IsNull())
49 50 51 52 53
		path = GetUserMusicDir();

	return path;
}

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

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

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

73
	return CreateConfiguredStorageLocal();
74
}
75 76

bool
77
IsStorageConfigured() noexcept
78
{
79
	return config_get_string(ConfigOption::MUSIC_DIR) != nullptr;
80
}