DumpDatabase.cxx 3.55 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/Thread.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
class GlobalInit {
47 48
	EventThread io_thread;

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

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

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

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

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

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

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

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

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

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

114
	const Path config_path = Path::FromFS(argv[1]);
115 116 117 118 119 120 121 122 123 124
	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 */

125
	GlobalInit init;
126

127
	ReadConfigFile(config_path);
128

129
	TagLoadConfig();
130

131 132
	MyDatabaseListener database_listener;

133 134
	/* do it */

135
	const auto *path = config_get_param(ConfigOption::DB_FILE);
136
	ConfigBlock block(path != nullptr ? path->line : -1);
137
	if (path != nullptr)
138
		block.AddBlockParam("path", path->value.c_str(), path->line);
139

140
	Database *db = plugin->create(init.GetEventLoop(),
141
				      init.GetEventLoop(),
142
				      database_listener, block);
143

144 145
	AtScopeExit(db) { delete db; };

146
	db->Open();
147

148 149
	AtScopeExit(db) { db->Close(); };

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

152
	db->Visit(selection, DumpDirectory, DumpSong, DumpPlaylist);
153 154

	return EXIT_SUCCESS;
155 156 157 158
 } catch (const std::exception &e) {
	LogError(e);
	return EXIT_FAILURE;
 }