DumpDatabase.cxx 3.45 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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"
Max Kellermann's avatar
Max Kellermann committed
21 22
#include "db/Registry.hxx"
#include "db/DatabasePlugin.hxx"
23
#include "db/Interface.hxx"
Max Kellermann's avatar
Max Kellermann committed
24 25 26
#include "db/Selection.hxx"
#include "db/DatabaseListener.hxx"
#include "db/LightDirectory.hxx"
27
#include "song/LightSong.hxx"
28
#include "db/PlaylistVector.hxx"
29
#include "ConfigGlue.hxx"
30
#include "tag/Config.hxx"
31
#include "fs/Path.hxx"
32
#include "event/Thread.hxx"
33
#include "util/ScopeExit.hxx"
34
#include "util/PrintException.hxx"
35

36
#include <stdexcept>
37 38 39 40 41 42 43
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;

#include <stdlib.h>

44
class GlobalInit {
45 46
	EventThread io_thread;

47 48
public:
	GlobalInit() {
49
		io_thread.Start();
50 51
	}

52
	~GlobalInit() = default;
53 54 55 56

	EventLoop &GetEventLoop() {
		return io_thread.GetEventLoop();
	}
57 58
};

59
#ifdef ENABLE_UPNP
Max Kellermann's avatar
Max Kellermann committed
60
#include "input/InputStream.hxx"
61
size_t
62
InputStream::LockRead(void *, size_t)
63 64 65 66 67
{
	return 0;
}
#endif

68 69
class MyDatabaseListener final : public DatabaseListener {
public:
70
	void OnDatabaseModified() noexcept override {
71 72
		cout << "DatabaseModified" << endl;
	}
73

74
	void OnDatabaseSongRemoved(const char *uri) noexcept override {
75
		cout << "SongRemoved " << uri << endl;
76
	}
77 78
};

79 80
static void
DumpDirectory(const LightDirectory &directory)
81
{
82
	cout << "D " << directory.GetPath() << endl;
83 84
}

85 86
static void
DumpSong(const LightSong &song)
87
{
88
	cout << "S ";
89 90
	if (song.directory != nullptr)
		cout << song.directory << "/";
91
	cout << song.uri << endl;
92 93
}

94 95
static void
DumpPlaylist(const PlaylistInfo &playlist, const LightDirectory &directory)
96
{
97 98
	cout << "P " << directory.GetPath()
	     << "/" << playlist.name.c_str() << endl;
99 100 101 102
}

int
main(int argc, char **argv)
103
try {
104 105 106 107 108
	if (argc != 3) {
		cerr << "Usage: DumpDatabase CONFIG PLUGIN" << endl;
		return 1;
	}

109
	const Path config_path = Path::FromFS(argv[1]);
110 111 112
	const char *const plugin_name = argv[2];

	const DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name);
113
	if (plugin == nullptr) {
114 115 116 117 118 119
		cerr << "No such database plugin: " << plugin_name << endl;
		return EXIT_FAILURE;
	}

	/* initialize MPD */

120
	GlobalInit init;
121

122
	const auto config = AutoLoadConfigFile(config_path);
123 124

	TagLoadConfig(config);
125

126 127
	MyDatabaseListener database_listener;

128 129
	/* do it */

130
	const auto *path = config.GetParam(ConfigOption::DB_FILE);
131
	ConfigBlock block(path != nullptr ? path->line : -1);
132
	if (path != nullptr)
133
		block.AddBlockParam("path", path->value, path->line);
134

135 136 137
	auto db = plugin->create(init.GetEventLoop(),
				 init.GetEventLoop(),
				 database_listener, block);
138

139
	db->Open();
140

141
	AtScopeExit(&db) { db->Close(); };
142

143
	const DatabaseSelection selection("", true);
144

145
	db->Visit(selection, DumpDirectory, DumpSong, DumpPlaylist);
146 147

	return EXIT_SUCCESS;
148 149
} catch (...) {
	PrintException(std::current_exception());
150
	return EXIT_FAILURE;
151
}