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 who User modifying playlist
234 * @param tracks List of tracks to set, or NULL to leave alone
235 * @param ntracks Length of @p tracks
236 * @param share Sharing status, or NULL to leave alone
237 * @return 0 on success, non-0 on error
239 * If the playlist exists it is just modified.
241 * If the playlist does not exist it is created. The default set of tracks is
242 * none, and the default sharing is private (if it is an owned one) or shared
245 * If neither @c tracks nor @c share are set then we only do an access check.
246 * The database is never modified (even to create the playlist) in this
249 * Possible return values:
251 * - @c EINVAL if the playlist name is invalid
252 * - @c EACCES if the playlist cannot be modified by @p who
254 int trackdb_playlist_set(const char *name
,
262 if(playlist_parse_name(name
, &owner
, 0)) {
263 error(0, "invalid playlist name '%s'", name
);
266 /* Check valid share types */
269 /* Playlists with an owner must be public or private */
270 if(strcmp(share
, "public")
271 && strcmp(share
, "private")) {
272 error(0, "playlist '%s' must be public or private", name
);
276 /* Playlists with no owner must be shared */
277 if(strcmp(share
, "shared")) {
278 error(0, "playlist '%s' must be shared", name
);
283 /* We've checked as much as we can for now, now go and attempt the change */
284 WITH_TRANSACTION(trackdb_playlist_set_tid(name
, who
, tracks
, ntracks
, share
,
289 static int trackdb_playlist_set_tid(const char *name
,
298 const char *event
= "playlist_modified";
300 if((e
= trackdb_getdata(trackdb_playlistsdb
, name
, &k
, tid
))
303 /* If the playlist doesn't exist set some defaults */
304 if(e
== DB_NOTFOUND
) {
305 char *defshare
, *owner
;
307 if(playlist_parse_name(name
, &owner
, &defshare
))
309 /* Can't create a non-shared playlist belonging to someone else. In fact
310 * this should be picked up by playlist_may_write() below but it's clearer
312 if(owner
&& strcmp(owner
, who
))
315 kvp_set(&k
, "count", 0);
316 kvp_set(&k
, "sharing", defshare
);
317 event
= "playlist_created";
319 /* Check that the modification is allowed */
320 if(!(s
= kvp_get(k
, "sharing"))) {
321 error(0, "playlist '%s' has no 'sharing' key", name
);
324 if(!playlist_may_write(name
, who
, s
))
326 /* If no change was requested then don't even create */
327 if(!share
&& !tracks
)
329 /* Set the new values */
331 kvp_set(&k
, "sharing", share
);
336 /* Sanity check track count */
337 if(ntracks
< 0 || ntracks
> config
->playlist_max
) {
338 error(0, "invalid track count %d", ntracks
);
342 for(n
= 0; n
< ntracks
; ++n
) {
343 snprintf(b
, sizeof b
, "%d", n
);
344 kvp_set(&k
, b
, tracks
[n
]);
346 /* Get the old track count */
347 if((s
= kvp_get(k
, "count")))
351 /* Delete old slots */
352 for(; n
< oldcount
; ++n
) {
353 snprintf(b
, sizeof b
, "%d", n
);
354 kvp_set(&k
, b
, NULL
);
356 /* Set the new count */
357 snprintf(b
, sizeof b
, "%d", ntracks
);
358 kvp_set(&k
, "count", b
);
360 /* Store the resulting record */
361 e
= trackdb_putdata(trackdb_playlistsdb
, name
, k
, tid
, 0);
364 eventlog(event
, name
, kvp_get(k
, "sharing"), (char *)0);
368 /** @brief Get a list of playlists
369 * @param who Who wants to know
370 * @param playlistsp Where to put list of playlists
371 * @param nplaylistsp Where to put count of playlists, or NULL
373 void trackdb_playlist_list(const char *who
,
378 WITH_TRANSACTION(trackdb_playlist_list_tid(who
, playlistsp
, nplaylistsp
,
382 static int trackdb_playlist_list_tid(const char *who
,
392 c
= trackdb_opencursor(trackdb_playlistsdb
, tid
);
393 memset(k
, 0, sizeof k
);
394 while(!(e
= c
->c_get(c
, k
, prepare_data(d
), DB_NEXT
))) {
395 char *name
= xstrndup(k
->data
, k
->size
), *owner
;
396 const char *share
= kvp_get(kvp_urldecode(d
->data
, d
->size
),
399 /* Extract owner; malformed names are skipped */
400 if(playlist_parse_name(name
, &owner
, 0)) {
401 error(0, "invalid playlist name '%s' found in database", name
);
405 error(0, "playlist '%s' has no 'sharing' key", name
);
408 /* Always list public and shared playlists
409 * Only list private ones to their owner
410 * Don't list anything else
412 if(!strcmp(share
, "public")
413 || !strcmp(share
, "shared")
414 || (!strcmp(share
, "private")
415 && owner
&& !strcmp(owner
, who
)))
416 vector_append(v
, name
);
418 trackdb_closecursor(c
);
422 case DB_LOCK_DEADLOCK
:
425 fatal(0, "c->c_get: %s", db_strerror(e
));
429 *playlistsp
= v
->vec
;
431 *nplaylistsp
= v
->nvec
;
435 /** @brief Delete a playlist
436 * @param name Playlist name
437 * @param who Who is deleting it
438 * @return 0 on success, non-0 on error
440 * Possible return values:
442 * - @c EINVAL if the playlist name is invalid
443 * - @c EACCES if the playlist cannot be modified by @p who
444 * - @c ENOENT if the playlist doesn't exist
446 int trackdb_playlist_delete(const char *name
,
451 if(playlist_parse_name(name
, &owner
, 0)) {
452 error(0, "invalid playlist name '%s'", name
);
455 /* We've checked as much as we can for now, now go and attempt the change */
456 WITH_TRANSACTION(trackdb_playlist_delete_tid(name
, who
, tid
));
462 static int trackdb_playlist_delete_tid(const char *name
,
469 if((e
= trackdb_getdata(trackdb_playlistsdb
, name
, &k
, tid
)))
471 /* Check that modification is allowed */
472 if(!(s
= kvp_get(k
, "sharing"))) {
473 error(0, "playlist '%s' has no 'sharing' key", name
);
476 if(!playlist_may_write(name
, who
, s
))
478 /* Delete the playlist */
479 e
= trackdb_delkey(trackdb_playlistsdb
, name
, tid
);
481 eventlog("playlist_deleted", name
, 0);