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>
35 #include "user_list.h"
36 #include "mysql_selector_command_list.h"
38 /** pointer to the shared memory area */
39 extern struct misc_meta_data
*mmd
;
41 static void *mysql_ptr
= NULL
;
44 * contains name/replacement pairs used by s_a_r_list()
49 /** the name of the macro */
51 /** the replacement text */
52 const char *replacement
;
55 static const struct para_macro mysql_macro_list
[] = {
57 .replacement
= "(data.%s != '1')"
60 .replacement
= "(data.%s = '1')"
63 .replacement
= "%sdata.Pic_Id"
66 .replacement
= "(data.name like '%s')"
69 .replacement
= "%sFLOOR((UNIX_TIMESTAMP(now())"
70 "-UNIX_TIMESTAMP(data.Lastplayed))/60)"
73 .replacement
= "%sdata.Numplayed"
80 * simple search and replace routine
82 * \param src source string
83 * \param macro_name the name of the macro
84 * \param replacement the replacement format string
86 * In \p src, replace each occurence of \p macro_name(arg) by the string
87 * determined by the \p replacement format string. \p replacement may (but
88 * needs not) contain a single string conversion specifier (%s) which gets
91 * \return A string in which all matches in \p src are replaced, or \p NULL if
92 * an syntax error was encountered. Caller must free the result.
96 __must_check __malloc
static char *s_a_r(const char *src
, const char* macro_name
,
97 const char *replacement
)
101 regmatch_t pmatch
[1];
104 const char *bufptr
= src
;
106 if (!macro_name
|| !replacement
|| !src
)
107 return para_strdup(src
);
108 regcomp(&preg
, macro_name
, 0);
109 while (regexec(&preg
, bufptr
, nmatch
, pmatch
, eflags
)
111 char *tmp
, *arg
, *o_bracket
, *c_bracket
;
113 o_bracket
= strchr(bufptr
+ pmatch
[0].rm_so
, '(');
114 c_bracket
= o_bracket
? strchr(o_bracket
, ')') : NULL
;
117 tmp
= para_strdup(bufptr
);
118 tmp
[pmatch
[0].rm_so
] = '\0';
119 dest
= para_strcat(dest
, tmp
);
122 arg
= para_strdup(o_bracket
+ 1);
123 arg
[c_bracket
- o_bracket
- 1] = '\0';
124 tmp
= make_message(replacement
, arg
);
126 dest
= para_strcat(dest
, tmp
);
131 dest
= para_strcat(dest
, bufptr
);
132 // PARA_DEBUG_LOG("%s: returning %s\n", __func__, dest);
139 * replace a string according to a list of macros
141 * \param macro_list the array containing a macro/replacement pairs.
142 * \param src the source string
144 * This function just calls s_a_r() for each element of \p macro_list.
146 * \return \p NULL if one of the underlying calls to \p s_a_r returned \p NULL.
147 * Otherwise the completely expanded version of \p src is returned.
149 __must_check __malloc
static char *s_a_r_list(const struct para_macro
*macro_list
,
152 const struct para_macro
*mp
= macro_list
;
153 char *ret
= NULL
, *tmp
= para_strdup(src
);
156 ret
= s_a_r(tmp
, mp
->name
, mp
->replacement
);
158 if (!ret
) /* syntax error */
163 //PARA_DEBUG_LOG("%s: returning %s\n", __func__, dest);
167 static int real_query(const char *query
)
171 PARA_DEBUG_LOG("%s\n", query
);
172 if (mysql_real_query(mysql_ptr
, query
, strlen(query
))) {
173 PARA_ERROR_LOG("real_query error (%s)\n",
174 mysql_error(mysql_ptr
));
181 * Use open connection given by mysql_ptr to query server. Returns a
182 * result pointer on succes and NULL on errors
184 static struct MYSQL_RES
*get_result(const char *query
)
188 if (real_query(query
) < 0)
190 result
= mysql_store_result(mysql_ptr
);
192 PARA_ERROR_LOG("%s", "store_result error\n");
196 * write input from fd to dynamically allocated char array,
197 * but maximal max_size byte. Return size.
199 static int fd2buf(int fd
, char **buf_ptr
, size_t max_size
)
201 const size_t chunk_size
= 1024;
203 char *buf
= para_malloc(size
* sizeof(char)), *p
= buf
;
206 while ((ret
= recv_bin_buffer(fd
, p
, chunk_size
)) > 0) {
208 if ((p
- buf
) + chunk_size
>= size
) {
212 if (size
> max_size
) {
216 tmp
= para_realloc(buf
, size
);
231 static char *escape_blob(const char* old
, size_t size
)
237 new = para_malloc(2 * size
* sizeof(char) + 1);
238 mysql_real_escape_string(mysql_ptr
, new, old
, size
);
242 static char *escape_str(const char* old
)
244 return escape_blob(old
, strlen(old
));
247 static char *escaped_basename(const char *name
)
249 char *esc
, *bn
= para_basename(name
);
253 esc
= escape_str(bn
);
261 int com_na(__a_unused
int fd
, int argc
, char *argv
[])
267 return -E_MYSQL_SYNTAX
;
268 tmp
= escape_str(argv
[1]);
271 q
= make_message("alter table data add %s char(1) "
272 "not null default 0", tmp
);
282 int com_da(__a_unused
int fd
, int argc
, char *argv
[])
288 return -E_MYSQL_SYNTAX
;
289 tmp
= escape_str(argv
[1]);
292 q
= make_message("alter table data drop %s", tmp
);
300 static int com_stradd_picadd(int fd
, int argc
, char *argv
[])
302 char *blob
= NULL
, *esc_blob
= NULL
, *q
= NULL
, *tmp
= NULL
;
303 const char *fmt
, *del_fmt
;
304 int ret
, stradd
= strcmp(argv
[0], "picadd");
308 return -E_MYSQL_SYNTAX
;
309 if (strlen(argv
[1]) >= MAXLINE
- 1)
310 return -E_NAMETOOLONG
;
315 fmt
= "insert into streams (name, def) values ('%s','%s')";
316 del_fmt
="delete from streams where name='%s'";
318 size
= MEDIUM_BLOB_SIZE
;
319 fmt
= "insert into pics (name, pic) values ('%s','%s')";
320 del_fmt
="delete from pics where pic='%s'";
322 tmp
= escape_str(argv
[1]);
325 q
= make_message(del_fmt
, tmp
);
331 if ((ret
= send_buffer(fd
, AWAITING_DATA_MSG
) < 0))
333 if ((ret
= fd2buf(fd
, &blob
, size
)) < 0)
339 esc_blob
= escape_blob(blob
, size
);
342 tmp
= escape_str(argv
[1]);
345 q
= make_message(fmt
, tmp
, esc_blob
);
356 int com_stradd(int fd
, int argc
, char *argv
[])
358 return com_stradd_picadd(fd
, argc
, argv
);
362 int com_picadd(int fd
, int argc
, char *argv
[])
364 return com_stradd_picadd(fd
, argc
, argv
);
368 * print results to fd
370 static int print_results(int fd
, void *result
,
371 my_ulonglong top
, my_ulonglong left
,
372 my_ulonglong bottom
, my_ulonglong right
)
378 for (i
= top
; i
<= bottom
; i
++) {
379 row
= mysql_fetch_row(result
);
382 for (j
= left
; j
<= right
; j
++) {
383 ret
= send_va_buffer(fd
, j
== left
? "%s" : "\t%s",
384 row
[j
]? row
[j
] : "NULL");
388 ret
= send_buffer(fd
, "\n");
398 int com_verb(int fd
, int argc
, char *argv
[])
402 my_ulonglong num_rows
, num_fields
, top
= 0, left
= 0;
406 return -E_MYSQL_SYNTAX
;
407 tmp
= escape_str(argv
[1]);
410 result
= get_result(tmp
);
413 /* return success, because it's ok to have no results */
415 num_fields
= mysql_field_count(mysql_ptr
);
416 num_rows
= mysql_num_rows(result
);
418 if (num_fields
&& num_rows
)
419 ret
= print_results(fd
, result
, top
, left
, num_rows
- 1,
421 mysql_free_result(result
);
425 /* returns NULL on errors or if there are no atts defined yet */
426 static void *get_all_attributes(void)
428 void *result
= get_result("desc data");
429 unsigned int num_rows
;
433 num_rows
= mysql_num_rows(result
);
435 mysql_free_result(result
);
438 mysql_data_seek(result
, (my_ulonglong
)4); /* skip Lastplayed, Numplayed... */
443 * list all attributes
445 int com_laa(int fd
, int argc
, __a_unused
char *argv
[])
449 my_ulonglong top
= 0, left
= 0, bottom
, right
= 0;
452 return -E_MYSQL_SYNTAX
;
453 result
= get_all_attributes();
456 bottom
= mysql_num_rows(result
);
458 return -E_MYSQL_SYNTAX
;
460 ret
= print_results(fd
, result
, top
, left
, bottom
, right
);
461 mysql_free_result(result
);
468 int com_hist(int fd
, int argc
, char *argv
[])
473 my_ulonglong num_rows
, top
= 0, left
= 0, right
= 1;
476 return -E_MYSQL_SYNTAX
;
478 char *tmp
= escape_str(argv
[1]);
481 atts
= make_message("where %s = '1'", tmp
);
484 atts
= para_strdup(NULL
);
486 q
= make_message("select name, to_days(now()) - to_days(lastplayed) from "
487 "data %s order by lastplayed", atts
);
489 result
= get_result(q
);
493 num_rows
= mysql_num_rows(result
);
496 ret
= print_results(fd
, result
, top
, left
, num_rows
- 1, right
);
497 mysql_free_result(result
);
502 * get last num audio files
504 int com_last(int fd
, int argc
, char *argv
[])
509 my_ulonglong top
= 0, left
= 0, right
= 0;
516 return -E_MYSQL_SYNTAX
;
517 q
= make_message("select name from data order by lastplayed desc "
519 result
= get_result(q
);
523 ret
= print_results(fd
, result
, top
, left
, mysql_num_rows(result
) - 1,
525 mysql_free_result(result
);
529 int com_mbox(int fd
, int argc
, char *argv
[])
534 my_ulonglong num_rows
, num_fields
, top
= 0, left
= 0;
535 char *query
= para_strdup("select concat('From foo@localhost ', "
536 "date_format(Lastplayed, '%a %b %e %T %Y'), "
537 "'\nReceived: from\nTo: bar\n");
540 result
= get_all_attributes();
544 while ((row
= mysql_fetch_row(result
))) {
549 tmp
= make_message("%sX-Attribute-%s: ', %s, '\n", query
,
554 query
= para_strcat(query
,
562 char *esc
= escape_str(argv
[1]), *tmp
;
566 tmp
= make_message("%s where name LIKE '%s'", query
, esc
);
571 mysql_free_result(result
);
573 result
= get_result(query
);
576 ret
= -E_EMPTY_RESULT
;
577 num_fields
= mysql_field_count(mysql_ptr
);
578 num_rows
= mysql_num_rows(result
);
579 if (!num_fields
|| !num_rows
)
581 ret
= print_results(fd
, result
, top
, left
, num_rows
- 1,
586 mysql_free_result(result
);
591 * get attributes by name. If verbose is not 0, this function returns a string
592 * of the form 'att1="0",att2="1"'... which is used in com_cam() for
593 * constructing a mysql update query. Otherwise the space-separated list of all
594 * attributes which are set in the audio file given by name is returned. Never
595 * returns NULL in *NON VERBOSE* mode.
597 static char *get_atts(char *name
, int verbose
)
599 char *atts
= NULL
, *buf
, *ebn
;
600 void *result
= NULL
, *result2
= NULL
;
603 my_ulonglong num_fields
, offset
= 4; /* skip Lastplayed, Numplayed... */
606 result2
= get_all_attributes();
609 ebn
= escaped_basename(name
);
612 buf
= make_message("select * from data where name='%s'", ebn
);
614 result
= get_result(buf
);
618 num_fields
= mysql_num_fields(result
);
621 mysql_data_seek(result2
, offset
);
622 row
= mysql_fetch_row(result
);
625 for (i
= 4; i
< num_fields
; i
++) {
626 int is_set
= row
[i
] && !strcmp(row
[i
], "1");
627 row2
= mysql_fetch_row(result2
);
628 if (!row2
|| !row2
[0])
630 if (atts
&& (verbose
|| is_set
))
631 atts
= para_strcat(atts
, verbose
? "," : " ");
632 if (is_set
|| verbose
)
633 atts
= para_strcat(atts
, row2
[0]);
635 atts
= para_strcat(atts
, is_set
? "=\"1\"" : "=\"0\"");
639 mysql_free_result(result2
);
641 mysql_free_result(result
);
642 if (!atts
&& !verbose
)
643 atts
= para_strdup("(none)");
647 /* never returns NULL in verbose mode */
648 static char *get_meta(char *name
, int verbose
)
652 char *ebn
, *q
, *ret
= NULL
;
653 const char *verbose_fmt
=
654 "select concat('lastplayed: ', "
655 "(to_days(now()) - to_days(lastplayed)),"
656 "' day(s). numplayed: ', numplayed, "
657 "', pic: ', pic_id) "
658 "from data where name = '%s'";
659 /* is that really needed? */
660 const char *fmt
= "select concat('lastplayed=\\'', lastplayed, "
661 "'\\', numplayed=\\'', numplayed, "
662 "'\\', pic_id=\\'', pic_id, '\\'') "
663 "from data where name = '%s'";
665 if (!(ebn
= escaped_basename(name
)))
667 q
= make_message(verbose
? verbose_fmt
: fmt
, ebn
);
669 result
= get_result(q
);
673 row
= mysql_fetch_row(result
);
676 ret
= para_strdup(row
[0]);
679 mysql_free_result(result
);
681 ret
= para_strdup("(not yet played)");
685 static char *get_dir(char *name
)
687 char *ret
= NULL
, *q
, *ebn
;
691 if (!(ebn
= escaped_basename(name
)))
693 q
= make_message("select dir from dir where name = '%s'", ebn
);
695 result
= get_result(q
);
699 row
= mysql_fetch_row(result
);
701 ret
= para_strdup(row
[0]);
702 mysql_free_result(result
);
706 /* never returns NULL */
707 static char *get_current_stream(void)
711 void *result
= get_result("select def from streams where "
712 "name = 'current_stream'");
716 row
= mysql_fetch_row(result
);
719 ret
= para_strdup(row
[0]);
720 mysql_free_result(result
);
724 mysql_free_result(result
);
725 return para_strdup("(none)");
729 * Read stream definition of stream streamname and construct mysql
730 * query. Return NULL on errors. If streamname is NULL, use current
731 * stream. If that is also NULL, use query that selects everything.
732 * If filename is NULL, query will list everything, otherwise only
733 * the score of given file.
735 static char *get_query(char *streamname
, char *filename
, int with_path
)
737 char *accept_opts
= NULL
, *deny_opts
= NULL
, *score
= NULL
;
738 char *where_clause
, *order
, *query
;
739 char command
[255] = ""; /* buffer for sscanf */
743 char *select_clause
= NULL
;
745 tmp
= get_current_stream();
747 tmp
= escape_str(streamname
);
751 if (!strcmp(tmp
, "(none)")) {
754 char *ret
, *ebn
= escaped_basename(filename
);
757 ret
= make_message("select to_days(now()) - "
758 "to_days(lastplayed) from data "
759 "where name = '%s'", ebn
);
765 "select concat(dir.dir, '/', dir.name) "
766 "from data, dir where dir.name = data.name "
767 "order by data.lastplayed"
770 "select name from data where name is not NULL "
771 "order by lastplayed"
775 query
= make_message("select def from streams where name = '%s'",
777 result
= get_result(query
);
782 row
= mysql_fetch_row(result
);
788 char *arg
, *line
= end
;
790 if (!(end
= strchr(line
, '\n')))
794 if (sscanf(line
, "%200s%n", command
, &n
) < 1)
797 if (!strcmp(command
, "accept:")) {
798 char *tmp2
= s_a_r_list(mysql_macro_list
, arg
);
800 accept_opts
= para_strcat(
801 accept_opts
, " or ");
802 accept_opts
= para_strcat(accept_opts
, tmp2
);
806 if (!strcmp(command
, "deny:")) {
807 char *tmp2
= s_a_r_list(mysql_macro_list
, arg
);
809 deny_opts
= para_strcat(deny_opts
, " or ");
810 deny_opts
= para_strcat(deny_opts
, tmp2
);
814 if (!score
&& !strcmp(command
, "score:"))
815 score
= s_a_r_list(mysql_macro_list
, arg
);
818 score
= s_a_r_list(mysql_macro_list
, conf
.mysql_default_score_arg
);
823 char *ebn
= escaped_basename(filename
);
826 select_clause
= make_message("select %s from data ", score
);
828 where_clause
= make_message( "where name = '%s' ", ebn
);
830 order
= para_strdup("");
833 select_clause
= para_strdup(with_path
?
834 "select concat(dir.dir, '/', dir.name) from data, dir "
835 "where dir.name = data.name "
837 "select name from data where name is not NULL");
838 order
= make_message("order by -(%s)", score
);
840 if (accept_opts
&& deny_opts
) {
841 where_clause
= make_message("and ((%s) and not (%s)) ",
842 accept_opts
, deny_opts
);
845 if (accept_opts
&& !deny_opts
) {
846 where_clause
= make_message("and (%s) ", accept_opts
);
849 if (!accept_opts
&& deny_opts
) {
850 where_clause
= make_message("and not (%s) ", deny_opts
);
853 where_clause
= para_strdup("");
855 query
= make_message("%s %s %s", select_clause
, where_clause
, order
);
865 mysql_free_result(result
);
872 * This is called from server and from some commands. Name must not be NULL
873 * Never returns NULL.
875 static char *get_selector_info(char *name
)
877 char *meta
, *atts
, *info
, *dir
, *query
, *stream
;
879 MYSQL_ROW row
= NULL
;
882 return para_strdup("(none)");
883 stream
= get_current_stream();
884 meta
= get_meta(name
, 1);
885 atts
= get_atts(name
, 0);
888 query
= get_query(stream
, name
, 0); /* FIXME: pass stream == NULL instead? */
891 result
= get_result(query
);
894 row
= mysql_fetch_row(result
);
896 info
= make_message("dbinfo1:dir: %s\n"
897 "dbinfo2:stream: %s, %s, score: %s\n"
899 dir
? dir
: "(not contained in table)",
901 (result
&& row
&& row
[0])? row
[0] : "(no score)",
908 mysql_free_result(result
);
912 /* might return NULL */
913 static char *get_current_audio_file(void)
917 name
= para_basename(mmd
->filename
);
922 /* If called as child, mmd_lock must be held */
923 static void update_mmd(char *info
)
925 PARA_DEBUG_LOG("%s", "updating shared memory area\n");
926 strncpy(mmd
->selector_info
, info
, MMD_INFO_SIZE
- 1);
927 mmd
->selector_info
[MMD_INFO_SIZE
- 1] = '\0';
930 static void refresh_selector_info(void)
932 char *name
= get_current_audio_file();
937 info
= get_selector_info(name
);
945 /* list attributes / print database info */
946 static int com_la_info(int fd
, int argc
, char *argv
[])
948 char *name
= NULL
, *meta
= NULL
, *atts
= NULL
, *dir
= NULL
;
949 int ret
, la
= strcmp(argv
[0], "info");
952 ret
= -E_GET_AUDIO_FILE
;
953 if (!(name
= get_current_audio_file()))
955 ret
= send_va_buffer(fd
, "%s\n", name
);
960 if (!(name
= escaped_basename(argv
[1])))
963 meta
= get_meta(name
, 1);
964 atts
= get_atts(name
, 0);
967 ret
= send_va_buffer(fd
, "%s\n", atts
);
969 ret
= send_va_buffer(fd
, "dir: %s\n" "%s\n" "attributes: %s\n",
970 dir
? dir
: "(not contained in table)", meta
, atts
);
979 /* list attributes */
980 int com_la(int fd
, int argc
, char *argv
[])
982 return com_la_info(fd
, argc
, argv
);
985 /* print database info */
986 int com_info(int fd
, int argc
, char *argv
[])
988 return com_la_info(fd
, argc
, argv
);
991 static int change_stream(const char *stream
)
995 query
= make_message("update streams set def='%s' "
996 "where name = 'current_stream'", stream
);
997 ret
= real_query(query
);
1002 static int get_pic_id_by_name(char *name
)
1005 void *result
= NULL
;
1009 if (!(ebn
= escaped_basename(name
)))
1011 q
= make_message("select pic_id from data where name = '%s'", ebn
);
1013 result
= get_result(q
);
1017 row
= mysql_fetch_row(result
);
1021 mysql_free_result(result
);
1025 static int remove_entry(const char *name
)
1027 char *q
, *ebn
= escaped_basename(name
);
1028 int ret
= -E_ESCAPE
;
1032 q
= make_message("delete from data where name = '%s'", ebn
);
1033 real_query(q
); /* ignore errors */
1035 q
= make_message("delete from dir where name = '%s'", ebn
);
1036 real_query(q
); /* ignore errors */
1044 static int add_entry(const char *name
)
1046 char *q
, *dn
, *ebn
= NULL
, *edn
= NULL
;
1049 if (!name
|| !*name
)
1050 return -E_MYSQL_SYNTAX
;
1051 ebn
= escaped_basename(name
);
1054 ret
= -E_MYSQL_SYNTAX
;
1055 dn
= para_dirname(name
);
1059 edn
= escape_str(dn
);
1063 q
= make_message("insert into data (name, pic_id) values "
1064 "('%s', '%s')", ebn
, "1");
1065 ret
= real_query(q
);
1066 // ret = 1; PARA_DEBUG_LOG("q: %s\n", q);
1070 q
= make_message("insert into dir (name, dir) values "
1071 "('%s', '%s')", ebn
, edn
);
1072 // ret = 1; PARA_DEBUG_LOG("q: %s\n", q);
1073 ret
= real_query(q
);
1084 * remove/add entries
1086 static int com_rm_ne(__a_unused
int fd
, int argc
, char *argv
[])
1088 int ne
= !strcmp(argv
[0], "ne");
1091 return -E_MYSQL_SYNTAX
;
1092 for (i
= 1; i
< argc
; i
++) {
1093 ret
= remove_entry(argv
[i
]);
1098 ret
= add_entry(argv
[i
]);
1108 int com_rm(int fd
, int argc
, char *argv
[])
1110 return com_rm_ne(fd
, argc
, argv
);
1116 int com_ne(int fd
, int argc
, char *argv
[])
1118 return com_ne(fd
, argc
, argv
);
1124 int com_mv(__a_unused
int fd
, int argc
, char *argv
[])
1126 char *q
, *dn
, *ebn1
= NULL
, *ebn2
= NULL
, *edn
= NULL
;
1130 return -E_MYSQL_SYNTAX
;
1132 ebn1
= escaped_basename(argv
[1]);
1133 ebn2
= escaped_basename(argv
[2]);
1134 if (!ebn1
|| !ebn2
|| !*ebn1
|| !*ebn2
)
1136 ret
= -E_MYSQL_SYNTAX
;
1137 if (!strcmp(ebn1
, ebn2
))
1139 remove_entry(argv
[2]); /* no need to escape, ignore error */
1140 q
= make_message("update data set name = '%s' where name = '%s'",
1142 ret
= real_query(q
);
1146 ret
= -E_AUDIO_FILE
;
1147 if (!mysql_affected_rows(mysql_ptr
))
1149 q
= make_message("update dir set name = '%s' where name = '%s'",
1151 ret
= real_query(q
);
1157 dn
= para_dirname(argv
[2]);
1161 edn
= escape_str(dn
);
1168 q
= make_message("update dir set dir = '%s' where name = '%s'",
1170 ret
= real_query(q
);
1182 static int com_set(__a_unused
int fd
, int argc
, char *argv
[])
1187 const char *field
= strcmp(argv
[0], "picass")? "numplayed" : "pic_id";
1190 return -E_MYSQL_SYNTAX
;
1192 for (i
= 2; i
< argc
; i
++) {
1193 ebn
= escaped_basename(argv
[i
]);
1196 q
= make_message("update data set %s = %lu "
1197 "where name = '%s'", field
, id
, ebn
);
1199 ret
= real_query(q
);
1208 * snp: set numplayed
1210 int com_picass(int fd
, int argc
, char *argv
[])
1212 return com_set(fd
, argc
, argv
);
1216 * snp: set numplayed
1218 int com_snp(int fd
, int argc
, char *argv
[])
1220 int ret
= com_set(fd
, argc
, argv
);
1222 refresh_selector_info();
1227 * picch: change entry's name in pics table
1229 int com_picch(__a_unused
int fd
, int argc
, char *argv
[])
1236 return -E_MYSQL_SYNTAX
;
1239 tmp
= escape_str(argv
[2]);
1242 q
= make_message("update pics set name = '%s' where id = %lu", tmp
, id
);
1244 ret
= real_query(q
);
1250 * piclist: print list of pics in db
1252 int com_piclist(__a_unused
int fd
, int argc
, __a_unused
char *argv
[])
1254 void *result
= NULL
;
1256 unsigned long *length
;
1260 return -E_MYSQL_SYNTAX
;
1261 result
= get_result("select id,name,pic from pics order by id");
1264 while ((row
= mysql_fetch_row(result
))) {
1265 length
= mysql_fetch_lengths(result
);
1266 if (!row
|| !row
[0] || !row
[1] || !row
[2])
1268 ret
= send_va_buffer(fd
, "%s\t%lu\t%s\n", row
[0], length
[2], row
[1]);
1274 mysql_free_result(result
);
1279 * picdel: delete picture from database
1281 int com_picdel(int fd
, int argc
, char *argv
[])
1289 return -E_MYSQL_SYNTAX
;
1290 for (i
= 1; i
< argc
; i
++) {
1292 q
= make_message("delete from pics where id = %lu", id
);
1293 ret
= real_query(q
);
1297 aff
= mysql_affected_rows(mysql_ptr
);
1299 ret
= send_va_buffer(fd
, "No such id: %lu\n", id
);
1304 q
= make_message("update data set pic_id = 1 where pic_id = %lu", id
);
1305 ret
= real_query(q
);
1311 * pic: get picture by name or by number
1313 int com_pic(int fd
, int argc
, char *argv
[])
1315 void *result
= NULL
;
1317 unsigned long *length
, id
;
1319 char *q
, *name
= NULL
;
1322 ret
= -E_GET_AUDIO_FILE
;
1323 name
= get_current_audio_file();
1326 name
= escaped_basename(argv
[1]);
1331 id
= atoi(name
+ 1);
1333 id
= get_pic_id_by_name(name
);
1337 q
= make_message("select pic from pics where id = '%lu'", id
);
1338 result
= get_result(q
);
1342 row
= mysql_fetch_row(result
);
1344 if (!row
|| !row
[0])
1346 length
= mysql_fetch_lengths(result
);
1347 ret
= send_bin_buffer(fd
, row
[0], *length
);
1349 mysql_free_result(result
);
1354 int com_strdel(__a_unused
int fd
, int argc
, char *argv
[])
1360 return -E_MYSQL_SYNTAX
;
1361 tmp
= escape_str(argv
[1]);
1364 q
= make_message("delete from streams where name='%s'", tmp
);
1366 ret
= real_query(q
);
1374 int com_ls(int fd
, int argc
, char *argv
[])
1379 my_ulonglong num_rows
, top
= 0, left
= 0, right
= 0;
1382 return -E_MYSQL_SYNTAX
;
1384 char *tmp
= escape_str(argv
[1]);
1387 q
= make_message("select name from data where name like '%s'",
1391 q
= para_strdup("select name from data");
1392 result
= get_result(q
);
1396 num_rows
= mysql_num_rows(result
);
1399 ret
= print_results(fd
, result
, top
, left
, num_rows
- 1, right
);
1400 mysql_free_result(result
);
1407 int com_summary(__a_unused
int fd
, int argc
, __a_unused
char *argv
[])
1412 void *result2
= NULL
;
1413 const char *fmt
= "select count(name) from data where %s='1'";
1414 int ret
= -E_NORESULT
;
1417 return -E_MYSQL_SYNTAX
;
1418 result
= get_all_attributes();
1421 while ((row
= mysql_fetch_row(result
))) {
1428 buf
= make_message(fmt
, row
[0]);
1429 result2
= get_result(buf
);
1434 row2
= mysql_fetch_row(result2
);
1435 if (!row2
|| !row2
[0])
1437 ret
= send_va_buffer(fd
, "%s\t%s\n", row
[0], row2
[0]);
1444 mysql_free_result(result2
);
1446 mysql_free_result(result
);
1450 static int get_numplayed(char *name
)
1454 const char *fmt
= "select numplayed from data where name = '%s'";
1455 char *buf
= make_message(fmt
, name
);
1456 int ret
= -E_NORESULT
;
1458 result
= get_result(buf
);
1463 row
= mysql_fetch_row(result
);
1464 if (!row
|| !row
[0])
1469 mysql_free_result(result
);
1473 static int update_audio_file(char *name
)
1476 const char *fmt1
= "update data set lastplayed = now() where name = '%s'";
1477 const char *fmt2
= "update data set numplayed = %i where name = '%s'";
1479 char *ebn
= escaped_basename(name
);
1484 q
= make_message(fmt1
, ebn
);
1485 ret
= real_query(q
);
1489 ret
= get_numplayed(ebn
);
1492 q
= make_message(fmt2
, ret
+ 1, ebn
);
1493 ret
= real_query(q
);
1500 static void update_audio_file_server_handler(char *name
)
1503 info
= get_selector_info(name
);
1506 update_audio_file(name
);
1509 int com_us(__a_unused
int fd
, int argc
, char *argv
[])
1515 return -E_MYSQL_SYNTAX
;
1516 tmp
= escape_str(argv
[1]);
1519 ret
= update_audio_file(argv
[1]);
1522 refresh_selector_info();
1526 /* select previous / next stream */
1527 static int com_ps_ns(__a_unused
int fd
, int argc
, char *argv
[])
1529 char *query
, *stream
= get_current_stream();
1530 void *result
= get_result("select name from streams");
1533 my_ulonglong num_rows
, match
, i
;
1535 ret
= -E_MYSQL_SYNTAX
;
1541 num_rows
= mysql_num_rows(result
);
1542 ret
= -E_EMPTY_RESULT
;
1546 for (i
= 0; i
< num_rows
; i
++) {
1547 row
= mysql_fetch_row(result
);
1548 if (!row
|| !row
[0])
1550 if (!strcmp(row
[0], "current_stream"))
1552 if (!strcmp(row
[0], stream
))
1559 if (!strcmp(argv
[0], "ps"))
1560 i
= match
> 0? match
- 1 : num_rows
- 1;
1562 i
= match
< num_rows
- 1? match
+ 1 : 0;
1564 mysql_data_seek(result
, i
);
1565 row
= mysql_fetch_row(result
);
1566 if (!row
|| !row
[0])
1568 if (!strcmp(row
[0], "current_stream")) {
1569 if (!strcmp(argv
[0], "ps")) {
1570 i
= match
< 2? match
+ num_rows
- 2 : match
- 2;
1573 i
= i
> num_rows
- 1? i
- num_rows
: i
;
1575 mysql_data_seek(result
, i
);
1576 row
= mysql_fetch_row(result
);
1577 if (!row
|| !row
[0])
1580 query
= make_message("update streams set def='%s' where name = "
1581 "'current_stream'", row
[0]);
1582 ret
= real_query(query
);
1584 refresh_selector_info();
1588 mysql_free_result(result
);
1592 /* select previous stream */
1593 int com_ps(int fd
, int argc
, char *argv
[])
1595 return com_ps_ns(fd
, argc
, argv
);
1598 /* select next stream */
1599 int com_ns(int fd
, int argc
, char *argv
[])
1601 return com_ps_ns(fd
, argc
, argv
);
1605 int com_streams(int fd
, int argc
, __a_unused
char *argv
[])
1607 unsigned int num_rows
;
1608 int i
, ret
= -E_NORESULT
;
1612 if (argc
> 1 && strcmp(argv
[1], "current_stream"))
1613 return -E_MYSQL_SYNTAX
;
1615 char *cs
= get_current_stream();
1616 ret
= send_va_buffer(fd
, "%s\n", cs
);
1620 result
= get_result("select name from streams");
1623 num_rows
= mysql_num_rows(result
);
1628 for (i
= 0; i
< num_rows
; i
++) {
1629 row
= mysql_fetch_row(result
);
1630 if (!row
|| !row
[0])
1632 if (strcmp(row
[0], "current_stream"))
1633 send_va_buffer(fd
, "%s\n", row
[0]);
1638 mysql_free_result(result
);
1642 /* query stream definition */
1643 int com_strq(int fd
, int argc
, char *argv
[])
1651 ret
= -E_GET_STREAM
;
1652 name
= get_current_stream();
1655 name
= escaped_basename(argv
[1]);
1660 query
= make_message("select def from streams where name='%s'", name
);
1662 result
= get_result(query
);
1667 row
= mysql_fetch_row(result
);
1668 if (!row
|| !row
[0])
1670 /* no '\n' needed */
1671 ret
= send_buffer(fd
, row
[0]);
1674 mysql_free_result(result
);
1678 /* change stream / change stream and play */
1679 static int com_cs_csp(int fd
, int argc
, char *argv
[])
1681 int ret
, stream_change
;
1682 char *query
, *stream
= NULL
;
1683 char *old_stream
= get_current_stream();
1684 int csp
= !strcmp(argv
[0], "csp");
1686 ret
= -E_MYSQL_SYNTAX
;
1692 ret
= send_va_buffer(fd
, "%s\n", old_stream
);
1696 /* test if stream is valid, no need to escape argv[1] */
1697 query
= get_query(argv
[1], NULL
, 0);
1702 stream
= escape_str(argv
[1]);
1705 stream_change
= strcmp(stream
, old_stream
);
1706 if (stream_change
) {
1707 ret
= change_stream(stream
);
1710 refresh_selector_info();
1714 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
1716 mmd
->new_vss_status_flags
|= VSS_NEXT
;
1727 int com_cs(int fd
, int argc
, char *argv
[])
1729 return com_cs_csp(fd
, argc
, argv
);
1732 /* change stream and play */
1733 int com_csp(int fd
, int argc
, char *argv
[])
1735 return com_cs_csp(fd
, argc
, argv
);
1738 /* score list / skip */
1739 static int com_sl_skip(int fd
, int argc
, char *argv
[])
1741 void *result
= NULL
;
1743 int ret
, i
, skip
= !strcmp(argv
[0], "skip");
1744 char *query
, *stream
, *tmp
;
1745 unsigned int num_rows
, num
;
1748 return -E_MYSQL_SYNTAX
;
1749 num
= atoi(argv
[1]);
1751 return -E_MYSQL_SYNTAX
;
1753 stream
= get_current_stream();
1755 return -E_GET_STREAM
;
1757 stream
= escape_str(argv
[2]);
1761 tmp
= get_query(stream
, NULL
, 0);
1764 return -E_GET_QUERY
;
1765 query
= make_message("%s limit %d", tmp
, num
);
1768 result
= get_result(query
);
1772 ret
= -E_EMPTY_RESULT
;
1773 num_rows
= mysql_num_rows(result
);
1776 for (i
= 0; i
< num_rows
&& i
< num
; i
++) {
1777 row
= mysql_fetch_row(result
);
1779 send_va_buffer(fd
, "Skipping %s\n", row
[0]);
1780 update_audio_file(row
[0]);
1782 send_va_buffer(fd
, "%s\n", row
[0]? row
[0]: "BUG");
1787 mysql_free_result(result
);
1792 int com_sl(int fd
, int argc
, char *argv
[])
1794 return com_sl_skip(fd
, argc
, argv
);
1798 int com_skip(int fd
, int argc
, char *argv
[])
1800 return com_sl_skip(fd
, argc
, argv
);
1804 * update attributes of name
1806 static int update_atts(int fd
, char *name
, char *atts
)
1809 char *ebn
, *q
, *old
, *new = NULL
;
1813 ebn
= escaped_basename(name
);
1816 q
= make_message("update data set %s where name = '%s'", atts
, ebn
);
1817 old
= get_atts(ebn
, 0);
1818 send_va_buffer(fd
, "old: %s\n", old
);
1820 ret
= real_query(q
);
1824 new = get_atts(ebn
, 0);
1825 ret
= send_va_buffer(fd
, "new: %s\n", new);
1835 int com_sa(int fd
, int argc
, char *argv
[])
1838 char *atts
= NULL
, *name
;
1841 return -E_MYSQL_SYNTAX
;
1842 for (i
= 1; i
< argc
; i
++) {
1844 char *esc
, *tmp
, *p
=argv
[i
];
1845 int len
= strlen(p
);
1849 switch (p
[len
- 1]) {
1860 esc
= escape_str(p
);
1863 tmp
= make_message("%s%s='%s'", atts
? "," : "", esc
,
1866 atts
= para_strcat(atts
, tmp
);
1872 if (i
>= argc
) { /* no name given, use current af */
1873 ret
= -E_GET_AUDIO_FILE
;
1874 if (!(name
= get_current_audio_file()))
1876 ret
= update_atts(fd
, name
, atts
);
1880 for (; argv
[i
] && ret
>= 0; i
++)
1881 ret
= update_atts(fd
, argv
[i
], atts
);
1883 refresh_selector_info();
1892 int com_cam(int fd
, int argc
, char *argv
[])
1894 char *name
= NULL
, *meta
= NULL
, *atts
= NULL
;
1898 return -E_MYSQL_SYNTAX
;
1899 if (!(name
= escaped_basename(argv
[1])))
1902 if (!(atts
= get_atts(name
, 1)))
1905 if (!(meta
= get_meta(name
, 0)))
1907 for (i
= 2; i
< argc
; i
++) {
1910 if (!(ebn
= escaped_basename(argv
[i
])))
1912 ret
= send_va_buffer(fd
, "updating %s\n", ebn
);
1917 q
= make_message("update data set %s where name = '%s'",
1919 if ((ret
= update_atts(fd
, ebn
, atts
)) >= 0)
1920 ret
= real_query(q
);
1940 static int com_vrfy_clean(int fd
, int argc
, __a_unused
char *argv
[])
1943 int ret
, vrfy_mode
= strcmp(argv
[0], "clean");
1944 void *result
= NULL
;
1947 my_ulonglong num_rows
, top
= 0, left
= 0, right
= 0;
1950 return -E_MYSQL_SYNTAX
;
1952 result
= get_result("select data.name from data left join dir on "
1953 "dir.name = data.name where dir.name is NULL");
1956 num_rows
= mysql_num_rows(result
);
1958 ret
= send_buffer(fd
, "No invalid entries\n");
1962 send_va_buffer(fd
, "found %i invalid entr%s\n", num_rows
,
1963 num_rows
== 1? "y" : "ies");
1964 ret
= print_results(fd
, result
, top
, left
, num_rows
- 1, right
);
1967 while ((row
= mysql_fetch_row(result
))) {
1972 escaped_name
= escape_str(row
[0]);
1975 send_va_buffer(fd
, "deleting %s\n", escaped_name
);
1976 query
= make_message("delete from data where name = '%s'",
1978 ret
= real_query(query
);
1986 mysql_free_result(result
);
1993 int com_vrfy(int fd
, int argc
, char **argv
)
1995 return com_vrfy_clean(fd
, argc
, argv
);
2001 int com_clean(int fd
, int argc
, char **argv
)
2003 return com_vrfy_clean(fd
, argc
, argv
);
2006 static FILE *out_file
;
2008 static int mysql_write_tmp_file(const char *dir
, const char *name
)
2010 int ret
= -E_TMPFILE
;
2011 char *msg
= make_message("%s\t%s\n", dir
, name
);
2012 if (fputs(msg
, out_file
) != EOF
)
2021 int com_upd(int fd
, int argc
, __a_unused
char *argv
[])
2023 char *tempname
= NULL
, *query
= NULL
;
2024 int ret
, out_fd
= -1, num
= 0;
2025 void *result
= NULL
;
2026 unsigned int num_rows
;
2030 return -E_MYSQL_SYNTAX
;
2032 tempname
= para_strdup("/tmp/mysql.tmp.XXXXXX");
2033 ret
= para_mkstemp(tempname
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
2037 out_file
= fdopen(out_fd
, "w");
2042 if (find_audio_files(conf
.mysql_audio_file_dir_arg
, mysql_write_tmp_file
) < 0)
2044 num
= ftell(out_file
);
2046 * we have to make sure the file hit the disk before we call
2051 PARA_DEBUG_LOG("wrote tempfile %s (%d bytes)\n", tempname
, num
);
2054 if ((ret
= real_query("delete from dir")) < 0)
2056 query
= make_message("load data infile '%s' ignore into table dir "
2057 "fields terminated by '\t' lines terminated by '\n' "
2058 "(dir, name)", tempname
);
2059 ret
= real_query(query
);
2063 result
= get_result("select dir.name from dir left join data on "
2064 "data.name = dir.name where data.name is NULL");
2068 num_rows
= mysql_num_rows(result
);
2070 ret
= send_buffer(fd
, "no new entries\n");
2073 while ((row
= mysql_fetch_row(result
))) {
2078 send_va_buffer(fd
, "new entry: %s\n", row
[0]);
2079 erow
= escape_str(row
[0]);
2082 query
= make_message("insert into data (name, pic_id) values "
2083 "('%s','%s')", erow
, "1");
2085 ret
= real_query(query
);
2098 mysql_free_result(result
);
2102 static char **server_get_audio_file_list(unsigned int num
)
2104 char **list
= para_malloc((num
+ 1) * sizeof(char *));
2105 char *tmp
, *query
, *stream
= get_current_stream();
2106 void *result
= NULL
;
2107 unsigned int num_rows
;
2111 tmp
= get_query(stream
, NULL
, 1);
2115 query
= make_message("%s limit %d", tmp
, num
);
2117 result
= get_result(query
);
2121 num_rows
= mysql_num_rows(result
);
2124 for (i
= 0; i
< num_rows
&& i
< num
; i
++) {
2125 row
= mysql_fetch_row(result
);
2126 if (!row
|| !row
[0])
2128 list
[i
] = para_strdup(row
[0]);
2141 mysql_free_result(result
);
2146 * connect to mysql server, return mysql pointer on success, -E_NOTCONN
2147 * on errors. Called from parent on startup and also from com_cdb().
2149 static int init_mysql_server(void)
2151 char *u
= conf
.mysql_user_arg
? conf
.mysql_user_arg
: para_logname();
2154 mysql_ptr
= mysql_init(NULL
);
2156 PARA_CRIT_LOG("%s", "mysql init error\n");
2159 if (conf
.mysql_port_arg
< 0)
2160 return -E_MYSQL_SYNTAX
;
2161 port
= conf
.mysql_port_arg
;
2162 PARA_DEBUG_LOG("connecting: %s@%s:%d\n", u
, conf
.mysql_host_arg
, port
);
2163 if (!conf
.mysql_user_arg
)
2166 * If host is NULL a connection to the local host is assumed,
2167 * If user is NULL, the current user is assumed
2169 if (!(mysql_ptr
= mysql_real_connect(mysql_ptr
,
2170 conf
.mysql_host_arg
,
2171 conf
.mysql_user_arg
,
2172 conf
.mysql_passwd_arg
,
2173 conf
.mysql_database_arg
,
2175 PARA_CRIT_LOG("%s", "connect error\n");
2178 PARA_INFO_LOG("%s", "success\n");
2182 /* mmd lock must be held */
2183 static void write_msg2mmd(int success
)
2185 sprintf(mmd
->selector_info
, "dbinfo1:%s\ndbinfo2:mysql-%s\ndbinfo3:\n",
2186 success
< 0? PARA_STRERROR(-success
) :
2187 "successfully connected to mysql server",
2188 success
< 0? "" : mysql_get_server_info(mysql_ptr
));
2191 /* create database */
2192 int com_cdb(int fd
, int argc
, char *argv
[])
2198 PARA_INFO_LOG("%s", "closing database\n");
2199 mysql_close(mysql_ptr
);
2201 /* dont use any database */
2202 conf
.mysql_database_arg
= NULL
; /* leak? */
2203 ret
= -E_MYSQL_INIT
;
2204 if (init_mysql_server() < 0 || !mysql_ptr
)
2207 conf
.mysql_database_arg
= para_strdup("paraslash");
2210 conf
.mysql_database_arg
= escape_str(argv
[1]);
2211 if (!conf
.mysql_database_arg
)
2214 query
= make_message("create database %s", conf
.mysql_database_arg
);
2215 ret
= real_query(query
);
2219 /* reconnect with database just created */
2220 mysql_close(mysql_ptr
);
2221 ret
= -E_MYSQL_INIT
;
2222 if (init_mysql_server() < 0 || !mysql_ptr
)
2228 if (real_query("create table data (name varchar(255) binary not null "
2230 "lastplayed datetime not null default "
2232 "numplayed int not null default 0, "
2233 "pic_id bigint unsigned not null default 1)") < 0)
2235 if (real_query("create table dir (name varchar(255) binary not null "
2236 "primary key, dir varchar(255) default null)") < 0)
2238 if (real_query("create table pics ("
2239 "id bigint(20) unsigned not null primary key "
2241 "name varchar(255) binary not null, "
2242 "pic mediumblob not null)") < 0)
2244 if (real_query("create table streams ("
2245 "name varchar(255) binary not null primary key, "
2246 "def blob not null)") < 0)
2248 if (real_query("insert into streams (name, def) values "
2249 "('current_stream', '(none)')") < 0)
2251 ret
= send_va_buffer(fd
, "successfully created database %s\n",
2252 conf
.mysql_database_arg
);
2257 static void shutdown_connection(void)
2260 PARA_NOTICE_LOG("%s", "shutting down mysql connection\n");
2261 mysql_close(mysql_ptr
);
2267 * the init function of the mysql-based audio file selector
2269 * \param db pointer to the struct to initialize
2271 * Check the command line options and initialize all function pointers of \a
2272 * db. Connect to the mysql server and initialize the info string.
2274 * \return This function returns success even if it could not connect
2275 * to the mysql server. This is because the connect is expected to fail
2276 * if there the paraslash database is not yet created. This gives the
2277 * user a chance to send the "cdb" to create the database.
2279 * \sa struct audio_file_selector, misc_meta_data::selector_info,
2282 int mysql_selector_init(struct audio_file_selector
*db
)
2286 if (!conf
.mysql_passwd_given
)
2287 return -E_NO_MYSQL_PASSWD
;
2288 if (!conf
.mysql_audio_file_dir_given
)
2289 return -E_NO_AF_DIR
;
2291 db
->cmd_list
= mysql_selector_cmds
;
2292 db
->get_audio_file_list
= server_get_audio_file_list
;
2293 db
->update_audio_file
= update_audio_file_server_handler
;
2294 db
->shutdown
= shutdown_connection
;
2295 ret
= init_mysql_server();
2297 PARA_WARNING_LOG("%s\n", PARA_STRERROR(-ret
));
2299 return 1; /* return success even if connect failed to give the
2300 * user the chance to exec com_cdb