From: Richard Kettlewell Date: Sun, 18 May 2008 12:12:49 +0000 (+0100) Subject: Resolve aliases in c_get, c_set, c_prefs and c_part. The intended X-Git-Tag: 4.0~79 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/commitdiff_plain/27ed5a6987fc300f25569628e9152fafcf34c49f Resolve aliases in c_get, c_set, c_prefs and c_part. The intended behaviour is that preferences always belong to the canonical version of the track and never to an alias. Added a new aliases.py test to verify this and related alias behaviour. Fixes defect 20. --- diff --git a/server/server.c b/server/server.c index 3d59398..fd0fcd8 100644 --- a/server/server.c +++ b/server/server.c @@ -614,9 +614,13 @@ static int c_allfiles(struct conn *c, static int c_get(struct conn *c, char **vec, int attribute((unused)) nvec) { - const char *v; + const char *v, *track; - if(vec[1][0] != '_' && (v = trackdb_get(vec[0], vec[1]))) + if(!(track = trackdb_resolve(vec[0]))) { + sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); + return 1; + } + if(vec[1][0] != '_' && (v = trackdb_get(track, vec[1]))) sink_printf(ev_writer_sink(c->w), "252 %s\n", quoteutf8(v)); else sink_writes(ev_writer_sink(c->w), "555 not found\n"); @@ -642,7 +646,13 @@ static int c_length(struct conn *c, static int c_set(struct conn *c, char **vec, int attribute((unused)) nvec) { - if(vec[1][0] != '_' && !trackdb_set(vec[0], vec[1], vec[2])) + const char *track; + + if(!(track = trackdb_resolve(vec[0]))) { + sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); + return 1; + } + if(vec[1][0] != '_' && !trackdb_set(track, vec[1], vec[2])) sink_writes(ev_writer_sink(c->w), "250 OK\n"); else sink_writes(ev_writer_sink(c->w), "550 not found\n"); @@ -653,8 +663,13 @@ static int c_prefs(struct conn *c, char **vec, int attribute((unused)) nvec) { struct kvp *k; + const char *track; - k = trackdb_get_all(vec[0]); + if(!(track = trackdb_resolve(vec[0]))) { + sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); + return 1; + } + k = trackdb_get_all(track); sink_writes(ev_writer_sink(c->w), "253 prefs follow\n"); for(; k; k = k->next) if(k->name[0] != '_') /* omit internal values */ @@ -667,6 +682,7 @@ static int c_prefs(struct conn *c, static int c_exists(struct conn *c, char **vec, int attribute((unused)) nvec) { + /* trackdb_exists() does its own alias checking */ sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[trackdb_exists(vec[0])]); return 1; } @@ -931,8 +947,14 @@ static int c_moveafter(struct conn *c, static int c_part(struct conn *c, char **vec, int attribute((unused)) nvec) { + const char *track; + + if(!(track = trackdb_resolve(vec[0]))) { + sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n"); + return 1; + } sink_printf(ev_writer_sink(c->w), "252 %s\n", - quoteutf8(trackdb_getpart(vec[0], vec[1], vec[2]))); + quoteutf8(trackdb_getpart(track, vec[1], vec[2]))); return 1; } diff --git a/tests/Makefile.am b/tests/Makefile.am index 08cfc0b..d21edcb 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -27,7 +27,7 @@ disorder_udplog_LDADD=$(LIBOBJS) ../lib/libdisorder.a disorder_udplog_DEPENDENCIES=../lib/libdisorder.a TESTS=cookie.py dbversion.py dump.py files.py play.py queue.py \ - recode.py search.py user-upgrade.py user.py + recode.py search.py user-upgrade.py user.py aliases.py TESTS_ENVIRONMENT=${PYTHON} -u diff --git a/tests/aliases.py b/tests/aliases.py new file mode 100755 index 0000000..6ff74d8 --- /dev/null +++ b/tests/aliases.py @@ -0,0 +1,70 @@ +#! /usr/bin/env python -u +# +# This file is part of DisOrder. +# Copyright (C) 2008 Richard Kettlewell +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +# USA +# +import dtest,disorder + +def test(): + """Exercise alias logic""" + dtest.start_daemon() + dtest.create_user() + dtest.rescan() + c = disorder.client() + + print " creating an alias in new directory" + track = "%s/misc/blahblahblah.ogg" % dtest.tracks + c.set(track, + "trackname_display_artist", + "Fred Smith") + c.set(track, + "trackname_display_album", + "wibble") + + print " checking it shows up in the right place" + alias = "%s/Fred Smith/wibble/blahblahblah.ogg" % dtest.tracks + files = c.files("%s/Fred Smith/wibble" % dtest.tracks) + assert files == [alias] + + print " checking part calculation" + artist = c.part(track, "display", "artist") + assert artist == "Fred Smith", "checking artist part" + album = c.part(track, "display", "album") + assert album == "wibble", "checking album part" + title = c.part(track, "display", "title") + assert title == "blahblahblah", "checking title part" + + print " checking part calculation on alias" + artist = c.part(alias, "display", "artist") + assert artist == "Fred Smith", "checking artist part" + album = c.part(alias, "display", "album") + assert album == "wibble", "checking album part" + title = c.part(alias, "display", "title") + assert title == "blahblahblah", "checking title part" + + # See defect #20 + print " checking that prefs always belong to the canonical name" + c.set(alias, "wibble", "spong") + value = c.get(track, "wibble") + assert value == "spong", "checking pref ended up on resolved track" + c.set(track, "foo", "bar") + value = c.get(alias, "foo") + assert value == "bar", "checking pref visible via alias" + +if __name__ == '__main__': + dtest.run()