DumpDatabase.cxx 3.79 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 37
#include <glib.h>

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

#include <stdlib.h>

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

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

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

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

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

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

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

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

110
#if !GLIB_CHECK_VERSION(2,32,0)
111
	g_thread_init(nullptr);
112 113
#endif

114 115 116 117
	/* initialize MPD */

	config_global_init();

118 119 120
	Error error;
	if (!ReadConfigFile(config_path, error)) {
		cerr << error.GetMessage() << endl;
121 122 123
		return EXIT_FAILURE;
	}

124
	TagLoadConfig();
125

126 127 128
	EventLoop event_loop;
	MyDatabaseListener database_listener;

129 130 131
	/* do it */

	const struct config_param *path = config_get_param(CONF_DB_FILE);
132
	config_param param("database", path != nullptr ? path->line : -1);
133
	if (path != nullptr)
134
		param.AddBlockParam("path", path->value.c_str(), path->line);
135

136 137
	Database *db = plugin->create(event_loop, database_listener,
				      param, error);
138 139

	if (db == nullptr) {
140
		cerr << error.GetMessage() << endl;
141 142 143
		return EXIT_FAILURE;
	}

144
	if (!db->Open(error)) {
145
		delete db;
146
		cerr << error.GetMessage() << endl;
147 148 149
		return EXIT_FAILURE;
	}

150
	const DatabaseSelection selection("", true);
151

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

	db->Close();
	delete db;

	/* deinitialize everything */

	config_global_finish();

	return EXIT_SUCCESS;
}