2 * Copyright (C) 2004-2006 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 random_dbtool.c Simple database tool implementation. Feel free to modify. */
21 #include <sys/time.h> /* gettimeofday */
22 #include "server.cmdline.h"
29 static int com_random_info(int, int, char **);
30 extern struct gengetopt_args_info conf;
31 extern struct misc_meta_data *mmd;
33 static unsigned int num_audio_files, audio_file_count;
34 static char **audio_file_list;
36 static int count_audio_files(__unused const char *dir, __unused const char *name)
42 static int remember_file(const char *dir, const char *name)
44 if (audio_file_count >= num_audio_files)
46 audio_file_list[audio_file_count] = make_message("%s/%s", dir, name);
51 /* array of commands that are supported by this database tool */
52 static struct server_command cmds[] = {
54 .name = "random_info",
55 .handler = com_random_info,
57 .description = "about the random database tool",
58 .synopsis = "random_info",
61 "Select a random file under the given directory"
67 static int com_random_info(int fd, __unused int argc, __unused char *argv[])
69 return send_buffer(fd, "Don't use for huge directories as it is "
70 "very inefficient in this case.\n");
74 * Load a list of all audio files into memory and chose num of them randomly.
75 * Called by server to determine next audio file to be streamed.
77 static char **random_get_audio_file_list(unsigned int num)
81 char **ret_list = NULL; /* what we are going to return */
83 audio_file_list = NULL;
85 /* first run, just count all audio files. dopey */
86 ret = find_audio_files(conf.random_dbtool_dir_arg, count_audio_files);
89 ret = -E_NOTHING_FOUND;
92 /* yeah, that doesn't scale, also dopey */
93 audio_file_list = para_malloc(num_audio_files * sizeof(char *));
95 /* second run (hot dentry cache, hopefully), fill audio_file_list */
96 ret = find_audio_files(conf.random_dbtool_dir_arg, remember_file);
99 /* careful, files might got deleted underneath */
100 num_audio_files = audio_file_count; /* can only decrease */
101 len = MIN(num, num_audio_files);
102 ret = -E_NOTHING_FOUND;
103 if (!len) /* nothing found, return NULL */
105 /* success, return NULL-terminated list */
106 ret_list = para_calloc((len + 1) * sizeof(char *));
107 for (i = 0; i < len; i++) { /* choose randomly */
108 int r = (int) ((num_audio_files + 0.0) * (rand()
109 / (RAND_MAX + 1.0)));
110 ret_list[i] = para_strdup(audio_file_list[r]);
113 if (audio_file_list) {
114 for (i = 0; i < num_audio_files; i++)
115 free(audio_file_list[i]);
116 free(audio_file_list);
119 // PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
123 static void random_shutdown(void)
125 PARA_DEBUG_LOG("%s", "thanks for using another dbtool.\n");
128 /** random's (constant) database info text */
129 #define DBINFO "dbinfo1:database info? You're kidding. I'm still dopey!\ndbinfo2:\ndbinfo3:\n"
132 * the init function for the random database tool
134 * Init all function pointers of \a db, init the dbinfo text and seed the
137 * \sa struct dbtool, misc_meta_data::dbinfo, mysql.c
139 int random_dbtool_init(struct dbtool *db)
143 PARA_INFO_LOG("%s", "registering random handlers ;)\n");
144 sprintf(mmd->dbinfo, DBINFO);
145 gettimeofday(&now, NULL);
148 db->get_audio_file_list = random_get_audio_file_list;
149 db->shutdown = random_shutdown;