File: run_input.cxx

package info (click to toggle)
mpd 0.24.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,760 kB
  • sloc: cpp: 75,041; python: 1,408; xml: 628; perl: 469; java: 289; sh: 286; ansic: 235; makefile: 105
file content (260 lines) | stat: -rw-r--r-- 5,330 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project

#include "config.h"
#include "TagSave.hxx"
#include "tag/Tag.hxx"
#include "ConfigGlue.hxx"
#include "input/InputStream.hxx"
#include "input/Init.hxx"
#include "input/Registry.hxx"
#include "input/InputPlugin.hxx"
#include "input/RemoteTagScanner.hxx"
#include "input/ScanTags.hxx"
#include "event/Thread.hxx"
#include "thread/Cond.hxx"
#include "Log.hxx"
#include "LogBackend.hxx"
#include "fs/Path.hxx"
#include "fs/NarrowPath.hxx"
#include "io/BufferedOutputStream.hxx"
#include "io/FileDescriptor.hxx"
#include "io/StdioOutputStream.hxx"
#include "cmdline/OptionDef.hxx"
#include "cmdline/OptionParser.hxx"
#include "util/PrintException.hxx"

#ifdef ENABLE_ARCHIVE
#include "archive/ArchiveList.hxx"
#endif

#include <stdexcept>

#include <unistd.h>
#include <stdlib.h>

static constexpr std::size_t MAX_CHUNK_SIZE = 16384;

struct CommandLine {
	const char *uri = nullptr;

	FromNarrowPath config_path;

	std::size_t seek = 0;

	std::size_t chunk_size = MAX_CHUNK_SIZE;

	bool verbose = false;

	bool scan = false;
};

enum Option {
	OPTION_CONFIG,
	OPTION_VERBOSE,
	OPTION_SCAN,
	OPTION_SEEK,
	OPTION_CHUNK_SIZE,
};

static constexpr OptionDef option_defs[] = {
	{"config", 0, true, "Load a MPD configuration file"},
	{"verbose", 'v', false, "Verbose logging"},
	{"scan", 0, false, "Scan tags instead of reading raw data"},
	{"seek", 0, true, "Start reading at this position"},
	{"chunk-size", 0, true, "Read this number of bytes at a time"},
};

static std::size_t
ParseSize(const char *s)
{
	char *endptr;
	std::size_t value = std::strtoul(s, &endptr, 10);
	if (endptr == s)
		throw std::runtime_error("Failed to parse integer");

	return value;
}

static CommandLine
ParseCommandLine(int argc, char **argv)
{
	CommandLine c;

	OptionParser option_parser(option_defs, argc, argv);
	while (auto o = option_parser.Next()) {
		switch (Option(o.index)) {
		case OPTION_CONFIG:
			c.config_path = o.value;
			break;

		case OPTION_VERBOSE:
			c.verbose = true;
			break;

		case OPTION_SCAN:
			c.scan = true;
			break;

		case OPTION_SEEK:
			c.seek = ParseSize(o.value);
			break;

		case OPTION_CHUNK_SIZE:
			c.chunk_size = ParseSize(o.value);
			if (c.chunk_size <= 0 || c.chunk_size > MAX_CHUNK_SIZE)
				throw std::runtime_error("Invalid chunk size");
			break;
		}
	}

	auto args = option_parser.GetRemaining();
	if (args.size() != 1)
		throw std::runtime_error("Usage: run_input [--verbose] [--config=FILE] [--scan] [--chunk-size=BYTES] URI");

	c.uri = args.front();
	return c;
}

class GlobalInit {
	const ConfigData config;
	EventThread io_thread;

#ifdef ENABLE_ARCHIVE
	const ScopeArchivePluginsInit archive_plugins_init{config};
#endif

	const ScopeInputPluginsInit input_plugins_init{config, io_thread.GetEventLoop()};

public:
	explicit GlobalInit(Path config_path)
		:config(AutoLoadConfigFile(config_path))
	{
		io_thread.Start();
	}
};

static void
tag_save(FILE *file, const Tag &tag)
{
	StdioOutputStream sos(file);
	WithBufferedOutputStream(sos, [&](auto &bos){
		tag_save(bos, tag);
	});
}

static int
dump_input_stream(InputStream &is, FileDescriptor out,
		  offset_type seek, size_t chunk_size)
{
	out.SetBinaryMode();

	std::unique_lock lock{is.mutex};

	if (seek > 0)
		is.Seek(lock, seek);

	/* print meta data */

	if (is.HasMimeType())
		fprintf(stderr, "MIME type: %s\n", is.GetMimeType());

	/* read data and tags from the stream */

	while (!is.IsEOF()) {
		{
			auto tag = is.ReadTag();
			if (tag) {
				fprintf(stderr, "Received a tag:\n");
				tag_save(stderr, *tag);
			}
		}

		std::byte buffer[MAX_CHUNK_SIZE];
		assert(chunk_size <= sizeof(buffer));
		size_t num_read = is.Read(lock, {buffer, chunk_size});
		if (num_read == 0)
			break;

		out.FullWrite({buffer, num_read});
	}

	is.Check();

	return 0;
}

class DumpRemoteTagHandler final : public RemoteTagHandler {
	Mutex mutex;
	Cond cond;

	Tag tag;
	std::exception_ptr error;

	bool done = false;

public:
	Tag Wait() {
		std::unique_lock lock{mutex};
		cond.wait(lock, [this]{ return done; });

		if (error)
			std::rethrow_exception(error);

		return std::move(tag);
	}

	/* virtual methods from RemoteTagHandler */
	void OnRemoteTag(Tag &&_tag) noexcept override {
		const std::scoped_lock lock{mutex};
		tag = std::move(_tag);
		done = true;
		cond.notify_all();
	}

	void OnRemoteTagError(std::exception_ptr e) noexcept override {
		const std::scoped_lock lock{mutex};
		error = std::move(e);
		done = true;
		cond.notify_all();
	}
};

static int
Scan(const char *uri)
{
	DumpRemoteTagHandler handler;

	auto scanner = InputScanTags(uri, handler);
	if (!scanner) {
		fprintf(stderr, "Unsupported URI\n");
		return EXIT_FAILURE;
	}

	scanner->Start();
	tag_save(stdout, handler.Wait());
	return EXIT_SUCCESS;
}

int main(int argc, char **argv)
try {
	const auto c = ParseCommandLine(argc, argv);

	/* initialize MPD */

	SetLogThreshold(c.verbose ? LogLevel::DEBUG : LogLevel::INFO);
	const GlobalInit init(c.config_path);

	if (c.scan)
		return Scan(c.uri);

	/* open the stream and dump it */

	Mutex mutex;
	auto is = InputStream::OpenReady(c.uri, mutex);
	return dump_input_stream(*is, FileDescriptor(STDOUT_FILENO),
				 c.seek, c.chunk_size);
} catch (...) {
	PrintException(std::current_exception());
	return EXIT_FAILURE;
}