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 struct para_macro 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(struct para_macro
*macro_list
, char *src
)
151 struct para_macro
*mp
= macro_list
;
152 char *ret
= NULL
, *tmp
= para_strdup(src
);
155 ret
= s_a_r(tmp
, mp
->name
, mp
->replacement
);
157 if (!ret
) /* syntax error */
162 //PARA_DEBUG_LOG("%s: returning %s\n", __func__, dest);
166 static int real_query(const char *query
)
170 PARA_DEBUG_LOG("%s\n", query
);
171 if (mysql_real_query(mysql_ptr
, query
, strlen(query
))) {
172 PARA_ERROR_LOG("real_query error (%s)\n",
173 mysql_error(mysql_ptr
));
180 * Use open connection given by mysql_ptr to query server. Returns a
181 * result pointer on succes and NULL on errors
183 static struct MYSQL_RES
*get_result(const char *query
)
187 if (real_query(query
) < 0)
189 result
= mysql_store_result(mysql_ptr
);
191 PARA_ERROR_LOG("%s", "store_result error\n");
195 * write input from fd to dynamically allocated char array,
196 * but maximal max_size byte. Return size.
198 static int fd2buf(int fd
, char **buf_ptr
, size_t max_size
)
200 const size_t chunk_size
= 1024;
202 char *buf
= para_malloc(size
* sizeof(char)), *p
= buf
;
205 while ((ret
= recv_bin_buffer(fd
, p
, chunk_size
)) > 0) {
207 if ((p
- buf
) + chunk_size
>= size
) {
211 if (size
> max_size
) {
215 tmp
= para_realloc(buf
, size
);
230 static char *escape_blob(const char* old
, int size
)
234 if (!mysql_ptr
|| size
< 0)
236 new = para_malloc(2 * size
* sizeof(char) + 1);
237 mysql_real_escape_string(mysql_ptr
, new, old
, size
);
241 static char *escape_str(const char* old
)
243 return escape_blob(old
, strlen(old
));
246 static char *escaped_basename(const char *name
)
248 char *esc
, *bn
= para_basename(name
);
252 esc
= escape_str(bn
);
260 int com_na(__a_unused
int fd
, int argc
, char *argv
[])
266 return -E_MYSQL_SYNTAX
;
267 tmp
= escape_str(argv
[1]);
270 q
= make_message("alter table data add %s char(1) "
271 "not null default 0", tmp
);
281 int com_da(__a_unused
int fd
, int argc
, char *argv
[])
287 return -E_MYSQL_SYNTAX
;
288 tmp
= escape_str(argv
[1]);
291 q
= make_message("alter table data drop %s", tmp
);
299 static int com_stradd_picadd(int fd
, int argc
, char *argv
[])
301 char *blob
= NULL
, *esc_blob
= NULL
, *q
= NULL
, *tmp
= NULL
;
302 const char *fmt
, *del_fmt
;
303 int ret
, stradd
= strcmp(argv
[0], "picadd");
307 return -E_MYSQL_SYNTAX
;
308 if (strlen(argv
[1]) >= MAXLINE
- 1)
309 return -E_NAMETOOLONG
;
314 fmt
= "insert into streams (name, def) values ('%s','%s')";
315 del_fmt
="delete from streams where name='%s'";
317 size
= MEDIUM_BLOB_SIZE
;
318 fmt
= "insert into pics (name, pic) values ('%s','%s')";
319 del_fmt
="delete from pics where pic='%s'";
321 tmp
= escape_str(argv
[1]);
324 q
= make_message(del_fmt
, tmp
);
330 if ((ret
= send_buffer(fd
, AWAITING_DATA_MSG
) < 0))
332 if ((ret
= fd2buf(fd
, &blob
, size
)) < 0)
338 esc_blob
= escape_blob(blob
, size
);
341 tmp
= escape_str(argv
[1]);
344 q
= make_message(fmt
, tmp
, esc_blob
);
355 int com_stradd(int fd
, int argc
, char *argv
[])
357 return com_stradd_picadd(fd
, argc
, argv
);
361 int com_picadd(int fd
, int argc
, char *argv
[])
363 return com_stradd_picadd(fd
, argc
, argv
);
367 * print results to fd
369 static int print_results(int fd
, void *result
,
370 unsigned int top
, unsigned int left
,
371 unsigned int bottom
, unsigned int right
)
377 for (i
= top
; i
<= bottom
; i
++) {
378 row
= mysql_fetch_row(result
);
381 for (j
= left
; j
<= right
; j
++) {
382 ret
= send_va_buffer(fd
, j
== left
? "%s" : "\t%s",
383 row
[j
]? row
[j
] : "NULL");
387 ret
= send_buffer(fd
, "\n");
397 int com_verb(int fd
, int argc
, char *argv
[])
401 unsigned int num_rows
, num_fields
;
405 return -E_MYSQL_SYNTAX
;
406 tmp
= escape_str(argv
[1]);
409 result
= get_result(tmp
);
412 /* return success, because it's ok to have no results */
414 num_fields
= mysql_field_count(mysql_ptr
);
415 num_rows
= mysql_num_rows(result
);
417 if (num_fields
&& num_rows
)
418 ret
= print_results(fd
, result
, 0, 0, num_rows
- 1,
420 mysql_free_result(result
);
424 /* returns NULL on errors or if there are no atts defined yet */
425 static void *get_all_attributes(void)
427 void *result
= get_result("desc data");
428 unsigned int num_rows
;
432 num_rows
= mysql_num_rows(result
);
434 mysql_free_result(result
);
437 mysql_data_seek(result
, 4); /* skip Lastplayed, Numplayed... */
442 * list all attributes
444 int com_laa(int fd
, int argc
, __a_unused
char *argv
[])
450 return -E_MYSQL_SYNTAX
;
451 result
= get_all_attributes();
454 ret
= print_results(fd
, result
, 0, 0, mysql_num_rows(result
) - 5, 0);
455 mysql_free_result(result
);
462 int com_hist(int fd
, int argc
, char *argv
[])
467 unsigned int num_rows
;
470 return -E_MYSQL_SYNTAX
;
472 char *tmp
= escape_str(argv
[1]);
475 atts
= make_message("where %s = '1'", tmp
);
478 atts
= para_strdup(NULL
);
480 q
= make_message("select name, to_days(now()) - to_days(lastplayed) from "
481 "data %s order by lastplayed", atts
);
483 result
= get_result(q
);
487 num_rows
= mysql_num_rows(result
);
490 ret
= print_results(fd
, result
, 0, 0, num_rows
- 1, 1);
491 mysql_free_result(result
);
496 * get last num audio files
498 int com_last(int fd
, int argc
, char *argv
[])
509 return -E_MYSQL_SYNTAX
;
510 q
= make_message("select name from data order by lastplayed desc "
512 result
= get_result(q
);
516 ret
= print_results(fd
, result
, 0, 0, mysql_num_rows(result
) - 1, 0);
517 mysql_free_result(result
);
521 int com_mbox(int fd
, int argc
, char *argv
[])
526 unsigned int num_rows
, num_fields
;
527 char *query
= para_strdup("select concat('From foo@localhost ', "
528 "date_format(Lastplayed, '%a %b %e %T %Y'), "
529 "'\nReceived: from\nTo: bar\n");
532 result
= get_all_attributes();
536 while ((row
= mysql_fetch_row(result
))) {
541 tmp
= make_message("%sX-Attribute-%s: ', %s, '\n", query
,
546 query
= para_strcat(query
,
554 char *esc
= escape_str(argv
[1]), *tmp
;
558 tmp
= make_message("%s where name LIKE '%s'", query
, esc
);
563 mysql_free_result(result
);
565 result
= get_result(query
);
568 ret
= -E_EMPTY_RESULT
;
569 num_fields
= mysql_field_count(mysql_ptr
);
570 num_rows
= mysql_num_rows(result
);
571 if (!num_fields
|| !num_rows
)
573 ret
= print_results(fd
, result
, 0, 0, num_rows
- 1, num_fields
- 1);
577 mysql_free_result(result
);
582 * get attributes by name. If verbose is not 0, this function returns a string
583 * of the form 'att1="0",att2="1"'... which is used in com_cam() for
584 * constructing a mysql update query. Otherwise the space-separated list of all
585 * attributes which are set in the audio file given by name is returned. Never
586 * returns NULL in *NON VERBOSE* mode.
588 static char *get_atts(char *name
, int verbose
)
590 char *atts
= NULL
, *buf
, *ebn
;
591 void *result
= NULL
, *result2
= NULL
;
596 result2
= get_all_attributes();
599 ebn
= escaped_basename(name
);
602 buf
= make_message("select * from data where name='%s'", ebn
);
604 result
= get_result(buf
);
608 num_fields
= mysql_num_fields(result
);
611 mysql_data_seek(result2
, 4); /* skip Lastplayed, Numplayed... */
612 row
= mysql_fetch_row(result
);
615 for (i
= 4; i
< num_fields
; i
++) {
616 int is_set
= row
[i
] && !strcmp(row
[i
], "1");
617 row2
= mysql_fetch_row(result2
);
618 if (!row2
|| !row2
[0])
620 if (atts
&& (verbose
|| is_set
))
621 atts
= para_strcat(atts
, verbose
? "," : " ");
622 if (is_set
|| verbose
)
623 atts
= para_strcat(atts
, row2
[0]);
625 atts
= para_strcat(atts
, is_set
? "=\"1\"" : "=\"0\"");
629 mysql_free_result(result2
);
631 mysql_free_result(result
);
632 if (!atts
&& !verbose
)
633 atts
= para_strdup("(none)");
637 /* never returns NULL in verbose mode */
638 static char *get_meta(char *name
, int verbose
)
642 char *ebn
, *q
, *ret
= NULL
;
643 const char *verbose_fmt
=
644 "select concat('lastplayed: ', "
645 "(to_days(now()) - to_days(lastplayed)),"
646 "' day(s). numplayed: ', numplayed, "
647 "', pic: ', pic_id) "
648 "from data where name = '%s'";
649 /* is that really needed? */
650 const char *fmt
= "select concat('lastplayed=\\'', lastplayed, "
651 "'\\', numplayed=\\'', numplayed, "
652 "'\\', pic_id=\\'', pic_id, '\\'') "
653 "from data where name = '%s'";
655 if (!(ebn
= escaped_basename(name
)))
657 q
= make_message(verbose
? verbose_fmt
: fmt
, ebn
);
659 result
= get_result(q
);
663 row
= mysql_fetch_row(result
);
666 ret
= para_strdup(row
[0]);
669 mysql_free_result(result
);
671 ret
= para_strdup("(not yet played)");
675 static char *get_dir(char *name
)
677 char *ret
= NULL
, *q
, *ebn
;
681 if (!(ebn
= escaped_basename(name
)))
683 q
= make_message("select dir from dir where name = '%s'", ebn
);
685 result
= get_result(q
);
689 row
= mysql_fetch_row(result
);
691 ret
= para_strdup(row
[0]);
692 mysql_free_result(result
);
696 /* never returns NULL */
697 static char *get_current_stream(void)
701 void *result
= get_result("select def from streams where "
702 "name = 'current_stream'");
706 row
= mysql_fetch_row(result
);
709 ret
= para_strdup(row
[0]);
710 mysql_free_result(result
);
714 mysql_free_result(result
);
715 return para_strdup("(none)");
719 * Read stream definition of stream streamname and construct mysql
720 * query. Return NULL on errors. If streamname is NULL, use current
721 * stream. If that is also NULL, use query that selects everything.
722 * If filename is NULL, query will list everything, otherwise only
723 * the score of given file.
725 static char *get_query(char *streamname
, char *filename
, int with_path
)
727 char *accept_opts
= NULL
, *deny_opts
= NULL
, *score
= NULL
;
728 char *where_clause
, *order
, *query
;
729 char command
[255] = ""; /* buffer for sscanf */
733 char *select_clause
= NULL
;
735 tmp
= get_current_stream();
737 tmp
= escape_str(streamname
);
741 if (!strcmp(tmp
, "(none)")) {
744 char *ret
, *ebn
= escaped_basename(filename
);
747 ret
= make_message("select to_days(now()) - "
748 "to_days(lastplayed) from data "
749 "where name = '%s'", ebn
);
755 "select concat(dir.dir, '/', dir.name) "
756 "from data, dir where dir.name = data.name "
757 "order by data.lastplayed"
760 "select name from data where name is not NULL "
761 "order by lastplayed"
765 query
= make_message("select def from streams where name = '%s'",
767 result
= get_result(query
);
772 row
= mysql_fetch_row(result
);
778 char *arg
, *line
= end
;
780 if (!(end
= strchr(line
, '\n')))
784 if (sscanf(line
, "%200s%n", command
, &n
) < 1)
787 if (!strcmp(command
, "accept:")) {
788 char *tmp2
= s_a_r_list(macro_list
, arg
);
790 accept_opts
= para_strcat(
791 accept_opts
, " or ");
792 accept_opts
= para_strcat(accept_opts
, tmp2
);
796 if (!strcmp(command
, "deny:")) {
797 char *tmp2
= s_a_r_list(macro_list
, arg
);
799 deny_opts
= para_strcat(deny_opts
, " or ");
800 deny_opts
= para_strcat(deny_opts
, tmp2
);
804 if (!score
&& !strcmp(command
, "score:"))
805 score
= s_a_r_list(macro_list
, arg
);
808 score
= s_a_r_list(macro_list
, conf
.mysql_default_score_arg
);
813 char *ebn
= escaped_basename(filename
);
816 select_clause
= make_message("select %s from data ", score
);
818 where_clause
= make_message( "where name = '%s' ", ebn
);
820 order
= para_strdup("");
823 select_clause
= para_strdup(with_path
?
824 "select concat(dir.dir, '/', dir.name) from data, dir "
825 "where dir.name = data.name "
827 "select name from data where name is not NULL");
828 order
= make_message("order by -(%s)", score
);
830 if (accept_opts
&& deny_opts
) {
831 where_clause
= make_message("and ((%s) and not (%s)) ",
832 accept_opts
, deny_opts
);
835 if (accept_opts
&& !deny_opts
) {
836 where_clause
= make_message("and (%s) ", accept_opts
);
839 if (!accept_opts
&& deny_opts
) {
840 where_clause
= make_message("and not (%s) ", deny_opts
);
843 where_clause
= para_strdup("");
845 query
= make_message("%s %s %s", select_clause
, where_clause
, order
);
855 mysql_free_result(result
);
862 * This is called from server and from some commands. Name must not be NULL
863 * Never returns NULL.
865 static char *get_selector_info(char *name
)
867 char *meta
, *atts
, *info
, *dir
, *query
, *stream
;
869 MYSQL_ROW row
= NULL
;
872 return para_strdup("(none)");
873 stream
= get_current_stream();
874 meta
= get_meta(name
, 1);
875 atts
= get_atts(name
, 0);
878 query
= get_query(stream
, name
, 0); /* FIXME: pass stream == NULL instead? */
881 result
= get_result(query
);
884 row
= mysql_fetch_row(result
);
886 info
= make_message("dbinfo1:dir: %s\n"
887 "dbinfo2:stream: %s, %s, score: %s\n"
889 dir
? dir
: "(not contained in table)",
891 (result
&& row
&& row
[0])? row
[0] : "(no score)",
898 mysql_free_result(result
);
903 /* might return NULL */
904 static char *get_current_audio_file(void)
908 name
= para_basename(mmd
->filename
);
913 /* list attributes / print database info */
914 static int com_la_info(int fd
, int argc
, char *argv
[])
916 char *name
= NULL
, *meta
= NULL
, *atts
= NULL
, *dir
= NULL
;
917 int ret
, com_la
= strcmp(argv
[0], "info");
920 ret
= -E_GET_AUDIO_FILE
;
921 if (!(name
= get_current_audio_file()))
923 ret
= send_va_buffer(fd
, "%s\n", name
);
928 if (!(name
= escaped_basename(argv
[1])))
931 meta
= get_meta(name
, 1);
932 atts
= get_atts(name
, 0);
935 ret
= send_va_buffer(fd
, "%s\n", atts
);
937 ret
= send_va_buffer(fd
, "dir: %s\n" "%s\n" "attributes: %s\n",
938 dir
? dir
: "(not contained in table)", meta
, atts
);
947 /* list attributes */
948 int com_la(int fd
, int argc
, char *argv
[])
950 return com_la_info(fd
, argc
, argv
);
953 /* print database info */
954 int com_info(int fd
, int argc
, char *argv
[])
956 return com_la_info(fd
, argc
, argv
);
959 static int change_stream(const char *stream
)
963 query
= make_message("update streams set def='%s' "
964 "where name = 'current_stream'", stream
);
965 ret
= real_query(query
);
970 static int get_pic_id_by_name(char *name
)
977 if (!(ebn
= escaped_basename(name
)))
979 q
= make_message("select pic_id from data where name = '%s'", ebn
);
981 result
= get_result(q
);
985 row
= mysql_fetch_row(result
);
989 mysql_free_result(result
);
993 static int remove_entry(const char *name
)
995 char *q
, *ebn
= escaped_basename(name
);
1000 q
= make_message("delete from data where name = '%s'", ebn
);
1001 real_query(q
); /* ignore errors */
1003 q
= make_message("delete from dir where name = '%s'", ebn
);
1004 real_query(q
); /* ignore errors */
1012 static int add_entry(const char *name
)
1014 char *q
, *dn
, *ebn
= NULL
, *edn
= NULL
;
1017 if (!name
|| !*name
)
1018 return -E_MYSQL_SYNTAX
;
1019 ebn
= escaped_basename(name
);
1022 ret
= -E_MYSQL_SYNTAX
;
1023 dn
= para_dirname(name
);
1027 edn
= escape_str(dn
);
1031 q
= make_message("insert into data (name, pic_id) values "
1032 "('%s', '%s')", ebn
, "1");
1033 ret
= real_query(q
);
1034 // ret = 1; PARA_DEBUG_LOG("q: %s\n", q);
1038 q
= make_message("insert into dir (name, dir) values "
1039 "('%s', '%s')", ebn
, edn
);
1040 // ret = 1; PARA_DEBUG_LOG("q: %s\n", q);
1041 ret
= real_query(q
);
1052 * remove/add entries
1054 static int com_rm_ne(__a_unused
int fd
, int argc
, char *argv
[])
1056 int ne
= !strcmp(argv
[0], "ne");
1059 return -E_MYSQL_SYNTAX
;
1060 for (i
= 1; i
< argc
; i
++) {
1061 ret
= remove_entry(argv
[i
]);
1066 ret
= add_entry(argv
[i
]);
1076 int com_rm(int fd
, int argc
, char *argv
[])
1078 return com_rm_ne(fd
, argc
, argv
);
1084 int com_ne(int fd
, int argc
, char *argv
[])
1086 return com_ne(fd
, argc
, argv
);
1092 int com_mv(__a_unused
int fd
, int argc
, char *argv
[])
1094 char *q
, *dn
, *ebn1
= NULL
, *ebn2
= NULL
, *edn
= NULL
;
1098 return -E_MYSQL_SYNTAX
;
1100 ebn1
= escaped_basename(argv
[1]);
1101 ebn2
= escaped_basename(argv
[2]);
1102 if (!ebn1
|| !ebn2
|| !*ebn1
|| !*ebn2
)
1104 ret
= -E_MYSQL_SYNTAX
;
1105 if (!strcmp(ebn1
, ebn2
))
1107 remove_entry(argv
[2]); /* no need to escape, ignore error */
1108 q
= make_message("update data set name = '%s' where name = '%s'",
1110 ret
= real_query(q
);
1114 ret
= -E_AUDIO_FILE
;
1115 if (!mysql_affected_rows(mysql_ptr
))
1117 q
= make_message("update dir set name = '%s' where name = '%s'",
1119 ret
= real_query(q
);
1125 dn
= para_dirname(argv
[2]);
1129 edn
= escape_str(dn
);
1136 q
= make_message("update dir set dir = '%s' where name = '%s'",
1138 ret
= real_query(q
);
1150 static int com_set(__a_unused
int fd
, int argc
, char *argv
[])
1155 const char *field
= strcmp(argv
[0], "picass")? "numplayed" : "pic_id";
1158 return -E_MYSQL_SYNTAX
;
1160 for (i
= 2; i
< argc
; i
++) {
1161 ebn
= escaped_basename(argv
[i
]);
1164 q
= make_message("update data set %s = %lu "
1165 "where name = '%s'", field
, id
, ebn
);
1167 ret
= real_query(q
);
1176 * snp: set numplayed
1178 int com_picass(int fd
, int argc
, char *argv
[])
1180 return com_set(fd
, argc
, argv
);
1184 * snp: set numplayed
1186 int com_snp(int fd
, int argc
, char *argv
[])
1188 return com_set(fd
, argc
, argv
);
1192 * picch: change entry's name in pics table
1194 int com_picch(__a_unused
int fd
, int argc
, char *argv
[])
1201 return -E_MYSQL_SYNTAX
;
1204 tmp
= escape_str(argv
[2]);
1207 q
= make_message("update pics set name = '%s' where id = %lu", tmp
, id
);
1209 ret
= real_query(q
);
1215 * piclist: print list of pics in db
1217 int com_piclist(__a_unused
int fd
, int argc
, __a_unused
char *argv
[])
1219 void *result
= NULL
;
1221 unsigned long *length
;
1225 return -E_MYSQL_SYNTAX
;
1226 result
= get_result("select id,name,pic from pics order by id");
1229 while ((row
= mysql_fetch_row(result
))) {
1230 length
= mysql_fetch_lengths(result
);
1231 if (!row
|| !row
[0] || !row
[1] || !row
[2])
1233 ret
= send_va_buffer(fd
, "%s\t%lu\t%s\n", row
[0], length
[2], row
[1]);
1239 mysql_free_result(result
);
1244 * picdel: delete picture from database
1246 int com_picdel(int fd
, int argc
, char *argv
[])
1254 return -E_MYSQL_SYNTAX
;
1255 for (i
= 1; i
< argc
; i
++) {
1257 q
= make_message("delete from pics where id = %lu", id
);
1258 ret
= real_query(q
);
1262 aff
= mysql_affected_rows(mysql_ptr
);
1264 ret
= send_va_buffer(fd
, "No such id: %lu\n", id
);
1269 q
= make_message("update data set pic_id = 1 where pic_id = %lu", id
);
1270 ret
= real_query(q
);
1276 * pic: get picture by name or by number
1278 int com_pic(int fd
, int argc
, char *argv
[])
1280 void *result
= NULL
;
1282 unsigned long *length
, id
;
1284 char *q
, *name
= NULL
;
1287 ret
= -E_GET_AUDIO_FILE
;
1288 name
= get_current_audio_file();
1291 name
= escaped_basename(argv
[1]);
1296 id
= atoi(name
+ 1);
1298 id
= get_pic_id_by_name(name
);
1302 q
= make_message("select pic from pics where id = '%lu'", id
);
1303 result
= get_result(q
);
1307 row
= mysql_fetch_row(result
);
1309 if (!row
|| !row
[0])
1311 length
= mysql_fetch_lengths(result
);
1312 ret
= send_bin_buffer(fd
, row
[0], *length
);
1314 mysql_free_result(result
);
1319 int com_strdel(__a_unused
int fd
, int argc
, char *argv
[])
1325 return -E_MYSQL_SYNTAX
;
1326 tmp
= escape_str(argv
[1]);
1329 q
= make_message("delete from streams where name='%s'", tmp
);
1331 ret
= real_query(q
);
1339 int com_ls(int fd
, int argc
, char *argv
[])
1344 unsigned int num_rows
;
1347 return -E_MYSQL_SYNTAX
;
1349 char *tmp
= escape_str(argv
[1]);
1352 q
= make_message("select name from data where name like '%s'",
1356 q
= para_strdup("select name from data");
1357 result
= get_result(q
);
1361 num_rows
= mysql_num_rows(result
);
1364 ret
= print_results(fd
, result
, 0, 0, num_rows
- 1, 0);
1365 mysql_free_result(result
);
1372 int com_summary(__a_unused
int fd
, int argc
, __a_unused
char *argv
[])
1377 void *result2
= NULL
;
1378 const char *fmt
= "select count(name) from data where %s='1'";
1379 int ret
= -E_NORESULT
;
1382 return -E_MYSQL_SYNTAX
;
1383 result
= get_all_attributes();
1386 while ((row
= mysql_fetch_row(result
))) {
1393 buf
= make_message(fmt
, row
[0]);
1394 result2
= get_result(buf
);
1399 row2
= mysql_fetch_row(result2
);
1400 if (!row2
|| !row2
[0])
1402 ret
= send_va_buffer(fd
, "%s\t%s\n", row
[0], row2
[0]);
1409 mysql_free_result(result2
);
1411 mysql_free_result(result
);
1415 static int get_numplayed(char *name
)
1419 const char *fmt
= "select numplayed from data where name = '%s'";
1420 char *buf
= make_message(fmt
, name
);
1421 int ret
= -E_NORESULT
;
1423 result
= get_result(buf
);
1428 row
= mysql_fetch_row(result
);
1429 if (!row
|| !row
[0])
1434 mysql_free_result(result
);
1438 static int update_audio_file(char *name
)
1441 const char *fmt1
= "update data set lastplayed = now() where name = '%s'";
1442 const char *fmt2
= "update data set numplayed = %i where name = '%s'";
1444 char *ebn
= escaped_basename(name
);
1449 q
= make_message(fmt1
, ebn
);
1450 ret
= real_query(q
);
1454 ret
= get_numplayed(ebn
);
1457 q
= make_message(fmt2
, ret
+ 1, ebn
);
1458 ret
= real_query(q
);
1465 /* If called as child, mmd_lock must be held */
1466 static void update_mmd(char *info
)
1468 PARA_DEBUG_LOG("%s", "updating shared memory area\n");
1469 strncpy(mmd
->selector_info
, info
, MMD_INFO_SIZE
- 1);
1470 mmd
->selector_info
[MMD_INFO_SIZE
- 1] = '\0';
1473 static void update_audio_file_server_handler(char *name
)
1476 info
= get_selector_info(name
);
1479 update_audio_file(name
);
1482 int com_us(__a_unused
int fd
, int argc
, char *argv
[])
1488 return -E_MYSQL_SYNTAX
;
1489 tmp
= escape_str(argv
[1]);
1492 ret
= update_audio_file(argv
[1]);
1497 static void refresh_selector_info(void)
1499 char *name
= get_current_audio_file();
1504 info
= get_selector_info(name
);
1512 /* select previous / next stream */
1513 static int com_ps_ns(__a_unused
int fd
, int argc
, char *argv
[])
1515 char *query
, *stream
= get_current_stream();
1516 void *result
= get_result("select name from streams");
1518 int match
= -1, ret
, i
;
1519 unsigned int num_rows
;
1522 return -E_MYSQL_SYNTAX
;
1526 num_rows
= mysql_num_rows(result
);
1527 ret
= -E_EMPTY_RESULT
;
1531 for (i
= 0; i
< num_rows
; i
++) {
1532 row
= mysql_fetch_row(result
);
1533 if (!row
|| !row
[0])
1535 if (!strcmp(row
[0], "current_stream"))
1537 if (!strcmp(row
[0], stream
)) {
1545 if (!strcmp(argv
[0], "ps"))
1546 i
= match
> 0? match
- 1 : num_rows
- 1;
1548 i
= match
< num_rows
- 1? match
+ 1 : 0;
1550 mysql_data_seek(result
, i
);
1551 row
= mysql_fetch_row(result
);
1552 if (!row
|| !row
[0])
1554 if (!strcmp(row
[0], "current_stream")) {
1555 if (!strcmp(argv
[0], "ps")) {
1557 i
= i
< 0? i
+ num_rows
: i
;
1560 i
= i
> num_rows
- 1? i
- num_rows
: i
;
1562 mysql_data_seek(result
, i
);
1563 row
= mysql_fetch_row(result
);
1564 if (!row
|| !row
[0])
1567 query
= make_message("update streams set def='%s' where name = "
1568 "'current_stream'", row
[0]);
1569 ret
= real_query(query
);
1571 refresh_selector_info();
1575 mysql_free_result(result
);
1579 /* select previous stream */
1580 int com_ps(int fd
, int argc
, char *argv
[])
1582 return com_ps_ns(fd
, argc
, argv
);
1585 /* select next stream */
1586 int com_ns(int fd
, int argc
, char *argv
[])
1588 return com_ps_ns(fd
, argc
, argv
);
1592 int com_streams(int fd
, int argc
, __a_unused
char *argv
[])
1594 unsigned int num_rows
;
1595 int i
, ret
= -E_NORESULT
;
1599 if (argc
> 1 && strcmp(argv
[1], "current_stream"))
1600 return -E_MYSQL_SYNTAX
;
1602 char *cs
= get_current_stream();
1603 ret
= send_va_buffer(fd
, "%s\n", cs
);
1607 result
= get_result("select name from streams");
1610 num_rows
= mysql_num_rows(result
);
1615 for (i
= 0; i
< num_rows
; i
++) {
1616 row
= mysql_fetch_row(result
);
1617 if (!row
|| !row
[0])
1619 if (strcmp(row
[0], "current_stream"))
1620 send_va_buffer(fd
, "%s\n", row
[0]);
1625 mysql_free_result(result
);
1629 /* query stream definition */
1630 int com_strq(int fd
, int argc
, char *argv
[])
1638 ret
= -E_GET_STREAM
;
1639 name
= get_current_stream();
1642 name
= escaped_basename(argv
[1]);
1647 query
= make_message("select def from streams where name='%s'", name
);
1649 result
= get_result(query
);
1654 row
= mysql_fetch_row(result
);
1655 if (!row
|| !row
[0])
1657 /* no '\n' needed */
1658 ret
= send_buffer(fd
, row
[0]);
1661 mysql_free_result(result
);
1665 /* change stream / change stream and play */
1666 static int com_cs_csp(int fd
, int argc
, char *argv
[])
1668 int ret
, stream_change
;
1669 char *query
, *stream
= NULL
;
1670 char *old_stream
= get_current_stream();
1671 int csp
= !strcmp(argv
[0], "csp");
1673 ret
= -E_MYSQL_SYNTAX
;
1679 ret
= send_va_buffer(fd
, "%s\n", old_stream
);
1683 /* test if stream is valid, no need to escape argv[1] */
1684 query
= get_query(argv
[1], NULL
, 0);
1689 stream
= escape_str(argv
[1]);
1692 stream_change
= strcmp(stream
, old_stream
);
1693 if (stream_change
) {
1694 ret
= change_stream(stream
);
1697 refresh_selector_info();
1701 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
1703 mmd
->new_vss_status_flags
|= VSS_NEXT
;
1714 int com_cs(int fd
, int argc
, char *argv
[])
1716 return com_cs_csp(fd
, argc
, argv
);
1719 /* change stream and play */
1720 int com_csp(int fd
, int argc
, char *argv
[])
1722 return com_cs_csp(fd
, argc
, argv
);
1725 /* score list / skip */
1726 static int com_sl_skip(int fd
, int argc
, char *argv
[])
1728 void *result
= NULL
;
1730 int ret
, i
, skip
= !strcmp(argv
[0], "skip");
1731 char *query
, *stream
, *tmp
;
1732 unsigned int num_rows
, num
;
1735 return -E_MYSQL_SYNTAX
;
1736 num
= atoi(argv
[1]);
1738 return -E_MYSQL_SYNTAX
;
1740 stream
= get_current_stream();
1742 return -E_GET_STREAM
;
1744 stream
= escape_str(argv
[2]);
1748 tmp
= get_query(stream
, NULL
, 0);
1751 return -E_GET_QUERY
;
1752 query
= make_message("%s limit %d", tmp
, num
);
1755 result
= get_result(query
);
1759 ret
= -E_EMPTY_RESULT
;
1760 num_rows
= mysql_num_rows(result
);
1763 for (i
= 0; i
< num_rows
&& i
< num
; i
++) {
1764 row
= mysql_fetch_row(result
);
1766 send_va_buffer(fd
, "Skipping %s\n", row
[0]);
1767 update_audio_file(row
[0]);
1769 send_va_buffer(fd
, "%s\n", row
[0]? row
[0]: "BUG");
1774 mysql_free_result(result
);
1779 int com_sl(int fd
, int argc
, char *argv
[])
1781 return com_sl_skip(fd
, argc
, argv
);
1785 int com_skip(int fd
, int argc
, char *argv
[])
1787 return com_sl_skip(fd
, argc
, argv
);
1791 * update attributes of name
1793 static int update_atts(int fd
, char *name
, char *atts
)
1796 char *ebn
, *q
, *old
, *new = NULL
;
1800 ebn
= escaped_basename(name
);
1803 q
= make_message("update data set %s where name = '%s'", atts
, ebn
);
1804 old
= get_atts(ebn
, 0);
1805 send_va_buffer(fd
, "old: %s\n", old
);
1807 ret
= real_query(q
);
1811 new = get_atts(ebn
, 0);
1812 ret
= send_va_buffer(fd
, "new: %s\n", new);
1822 int com_sa(int fd
, int argc
, char *argv
[])
1825 char *atts
= NULL
, *name
;
1828 return -E_MYSQL_SYNTAX
;
1829 for (i
= 1; i
< argc
; i
++) {
1831 char *esc
, *tmp
, *p
=argv
[i
];
1832 int len
= strlen(p
);
1836 switch (p
[len
- 1]) {
1847 esc
= escape_str(p
);
1850 tmp
= make_message("%s%s='%s'", atts
? "," : "", esc
,
1853 atts
= para_strcat(atts
, tmp
);
1859 if (i
>= argc
) { /* no name given, use current af */
1860 ret
= -E_GET_AUDIO_FILE
;
1861 if (!(name
= get_current_audio_file()))
1863 ret
= update_atts(fd
, name
, atts
);
1867 for (; argv
[i
] && ret
>= 0; i
++)
1868 ret
= update_atts(fd
, argv
[i
], atts
);
1870 refresh_selector_info();
1879 int com_cam(int fd
, int argc
, char *argv
[])
1881 char *name
= NULL
, *meta
= NULL
, *atts
= NULL
;
1885 return -E_MYSQL_SYNTAX
;
1886 if (!(name
= escaped_basename(argv
[1])))
1889 if (!(atts
= get_atts(name
, 1)))
1892 if (!(meta
= get_meta(name
, 0)))
1894 for (i
= 2; i
< argc
; i
++) {
1897 if (!(ebn
= escaped_basename(argv
[i
])))
1899 ret
= send_va_buffer(fd
, "updating %s\n", ebn
);
1904 q
= make_message("update data set %s where name = '%s'",
1906 if ((ret
= update_atts(fd
, ebn
, atts
)) >= 0)
1907 ret
= real_query(q
);
1927 static int com_vrfy_clean(int fd
, int argc
, __a_unused
char *argv
[])
1930 int ret
, vrfy_mode
= strcmp(argv
[0], "clean");
1931 void *result
= NULL
;
1932 unsigned int num_rows
;
1937 return -E_MYSQL_SYNTAX
;
1939 result
= get_result("select data.name from data left join dir on "
1940 "dir.name = data.name where dir.name is NULL");
1943 num_rows
= mysql_num_rows(result
);
1945 ret
= send_buffer(fd
, "No invalid entries\n");
1949 send_va_buffer(fd
, "found %i invalid entr%s\n", num_rows
,
1950 num_rows
== 1? "y" : "ies");
1951 ret
= print_results(fd
, result
, 0, 0, num_rows
- 1, 0);
1954 while ((row
= mysql_fetch_row(result
))) {
1959 escaped_name
= escape_str(row
[0]);
1962 send_va_buffer(fd
, "deleting %s\n", escaped_name
);
1963 query
= make_message("delete from data where name = '%s'",
1965 ret
= real_query(query
);
1973 mysql_free_result(result
);
1980 int com_vrfy(int fd
, int argc
, char **argv
)
1982 return com_vrfy_clean(fd
, argc
, argv
);
1988 int com_clean(int fd
, int argc
, char **argv
)
1990 return com_vrfy_clean(fd
, argc
, argv
);
1993 static FILE *out_file
;
1995 static int mysql_write_tmp_file(const char *dir
, const char *name
)
1997 int ret
= -E_TMPFILE
;
1998 char *msg
= make_message("%s\t%s\n", dir
, name
);
1999 if (fputs(msg
, out_file
) != EOF
)
2008 int com_upd(int fd
, int argc
, __a_unused
char *argv
[])
2010 char *tempname
= NULL
, *query
= NULL
;
2011 int ret
, out_fd
= -1, num
= 0;
2012 void *result
= NULL
;
2013 unsigned int num_rows
;
2017 return -E_MYSQL_SYNTAX
;
2019 tempname
= para_strdup("/tmp/mysql.tmp.XXXXXX");
2020 ret
= para_mkstemp(tempname
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
2024 out_file
= fdopen(out_fd
, "w");
2029 if (find_audio_files(conf
.mysql_audio_file_dir_arg
, mysql_write_tmp_file
) < 0)
2031 num
= ftell(out_file
);
2033 * we have to make sure the file hit the disk before we call
2038 PARA_DEBUG_LOG("wrote tempfile %s (%d bytes)\n", tempname
, num
);
2041 if ((ret
= real_query("delete from dir")) < 0)
2043 query
= make_message("load data infile '%s' ignore into table dir "
2044 "fields terminated by '\t' lines terminated by '\n' "
2045 "(dir, name)", tempname
);
2046 ret
= real_query(query
);
2050 result
= get_result("select dir.name from dir left join data on "
2051 "data.name = dir.name where data.name is NULL");
2055 num_rows
= mysql_num_rows(result
);
2057 ret
= send_buffer(fd
, "no new entries\n");
2060 while ((row
= mysql_fetch_row(result
))) {
2065 send_va_buffer(fd
, "new entry: %s\n", row
[0]);
2066 erow
= escape_str(row
[0]);
2069 query
= make_message("insert into data (name, pic_id) values "
2070 "('%s','%s')", erow
, "1");
2072 ret
= real_query(query
);
2085 mysql_free_result(result
);
2089 static char **server_get_audio_file_list(unsigned int num
)
2091 char **list
= para_malloc((num
+ 1) * sizeof(char *));
2092 char *tmp
, *query
, *stream
= get_current_stream();
2093 void *result
= NULL
;
2094 unsigned int num_rows
;
2098 tmp
= get_query(stream
, NULL
, 1);
2102 query
= make_message("%s limit %d", tmp
, num
);
2104 result
= get_result(query
);
2108 num_rows
= mysql_num_rows(result
);
2111 for (i
= 0; i
< num_rows
&& i
< num
; i
++) {
2112 row
= mysql_fetch_row(result
);
2113 if (!row
|| !row
[0])
2115 list
[i
] = para_strdup(row
[0]);
2128 mysql_free_result(result
);
2133 * connect to mysql server, return mysql pointer on success, -E_NOTCONN
2134 * on errors. Called from parent on startup and also from com_cdb().
2136 static int init_mysql_server(void)
2138 char *u
= conf
.mysql_user_arg
? conf
.mysql_user_arg
: para_logname();
2140 mysql_ptr
= mysql_init(NULL
);
2142 PARA_CRIT_LOG("%s", "mysql init error\n");
2145 PARA_DEBUG_LOG("connecting: %s@%s:%d\n", u
, conf
.mysql_host_arg
,
2146 conf
.mysql_port_arg
);
2147 if (!conf
.mysql_user_arg
)
2150 * If host is NULL a connection to the local host is assumed,
2151 * If user is NULL, the current user is assumed
2153 if (!(mysql_ptr
= mysql_real_connect(mysql_ptr
,
2154 conf
.mysql_host_arg
,
2155 conf
.mysql_user_arg
,
2156 conf
.mysql_passwd_arg
,
2157 conf
.mysql_database_arg
,
2158 conf
.mysql_port_arg
, NULL
, 0))) {
2159 PARA_CRIT_LOG("%s", "connect error\n");
2162 PARA_INFO_LOG("%s", "success\n");
2166 /* mmd lock must be held */
2167 static void write_msg2mmd(int success
)
2169 sprintf(mmd
->selector_info
, "dbinfo1:%s\ndbinfo2:mysql-%s\ndbinfo3:\n",
2170 success
< 0? PARA_STRERROR(-success
) :
2171 "successfully connected to mysql server",
2172 success
< 0? "" : mysql_get_server_info(mysql_ptr
));
2175 /* create database */
2176 int com_cdb(int fd
, int argc
, char *argv
[])
2182 PARA_INFO_LOG("%s", "closing database\n");
2183 mysql_close(mysql_ptr
);
2185 /* dont use any database */
2186 conf
.mysql_database_arg
= NULL
; /* leak? */
2187 ret
= -E_MYSQL_INIT
;
2188 if (init_mysql_server() < 0 || !mysql_ptr
)
2191 conf
.mysql_database_arg
= para_strdup("paraslash");
2194 conf
.mysql_database_arg
= escape_str(argv
[1]);
2195 if (!conf
.mysql_database_arg
)
2198 query
= make_message("create database %s", conf
.mysql_database_arg
);
2199 ret
= real_query(query
);
2203 /* reconnect with database just created */
2204 mysql_close(mysql_ptr
);
2205 ret
= -E_MYSQL_INIT
;
2206 if (init_mysql_server() < 0 || !mysql_ptr
)
2212 if (real_query("create table data (name varchar(255) binary not null "
2214 "lastplayed datetime not null default "
2216 "numplayed int not null default 0, "
2217 "pic_id bigint unsigned not null default 1)") < 0)
2219 if (real_query("create table dir (name varchar(255) binary not null "
2220 "primary key, dir varchar(255) default null)") < 0)
2222 if (real_query("create table pics ("
2223 "id bigint(20) unsigned not null primary key "
2225 "name varchar(255) binary not null, "
2226 "pic mediumblob not null)") < 0)
2228 if (real_query("create table streams ("
2229 "name varchar(255) binary not null primary key, "
2230 "def blob not null)") < 0)
2232 if (real_query("insert into streams (name, def) values "
2233 "('current_stream', '(none)')") < 0)
2235 ret
= send_va_buffer(fd
, "successfully created database %s\n",
2236 conf
.mysql_database_arg
);
2241 static void shutdown_connection(void)
2244 PARA_NOTICE_LOG("%s", "shutting down mysql connection\n");
2245 mysql_close(mysql_ptr
);
2251 * the init function of the mysql-based audio file selector
2253 * \param db pointer to the struct to initialize
2255 * Check the command line options and initialize all function pointers of \a
2256 * db. Connect to the mysql server and initialize the info string.
2258 * \return This function returns success even if it could not connect
2259 * to the mysql server. This is because the connect is expected to fail
2260 * if there the paraslash database is not yet created. This gives the
2261 * user a chance to send the "cdb" to create the database.
2263 * \sa struct audio_file_selector, misc_meta_data::selector_info,
2266 int mysql_selector_init(struct audio_file_selector
*db
)
2270 if (!conf
.mysql_passwd_given
)
2271 return -E_NO_MYSQL_PASSWD
;
2272 if (!conf
.mysql_audio_file_dir_given
)
2273 return -E_NO_AF_DIR
;
2275 db
->cmd_list
= mysql_selector_cmds
;
2276 db
->get_audio_file_list
= server_get_audio_file_list
;
2277 db
->update_audio_file
= update_audio_file_server_handler
;
2278 db
->shutdown
= shutdown_connection
;
2279 ret
= init_mysql_server();
2281 PARA_WARNING_LOG("%s\n", PARA_STRERROR(-ret
));
2283 return 1; /* return success even if connect failed to give the
2284 * user the chance to exec com_cdb