chiark / gitweb /
Low level playlist database operations: read, createwrite, list, delete.
[disorder] / lib / trackdb-playlists.c
CommitLineData
2563dc1f
RK
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20/** @file lib/trackdb-playlists.c
21 * @brief Track database playlist support
22 *
23 * This file implements reading and modification of playlists, including access
24 * control, but not locking or event logging (at least yet).
25 */
26#include "common.h"
27
28#include <errno.h>
29
30#include "trackdb-int.h"
31#include "mem.h"
32#include "log.h"
33#include "configuration.h"
34#include "vector.h"
35
36static int trackdb_playlist_get_tid(const char *name,
37 const char *who,
38 char ***tracksp,
39 int *ntracksp,
40 char **sharep,
41 DB_TXN *tid);
42static int trackdb_playlist_set_tid(const char *name,
43 const char *who,
44 char **tracks,
45 int ntracks,
46 const char *share,
47 DB_TXN *tid);
48static int trackdb_playlist_list_tid(const char *who,
49 char ***playlistsp,
50 int *nplaylistsp,
51 DB_TXN *tid);
52static int trackdb_playlist_delete_tid(const char *name,
53 const char *who,
54 DB_TXN *tid);
55
56/** @brief Parse a playlist name
57 * @param name Playlist name
58 * @param ownerp Where to put owner, or NULL
59 * @param sharep Where to put default sharing, or NULL
60 * @return 0 on success, -1 on error
61 *
62 * Playlists take the form USER.PLAYLIST or just PLAYLIST. The PLAYLIST part
63 * is alphanumeric and nonempty. USER is a username (see valid_username()).
64 */
65int playlist_parse_name(const char *name,
66 char **ownerp,
67 char **sharep) {
68 const char *dot = strchr(name, '.'), *share;
69 char *owner;
70
71 if(dot) {
72 /* Owned playlist */
73 owner = xstrndup(name, dot - name);
74 if(!valid_username(owner))
75 return -1;
76 if(!valid_username(dot + 1))
77 return -1;
78 share = "private";
79 } else {
80 /* Shared playlist */
81 if(!valid_username(name))
82 return -1;
83 owner = 0;
84 share = "public";
85 }
86 if(ownerp)
87 *ownerp = owner;
88 if(sharep)
89 *sharep = xstrdup(share);
90 return 0;
91}
92
93/** @brief Check read access rights
94 * @param name Playlist name
95 * @param who Who wants to read
96 * @param share Playlist share status
97 */
98static int playlist_may_read(const char *name,
99 const char *who,
100 const char *share) {
101 char *owner;
102
103 if(!playlist_parse_name(name, &owner, 0))
104 return 0;
105 /* Anyone can read shared playlists */
106 if(!owner)
107 return 1;
108 /* You can always read playlists you own */
109 if(!strcmp(owner, who))
110 return 1;
111 /* You can read public playlists */
112 if(!strcmp(share, "public"))
113 return 1;
114 /* Anything else is prohibited */
115 return 0;
116}
117
118/** @brief Check modify access rights
119 * @param name Playlist name
120 * @param who Who wants to modify
121 * @param share Playlist share status
122 */
123static int playlist_may_write(const char *name,
124 const char *who,
125 const char attribute((unused)) *share) {
126 char *owner;
127
128 if(!playlist_parse_name(name, &owner, 0))
129 return 0;
130 /* Anyone can modify shared playlists */
131 if(!owner)
132 return 1;
133 /* You can always modify playlists you own */
134 if(!strcmp(owner, who))
135 return 1;
136 /* Anything else is prohibited */
137 return 0;
138}
139
140/** @brief Get playlist data
141 * @param name Name of playlist
142 * @param who Who wants to know
143 * @param tracksp Where to put list of tracks, or NULL
144 * @param ntracksp Where to put count of tracks, or NULL
145 * @param sharep Where to put sharing type, or NULL
146 * @return 0 on success, non-0 on error
147 *
148 * Possible return values:
149 * - @c 0 on success
150 * - @c DB_NOTFOUND if the playlist doesn't exist
151 * - @c EINVAL if the playlist name is invalid
152 * - @c EACCES if the playlist cannot be read by @p who
153 */
154int trackdb_playlist_get(const char *name,
155 const char *who,
156 char ***tracksp,
157 int *ntracksp,
158 char **sharep) {
159 int e;
160
161 if(!playlist_parse_name(name, 0, 0)) {
162 error(0, "invalid playlist name '%s'", name);
163 return EINVAL;
164 }
165 WITH_TRANSACTION(trackdb_playlist_get_tid(name, who,
166 tracksp, ntracksp, sharep,
167 tid));
168 return e;
169}
170
171static int trackdb_playlist_get_tid(const char *name,
172 const char *who,
173 char ***tracksp,
174 int *ntracksp,
175 char **sharep,
176 DB_TXN *tid) {
177 struct kvp *k;
178 int e, ntracks;
179 const char *s;
180
181 if((e = trackdb_getdata(trackdb_playlistsdb, name, &k, tid)))
182 return e;
183 /* Get sharability */
184 if(!(s = kvp_get(k, "sharing"))) {
185 error(0, "playlist '%s' has no 'sharing' key", name);
186 s = "private";
187 }
188 /* Check the read is allowed */
189 if(!playlist_may_read(name, who, s))
190 return EACCES;
191 /* Return sharability */
192 if(sharep)
193 *sharep = xstrdup(s);
194 /* Get track count */
195 if(!(s = kvp_get(k, "count"))) {
196 error(0, "playlist '%s' has no 'count' key", name);
197 s = "0";
198 }
199 ntracks = atoi(s);
200 if(ntracks < 0) {
201 error(0, "playlist '%s' has negative count", name);
202 ntracks = 0;
203 }
204 /* Return track count */
205 if(ntracksp)
206 *ntracksp = ntracks;
207 if(tracksp) {
208 /* Get track list */
209 char **tracks = xcalloc(ntracks + 1, sizeof (char *));
210 char b[16];
211
212 for(int n = 0; n < ntracks; ++n) {
213 snprintf(b, sizeof b, "%d", n);
214 if(!(s = kvp_get(k, b))) {
215 error(0, "playlist '%s' lacks track %d", name, n);
216 s = "unknown";
217 }
218 tracks[n] = xstrdup(s);
219 }
220 tracks[ntracks] = 0;
221 /* Return track list */
222 *tracksp = tracks;
223 }
224 return 0;
225}
226
227/** @brief Modify or create a playlist
228 * @param name Playlist name
229 * @param tracks List of tracks to set, or NULL to leave alone
230 * @param ntracks Length of @p tracks
231 * @param share Sharing status, or NULL to leave alone
232 * @return 0 on success, non-0 on error
233 *
234 * If the playlist exists it is just modified.
235 *
236 * If the playlist does not exist it is created. The default set of tracks is
237 * none, and the default sharing is private (if it is an owned one) or shared
238 * (otherwise).
239 *
240 * Possible return values:
241 * - @c 0 on success
242 * - @c EINVAL if the playlist name is invalid
243 * - @c EACCES if the playlist cannot be modified by @p who
244 */
245int trackdb_playlist_set(const char *name,
246 const char *who,
247 char **tracks,
248 int ntracks,
249 const char *share) {
250 int e;
251 char *owner;
252
253 if(!playlist_parse_name(name, &owner, 0)) {
254 error(0, "invalid playlist name '%s'", name);
255 return EINVAL;
256 }
257 /* Check valid share types */
258 if(share) {
259 if(owner) {
260 /* Playlists with an owner must be public or private */
261 if(strcmp(share, "public")
262 && strcmp(share, "private")) {
263 error(0, "playlist '%s' must be public or private", name);
264 return EINVAL;
265 }
266 } else {
267 /* Playlists with no owner must be shared */
268 if(strcmp(share, "shared")) {
269 error(0, "playlist '%s' must be shared", name);
270 return EINVAL;
271 }
272 }
273 }
274 /* We've checked as much as we can for now, now go and attempt the change */
275 WITH_TRANSACTION(trackdb_playlist_set_tid(name, who, tracks, ntracks, share,
276 tid));
277 return e;
278}
279
280static int trackdb_playlist_set_tid(const char *name,
281 const char *who,
282 char **tracks,
283 int ntracks,
284 const char *share,
285 DB_TXN *tid) {
286 struct kvp *k;
287 int e;
288 const char *s;
289
290 if((e = trackdb_getdata(trackdb_playlistsdb, name, &k, tid))
291 && e != DB_NOTFOUND)
292 return e;
293 /* If the playlist doesn't exist set some defaults */
294 if(e == DB_NOTFOUND) {
295 char *defshare, *owner;
296
297 if(playlist_parse_name(name, &owner, &defshare))
298 return EINVAL;
299 /* Can't create a non-shared playlist belonging to someone else. In fact
300 * this should be picked up by playlist_may_write() below but it's clearer
301 * to do it here. */
302 if(owner && strcmp(owner, who))
303 return EACCES;
304 k = 0;
305 kvp_set(&k, "count", 0);
306 kvp_set(&k, "sharing", defshare);
307 }
308 /* Check that the modification is allowed */
309 if(!(s = kvp_get(k, "sharing"))) {
310 error(0, "playlist '%s' has no 'sharing' key", name);
311 s = "private";
312 }
313 if(!playlist_may_write(name, who, s))
314 return EACCES;
315 /* Set the new values */
316 if(share)
317 kvp_set(&k, "share", share);
318 if(tracks) {
319 char b[16];
320 int oldcount, n;
321
322 /* Sanity check track count */
323 if(ntracks < 0 || ntracks > config->playlist_max) {
324 error(0, "invalid track count %d", ntracks);
325 return EINVAL;
326 }
327 /* Set the tracks */
328 for(n = 0; n < ntracks; ++n) {
329 snprintf(b, sizeof b, "%d", n);
330 kvp_set(&k, b, tracks[n]);
331 }
332 /* Get the old track count */
333 if((s = kvp_get(k, "count")))
334 oldcount = atoi(s);
335 else
336 oldcount = 0;
337 /* Delete old slots */
338 for(; n < oldcount; ++n) {
339 snprintf(b, sizeof b, "%d", n);
340 kvp_set(&k, b, NULL);
341 }
342 /* Set the new count */
343 snprintf(b, sizeof b, "%d", ntracks);
344 kvp_set(&k, "count", b);
345 }
346 /* Store the resulting record */
347 return trackdb_putdata(trackdb_playlistsdb, name, k, tid, 0);
348}
349
350/** @brief Get a list of playlists
351 * @param who Who wants to know
352 * @param playlistsp Where to put list of playlists
353 * @param nplaylistsp Where to put count of playlists, or NULL
354 */
355void trackdb_playlist_list(const char *who,
356 char ***playlistsp,
357 int *nplaylistsp) {
358 int e;
359
360 WITH_TRANSACTION(trackdb_playlist_list_tid(who, playlistsp, nplaylistsp,
361 tid));
362}
363
364static int trackdb_playlist_list_tid(const char *who,
365 char ***playlistsp,
366 int *nplaylistsp,
367 DB_TXN *tid) {
368 struct vector v[1];
369 DBC *c;
370 DBT k[1], d[1];
371 int e;
372
373 vector_init(v);
374 c = trackdb_opencursor(trackdb_playlistsdb, tid);
375 memset(k, 0, sizeof k);
376 while(!(e = c->c_get(c, k, prepare_data(d), DB_NEXT))) {
377 char *name = xstrndup(k->data, k->size), *owner;
378 const char *share = kvp_get(kvp_urldecode(d->data, d->size),
379 "share");
380
381 /* Extract owner; malformed names are skipped */
382 if(playlist_parse_name(name, &owner, 0)) {
383 error(0, "invalid playlist name '%s' found in database", name);
384 continue;
385 }
386 /* Always list public and shared playlists
387 * Only list private ones to their owner
388 * Don't list anything else
389 */
390 if(!strcmp(share, "public")
391 || !strcmp(share, "shared")
392 || (!strcmp(share, "private")
393 && owner && !strcmp(owner, who)))
394 vector_append(v, name);
395 }
396 switch(e) {
397 case DB_NOTFOUND:
398 break;
399 case DB_LOCK_DEADLOCK:
400 return e;
401 default:
402 fatal(0, "c->c_get: %s", db_strerror(e));
403 }
404 vector_terminate(v);
405 if(playlistsp)
406 *playlistsp = v->vec;
407 if(nplaylistsp)
408 *nplaylistsp = v->nvec;
409 return 0;
410}
411
412/** @brief Delete a playlist
413 * @param name Playlist name
414 * @param who Who is deleting it
415 * @return 0 on success, non-0 on error
416 *
417 * Possible return values:
418 * - @c 0 on success
419 * - @c EINVAL if the playlist name is invalid
420 * - @c EACCES if the playlist cannot be modified by @p who
421 * - @c DB_NOTFOUND if the playlist doesn't exist
422 */
423int trackdb_playlist_delete(const char *name,
424 const char *who) {
425 int e;
426 char *owner;
427
428 if(!playlist_parse_name(name, &owner, 0)) {
429 error(0, "invalid playlist name '%s'", name);
430 return EINVAL;
431 }
432 /* We've checked as much as we can for now, now go and attempt the change */
433 WITH_TRANSACTION(trackdb_playlist_delete_tid(name, who, tid));
434 return e;
435}
436
437static int trackdb_playlist_delete_tid(const char *name,
438 const char *who,
439 DB_TXN *tid) {
440 struct kvp *k;
441 int e;
442 const char *s;
443
444 if((e = trackdb_getdata(trackdb_playlistsdb, name, &k, tid)))
445 return e;
446 /* Check that modification is allowed */
447 if(!(s = kvp_get(k, "sharing"))) {
448 error(0, "playlist '%s' has no 'sharing' key", name);
449 s = "private";
450 }
451 if(!playlist_may_write(name, who, s))
452 return EACCES;
453 /* Delete the playlist */
454 return trackdb_delkey(trackdb_playlistsdb, name, tid);
455}
456
457/*
458Local Variables:
459c-basic-offset:2
460comment-column:40
461fill-column:79
462indent-tabs-mode:nil
463End:
464*/