Debian Codegraph @

src/ezstream-cfgmigrate.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2018, 2020 Moritz Grimm <[email protected]>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
20
21 #include "compat.h"
22
23 #if defined(HAVE_LIBGEN_H) && !defined(__linux__)
24 # include <libgen.h>
25 #endif /* HAVE_LIBGEN_H && !__linux__ */
26 #include <limits.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include "cfg.h"
32 #include "cfgfile_xml.h"
33 #include "ezconfig0.h"
34 #include "log.h"
35 #include "util.h"
36 #include "xalloc.h"
37
38 static char *v0_cfgfile;
39 static unsigned int cfg_verbosity;
40
41 #define OPTSTRING "0:hv"
42 enum opt_vals {
43 OPT_V0CONFIGFILE = '0',
44 OPT_HELP = 'h',
45 OPT_VERBOSE = 'v',
46 OPT_INVALID = '?'
47 };
48
49 static void _usage(void);
50 static void _usage_help(void);
51 static int _cmdline_parse(int, char *[], int *);
52 static int _url_parse(const char *, char **, char **, char **);
53 static int _parse_ezconfig0(EZCONFIG *);
54
55 static void
56 _usage(void)
57 {
58 fprintf(stderr, "usage: %s [-hv] -0 v0-cfgfile\n",
59 util_get_progname(NULL));
60 }
61
62 static void
63 _usage_help(void)
64 {
65 fprintf(stderr, "\n");
66 fprintf(stderr, " -0 v0-cfgfile migrate from v0-cfgfile (ezstream version 0.x)\n");
67 fprintf(stderr, " -h print this help and exit\n");
68 fprintf(stderr, " -v increase logging verbosity\n");
69 }
70
71 static int
72 _cmdline_parse(int argc, char *argv[], int *ret_p)
73 {
74 v0_cfgfile = NULL;
75 cfg_verbosity = 0;
76 optind = 1;
77 for (;;) {
78 int ch;
79
80 ch = getopt(argc, argv, OPTSTRING);
81 if (0 > ch)
82 break;
83
84 switch (ch) {
85 case OPT_V0CONFIGFILE:
86 v0_cfgfile = optarg;
87 break;
88 case OPT_HELP:
89 _usage();
90 _usage_help();
91 *ret_p = 0;
92 return (-1);
93 case OPT_VERBOSE:
94 cfg_verbosity++;
95 break;
96 case OPT_INVALID:
97 default:
98 _usage();
99 *ret_p = 2;
100 return (-1);
101 }
102 }
103
104 if (!v0_cfgfile) {
105 fprintf(stderr, "-%c must be provided\n",
106 OPT_V0CONFIGFILE);
107 _usage();
108 *ret_p = 2;
109 return (-1);
110 }
111
112 return (0);
113 }
114
115 static int
116 _url_parse(const char *url, char **hostname, char **port,
117 char **mountname)
118 {
119 const char *p1, *p2, *p3;
120 size_t hostsiz, portsiz, mountsiz;
121
122 if (strncasecmp(url, "http://", strlen("http://")) != 0) {
123 log_error("%s: Invalid <url>: Not an HTTP address",
124 v0_cfgfile);
125 return (-1);
126 }
127
128 p1 = url + strlen("http://");
129 p2 = strchr(p1, ':');
130 if (p2 == NULL) {
131 log_error("%s: Invalid <url>: Missing port", v0_cfgfile);
132 return (-1);
133 }
134 hostsiz = (size_t)(p2 - p1) + 1;
135 *hostname = xmalloc(hostsiz);
136 strlcpy(*hostname, p1, hostsiz);
137
138 p2++;
139 p3 = strchr(p2, '/');
140 if (p3 == NULL) {
141 log_error("%s: Invalid <url>: Missing mountpoint or too long port number",
142 v0_cfgfile);
143 xfree(*hostname);
144 return (-1);
145 }
146
147 portsiz = (size_t)(p3 - p2) + 1;
148 *port = xmalloc(portsiz);
149 strlcpy(*port, p2, portsiz);
150
151 mountsiz = strlen(p3) + 1;
152 *mountname = xmalloc(mountsiz);
153 strlcpy(*mountname, p3, mountsiz);
154
155 return (0);
156 }
157
158 #define ENTITY_SET(e, el, func, ctx, val) do { \
159 const char *err_str2; \
160 \
161 if (0 > (func)((e), (el), (val), &err_str2)) { \
162 log_warning("%s: %s: %s: %s", v0_cfgfile, (ctx), \
163 err_str2, (val)); \
164 warnings++; \
165 } \
166 } while (0)
167
168 static int
169 _parse_ezconfig0(EZCONFIG *ez)
170 {
171 char *hostname, *port, *mountname;
172 const char *err_str;
173 int warnings = 0;
174 cfg_server_list_t srv_list = cfg_get_servers();
175 cfg_server_t srv = cfg_server_list_get(srv_list, CFG_DEFAULT);
176 cfg_stream_list_t str_list = cfg_get_streams();
177 cfg_stream_t str = cfg_stream_list_get(str_list, CFG_DEFAULT);
178 cfg_intake_list_t in_list = cfg_get_intakes();
179 cfg_intake_t in = cfg_intake_list_get(in_list, CFG_DEFAULT);
180 cfg_encoder_list_t enc_list = cfg_get_encoders();
181 cfg_decoder_list_t dec_list = cfg_get_decoders();;
182 unsigned int i;
183 char strbuf[BUFSIZ];
184
185 if (NULL == ez->URL || 0 == strlen(ez->URL)) {
186 log_error("%s: Missing <url> -- not an ezstream version 0.x configuration?",
187 v0_cfgfile);
188 return (-1);
189 }
190 if (0 > _url_parse(ez->URL, &hostname, &port, &mountname))
191 return (-1);
192 ENTITY_SET(srv, srv_list, cfg_server_set_protocol, "<url>", "HTTP");
193 ENTITY_SET(srv, srv_list, cfg_server_set_hostname, "<url>", hostname);
194 xfree(hostname);
195 ENTITY_SET(srv, srv_list, cfg_server_set_port, "<url>", port);
196 xfree(port);
197 ENTITY_SET(str, str_list, cfg_stream_set_mountpoint, "<url>",
198 mountname);
199 xfree(mountname);
200
201 if (ez->username)
202 ENTITY_SET(srv, srv_list, cfg_server_set_user,
203 "<sourceuser>", ez->username);
204 if (ez->password)
205 ENTITY_SET(srv, srv_list, cfg_server_set_password,
206 "<sourcepassword>", ez->password);
207 if (ez->format) {
208 if (0 == strcasecmp(ez->format, "vorbis") ||
209 0 == strcasecmp(ez->format, "theora"))
210 ENTITY_SET(str, str_list, cfg_stream_set_format,
211 "<format>", "Ogg");
212 else
213 ENTITY_SET(str, str_list, cfg_stream_set_format,
214 "<format>", ez->format);
215 }
216 if (ez->fileName) {
217 if (0 == strcasecmp(ez->fileName, "stdin"))
218 ENTITY_SET(in, in_list, cfg_intake_set_type,
219 "<filename>", "stdin");
220 else
221 ENTITY_SET(in, in_list, cfg_intake_set_filename,
222 "<filename>", ez->fileName);
223 }
224 if (ez->metadataProgram) {
225 if (0 > cfg_set_metadata_program(ez->metadataProgram,
226 &err_str)) {
227 log_warning("%s: %s: %s: %s", v0_cfgfile,
228 "<metadata_progname>", err_str,
229 ez->metadataProgram);
230 warnings++;
231 }
232 }
233 if (ez->metadataFormat) {
234 if (0 > cfg_set_metadata_format_str(ez->metadataFormat,
235 &err_str)) {
236 log_warning("%s: %s: %s: %s", v0_cfgfile,
237 "<metadata_format>", err_str, ez->metadataFormat);
238 warnings++;
239 }
240 }
241 if (ez->metadataRefreshInterval) {
242 snprintf(strbuf, sizeof(strbuf), "%d",
243 ez->metadataRefreshInterval);
244 if (0 > cfg_set_metadata_refresh_interval(strbuf, &err_str)) {
245 log_warning("%s: %s: %s: %s", v0_cfgfile,
246 "<metadata_refreshinterval>", err_str, strbuf);
247 warnings++;
248 }
249 }
250 if (ez->fileNameIsProgram)
251 ENTITY_SET(in, in_list, cfg_intake_set_type,
252 "playlist_program", "program");
253 snprintf(strbuf, sizeof(strbuf), "%d", ez->shuffle);
254 ENTITY_SET(in, in_list, cfg_intake_set_shuffle,
255 "<shuffle>", strbuf);
256 snprintf(strbuf, sizeof(strbuf), "%d", ez->streamOnce);
257 ENTITY_SET(in, in_list, cfg_intake_set_stream_once,
258 "<stream_once>", strbuf);
259 snprintf(strbuf, sizeof(strbuf), "%u", ez->reconnectAttempts);
260 ENTITY_SET(srv, srv_list, cfg_server_set_reconnect_attempts,
261 "<reconnect_tries>", strbuf);
262 if (ez->serverName)
263 ENTITY_SET(str, str_list, cfg_stream_set_stream_name,
264 "<svrinfoname>", ez->serverName);
265 if (ez->serverURL)
266 ENTITY_SET(str, str_list, cfg_stream_set_stream_url,
267 "<svrinfourl>", ez->serverURL);
268 if (ez->serverGenre)
269 ENTITY_SET(str, str_list, cfg_stream_set_stream_genre,
270 "<svrinfogenre>", ez->serverGenre);
271 if (ez->serverDescription)
272 ENTITY_SET(str, str_list, cfg_stream_set_stream_description,
273 "<svrinfodescription>", ez->serverDescription);
274 if (ez->serverBitrate)
275 ENTITY_SET(str, str_list, cfg_stream_set_stream_bitrate,
276 "<svrinfobitrate>", ez->serverBitrate);
277 if (ez->serverChannels)
278 ENTITY_SET(str, str_list, cfg_stream_set_stream_channels,
279 "<svrinfochannels>", ez->serverChannels);
280 if (ez->serverSamplerate)
281 ENTITY_SET(str, str_list, cfg_stream_set_stream_samplerate,
282 "<svrinfosamplerate>", ez->serverSamplerate);
283 if (ez->serverQuality)
284 ENTITY_SET(str, str_list, cfg_stream_set_stream_quality,
285 "<svrinfoquality>", ez->serverQuality);
286 ENTITY_SET(str, str_list, cfg_stream_set_public,
287 "<svrinfopublic>", ez->serverPublic ? "yes" : "no");
288 if (ez->reencode) {
289 if (0 == strcasecmp(ez->format, "vorbis") ||
290 0 == strcasecmp(ez->format, "theora"))
291 ENTITY_SET(str, str_list, cfg_stream_set_encoder,
292 "<reencode>", "Ogg");
293 else
294 ENTITY_SET(str, str_list, cfg_stream_set_encoder,
295 "<reencode>", ez->format);
296 }
297
298 for (i = 0; i < MAX_FORMAT_ENCDEC; i++) {
299 FORMAT_ENCDEC *ed = ez->encoderDecoders[i];
300
301 if (!ed)
302 continue;
303
304 if (ed->encoder) {
305 cfg_encoder_t enc = cfg_encoder_list_get(enc_list,
306 CFG_DEFAULT);
307
308 ENTITY_SET(enc, enc_list, cfg_encoder_set_program,
309 "<encode>", ed->encoder);
310 if (ed->format) {
311 if (0 == strcasecmp(ed->format, "vorbis") ||
312 0 == strcasecmp(ed->format, "theora")) {
313 ENTITY_SET(enc, enc_list,
314 cfg_encoder_set_name,
315 "<format> (encoder)",
316 "Ogg");
317 ENTITY_SET(enc, enc_list,
318 cfg_encoder_set_format_str,
319 "<format> (encoder)",
320 "Ogg");
321 } else {
322 ENTITY_SET(enc, enc_list,
323 cfg_encoder_set_name,
324 "<format> (encoder)",
325 ed->format);
326 ENTITY_SET(enc, enc_list,
327 cfg_encoder_set_format_str,
328 "<format> (encoder)",
329 ed->format);
330 }
331 }
332 if (0 > cfg_encoder_validate(enc, &err_str)) {
333 log_warning("%s: %s: %s", v0_cfgfile,
334 "<encdec> (encoder)", err_str);
335 cfg_encoder_list_remove(enc_list, &enc);
336 warnings++;
337 }
338 }
339 if (ed->decoder) {
340 cfg_decoder_t dec = NULL;
341
342 if (ed->format) {
343 if (0 == strcasecmp(ed->format, "vorbis") ||
344 0 == strcasecmp(ed->format, "theora"))
345 dec = cfg_decoder_list_find(dec_list,
346 "Ogg");
347 else
348 dec = cfg_decoder_list_find(dec_list,
349 ed->format);
350 }
351 if (NULL == dec)
352 dec = cfg_decoder_list_get(dec_list, CFG_DEFAULT);
353
354 ENTITY_SET(dec, dec_list, cfg_decoder_set_program,
355 "<decode>", ed->decoder);
356 if (ed->format) {
357 if (0 == strcasecmp(ed->format, "vorbis") ||
358 0 == strcasecmp(ed->format, "theora"))
359 ENTITY_SET(dec, dec_list,
360 cfg_decoder_set_name,
361 "<format> (decoder)",
362 "Ogg");
363 else
364 ENTITY_SET(dec, dec_list,
365 cfg_decoder_set_name,
366 "<format> (decoder)",
367 ed->format);
368 }
369 if (ed->match)
370 ENTITY_SET(dec, dec_list, cfg_decoder_add_match,
371 "<match>", ed->match);
372
373 if (0 > cfg_decoder_validate(dec, &err_str)) {
374 log_warning("%s: %s: %s", v0_cfgfile,
375 "<encdec> (decoder)", err_str);
376 cfg_decoder_list_remove(dec_list, &dec);
377 warnings++;
378 }
379 }
380 }
381
382 if (warnings)
383 log_warning("%s: %u warnings", v0_cfgfile, warnings);
384
385 if (cfg_stream_get_encoder(str) &&
386 NULL == cfg_encoder_list_find(enc_list,
387 cfg_stream_get_encoder(str))) {
388 log_warning("%s: %s encoder not found; disabling reencoding",
389 v0_cfgfile, cfg_stream_get_encoder(str));
390 cfg_stream_set_encoder(str, NULL, NULL, NULL);
391 }
392
393 if (0 > cfg_server_validate(srv, &err_str) ||
394 0 > cfg_stream_validate(str, &err_str) ||
395 0 > cfg_intake_validate(in, &err_str)) {
396 log_error("%s: configuration invalid: %s", v0_cfgfile,
397 err_str);
398 return (-1);
399 }
400
401 return (0);
402 }
403
404 int
405 main(int argc, char *argv[])
406 {
407 int ret;
408 EZCONFIG *ez = NULL;
409
410 ret = 1;
411 if (0 > cfg_init() ||
412 0 > log_init(util_get_progname(argv[0])) ||
413 0 > _cmdline_parse(argc, argv, &ret))
414 return (ret);
415 (void)cfg_set_program_name(util_get_progname(argv[0]), NULL);
416
417 if (0 > parseConfig(v0_cfgfile))
418 goto error;
419 ez = getEZConfig();
420
421 if (0 > _parse_ezconfig0(ez))
422 goto error;
423
424 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
425 printf("<!--\n");
426 printf(" This ezstream version 1.x configuration file was generated by\n");
427 printf(" %s.\n\n", util_get_progname(argv[0]));
428 printf(" Source (ezstream version 0.x):\n");
429 printf(" %s\n", basename(v0_cfgfile));
430 printf(" -->\n\n");
431 cfgfile_xml_print(stdout);
432
433 freeConfig(ez);
434
435 log_exit();
436 cfg_exit();
437
438 return (0);
439
440 error:
441 if (ez)
442 freeConfig(ez);
443
444 log_exit();
445 cfg_exit();
446
447 return (1);
448 }