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
, *atts
, *info
, *dir
, *query
, *stream
;
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)",
800 mysql_free_result(result
);
805 /* might return NULL */
806 static char *get_current_audio_file(void)
810 name
= para_basename(mmd
->filename
);
815 /* list attributes / print database info */
816 static int com_la_info(int fd
, int argc
, char *argv
[])
818 char *name
= NULL
, *meta
= NULL
, *atts
= NULL
, *dir
= NULL
;
819 int ret
, com_la
= strcmp(argv
[0], "info");
822 ret
= -E_GET_AUDIO_FILE
;
823 if (!(name
= get_current_audio_file()))
825 ret
= send_va_buffer(fd
, "%s\n", name
);
830 if (!(name
= escaped_basename(argv
[1])))
833 meta
= get_meta(name
, 1);
834 atts
= get_atts(name
, 0);
837 ret
= send_va_buffer(fd
, "%s\n", atts
);
839 ret
= send_va_buffer(fd
, "dir: %s\n" "%s\n" "attributes: %s\n",
840 dir
? dir
: "(not contained in table)", meta
, atts
);
849 /* list attributes */
850 int com_la(int fd
, int argc
, char *argv
[])
852 return com_la_info(fd
, argc
, argv
);
855 /* print database info */
856 int com_info(int fd
, int argc
, char *argv
[])
858 return com_la_info(fd
, argc
, argv
);
861 static int change_stream(const char *stream
)
865 query
= make_message("update streams set def='%s' "
866 "where name = 'current_stream'", stream
);
867 ret
= real_query(query
);
872 static int get_pic_id_by_name(char *name
)
879 if (!(ebn
= escaped_basename(name
)))
881 q
= make_message("select pic_id from data where name = '%s'", ebn
);
883 result
= get_result(q
);
887 row
= mysql_fetch_row(result
);
891 mysql_free_result(result
);
895 static int remove_entry(const char *name
)
897 char *q
, *ebn
= escaped_basename(name
);
902 q
= make_message("delete from data where name = '%s'", ebn
);
903 real_query(q
); /* ignore errors */
905 q
= make_message("delete from dir where name = '%s'", ebn
);
906 real_query(q
); /* ignore errors */
914 static int add_entry(const char *name
)
916 char *q
, *dn
, *ebn
= NULL
, *edn
= NULL
;
920 return -E_MYSQL_SYNTAX
;
921 ebn
= escaped_basename(name
);
924 ret
= -E_MYSQL_SYNTAX
;
925 dn
= para_dirname(name
);
929 edn
= escape_str(dn
);
933 q
= make_message("insert into data (name, pic_id) values "
934 "('%s', '%s')", ebn
, "1");
936 // ret = 1; PARA_DEBUG_LOG("q: %s\n", q);
940 q
= make_message("insert into dir (name, dir) values "
941 "('%s', '%s')", ebn
, edn
);
942 // ret = 1; PARA_DEBUG_LOG("q: %s\n", q);
956 static int com_rm_ne(__a_unused
int fd
, int argc
, char *argv
[])
958 int ne
= !strcmp(argv
[0], "ne");
961 return -E_MYSQL_SYNTAX
;
962 for (i
= 1; i
< argc
; i
++) {
963 ret
= remove_entry(argv
[i
]);
968 ret
= add_entry(argv
[i
]);
978 int com_rm(int fd
, int argc
, char *argv
[])
980 return com_rm_ne(fd
, argc
, argv
);
986 int com_ne(int fd
, int argc
, char *argv
[])
988 return com_ne(fd
, argc
, argv
);
994 int com_mv(__a_unused
int fd
, int argc
, char *argv
[])
996 char *q
, *dn
, *ebn1
= NULL
, *ebn2
= NULL
, *edn
= NULL
;
1000 return -E_MYSQL_SYNTAX
;
1002 ebn1
= escaped_basename(argv
[1]);
1003 ebn2
= escaped_basename(argv
[2]);
1004 if (!ebn1
|| !ebn2
|| !*ebn1
|| !*ebn2
)
1006 ret
= -E_MYSQL_SYNTAX
;
1007 if (!strcmp(ebn1
, ebn2
))
1009 remove_entry(argv
[2]); /* no need to escape, ignore error */
1010 q
= make_message("update data set name = '%s' where name = '%s'",
1012 ret
= real_query(q
);
1016 ret
= -E_AUDIO_FILE
;
1017 if (!mysql_affected_rows(mysql_ptr
))
1019 q
= make_message("update dir set name = '%s' where name = '%s'",
1021 ret
= real_query(q
);
1027 dn
= para_dirname(argv
[2]);
1031 edn
= escape_str(dn
);
1038 q
= make_message("update dir set dir = '%s' where name = '%s'",
1040 ret
= real_query(q
);
1052 static int com_set(__a_unused
int fd
, int argc
, char *argv
[])
1057 const char *field
= strcmp(argv
[0], "picass")? "numplayed" : "pic_id";
1060 return -E_MYSQL_SYNTAX
;
1062 for (i
= 2; i
< argc
; i
++) {
1063 ebn
= escaped_basename(argv
[i
]);
1066 q
= make_message("update data set %s = %lu "
1067 "where name = '%s'", field
, id
, ebn
);
1069 ret
= real_query(q
);
1078 * snp: set numplayed
1080 int com_picass(int fd
, int argc
, char *argv
[])
1082 return com_set(fd
, argc
, argv
);
1086 * snp: set numplayed
1088 int com_snp(int fd
, int argc
, char *argv
[])
1090 return com_set(fd
, argc
, argv
);
1094 * picch: change entry's name in pics table
1096 int com_picch(__a_unused
int fd
, int argc
, char *argv
[])
1103 return -E_MYSQL_SYNTAX
;
1106 tmp
= escape_str(argv
[2]);
1109 q
= make_message("update pics set name = '%s' where id = %lu", tmp
, id
);
1111 ret
= real_query(q
);
1117 * piclist: print list of pics in db
1119 int com_piclist(__a_unused
int fd
, int argc
, __a_unused
char *argv
[])
1121 void *result
= NULL
;
1123 unsigned long *length
;
1127 return -E_MYSQL_SYNTAX
;
1128 result
= get_result("select id,name,pic from pics order by id");
1131 while ((row
= mysql_fetch_row(result
))) {
1132 length
= mysql_fetch_lengths(result
);
1133 if (!row
|| !row
[0] || !row
[1] || !row
[2])
1135 ret
= send_va_buffer(fd
, "%s\t%lu\t%s\n", row
[0], length
[2], row
[1]);
1141 mysql_free_result(result
);
1146 * picdel: delete picture from database
1148 int com_picdel(int fd
, int argc
, char *argv
[])
1156 return -E_MYSQL_SYNTAX
;
1157 for (i
= 1; i
< argc
; i
++) {
1159 q
= make_message("delete from pics where id = %lu", id
);
1160 ret
= real_query(q
);
1164 aff
= mysql_affected_rows(mysql_ptr
);
1166 ret
= send_va_buffer(fd
, "No such id: %lu\n", id
);
1171 q
= make_message("update data set pic_id = 1 where pic_id = %lu", id
);
1172 ret
= real_query(q
);
1178 * pic: get picture by name or by number
1180 int com_pic(int fd
, int argc
, char *argv
[])
1182 void *result
= NULL
;
1184 unsigned long *length
, id
;
1186 char *q
, *name
= NULL
;
1189 ret
= -E_GET_AUDIO_FILE
;
1190 name
= get_current_audio_file();
1193 name
= escaped_basename(argv
[1]);
1198 id
= atoi(name
+ 1);
1200 id
= get_pic_id_by_name(name
);
1204 q
= make_message("select pic from pics where id = '%lu'", id
);
1205 result
= get_result(q
);
1209 row
= mysql_fetch_row(result
);
1211 if (!row
|| !row
[0])
1213 length
= mysql_fetch_lengths(result
);
1214 ret
= send_bin_buffer(fd
, row
[0], *length
);
1216 mysql_free_result(result
);
1221 int com_strdel(__a_unused
int fd
, int argc
, char *argv
[])
1227 return -E_MYSQL_SYNTAX
;
1228 tmp
= escape_str(argv
[1]);
1231 q
= make_message("delete from streams where name='%s'", tmp
);
1233 ret
= real_query(q
);
1241 int com_ls(int fd
, int argc
, char *argv
[])
1246 unsigned int num_rows
;
1249 return -E_MYSQL_SYNTAX
;
1251 char *tmp
= escape_str(argv
[1]);
1254 q
= make_message("select name from data where name like '%s'",
1258 q
= para_strdup("select name from data");
1259 result
= get_result(q
);
1263 num_rows
= mysql_num_rows(result
);
1266 ret
= print_results(fd
, result
, 0, 0, num_rows
- 1, 0);
1267 mysql_free_result(result
);
1274 int com_summary(__a_unused
int fd
, int argc
, __a_unused
char *argv
[])
1279 void *result2
= NULL
;
1280 const char *fmt
= "select count(name) from data where %s='1'";
1281 int ret
= -E_NORESULT
;
1284 return -E_MYSQL_SYNTAX
;
1285 result
= get_all_attributes();
1288 while ((row
= mysql_fetch_row(result
))) {
1295 buf
= make_message(fmt
, row
[0]);
1296 result2
= get_result(buf
);
1301 row2
= mysql_fetch_row(result2
);
1302 if (!row2
|| !row2
[0])
1304 ret
= send_va_buffer(fd
, "%s\t%s\n", row
[0], row2
[0]);
1311 mysql_free_result(result2
);
1313 mysql_free_result(result
);
1317 static int get_numplayed(char *name
)
1321 const char *fmt
= "select numplayed from data where name = '%s'";
1322 char *buf
= make_message(fmt
, name
);
1323 int ret
= -E_NORESULT
;
1325 result
= get_result(buf
);
1330 row
= mysql_fetch_row(result
);
1331 if (!row
|| !row
[0])
1336 mysql_free_result(result
);
1340 static int update_audio_file(char *name
)
1343 const char *fmt1
= "update data set lastplayed = now() where name = '%s'";
1344 const char *fmt2
= "update data set numplayed = %i where name = '%s'";
1346 char *ebn
= escaped_basename(name
);
1351 q
= make_message(fmt1
, ebn
);
1352 ret
= real_query(q
);
1356 ret
= get_numplayed(ebn
);
1359 q
= make_message(fmt2
, ret
+ 1, ebn
);
1360 ret
= real_query(q
);
1367 /* If called as child, mmd_lock must be held */
1368 static void update_mmd(char *info
)
1370 PARA_DEBUG_LOG("%s", "updating shared memory area\n");
1371 strncpy(mmd
->selector_info
, info
, MMD_INFO_SIZE
- 1);
1372 mmd
->selector_info
[MMD_INFO_SIZE
- 1] = '\0';
1375 static void update_audio_file_server_handler(char *name
)
1378 info
= get_selector_info(name
);
1381 update_audio_file(name
);
1384 int com_us(__a_unused
int fd
, int argc
, char *argv
[])
1390 return -E_MYSQL_SYNTAX
;
1391 tmp
= escape_str(argv
[1]);
1394 ret
= update_audio_file(argv
[1]);
1399 static void refresh_selector_info(void)
1401 char *name
= get_current_audio_file();
1406 info
= get_selector_info(name
);
1414 /* select previous / next stream */
1415 static int com_ps_ns(__a_unused
int fd
, int argc
, char *argv
[])
1417 char *query
, *stream
= get_current_stream();
1418 void *result
= get_result("select name from streams");
1420 int match
= -1, ret
, i
;
1421 unsigned int num_rows
;
1424 return -E_MYSQL_SYNTAX
;
1428 num_rows
= mysql_num_rows(result
);
1429 ret
= -E_EMPTY_RESULT
;
1433 for (i
= 0; i
< num_rows
; i
++) {
1434 row
= mysql_fetch_row(result
);
1435 if (!row
|| !row
[0])
1437 if (!strcmp(row
[0], "current_stream"))
1439 if (!strcmp(row
[0], stream
)) {
1447 if (!strcmp(argv
[0], "ps"))
1448 i
= match
> 0? match
- 1 : num_rows
- 1;
1450 i
= match
< num_rows
- 1? match
+ 1 : 0;
1452 mysql_data_seek(result
, i
);
1453 row
= mysql_fetch_row(result
);
1454 if (!row
|| !row
[0])
1456 if (!strcmp(row
[0], "current_stream")) {
1457 if (!strcmp(argv
[0], "ps")) {
1459 i
= i
< 0? i
+ num_rows
: i
;
1462 i
= i
> num_rows
- 1? i
- num_rows
: i
;
1464 mysql_data_seek(result
, i
);
1465 row
= mysql_fetch_row(result
);
1466 if (!row
|| !row
[0])
1469 query
= make_message("update streams set def='%s' where name = "
1470 "'current_stream'", row
[0]);
1471 ret
= real_query(query
);
1473 refresh_selector_info();
1477 mysql_free_result(result
);
1481 /* select previous stream */
1482 int com_ps(int fd
, int argc
, char *argv
[])
1484 return com_ps_ns(fd
, argc
, argv
);
1487 /* select next stream */
1488 int com_ns(int fd
, int argc
, char *argv
[])
1490 return com_ps_ns(fd
, argc
, argv
);
1494 int com_streams(int fd
, int argc
, __a_unused
char *argv
[])
1496 unsigned int num_rows
;
1497 int i
, ret
= -E_NORESULT
;
1501 if (argc
> 1 && strcmp(argv
[1], "current_stream"))
1502 return -E_MYSQL_SYNTAX
;
1504 char *cs
= get_current_stream();
1505 ret
= send_va_buffer(fd
, "%s\n", cs
);
1509 result
= get_result("select name from streams");
1512 num_rows
= mysql_num_rows(result
);
1517 for (i
= 0; i
< num_rows
; i
++) {
1518 row
= mysql_fetch_row(result
);
1519 if (!row
|| !row
[0])
1521 if (strcmp(row
[0], "current_stream"))
1522 send_va_buffer(fd
, "%s\n", row
[0]);
1527 mysql_free_result(result
);
1531 /* query stream definition */
1532 int com_strq(int fd
, int argc
, char *argv
[])
1540 ret
= -E_GET_STREAM
;
1541 name
= get_current_stream();
1544 name
= escaped_basename(argv
[1]);
1549 query
= make_message("select def from streams where name='%s'", name
);
1551 result
= get_result(query
);
1556 row
= mysql_fetch_row(result
);
1557 if (!row
|| !row
[0])
1559 /* no '\n' needed */
1560 ret
= send_buffer(fd
, row
[0]);
1563 mysql_free_result(result
);
1567 /* change stream / change stream and play */
1568 static int com_cs_csp(int fd
, int argc
, char *argv
[])
1570 int ret
, stream_change
;
1571 char *query
, *stream
= NULL
;
1572 char *old_stream
= get_current_stream();
1573 int csp
= !strcmp(argv
[0], "csp");
1575 ret
= -E_MYSQL_SYNTAX
;
1581 ret
= send_va_buffer(fd
, "%s\n", old_stream
);
1585 /* test if stream is valid, no need to escape argv[1] */
1586 query
= get_query(argv
[1], NULL
, 0);
1591 stream
= escape_str(argv
[1]);
1594 stream_change
= strcmp(stream
, old_stream
);
1595 if (stream_change
) {
1596 ret
= change_stream(stream
);
1599 refresh_selector_info();
1603 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
1605 mmd
->new_vss_status_flags
|= VSS_NEXT
;
1616 int com_cs(int fd
, int argc
, char *argv
[])
1618 return com_cs_csp(fd
, argc
, argv
);
1621 /* change stream and play */
1622 int com_csp(int fd
, int argc
, char *argv
[])
1624 return com_cs_csp(fd
, argc
, argv
);
1627 /* score list / skip */
1628 static int com_sl_skip(int fd
, int argc
, char *argv
[])
1630 void *result
= NULL
;
1632 int ret
, i
, skip
= !strcmp(argv
[0], "skip");
1633 char *query
, *stream
, *tmp
;
1634 unsigned int num_rows
, num
;
1637 return -E_MYSQL_SYNTAX
;
1638 num
= atoi(argv
[1]);
1640 return -E_MYSQL_SYNTAX
;
1642 stream
= get_current_stream();
1644 return -E_GET_STREAM
;
1646 stream
= escape_str(argv
[2]);
1650 tmp
= get_query(stream
, NULL
, 0);
1653 return -E_GET_QUERY
;
1654 query
= make_message("%s limit %d", tmp
, num
);
1657 result
= get_result(query
);
1661 ret
= -E_EMPTY_RESULT
;
1662 num_rows
= mysql_num_rows(result
);
1665 for (i
= 0; i
< num_rows
&& i
< num
; i
++) {
1666 row
= mysql_fetch_row(result
);
1668 send_va_buffer(fd
, "Skipping %s\n", row
[0]);
1669 update_audio_file(row
[0]);
1671 send_va_buffer(fd
, "%s\n", row
[0]? row
[0]: "BUG");
1676 mysql_free_result(result
);
1681 int com_sl(int fd
, int argc
, char *argv
[])
1683 return com_sl_skip(fd
, argc
, argv
);
1687 int com_skip(int fd
, int argc
, char *argv
[])
1689 return com_sl_skip(fd
, argc
, argv
);
1693 * update attributes of name
1695 static int update_atts(int fd
, char *name
, char *atts
)
1698 char *ebn
, *q
, *old
, *new = NULL
;
1702 ebn
= escaped_basename(name
);
1705 q
= make_message("update data set %s where name = '%s'", atts
, ebn
);
1706 old
= get_atts(ebn
, 0);
1707 send_va_buffer(fd
, "old: %s\n", old
);
1709 ret
= real_query(q
);
1713 new = get_atts(ebn
, 0);
1714 ret
= send_va_buffer(fd
, "new: %s\n", new);
1724 int com_sa(int fd
, int argc
, char *argv
[])
1727 char *atts
= NULL
, *name
;
1730 return -E_MYSQL_SYNTAX
;
1731 for (i
= 1; i
< argc
; i
++) {
1733 char *esc
, *tmp
, *p
=argv
[i
];
1734 int len
= strlen(p
);
1738 switch (p
[len
- 1]) {
1749 esc
= escape_str(p
);
1752 tmp
= make_message("%s%s='%s'", atts
? "," : "", esc
,
1755 atts
= para_strcat(atts
, tmp
);
1761 if (i
>= argc
) { /* no name given, use current af */
1762 ret
= -E_GET_AUDIO_FILE
;
1763 if (!(name
= get_current_audio_file()))
1765 ret
= update_atts(fd
, name
, atts
);
1769 for (; argv
[i
] && ret
>= 0; i
++)
1770 ret
= update_atts(fd
, argv
[i
], atts
);
1772 refresh_selector_info();
1781 int com_cam(int fd
, int argc
, char *argv
[])
1783 char *name
= NULL
, *meta
= NULL
, *atts
= NULL
;
1787 return -E_MYSQL_SYNTAX
;
1788 if (!(name
= escaped_basename(argv
[1])))
1791 if (!(atts
= get_atts(name
, 1)))
1794 if (!(meta
= get_meta(name
, 0)))
1796 for (i
= 2; i
< argc
; i
++) {
1799 if (!(ebn
= escaped_basename(argv
[i
])))
1801 ret
= send_va_buffer(fd
, "updating %s\n", ebn
);
1806 q
= make_message("update data set %s where name = '%s'",
1808 if ((ret
= update_atts(fd
, ebn
, atts
)) >= 0)
1809 ret
= real_query(q
);
1829 static int com_vrfy_clean(int fd
, int argc
, __a_unused
char *argv
[])
1832 int ret
, vrfy_mode
= strcmp(argv
[0], "clean");
1833 void *result
= NULL
;
1834 unsigned int num_rows
;
1839 return -E_MYSQL_SYNTAX
;
1841 result
= get_result("select data.name from data left join dir on "
1842 "dir.name = data.name where dir.name is NULL");
1845 num_rows
= mysql_num_rows(result
);
1847 ret
= send_buffer(fd
, "No invalid entries\n");
1851 send_va_buffer(fd
, "found %i invalid entr%s\n", num_rows
,
1852 num_rows
== 1? "y" : "ies");
1853 ret
= print_results(fd
, result
, 0, 0, num_rows
- 1, 0);
1856 while ((row
= mysql_fetch_row(result
))) {
1861 escaped_name
= escape_str(row
[0]);
1864 send_va_buffer(fd
, "deleting %s\n", escaped_name
);
1865 query
= make_message("delete from data where name = '%s'",
1867 ret
= real_query(query
);
1875 mysql_free_result(result
);
1882 int com_vrfy(int fd
, int argc
, char **argv
)
1884 return com_vrfy_clean(fd
, argc
, argv
);
1890 int com_clean(int fd
, int argc
, char **argv
)
1892 return com_vrfy_clean(fd
, argc
, argv
);
1895 static FILE *out_file
;
1897 static int mysql_write_tmp_file(const char *dir
, const char *name
)
1899 int ret
= -E_TMPFILE
;
1900 char *msg
= make_message("%s\t%s\n", dir
, name
);
1901 if (fputs(msg
, out_file
) != EOF
)
1910 int com_upd(int fd
, int argc
, __a_unused
char *argv
[])
1912 char *tempname
= NULL
, *query
= NULL
;
1913 int ret
, out_fd
= -1, num
= 0;
1914 void *result
= NULL
;
1915 unsigned int num_rows
;
1919 return -E_MYSQL_SYNTAX
;
1921 tempname
= para_strdup("/tmp/mysql.tmp.XXXXXX");
1922 ret
= para_mkstemp(tempname
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
1926 out_file
= fdopen(out_fd
, "w");
1931 if (find_audio_files(conf
.mysql_audio_file_dir_arg
, mysql_write_tmp_file
) < 0)
1933 num
= ftell(out_file
);
1935 * we have to make sure the file hit the disk before we call
1940 PARA_DEBUG_LOG("wrote tempfile %s (%d bytes)\n", tempname
, num
);
1943 if ((ret
= real_query("delete from dir")) < 0)
1945 query
= make_message("load data infile '%s' ignore into table dir "
1946 "fields terminated by '\t' lines terminated by '\n' "
1947 "(dir, name)", tempname
);
1948 ret
= real_query(query
);
1952 result
= get_result("select dir.name from dir left join data on "
1953 "data.name = dir.name where data.name is NULL");
1957 num_rows
= mysql_num_rows(result
);
1959 ret
= send_buffer(fd
, "no new entries\n");
1962 while ((row
= mysql_fetch_row(result
))) {
1967 send_va_buffer(fd
, "new entry: %s\n", row
[0]);
1968 erow
= escape_str(row
[0]);
1971 query
= make_message("insert into data (name, pic_id) values "
1972 "('%s','%s')", erow
, "1");
1974 ret
= real_query(query
);
1987 mysql_free_result(result
);
1991 static char **server_get_audio_file_list(unsigned int num
)
1993 char **list
= para_malloc((num
+ 1) * sizeof(char *));
1994 char *tmp
, *query
, *stream
= get_current_stream();
1995 void *result
= NULL
;
1996 unsigned int num_rows
;
2000 tmp
= get_query(stream
, NULL
, 1);
2004 query
= make_message("%s limit %d", tmp
, num
);
2006 result
= get_result(query
);
2010 num_rows
= mysql_num_rows(result
);
2013 for (i
= 0; i
< num_rows
&& i
< num
; i
++) {
2014 row
= mysql_fetch_row(result
);
2015 if (!row
|| !row
[0])
2017 list
[i
] = para_strdup(row
[0]);
2030 mysql_free_result(result
);
2035 * connect to mysql server, return mysql pointer on success, -E_NOTCONN
2036 * on errors. Called from parent on startup and also from com_cdb().
2038 static int init_mysql_server(void)
2040 char *u
= conf
.mysql_user_arg
? conf
.mysql_user_arg
: para_logname();
2042 mysql_ptr
= mysql_init(NULL
);
2044 PARA_CRIT_LOG("%s", "mysql init error\n");
2047 PARA_DEBUG_LOG("connecting: %s@%s:%d\n", u
, conf
.mysql_host_arg
,
2048 conf
.mysql_port_arg
);
2049 if (!conf
.mysql_user_arg
)
2052 * If host is NULL a connection to the local host is assumed,
2053 * If user is NULL, the current user is assumed
2055 if (!(mysql_ptr
= mysql_real_connect(mysql_ptr
,
2056 conf
.mysql_host_arg
,
2057 conf
.mysql_user_arg
,
2058 conf
.mysql_passwd_arg
,
2059 conf
.mysql_database_arg
,
2060 conf
.mysql_port_arg
, NULL
, 0))) {
2061 PARA_CRIT_LOG("%s", "connect error\n");
2064 PARA_INFO_LOG("%s", "success\n");
2068 /* mmd lock must be held */
2069 static void write_msg2mmd(int success
)
2071 sprintf(mmd
->selector_info
, "dbinfo1:%s\ndbinfo2:mysql-%s\ndbinfo3:\n",
2072 success
< 0? PARA_STRERROR(-success
) :
2073 "successfully connected to mysql server",
2074 success
< 0? "" : mysql_get_server_info(mysql_ptr
));
2077 /* create database */
2078 int com_cdb(int fd
, int argc
, char *argv
[])
2084 PARA_INFO_LOG("%s", "closing database\n");
2085 mysql_close(mysql_ptr
);
2087 /* dont use any database */
2088 conf
.mysql_database_arg
= NULL
; /* leak? */
2089 ret
= -E_MYSQL_INIT
;
2090 if (init_mysql_server() < 0 || !mysql_ptr
)
2093 conf
.mysql_database_arg
= para_strdup("paraslash");
2096 conf
.mysql_database_arg
= escape_str(argv
[1]);
2097 if (!conf
.mysql_database_arg
)
2100 query
= make_message("create database %s", conf
.mysql_database_arg
);
2101 ret
= real_query(query
);
2105 /* reconnect with database just created */
2106 mysql_close(mysql_ptr
);
2107 ret
= -E_MYSQL_INIT
;
2108 if (init_mysql_server() < 0 || !mysql_ptr
)
2114 if (real_query("create table data (name varchar(255) binary not null "
2116 "lastplayed datetime not null default "
2118 "numplayed int not null default 0, "
2119 "pic_id bigint unsigned not null default 1)") < 0)
2121 if (real_query("create table dir (name varchar(255) binary not null "
2122 "primary key, dir varchar(255) default null)") < 0)
2124 if (real_query("create table pics ("
2125 "id bigint(20) unsigned not null primary key "
2127 "name varchar(255) binary not null, "
2128 "pic mediumblob not null)") < 0)
2130 if (real_query("create table streams ("
2131 "name varchar(255) binary not null primary key, "
2132 "def blob not null)") < 0)
2134 if (real_query("insert into streams (name, def) values "
2135 "('current_stream', '(none)')") < 0)
2137 ret
= send_va_buffer(fd
, "successfully created database %s\n",
2138 conf
.mysql_database_arg
);
2143 static void shutdown_connection(void)
2146 PARA_NOTICE_LOG("%s", "shutting down mysql connection\n");
2147 mysql_close(mysql_ptr
);
2153 * the init function of the mysql-based audio file selector
2155 * \param db pointer to the struct to initialize
2157 * Check the command line options and initialize all function pointers of \a
2158 * db. Connect to the mysql server and initialize the info string.
2160 * \return This function returns success even if it could not connect
2161 * to the mysql server. This is because the connect is expected to fail
2162 * if there the paraslash database is not yet created. This gives the
2163 * user a chance to send the "cdb" to create the database.
2165 * \sa struct audio_file_selector, misc_meta_data::selector_info,
2168 int mysql_selector_init(struct audio_file_selector
*db
)
2172 if (!conf
.mysql_passwd_given
)
2173 return -E_NO_MYSQL_PASSWD
;
2174 if (!conf
.mysql_audio_file_dir_given
)
2175 return -E_NO_AF_DIR
;
2177 db
->cmd_list
= mysql_selector_cmds
;
2178 db
->get_audio_file_list
= server_get_audio_file_list
;
2179 db
->update_audio_file
= update_audio_file_server_handler
;
2180 db
->shutdown
= shutdown_connection
;
2181 ret
= init_mysql_server();
2183 PARA_WARNING_LOG("%s\n", PARA_STRERROR(-ret
));
2185 return 1; /* return success even if connect failed to give the
2186 * user the chance to exec com_cdb