DumpDatabase.cxx 3.46 KB
Newer Older
1
/*
2
 * Copyright 2003-2019 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
#include "db/Selection.hxx"
#include "db/DatabaseListener.hxx"
#include "db/LightDirectory.hxx"
27
#include "song/LightSong.hxx"
28
#include "db/PlaylistVector.hxx"
29
#include "ConfigGlue.hxx"
30
#include "tag/Config.hxx"
31
#include "fs/Path.hxx"
32
#include "fs/NarrowPath.hxx"
33
#include "event/Thread.hxx"
34
#include "util/ScopeExit.hxx"
35
#include "util/PrintException.hxx"
36

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

#include <stdlib.h>

45
class GlobalInit {
46 47
	EventThread io_thread;

48 49
public:
	GlobalInit() {
50
		io_thread.Start();
51 52 53 54
	}

	~GlobalInit() {
	}
55 56 57 58

	EventLoop &GetEventLoop() {
		return io_thread.GetEventLoop();
	}
59 60
};

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

70 71 72 73 74
class MyDatabaseListener final : public DatabaseListener {
public:
	virtual void OnDatabaseModified() override {
		cout << "DatabaseModified" << endl;
	}
75

76 77
	virtual void OnDatabaseSongRemoved(const char *uri) override {
		cout << "SongRemoved " << uri << endl;
78
	}
79 80
};

81 82
static void
DumpDirectory(const LightDirectory &directory)
83
{
84
	cout << "D " << directory.GetPath() << endl;
85 86
}

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

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

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

111
	const FromNarrowPath config_path = argv[1];
112 113 114 115 116 117 118 119 120 121
	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 */

122
	GlobalInit init;
123

124
	const auto config = AutoLoadConfigFile(config_path);
125 126

	TagLoadConfig(config);
127

128 129
	MyDatabaseListener database_listener;

130 131
	/* do it */

132
	const auto *path = config.GetParam(ConfigOption::DB_FILE);
133
	ConfigBlock block(path != nullptr ? path->line : -1);
134
	if (path != nullptr)
135
		block.AddBlockParam("path", path->value, path->line);
136

137 138 139
	auto db = plugin->create(init.GetEventLoop(),
				 init.GetEventLoop(),
				 database_listener, block);
140

141
	db->Open();
142

143
	AtScopeExit(&db) { db->Close(); };
144

145
	const DatabaseSelection selection("", true);
146

147
	db->Visit(selection, DumpDirectory, DumpSong, DumpPlaylist);
148 149

	return EXIT_SUCCESS;
150 151
} catch (...) {
	PrintException(std::current_exception());
152
	return EXIT_FAILURE;
153
}