DumpDatabase.cxx 3.47 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
 * 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 "fs/NarrowPath.hxx"
33
#include "event/Thread.hxx"
34
#include "util/ScopeExit.hxx"
35
#include "util/PrintException.hxx"
36

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

#include <stdlib.h>

45
class GlobalInit {
46 47
	EventThread io_thread;

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

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

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

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

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

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

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

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

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

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

110
	const FromNarrowPath config_path = argv[1];
111 112 113
	const char *const plugin_name = argv[2];

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

	/* initialize MPD */

121
	GlobalInit init;
122

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

	TagLoadConfig(config);
126

127 128
	MyDatabaseListener database_listener;

129 130
	/* do it */

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

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

140
	db->Open();
141

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

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

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

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