the new plm database tool
[paraslash.git] / plm_dbtool.c
1 /*
2  * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 /** \file plm_dbtool.c Simple playlist manager for paraslash  */
20
21 #include <sys/time.h> /* gettimeofday */
22 #include "server.cmdline.h"
23 #include "server.h"
24 #include "db.h"
25 #include "error.h"
26 #include "net.h"
27 #include "string.h"
28
29 #define MAX_PLAYLIST_LEN 10000
30 #define MAX_PLAYLIST_BYTES (1024 * 1024)
31
32 static unsigned playlist_len, playlist_size, current_playlist_entry;
33 static char **playlist;
34
35 static int com_ppl(int, int, char **);
36 static int com_lpl(int, int, char **);
37 extern struct misc_meta_data *mmd;
38
39 /* array of commands that are supported by this database tool */
40 static struct server_command cmds[] = {
41 {
42 .name = "ppl",
43 .handler = com_ppl,
44 .perms = DB_READ,
45 .description = "print playlist",
46 .synopsis = "ppl",
47 .help =
48 "Print out the current playlist"
49 }, {
50 .name = "lpl",
51 .handler = com_lpl,
52 .perms = DB_WRITE,
53 .description = "load playlist",
54 .synopsis = "lpl",
55 .help =
56 "Read a new playlist from stdin"
57
58 }, {
59 .name = NULL,
60 }
61 };
62
63 static void playlist_add(char *path)
64 {
65         if (playlist_len >= playlist_size) {
66                 if (playlist_size >= MAX_PLAYLIST_LEN)
67                         return;
68                 playlist_size *= 2;
69                 playlist = para_realloc(playlist, playlist_size * sizeof(char *));
70         }
71         PARA_DEBUG_LOG("adding #%d: %s\n", playlist_len, path);
72         playlist[playlist_len] = para_strdup(path);
73         playlist_len++;
74 }
75
76 static int com_lpl(int fd, __unused int argc, __unused char *argv[])
77 {
78         unsigned i, loaded = 0;
79         char buf[_POSIX_PATH_MAX];
80         ssize_t ret;
81
82         PARA_DEBUG_LOG("freeing playlist (%d entries)\n", playlist_len);
83         for (i = 0; i < playlist_len; i++)
84                 free(playlist[i]);
85         current_playlist_entry = 0;
86         playlist_len = 0;
87         ret = send_buffer(fd, AWAITING_DATA_MSG);
88         if (ret < 0)
89                 return ret;
90 again:
91         ret = recv_bin_buffer(fd, buf + loaded, sizeof(buf) - loaded);
92         if (ret < 0)
93                 goto err_out;
94         if (!ret) {
95                 PARA_DEBUG_LOG("loaded playlist (%d entries)\n", playlist_len);
96                 return playlist_len;
97         }
98         loaded += ret;
99         loaded = for_each_line(buf, loaded, &playlist_add, 0);
100         if (loaded >= sizeof(buf))
101                 goto err_out;
102         goto again;
103 err_out:
104         return -E_LOAD_PLAYLIST;
105 }
106
107 static int com_ppl(int fd, __unused int argc, __unused char *argv[])
108 {
109         unsigned i;
110
111         PARA_DEBUG_LOG("sending playlist (%d entries)\n", playlist_len);
112         for (i = 0; i < playlist_len; i++) {
113                 int ret = send_buffer(fd, playlist[i]);
114                 if (ret < 0)
115                         return ret;
116         }
117         return 1;
118 }
119
120 static char **plm_get_audio_file_list(unsigned int num)
121 {
122         char **file_list;
123         unsigned i;
124
125         return NULL;
126         num = MIN(num, playlist_len);
127         if (!num)
128                 return NULL;
129         file_list = para_malloc((num + 1) * sizeof(char *));
130         for (i = 0; i < num; i++) {
131                 unsigned j = (current_playlist_entry + i) % playlist_len;
132                 file_list[i] = para_strdup(playlist[j]);
133         }
134         file_list[i] = NULL;
135         return file_list;
136 }
137
138 static void plm_shutdown(void)
139 {
140         /* free the playlist */
141 }
142
143 /**
144  *  the init function for the plm database tool
145  *
146  * Init all function pointers of \a db
147  *
148  * \sa struct dbtool, misc_meta_data::dbinfo, mysql.c random_dbtool.c
149  */
150 int plm_dbtool_init(struct dbtool *db)
151 {
152         playlist = para_calloc(100 * sizeof(char *)); /* guess 100 is enough */
153         playlist_size = 100;
154         sprintf(mmd->dbinfo, "plm initialized");
155         db->cmd_list = cmds;
156         db->get_audio_file_list = plm_get_audio_file_list;
157         db->shutdown = plm_shutdown;
158         return 1;
159 }