X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/812b526d127c6657e571db8b33a58137af6709cd..128962e066a3bebc3ae0caba614d060cfcd29ca9:/lib/trackdb-playlists.c diff --git a/lib/trackdb-playlists.c b/lib/trackdb-playlists.c index bef7107..27d3319 100644 --- a/lib/trackdb-playlists.c +++ b/lib/trackdb-playlists.c @@ -33,6 +33,7 @@ #include "configuration.h" #include "vector.h" #include "eventlog.h" +#include "validity.h" static int trackdb_playlist_get_tid(const char *name, const char *who, @@ -54,43 +55,6 @@ static int trackdb_playlist_delete_tid(const char *name, const char *who, DB_TXN *tid); -/** @brief Parse a playlist name - * @param name Playlist name - * @param ownerp Where to put owner, or NULL - * @param sharep Where to put default sharing, or NULL - * @return 0 on success, -1 on error - * - * Playlists take the form USER.PLAYLIST or just PLAYLIST. The PLAYLIST part - * is alphanumeric and nonempty. USER is a username (see valid_username()). - */ -int playlist_parse_name(const char *name, - char **ownerp, - char **sharep) { - const char *dot = strchr(name, '.'), *share; - char *owner; - - if(dot) { - /* Owned playlist */ - owner = xstrndup(name, dot - name); - if(!valid_username(owner)) - return -1; - if(!valid_username(dot + 1)) - return -1; - share = "private"; - } else { - /* Shared playlist */ - if(!valid_username(name)) - return -1; - owner = 0; - share = "shared"; - } - if(ownerp) - *ownerp = owner; - if(sharep) - *sharep = xstrdup(share); - return 0; -} - /** @brief Check read access rights * @param name Playlist name * @param who Who wants to read @@ -160,7 +124,7 @@ int trackdb_playlist_get(const char *name, int e; if(playlist_parse_name(name, 0, 0)) { - error(0, "invalid playlist name '%s'", name); + disorder_error(0, "invalid playlist name '%s'", name); return EINVAL; } WITH_TRANSACTION(trackdb_playlist_get_tid(name, who, @@ -186,7 +150,7 @@ static int trackdb_playlist_get_tid(const char *name, return e; /* Get sharability */ if(!(s = kvp_get(k, "sharing"))) { - error(0, "playlist '%s' has no 'sharing' key", name); + disorder_error(0, "playlist '%s' has no 'sharing' key", name); s = "private"; } /* Check the read is allowed */ @@ -197,12 +161,12 @@ static int trackdb_playlist_get_tid(const char *name, *sharep = xstrdup(s); /* Get track count */ if(!(s = kvp_get(k, "count"))) { - error(0, "playlist '%s' has no 'count' key", name); + disorder_error(0, "playlist '%s' has no 'count' key", name); s = "0"; } ntracks = atoi(s); if(ntracks < 0) { - error(0, "playlist '%s' has negative count", name); + disorder_error(0, "playlist '%s' has negative count", name); ntracks = 0; } /* Return track count */ @@ -216,7 +180,7 @@ static int trackdb_playlist_get_tid(const char *name, for(int n = 0; n < ntracks; ++n) { snprintf(b, sizeof b, "%d", n); if(!(s = kvp_get(k, b))) { - error(0, "playlist '%s' lacks track %d", name, n); + disorder_error(0, "playlist '%s' lacks track %d", name, n); s = "unknown"; } tracks[n] = xstrdup(s); @@ -230,6 +194,7 @@ static int trackdb_playlist_get_tid(const char *name, /** @brief Modify or create a playlist * @param name Playlist name + * @param who User modifying playlist * @param tracks List of tracks to set, or NULL to leave alone * @param ntracks Length of @p tracks * @param share Sharing status, or NULL to leave alone @@ -259,7 +224,7 @@ int trackdb_playlist_set(const char *name, char *owner; if(playlist_parse_name(name, &owner, 0)) { - error(0, "invalid playlist name '%s'", name); + disorder_error(0, "invalid playlist name '%s'", name); return EINVAL; } /* Check valid share types */ @@ -268,13 +233,13 @@ int trackdb_playlist_set(const char *name, /* Playlists with an owner must be public or private */ if(strcmp(share, "public") && strcmp(share, "private")) { - error(0, "playlist '%s' must be public or private", name); + disorder_error(0, "playlist '%s' must be public or private", name); return EINVAL; } } else { /* Playlists with no owner must be shared */ if(strcmp(share, "shared")) { - error(0, "playlist '%s' must be shared", name); + disorder_error(0, "playlist '%s' must be shared", name); return EINVAL; } } @@ -317,7 +282,7 @@ static int trackdb_playlist_set_tid(const char *name, } /* Check that the modification is allowed */ if(!(s = kvp_get(k, "sharing"))) { - error(0, "playlist '%s' has no 'sharing' key", name); + disorder_error(0, "playlist '%s' has no 'sharing' key", name); s = "private"; } if(!playlist_may_write(name, who, s)) @@ -334,7 +299,7 @@ static int trackdb_playlist_set_tid(const char *name, /* Sanity check track count */ if(ntracks < 0 || ntracks > config->playlist_max) { - error(0, "invalid track count %d", ntracks); + disorder_error(0, "invalid track count %d", ntracks); return EINVAL; } /* Set the tracks */ @@ -397,11 +362,11 @@ static int trackdb_playlist_list_tid(const char *who, /* Extract owner; malformed names are skipped */ if(playlist_parse_name(name, &owner, 0)) { - error(0, "invalid playlist name '%s' found in database", name); + disorder_error(0, "invalid playlist name '%s' found in database", name); continue; } if(!share) { - error(0, "playlist '%s' has no 'sharing' key", name); + disorder_error(0, "playlist '%s' has no 'sharing' key", name); continue; } /* Always list public and shared playlists @@ -421,7 +386,7 @@ static int trackdb_playlist_list_tid(const char *who, case DB_LOCK_DEADLOCK: return e; default: - fatal(0, "c->c_get: %s", db_strerror(e)); + disorder_fatal(0, "c->c_get: %s", db_strerror(e)); } vector_terminate(v); if(playlistsp) @@ -448,7 +413,7 @@ int trackdb_playlist_delete(const char *name, char *owner; if(playlist_parse_name(name, &owner, 0)) { - error(0, "invalid playlist name '%s'", name); + disorder_error(0, "invalid playlist name '%s'", name); return EINVAL; } /* We've checked as much as we can for now, now go and attempt the change */ @@ -469,7 +434,7 @@ static int trackdb_playlist_delete_tid(const char *name, return e; /* Check that modification is allowed */ if(!(s = kvp_get(k, "sharing"))) { - error(0, "playlist '%s' has no 'sharing' key", name); + disorder_error(0, "playlist '%s' has no 'sharing' key", name); s = "private"; } if(!playlist_may_write(name, who, s))