aft.c: Fix a nasty memory corruption bug in afs_ls on 64 bit archs.
[paraslash.git] / random_selector.c
1 /*
2  * Copyright (C) 2004-2007 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file random_selector.c An audio file selector which chooses files by random */
8
9 #include <sys/time.h> /* gettimeofday */
10 #include "para.h"
11 #include "server.cmdline.h"
12 #include "afh.h"
13 #include "server.h"
14 #include "afs_common.h"
15 #include "error.h"
16 #include "net.h"
17 #include "string.h"
18 #include "random_selector_command_list.h"
19
20 extern struct misc_meta_data *mmd;
21
22 static unsigned int num_audio_files, audio_file_count;
23 static char **audio_file_list;
24
25 static int count_audio_files(__a_unused const char *dir, __a_unused const char *name)
26 {
27         num_audio_files++;
28         return 1;
29 }
30
31 static int remember_file(const char *dir, const char *name)
32 {
33         if (audio_file_count < num_audio_files) {
34                 audio_file_list[audio_file_count] =
35                         make_message("%s/%s", dir, name);
36                 audio_file_count++;
37         }
38         return 1;
39 }
40
41 int com_random_info(int fd, __a_unused int argc, __a_unused char * const * const argv)
42 {
43         return send_buffer(fd, "Don't use for huge directories as it is "
44                 "very inefficient in this case.\n");
45 }
46
47 /*
48  * Load a list of all audio files into memory and chose num of them randomly.
49  * Called by server to determine next audio file to be streamed.
50  */
51 static char **random_get_audio_file_list(unsigned int num)
52 {
53         int i, ret;
54         unsigned int len;
55         char **ret_list = NULL; /* what we are going to return */
56
57         audio_file_list = NULL;
58         num_audio_files = 0;
59         /* first run, just count all audio files. dopey */
60         ret = find_audio_files(conf.random_dir_arg, count_audio_files);
61         if (ret < 0)
62                 goto out;
63         ret = -E_NOTHING_FOUND;
64         if (!num_audio_files)
65                 goto out;
66         /* yeah, that doesn't scale, also dopey */
67         audio_file_list = para_malloc(num_audio_files * sizeof(char *));
68         audio_file_count = 0;
69         /* second run (hot dentry cache, hopefully), fill audio_file_list */
70         ret = find_audio_files(conf.random_dir_arg, remember_file);
71         if (ret < 0)
72                 goto out;
73         /* careful, files might got deleted underneath */
74         num_audio_files = audio_file_count; /* can only decrease */
75         len = PARA_MIN(num, num_audio_files);
76         ret = -E_NOTHING_FOUND;
77         if (!len) /* nothing found, return NULL */
78                 goto out;
79         /* success, return NULL-terminated list */
80         ret_list = para_calloc((len + 1) * sizeof(char *));
81         for (i = 0; i < len; i++) { /* choose randomly */
82                 int r = (int) ((num_audio_files + 0.0) * (rand()
83                         / (RAND_MAX + 1.0)));
84                 ret_list[i] = para_strdup(audio_file_list[r]);
85         }
86         ret = 1;
87 out:
88         if (audio_file_list) {
89                 for (i = 0; i < num_audio_files; i++)
90                         free(audio_file_list[i]);
91                 free(audio_file_list);
92         }
93         if (ret > 0) {
94         } else
95                 sprintf(mmd->selector_info, "dbinfo1:%s\n", PARA_STRERROR(-ret));
96         return ret_list;
97 }
98
99 static void random_update_audio_file(char *audio_file)
100 {
101         char *dn = para_dirname(audio_file);
102         snprintf(mmd->selector_info, MMD_INFO_SIZE - 1,
103                 "dbinfo1:current dir: %s\n"
104                 "dbinfo2:random_dir: %s\n"
105                 "dbinfo3:%d files available\n",
106                 dn, conf.random_dir_arg, num_audio_files);
107         free(dn);
108         mmd->selector_info[MMD_INFO_SIZE - 1] = '\0';
109 }
110 static void random_shutdown(void)
111 {
112 }
113
114 /**
115  * the init function for the random audio file selector
116  *
117  * \param s pointer ro the struct to iniitalize
118  *
119  * Init all function pointers of \a s, init the info text and seed the PRNG.
120  *
121  * \sa struct audio_file_selector, misc_meta_data::selector_info, mysql.c
122  */
123 int random_selector_init(struct audio_file_selector *s)
124 {
125         struct timeval now;
126         unsigned int seed;
127
128         PARA_INFO_LOG("%s", "registering random handlers ;)\n");
129         gettimeofday(&now, NULL);
130         seed = now.tv_usec;
131         srand(seed);
132         s->cmd_list = random_selector_cmds;
133         s->get_audio_file_list = random_get_audio_file_list;
134         s->shutdown = random_shutdown;
135         s->update_audio_file = random_update_audio_file;
136         snprintf(mmd->selector_info, MMD_INFO_SIZE - 1,
137                 "dbinfo1: Welcome to the random selector\n"
138                 "dbinfo2: random_dir: %s\n", conf.random_dir_arg);
139         mmd->selector_info[MMD_INFO_SIZE - 1] = '\0';
140         return 1;
141 }