Change the type of the argv argument of all commands.
[paraslash.git] / mysql_selector.c
1 /*
2 * Copyright (C) 1999-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file mysql_selector.c para_server's mysql-based audio file selector */
8
9 /** \cond some internal constants */
10 #define MEDIUM_BLOB_SIZE 16777220 /* (2**24 + 4) */
11 #define BLOB_SIZE 65539 /* (2**16 + 3) */
12 /** \endcond */
13
14 #include "para.h"
15 #include "server.cmdline.h"
16 #include "afh.h"
17 #include "server.h"
18 #include "vss.h"
19 #include "afs_common.h"
20 #include <mysql/mysql.h>
21 #include <mysql/mysql_version.h>
22 #include <regex.h>
23 #include "error.h"
24 #include "net.h"
25 #include "string.h"
26 #include "list.h"
27 #include "user_list.h"
28 #include "mysql_selector_command_list.h"
29 #include "ipc.h"
30
31 /** pointer to the shared memory area */
32 extern struct misc_meta_data *mmd;
33
34 static void *mysql_ptr = NULL;
35 static int mysql_lock;
36
37 /**
38 * contains name/replacement pairs used by s_a_r_list()
39 *
40 * \sa s_a_r()
41 */
42 struct para_macro {
43 /** the name of the macro */
44 const char *name;
45 /** the replacement text */
46 const char *replacement;
47 };
48
49 static const struct para_macro mysql_macro_list[] = {
50 { .name = "IS_N_SET",
51 .replacement = "(data.%s != '1')"
52 }, {
53 .name = "IS_SET",
54 .replacement = "(data.%s = '1')"
55 }, {
56 .name = "PICID",
57 .replacement = "%sdata.Pic_Id"
58 }, {
59 .name = "NAME_LIKE",
60 .replacement = "(data.name like '%s')"
61 }, {
62 .name = "LASTPLAYED",
63 .replacement = "%sFLOOR((UNIX_TIMESTAMP(now())"
64 "-UNIX_TIMESTAMP(data.Lastplayed))/60)"
65 }, {
66 .name = "NUMPLAYED",
67 .replacement = "%sdata.Numplayed"
68 }, {
69 .name = NULL,
70 }
71 };
72
73 /**
74 * Simple search and replace routine.
75 *
76 * \param src Source String.
77 * \param macro_name The name of the macro.
78 * \param replacement The replacement format string.
79 *
80 * In \p src, replace each occurence of \p macro_name(arg) by the string
81 * determined by the \p replacement format string. \p replacement may (but
82 * needs not) contain a single string conversion specifier (%s) which gets
83 * replaced by \p arg.
84 *
85 * \return A string in which all matches in \p src are replaced, or \p NULL if
86 * an error was encountered. Caller must free the result.
87 *
88 * \sa regcomp(3)
89 */
90 __must_check __malloc static char *s_a_r(const char *src, const char* macro_name,
91 const char *replacement)
92 {
93 regex_t preg;
94 size_t nmatch = 1;
95 regmatch_t pmatch[1];
96 int eflags = 0;
97 char *dest = NULL;
98 const char *bufptr = src;
99
100 if (!macro_name || !replacement || !src)
101 return para_strdup(src);
102 if (regcomp(&preg, macro_name, 0) != 0)
103 return NULL;
104 while (regexec(&preg, bufptr, nmatch, pmatch, eflags)
105 != REG_NOMATCH) {
106 char *tmp, *arg, *o_bracket, *c_bracket;
107
108 o_bracket = strchr(bufptr + pmatch[0].rm_so, '(');
109 c_bracket = o_bracket? strchr(o_bracket, ')') : NULL;
110 if (!c_bracket)
111 goto out;
112 tmp = para_strdup(bufptr);
113 tmp[pmatch[0].rm_so] = '\0';
114 dest = para_strcat(dest, tmp);
115 free(tmp);
116
117 arg = para_strdup(o_bracket + 1);
118 arg[c_bracket - o_bracket - 1] = '\0';
119 tmp = make_message(replacement, arg);
120 free(arg);
121 dest = para_strcat(dest, tmp);
122 free(tmp);
123 bufptr = c_bracket;
124 bufptr++;
125 }
126 dest = para_strcat(dest, bufptr);
127 // PARA_DEBUG_LOG("%s: returning %s\n", __func__, dest);
128 out:
129 regfree(&preg);
130 return dest;
131 }
132
133 /**
134 * replace a string according to a list of macros
135 *
136 * \param macro_list the array containing a macro/replacement pairs.
137 * \param src the source string
138 *
139 * This function just calls s_a_r() for each element of \p macro_list.
140 *
141 * \return \p NULL if one of the underlying calls to \p s_a_r returned \p NULL.
142 * Otherwise the completely expanded version of \p src is returned.
143 */
144 __must_check __malloc static char *s_a_r_list(const struct para_macro *macro_list,
145 char *src)
146 {
147 const struct para_macro *mp = macro_list;
148 char *ret = NULL, *tmp = para_strdup(src);
149
150 while (mp->name) {
151 ret = s_a_r(tmp, mp->name, mp->replacement);
152 free(tmp);
153 if (!ret) /* syntax error */
154 return NULL;
155 tmp = ret;
156 mp++;
157 }
158 //PARA_DEBUG_LOG("%s: returning %s\n", __func__, dest);
159 return ret;
160 }
161
162 static int lockless_real_query(const char *query)
163 {
164 if (!mysql_ptr)
165 return -E_NOTCONN;
166 PARA_DEBUG_LOG("%s\n", query);
167 if (mysql_real_query(mysql_ptr, query, strlen(query))) {
168 PARA_ERROR_LOG("real_query error (%s)\n",
169 mysql_error(mysql_ptr));
170 return -E_QFAILED;
171 }
172 return 1;
173 }
174
175 static int real_query(const char *query)
176 {
177 int ret;
178
179 mutex_lock(mysql_lock);
180 ret = lockless_real_query(query);
181 mutex_unlock(mysql_lock);
182 return ret;
183 }
184
185 /*
186 * Use open connection given by mysql_ptr to query server. Returns a
187 * result pointer on succes and NULL on errors
188 */
189 static struct MYSQL_RES *get_result(const char *query)
190 {
191 void *result = NULL;
192
193 mutex_lock(mysql_lock);
194 if (lockless_real_query(query) < 0)
195 goto out;
196 result = mysql_store_result(mysql_ptr);
197 if (!result)
198 PARA_ERROR_LOG("%s", "store_result error\n");
199 out:
200 mutex_unlock(mysql_lock);
201 return result;
202 }
203 /*
204 * write input from fd to dynamically allocated char array,
205 * but maximal max_size byte. Return size.
206 */
207 static int fd2buf(int fd, char **buf_ptr, size_t max_size)
208 {
209 const size_t chunk_size = 1024;
210 size_t size = 2048;
211 char *buf = para_malloc(size * sizeof(char)), *p = buf;
212 int ret;
213
214 while ((ret = recv_bin_buffer(fd, p, chunk_size)) > 0) {
215 p += ret;
216 if ((p - buf) + chunk_size >= size) {
217 char *tmp;
218
219 size *= 2;
220 if (size > max_size) {
221 ret = -E_TOOBIG;
222 goto out;
223 }
224 tmp = para_realloc(buf, size);
225 p = (p - buf) + tmp;
226 buf = tmp;
227 }
228 }
229 if (ret < 0)
230 goto out;
231 *buf_ptr = buf;
232 ret = p - buf;
233 out:
234 if (ret < 0 && buf)
235 free(buf);
236 return ret;
237 }
238
239 static char *escape_blob(const char* old, size_t size)
240 {
241 char *new;
242
243 if (!mysql_ptr)
244 return NULL;
245 new = para_malloc(2 * size * sizeof(char) + 1);
246 mysql_real_escape_string(mysql_ptr, new, old, size);
247 return new;
248 }
249
250 static char *escape_str(const char* old)
251 {
252 return escape_blob(old, strlen(old));
253 }
254
255 static char *escaped_basename(const char *name)
256 {
257 char *esc, *bn = para_basename(name);
258
259 if (!bn)
260 return NULL;
261 esc = escape_str(bn);
262 free(bn);
263 return esc;
264 }
265
266 /*
267 * new attribute
268 */
269 int com_na(__a_unused int fd, int argc, char * const * argv)
270 {
271 char *q, *tmp;
272 int ret;
273
274 if (argc < 2)
275 return -E_MYSQL_SYNTAX;
276 tmp = escape_str(argv[1]);
277 if (!tmp)
278 return -E_ESCAPE;
279 q = make_message("alter table data add %s char(1) "
280 "not null default 0", tmp);
281 free(tmp);
282 ret = real_query(q);
283 free(q);
284 return ret;
285 }
286
287 /*
288 * delete attribute
289 */
290 int com_da(__a_unused int fd, int argc, char * const * argv)
291 {
292 char *q, *tmp;
293 int ret;
294
295 if (argc < 2)
296 return -E_MYSQL_SYNTAX;
297 tmp = escape_str(argv[1]);
298 if (!tmp)
299 return -E_ESCAPE;
300 q = make_message("alter table data drop %s", tmp);
301 free(tmp);
302 ret = real_query(q);
303 free(q);
304 return ret;
305 }
306
307 /* stradd/pic_add */
308 static int com_stradd_picadd(int fd, int argc, char * const * argv)
309 {
310 char *blob = NULL, *esc_blob = NULL, *q = NULL, *tmp = NULL;
311 const char *fmt, *del_fmt;
312 int ret, stradd = strcmp(argv[0], "picadd");
313 size_t size;
314
315 if (argc < 2)
316 return -E_MYSQL_SYNTAX;
317 if (strlen(argv[1]) >= MAXLINE - 1)
318 return -E_NAMETOOLONG;
319 if (!mysql_ptr)
320 return -E_NOTCONN;
321 if (stradd) {
322 size = BLOB_SIZE;
323 fmt = "insert into streams (name, def) values ('%s','%s')";
324 del_fmt="delete from streams where name='%s'";
325 } else {
326 size = MEDIUM_BLOB_SIZE;
327 fmt = "insert into pics (name, pic) values ('%s','%s')";
328 del_fmt="delete from pics where pic='%s'";
329 }
330 tmp = escape_str(argv[1]);
331 if (!tmp)
332 return -E_ESCAPE;
333 q = make_message(del_fmt, tmp);
334 free(tmp);
335 ret = real_query(q);
336 free(q);
337 if (ret < 0)
338 return ret;
339 if ((ret = send_buffer(fd, AWAITING_DATA_MSG) < 0))
340 return ret;
341 if ((ret = fd2buf(fd, &blob, size)) < 0)
342 return ret;
343 size = ret;
344 if (stradd)
345 blob[size] = '\0';
346 ret = -E_ESCAPE;
347 esc_blob = escape_blob(blob, size);
348 if (!esc_blob)
349 goto out;
350 tmp = escape_str(argv[1]);
351 if (!tmp)
352 goto out;
353 q = make_message(fmt, tmp, esc_blob);
354 ret = real_query(q);
355 out:
356 free(blob);
357 free(esc_blob);
358 free(tmp);
359 free(q);
360 return ret;
361 }
362
363 /* stradd */
364 int com_stradd(int fd, int argc, char * const * argv)
365 {
366 return com_stradd_picadd(fd, argc, argv);
367 }
368
369 /* pic_add */
370 int com_picadd(int fd, int argc, char * const * argv)
371 {
372 return com_stradd_picadd(fd, argc, argv);
373 }
374
375 /*
376 * print results to fd
377 */
378 static int print_results(int fd, void *result,
379 my_ulonglong top, my_ulonglong left,
380 my_ulonglong bottom, my_ulonglong right)
381 {
382 unsigned int i,j;
383 int ret;
384 MYSQL_ROW row;
385
386 for (i = top; i <= bottom; i++) {
387 row = mysql_fetch_row(result);
388 if (!row || !row[0])
389 return -E_NOROW;
390 for (j = left; j <= right; j++) {
391 ret = send_va_buffer(fd, j == left? "%s" : "\t%s",
392 row[j]? row[j] : "NULL");
393 if (ret < 0)
394 return ret;
395 }
396 ret = send_buffer(fd, "\n");
397 if (ret < 0)
398 return ret;
399 }
400 return 0;
401 }
402
403 /*
404 * verbatim
405 */
406 int com_verb(int fd, int argc, char * const * argv)
407 {
408 void *result = NULL;
409 int ret;
410 my_ulonglong num_rows, num_fields, top = 0, left = 0;
411 char *tmp;
412
413 if (argc < 2)
414 return -E_MYSQL_SYNTAX;
415 tmp = escape_str(argv[1]);
416 if (!tmp)
417 return -E_ESCAPE;
418 result = get_result(tmp);
419 free(tmp);
420 if (!result)
421 /* return success, because it's ok to have no results */
422 return 1;
423 num_fields = mysql_field_count(mysql_ptr);
424 num_rows = mysql_num_rows(result);
425 ret = 1;
426 if (num_fields && num_rows)
427 ret = print_results(fd, result, top, left, num_rows - 1,
428 num_fields - 1);
429 mysql_free_result(result);
430 return ret;
431 }
432
433 /* returns NULL on errors or if there are no atts defined yet */
434 static void *get_all_attributes(void)
435 {
436 void *result = get_result("desc data");
437 unsigned int num_rows;
438
439 if (!result)
440 return NULL;
441 num_rows = mysql_num_rows(result);
442 if (num_rows < 5) {
443 mysql_free_result(result);
444 return NULL;
445 }
446 mysql_data_seek(result, (my_ulonglong)4); /* skip Lastplayed, Numplayed... */
447 return result;
448 }
449
450 /*
451 * list all attributes
452 */
453 int com_laa(int fd, int argc, __a_unused char * const * argv)
454 {
455 void *result;
456 int ret;
457 my_ulonglong top = 0, left = 0, bottom, right = 0;
458
459 if (argc != 1)
460 return -E_MYSQL_SYNTAX;
461 result = get_all_attributes();
462 if (!result)
463 return -E_NOATTS;
464 bottom = mysql_num_rows(result);
465 if (bottom < 5)
466 return -E_MYSQL_SYNTAX;
467 bottom -= 5;
468 ret = print_results(fd, result, top, left, bottom, right);
469 mysql_free_result(result);
470 return ret;
471 }
472
473 /*
474 * history
475 */
476 int com_hist(int fd, int argc, char * const * argv)
477 {
478 int ret;
479 void *result = NULL;
480 char *q, *atts;
481 my_ulonglong num_rows, top = 0, left = 0, right = 1;
482
483 if (argc > 3)
484 return -E_MYSQL_SYNTAX;
485 if (argc > 1) {
486 char *tmp = escape_str(argv[1]);
487 if (!tmp)
488 return -E_ESCAPE;
489 atts = make_message("where %s = '1'", tmp);
490 free(tmp);
491 } else
492 atts = para_strdup(NULL);
493
494 q = make_message("select name, to_days(now()) - to_days(lastplayed) from "
495 "data %s order by lastplayed", atts);
496 free(atts);
497 result = get_result(q);
498 free(q);
499 if (!result)
500 return -E_NORESULT;
501 num_rows = mysql_num_rows(result);
502 ret = 1;
503 if (num_rows)
504 ret = print_results(fd, result, top, left, num_rows - 1, right);
505 mysql_free_result(result);
506 return ret;
507 }
508
509 /*
510 * get last num audio files
511 */
512 int com_last(int fd, int argc, char * const * argv)
513 {
514 void *result = NULL;
515 char *q;
516 int num, ret;
517 my_ulonglong top = 0, left = 0, right = 0;
518
519 if (argc < 2)
520 num = 10;
521 else
522 num = atoi(argv[1]);
523 if (!num)
524 return -E_MYSQL_SYNTAX;
525 q = make_message("select name from data order by lastplayed desc "
526 "limit %u", num);
527 result = get_result(q);
528 free(q);
529 if (!result)
530 return -E_NORESULT;
531 ret = print_results(fd, result, top, left, mysql_num_rows(result) - 1,
532 right);
533 mysql_free_result(result);
534 return ret;
535 }
536
537 int com_mbox(int fd, int argc, char * const * argv)
538 {
539 void *result;
540 MYSQL_ROW row;
541 int ret;
542 my_ulonglong num_rows, num_fields, top = 0, left = 0;
543 char *query = para_strdup("select concat('From foo@localhost ', "
544 "date_format(Lastplayed, '%a %b %e %T %Y'), "
545 "'\nReceived: from\nTo: bar\n");
546
547 ret = -E_NOATTS;
548 result = get_all_attributes();
549 if (!result)
550 goto out;
551 ret = -E_NOROW;
552 while ((row = mysql_fetch_row(result))) {
553 char *tmp;
554
555 if (!row[0])
556 goto out;
557 tmp = make_message("%sX-Attribute-%s: ', %s, '\n", query,
558 row[0], row[0]);
559 free(query);
560 query = tmp;
561 }
562 query = para_strcat(query,
563 "From: a\n"
564 "Subject: "
565 "', name, '"
566 "\n\n\n"
567 "') from data"
568 );
569 if (argc >= 2) {
570 char *esc = escape_str(argv[1]), *tmp;
571 ret = -E_ESCAPE;
572 if (!esc)
573 goto out;
574 tmp = make_message("%s where name LIKE '%s'", query, esc);
575 free(esc);
576 free(query);
577 query = tmp;
578 }
579 mysql_free_result(result);
580 ret = -E_NORESULT;
581 result = get_result(query);
582 if (!result)
583 goto out;
584 ret = -E_EMPTY_RESULT;
585 num_fields = mysql_field_count(mysql_ptr);
586 num_rows = mysql_num_rows(result);
587 if (!num_fields || !num_rows)
588 goto out;
589 ret = print_results(fd, result, top, left, num_rows - 1,
590 num_fields - 1);
591 out:
592 free(query);
593 if (result)
594 mysql_free_result(result);
595 return ret;
596 }
597
598 /*
599 * get attributes by name. If verbose is not 0, this function returns a string
600 * of the form 'att1="0",att2="1"'... which is used in com_cam() for
601 * constructing a mysql update query. Otherwise the space-separated list of all
602 * attributes which are set in the audio file given by name is returned. Never
603 * returns NULL in *NON VERBOSE* mode.
604 */
605 static char *get_atts(char *name, int verbose)
606 {
607 char *atts = NULL, *buf, *ebn;
608 void *result = NULL, *result2 = NULL;
609 MYSQL_ROW row, row2;
610 int i;
611 my_ulonglong num_fields, offset = 4; /* skip Lastplayed, Numplayed... */
612
613
614 result2 = get_all_attributes();
615 if (!result2)
616 goto out;
617 ebn = escaped_basename(name);
618 if (!ebn)
619 goto out;
620 buf = make_message("select * from data where name='%s'", ebn);
621 free(ebn);
622 result = get_result(buf);
623 free(buf);
624 if (!result)
625 goto out;
626 num_fields = mysql_num_fields(result);
627 if (num_fields < 5)
628 goto out;
629 mysql_data_seek(result2, offset);
630 row = mysql_fetch_row(result);
631 if (!row)
632 goto out;
633 for (i = 4; i < num_fields; i++) {
634 int is_set = row[i] && !strcmp(row[i], "1");
635 row2 = mysql_fetch_row(result2);
636 if (!row2 || !row2[0])
637 goto out;
638 if (atts && (verbose || is_set))
639 atts = para_strcat(atts, verbose? "," : " ");
640 if (is_set || verbose)
641 atts = para_strcat(atts, row2[0]);
642 if (verbose)
643 atts = para_strcat(atts, is_set? "=\"1\"" : "=\"0\"");
644 }
645 out:
646 if (result2)
647 mysql_free_result(result2);
648 if (result)
649 mysql_free_result(result);
650 if (!atts && !verbose)
651 atts = para_strdup("(none)");
652 return atts;
653 }
654
655 /* never returns NULL in verbose mode */
656 static char *get_meta(char *name, int verbose)
657 {
658 MYSQL_ROW row;
659 void *result = NULL;
660 char *ebn, *q, *ret = NULL;
661 const char *verbose_fmt =
662 "select concat('lastplayed: ', "
663 "(to_days(now()) - to_days(lastplayed)),"
664 "' day(s). numplayed: ', numplayed, "
665 "', pic: ', pic_id) "
666 "from data where name = '%s'";
667 /* is that really needed? */
668 const char *fmt = "select concat('lastplayed=\\'', lastplayed, "
669 "'\\', numplayed=\\'', numplayed, "
670 "'\\', pic_id=\\'', pic_id, '\\'') "
671 "from data where name = '%s'";
672
673 if (!(ebn = escaped_basename(name)))
674 goto out;
675 q = make_message(verbose? verbose_fmt : fmt, ebn);
676 free(ebn);
677 result = get_result(q);
678 free(q);
679 if (!result)
680 goto out;
681 row = mysql_fetch_row(result);
682 if (!row || !row[0])
683 goto out;
684 ret = para_strdup(row[0]);
685 out:
686 if (result)
687 mysql_free_result(result);
688 if (!ret && verbose)
689 ret = para_strdup("(not yet played)");
690 return ret;
691 }
692
693 static char *get_dir(char *name)
694 {
695 char *ret = NULL, *q, *ebn;
696 void *result;
697 MYSQL_ROW row;
698
699 if (!(ebn = escaped_basename(name)))
700 return NULL;
701 q = make_message("select dir from dir where name = '%s'", ebn);
702 free(ebn);
703 result = get_result(q);
704 free(q);
705 if (!result)
706 return NULL;
707 row = mysql_fetch_row(result);
708 if (row && row[0])
709 ret = para_strdup(row[0]);
710 mysql_free_result(result);
711 return ret;
712 }
713
714 /* never returns NULL */
715 static char *get_current_stream(void)
716 {
717 char *ret;
718 MYSQL_ROW row;
719 void *result = get_result("select def from streams where "
720 "name = 'current_stream'");
721
722 if (!result)
723 goto err_out;
724 row = mysql_fetch_row(result);
725 if (!row || !row[0])
726 goto err_out;
727 ret = para_strdup(row[0]);
728 mysql_free_result(result);
729 return ret;
730 err_out:
731 if (result)
732 mysql_free_result(result);
733 return para_strdup("(none)");
734 }
735
736 /*
737 * Read stream definition of stream streamname and construct mysql
738 * query. Return NULL on errors. If streamname is NULL, use current
739 * stream. If that is also NULL, use query that selects everything.
740 * If filename is NULL, query will list everything, otherwise only
741 * the score of given file.
742 */
743 static char *get_query(const char *streamname, char *filename, int with_path)
744 {
745 char *accept_opts = NULL, *deny_opts = NULL, *score = NULL;
746 char *where_clause, *order, *query;
747 char command[255] = ""; /* buffer for sscanf */
748 void *result;
749 MYSQL_ROW row;
750 char *end, *tmp;
751 char *select_clause = NULL;
752 if (!streamname)
753 tmp = get_current_stream();
754 else {
755 tmp = escape_str(streamname);
756 if (!tmp)
757 return NULL;
758 }
759 if (!strcmp(tmp, "(none)")) {
760 free(tmp);
761 if (filename) {
762 char *ret, *ebn = escaped_basename(filename);
763 if (!ebn)
764 return NULL;
765 ret = make_message("select to_days(now()) - "
766 "to_days(lastplayed) from data "
767 "where name = '%s'", ebn);
768 free(ebn);
769 return ret;
770 }
771 if (with_path)
772 return make_message(
773 "select concat(dir.dir, '/', dir.name) "
774 "from data, dir where dir.name = data.name "
775 "order by data.lastplayed"
776 );
777 return make_message(
778 "select name from data where name is not NULL "
779 "order by lastplayed"
780 );
781 }
782 free(tmp);
783 query = make_message("select def from streams where name = '%s'",
784 streamname);
785 result = get_result(query);
786 free(query);
787 query = NULL;
788 if (!result)
789 goto out;
790 row = mysql_fetch_row(result);
791 if (!row || !row[0])
792 goto out;
793 end = row[0];
794 while (*end) {
795 int n;
796 char *arg, *line = end;
797
798 if (!(end = strchr(line, '\n')))
799 break;
800 *end = '\0';
801 end++;
802 if (sscanf(line, "%200s%n", command, &n) < 1)
803 continue;
804 arg = line + n;
805 if (!strcmp(command, "accept:")) {
806 char *tmp2 = s_a_r_list(mysql_macro_list, arg);
807 if (accept_opts)
808 accept_opts = para_strcat(
809 accept_opts, " or ");
810 accept_opts = para_strcat(accept_opts, tmp2);
811 free(tmp2);
812 continue;
813 }
814 if (!strcmp(command, "deny:")) {
815 char *tmp2 = s_a_r_list(mysql_macro_list, arg);
816 if (deny_opts)
817 deny_opts = para_strcat(deny_opts, " or ");
818 deny_opts = para_strcat(deny_opts, tmp2);
819 free(tmp2);
820 continue;
821 }
822 if (!score && !strcmp(command, "score:"))
823 score = s_a_r_list(mysql_macro_list, arg);
824 }
825 if (!score) {
826 score = s_a_r_list(mysql_macro_list, conf.mysql_default_score_arg);
827 if (!score)
828 goto out;
829 }
830 if (filename) {
831 char *ebn = escaped_basename(filename);
832 if (!ebn)
833 goto out;
834 select_clause = make_message("select %s from data ", score);
835 free(score);
836 where_clause = make_message( "where name = '%s' ", ebn);
837 free(ebn);
838 order = para_strdup("");
839 goto write_query;
840 }
841 select_clause = para_strdup(with_path?
842 "select concat(dir.dir, '/', dir.name) from data, dir "
843 "where dir.name = data.name "
844 :
845 "select name from data where name is not NULL");
846 order = make_message("order by -(%s)", score);
847 free(score);
848 if (accept_opts && deny_opts) {
849 where_clause = make_message("and ((%s) and not (%s)) ",
850 accept_opts, deny_opts);
851 goto write_query;
852 }
853 if (accept_opts && !deny_opts) {
854 where_clause = make_message("and (%s) ", accept_opts);
855 goto write_query;
856 }
857 if (!accept_opts && deny_opts) {
858 where_clause = make_message("and not (%s) ", deny_opts);
859 goto write_query;
860 }
861 where_clause = para_strdup("");
862 write_query:
863 query = make_message("%s %s %s", select_clause, where_clause, order);
864 free(order);
865 free(select_clause);
866 free(where_clause);
867 out:
868 if (accept_opts)
869 free(accept_opts);
870 if (deny_opts)
871 free(deny_opts);
872 if (result)
873 mysql_free_result(result);
874 return query;
875 }
876
877
878
879 /*
880 * This is called from server and from some commands. Name must not be NULL
881 * Never returns NULL.
882 */
883 static char *get_selector_info(char *name)
884 {
885 char *meta, *atts, *info, *dir, *query, *stream;
886 void *result = NULL;
887 MYSQL_ROW row = NULL;
888
889 if (!name)
890 return para_strdup("(none)");
891 stream = get_current_stream();
892 meta = get_meta(name, 1);
893 atts = get_atts(name, 0);
894 dir = get_dir(name);
895 /* get score */
896 query = get_query(stream, name, 0); /* FIXME: pass stream == NULL instead? */
897 if (!query)
898 goto write;
899 result = get_result(query);
900 free(query);
901 if (result)
902 row = mysql_fetch_row(result);
903 write:
904 info = make_message("dbinfo1:dir: %s\n"
905 "dbinfo2:stream: %s, %s, score: %s\n"
906 "dbinfo3:%s\n",
907 dir? dir : "(not contained in table)",
908 stream, meta,
909 (result && row && row[0])? row[0] : "(no score)",
910 atts);
911 free(dir);
912 free(meta);
913 free(atts);
914 free(stream);
915 if (result)
916 mysql_free_result(result);
917 return info;
918 }
919
920 /* might return NULL */
921 static char *get_current_audio_file(void)
922 {
923 char *name;
924 mmd_lock();
925 name = para_basename(mmd->filename);
926 mmd_unlock();
927 return name;
928 }
929
930 /* If called as child, mmd_lock must be held */
931 static void update_mmd(char *info)
932 {
933 PARA_DEBUG_LOG("%s", "updating shared memory area\n");
934 strncpy(mmd->selector_info, info, MMD_INFO_SIZE - 1);
935 mmd->selector_info[MMD_INFO_SIZE - 1] = '\0';
936 }
937
938 static void refresh_selector_info(void)
939 {
940 char *name = get_current_audio_file();
941 char *info;
942
943 if (!name)
944 return;
945 info = get_selector_info(name);
946 free(name);
947 mmd_lock();
948 update_mmd(info);
949 mmd_unlock();
950 free(info);
951 }
952
953 /* list attributes / print database info */
954 static int com_la_info(int fd, int argc, char * const * argv)
955 {
956 char *name = NULL, *meta = NULL, *atts = NULL, *dir = NULL;
957 int ret, la = strcmp(argv[0], "info");
958
959 if (argc < 2) {
960 ret = -E_GET_AUDIO_FILE;
961 if (!(name = get_current_audio_file()))
962 goto out;
963 ret = send_va_buffer(fd, "%s\n", name);
964 if (ret < 0)
965 goto out;
966 } else {
967 ret = -E_ESCAPE;
968 if (!(name = escaped_basename(argv[1])))
969 goto out;
970 }
971 meta = get_meta(name, 1);
972 atts = get_atts(name, 0);
973 dir = get_dir(name);
974 if (la)
975 ret = send_va_buffer(fd, "%s\n", atts);
976 else
977 ret = send_va_buffer(fd, "dir: %s\n" "%s\n" "attributes: %s\n",
978 dir? dir : "(not contained in table)", meta, atts);
979 out:
980 free(meta);
981 free(atts);
982 free(dir);
983 free(name);
984 return ret;
985 }
986
987 /* list attributes */
988 int com_la(int fd, int argc, char * const * argv)
989 {
990 return com_la_info(fd, argc, argv);
991 }
992
993 /* print database info */
994 int com_info(int fd, int argc, char * const * argv)
995 {
996 return com_la_info(fd, argc, argv);
997 }
998
999 static int change_stream(const char *stream)
1000 {
1001 char *query;
1002 int ret;
1003 query = make_message("update streams set def='%s' "
1004 "where name = 'current_stream'", stream);
1005 ret = real_query(query);
1006 free(query);
1007 return ret;
1008 }
1009
1010 static int get_pic_id_by_name(char *name)
1011 {
1012 char *q, *ebn;
1013 void *result = NULL;
1014 int ret;
1015 MYSQL_ROW row;
1016
1017 if (!(ebn = escaped_basename(name)))
1018 return -E_ESCAPE;
1019 q = make_message("select pic_id from data where name = '%s'", ebn);
1020 free(ebn);
1021 result = get_result(q);
1022 free(q);
1023 if (!result)
1024 return -E_NORESULT;
1025 row = mysql_fetch_row(result);
1026 ret = -E_NOROW;
1027 if (row && row[0])
1028 ret = atoi(row[0]);
1029 mysql_free_result(result);
1030 return ret;
1031 }
1032
1033 static int remove_entry(const char *name)
1034 {
1035 char *q, *ebn = escaped_basename(name);
1036 int ret = -E_ESCAPE;
1037
1038 if (!ebn)
1039 goto out;
1040 q = make_message("delete from data where name = '%s'", ebn);
1041 real_query(q); /* ignore errors */
1042 free(q);
1043 q = make_message("delete from dir where name = '%s'", ebn);
1044 real_query(q); /* ignore errors */
1045 free(q);
1046 ret = 1;
1047 out:
1048 free(ebn);
1049 return ret;
1050 }
1051
1052 static int add_entry(const char *name)
1053 {
1054 char *q, *dn, *ebn = NULL, *edn = NULL;
1055 int ret;
1056
1057 if (!name || !*name)
1058 return -E_MYSQL_SYNTAX;
1059 ebn = escaped_basename(name);
1060 if (!ebn)
1061 return -E_ESCAPE;
1062 ret = -E_MYSQL_SYNTAX;
1063 dn = para_dirname(name);
1064 if (!dn)
1065 goto out;
1066 ret = -E_ESCAPE;
1067 edn = escape_str(dn);
1068 free(dn);
1069 if (!edn || !*edn)
1070 goto out;
1071 q = make_message("insert into data (name, pic_id) values "
1072 "('%s', '%s')", ebn, "1");
1073 ret = real_query(q);
1074 // ret = 1; PARA_DEBUG_LOG("q: %s\n", q);
1075 free(q);
1076 if (ret < 0)
1077 goto out;
1078 q = make_message("insert into dir (name, dir) values "
1079 "('%s', '%s')", ebn, edn);
1080 // ret = 1; PARA_DEBUG_LOG("q: %s\n", q);
1081 ret = real_query(q);
1082 free(q);
1083 out:
1084 if (ebn)
1085 free(ebn);
1086 if (edn)
1087 free(edn);
1088 return ret;
1089 }
1090
1091 /*
1092 * remove/add entries
1093 */
1094 static int com_rm_ne(__a_unused int fd, int argc, char * const * argv)
1095 {
1096 int ne = !strcmp(argv[0], "ne");
1097 int i, ret;
1098 if (argc < 2)
1099 return -E_MYSQL_SYNTAX;
1100 for (i = 1; i < argc; i++) {
1101 ret = remove_entry(argv[i]);
1102 if (ret < 0)
1103 return ret;
1104 if (!ne)
1105 continue;
1106 ret = add_entry(argv[i]);
1107 if (ret < 0)
1108 return ret;
1109 }
1110 return 1;
1111 }
1112
1113 /*
1114 * rm
1115 */
1116 int com_rm(int fd, int argc, char * const * argv)
1117 {
1118 return com_rm_ne(fd, argc, argv);
1119 }
1120
1121 /*
1122 * ne
1123 */
1124 int com_ne(int fd, int argc, char * const * argv)
1125 {
1126 return com_ne(fd, argc, argv);
1127 }
1128
1129 /*
1130 * mv: rename entry
1131 */
1132 int com_mv(__a_unused int fd, int argc, char * const * argv)
1133 {
1134 char *q, *dn, *ebn1 = NULL, *ebn2 = NULL, *edn = NULL;
1135 int ret;
1136
1137 if (argc != 3)
1138 return -E_MYSQL_SYNTAX;
1139 ret = -E_ESCAPE;
1140 ebn1 = escaped_basename(argv[1]);
1141 ebn2 = escaped_basename(argv[2]);
1142 if (!ebn1 || !ebn2 || !*ebn1 || !*ebn2)
1143 goto out;
1144 ret = -E_MYSQL_SYNTAX;
1145 if (!strcmp(ebn1, ebn2))
1146 goto update_dir;
1147 remove_entry(argv[2]); /* no need to escape, ignore error */
1148 q = make_message("update data set name = '%s' where name = '%s'",
1149 ebn2, ebn1);
1150 ret = real_query(q);
1151 free(q);
1152 if (ret < 0)
1153 goto out;
1154 ret = -E_AUDIO_FILE;
1155 if (!mysql_affected_rows(mysql_ptr))
1156 goto out;
1157 q = make_message("update dir set name = '%s' where name = '%s'",
1158 ebn2, ebn1);
1159 ret = real_query(q);
1160 free(q);
1161 if (ret < 0)
1162 goto out;
1163 update_dir:
1164 ret = 1;
1165 dn = para_dirname(argv[2]);
1166 if (!dn)
1167 goto out;
1168 ret = -E_ESCAPE;
1169 edn = escape_str(dn);
1170 free(dn);
1171 if (!edn)
1172 goto out;
1173 ret = 1;
1174 if (!*edn)
1175 goto out;
1176 q = make_message("update dir set dir = '%s' where name = '%s'",
1177 edn, ebn2);
1178 ret = real_query(q);
1179 free(q);
1180 out:
1181 free(edn);
1182 free(ebn1);
1183 free(ebn2);
1184 return ret;
1185 }
1186
1187 /*
1188 * set field
1189 */
1190 static int com_set(__a_unused int fd, int argc, char * const * argv)
1191 {
1192 char *q, *ebn;
1193 long unsigned id;
1194 int i, ret;
1195 const char *field = strcmp(argv[0], "picass")? "numplayed" : "pic_id";
1196
1197 if (argc < 3)
1198 return -E_MYSQL_SYNTAX;
1199 id = atol(argv[1]);
1200 for (i = 2; i < argc; i++) {
1201 ebn = escaped_basename(argv[i]);
1202 if (!ebn)
1203 return -E_ESCAPE;
1204 q = make_message("update data set %s = %lu "
1205 "where name = '%s'", field, id, ebn);
1206 free(ebn);
1207 ret = real_query(q);
1208 free(q);
1209 if (ret < 0)
1210 return ret;
1211 }
1212 return 1;
1213 }
1214
1215 /*
1216 * snp: set numplayed
1217 */
1218 int com_picass(int fd, int argc, char * const * argv)
1219 {
1220 return com_set(fd, argc, argv);
1221 }
1222
1223 /*
1224 * snp: set numplayed
1225 */
1226 int com_snp(int fd, int argc, char * const * argv)
1227 {
1228 int ret = com_set(fd, argc, argv);
1229 if (ret >= 0)
1230 refresh_selector_info();
1231 return ret;
1232 }
1233
1234 /*
1235 * picch: change entry's name in pics table
1236 */
1237 int com_picch(__a_unused int fd, int argc, char * const * argv)
1238 {
1239 int ret;
1240 long unsigned id;
1241 char *q, *tmp;
1242
1243 if (argc != 3)
1244 return -E_MYSQL_SYNTAX;
1245 id = atol(argv[1]);
1246 ret = -E_ESCAPE;
1247 tmp = escape_str(argv[2]);
1248 if (!tmp)
1249 return -E_ESCAPE;
1250 q = make_message("update pics set name = '%s' where id = %lu", tmp, id);
1251 free(tmp);
1252 ret = real_query(q);
1253 free(q);
1254 return ret;
1255 }
1256
1257 /*
1258 * piclist: print list of pics in db
1259 */
1260 int com_piclist(__a_unused int fd, int argc, __a_unused char * const * argv)
1261 {
1262 void *result = NULL;
1263 MYSQL_ROW row;
1264 unsigned long *length;
1265 int ret;
1266
1267 if (argc != 1)
1268 return -E_MYSQL_SYNTAX;
1269 result = get_result("select id,name,pic from pics order by id");
1270 if (!result)
1271 return -E_NORESULT;
1272 while ((row = mysql_fetch_row(result))) {
1273 length = mysql_fetch_lengths(result);
1274 if (!row || !row[0] || !row[1] || !row[2])
1275 continue;
1276 ret = send_va_buffer(fd, "%s\t%lu\t%s\n", row[0], length[2], row[1]);
1277 if (ret < 0)
1278 goto out;
1279 }
1280 ret = 1;
1281 out:
1282 mysql_free_result(result);
1283 return ret;
1284 }
1285
1286 /*
1287 * picdel: delete picture from database
1288 */
1289 int com_picdel(int fd, int argc, char * const * argv)
1290 {
1291 char *q;
1292 long unsigned id;
1293 my_ulonglong aff;
1294 int i, ret;
1295
1296 if (argc < 2)
1297 return -E_MYSQL_SYNTAX;
1298 for (i = 1; i < argc; i++) {
1299 id = atol(argv[i]);
1300 q = make_message("delete from pics where id = %lu", id);
1301 ret = real_query(q);
1302 free(q);
1303 if (ret < 0)
1304 return ret;
1305 aff = mysql_affected_rows(mysql_ptr);
1306 if (!aff) {
1307 ret = send_va_buffer(fd, "No such id: %lu\n", id);
1308 if (ret < 0)
1309 return ret;
1310 continue;
1311 }
1312 q = make_message("update data set pic_id = 1 where pic_id = %lu", id);
1313 ret = real_query(q);
1314 free(q);
1315 }
1316 return 1;
1317 }
1318 /*
1319 * pic: get picture by name or by number
1320 */
1321 int com_pic(int fd, int argc, char * const * argv)
1322 {
1323 void *result = NULL;
1324 MYSQL_ROW row;
1325 unsigned long *length, id;
1326 int ret;
1327 char *q, *name = NULL;
1328
1329 if (argc < 2) {
1330 ret = -E_GET_AUDIO_FILE;
1331 name = get_current_audio_file();
1332 } else {
1333 ret = -E_ESCAPE;
1334 name = escaped_basename(argv[1]);
1335 }
1336 if (!name)
1337 return ret;
1338 if (*name == '#')
1339 id = atoi(name + 1);
1340 else
1341 id = get_pic_id_by_name(name);
1342 free(name);
1343 if (id <= 0)
1344 return id;
1345 q = make_message("select pic from pics where id = '%lu'", id);
1346 result = get_result(q);
1347 free(q);
1348 if (!result)
1349 return -E_NORESULT;
1350 row = mysql_fetch_row(result);
1351 ret = -E_NOROW;
1352 if (!row || !row[0])
1353 goto out;
1354 length = mysql_fetch_lengths(result);
1355 ret = send_bin_buffer(fd, row[0], *length);
1356 out:
1357 mysql_free_result(result);
1358 return ret;
1359 }
1360
1361 /* strdel */
1362 int com_strdel(__a_unused int fd, int argc, char * const * argv)
1363 {
1364 char *q, *tmp;
1365 int ret;
1366
1367 if (argc < 2)
1368 return -E_MYSQL_SYNTAX;
1369 tmp = escape_str(argv[1]);
1370 if (!tmp)
1371 return -E_ESCAPE;
1372 q = make_message("delete from streams where name='%s'", tmp);
1373 free(tmp);
1374 ret = real_query(q);
1375 free(q);
1376 return ret;
1377 }
1378
1379 /*
1380 * ls
1381 */
1382 int com_ls(int fd, int argc, char * const * argv)
1383 {
1384 char *q;
1385 void *result;
1386 int ret;
1387 my_ulonglong num_rows, top = 0, left = 0, right = 0;
1388
1389 if (argc > 2)
1390 return -E_MYSQL_SYNTAX;
1391 if (argc > 1) {
1392 char *tmp = escape_str(argv[1]);
1393 if (!tmp)
1394 return -E_ESCAPE;
1395 q = make_message("select name from data where name like '%s'",
1396 tmp);
1397 free(tmp);
1398 } else
1399 q = para_strdup("select name from data");
1400 result = get_result(q);
1401 free(q);
1402 if (!result)
1403 return -E_NORESULT;
1404 num_rows = mysql_num_rows(result);
1405 ret = 1;
1406 if (num_rows)
1407 ret = print_results(fd, result, top, left, num_rows - 1, right);
1408 mysql_free_result(result);
1409 return ret;
1410 }
1411
1412 /*
1413 * summary
1414 */
1415 int com_summary(__a_unused int fd, int argc, __a_unused char * const * argv)
1416 {
1417 MYSQL_ROW row;
1418 MYSQL_ROW row2;
1419 void *result;
1420 void *result2 = NULL;
1421 const char *fmt = "select count(name) from data where %s='1'";
1422 int ret = -E_NORESULT;
1423
1424 if (argc != 1)
1425 return -E_MYSQL_SYNTAX;
1426 result = get_all_attributes();
1427 if (!result)
1428 goto out;
1429 while ((row = mysql_fetch_row(result))) {
1430 char *buf;
1431
1432 ret = -E_NOROW;
1433 if (!row[0])
1434 goto out;
1435 ret = -E_NORESULT;
1436 buf = make_message(fmt, row[0]);
1437 result2 = get_result(buf);
1438 free(buf);
1439 if (!result2)
1440 goto out;
1441 ret = -E_NOROW;
1442 row2 = mysql_fetch_row(result2);
1443 if (!row2 || !row2[0])
1444 goto out;
1445 ret = send_va_buffer(fd, "%s\t%s\n", row[0], row2[0]);
1446 if (ret < 0)
1447 goto out;
1448 }
1449 ret = 1;
1450 out:
1451 if (result2)
1452 mysql_free_result(result2);
1453 if (result)
1454 mysql_free_result(result);
1455 return ret;
1456 }
1457
1458 static int get_numplayed(char *name)
1459 {
1460 void *result;
1461 MYSQL_ROW row;
1462 const char *fmt = "select numplayed from data where name = '%s'";
1463 char *buf = make_message(fmt, name);
1464 int ret = -E_NORESULT;
1465
1466 result = get_result(buf);
1467 free(buf);
1468 if (!result)
1469 goto out;
1470 ret = -E_NOROW;
1471 row = mysql_fetch_row(result);
1472 if (!row || !row[0])
1473 goto out;
1474 ret = atoi(row[0]);
1475 out:
1476 if (result)
1477 mysql_free_result(result);
1478 return ret;
1479 }
1480
1481 static int update_audio_file(const char *name)
1482 {
1483 int ret;
1484 const char *fmt1 = "update data set lastplayed = now() where name = '%s'";
1485 const char *fmt2 = "update data set numplayed = %i where name = '%s'";
1486 char *q;
1487 char *ebn = escaped_basename(name);
1488
1489 ret = -E_ESCAPE;
1490 if (!ebn)
1491 goto out;
1492 q = make_message(fmt1, ebn);
1493 ret = real_query(q);
1494 free(q);
1495 if (ret < 0)
1496 goto out;
1497 ret = get_numplayed(ebn);
1498 if (ret < 0)
1499 goto out;
1500 q = make_message(fmt2, ret + 1, ebn);
1501 ret = real_query(q);
1502 free(q);
1503 out:
1504 free(ebn);
1505 return ret;
1506 }
1507
1508 static void update_audio_file_server_handler(char *name)
1509 {
1510 char *info;
1511 info = get_selector_info(name);
1512 update_mmd(info);
1513 free(info);
1514 update_audio_file(name);
1515 }
1516
1517 int com_us(__a_unused int fd, int argc, char * const * argv)
1518 {
1519 char *tmp;
1520 int ret;
1521
1522 if (argc != 2)
1523 return -E_MYSQL_SYNTAX;
1524 tmp = escape_str(argv[1]);
1525 if (!tmp)
1526 return -E_ESCAPE;
1527 ret = update_audio_file(argv[1]);
1528 free(tmp);
1529 if (ret >= 0)
1530 refresh_selector_info();
1531 return ret;
1532 }
1533
1534 /* select previous / next stream */
1535 static int com_ps_ns(__a_unused int fd, int argc, char * const * argv)
1536 {
1537 char *query, *stream = get_current_stream();
1538 void *result = get_result("select name from streams");
1539 MYSQL_ROW row;
1540 int ret;
1541 my_ulonglong num_rows, match, i;
1542
1543 ret = -E_MYSQL_SYNTAX;
1544 if (argc != 1)
1545 goto out;
1546 ret = -E_NORESULT;
1547 if (!result)
1548 goto out;
1549 num_rows = mysql_num_rows(result);
1550 ret = -E_EMPTY_RESULT;
1551 if (num_rows < 2)
1552 goto out;
1553 ret = -E_NOROW;
1554 for (i = 0; i < num_rows; i++) {
1555 row = mysql_fetch_row(result);
1556 if (!row || !row[0])
1557 goto out;
1558 if (!strcmp(row[0], "current_stream"))
1559 continue;
1560 if (!strcmp(row[0], stream))
1561 break;
1562 }
1563 ret = -E_NO_STREAM;
1564 if (i == num_rows)
1565 goto out;
1566 match = i;
1567 if (!strcmp(argv[0], "ps"))
1568 i = match > 0? match - 1 : num_rows - 1;
1569 else
1570 i = match < num_rows - 1? match + 1 : 0;
1571 ret = -E_NOROW;
1572 mysql_data_seek(result, i);
1573 row = mysql_fetch_row(result);
1574 if (!row || !row[0])
1575 goto out;
1576 if (!strcmp(row[0], "current_stream")) {
1577 if (!strcmp(argv[0], "ps")) {
1578 i = match < 2? match + num_rows - 2 : match - 2;
1579 } else {
1580 i = match + 2;
1581 i = i > num_rows - 1? i - num_rows : i;
1582 }
1583 mysql_data_seek(result, i);
1584 row = mysql_fetch_row(result);
1585 if (!row || !row[0])
1586 goto out;
1587 }
1588 query = make_message("update streams set def='%s' where name = "
1589 "'current_stream'", row[0]);
1590 ret = real_query(query);
1591 free(query);
1592 refresh_selector_info();
1593 out:
1594 free(stream);
1595 if (result)
1596 mysql_free_result(result);
1597 return ret;
1598 }
1599
1600 /* select previous stream */
1601 int com_ps(int fd, int argc, char * const * argv)
1602 {
1603 return com_ps_ns(fd, argc, argv);
1604 }
1605
1606 /* select next stream */
1607 int com_ns(int fd, int argc, char * const * argv)
1608 {
1609 return com_ps_ns(fd, argc, argv);
1610 }
1611
1612 /* streams */
1613 int com_streams(int fd, int argc, __a_unused char * const * argv)
1614 {
1615 unsigned int num_rows;
1616 int i, ret = -E_NORESULT;
1617 void *result;
1618 MYSQL_ROW row;
1619
1620 if (argc > 1 && strcmp(argv[1], "current_stream"))
1621 return -E_MYSQL_SYNTAX;
1622 if (argc > 1) {
1623 char *cs = get_current_stream();
1624 ret = send_va_buffer(fd, "%s\n", cs);
1625 free(cs);
1626 return ret;
1627 }
1628 result = get_result("select name from streams");
1629 if (!result)
1630 goto out;
1631 num_rows = mysql_num_rows(result);
1632 ret = 1;
1633 if (!num_rows)
1634 goto out;
1635 ret = -E_NOROW;
1636 for (i = 0; i < num_rows; i++) {
1637 row = mysql_fetch_row(result);
1638 if (!row || !row[0])
1639 goto out;
1640 if (strcmp(row[0], "current_stream"))
1641 send_va_buffer(fd, "%s\n", row[0]);
1642 }
1643 ret = 1;
1644 out:
1645 if (result)
1646 mysql_free_result(result);
1647 return ret;
1648 }
1649
1650 /* query stream definition */
1651 int com_strq(int fd, int argc, char * const * argv)
1652 {
1653 MYSQL_ROW row;
1654 char *query, *name;
1655 void *result;
1656 int ret;
1657
1658 if (argc < 2) {
1659 ret = -E_GET_STREAM;
1660 name = get_current_stream();
1661 } else {
1662 ret = -E_ESCAPE;
1663 name = escaped_basename(argv[1]);
1664 }
1665 if (!name)
1666 return ret;
1667 ret = -E_NORESULT;
1668 query = make_message("select def from streams where name='%s'", name);
1669 free(name);
1670 result = get_result(query);
1671 free(query);
1672 if (!result)
1673 goto out;
1674 ret = -E_NOROW;
1675 row = mysql_fetch_row(result);
1676 if (!row || !row[0])
1677 goto out;
1678 /* no '\n' needed */
1679 ret = send_buffer(fd, row[0]);
1680 out:
1681 if (result)
1682 mysql_free_result(result);
1683 return ret;
1684 }
1685
1686 /* change stream / change stream and play */
1687 static int com_cs_csp(int fd, int argc, char * const * argv)
1688 {
1689 int ret, stream_change;
1690 char *query, *stream = NULL;
1691 char *old_stream = get_current_stream();
1692 int csp = !strcmp(argv[0], "csp");
1693
1694 ret = -E_MYSQL_SYNTAX;
1695 if (argc > 2)
1696 goto out;
1697 if (argc == 1) {
1698 if (csp)
1699 goto out;
1700 ret = send_va_buffer(fd, "%s\n", old_stream);
1701 goto out;
1702 }
1703 ret = -E_GET_QUERY;
1704 /* test if stream is valid, no need to escape argv[1] */
1705 query = get_query(argv[1], NULL, 0);
1706 if (!query)
1707 goto out;
1708 free(query);
1709 /* stream is ok */
1710 stream = escape_str(argv[1]);
1711 if (!stream)
1712 goto out;
1713 stream_change = strcmp(stream, old_stream);
1714 if (stream_change) {
1715 ret = change_stream(stream);
1716 if (ret < 0)
1717 goto out;
1718 refresh_selector_info();
1719 }
1720 if (csp) {
1721 mmd_lock();
1722 mmd->new_vss_status_flags |= VSS_PLAYING;
1723 if (stream_change)
1724 mmd->new_vss_status_flags |= VSS_NEXT;
1725 mmd_unlock();
1726 }
1727 ret = 1;
1728 out:
1729 free(old_stream);
1730 free(stream);
1731 return ret;
1732 }
1733
1734 /* change stream */
1735 int com_cs(int fd, int argc, char * const * argv)
1736 {
1737 return com_cs_csp(fd, argc, argv);
1738 }
1739
1740 /* change stream and play */
1741 int com_csp(int fd, int argc, char * const * argv)
1742 {
1743 return com_cs_csp(fd, argc, argv);
1744 }
1745
1746 /* score list / skip */
1747 static int com_sl_skip(int fd, int argc, char * const * argv)
1748 {
1749 void *result = NULL;
1750 MYSQL_ROW row;
1751 int ret, i, skip = !strcmp(argv[0], "skip");
1752 char *query, *stream, *tmp;
1753 unsigned int num_rows, num;
1754
1755 if (argc < 2)
1756 return -E_MYSQL_SYNTAX;
1757 num = atoi(argv[1]);
1758 if (!num)
1759 return -E_MYSQL_SYNTAX;
1760 if (argc == 2) {
1761 stream = get_current_stream();
1762 if (!stream)
1763 return -E_GET_STREAM;
1764 } else {
1765 stream = escape_str(argv[2]);
1766 if (!stream)
1767 return -E_ESCAPE;
1768 }
1769 tmp = get_query(stream, NULL, 0);
1770 free(stream);
1771 if (!tmp)
1772 return -E_GET_QUERY;
1773 query = make_message("%s limit %d", tmp, num);
1774 free(tmp);
1775 ret = -E_NORESULT;
1776 result = get_result(query);
1777 free(query);
1778 if (!result)
1779 goto out;
1780 ret = -E_EMPTY_RESULT;
1781 num_rows = mysql_num_rows(result);
1782 if (!num_rows)
1783 goto out;
1784 for (i = 0; i < num_rows && i < num; i++) {
1785 row = mysql_fetch_row(result);
1786 if (skip) {
1787 send_va_buffer(fd, "Skipping %s\n", row[0]);
1788 update_audio_file(row[0]);
1789 } else
1790 send_va_buffer(fd, "%s\n", row[0]? row[0]: "BUG");
1791 }
1792 ret = 1;
1793 out:
1794 if (result)
1795 mysql_free_result(result);
1796 return ret;
1797 }
1798
1799 /* score list */
1800 int com_sl(int fd, int argc, char * const * argv)
1801 {
1802 return com_sl_skip(fd, argc, argv);
1803 }
1804
1805 /* skip */
1806 int com_skip(int fd, int argc, char * const * argv)
1807 {
1808 return com_sl_skip(fd, argc, argv);
1809 }
1810
1811 /*
1812 * update attributes of name
1813 */
1814 static int update_atts(int fd, const char *name, char *atts)
1815 {
1816 int ret;
1817 char *ebn, *q, *old, *new = NULL;
1818
1819 if (!mysql_ptr)
1820 return -E_NOTCONN;
1821 ebn = escaped_basename(name);
1822 if (!ebn)
1823 return -E_ESCAPE;
1824 q = make_message("update data set %s where name = '%s'", atts, ebn);
1825 old = get_atts(ebn, 0);
1826 send_va_buffer(fd, "old: %s\n", old);
1827 free(old);
1828 ret = real_query(q);
1829 free(q);
1830 if (ret < 0)
1831 goto out;
1832 new = get_atts(ebn, 0);
1833 ret = send_va_buffer(fd, "new: %s\n", new);
1834 free(new);
1835 out:
1836 free(ebn);
1837 return ret;
1838 }
1839
1840 /*
1841 * set attributes
1842 */
1843 int com_sa(int fd, int argc, char * const * argv)
1844 {
1845 int i, ret;
1846 char *atts = NULL, *name;
1847
1848 if (argc < 2)
1849 return -E_MYSQL_SYNTAX;
1850 for (i = 1; i < argc; i++) {
1851 int unset = 0;
1852 char *esc, *tmp, *p;
1853 int len = strlen(argv[i]);
1854
1855 if (!len)
1856 continue;
1857 switch (argv[i][len - 1]) {
1858 case '+':
1859 unset = 0;
1860 break;
1861 case '-':
1862 unset = 1;
1863 break;
1864 default:
1865 goto no_more_atts;
1866 }
1867 p = para_strdup(argv[i]);
1868 p[len - 1] = '\0';
1869 esc = escape_str(p);
1870 free(p);
1871 if (!esc)
1872 return -E_ESCAPE;
1873 tmp = make_message("%s%s='%s'", atts? "," : "", esc,
1874 unset? "0" : "1");
1875 free(esc);
1876 atts = para_strcat(atts, tmp);
1877 free(tmp);
1878 }
1879 no_more_atts:
1880 if (!atts)
1881 return -E_NOATTS;
1882 if (i >= argc) { /* no name given, use current af */
1883 ret = -E_GET_AUDIO_FILE;
1884 if (!(name = get_current_audio_file()))
1885 goto out;
1886 ret = update_atts(fd, name, atts);
1887 free(name);
1888 } else {
1889 ret = 1;
1890 for (; argv[i] && ret >= 0; i++)
1891 ret = update_atts(fd, argv[i], atts);
1892 }
1893 refresh_selector_info();
1894 out:
1895 free(atts);
1896 return ret;
1897 }
1898
1899 /*
1900 * copy attributes
1901 */
1902 int com_cam(int fd, int argc, char * const * argv)
1903 {
1904 char *name = NULL, *meta = NULL, *atts = NULL;
1905 int i, ret;
1906
1907 if (argc < 3)
1908 return -E_MYSQL_SYNTAX;
1909 if (!(name = escaped_basename(argv[1])))
1910 return -E_ESCAPE;
1911 ret = -E_NOATTS;
1912 if (!(atts = get_atts(name, 1)))
1913 goto out;
1914 ret = -E_META;
1915 if (!(meta = get_meta(name, 0)))
1916 goto out;
1917 for (i = 2; i < argc; i++) {
1918 char *ebn, *q;
1919 ret = -E_ESCAPE;
1920 if (!(ebn = escaped_basename(argv[i])))
1921 goto out;
1922 ret = send_va_buffer(fd, "updating %s\n", ebn);
1923 if (ret < 0) {
1924 free(ebn);
1925 goto out;
1926 }
1927 q = make_message("update data set %s where name = '%s'",
1928 meta, ebn);
1929 if ((ret = update_atts(fd, ebn, atts)) >= 0)
1930 ret = real_query(q);
1931 free(ebn);
1932 free(q);
1933 if (ret < 0)
1934 goto out;
1935 }
1936 ret = 1;
1937 out:
1938 if (name)
1939 free(name);
1940 if (meta)
1941 free(meta);
1942 if (atts)
1943 free(atts);
1944 return ret;
1945 }
1946
1947 /*
1948 * verify / clean
1949 */
1950 static int com_vrfy_clean(int fd, int argc, __a_unused char * const * argv)
1951 {
1952 char *query;
1953 int ret, vrfy_mode = strcmp(argv[0], "clean");
1954 void *result = NULL;
1955 MYSQL_ROW row;
1956 char *escaped_name;
1957 my_ulonglong num_rows, top = 0, left = 0, right = 0;
1958
1959 if (argc != 1)
1960 return -E_MYSQL_SYNTAX;
1961 ret = -E_NORESULT;
1962 result = get_result("select data.name from data left join dir on "
1963 "dir.name = data.name where dir.name is NULL");
1964 if (!result)
1965 goto out;
1966 num_rows = mysql_num_rows(result);
1967 if (!num_rows) {
1968 ret = send_buffer(fd, "No invalid entries\n");
1969 goto out;
1970 }
1971 if (vrfy_mode) {
1972 send_va_buffer(fd, "found %lli invalid entr%s\n", num_rows,
1973 num_rows == 1? "y" : "ies");
1974 ret = print_results(fd, result, top, left, num_rows - 1, right);
1975 goto out;
1976 }
1977 while ((row = mysql_fetch_row(result))) {
1978 ret = -E_NOROW;
1979 if (!row[0])
1980 goto out;
1981 ret = -E_ESCAPE;
1982 escaped_name = escape_str(row[0]);
1983 if (!escaped_name)
1984 goto out;
1985 send_va_buffer(fd, "deleting %s\n", escaped_name);
1986 query = make_message("delete from data where name = '%s'",
1987 escaped_name);
1988 ret = real_query(query);
1989 free(query);
1990 if (ret < 0)
1991 goto out;
1992 }
1993
1994 out:
1995 if (result)
1996 mysql_free_result(result);
1997 return ret;
1998 }
1999
2000 /*
2001 * verify
2002 */
2003 int com_vrfy(int fd, int argc, char * const * argv)
2004 {
2005 return com_vrfy_clean(fd, argc, argv);
2006 }
2007
2008 /*
2009 * clean
2010 */
2011 int com_clean(int fd, int argc, char * const * argv)
2012 {
2013 return com_vrfy_clean(fd, argc, argv);
2014 }
2015
2016 static FILE *out_file;
2017
2018 static int mysql_write_tmp_file(const char *dir, const char *name)
2019 {
2020 int ret = -E_TMPFILE;
2021 char *msg = make_message("%s\t%s\n", dir, name);
2022 if (fputs(msg, out_file) != EOF)
2023 ret = 1;
2024 free(msg);
2025 return ret;
2026 }
2027
2028 /*
2029 * update database
2030 */
2031 int com_upd(int fd, int argc, __a_unused char * const * argv)
2032 {
2033 char *tempname = NULL, *query = NULL;
2034 int ret, out_fd = -1, num = 0;
2035 void *result = NULL;
2036 unsigned int num_rows;
2037 MYSQL_ROW row;
2038
2039 if (argc != 1)
2040 return -E_MYSQL_SYNTAX;
2041 out_file = NULL;
2042 tempname = para_strdup("/tmp/mysql.tmp.XXXXXX");
2043 ret = para_mkstemp(tempname, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
2044 if (ret < 0)
2045 goto out;
2046 out_fd = ret;
2047 out_file = fdopen(out_fd, "w");
2048 if (!out_file) {
2049 close(out_fd);
2050 goto out;
2051 }
2052 if (find_audio_files(conf.mysql_audio_file_dir_arg, mysql_write_tmp_file) < 0)
2053 goto out;
2054 num = ftell(out_file);
2055 /*
2056 * we have to make sure the file hit the disk before we call
2057 * real_query
2058 */
2059 fclose(out_file);
2060 out_file = NULL;
2061 PARA_DEBUG_LOG("wrote tempfile %s (%d bytes)\n", tempname, num);
2062 if (!num)
2063 goto out;
2064 if ((ret = real_query("delete from dir")) < 0)
2065 goto out;
2066 query = make_message("load data infile '%s' ignore into table dir "
2067 "fields terminated by '\t' lines terminated by '\n' "
2068 "(dir, name)", tempname);
2069 ret = real_query(query);
2070 free(query);
2071 if (ret < 0)
2072 goto out;
2073 result = get_result("select dir.name from dir left join data on "
2074 "data.name = dir.name where data.name is NULL");
2075 ret = -E_NORESULT;
2076 if (!result)
2077 goto out;
2078 num_rows = mysql_num_rows(result);
2079 if (!num_rows) {
2080 ret = send_buffer(fd, "no new entries\n");
2081 goto out;
2082 }
2083 while ((row = mysql_fetch_row(result))) {
2084 char *erow;
2085 ret = -E_NOROW;
2086 if (!row[0])
2087 goto out;
2088 send_va_buffer(fd, "new entry: %s\n", row[0]);
2089 erow = escape_str(row[0]);
2090 if (!erow)
2091 goto out;
2092 query = make_message("insert into data (name, pic_id) values "
2093 "('%s','%s')", erow, "1");
2094 free(erow);
2095 ret = real_query(query);
2096 free(query);
2097 if (ret < 0)
2098 goto out;
2099 }
2100 ret = 1;
2101 out:
2102 if (out_fd >= 0)
2103 unlink(tempname);
2104 free(tempname);
2105 if (out_file)
2106 fclose(out_file);
2107 if (result)
2108 mysql_free_result(result);
2109 return ret;
2110 }
2111
2112 static char **server_get_audio_file_list(unsigned int num)
2113 {
2114 char **list = para_malloc((num + 1) * sizeof(char *));
2115 char *tmp, *query, *stream = get_current_stream();
2116 void *result = NULL;
2117 unsigned int num_rows;
2118 int i = 0;
2119 MYSQL_ROW row;
2120
2121 tmp = get_query(stream, NULL, 1);
2122 free(stream);
2123 if (!tmp)
2124 goto err_out;
2125 query = make_message("%s limit %d", tmp, num);
2126 free(tmp);
2127 result = get_result(query);
2128 free(query);
2129 if (!result)
2130 goto err_out;
2131 num_rows = mysql_num_rows(result);
2132 if (!num_rows)
2133 goto err_out;
2134 for (i = 0; i < num_rows && i < num; i++) {
2135 row = mysql_fetch_row(result);
2136 if (!row || !row[0])
2137 goto err_out;
2138 list[i] = para_strdup(row[0]);
2139 }
2140 list[i] = NULL;
2141 goto success;
2142 err_out:
2143 while (i > 0) {
2144 i--;
2145 free(list[i]);
2146 }
2147 free(list);
2148 list = NULL;
2149 success:
2150 if (result)
2151 mysql_free_result(result);
2152 return list;
2153 }
2154
2155 /*
2156 * connect to mysql server, return mysql pointer on success, -E_NOTCONN
2157 * on errors. Called from parent on startup and also from com_cdb().
2158 */
2159 static int init_mysql_server(void)
2160 {
2161 char *u = conf.mysql_user_arg? conf.mysql_user_arg : para_logname();
2162 unsigned int port;
2163
2164 mysql_ptr = mysql_init(NULL);
2165 if (!mysql_ptr) {
2166 PARA_CRIT_LOG("%s", "mysql init error\n");
2167 return -E_NOTCONN;
2168 }
2169 if (conf.mysql_port_arg < 0)
2170 return -E_MYSQL_SYNTAX;
2171 port = conf.mysql_port_arg;
2172 PARA_DEBUG_LOG("connecting: %s@%s:%d\n", u, conf.mysql_host_arg, port);
2173 if (!conf.mysql_user_arg)
2174 free(u);
2175 /*
2176 * If host is NULL a connection to the local host is assumed,
2177 * If user is NULL, the current user is assumed
2178 */
2179 if (!(mysql_ptr = mysql_real_connect(mysql_ptr,
2180 conf.mysql_host_arg,
2181 conf.mysql_user_arg,
2182 conf.mysql_passwd_arg,
2183 conf.mysql_database_arg,
2184 port, NULL, 0))) {
2185 PARA_CRIT_LOG("%s", "connect error\n");
2186 return -E_NOTCONN;
2187 }
2188 PARA_INFO_LOG("%s", "success\n");
2189 return 1;
2190 }
2191
2192 /* mmd lock must be held */
2193 static void write_msg2mmd(int success)
2194 {
2195 sprintf(mmd->selector_info, "dbinfo1:%s\ndbinfo2:mysql-%s\ndbinfo3:\n",
2196 success < 0? PARA_STRERROR(-success) :
2197 "successfully connected to mysql server",
2198 success < 0? "" : mysql_get_server_info(mysql_ptr));
2199 }
2200
2201 /* create database */
2202 int com_cdb(int fd, int argc, char * const * argv)
2203 {
2204 char *query;
2205 int ret;
2206
2207 if (mysql_ptr) {
2208 PARA_INFO_LOG("%s", "closing database\n");
2209 mysql_close(mysql_ptr);
2210 }
2211 /* dont use any database */
2212 conf.mysql_database_arg = NULL; /* leak? */
2213 ret = -E_MYSQL_INIT;
2214 if (init_mysql_server() < 0 || !mysql_ptr)
2215 goto out;
2216 if (argc < 2)
2217 conf.mysql_database_arg = para_strdup("paraslash");
2218 else {
2219 ret = -E_ESCAPE;
2220 conf.mysql_database_arg = escape_str(argv[1]);
2221 if (!conf.mysql_database_arg)
2222 goto out;
2223 }
2224 query = make_message("create database %s", conf.mysql_database_arg);
2225 ret = real_query(query);
2226 free(query);
2227 if (ret < 0)
2228 goto out;
2229 /* reconnect with database just created */
2230 mysql_close(mysql_ptr);
2231 ret = -E_MYSQL_INIT;
2232 if (init_mysql_server() < 0 || !mysql_ptr)
2233 goto out;
2234 mmd_lock();
2235 write_msg2mmd(1);
2236 mmd_unlock();
2237 ret = -E_QFAILED;
2238 if (real_query("create table data (name varchar(255) binary not null "
2239 "primary key, "
2240 "lastplayed datetime not null default "
2241 "'1970-01-01', "
2242 "numplayed int not null default 0, "
2243 "pic_id bigint unsigned not null default 1)") < 0)
2244 goto out;
2245 if (real_query("create table dir (name varchar(255) binary not null "
2246 "primary key, dir varchar(255) default null)") < 0)
2247 goto out;
2248 if (real_query("create table pics ("
2249 "id bigint(20) unsigned not null primary key "
2250 "auto_increment, "
2251 "name varchar(255) binary not null, "
2252 "pic mediumblob not null)") < 0)
2253 goto out;
2254 if (real_query("create table streams ("
2255 "name varchar(255) binary not null primary key, "
2256 "def blob not null)") < 0)
2257 goto out;
2258 if (real_query("insert into streams (name, def) values "
2259 "('current_stream', '(none)')") < 0)
2260 goto out;
2261 ret = send_va_buffer(fd, "successfully created database %s\n",
2262 conf.mysql_database_arg);
2263 out:
2264 return ret;
2265 }
2266
2267 static void shutdown_connection(void)
2268 {
2269 int ret;
2270
2271 if (mysql_ptr) {
2272 PARA_NOTICE_LOG("%s", "shutting down mysql connection\n");
2273 mysql_close(mysql_ptr);
2274 mysql_ptr = NULL;
2275 }
2276 if (mysql_lock) {
2277 ret = mutex_destroy(mysql_lock);
2278 if (ret < 0)
2279 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
2280 mysql_lock = 0;
2281 }
2282 }
2283
2284 /**
2285 * the init function of the mysql-based audio file selector
2286 *
2287 * \param db pointer to the struct to initialize
2288 *
2289 * Check the command line options and initialize all function pointers of \a
2290 * db. Connect to the mysql server and initialize the info string.
2291 *
2292 * \return This function returns success even if it could not connect
2293 * to the mysql server. This is because the connect is expected to fail
2294 * if there the paraslash database is not yet created. This gives the
2295 * user a chance to send the "cdb" to create the database.
2296 *
2297 * \sa struct audio_file_selector, misc_meta_data::selector_info,
2298 * random_selector.c
2299 */
2300 int mysql_selector_init(struct audio_file_selector *db)
2301 {
2302 int ret;
2303
2304 if (!conf.mysql_passwd_given)
2305 return -E_NO_MYSQL_PASSWD;
2306 if (!conf.mysql_audio_file_dir_given)
2307 return -E_NO_AF_DIR;
2308 db->name = "mysql";
2309 db->cmd_list = mysql_selector_cmds;
2310 db->get_audio_file_list = server_get_audio_file_list;
2311 db->update_audio_file = update_audio_file_server_handler;
2312 db->shutdown = shutdown_connection;
2313 ret = mutex_new();
2314 if (ret < 0)
2315 return ret;
2316 mysql_lock = ret;
2317 ret = init_mysql_server();
2318 if (ret < 0)
2319 PARA_WARNING_LOG("%s\n", PARA_STRERROR(-ret));
2320 write_msg2mmd(ret);
2321 return 1; /* return success even if connect failed to give the
2322 * user the chance to exec com_cdb
2323 */
2324 }