DumpDatabase.cxx 3.34 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/ConfigGlobal.hxx"
30
#include "config/Param.hxx"
31
#include "config/Block.hxx"
32
#include "tag/Config.hxx"
33
#include "fs/Path.hxx"
34
#include "event/Loop.hxx"
35
#include "Log.hxx"
36
#include "util/ScopeExit.hxx"
37

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

#include <stdlib.h>

46
#ifdef ENABLE_UPNP
Max Kellermann's avatar
Max Kellermann committed
47
#include "input/InputStream.hxx"
48
size_t
49
InputStream::LockRead(void *, size_t)
50 51 52 53 54
{
	return 0;
}
#endif

55 56 57 58 59
class MyDatabaseListener final : public DatabaseListener {
public:
	virtual void OnDatabaseModified() override {
		cout << "DatabaseModified" << endl;
	}
60

61 62
	virtual void OnDatabaseSongRemoved(const char *uri) override {
		cout << "SongRemoved " << uri << endl;
63
	}
64 65
};

66 67
static void
DumpDirectory(const LightDirectory &directory)
68
{
69
	cout << "D " << directory.GetPath() << endl;
70 71
}

72 73
static void
DumpSong(const LightSong &song)
74
{
75
	cout << "S ";
76 77
	if (song.directory != nullptr)
		cout << song.directory << "/";
78
	cout << song.uri << endl;
79 80
}

81 82
static void
DumpPlaylist(const PlaylistInfo &playlist, const LightDirectory &directory)
83
{
84 85
	cout << "P " << directory.GetPath()
	     << "/" << playlist.name.c_str() << endl;
86 87 88 89
}

int
main(int argc, char **argv)
90
try {
91 92 93 94 95
	if (argc != 3) {
		cerr << "Usage: DumpDatabase CONFIG PLUGIN" << endl;
		return 1;
	}

96
	const Path config_path = Path::FromFS(argv[1]);
97 98 99 100 101 102 103 104 105 106 107
	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 */

	config_global_init();
108
	AtScopeExit() { config_global_finish(); };
109

110
	ReadConfigFile(config_path);
111

112
	TagLoadConfig();
113

114 115 116
	EventLoop event_loop;
	MyDatabaseListener database_listener;

117 118
	/* do it */

119
	const auto *path = config_get_param(ConfigOption::DB_FILE);
120
	ConfigBlock block(path != nullptr ? path->line : -1);
121
	if (path != nullptr)
122
		block.AddBlockParam("path", path->value.c_str(), path->line);
123

124
	Database *db = plugin->create(event_loop, database_listener, block);
125

126 127
	AtScopeExit(db) { delete db; };

128
	db->Open();
129

130 131
	AtScopeExit(db) { db->Close(); };

132
	const DatabaseSelection selection("", true);
133

134
	db->Visit(selection, DumpDirectory, DumpSong, DumpPlaylist);
135 136

	return EXIT_SUCCESS;
137 138 139 140
 } catch (const std::exception &e) {
	LogError(e);
	return EXIT_FAILURE;
 }