DumpDatabase.cxx 3.63 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
 * 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 27
#include "db/Selection.hxx"
#include "db/DatabaseListener.hxx"
#include "db/LightDirectory.hxx"
#include "db/LightSong.hxx"
28
#include "db/PlaylistVector.hxx"
29
#include "config/Global.hxx"
30
#include "config/Data.hxx"
31
#include "config/Param.hxx"
32
#include "config/Block.hxx"
33
#include "tag/Config.hxx"
34
#include "fs/Path.hxx"
35
#include "event/Thread.hxx"
36
#include "util/ScopeExit.hxx"
37
#include "util/PrintException.hxx"
38

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

#include <stdlib.h>

47
class GlobalInit {
48 49
	EventThread io_thread;

50 51
public:
	GlobalInit() {
52
		io_thread.Start();
53 54 55 56 57 58
		config_global_init();
	}

	~GlobalInit() {
		config_global_finish();
	}
59 60 61 62

	EventLoop &GetEventLoop() {
		return io_thread.GetEventLoop();
	}
63 64
};

65
#ifdef ENABLE_UPNP
Max Kellermann's avatar
Max Kellermann committed
66
#include "input/InputStream.hxx"
67
size_t
68
InputStream::LockRead(void *, size_t)
69 70 71 72 73
{
	return 0;
}
#endif

74 75 76 77 78
class MyDatabaseListener final : public DatabaseListener {
public:
	virtual void OnDatabaseModified() override {
		cout << "DatabaseModified" << endl;
	}
79

80 81
	virtual void OnDatabaseSongRemoved(const char *uri) override {
		cout << "SongRemoved " << uri << endl;
82
	}
83 84
};

85 86
static void
DumpDirectory(const LightDirectory &directory)
87
{
88
	cout << "D " << directory.GetPath() << endl;
89 90
}

91 92
static void
DumpSong(const LightSong &song)
93
{
94
	cout << "S ";
95 96
	if (song.directory != nullptr)
		cout << song.directory << "/";
97
	cout << song.uri << endl;
98 99
}

100 101
static void
DumpPlaylist(const PlaylistInfo &playlist, const LightDirectory &directory)
102
{
103 104
	cout << "P " << directory.GetPath()
	     << "/" << playlist.name.c_str() << endl;
105 106 107 108
}

int
main(int argc, char **argv)
109
try {
110 111 112 113 114
	if (argc != 3) {
		cerr << "Usage: DumpDatabase CONFIG PLUGIN" << endl;
		return 1;
	}

115
	const Path config_path = Path::FromFS(argv[1]);
116 117 118 119 120 121 122 123 124 125
	const char *const plugin_name = argv[2];

	const DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name);
	if (plugin == NULL) {
		cerr << "No such database plugin: " << plugin_name << endl;
		return EXIT_FAILURE;
	}

	/* initialize MPD */

126
	GlobalInit init;
127

128
	ReadConfigFile(config_path);
129

130 131 132
	const auto &config = GetGlobalConfig();

	TagLoadConfig(config);
133

134 135
	MyDatabaseListener database_listener;

136 137
	/* do it */

138
	const auto *path = config.GetParam(ConfigOption::DB_FILE);
139
	ConfigBlock block(path != nullptr ? path->line : -1);
140
	if (path != nullptr)
141
		block.AddBlockParam("path", path->value, path->line);
142

143
	Database *db = plugin->create(init.GetEventLoop(),
144
				      init.GetEventLoop(),
145
				      database_listener, block);
146

147 148
	AtScopeExit(db) { delete db; };

149
	db->Open();
150

151 152
	AtScopeExit(db) { db->Close(); };

153
	const DatabaseSelection selection("", true);
154

155
	db->Visit(selection, DumpDirectory, DumpSong, DumpPlaylist);
156 157

	return EXIT_SUCCESS;
158 159
} catch (...) {
	PrintException(std::current_exception());
160
	return EXIT_FAILURE;
161
}