DumpDatabase.cxx 3.63 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 40 41 42 43 44 45

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

#include <stdlib.h>

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

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

61 62
	virtual void OnDatabaseSongRemoved(const char *uri) override {
		cout << "SongRemoved " << uri << endl;
63
	}
64 65
};

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

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

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

int
main(int argc, char **argv)
94
try {
95 96 97 98 99
	if (argc != 3) {
		cerr << "Usage: DumpDatabase CONFIG PLUGIN" << endl;
		return 1;
	}

100
	const Path config_path = Path::FromFS(argv[1]);
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
	AtScopeExit() { config_global_finish(); };
113

114
	Error error;
115
	ReadConfigFile(config_path);
116

117
	TagLoadConfig();
118

119 120 121
	EventLoop event_loop;
	MyDatabaseListener database_listener;

122 123
	/* do it */

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

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

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

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

139
	db->Open();
140

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

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

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

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