2 * Copyright (C) 1999-2007 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file mysql_selector.c para_server's mysql-based audio file selector */
21 /** \cond some internal constants */
22 #define MEDIUM_BLOB_SIZE 16777220 /* (2**24 + 4) */
23 #define BLOB_SIZE 65539 /* (2**16 + 3) */
25 #include "server.cmdline.h"
29 #include <mysql/mysql.h>
30 #include <mysql/mysql_version.h>
34 #include "user_list.h"
35 #include "mysql_selector_command_list.h"
37 /** pointer to the shared memory area */
38 extern struct misc_meta_data *mmd;
40 static void *mysql_ptr = NULL;
42 static struct para_macro macro_list[] = {
44 .replacement = "(data.%s != '1')"
47 .replacement = "(data.%s = '1')"
50 .replacement = "%sdata.Pic_Id"
53 .replacement = "(data.name like '%s')"
56 .replacement = "%sFLOOR((UNIX_TIMESTAMP(now())"
57 "-UNIX_TIMESTAMP(data.Lastplayed))/60)"
60 .replacement = "%sdata.Numplayed"
66 static int real_query(const char *query)
70 PARA_DEBUG_LOG("%s\n", query);
71 if (mysql_real_query(mysql_ptr, query, strlen(query))) {
72 PARA_ERROR_LOG("real_query error (%s)\n",
73 mysql_error(mysql_ptr));
80 * Use open connection given by mysql_ptr to query server. Returns a
81 * result pointer on succes and NULL on errors
83 static struct MYSQL_RES *get_result(const char *query)
87 if (real_query(query) < 0)
89 result = mysql_store_result(mysql_ptr);
91 PARA_ERROR_LOG("%s", "store_result error\n");
95 * write input from fd to dynamically allocated char array,
96 * but maximal max_size byte. Return size.
98 static int fd2buf(int fd, char **buf_ptr, size_t max_size)
100 const size_t chunk_size = 1024;
102 char *buf = para_malloc(size * sizeof(char)), *p = buf;
105 while ((ret = recv_bin_buffer(fd, p, chunk_size)) > 0) {
107 if ((p - buf) + chunk_size >= size) {
111 if (size > max_size) {
115 tmp = para_realloc(buf, size);
130 static char *escape_blob(const char* old, int size)
134 if (!mysql_ptr || size < 0)
136 new = para_malloc(2 * size * sizeof(char) + 1);
137 mysql_real_escape_string(mysql_ptr, new, old, size);
141 static char *escape_str(const char* old)
143 return escape_blob(old, strlen(old));
146 static char *escaped_basename(const char *name)
148 char *esc, *bn = para_basename(name);
152 esc = escape_str(bn);
160 int com_na(__a_unused int fd, int argc, char *argv[])
166 return -E_MYSQL_SYNTAX;
167 tmp = escape_str(argv[1]);
170 q = make_message("alter table data add %s char(1) "
171 "not null default 0", tmp);
181 int com_da(__a_unused int fd, int argc, char *argv[])
187 return -E_MYSQL_SYNTAX;
188 tmp = escape_str(argv[1]);
191 q = make_message("alter table data drop %s", tmp);
199 static int com_stradd_picadd(int fd, int argc, char *argv[])
201 char *blob = NULL, *esc_blob = NULL, *q = NULL, *tmp = NULL;
202 const char *fmt, *del_fmt;
203 int ret, stradd = strcmp(argv[0], "picadd");
207 return -E_MYSQL_SYNTAX;
208 if (strlen(argv[1]) >= MAXLINE - 1)
209 return -E_NAMETOOLONG;
214 fmt = "insert into streams (name, def) values ('%s','%s')";
215 del_fmt="delete from streams where name='%s'";
217 size = MEDIUM_BLOB_SIZE;
218 fmt = "insert into pics (name, pic) values ('%s','%s')";
219 del_fmt="delete from pics where pic='%s'";
221 tmp = escape_str(argv[1]);
224 q = make_message(del_fmt, tmp);
230 if ((ret = send_buffer(fd, AWAITING_DATA_MSG) < 0))
232 if ((ret = fd2buf(fd, &blob, size)) < 0)
238 esc_blob = escape_blob(blob, size);
241 tmp = escape_str(argv[1]);
244 q = make_message(fmt, tmp, esc_blob);
255 int com_stradd(int fd, int argc, char *argv[])
257 return com_stradd_picadd(fd, argc, argv);
261 int com_picadd(int fd, int argc, char *argv[])
263 return com_stradd_picadd(fd, argc, argv);
267 * print results to fd
269 static int print_results(int fd, void *result,
270 unsigned int top, unsigned int left,
271 unsigned int bottom, unsigned int right)
277 for (i = top; i <= bottom; i++) {
278 row = mysql_fetch_row(result);
281 for (j = left; j <= right; j++) {
282 ret = send_va_buffer(fd, j == left? "%s" : "\t%s",
283 row[j]? row[j] : "NULL");
287 ret = send_buffer(fd, "\n");
297 int com_verb(int fd, int argc, char *argv[])
301 unsigned int num_rows, num_fields;
305 return -E_MYSQL_SYNTAX;
306 tmp = escape_str(argv[1]);
309 result = get_result(tmp);
312 /* return success, because it's ok to have no results */
314 num_fields = mysql_field_count(mysql_ptr);
315 num_rows = mysql_num_rows(result);
317 if (num_fields && num_rows)
318 ret = print_results(fd, result, 0, 0, num_rows - 1,
320 mysql_free_result(result);
324 /* returns NULL on errors or if there are no atts defined yet */
325 static void *get_all_attributes(void)
327 void *result = get_result("desc data");
328 unsigned int num_rows;
332 num_rows = mysql_num_rows(result);
334 mysql_free_result(result);
337 mysql_data_seek(result, 4); /* skip Lastplayed, Numplayed... */
342 * list all attributes
344 int com_laa(int fd, int argc, __a_unused char *argv[])
350 return -E_MYSQL_SYNTAX;
351 result = get_all_attributes();
354 ret = print_results(fd, result, 0, 0, mysql_num_rows(result) - 5, 0);
355 mysql_free_result(result);
362 int com_hist(int fd, int argc, char *argv[]) {
366 unsigned int num_rows;
369 return -E_MYSQL_SYNTAX;
371 char *tmp = escape_str(argv[1]);
374 atts = make_message("where %s = '1'", tmp);
377 atts = para_strdup(NULL);
379 q = make_message("select name, to_days(now()) - to_days(lastplayed) from "
380 "data %s order by lastplayed", atts);
382 result = get_result(q);
386 num_rows = mysql_num_rows(result);
389 ret = print_results(fd, result, 0, 0, num_rows - 1, 1);
390 mysql_free_result(result);
395 * get last num audio files
397 int com_last(int fd, int argc, char *argv[])
408 return -E_MYSQL_SYNTAX;
409 q = make_message("select name from data order by lastplayed desc "
411 result = get_result(q);
415 ret = print_results(fd, result, 0, 0, mysql_num_rows(result) - 1, 0);
416 mysql_free_result(result);
420 int com_mbox(int fd, int argc, char *argv[])
425 unsigned int num_rows, num_fields;
426 char *query = para_strdup("select concat('From foo@localhost ', "
427 "date_format(Lastplayed, '%a %b %e %T %Y'), "
428 "'\nReceived: from\nTo: bar\n");
431 result = get_all_attributes();
435 while ((row = mysql_fetch_row(result))) {
440 tmp = make_message("%s X-Attribute-%s: ', %s, '\n", query,
445 query = para_strcat(query,
453 char *esc = escape_str(argv[1]), *tmp;
457 tmp = make_message("%s where name LIKE '%s'", query, esc);
462 mysql_free_result(result);
464 result = get_result(query);
467 ret = -E_EMPTY_RESULT;
468 num_fields = mysql_field_count(mysql_ptr);
469 num_rows = mysql_num_rows(result);
470 if (!num_fields || !num_rows)
472 ret = print_results(fd, result, 0, 0, num_rows - 1, num_fields - 1);
476 mysql_free_result(result);
480 /* get attributes by name. If verbose is not 0, get_a writes a string
481 * into atts of the form 'att1="0",att2="1"', which is used in com_cam
482 * for contructing a mysql update query.
483 * never returns NULL in *NON VERBOSE* mode
485 static char *get_atts(char *name, int verbose)
487 char *atts = NULL, *buf, *ebn;
488 void *result = NULL, *result2 = NULL;
491 unsigned int num_fields;
494 result2 = get_all_attributes();
498 if (!(ebn = escaped_basename(name)))
500 buf = make_message("select * from data where name='%s'", ebn);
503 result = get_result(buf);
507 ret = -E_EMPTY_RESULT;
508 num_fields = mysql_num_fields(result);
511 mysql_data_seek(result2, 4); /* skip Lastplayed, Numplayed... */
512 row = mysql_fetch_row(result);
516 for (i = 4; i < num_fields; i++) {
517 int is_set = row[i] && !strcmp(row[i], "1");
518 row2 = mysql_fetch_row(result2);
519 if (!row2 || !row2[0])
521 if (atts && (verbose || is_set))
522 atts = para_strcat(atts, verbose? "," : " ");
523 if (is_set || verbose)
524 atts = para_strcat(atts, row2[0]);
526 atts = para_strcat(atts, is_set? "=\"1\"" : "=\"0\"");
531 mysql_free_result(result2);
533 mysql_free_result(result);
534 if (!atts && !verbose)
535 atts = para_strdup("(none)");
539 /* never returns NULL in verbose mode */
540 static char *get_meta(char *name, int verbose)
544 char *ebn, *q, *ret = NULL;
545 const char *verbose_fmt =
546 "select concat('lastplayed: ', "
547 "(to_days(now()) - to_days(lastplayed)),"
548 "' day(s). numplayed: ', numplayed, "
549 "', pic: ', pic_id) "
550 "from data where name = '%s'";
551 /* is that really needed? */
552 const char *fmt = "select concat('lastplayed=\\'', lastplayed, "
553 "'\\', numplayed=\\'', numplayed, "
554 "'\\', pic_id=\\'', pic_id, '\\'') "
555 "from data where name = '%s'";
557 if (!(ebn = escaped_basename(name)))
559 q = make_message(verbose? verbose_fmt : fmt, ebn);
561 result = get_result(q);
565 row = mysql_fetch_row(result);
568 ret = para_strdup(row[0]);
571 mysql_free_result(result);
573 ret = para_strdup("(not yet played)");
577 static char *get_dir(char *name)
579 char *ret = NULL, *q, *ebn;
583 if (!(ebn = escaped_basename(name)))
585 q = make_message("select dir from dir where name = '%s'", ebn);
587 result = get_result(q);
591 row = mysql_fetch_row(result);
593 ret = para_strdup(row[0]);
594 mysql_free_result(result);
598 /* never returns NULL */
599 static char *get_current_stream(void)
603 void *result = get_result("select def from streams where "
604 "name = 'current_stream'");
608 row = mysql_fetch_row(result);
611 ret = para_strdup(row[0]);
612 mysql_free_result(result);
616 mysql_free_result(result);
617 return para_strdup("(none)");
621 * Read stream definition of stream streamname and construct mysql
622 * query. Return NULL on errors. If streamname is NULL, use current
623 * stream. If that is also NULL, use query that selects everything.
624 * If filename is NULL, query will list everything, otherwise only
625 * the score of given file.
627 static char *get_query(char *streamname, char *filename, int with_path)
629 char *accept_opts = NULL, *deny_opts = NULL, *score = NULL;
630 char *where_clause, *order, *query;
631 char command[255] = ""; /* buffer for sscanf */
635 char *select_clause = NULL;
637 tmp = get_current_stream();
639 tmp = escape_str(streamname);
643 if (!strcmp(tmp, "(none)")) {
646 char *ret, *ebn = escaped_basename(filename);
649 ret = make_message("select to_days(now()) - "
650 "to_days(lastplayed) from data "
651 "where name = '%s'", ebn);
657 "select concat(dir.dir, '/', dir.name) "
658 "from data, dir where dir.name = data.name "
659 "order by data.lastplayed"
662 "select name from data where name is not NULL "
663 "order by lastplayed"
667 query = make_message("select def from streams where name = '%s'",
669 result = get_result(query);
674 row = mysql_fetch_row(result);
680 char *arg, *line = end;
682 if (!(end = strchr(line, '\n')))
686 if (sscanf(line, "%200s%n", command, &n) < 1)
689 if (!strcmp(command, "accept:")) {
690 char *tmp2 = s_a_r_list(macro_list, arg);
692 accept_opts = para_strcat(
693 accept_opts, " or ");
694 accept_opts = para_strcat(accept_opts, tmp2);
698 if (!strcmp(command, "deny:")) {
699 char *tmp2 = s_a_r_list(macro_list, arg);
701 deny_opts = para_strcat(deny_opts, " or ");
702 deny_opts = para_strcat(deny_opts, tmp2);
706 if (!score && !strcmp(command, "score:"))
707 score = s_a_r_list(macro_list, arg);
710 score = s_a_r_list(macro_list, conf.mysql_default_score_arg);
715 char *ebn = escaped_basename(filename);
718 select_clause = make_message("select %s from data ", score);
720 where_clause = make_message( "where name = '%s' ", ebn);
722 order = para_strdup("");
725 select_clause = para_strdup(with_path?
726 "select concat(dir.dir, '/', dir.name) from data, dir "
727 "where dir.name = data.name "
729 "select name from data where name is not NULL");
730 order = make_message("order by -(%s)", score);
732 if (accept_opts && deny_opts) {
733 where_clause = make_message("and ((%s) and not (%s)) ",
734 accept_opts, deny_opts);
737 if (accept_opts && !deny_opts) {
738 where_clause = make_message("and (%s) ", accept_opts);
741 if (!accept_opts && deny_opts) {
742 where_clause = make_message("and not (%s) ", deny_opts);
745 where_clause = para_strdup("");
747 query = make_message("%s %s %s", select_clause, where_clause, order);
757 mysql_free_result(result);
764 * This is called from server and from some commands. Name must not be NULL
765 * Never returns NULL.
767 static char *get_selector_info(char *name)
769 char *meta = NULL, *atts = NULL, *info, *dir = NULL, *query, *stream = NULL;
771 MYSQL_ROW row = NULL;
774 return para_strdup("(none)");
775 stream = get_current_stream();
776 meta = get_meta(name, 1);
777 atts = get_atts(name, 0);
780 query = get_query(stream, name, 0); /* FIXME: pass stream == NULL instead? */
783 result = get_result(query);
786 row = mysql_fetch_row(result);
788 info = make_message("dbinfo1:dir: %s\n"
789 "dbinfo2:stream: %s, %s, score: %s\n"
791 dir? dir : "(not contained in table)",
793 (result && row && row[0])? row[0] : "(no score)",
804 mysql_free_result(result);
809 /* might return NULL */
810 static char *get_current_audio_file(void)
814 name = para_basename(mmd->filename);
819 /* list attributes / print database info */
820 static int com_la_info(int fd, int argc, char *argv[])
822 char *name = NULL, *meta = NULL, *atts = NULL, *dir = NULL;
823 int ret, com_la = strcmp(argv[0], "info");
826 ret = -E_GET_AUDIO_FILE;
827 if (!(name = get_current_audio_file()))
829 ret = send_va_buffer(fd, "%s\n", name);
834 if (!(name = escaped_basename(argv[1])))
837 meta = get_meta(name, 1);
838 atts = get_atts(name, 0);
841 ret = send_va_buffer(fd, "%s\n", atts);
843 ret = send_va_buffer(fd, "dir: %s\n" "%s\n" "attributes: %s\n",
844 dir? dir : "(not contained in table)", meta, atts);
853 /* list attributes */
854 int com_la(int fd, int argc, char *argv[])
856 return com_la_info(fd, argc, argv);
859 /* print database info */
860 int com_info(int fd, int argc, char *argv[])
862 return com_la_info(fd, argc, argv);
865 static int change_stream(const char *stream)
869 query = make_message("update streams set def='%s' "
870 "where name = 'current_stream'", stream);
871 ret = real_query(query);
876 static int get_pic_id_by_name(char *name)
883 if (!(ebn = escaped_basename(name)))
885 q = make_message("select pic_id from data where name = '%s'", ebn);
887 result = get_result(q);
891 row = mysql_fetch_row(result);
895 mysql_free_result(result);
899 static int remove_entry(const char *name)
901 char *q, *ebn = escaped_basename(name);
906 q = make_message("delete from data where name = '%s'", ebn);
907 real_query(q); /* ignore errors */
909 q = make_message("delete from dir where name = '%s'", ebn);
910 real_query(q); /* ignore errors */
918 static int add_entry(const char *name)
920 char *q, *dn, *ebn = NULL, *edn = NULL;
924 return -E_MYSQL_SYNTAX;
925 ebn = escaped_basename(name);
928 ret = -E_MYSQL_SYNTAX;
929 dn = para_dirname(name);
933 edn = escape_str(dn);
937 q = make_message("insert into data (name, pic_id) values "
938 "('%s', '%s')", ebn, "1");
940 // ret = 1; PARA_DEBUG_LOG("q: %s\n", q);
944 q = make_message("insert into dir (name, dir) values "
945 "('%s', '%s')", ebn, edn);
946 // ret = 1; PARA_DEBUG_LOG("q: %s\n", q);
960 static int com_rm_ne(__a_unused int fd, int argc, char *argv[])
962 int ne = !strcmp(argv[0], "ne");
965 return -E_MYSQL_SYNTAX;
966 for (i = 1; i < argc; i++) {
967 ret = remove_entry(argv[i]);
972 ret = add_entry(argv[i]);
982 int com_rm(int fd, int argc, char *argv[])
984 return com_rm_ne(fd, argc, argv);
990 int com_ne(int fd, int argc, char *argv[])
992 return com_ne(fd, argc, argv);
998 int com_mv(__a_unused int fd, int argc, char *argv[])
1000 char *q, *dn, *ebn1 = NULL, *ebn2 = NULL, *edn = NULL;
1004 return -E_MYSQL_SYNTAX;
1006 ebn1 = escaped_basename(argv[1]);
1007 ebn2 = escaped_basename(argv[2]);
1008 if (!ebn1 || !ebn2 || !*ebn1 || !*ebn2)
1010 ret = -E_MYSQL_SYNTAX;
1011 if (!strcmp(ebn1, ebn2))
1013 remove_entry(argv[2]); /* no need to escape, ignore error */
1014 q = make_message("update data set name = '%s' where name = '%s'",
1016 ret = real_query(q);
1020 ret = -E_AUDIO_FILE;
1021 if (!mysql_affected_rows(mysql_ptr))
1023 q = make_message("update dir set name = '%s' where name = '%s'",
1025 ret = real_query(q);
1031 dn = para_dirname(argv[2]);
1035 edn = escape_str(dn);
1042 q = make_message("update dir set dir = '%s' where name = '%s'",
1044 ret = real_query(q);
1056 static int com_set(__a_unused int fd, int argc, char *argv[])
1061 const char *field = strcmp(argv[0], "picass")? "numplayed" : "pic_id";
1064 return -E_MYSQL_SYNTAX;
1066 for (i = 2; i < argc; i++) {
1067 ebn = escaped_basename(argv[i]);
1070 q = make_message("update data set %s = %lu "
1071 "where name = '%s'", field, id, ebn);
1073 ret = real_query(q);
1082 * snp: set numplayed
1084 int com_picass(int fd, int argc, char *argv[])
1086 return com_set(fd, argc, argv);
1090 * snp: set numplayed
1092 int com_snp(int fd, int argc, char *argv[])
1094 return com_set(fd, argc, argv);
1098 * picch: change entry's name in pics table
1100 int com_picch(__a_unused int fd, int argc, char *argv[])
1107 return -E_MYSQL_SYNTAX;
1110 tmp = escape_str(argv[2]);
1113 q = make_message("update pics set name = '%s' where id = %lu", tmp, id);
1115 ret = real_query(q);
1121 * piclist: print list of pics in db
1123 int com_piclist(__a_unused int fd, int argc, __a_unused char *argv[])
1125 void *result = NULL;
1127 unsigned long *length;
1131 return -E_MYSQL_SYNTAX;
1132 result = get_result("select id,name,pic from pics order by id");
1135 while ((row = mysql_fetch_row(result))) {
1136 length = mysql_fetch_lengths(result);
1137 if (!row || !row[0] || !row[1] || !row[2])
1139 ret = send_va_buffer(fd, "%s\t%lu\t%s\n", row[0], length[2], row[1]);
1145 mysql_free_result(result);
1150 * picdel: delete picture from database
1152 int com_picdel(int fd, int argc, char *argv[])
1160 return -E_MYSQL_SYNTAX;
1161 for (i = 1; i < argc; i++) {
1163 q = make_message("delete from pics where id = %lu", id);
1164 ret = real_query(q);
1168 aff = mysql_affected_rows(mysql_ptr);
1170 ret = send_va_buffer(fd, "No such id: %lu\n", id);
1175 q = make_message("update data set pic_id = 1 where pic_id = %lu", id);
1176 ret = real_query(q);
1182 * pic: get picture by name or by number
1184 int com_pic(int fd, int argc, char *argv[])
1186 void *result = NULL;
1188 unsigned long *length, id;
1190 char *q, *name = NULL;
1193 ret = -E_GET_AUDIO_FILE;
1194 name = get_current_audio_file();
1197 name = escaped_basename(argv[1]);
1202 id = atoi(name + 1);
1204 id = get_pic_id_by_name(name);
1208 q = make_message("select pic from pics where id = '%lu'", id);
1209 result = get_result(q);
1213 row = mysql_fetch_row(result);
1215 if (!row || !row[0])
1217 length = mysql_fetch_lengths(result);
1218 ret = send_bin_buffer(fd, row[0], *length);
1220 mysql_free_result(result);
1225 int com_strdel(__a_unused int fd, int argc, char *argv[])
1231 return -E_MYSQL_SYNTAX;
1232 tmp = escape_str(argv[1]);
1235 q = make_message("delete from streams where name='%s'", tmp);
1237 ret = real_query(q);
1245 int com_ls(int fd, int argc, char *argv[])
1250 unsigned int num_rows;
1253 return -E_MYSQL_SYNTAX;
1255 char *tmp = escape_str(argv[1]);
1258 q = make_message("select name from data where name like '%s'",
1262 q = para_strdup("select name from data");
1263 result = get_result(q);
1267 num_rows = mysql_num_rows(result);
1270 ret = print_results(fd, result, 0, 0, num_rows - 1, 0);
1271 mysql_free_result(result);
1278 int com_summary(__a_unused int fd, int argc, __a_unused char *argv[])
1283 void *result2 = NULL;
1284 const char *fmt = "select count(name) from data where %s='1'";
1285 int ret = -E_NORESULT;
1288 return -E_MYSQL_SYNTAX;
1289 result = get_all_attributes();
1292 while ((row = mysql_fetch_row(result))) {
1299 buf = make_message(fmt, row[0]);
1300 result2 = get_result(buf);
1305 row2 = mysql_fetch_row(result2);
1306 if (!row2 || !row2[0])
1308 ret = send_va_buffer(fd, "%s\t%s\n", row[0], row2[0]);
1315 mysql_free_result(result2);
1317 mysql_free_result(result);
1321 static int get_numplayed(char *name)
1325 const char *fmt = "select numplayed from data where name = '%s'";
1326 char *buf = make_message(fmt, name);
1327 int ret = -E_NORESULT;
1329 result = get_result(buf);
1334 row = mysql_fetch_row(result);
1335 if (!row || !row[0])
1340 mysql_free_result(result);
1344 static int update_audio_file(char *name)
1347 const char *fmt1 = "update data set lastplayed = now() where name = '%s'";
1348 const char *fmt2 = "update data set numplayed = %i where name = '%s'";
1350 char *ebn = escaped_basename(name);
1355 q = make_message(fmt1, ebn);
1356 ret = real_query(q);
1360 ret = get_numplayed(ebn);
1363 q = make_message(fmt2, ret + 1, ebn);
1364 ret = real_query(q);
1371 /* If called as child, mmd_lock must be held */
1372 static void update_mmd(char *info)
1374 PARA_DEBUG_LOG("%s", "updating shared memory area\n");
1375 strncpy(mmd->selector_info, info, MMD_INFO_SIZE - 1);
1376 mmd->selector_info[MMD_INFO_SIZE - 1] = '\0';
1379 static void update_audio_file_server_handler(char *name)
1382 info = get_selector_info(name);
1385 update_audio_file(name);
1388 int com_us(__a_unused int fd, int argc, char *argv[])
1394 return -E_MYSQL_SYNTAX;
1395 tmp = escape_str(argv[1]);
1398 ret = update_audio_file(argv[1]);
1403 static void refresh_selector_info(void)
1405 char *name = get_current_audio_file();
1410 info = get_selector_info(name);
1418 /* select previous / next stream */
1419 static int com_ps_ns(__a_unused int fd, int argc, char *argv[])
1421 char *query, *stream = get_current_stream();
1422 void *result = get_result("select name from streams");
1424 int match = -1, ret, i;
1425 unsigned int num_rows;
1428 return -E_MYSQL_SYNTAX;
1432 num_rows = mysql_num_rows(result);
1433 ret = -E_EMPTY_RESULT;
1437 for (i = 0; i < num_rows; i++) {
1438 row = mysql_fetch_row(result);
1439 if (!row || !row[0])
1441 if (!strcmp(row[0], "current_stream"))
1443 if (!strcmp(row[0], stream)) {
1451 if (!strcmp(argv[0], "ps"))
1452 i = match > 0? match - 1 : num_rows - 1;
1454 i = match < num_rows - 1? match + 1 : 0;
1456 mysql_data_seek(result, i);
1457 row = mysql_fetch_row(result);
1458 if (!row || !row[0])
1460 if (!strcmp(row[0], "current_stream")) {
1461 if (!strcmp(argv[0], "ps")) {
1463 i = i < 0? i + num_rows : i;
1466 i = i > num_rows - 1? i - num_rows : i;
1468 mysql_data_seek(result, i);
1469 row = mysql_fetch_row(result);
1470 if (!row || !row[0])
1473 query = make_message("update streams set def='%s' where name = "
1474 "'current_stream'", row[0]);
1475 ret = real_query(query);
1477 refresh_selector_info();
1481 mysql_free_result(result);
1485 /* select previous stream */
1486 int com_ps(int fd, int argc, char *argv[])
1488 return com_ps_ns(fd, argc, argv);
1491 /* select next stream */
1492 int com_ns(int fd, int argc, char *argv[])
1494 return com_ps_ns(fd, argc, argv);
1498 int com_streams(int fd, int argc, __a_unused char *argv[])
1500 unsigned int num_rows;
1501 int i, ret = -E_NORESULT;
1505 if (argc > 1 && strcmp(argv[1], "current_stream"))
1506 return -E_MYSQL_SYNTAX;
1508 char *cs = get_current_stream();
1509 ret = send_va_buffer(fd, "%s\n", cs);
1513 result = get_result("select name from streams");
1516 num_rows = mysql_num_rows(result);
1521 for (i = 0; i < num_rows; i++) {
1522 row = mysql_fetch_row(result);
1523 if (!row || !row[0])
1525 if (strcmp(row[0], "current_stream"))
1526 send_va_buffer(fd, "%s\n", row[0]);
1531 mysql_free_result(result);
1535 /* query stream definition */
1536 int com_strq(int fd, int argc, char *argv[])
1544 ret = -E_GET_STREAM;
1545 name = get_current_stream();
1548 name = escaped_basename(argv[1]);
1553 query = make_message("select def from streams where name='%s'", name);
1555 result = get_result(query);
1560 row = mysql_fetch_row(result);
1561 if (!row || !row[0])
1563 /* no '\n' needed */
1564 ret = send_buffer(fd, row[0]);
1567 mysql_free_result(result);
1571 /* change stream / change stream and play */
1572 static int com_cs_csp(int fd, int argc, char *argv[])
1574 int ret, stream_change;
1575 char *query, *stream = NULL;
1576 char *old_stream = get_current_stream();
1577 int csp = !strcmp(argv[0], "csp");
1579 ret = -E_MYSQL_SYNTAX;
1585 ret = send_va_buffer(fd, "%s\n", old_stream);
1589 /* test if stream is valid, no need to escape argv[1] */
1590 query = get_query(argv[1], NULL, 0);
1595 stream = escape_str(argv[1]);
1598 stream_change = strcmp(stream, old_stream);
1599 if (stream_change) {
1600 ret = change_stream(stream);
1603 refresh_selector_info();
1607 mmd->new_vss_status_flags |= VSS_PLAYING;
1609 mmd->new_vss_status_flags |= VSS_NEXT;
1620 int com_cs(int fd, int argc, char *argv[])
1622 return com_cs_csp(fd, argc, argv);
1625 /* change stream and play */
1626 int com_csp(int fd, int argc, char *argv[])
1628 return com_cs_csp(fd, argc, argv);
1631 /* score list / skip */
1632 static int com_sl_skip(int fd, int argc, char *argv[])
1634 void *result = NULL;
1636 int ret, i, skip = !strcmp(argv[0], "skip");
1637 char *query, *stream, *tmp;
1638 unsigned int num_rows, num;
1641 return -E_MYSQL_SYNTAX;
1642 num = atoi(argv[1]);
1644 return -E_MYSQL_SYNTAX;
1646 stream = get_current_stream();
1648 return -E_GET_STREAM;
1650 stream = escape_str(argv[2]);
1654 tmp = get_query(stream, NULL, 0);
1657 return -E_GET_QUERY;
1658 query = make_message("%s limit %d", tmp, num);
1661 result = get_result(query);
1665 ret = -E_EMPTY_RESULT;
1666 num_rows = mysql_num_rows(result);
1669 for (i = 0; i < num_rows && i < num; i++) {
1670 row = mysql_fetch_row(result);
1672 send_va_buffer(fd, "Skipping %s\n", row[0]);
1673 update_audio_file(row[0]);
1675 send_va_buffer(fd, "%s\n", row[0]? row[0]: "BUG");
1680 mysql_free_result(result);
1685 int com_sl(int fd, int argc, char *argv[])
1687 return com_sl_skip(fd, argc, argv);
1691 int com_skip(int fd, int argc, char *argv[])
1693 return com_sl_skip(fd, argc, argv);
1697 * update attributes of name
1699 static int update_atts(int fd, char *name, char *atts)
1702 char *ebn, *q, *old, *new = NULL;
1706 ebn = escaped_basename(name);
1709 q = make_message("update data set %s where name = '%s'", atts, ebn);
1710 old = get_atts(ebn, 0);
1711 send_va_buffer(fd, "old: %s\n", old);
1713 ret = real_query(q);
1717 new = get_atts(ebn, 0);
1718 ret = send_va_buffer(fd, "new: %s\n", new);
1728 int com_sa(int fd, int argc, char *argv[])
1731 char *atts = NULL, *name;
1734 return -E_MYSQL_SYNTAX;
1735 for (i = 1; i < argc; i++) {
1737 char *esc, *tmp, *p =argv[i];
1738 int len = strlen(p);
1742 switch (p[len - 1]) {
1753 esc = escape_str(p);
1756 tmp = make_message("%s%s='%s'", atts? "," : "", esc,
1759 atts = para_strcat(atts, tmp);
1765 if (i >= argc) { /* no name given, use current af */
1766 ret = -E_GET_AUDIO_FILE;
1767 if (!(name = get_current_audio_file()))
1769 ret = update_atts(fd, name, atts);
1773 for (; argv[i] && ret >= 0; i++)
1774 ret = update_atts(fd, argv[i], atts);
1776 refresh_selector_info();
1785 int com_cam(int fd, int argc, char *argv[])
1787 char *name = NULL, *meta = NULL, *atts = NULL;
1791 return -E_MYSQL_SYNTAX;
1792 if (!(name = escaped_basename(argv[1])))
1795 if (!(atts = get_atts(name, 1)))
1798 if (!(meta = get_meta(name, 0)))
1800 for (i = 2; i < argc; i++) {
1803 if (!(ebn = escaped_basename(argv[i])))
1805 ret = send_va_buffer(fd, "updating %s\n", ebn);
1810 q = make_message("update data set %s where name = '%s'",
1812 if ((ret = update_atts(fd, ebn, atts)) >= 0)
1813 ret = real_query(q);
1833 static int com_vrfy_clean(int fd, int argc, __a_unused char *argv[])
1836 int ret, vrfy_mode = strcmp(argv[0], "clean");
1837 void *result = NULL;
1838 unsigned int num_rows;
1843 return -E_MYSQL_SYNTAX;
1845 result = get_result("select data.name from data left join dir on "
1846 "dir.name = data.name where dir.name is NULL");
1849 num_rows = mysql_num_rows(result);
1851 ret = send_buffer(fd, "No invalid entries\n");
1855 send_va_buffer(fd, "found %i invalid entr%s\n", num_rows,
1856 num_rows == 1? "y" : "ies");
1857 ret = print_results(fd, result, 0, 0, num_rows - 1, 0);
1860 while ((row = mysql_fetch_row(result))) {
1865 escaped_name = escape_str(row[0]);
1868 send_va_buffer(fd, "deleting %s\n", escaped_name);
1869 query = make_message("delete from data where name = '%s'",
1871 ret = real_query(query);
1879 mysql_free_result(result);
1886 int com_vrfy(int fd, int argc, char **argv)
1888 return com_vrfy_clean(fd, argc, argv);
1894 int com_clean(int fd, int argc, char **argv)
1896 return com_vrfy_clean(fd, argc, argv);
1899 static FILE *out_file;
1901 static int mysql_write_tmp_file(const char *dir, const char *name)
1903 int ret = -E_TMPFILE;
1904 char *msg = make_message("%s\t%s\n", dir, name);
1905 if (fputs(msg, out_file) != EOF)
1914 int com_upd(int fd, int argc, __a_unused char *argv[])
1916 char *tempname = NULL, *query = NULL;
1917 int ret, out_fd = -1, num = 0;
1918 void *result = NULL;
1919 unsigned int num_rows;
1923 return -E_MYSQL_SYNTAX;
1925 tempname = para_strdup("/tmp/mysql.tmp.XXXXXX");
1926 ret = para_mkstemp(tempname, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
1930 out_file = fdopen(out_fd, "w");
1935 if (find_audio_files(conf.mysql_audio_file_dir_arg, mysql_write_tmp_file) < 0)
1937 num = ftell(out_file);
1939 * we have to make sure the file hit the disk before we call
1944 PARA_DEBUG_LOG("wrote tempfile %s (%d bytes)\n", tempname, num);
1947 if ((ret = real_query("delete from dir")) < 0)
1949 query = make_message("load data infile '%s' ignore into table dir "
1950 "fields terminated by '\t' lines terminated by '\n' "
1951 "(dir, name)", tempname);
1952 ret = real_query(query);
1956 result = get_result("select dir.name from dir left join data on "
1957 "data.name = dir.name where data.name is NULL");
1961 num_rows = mysql_num_rows(result);
1963 ret = send_buffer(fd, "no new entries\n");
1966 while ((row = mysql_fetch_row(result))) {
1971 send_va_buffer(fd, "new entry: %s\n", row[0]);
1972 erow = escape_str(row[0]);
1975 query = make_message("insert into data (name, pic_id) values "
1976 "('%s','%s')", erow, "1");
1978 ret = real_query(query);
1991 mysql_free_result(result);
1995 static char **server_get_audio_file_list(unsigned int num)
1997 char **list = para_malloc((num + 1) * sizeof(char *));
1998 char *tmp, *query, *stream = get_current_stream();
1999 void *result = NULL;
2000 unsigned int num_rows;
2004 tmp = get_query(stream, NULL, 1);
2008 query = make_message("%s limit %d", tmp, num);
2010 result = get_result(query);
2014 num_rows = mysql_num_rows(result);
2017 for (i = 0; i < num_rows && i < num; i++) {
2018 row = mysql_fetch_row(result);
2019 if (!row || !row[0])
2021 list[i] = para_strdup(row[0]);
2034 mysql_free_result(result);
2039 * connect to mysql server, return mysql pointer on success, -E_NOTCONN
2040 * on errors. Called from parent on startup and also from com_cdb().
2042 static int init_mysql_server(void)
2044 char *u = conf.mysql_user_arg? conf.mysql_user_arg : para_logname();
2046 mysql_ptr = mysql_init(NULL);
2048 PARA_CRIT_LOG("%s", "mysql init error\n");
2051 PARA_DEBUG_LOG("connecting: %s@%s:%d\n", u, conf.mysql_host_arg,
2052 conf.mysql_port_arg);
2053 if (!conf.mysql_user_arg)
2056 * If host is NULL a connection to the local host is assumed,
2057 * If user is NULL, the current user is assumed
2059 if (!(mysql_ptr = mysql_real_connect(mysql_ptr,
2060 conf.mysql_host_arg,
2061 conf.mysql_user_arg,
2062 conf.mysql_passwd_arg,
2063 conf.mysql_database_arg,
2064 conf.mysql_port_arg, NULL, 0))) {
2065 PARA_CRIT_LOG("%s", "connect error\n");
2068 PARA_INFO_LOG("%s", "success\n");
2072 /* mmd lock must be held */
2073 static void write_msg2mmd(int success)
2075 sprintf(mmd->selector_info, "dbinfo1:%s\ndbinfo2:mysql-%s\ndbinfo3:\n",
2076 success < 0? PARA_STRERROR(-success) :
2077 "successfully connected to mysql server",
2078 success < 0? "" : mysql_get_server_info(mysql_ptr));
2081 /* create database */
2082 int com_cdb(int fd, int argc, char *argv[])
2088 PARA_INFO_LOG("%s", "closing database\n");
2089 mysql_close(mysql_ptr);
2091 /* dont use any database */
2092 conf.mysql_database_arg = NULL; /* leak? */
2093 ret = -E_MYSQL_INIT;
2094 if (init_mysql_server() < 0 || !mysql_ptr)
2097 conf.mysql_database_arg = para_strdup("paraslash");
2100 conf.mysql_database_arg = escape_str(argv[1]);
2101 if (!conf.mysql_database_arg)
2104 query = make_message("create database %s", conf.mysql_database_arg);
2105 ret = real_query(query);
2109 /* reconnect with database just created */
2110 mysql_close(mysql_ptr);
2111 ret = -E_MYSQL_INIT;
2112 if (init_mysql_server() < 0 || !mysql_ptr)
2118 if (real_query("create table data (name varchar(255) binary not null "
2120 "lastplayed datetime not null default "
2122 "numplayed int not null default 0, "
2123 "pic_id bigint unsigned not null default 1)") < 0)
2125 if (real_query("create table dir (name varchar(255) binary not null "
2126 "primary key, dir varchar(255) default null)") < 0)
2128 if (real_query("create table pics ("
2129 "id bigint(20) unsigned not null primary key "
2131 "name varchar(255) binary not null, "
2132 "pic mediumblob not null)") < 0)
2134 if (real_query("create table streams ("
2135 "name varchar(255) binary not null primary key, "
2136 "def blob not null)") < 0)
2138 if (real_query("insert into streams (name, def) values "
2139 "('current_stream', '(none)')") < 0)
2141 ret = send_va_buffer(fd, "successfully created database %s\n",
2142 conf.mysql_database_arg);
2147 static void shutdown_connection(void)
2150 PARA_NOTICE_LOG("%s", "shutting down mysql connection\n");
2151 mysql_close(mysql_ptr);
2157 * the init function of the mysql-based audio file selector
2159 * Check the command line options and initialize all function pointers of \a db.
2160 * Connect to the mysql server and initialize the info string.
2162 * \sa struct audio_file_selector, misc_meta_data::selector_info,
2165 int mysql_selector_init(struct audio_file_selector *db)
2169 if (!conf.mysql_passwd_given)
2170 return -E_NO_MYSQL_PASSWD;
2171 if (!conf.mysql_audio_file_dir_given)
2172 return -E_NO_AF_DIR;
2174 db->cmd_list = mysql_selector_cmds;
2175 db->get_audio_file_list = server_get_audio_file_list;
2176 db->update_audio_file = update_audio_file_server_handler;
2177 db->shutdown = shutdown_connection;
2178 ret = init_mysql_server();
2180 PARA_WARNING_LOG("%s\n", PARA_STRERROR(-ret));
2182 return 1; /* return success even if connect failed to give the
2183 * user the chance to exec com_cdb