2 * Copyright (C) 1999-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file mysql_selector.c para_server's mysql-based audio file selector */
9 /** \cond some internal constants */
10 #define MEDIUM_BLOB_SIZE 16777220 /* (2**24 + 4) */
11 #define BLOB_SIZE 65539 /* (2**16 + 3) */
13 #include "server.cmdline.h"
17 #include <mysql/mysql.h>
18 #include <mysql/mysql_version.h>
23 #include "user_list.h"
24 #include "mysql_selector_command_list.h"
27 /** pointer to the shared memory area */
28 extern struct misc_meta_data
*mmd
;
30 static void *mysql_ptr
= NULL
;
31 static int mysql_lock
;
34 * contains name/replacement pairs used by s_a_r_list()
39 /** the name of the macro */
41 /** the replacement text */
42 const char *replacement
;
45 static const struct para_macro mysql_macro_list
[] = {
47 .replacement
= "(data.%s != '1')"
50 .replacement
= "(data.%s = '1')"
53 .replacement
= "%sdata.Pic_Id"
56 .replacement
= "(data.name like '%s')"
59 .replacement
= "%sFLOOR((UNIX_TIMESTAMP(now())"
60 "-UNIX_TIMESTAMP(data.Lastplayed))/60)"
63 .replacement
= "%sdata.Numplayed"
70 * Simple search and replace routine.
72 * \param src Source String.
73 * \param macro_name The name of the macro.
74 * \param replacement The replacement format string.
76 * In \p src, replace each occurence of \p macro_name(arg) by the string
77 * determined by the \p replacement format string. \p replacement may (but
78 * needs not) contain a single string conversion specifier (%s) which gets
81 * \return A string in which all matches in \p src are replaced, or \p NULL if
82 * an error was encountered. Caller must free the result.
86 __must_check __malloc
static char *s_a_r(const char *src
, const char* macro_name
,
87 const char *replacement
)
94 const char *bufptr
= src
;
96 if (!macro_name
|| !replacement
|| !src
)
97 return para_strdup(src
);
98 if (regcomp(&preg
, macro_name
, 0) != 0)
100 while (regexec(&preg
, bufptr
, nmatch
, pmatch
, eflags
)
102 char *tmp
, *arg
, *o_bracket
, *c_bracket
;
104 o_bracket
= strchr(bufptr
+ pmatch
[0].rm_so
, '(');
105 c_bracket
= o_bracket
? strchr(o_bracket
, ')') : NULL
;
108 tmp
= para_strdup(bufptr
);
109 tmp
[pmatch
[0].rm_so
] = '\0';
110 dest
= para_strcat(dest
, tmp
);
113 arg
= para_strdup(o_bracket
+ 1);
114 arg
[c_bracket
- o_bracket
- 1] = '\0';
115 tmp
= make_message(replacement
, arg
);
117 dest
= para_strcat(dest
, tmp
);
122 dest
= para_strcat(dest
, bufptr
);
123 // PARA_DEBUG_LOG("%s: returning %s\n", __func__, dest);
130 * replace a string according to a list of macros
132 * \param macro_list the array containing a macro/replacement pairs.
133 * \param src the source string
135 * This function just calls s_a_r() for each element of \p macro_list.
137 * \return \p NULL if one of the underlying calls to \p s_a_r returned \p NULL.
138 * Otherwise the completely expanded version of \p src is returned.
140 __must_check __malloc
static char *s_a_r_list(const struct para_macro
*macro_list
,
143 const struct para_macro
*mp
= macro_list
;
144 char *ret
= NULL
, *tmp
= para_strdup(src
);
147 ret
= s_a_r(tmp
, mp
->name
, mp
->replacement
);
149 if (!ret
) /* syntax error */
154 //PARA_DEBUG_LOG("%s: returning %s\n", __func__, dest);
158 static int lockless_real_query(const char *query
)
162 PARA_DEBUG_LOG("%s\n", query
);
163 if (mysql_real_query(mysql_ptr
, query
, strlen(query
))) {
164 PARA_ERROR_LOG("real_query error (%s)\n",
165 mysql_error(mysql_ptr
));
171 static int real_query(const char *query
)
175 mutex_lock(mysql_lock
);
176 ret
= lockless_real_query(query
);
177 mutex_unlock(mysql_lock
);
182 * Use open connection given by mysql_ptr to query server. Returns a
183 * result pointer on succes and NULL on errors
185 static struct MYSQL_RES
*get_result(const char *query
)
189 mutex_lock(mysql_lock
);
190 if (lockless_real_query(query
) < 0)
192 result
= mysql_store_result(mysql_ptr
);
194 PARA_ERROR_LOG("%s", "store_result error\n");
196 mutex_unlock(mysql_lock
);
200 * write input from fd to dynamically allocated char array,
201 * but maximal max_size byte. Return size.
203 static int fd2buf(int fd
, char **buf_ptr
, size_t max_size
)
205 const size_t chunk_size
= 1024;
207 char *buf
= para_malloc(size
* sizeof(char)), *p
= buf
;
210 while ((ret
= recv_bin_buffer(fd
, p
, chunk_size
)) > 0) {
212 if ((p
- buf
) + chunk_size
>= size
) {
216 if (size
> max_size
) {
220 tmp
= para_realloc(buf
, size
);
235 static char *escape_blob(const char* old
, size_t size
)
241 new = para_malloc(2 * size
* sizeof(char) + 1);
242 mysql_real_escape_string(mysql_ptr
, new, old
, size
);
246 static char *escape_str(const char* old
)
248 return escape_blob(old
, strlen(old
));
251 static char *escaped_basename(const char *name
)
253 char *esc
, *bn
= para_basename(name
);
257 esc
= escape_str(bn
);
265 int com_na(__a_unused
int fd
, int argc
, char *argv
[])
271 return -E_MYSQL_SYNTAX
;
272 tmp
= escape_str(argv
[1]);
275 q
= make_message("alter table data add %s char(1) "
276 "not null default 0", tmp
);
286 int com_da(__a_unused
int fd
, int argc
, char *argv
[])
292 return -E_MYSQL_SYNTAX
;
293 tmp
= escape_str(argv
[1]);
296 q
= make_message("alter table data drop %s", tmp
);
304 static int com_stradd_picadd(int fd
, int argc
, char *argv
[])
306 char *blob
= NULL
, *esc_blob
= NULL
, *q
= NULL
, *tmp
= NULL
;
307 const char *fmt
, *del_fmt
;
308 int ret
, stradd
= strcmp(argv
[0], "picadd");
312 return -E_MYSQL_SYNTAX
;
313 if (strlen(argv
[1]) >= MAXLINE
- 1)
314 return -E_NAMETOOLONG
;
319 fmt
= "insert into streams (name, def) values ('%s','%s')";
320 del_fmt
="delete from streams where name='%s'";
322 size
= MEDIUM_BLOB_SIZE
;
323 fmt
= "insert into pics (name, pic) values ('%s','%s')";
324 del_fmt
="delete from pics where pic='%s'";
326 tmp
= escape_str(argv
[1]);
329 q
= make_message(del_fmt
, tmp
);
335 if ((ret
= send_buffer(fd
, AWAITING_DATA_MSG
) < 0))
337 if ((ret
= fd2buf(fd
, &blob
, size
)) < 0)
343 esc_blob
= escape_blob(blob
, size
);
346 tmp
= escape_str(argv
[1]);
349 q
= make_message(fmt
, tmp
, esc_blob
);
360 int com_stradd(int fd
, int argc
, char *argv
[])
362 return com_stradd_picadd(fd
, argc
, argv
);
366 int com_picadd(int fd
, int argc
, char *argv
[])
368 return com_stradd_picadd(fd
, argc
, argv
);
372 * print results to fd
374 static int print_results(int fd
, void *result
,
375 my_ulonglong top
, my_ulonglong left
,
376 my_ulonglong bottom
, my_ulonglong right
)
382 for (i
= top
; i
<= bottom
; i
++) {
383 row
= mysql_fetch_row(result
);
386 for (j
= left
; j
<= right
; j
++) {
387 ret
= send_va_buffer(fd
, j
== left
? "%s" : "\t%s",
388 row
[j
]? row
[j
] : "NULL");
392 ret
= send_buffer(fd
, "\n");
402 int com_verb(int fd
, int argc
, char *argv
[])
406 my_ulonglong num_rows
, num_fields
, top
= 0, left
= 0;
410 return -E_MYSQL_SYNTAX
;
411 tmp
= escape_str(argv
[1]);
414 result
= get_result(tmp
);
417 /* return success, because it's ok to have no results */
419 num_fields
= mysql_field_count(mysql_ptr
);
420 num_rows
= mysql_num_rows(result
);
422 if (num_fields
&& num_rows
)
423 ret
= print_results(fd
, result
, top
, left
, num_rows
- 1,
425 mysql_free_result(result
);
429 /* returns NULL on errors or if there are no atts defined yet */
430 static void *get_all_attributes(void)
432 void *result
= get_result("desc data");
433 unsigned int num_rows
;
437 num_rows
= mysql_num_rows(result
);
439 mysql_free_result(result
);
442 mysql_data_seek(result
, (my_ulonglong
)4); /* skip Lastplayed, Numplayed... */
447 * list all attributes
449 int com_laa(int fd
, int argc
, __a_unused
char *argv
[])
453 my_ulonglong top
= 0, left
= 0, bottom
, right
= 0;
456 return -E_MYSQL_SYNTAX
;
457 result
= get_all_attributes();
460 bottom
= mysql_num_rows(result
);
462 return -E_MYSQL_SYNTAX
;
464 ret
= print_results(fd
, result
, top
, left
, bottom
, right
);
465 mysql_free_result(result
);
472 int com_hist(int fd
, int argc
, char *argv
[])
477 my_ulonglong num_rows
, top
= 0, left
= 0, right
= 1;
480 return -E_MYSQL_SYNTAX
;
482 char *tmp
= escape_str(argv
[1]);
485 atts
= make_message("where %s = '1'", tmp
);
488 atts
= para_strdup(NULL
);
490 q
= make_message("select name, to_days(now()) - to_days(lastplayed) from "
491 "data %s order by lastplayed", atts
);
493 result
= get_result(q
);
497 num_rows
= mysql_num_rows(result
);
500 ret
= print_results(fd
, result
, top
, left
, num_rows
- 1, right
);
501 mysql_free_result(result
);
506 * get last num audio files
508 int com_last(int fd
, int argc
, char *argv
[])
513 my_ulonglong top
= 0, left
= 0, right
= 0;
520 return -E_MYSQL_SYNTAX
;
521 q
= make_message("select name from data order by lastplayed desc "
523 result
= get_result(q
);
527 ret
= print_results(fd
, result
, top
, left
, mysql_num_rows(result
) - 1,
529 mysql_free_result(result
);
533 int com_mbox(int fd
, int argc
, char *argv
[])
538 my_ulonglong num_rows
, num_fields
, top
= 0, left
= 0;
539 char *query
= para_strdup("select concat('From foo@localhost ', "
540 "date_format(Lastplayed, '%a %b %e %T %Y'), "
541 "'\nReceived: from\nTo: bar\n");
544 result
= get_all_attributes();
548 while ((row
= mysql_fetch_row(result
))) {
553 tmp
= make_message("%sX-Attribute-%s: ', %s, '\n", query
,
558 query
= para_strcat(query
,
566 char *esc
= escape_str(argv
[1]), *tmp
;
570 tmp
= make_message("%s where name LIKE '%s'", query
, esc
);
575 mysql_free_result(result
);
577 result
= get_result(query
);
580 ret
= -E_EMPTY_RESULT
;
581 num_fields
= mysql_field_count(mysql_ptr
);
582 num_rows
= mysql_num_rows(result
);
583 if (!num_fields
|| !num_rows
)
585 ret
= print_results(fd
, result
, top
, left
, num_rows
- 1,
590 mysql_free_result(result
);
595 * get attributes by name. If verbose is not 0, this function returns a string
596 * of the form 'att1="0",att2="1"'... which is used in com_cam() for
597 * constructing a mysql update query. Otherwise the space-separated list of all
598 * attributes which are set in the audio file given by name is returned. Never
599 * returns NULL in *NON VERBOSE* mode.
601 static char *get_atts(char *name
, int verbose
)
603 char *atts
= NULL
, *buf
, *ebn
;
604 void *result
= NULL
, *result2
= NULL
;
607 my_ulonglong num_fields
, offset
= 4; /* skip Lastplayed, Numplayed... */
610 result2
= get_all_attributes();
613 ebn
= escaped_basename(name
);
616 buf
= make_message("select * from data where name='%s'", ebn
);
618 result
= get_result(buf
);
622 num_fields
= mysql_num_fields(result
);
625 mysql_data_seek(result2
, offset
);
626 row
= mysql_fetch_row(result
);
629 for (i
= 4; i
< num_fields
; i
++) {
630 int is_set
= row
[i
] && !strcmp(row
[i
], "1");
631 row2
= mysql_fetch_row(result2
);
632 if (!row2
|| !row2
[0])
634 if (atts
&& (verbose
|| is_set
))
635 atts
= para_strcat(atts
, verbose
? "," : " ");
636 if (is_set
|| verbose
)
637 atts
= para_strcat(atts
, row2
[0]);
639 atts
= para_strcat(atts
, is_set
? "=\"1\"" : "=\"0\"");
643 mysql_free_result(result2
);
645 mysql_free_result(result
);
646 if (!atts
&& !verbose
)
647 atts
= para_strdup("(none)");
651 /* never returns NULL in verbose mode */
652 static char *get_meta(char *name
, int verbose
)
656 char *ebn
, *q
, *ret
= NULL
;
657 const char *verbose_fmt
=
658 "select concat('lastplayed: ', "
659 "(to_days(now()) - to_days(lastplayed)),"
660 "' day(s). numplayed: ', numplayed, "
661 "', pic: ', pic_id) "
662 "from data where name = '%s'";
663 /* is that really needed? */
664 const char *fmt
= "select concat('lastplayed=\\'', lastplayed, "
665 "'\\', numplayed=\\'', numplayed, "
666 "'\\', pic_id=\\'', pic_id, '\\'') "
667 "from data where name = '%s'";
669 if (!(ebn
= escaped_basename(name
)))
671 q
= make_message(verbose
? verbose_fmt
: fmt
, ebn
);
673 result
= get_result(q
);
677 row
= mysql_fetch_row(result
);
680 ret
= para_strdup(row
[0]);
683 mysql_free_result(result
);
685 ret
= para_strdup("(not yet played)");
689 static char *get_dir(char *name
)
691 char *ret
= NULL
, *q
, *ebn
;
695 if (!(ebn
= escaped_basename(name
)))
697 q
= make_message("select dir from dir where name = '%s'", ebn
);
699 result
= get_result(q
);
703 row
= mysql_fetch_row(result
);
705 ret
= para_strdup(row
[0]);
706 mysql_free_result(result
);
710 /* never returns NULL */
711 static char *get_current_stream(void)
715 void *result
= get_result("select def from streams where "
716 "name = 'current_stream'");
720 row
= mysql_fetch_row(result
);
723 ret
= para_strdup(row
[0]);
724 mysql_free_result(result
);
728 mysql_free_result(result
);
729 return para_strdup("(none)");
733 * Read stream definition of stream streamname and construct mysql
734 * query. Return NULL on errors. If streamname is NULL, use current
735 * stream. If that is also NULL, use query that selects everything.
736 * If filename is NULL, query will list everything, otherwise only
737 * the score of given file.
739 static char *get_query(char *streamname
, char *filename
, int with_path
)
741 char *accept_opts
= NULL
, *deny_opts
= NULL
, *score
= NULL
;
742 char *where_clause
, *order
, *query
;
743 char command
[255] = ""; /* buffer for sscanf */
747 char *select_clause
= NULL
;
749 tmp
= get_current_stream();
751 tmp
= escape_str(streamname
);
755 if (!strcmp(tmp
, "(none)")) {
758 char *ret
, *ebn
= escaped_basename(filename
);
761 ret
= make_message("select to_days(now()) - "
762 "to_days(lastplayed) from data "
763 "where name = '%s'", ebn
);
769 "select concat(dir.dir, '/', dir.name) "
770 "from data, dir where dir.name = data.name "
771 "order by data.lastplayed"
774 "select name from data where name is not NULL "
775 "order by lastplayed"
779 query
= make_message("select def from streams where name = '%s'",
781 result
= get_result(query
);
786 row
= mysql_fetch_row(result
);
792 char *arg
, *line
= end
;
794 if (!(end
= strchr(line
, '\n')))
798 if (sscanf(line
, "%200s%n", command
, &n
) < 1)
801 if (!strcmp(command
, "accept:")) {
802 char *tmp2
= s_a_r_list(mysql_macro_list
, arg
);
804 accept_opts
= para_strcat(
805 accept_opts
, " or ");
806 accept_opts
= para_strcat(accept_opts
, tmp2
);
810 if (!strcmp(command
, "deny:")) {
811 char *tmp2
= s_a_r_list(mysql_macro_list
, arg
);
813 deny_opts
= para_strcat(deny_opts
, " or ");
814 deny_opts
= para_strcat(deny_opts
, tmp2
);
818 if (!score
&& !strcmp(command
, "score:"))
819 score
= s_a_r_list(mysql_macro_list
, arg
);
822 score
= s_a_r_list(mysql_macro_list
, conf
.mysql_default_score_arg
);
827 char *ebn
= escaped_basename(filename
);
830 select_clause
= make_message("select %s from data ", score
);
832 where_clause
= make_message( "where name = '%s' ", ebn
);
834 order
= para_strdup("");
837 select_clause
= para_strdup(with_path
?
838 "select concat(dir.dir, '/', dir.name) from data, dir "
839 "where dir.name = data.name "
841 "select name from data where name is not NULL");
842 order
= make_message("order by -(%s)", score
);
844 if (accept_opts
&& deny_opts
) {
845 where_clause
= make_message("and ((%s) and not (%s)) ",
846 accept_opts
, deny_opts
);
849 if (accept_opts
&& !deny_opts
) {
850 where_clause
= make_message("and (%s) ", accept_opts
);
853 if (!accept_opts
&& deny_opts
) {
854 where_clause
= make_message("and not (%s) ", deny_opts
);
857 where_clause
= para_strdup("");
859 query
= make_message("%s %s %s", select_clause
, where_clause
, order
);
869 mysql_free_result(result
);
876 * This is called from server and from some commands. Name must not be NULL
877 * Never returns NULL.
879 static char *get_selector_info(char *name
)
881 char *meta
, *atts
, *info
, *dir
, *query
, *stream
;
883 MYSQL_ROW row
= NULL
;
886 return para_strdup("(none)");
887 stream
= get_current_stream();
888 meta
= get_meta(name
, 1);
889 atts
= get_atts(name
, 0);
892 query
= get_query(stream
, name
, 0); /* FIXME: pass stream == NULL instead? */
895 result
= get_result(query
);
898 row
= mysql_fetch_row(result
);
900 info
= make_message("dbinfo1:dir: %s\n"
901 "dbinfo2:stream: %s, %s, score: %s\n"
903 dir
? dir
: "(not contained in table)",
905 (result
&& row
&& row
[0])? row
[0] : "(no score)",
912 mysql_free_result(result
);
916 /* might return NULL */
917 static char *get_current_audio_file(void)
921 name
= para_basename(mmd
->filename
);
926 /* If called as child, mmd_lock must be held */
927 static void update_mmd(char *info
)
929 PARA_DEBUG_LOG("%s", "updating shared memory area\n");
930 strncpy(mmd
->selector_info
, info
, MMD_INFO_SIZE
- 1);
931 mmd
->selector_info
[MMD_INFO_SIZE
- 1] = '\0';
934 static void refresh_selector_info(void)
936 char *name
= get_current_audio_file();
941 info
= get_selector_info(name
);
949 /* list attributes / print database info */
950 static int com_la_info(int fd
, int argc
, char *argv
[])
952 char *name
= NULL
, *meta
= NULL
, *atts
= NULL
, *dir
= NULL
;
953 int ret
, la
= strcmp(argv
[0], "info");
956 ret
= -E_GET_AUDIO_FILE
;
957 if (!(name
= get_current_audio_file()))
959 ret
= send_va_buffer(fd
, "%s\n", name
);
964 if (!(name
= escaped_basename(argv
[1])))
967 meta
= get_meta(name
, 1);
968 atts
= get_atts(name
, 0);
971 ret
= send_va_buffer(fd
, "%s\n", atts
);
973 ret
= send_va_buffer(fd
, "dir: %s\n" "%s\n" "attributes: %s\n",
974 dir
? dir
: "(not contained in table)", meta
, atts
);
983 /* list attributes */
984 int com_la(int fd
, int argc
, char *argv
[])
986 return com_la_info(fd
, argc
, argv
);
989 /* print database info */
990 int com_info(int fd
, int argc
, char *argv
[])
992 return com_la_info(fd
, argc
, argv
);
995 static int change_stream(const char *stream
)
999 query
= make_message("update streams set def='%s' "
1000 "where name = 'current_stream'", stream
);
1001 ret
= real_query(query
);
1006 static int get_pic_id_by_name(char *name
)
1009 void *result
= NULL
;
1013 if (!(ebn
= escaped_basename(name
)))
1015 q
= make_message("select pic_id from data where name = '%s'", ebn
);
1017 result
= get_result(q
);
1021 row
= mysql_fetch_row(result
);
1025 mysql_free_result(result
);
1029 static int remove_entry(const char *name
)
1031 char *q
, *ebn
= escaped_basename(name
);
1032 int ret
= -E_ESCAPE
;
1036 q
= make_message("delete from data where name = '%s'", ebn
);
1037 real_query(q
); /* ignore errors */
1039 q
= make_message("delete from dir where name = '%s'", ebn
);
1040 real_query(q
); /* ignore errors */
1048 static int add_entry(const char *name
)
1050 char *q
, *dn
, *ebn
= NULL
, *edn
= NULL
;
1053 if (!name
|| !*name
)
1054 return -E_MYSQL_SYNTAX
;
1055 ebn
= escaped_basename(name
);
1058 ret
= -E_MYSQL_SYNTAX
;
1059 dn
= para_dirname(name
);
1063 edn
= escape_str(dn
);
1067 q
= make_message("insert into data (name, pic_id) values "
1068 "('%s', '%s')", ebn
, "1");
1069 ret
= real_query(q
);
1070 // ret = 1; PARA_DEBUG_LOG("q: %s\n", q);
1074 q
= make_message("insert into dir (name, dir) values "
1075 "('%s', '%s')", ebn
, edn
);
1076 // ret = 1; PARA_DEBUG_LOG("q: %s\n", q);
1077 ret
= real_query(q
);
1088 * remove/add entries
1090 static int com_rm_ne(__a_unused
int fd
, int argc
, char *argv
[])
1092 int ne
= !strcmp(argv
[0], "ne");
1095 return -E_MYSQL_SYNTAX
;
1096 for (i
= 1; i
< argc
; i
++) {
1097 ret
= remove_entry(argv
[i
]);
1102 ret
= add_entry(argv
[i
]);
1112 int com_rm(int fd
, int argc
, char *argv
[])
1114 return com_rm_ne(fd
, argc
, argv
);
1120 int com_ne(int fd
, int argc
, char *argv
[])
1122 return com_ne(fd
, argc
, argv
);
1128 int com_mv(__a_unused
int fd
, int argc
, char *argv
[])
1130 char *q
, *dn
, *ebn1
= NULL
, *ebn2
= NULL
, *edn
= NULL
;
1134 return -E_MYSQL_SYNTAX
;
1136 ebn1
= escaped_basename(argv
[1]);
1137 ebn2
= escaped_basename(argv
[2]);
1138 if (!ebn1
|| !ebn2
|| !*ebn1
|| !*ebn2
)
1140 ret
= -E_MYSQL_SYNTAX
;
1141 if (!strcmp(ebn1
, ebn2
))
1143 remove_entry(argv
[2]); /* no need to escape, ignore error */
1144 q
= make_message("update data set name = '%s' where name = '%s'",
1146 ret
= real_query(q
);
1150 ret
= -E_AUDIO_FILE
;
1151 if (!mysql_affected_rows(mysql_ptr
))
1153 q
= make_message("update dir set name = '%s' where name = '%s'",
1155 ret
= real_query(q
);
1161 dn
= para_dirname(argv
[2]);
1165 edn
= escape_str(dn
);
1172 q
= make_message("update dir set dir = '%s' where name = '%s'",
1174 ret
= real_query(q
);
1186 static int com_set(__a_unused
int fd
, int argc
, char *argv
[])
1191 const char *field
= strcmp(argv
[0], "picass")? "numplayed" : "pic_id";
1194 return -E_MYSQL_SYNTAX
;
1196 for (i
= 2; i
< argc
; i
++) {
1197 ebn
= escaped_basename(argv
[i
]);
1200 q
= make_message("update data set %s = %lu "
1201 "where name = '%s'", field
, id
, ebn
);
1203 ret
= real_query(q
);
1212 * snp: set numplayed
1214 int com_picass(int fd
, int argc
, char *argv
[])
1216 return com_set(fd
, argc
, argv
);
1220 * snp: set numplayed
1222 int com_snp(int fd
, int argc
, char *argv
[])
1224 int ret
= com_set(fd
, argc
, argv
);
1226 refresh_selector_info();
1231 * picch: change entry's name in pics table
1233 int com_picch(__a_unused
int fd
, int argc
, char *argv
[])
1240 return -E_MYSQL_SYNTAX
;
1243 tmp
= escape_str(argv
[2]);
1246 q
= make_message("update pics set name = '%s' where id = %lu", tmp
, id
);
1248 ret
= real_query(q
);
1254 * piclist: print list of pics in db
1256 int com_piclist(__a_unused
int fd
, int argc
, __a_unused
char *argv
[])
1258 void *result
= NULL
;
1260 unsigned long *length
;
1264 return -E_MYSQL_SYNTAX
;
1265 result
= get_result("select id,name,pic from pics order by id");
1268 while ((row
= mysql_fetch_row(result
))) {
1269 length
= mysql_fetch_lengths(result
);
1270 if (!row
|| !row
[0] || !row
[1] || !row
[2])
1272 ret
= send_va_buffer(fd
, "%s\t%lu\t%s\n", row
[0], length
[2], row
[1]);
1278 mysql_free_result(result
);
1283 * picdel: delete picture from database
1285 int com_picdel(int fd
, int argc
, char *argv
[])
1293 return -E_MYSQL_SYNTAX
;
1294 for (i
= 1; i
< argc
; i
++) {
1296 q
= make_message("delete from pics where id = %lu", id
);
1297 ret
= real_query(q
);
1301 aff
= mysql_affected_rows(mysql_ptr
);
1303 ret
= send_va_buffer(fd
, "No such id: %lu\n", id
);
1308 q
= make_message("update data set pic_id = 1 where pic_id = %lu", id
);
1309 ret
= real_query(q
);
1315 * pic: get picture by name or by number
1317 int com_pic(int fd
, int argc
, char *argv
[])
1319 void *result
= NULL
;
1321 unsigned long *length
, id
;
1323 char *q
, *name
= NULL
;
1326 ret
= -E_GET_AUDIO_FILE
;
1327 name
= get_current_audio_file();
1330 name
= escaped_basename(argv
[1]);
1335 id
= atoi(name
+ 1);
1337 id
= get_pic_id_by_name(name
);
1341 q
= make_message("select pic from pics where id = '%lu'", id
);
1342 result
= get_result(q
);
1346 row
= mysql_fetch_row(result
);
1348 if (!row
|| !row
[0])
1350 length
= mysql_fetch_lengths(result
);
1351 ret
= send_bin_buffer(fd
, row
[0], *length
);
1353 mysql_free_result(result
);
1358 int com_strdel(__a_unused
int fd
, int argc
, char *argv
[])
1364 return -E_MYSQL_SYNTAX
;
1365 tmp
= escape_str(argv
[1]);
1368 q
= make_message("delete from streams where name='%s'", tmp
);
1370 ret
= real_query(q
);
1378 int com_ls(int fd
, int argc
, char *argv
[])
1383 my_ulonglong num_rows
, top
= 0, left
= 0, right
= 0;
1386 return -E_MYSQL_SYNTAX
;
1388 char *tmp
= escape_str(argv
[1]);
1391 q
= make_message("select name from data where name like '%s'",
1395 q
= para_strdup("select name from data");
1396 result
= get_result(q
);
1400 num_rows
= mysql_num_rows(result
);
1403 ret
= print_results(fd
, result
, top
, left
, num_rows
- 1, right
);
1404 mysql_free_result(result
);
1411 int com_summary(__a_unused
int fd
, int argc
, __a_unused
char *argv
[])
1416 void *result2
= NULL
;
1417 const char *fmt
= "select count(name) from data where %s='1'";
1418 int ret
= -E_NORESULT
;
1421 return -E_MYSQL_SYNTAX
;
1422 result
= get_all_attributes();
1425 while ((row
= mysql_fetch_row(result
))) {
1432 buf
= make_message(fmt
, row
[0]);
1433 result2
= get_result(buf
);
1438 row2
= mysql_fetch_row(result2
);
1439 if (!row2
|| !row2
[0])
1441 ret
= send_va_buffer(fd
, "%s\t%s\n", row
[0], row2
[0]);
1448 mysql_free_result(result2
);
1450 mysql_free_result(result
);
1454 static int get_numplayed(char *name
)
1458 const char *fmt
= "select numplayed from data where name = '%s'";
1459 char *buf
= make_message(fmt
, name
);
1460 int ret
= -E_NORESULT
;
1462 result
= get_result(buf
);
1467 row
= mysql_fetch_row(result
);
1468 if (!row
|| !row
[0])
1473 mysql_free_result(result
);
1477 static int update_audio_file(char *name
)
1480 const char *fmt1
= "update data set lastplayed = now() where name = '%s'";
1481 const char *fmt2
= "update data set numplayed = %i where name = '%s'";
1483 char *ebn
= escaped_basename(name
);
1488 q
= make_message(fmt1
, ebn
);
1489 ret
= real_query(q
);
1493 ret
= get_numplayed(ebn
);
1496 q
= make_message(fmt2
, ret
+ 1, ebn
);
1497 ret
= real_query(q
);
1504 static void update_audio_file_server_handler(char *name
)
1507 info
= get_selector_info(name
);
1510 update_audio_file(name
);
1513 int com_us(__a_unused
int fd
, int argc
, char *argv
[])
1519 return -E_MYSQL_SYNTAX
;
1520 tmp
= escape_str(argv
[1]);
1523 ret
= update_audio_file(argv
[1]);
1526 refresh_selector_info();
1530 /* select previous / next stream */
1531 static int com_ps_ns(__a_unused
int fd
, int argc
, char *argv
[])
1533 char *query
, *stream
= get_current_stream();
1534 void *result
= get_result("select name from streams");
1537 my_ulonglong num_rows
, match
, i
;
1539 ret
= -E_MYSQL_SYNTAX
;
1545 num_rows
= mysql_num_rows(result
);
1546 ret
= -E_EMPTY_RESULT
;
1550 for (i
= 0; i
< num_rows
; i
++) {
1551 row
= mysql_fetch_row(result
);
1552 if (!row
|| !row
[0])
1554 if (!strcmp(row
[0], "current_stream"))
1556 if (!strcmp(row
[0], stream
))
1563 if (!strcmp(argv
[0], "ps"))
1564 i
= match
> 0? match
- 1 : num_rows
- 1;
1566 i
= match
< num_rows
- 1? match
+ 1 : 0;
1568 mysql_data_seek(result
, i
);
1569 row
= mysql_fetch_row(result
);
1570 if (!row
|| !row
[0])
1572 if (!strcmp(row
[0], "current_stream")) {
1573 if (!strcmp(argv
[0], "ps")) {
1574 i
= match
< 2? match
+ num_rows
- 2 : match
- 2;
1577 i
= i
> num_rows
- 1? i
- num_rows
: i
;
1579 mysql_data_seek(result
, i
);
1580 row
= mysql_fetch_row(result
);
1581 if (!row
|| !row
[0])
1584 query
= make_message("update streams set def='%s' where name = "
1585 "'current_stream'", row
[0]);
1586 ret
= real_query(query
);
1588 refresh_selector_info();
1592 mysql_free_result(result
);
1596 /* select previous stream */
1597 int com_ps(int fd
, int argc
, char *argv
[])
1599 return com_ps_ns(fd
, argc
, argv
);
1602 /* select next stream */
1603 int com_ns(int fd
, int argc
, char *argv
[])
1605 return com_ps_ns(fd
, argc
, argv
);
1609 int com_streams(int fd
, int argc
, __a_unused
char *argv
[])
1611 unsigned int num_rows
;
1612 int i
, ret
= -E_NORESULT
;
1616 if (argc
> 1 && strcmp(argv
[1], "current_stream"))
1617 return -E_MYSQL_SYNTAX
;
1619 char *cs
= get_current_stream();
1620 ret
= send_va_buffer(fd
, "%s\n", cs
);
1624 result
= get_result("select name from streams");
1627 num_rows
= mysql_num_rows(result
);
1632 for (i
= 0; i
< num_rows
; i
++) {
1633 row
= mysql_fetch_row(result
);
1634 if (!row
|| !row
[0])
1636 if (strcmp(row
[0], "current_stream"))
1637 send_va_buffer(fd
, "%s\n", row
[0]);
1642 mysql_free_result(result
);
1646 /* query stream definition */
1647 int com_strq(int fd
, int argc
, char *argv
[])
1655 ret
= -E_GET_STREAM
;
1656 name
= get_current_stream();
1659 name
= escaped_basename(argv
[1]);
1664 query
= make_message("select def from streams where name='%s'", name
);
1666 result
= get_result(query
);
1671 row
= mysql_fetch_row(result
);
1672 if (!row
|| !row
[0])
1674 /* no '\n' needed */
1675 ret
= send_buffer(fd
, row
[0]);
1678 mysql_free_result(result
);
1682 /* change stream / change stream and play */
1683 static int com_cs_csp(int fd
, int argc
, char *argv
[])
1685 int ret
, stream_change
;
1686 char *query
, *stream
= NULL
;
1687 char *old_stream
= get_current_stream();
1688 int csp
= !strcmp(argv
[0], "csp");
1690 ret
= -E_MYSQL_SYNTAX
;
1696 ret
= send_va_buffer(fd
, "%s\n", old_stream
);
1700 /* test if stream is valid, no need to escape argv[1] */
1701 query
= get_query(argv
[1], NULL
, 0);
1706 stream
= escape_str(argv
[1]);
1709 stream_change
= strcmp(stream
, old_stream
);
1710 if (stream_change
) {
1711 ret
= change_stream(stream
);
1714 refresh_selector_info();
1718 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
1720 mmd
->new_vss_status_flags
|= VSS_NEXT
;
1731 int com_cs(int fd
, int argc
, char *argv
[])
1733 return com_cs_csp(fd
, argc
, argv
);
1736 /* change stream and play */
1737 int com_csp(int fd
, int argc
, char *argv
[])
1739 return com_cs_csp(fd
, argc
, argv
);
1742 /* score list / skip */
1743 static int com_sl_skip(int fd
, int argc
, char *argv
[])
1745 void *result
= NULL
;
1747 int ret
, i
, skip
= !strcmp(argv
[0], "skip");
1748 char *query
, *stream
, *tmp
;
1749 unsigned int num_rows
, num
;
1752 return -E_MYSQL_SYNTAX
;
1753 num
= atoi(argv
[1]);
1755 return -E_MYSQL_SYNTAX
;
1757 stream
= get_current_stream();
1759 return -E_GET_STREAM
;
1761 stream
= escape_str(argv
[2]);
1765 tmp
= get_query(stream
, NULL
, 0);
1768 return -E_GET_QUERY
;
1769 query
= make_message("%s limit %d", tmp
, num
);
1772 result
= get_result(query
);
1776 ret
= -E_EMPTY_RESULT
;
1777 num_rows
= mysql_num_rows(result
);
1780 for (i
= 0; i
< num_rows
&& i
< num
; i
++) {
1781 row
= mysql_fetch_row(result
);
1783 send_va_buffer(fd
, "Skipping %s\n", row
[0]);
1784 update_audio_file(row
[0]);
1786 send_va_buffer(fd
, "%s\n", row
[0]? row
[0]: "BUG");
1791 mysql_free_result(result
);
1796 int com_sl(int fd
, int argc
, char *argv
[])
1798 return com_sl_skip(fd
, argc
, argv
);
1802 int com_skip(int fd
, int argc
, char *argv
[])
1804 return com_sl_skip(fd
, argc
, argv
);
1808 * update attributes of name
1810 static int update_atts(int fd
, char *name
, char *atts
)
1813 char *ebn
, *q
, *old
, *new = NULL
;
1817 ebn
= escaped_basename(name
);
1820 q
= make_message("update data set %s where name = '%s'", atts
, ebn
);
1821 old
= get_atts(ebn
, 0);
1822 send_va_buffer(fd
, "old: %s\n", old
);
1824 ret
= real_query(q
);
1828 new = get_atts(ebn
, 0);
1829 ret
= send_va_buffer(fd
, "new: %s\n", new);
1839 int com_sa(int fd
, int argc
, char *argv
[])
1842 char *atts
= NULL
, *name
;
1845 return -E_MYSQL_SYNTAX
;
1846 for (i
= 1; i
< argc
; i
++) {
1848 char *esc
, *tmp
, *p
=argv
[i
];
1849 int len
= strlen(p
);
1853 switch (p
[len
- 1]) {
1864 esc
= escape_str(p
);
1867 tmp
= make_message("%s%s='%s'", atts
? "," : "", esc
,
1870 atts
= para_strcat(atts
, tmp
);
1876 if (i
>= argc
) { /* no name given, use current af */
1877 ret
= -E_GET_AUDIO_FILE
;
1878 if (!(name
= get_current_audio_file()))
1880 ret
= update_atts(fd
, name
, atts
);
1884 for (; argv
[i
] && ret
>= 0; i
++)
1885 ret
= update_atts(fd
, argv
[i
], atts
);
1887 refresh_selector_info();
1896 int com_cam(int fd
, int argc
, char *argv
[])
1898 char *name
= NULL
, *meta
= NULL
, *atts
= NULL
;
1902 return -E_MYSQL_SYNTAX
;
1903 if (!(name
= escaped_basename(argv
[1])))
1906 if (!(atts
= get_atts(name
, 1)))
1909 if (!(meta
= get_meta(name
, 0)))
1911 for (i
= 2; i
< argc
; i
++) {
1914 if (!(ebn
= escaped_basename(argv
[i
])))
1916 ret
= send_va_buffer(fd
, "updating %s\n", ebn
);
1921 q
= make_message("update data set %s where name = '%s'",
1923 if ((ret
= update_atts(fd
, ebn
, atts
)) >= 0)
1924 ret
= real_query(q
);
1944 static int com_vrfy_clean(int fd
, int argc
, __a_unused
char *argv
[])
1947 int ret
, vrfy_mode
= strcmp(argv
[0], "clean");
1948 void *result
= NULL
;
1951 my_ulonglong num_rows
, top
= 0, left
= 0, right
= 0;
1954 return -E_MYSQL_SYNTAX
;
1956 result
= get_result("select data.name from data left join dir on "
1957 "dir.name = data.name where dir.name is NULL");
1960 num_rows
= mysql_num_rows(result
);
1962 ret
= send_buffer(fd
, "No invalid entries\n");
1966 send_va_buffer(fd
, "found %lli invalid entr%s\n", num_rows
,
1967 num_rows
== 1? "y" : "ies");
1968 ret
= print_results(fd
, result
, top
, left
, num_rows
- 1, right
);
1971 while ((row
= mysql_fetch_row(result
))) {
1976 escaped_name
= escape_str(row
[0]);
1979 send_va_buffer(fd
, "deleting %s\n", escaped_name
);
1980 query
= make_message("delete from data where name = '%s'",
1982 ret
= real_query(query
);
1990 mysql_free_result(result
);
1997 int com_vrfy(int fd
, int argc
, char **argv
)
1999 return com_vrfy_clean(fd
, argc
, argv
);
2005 int com_clean(int fd
, int argc
, char **argv
)
2007 return com_vrfy_clean(fd
, argc
, argv
);
2010 static FILE *out_file
;
2012 static int mysql_write_tmp_file(const char *dir
, const char *name
)
2014 int ret
= -E_TMPFILE
;
2015 char *msg
= make_message("%s\t%s\n", dir
, name
);
2016 if (fputs(msg
, out_file
) != EOF
)
2025 int com_upd(int fd
, int argc
, __a_unused
char *argv
[])
2027 char *tempname
= NULL
, *query
= NULL
;
2028 int ret
, out_fd
= -1, num
= 0;
2029 void *result
= NULL
;
2030 unsigned int num_rows
;
2034 return -E_MYSQL_SYNTAX
;
2036 tempname
= para_strdup("/tmp/mysql.tmp.XXXXXX");
2037 ret
= para_mkstemp(tempname
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
2041 out_file
= fdopen(out_fd
, "w");
2046 if (find_audio_files(conf
.mysql_audio_file_dir_arg
, mysql_write_tmp_file
) < 0)
2048 num
= ftell(out_file
);
2050 * we have to make sure the file hit the disk before we call
2055 PARA_DEBUG_LOG("wrote tempfile %s (%d bytes)\n", tempname
, num
);
2058 if ((ret
= real_query("delete from dir")) < 0)
2060 query
= make_message("load data infile '%s' ignore into table dir "
2061 "fields terminated by '\t' lines terminated by '\n' "
2062 "(dir, name)", tempname
);
2063 ret
= real_query(query
);
2067 result
= get_result("select dir.name from dir left join data on "
2068 "data.name = dir.name where data.name is NULL");
2072 num_rows
= mysql_num_rows(result
);
2074 ret
= send_buffer(fd
, "no new entries\n");
2077 while ((row
= mysql_fetch_row(result
))) {
2082 send_va_buffer(fd
, "new entry: %s\n", row
[0]);
2083 erow
= escape_str(row
[0]);
2086 query
= make_message("insert into data (name, pic_id) values "
2087 "('%s','%s')", erow
, "1");
2089 ret
= real_query(query
);
2102 mysql_free_result(result
);
2106 static char **server_get_audio_file_list(unsigned int num
)
2108 char **list
= para_malloc((num
+ 1) * sizeof(char *));
2109 char *tmp
, *query
, *stream
= get_current_stream();
2110 void *result
= NULL
;
2111 unsigned int num_rows
;
2115 tmp
= get_query(stream
, NULL
, 1);
2119 query
= make_message("%s limit %d", tmp
, num
);
2121 result
= get_result(query
);
2125 num_rows
= mysql_num_rows(result
);
2128 for (i
= 0; i
< num_rows
&& i
< num
; i
++) {
2129 row
= mysql_fetch_row(result
);
2130 if (!row
|| !row
[0])
2132 list
[i
] = para_strdup(row
[0]);
2145 mysql_free_result(result
);
2150 * connect to mysql server, return mysql pointer on success, -E_NOTCONN
2151 * on errors. Called from parent on startup and also from com_cdb().
2153 static int init_mysql_server(void)
2155 char *u
= conf
.mysql_user_arg
? conf
.mysql_user_arg
: para_logname();
2158 mysql_ptr
= mysql_init(NULL
);
2160 PARA_CRIT_LOG("%s", "mysql init error\n");
2163 if (conf
.mysql_port_arg
< 0)
2164 return -E_MYSQL_SYNTAX
;
2165 port
= conf
.mysql_port_arg
;
2166 PARA_DEBUG_LOG("connecting: %s@%s:%d\n", u
, conf
.mysql_host_arg
, port
);
2167 if (!conf
.mysql_user_arg
)
2170 * If host is NULL a connection to the local host is assumed,
2171 * If user is NULL, the current user is assumed
2173 if (!(mysql_ptr
= mysql_real_connect(mysql_ptr
,
2174 conf
.mysql_host_arg
,
2175 conf
.mysql_user_arg
,
2176 conf
.mysql_passwd_arg
,
2177 conf
.mysql_database_arg
,
2179 PARA_CRIT_LOG("%s", "connect error\n");
2182 PARA_INFO_LOG("%s", "success\n");
2186 /* mmd lock must be held */
2187 static void write_msg2mmd(int success
)
2189 sprintf(mmd
->selector_info
, "dbinfo1:%s\ndbinfo2:mysql-%s\ndbinfo3:\n",
2190 success
< 0? PARA_STRERROR(-success
) :
2191 "successfully connected to mysql server",
2192 success
< 0? "" : mysql_get_server_info(mysql_ptr
));
2195 /* create database */
2196 int com_cdb(int fd
, int argc
, char *argv
[])
2202 PARA_INFO_LOG("%s", "closing database\n");
2203 mysql_close(mysql_ptr
);
2205 /* dont use any database */
2206 conf
.mysql_database_arg
= NULL
; /* leak? */
2207 ret
= -E_MYSQL_INIT
;
2208 if (init_mysql_server() < 0 || !mysql_ptr
)
2211 conf
.mysql_database_arg
= para_strdup("paraslash");
2214 conf
.mysql_database_arg
= escape_str(argv
[1]);
2215 if (!conf
.mysql_database_arg
)
2218 query
= make_message("create database %s", conf
.mysql_database_arg
);
2219 ret
= real_query(query
);
2223 /* reconnect with database just created */
2224 mysql_close(mysql_ptr
);
2225 ret
= -E_MYSQL_INIT
;
2226 if (init_mysql_server() < 0 || !mysql_ptr
)
2232 if (real_query("create table data (name varchar(255) binary not null "
2234 "lastplayed datetime not null default "
2236 "numplayed int not null default 0, "
2237 "pic_id bigint unsigned not null default 1)") < 0)
2239 if (real_query("create table dir (name varchar(255) binary not null "
2240 "primary key, dir varchar(255) default null)") < 0)
2242 if (real_query("create table pics ("
2243 "id bigint(20) unsigned not null primary key "
2245 "name varchar(255) binary not null, "
2246 "pic mediumblob not null)") < 0)
2248 if (real_query("create table streams ("
2249 "name varchar(255) binary not null primary key, "
2250 "def blob not null)") < 0)
2252 if (real_query("insert into streams (name, def) values "
2253 "('current_stream', '(none)')") < 0)
2255 ret
= send_va_buffer(fd
, "successfully created database %s\n",
2256 conf
.mysql_database_arg
);
2261 static void shutdown_connection(void)
2266 PARA_NOTICE_LOG("%s", "shutting down mysql connection\n");
2267 mysql_close(mysql_ptr
);
2271 ret
= mutex_destroy(mysql_lock
);
2273 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
2279 * the init function of the mysql-based audio file selector
2281 * \param db pointer to the struct to initialize
2283 * Check the command line options and initialize all function pointers of \a
2284 * db. Connect to the mysql server and initialize the info string.
2286 * \return This function returns success even if it could not connect
2287 * to the mysql server. This is because the connect is expected to fail
2288 * if there the paraslash database is not yet created. This gives the
2289 * user a chance to send the "cdb" to create the database.
2291 * \sa struct audio_file_selector, misc_meta_data::selector_info,
2294 int mysql_selector_init(struct audio_file_selector
*db
)
2298 if (!conf
.mysql_passwd_given
)
2299 return -E_NO_MYSQL_PASSWD
;
2300 if (!conf
.mysql_audio_file_dir_given
)
2301 return -E_NO_AF_DIR
;
2303 db
->cmd_list
= mysql_selector_cmds
;
2304 db
->get_audio_file_list
= server_get_audio_file_list
;
2305 db
->update_audio_file
= update_audio_file_server_handler
;
2306 db
->shutdown
= shutdown_connection
;
2311 ret
= init_mysql_server();
2313 PARA_WARNING_LOG("%s\n", PARA_STRERROR(-ret
));
2315 return 1; /* return success even if connect failed to give the
2316 * user the chance to exec com_cdb