DumpDatabase.cxx 3.84 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 30
#include "config/ConfigGlobal.hxx"
#include "config/ConfigData.hxx"
31
#include "tag/TagConfig.hxx"
32
#include "fs/Path.hxx"
33
#include "event/Loop.hxx"
34
#include "util/Error.hxx"
35

36
#ifdef HAVE_GLIB
37
#include <glib.h>
38
#endif
39

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

#include <stdlib.h>

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

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

	virtual void OnDatabaseSongRemoved(const LightSong &song) override {
		cout << "SongRemoved " << song.GetURI() << endl;
	}
65 66
};

67
static bool
68
DumpDirectory(const LightDirectory &directory, Error &)
69
{
70
	cout << "D " << directory.GetPath() << endl;
71 72 73 74
	return true;
}

static bool
75
DumpSong(const LightSong &song, Error &)
76
{
77
	cout << "S ";
78 79
	if (song.directory != nullptr)
		cout << song.directory << "/";
80
	cout << song.uri << endl;
81 82 83 84
	return true;
}

static bool
85
DumpPlaylist(const PlaylistInfo &playlist,
86
	     const LightDirectory &directory, Error &)
87
{
88 89
	cout << "P " << directory.GetPath()
	     << "/" << playlist.name.c_str() << endl;
90 91 92 93 94 95 96 97 98 99 100
	return true;
}

int
main(int argc, char **argv)
{
	if (argc != 3) {
		cerr << "Usage: DumpDatabase CONFIG PLUGIN" << endl;
		return 1;
	}

101
	const Path config_path = Path::FromFS(argv[1]);
102 103 104 105 106 107 108 109 110 111
	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 GLib */

112
#ifdef HAVE_GLIB
113
#if !GLIB_CHECK_VERSION(2,32,0)
114
	g_thread_init(nullptr);
115
#endif
116 117
#endif

118 119 120 121
	/* initialize MPD */

	config_global_init();

122 123 124
	Error error;
	if (!ReadConfigFile(config_path, error)) {
		cerr << error.GetMessage() << endl;
125 126 127
		return EXIT_FAILURE;
	}

128
	TagLoadConfig();
129

130 131 132
	EventLoop event_loop;
	MyDatabaseListener database_listener;

133 134 135
	/* do it */

	const struct config_param *path = config_get_param(CONF_DB_FILE);
136
	config_param param("database", path != nullptr ? path->line : -1);
137
	if (path != nullptr)
138
		param.AddBlockParam("path", path->value.c_str(), path->line);
139

140 141
	Database *db = plugin->create(event_loop, database_listener,
				      param, error);
142 143

	if (db == nullptr) {
144
		cerr << error.GetMessage() << endl;
145 146 147
		return EXIT_FAILURE;
	}

148
	if (!db->Open(error)) {
149
		delete db;
150
		cerr << error.GetMessage() << endl;
151 152 153
		return EXIT_FAILURE;
	}

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

156
	if (!db->Visit(selection, DumpDirectory, DumpSong, DumpPlaylist,
157
		       error)) {
158 159
		db->Close();
		delete db;
160
		cerr << error.GetMessage() << endl;
161 162 163 164 165 166 167 168 169 170 171 172
		return EXIT_FAILURE;
	}

	db->Close();
	delete db;

	/* deinitialize everything */

	config_global_finish();

	return EXIT_SUCCESS;
}