DumpDatabase.cxx 3.64 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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/TagConfig.hxx"
33
#include "fs/Path.hxx"
34
#include "event/Loop.hxx"
35
#include "Log.hxx"
36
#include "util/Error.hxx"
37
#include "util/ScopeExit.hxx"
38

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

#include <stdlib.h>

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

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

62 63
	virtual void OnDatabaseSongRemoved(const char *uri) override {
		cout << "SongRemoved " << uri << endl;
64
	}
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
	return true;
}

int
main(int argc, char **argv)
95
try {
96 97 98 99 100
	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 112
	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();
113
	AtScopeExit() { config_global_finish(); };
114

115
	Error error;
116
	ReadConfigFile(config_path);
117

118
	TagLoadConfig();
119

120 121 122
	EventLoop event_loop;
	MyDatabaseListener database_listener;

123 124
	/* do it */

125
	const auto *path = config_get_param(ConfigOption::DB_FILE);
126
	ConfigBlock block(path != nullptr ? path->line : -1);
127
	if (path != nullptr)
128
		block.AddBlockParam("path", path->value.c_str(), path->line);
129

130
	Database *db = plugin->create(event_loop, database_listener,
131
				      block, error);
132 133

	if (db == nullptr) {
134
		cerr << error.GetMessage() << endl;
135 136 137
		return EXIT_FAILURE;
	}

138 139
	AtScopeExit(db) { delete db; };

140
	db->Open();
141

142 143
	AtScopeExit(db) { db->Close(); };

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

146
	if (!db->Visit(selection, DumpDirectory, DumpSong, DumpPlaylist,
147 148
		       error)) {
		cerr << error.GetMessage() << endl;
149 150 151 152
		return EXIT_FAILURE;
	}

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