DumpDatabase.cxx 3.7 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 38 39 40 41 42 43 44

#include <iostream>
using std::cout;
using std::cerr;
using std::endl;

#include <stdlib.h>

45
#ifdef ENABLE_UPNP
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
	return true;
}

int
main(int argc, char **argv)
93
try {
94 95 96 97 98
	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 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 MPD */

	config_global_init();

112
	Error error;
113
	ReadConfigFile(config_path);
114

115
	TagLoadConfig();
116

117 118 119
	EventLoop event_loop;
	MyDatabaseListener database_listener;

120 121
	/* do it */

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

127
	Database *db = plugin->create(event_loop, database_listener,
128
				      block, error);
129 130

	if (db == nullptr) {
131
		cerr << error.GetMessage() << endl;
132 133 134
		return EXIT_FAILURE;
	}

135
	if (!db->Open(error)) {
136
		delete db;
137
		cerr << error.GetMessage() << endl;
138 139 140
		return EXIT_FAILURE;
	}

141
	const DatabaseSelection selection("", true);
142

143
	if (!db->Visit(selection, DumpDirectory, DumpSong, DumpPlaylist,
144
		       error)) {
145 146
		db->Close();
		delete db;
147
		cerr << error.GetMessage() << endl;
148 149 150 151 152 153 154 155 156 157 158
		return EXIT_FAILURE;
	}

	db->Close();
	delete db;

	/* deinitialize everything */

	config_global_finish();

	return EXIT_SUCCESS;
159 160 161 162
 } catch (const std::exception &e) {
	LogError(e);
	return EXIT_FAILURE;
 }