X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=mysql_selector.c;h=98db17dc758762b1b16feb2a2cae3da4f38b8571;hb=69f0930489da9ace5af8aee104ad21d188aed387;hp=b51f4f079c82cf12994e9f91f0fb553769983932;hpb=e04fd7a21f2013ce0edd3140195cb3f11ce91faf;p=paraslash.git diff --git a/mysql_selector.c b/mysql_selector.c index b51f4f07..98db17dc 100644 --- a/mysql_selector.c +++ b/mysql_selector.c @@ -38,7 +38,6 @@ extern struct misc_meta_data *mmd; static void *mysql_ptr = NULL; - static int com_cam(int, int, char **); static int com_cdb(int, int, char **); static int com_cs(int, int, char **); @@ -600,7 +599,7 @@ static struct para_macro macro_list[] = { } }; -static int real_query(char *query) +static int real_query(const char *query) { if (!mysql_ptr) return -E_NOTCONN; @@ -617,7 +616,7 @@ static int real_query(char *query) * Use open connection given by mysql_ptr to query server. Returns a * result pointer on succes and NULL on errors */ -static struct MYSQL_RES *get_result(char *query) +static struct MYSQL_RES *get_result(const char *query) { void *result; @@ -699,7 +698,7 @@ static int com_na(__unused int fd, int argc, char *argv[]) char *q; int ret; - if (argc < 1) + if (argc < 2) return -E_MYSQL_SYNTAX; q = make_message("alter table data add %s char(1) " "not null default 0", argv[1]); @@ -716,7 +715,7 @@ static int com_da(__unused int fd, int argc, char *argv[]) char *q; int ret; - if (argc < 1) + if (argc < 2) return -E_MYSQL_SYNTAX; q = make_message("alter table data drop %s", argv[1]); ret = real_query(q); @@ -732,7 +731,7 @@ static int com_stradd_picadd(int fd, int argc, char *argv[]) int ret, stradd = strcmp(argv[0], "picadd"); size_t size; - if (argc < 1) + if (argc < 2) return -E_MYSQL_SYNTAX; if (strlen(argv[1]) >= MAXLINE - 1) return -E_NAMETOOLONG; @@ -808,9 +807,9 @@ static int com_verb(int fd, int argc, char *argv[]) int ret; unsigned int num_rows, num_fields; - if (argc < 1) + if (argc < 2) return -E_MYSQL_SYNTAX; - result = get_result(argv[1]); + result = get_result(argv[1]); if (!result) /* return success, because it's ok to have no results */ return 1; @@ -837,7 +836,7 @@ static void *get_all_attributes(void) mysql_free_result(result); return NULL; } - mysql_data_seek(result, 4); /* skip Lastplayed, Numplayed... */ + mysql_data_seek(result, 4); /* skip Lastplayed, Numplayed... */ return result; } @@ -849,7 +848,7 @@ static int com_laa(int fd, int argc, __unused char *argv[]) void *result; int ret; - if (argc) + if (argc != 1) return -E_MYSQL_SYNTAX; result = get_all_attributes(); if (!result) @@ -870,10 +869,10 @@ static int com_hist(int fd, int argc, char *argv[]) { q = make_message("select name, to_days(now()) - to_days(lastplayed) from " "data%s%s%s order by lastplayed", - (argc < 1)? "" : " where ", - (argc < 1)? "" : argv[1], - (argc < 1)? "" : " = '1'"); - result = get_result(q); + (argc < 2)? "" : " where ", + (argc < 2)? "" : argv[1], + (argc < 2)? "" : " = '1'"); + result = get_result(q); free(q); if (!result) return -E_NORESULT; @@ -894,7 +893,7 @@ static int com_last(int fd, int argc, char *argv[]) char *q; int num, ret; - if (argc < 1) + if (argc < 2) num = 10; else num = atoi(argv[1]); @@ -943,7 +942,7 @@ static int com_mbox(int fd, int argc, char *argv[]) "\n\n\n" "') from data" ); - if (argc >= 1) { + if (argc >= 2) { char *tmp = make_message("%s where name LIKE '%s'", query, argv[1]); free(query); @@ -951,7 +950,7 @@ static int com_mbox(int fd, int argc, char *argv[]) } mysql_free_result(result); ret = -E_NORESULT; - result = get_result(query); + result = get_result(query); if (!result) goto out; ret = -E_EMPTY_RESULT; @@ -998,7 +997,7 @@ static char *get_atts(char *name, int verbose) num_fields = mysql_num_fields(result); if (num_fields < 5) goto out; - mysql_data_seek(result2, 4); /* skip Lastplayed, Numplayed... */ + mysql_data_seek(result2, 4); /* skip Lastplayed, Numplayed... */ row = mysql_fetch_row(result); ret = -E_NOROW; if (!row) @@ -1010,11 +1009,11 @@ static char *get_atts(char *name, int verbose) goto out; if (atts && (verbose || is_set)) atts = para_strcat(atts, verbose? "," : " "); - if (is_set || verbose) + if (is_set || verbose) atts = para_strcat(atts, row2[0]); if (verbose) atts = para_strcat(atts, is_set? "=\"1\"" : "=\"0\""); - } + } ret = 1; out: if (result2) @@ -1048,7 +1047,7 @@ static char *get_meta(char *name, int verbose) goto out; q = make_message(verbose? verbose_fmt : fmt, ebn); free(ebn); - result = get_result(q); + result = get_result(q); free(q); if (!result) goto out; @@ -1187,7 +1186,7 @@ static char *get_query(char *streamname, char *filename, int with_path) free(tmp2); continue; } - if (!strcmp(command, "score:")) + if (!score && !strcmp(command, "score:")) score = s_a_r_list(macro_list, arg); } if (!score) { @@ -1207,10 +1206,10 @@ static char *get_query(char *streamname, char *filename, int with_path) goto write_query; } select_clause = para_strdup(with_path? - "select concat(dir.dir, '/', dir.name) from data, dir " - "where dir.name = data.name " - : - "select name from data where name is not NULL"); + "select concat(dir.dir, '/', dir.name) from data, dir " + "where dir.name = data.name " + : + "select name from data where name is not NULL"); order = make_message("order by -(%s)", score); free(score); if (accept_opts && deny_opts) { @@ -1307,7 +1306,7 @@ static int com_info(int fd, int argc, char *argv[]) char *name = NULL, *meta = NULL, *atts = NULL, *dir = NULL; int ret, com_la = strcmp(argv[0], "info"); - if (argc < 1) { + if (argc < 2) { ret = -E_GET_AUDIO_FILE; if (!(name = get_current_audio_file())) goto out; @@ -1338,7 +1337,7 @@ out: free(name); return ret; } -static int change_stream(char *stream) +static int change_stream(const char *stream) { char *query; int ret; @@ -1442,9 +1441,9 @@ static int com_rm_ne(__unused int fd, int argc, char *argv[]) { int ne = !strcmp(argv[0], "ne"); int i, ret; - if (argc < 1) + if (argc < 2) return -E_MYSQL_SYNTAX; - for (i = 1; i <= argc; i++) { + for (i = 1; i < argc; i++) { ret = remove_entry(argv[i]); if (ret < 0) return ret; @@ -1465,7 +1464,7 @@ static int com_mv(__unused int fd, int argc, char *argv[]) char *q, *dn, *ebn1 = NULL, *ebn2 = NULL, *edn = NULL; int ret; - if (argc != 2) + if (argc != 3) return -E_MYSQL_SYNTAX; ebn1 = escaped_basename(argv[1]); ebn2 = escaped_basename(argv[2]); @@ -1517,12 +1516,12 @@ static int com_set(__unused int fd, int argc, char *argv[]) char *q, *ebn; long unsigned id; int i, ret; - char *field = strcmp(argv[0], "picass")? "numplayed" : "pic_id"; + const char *field = strcmp(argv[0], "picass")? "numplayed" : "pic_id"; - if (argc < 2) + if (argc < 3) return -E_MYSQL_SYNTAX; id = atol(argv[1]); - for (i = 2; i <= argc; i++) { + for (i = 2; i < argc; i++) { ebn = escaped_basename(argv[i]); if (!ebn) return -E_ESCAPE; @@ -1546,7 +1545,7 @@ static int com_picch(__unused int fd, int argc, char *argv[]) long unsigned id; char *q; - if (argc != 2) + if (argc != 3) return -E_MYSQL_SYNTAX; id = atol(argv[1]); if (strlen(argv[2]) > MAXLINE) @@ -1567,7 +1566,7 @@ static int com_piclist(__unused int fd, int argc, __unused char *argv[]) unsigned long *length; int ret; - if (argc) + if (argc != 1) return -E_MYSQL_SYNTAX; result = get_result("select id,name,pic from pics order by id"); if (!result) @@ -1596,9 +1595,9 @@ static int com_picdel(int fd, int argc, char *argv[]) my_ulonglong aff; int i, ret; - if (argc < 1) + if (argc < 2) return -E_MYSQL_SYNTAX; - for (i = 1; i <= argc; i++) { + for (i = 1; i < argc; i++) { id = atol(argv[i]); q = make_message("delete from pics where id = %lu", id); ret = real_query(q); @@ -1629,7 +1628,7 @@ static int com_pic(int fd, int argc, char *argv[]) int ret; char *q, *name = NULL; - if (argc < 1) { + if (argc < 2) { ret = -E_GET_AUDIO_FILE; name = get_current_audio_file(); } else { @@ -1667,7 +1666,7 @@ static int com_strdel(__unused int fd, int argc, char *argv[]) char *tmp; int ret = -1; - if (argc < 1) + if (argc < 2) return -E_MYSQL_SYNTAX; tmp = make_message("delete from streams where name='%s'", argv[1]); ret = real_query(tmp); @@ -1691,12 +1690,12 @@ static int com_ls(int fd, int argc, char *argv[]) int ret; unsigned int num_rows; - if (argc > 0) + if (argc > 1) q = make_message("select name from data where name LIKE '%s'", argv[1]); else q = para_strdup("select name from data"); - result = get_result(q); + result = get_result(q); free(q); if (!result) return -E_NORESULT; @@ -1719,7 +1718,7 @@ static int com_summary(__unused int fd, int argc, __unused char *argv[]) const char *fmt = "select count(name) from data where %s='1'"; int ret = -E_NORESULT; - if (argc) + if (argc != 1) return -E_MYSQL_SYNTAX; result = get_all_attributes(); if (!result) @@ -1761,7 +1760,7 @@ static int get_numplayed(char *name) char *buf = make_message(fmt, name); int ret = -E_NORESULT; - result = get_result(buf); + result = get_result(buf); free(buf); if (!result) goto out; @@ -1822,7 +1821,7 @@ static void update_audio_file_server_handler(char *name) static int com_us(__unused int fd, int argc, char *argv[]) { - if (argc != 1) + if (argc != 2) return -E_MYSQL_SYNTAX; return update_audio_file(argv[1]); } @@ -1851,7 +1850,7 @@ static int com_ps(__unused int fd, int argc, char *argv[]) int match = -1, ret, i; unsigned int num_rows; - if (argc) + if (argc != 1) return -E_MYSQL_SYNTAX; ret = -E_NORESULT; if (!result) @@ -1880,7 +1879,7 @@ static int com_ps(__unused int fd, int argc, char *argv[]) else i = match < num_rows - 1? match + 1 : 0; ret = -E_NOROW; - mysql_data_seek(result, i); + mysql_data_seek(result, i); row = mysql_fetch_row(result); if (!row || !row[0]) goto out; @@ -1917,9 +1916,9 @@ static int com_streams(int fd, int argc, __unused char *argv[]) void *result; MYSQL_ROW row; - if (argc && strcmp(argv[1], "current_stream")) + if (argc > 1 && strcmp(argv[1], "current_stream")) return -E_MYSQL_SYNTAX; - if (argc) { + if (argc > 1) { char *cs = get_current_stream(); ret = send_va_buffer(fd, "%s\n", cs); free(cs); @@ -1955,7 +1954,7 @@ static int com_strq(int fd, int argc, char *argv[]) void *result; int ret; - if (argc < 1) { + if (argc < 2) { ret = -E_GET_STREAM; name = get_current_stream(); } else { @@ -1991,7 +1990,7 @@ static int com_cs(int fd, int argc, char *argv[]) char *old_stream = get_current_stream(); int csp = !strcmp(argv[0], "csp"); - if (!argc) { + if (argc == 1) { ret = -E_MYSQL_SYNTAX; if (csp) goto out; @@ -2035,12 +2034,12 @@ static int com_sl(int fd, int argc, char *argv[]) char *query, *stream, *tmp; unsigned int num_rows, num; - if (argc < 1) + if (argc < 2) return -E_MYSQL_SYNTAX; num = atoi(argv[1]); if (!num) return -E_MYSQL_SYNTAX; - stream = (argc == 1)? get_current_stream() : para_strdup(argv[2]); + stream = (argc == 2)? get_current_stream() : para_strdup(argv[2]); tmp = get_query(stream, NULL, 0); query = make_message("%s limit %d", tmp, num); free(tmp); @@ -2109,9 +2108,9 @@ static int com_sa(int fd, int argc, char *argv[]) int i, ret; char *atts = NULL, *name; - if (argc < 1) + if (argc < 2) return -E_MYSQL_SYNTAX; - for (i = 1; i <= argc; i++) { + for (i = 1; i < argc; i++) { int unset = 0; char *tmp, *p =argv[i]; int len = strlen(p); @@ -2137,7 +2136,7 @@ static int com_sa(int fd, int argc, char *argv[]) no_more_atts: if (!atts) return -E_NOATTS; - if (i > argc) { /* no name given, use current af */ + if (i >= argc) { /* no name given, use current af */ ret = -E_GET_AUDIO_FILE; if (!(name = get_current_audio_file())) goto out; @@ -2161,7 +2160,7 @@ static int com_cam(int fd, int argc, char *argv[]) char *name = NULL, *meta = NULL, *atts = NULL; int i, ret; - if (argc < 2) + if (argc < 3) return -E_MYSQL_SYNTAX; if (!(name = escaped_basename(argv[1]))) return -E_ESCAPE; @@ -2171,7 +2170,7 @@ static int com_cam(int fd, int argc, char *argv[]) ret = -E_META; if (!(meta = get_meta(name, 0))) goto out; - for (i = 2; i <= argc; i++) { + for (i = 2; i < argc; i++) { char *ebn, *q; ret = -E_ESCAPE; if (!(ebn = escaped_basename(argv[i]))) @@ -2213,7 +2212,7 @@ static int com_vrfy(int fd, int argc, __unused char *argv[]) MYSQL_ROW row; char *escaped_name; - if (argc) + if (argc != 1) return -E_MYSQL_SYNTAX; ret = -E_NORESULT; result = get_result("select data.name from data left join dir on " @@ -2278,7 +2277,7 @@ static int com_upd(int fd, int argc, __unused char *argv[]) unsigned int num_rows; MYSQL_ROW row; - if (argc) + if (argc != 1) return -E_MYSQL_SYNTAX; out_file = NULL; tempname = para_strdup("/tmp/mysql.tmp.XXXXXX"); @@ -2436,17 +2435,9 @@ static void write_msg2mmd(int success) /* create database */ static int com_cdb(int fd, int argc, char *argv[]) { - char *query, *name; + char *query; int ret; - if (argc < 1) - name = "paraslash"; - else { - ret = -E_NAMETOOLONG; - name = argv[1]; - if (strlen(name) > MAXLINE) - goto out; - } if (mysql_ptr) { PARA_INFO_LOG("%s", "closing database\n"); mysql_close(mysql_ptr); @@ -2456,14 +2447,15 @@ static int com_cdb(int fd, int argc, char *argv[]) ret = -E_MYSQL_INIT; if (init_mysql_server() < 0 || !mysql_ptr) goto out; - query = make_message("create database %s", name); + conf.mysql_database_arg = para_strdup((argc < 2)? + "paraslash" : argv[1]); + query = make_message("create database %s", conf.mysql_database_arg); ret = real_query(query); free(query); if (ret < 0) goto out; /* reconnect with database just created */ mysql_close(mysql_ptr); - conf.mysql_database_arg = para_strdup(name); ret = -E_MYSQL_INIT; if (init_mysql_server() < 0 || !mysql_ptr) goto out; @@ -2494,7 +2486,8 @@ static int com_cdb(int fd, int argc, char *argv[]) if (real_query("insert into streams (name, def) values " "('current_stream', '(none)')") < 0) goto out; - ret = send_va_buffer(fd, "successfully created database %s\n", name); + ret = send_va_buffer(fd, "successfully created database %s\n", + conf.mysql_database_arg); out: return ret; }