2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
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.
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.
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
20 /** @file lib/trackdb-playlists.c
21 * @brief Track database playlist support
23 * This file implements reading and modification of playlists, including access
24 * control, but not locking or event logging (at least yet).
30 #include "trackdb-int.h"
33 #include "configuration.h"
37 static int trackdb_playlist_get_tid(const char *name,
43 static int trackdb_playlist_set_tid(const char *name,
49 static int trackdb_playlist_list_tid(const char *who,
53 static int trackdb_playlist_delete_tid(const char *name,
57 /** @brief Parse a playlist name
58 * @param name Playlist name
59 * @param ownerp Where to put owner, or NULL
60 * @param sharep Where to put default sharing, or NULL
61 * @return 0 on success, -1 on error
63 * Playlists take the form USER.PLAYLIST or just PLAYLIST. The PLAYLIST part
64 * is alphanumeric and nonempty. USER is a username (see valid_username()).
66 int playlist_parse_name(const char *name,
69 const char *dot = strchr(name, '.'), *share;
74 owner = xstrndup(name, dot - name);
75 if(!valid_username(owner))
77 if(!valid_username(dot + 1))
82 if(!valid_username(name))
90 *sharep = xstrdup(share);
94 /** @brief Check read access rights
95 * @param name Playlist name
96 * @param who Who wants to read
97 * @param share Playlist share status
99 static int playlist_may_read(const char *name,
104 if(playlist_parse_name(name, &owner, 0))
106 /* Anyone can read shared playlists */
109 /* You can always read playlists you own */
110 if(!strcmp(owner, who))
112 /* You can read public playlists */
113 if(!strcmp(share, "public"))
115 /* Anything else is prohibited */
119 /** @brief Check modify access rights
120 * @param name Playlist name
121 * @param who Who wants to modify
122 * @param share Playlist share status
124 static int playlist_may_write(const char *name,
126 const char attribute((unused)) *share) {
129 if(playlist_parse_name(name, &owner, 0))
131 /* Anyone can modify shared playlists */
134 /* You can always modify playlists you own */
135 if(!strcmp(owner, who))
137 /* Anything else is prohibited */
141 /** @brief Get playlist data
142 * @param name Name of playlist
143 * @param who Who wants to know
144 * @param tracksp Where to put list of tracks, or NULL
145 * @param ntracksp Where to put count of tracks, or NULL
146 * @param sharep Where to put sharing type, or NULL
147 * @return 0 on success, non-0 on error
149 * Possible return values:
151 * - @c ENOENT if the playlist doesn't exist
152 * - @c EINVAL if the playlist name is invalid
153 * - @c EACCES if the playlist cannot be read by @p who
155 int trackdb_playlist_get(const char *name,
162 if(playlist_parse_name(name, 0, 0)) {
163 error(0, "invalid playlist name '%s'", name);
166 WITH_TRANSACTION(trackdb_playlist_get_tid(name, who,
167 tracksp, ntracksp, sharep,
169 /* Don't expose libdb error codes too much */
175 static int trackdb_playlist_get_tid(const char *name,
185 if((e = trackdb_getdata(trackdb_playlistsdb, name, &k, tid)))
187 /* Get sharability */
188 if(!(s = kvp_get(k, "sharing"))) {
189 error(0, "playlist '%s' has no 'sharing' key", name);
192 /* Check the read is allowed */
193 if(!playlist_may_read(name, who, s))
195 /* Return sharability */
197 *sharep = xstrdup(s);
198 /* Get track count */
199 if(!(s = kvp_get(k, "count"))) {
200 error(0, "playlist '%s' has no 'count' key", name);
205 error(0, "playlist '%s' has negative count", name);
208 /* Return track count */
213 char **tracks = xcalloc(ntracks + 1, sizeof (char *));
216 for(int n = 0; n < ntracks; ++n) {
217 snprintf(b, sizeof b, "%d", n);
218 if(!(s = kvp_get(k, b))) {
219 error(0, "playlist '%s' lacks track %d", name, n);
222 tracks[n] = xstrdup(s);
225 /* Return track list */
231 /** @brief Modify or create a playlist
232 * @param name Playlist name
233 * @param tracks List of tracks to set, or NULL to leave alone
234 * @param ntracks Length of @p tracks
235 * @param share Sharing status, or NULL to leave alone
236 * @return 0 on success, non-0 on error
238 * If the playlist exists it is just modified.
240 * If the playlist does not exist it is created. The default set of tracks is
241 * none, and the default sharing is private (if it is an owned one) or shared
244 * If neither @c tracks nor @c share are set then we only do an access check.
245 * The database is never modified (even to create the playlist) in this
248 * Possible return values:
250 * - @c EINVAL if the playlist name is invalid
251 * - @c EACCES if the playlist cannot be modified by @p who
253 int trackdb_playlist_set(const char *name,
261 if(playlist_parse_name(name, &owner, 0)) {
262 error(0, "invalid playlist name '%s'", name);
265 /* Check valid share types */
268 /* Playlists with an owner must be public or private */
269 if(strcmp(share, "public")
270 && strcmp(share, "private")) {
271 error(0, "playlist '%s' must be public or private", name);
275 /* Playlists with no owner must be shared */
276 if(strcmp(share, "shared")) {
277 error(0, "playlist '%s' must be shared", name);
282 /* We've checked as much as we can for now, now go and attempt the change */
283 WITH_TRANSACTION(trackdb_playlist_set_tid(name, who, tracks, ntracks, share,
288 static int trackdb_playlist_set_tid(const char *name,
297 const char *event = "playlist_modified";
299 if((e = trackdb_getdata(trackdb_playlistsdb, name, &k, tid))
302 /* If the playlist doesn't exist set some defaults */
303 if(e == DB_NOTFOUND) {
304 char *defshare, *owner;
306 if(playlist_parse_name(name, &owner, &defshare))
308 /* Can't create a non-shared playlist belonging to someone else. In fact
309 * this should be picked up by playlist_may_write() below but it's clearer
311 if(owner && strcmp(owner, who))
314 kvp_set(&k, "count", 0);
315 kvp_set(&k, "sharing", defshare);
316 event = "playlist_created";
318 /* Check that the modification is allowed */
319 if(!(s = kvp_get(k, "sharing"))) {
320 error(0, "playlist '%s' has no 'sharing' key", name);
323 if(!playlist_may_write(name, who, s))
325 /* If no change was requested then don't even create */
326 if(!share && !tracks)
328 /* Set the new values */
330 kvp_set(&k, "sharing", share);
335 /* Sanity check track count */
336 if(ntracks < 0 || ntracks > config->playlist_max) {
337 error(0, "invalid track count %d", ntracks);
341 for(n = 0; n < ntracks; ++n) {
342 snprintf(b, sizeof b, "%d", n);
343 kvp_set(&k, b, tracks[n]);
345 /* Get the old track count */
346 if((s = kvp_get(k, "count")))
350 /* Delete old slots */
351 for(; n < oldcount; ++n) {
352 snprintf(b, sizeof b, "%d", n);
353 kvp_set(&k, b, NULL);
355 /* Set the new count */
356 snprintf(b, sizeof b, "%d", ntracks);
357 kvp_set(&k, "count", b);
359 /* Store the resulting record */
360 e = trackdb_putdata(trackdb_playlistsdb, name, k, tid, 0);
363 eventlog(event, name, kvp_get(k, "sharing"), (char *)0);
367 /** @brief Get a list of playlists
368 * @param who Who wants to know
369 * @param playlistsp Where to put list of playlists
370 * @param nplaylistsp Where to put count of playlists, or NULL
372 void trackdb_playlist_list(const char *who,
377 WITH_TRANSACTION(trackdb_playlist_list_tid(who, playlistsp, nplaylistsp,
381 static int trackdb_playlist_list_tid(const char *who,
391 c = trackdb_opencursor(trackdb_playlistsdb, tid);
392 memset(k, 0, sizeof k);
393 while(!(e = c->c_get(c, k, prepare_data(d), DB_NEXT))) {
394 char *name = xstrndup(k->data, k->size), *owner;
395 const char *share = kvp_get(kvp_urldecode(d->data, d->size),
398 /* Extract owner; malformed names are skipped */
399 if(playlist_parse_name(name, &owner, 0)) {
400 error(0, "invalid playlist name '%s' found in database", name);
404 error(0, "playlist '%s' has no 'sharing' key", name);
407 /* Always list public and shared playlists
408 * Only list private ones to their owner
409 * Don't list anything else
411 if(!strcmp(share, "public")
412 || !strcmp(share, "shared")
413 || (!strcmp(share, "private")
414 && owner && !strcmp(owner, who)))
415 vector_append(v, name);
417 trackdb_closecursor(c);
421 case DB_LOCK_DEADLOCK:
424 fatal(0, "c->c_get: %s", db_strerror(e));
428 *playlistsp = v->vec;
430 *nplaylistsp = v->nvec;
434 /** @brief Delete a playlist
435 * @param name Playlist name
436 * @param who Who is deleting it
437 * @return 0 on success, non-0 on error
439 * Possible return values:
441 * - @c EINVAL if the playlist name is invalid
442 * - @c EACCES if the playlist cannot be modified by @p who
443 * - @c ENOENT if the playlist doesn't exist
445 int trackdb_playlist_delete(const char *name,
450 if(playlist_parse_name(name, &owner, 0)) {
451 error(0, "invalid playlist name '%s'", name);
454 /* We've checked as much as we can for now, now go and attempt the change */
455 WITH_TRANSACTION(trackdb_playlist_delete_tid(name, who, tid));
461 static int trackdb_playlist_delete_tid(const char *name,
468 if((e = trackdb_getdata(trackdb_playlistsdb, name, &k, tid)))
470 /* Check that modification is allowed */
471 if(!(s = kvp_get(k, "sharing"))) {
472 error(0, "playlist '%s' has no 'sharing' key", name);
475 if(!playlist_may_write(name, who, s))
477 /* Delete the playlist */
478 e = trackdb_delkey(trackdb_playlistsdb, name, tid);
480 eventlog("playlist_deleted", name, 0);