From f35e5800ee5e7219ec5bd2f6c17505ff0cbcbd30 Mon Sep 17 00:00:00 2001 Message-Id: From: Mark Wooding Date: Sat, 1 Dec 2007 17:31:18 +0000 Subject: [PATCH] fixes and test for disorder-dump Organization: Straylight/Edgeware From: Richard Kettlewell --- python/disorder.py.in | 28 ++++++++++++++++++++++ server/dump.c | 34 +++++++++++++++++++++++--- tests/Makefile.am | 2 +- tests/dump.py | 56 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 4 deletions(-) create mode 100755 tests/dump.py diff --git a/python/disorder.py.in b/python/disorder.py.in index 95465c0..01b92ed 100644 --- a/python/disorder.py.in +++ b/python/disorder.py.in @@ -755,6 +755,34 @@ class client: ret, details = self._simple("part", track, context, part) return details + def setglobal(self, key, value): + """Set a global preference value. + + Arguments: + key -- the preference name + value -- the new preference value + """ + self._simple("set-global", key, value) + + def unsetglobal(self, key): + """Unset a global preference value. + + Arguments: + key -- the preference to remove + """ + self._simple("set-global", key, value) + + def getglobal(self, key): + """Get a global preference value. + + Arguments: + key -- the preference to look up + + The return value is the preference + """ + ret, details = self._simple("get-global", key) + return details + ######################################################################## # I/O infrastructure diff --git a/server/dump.c b/server/dump.c index 84b45d0..9363a34 100644 --- a/server/dump.c +++ b/server/dump.c @@ -106,6 +106,7 @@ static void do_dump(FILE *fp, const char *tag, fatal(errno, "error calling ftruncate"); if(fprintf(fp, "V%c\n", (tracksdb || searchdb) ? '1' : '0') < 0) fatal(errno, "error writing to %s", tag); + /* dump the preferences */ cursor = trackdb_opencursor(trackdb_prefsdb, tid); err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), DB_FIRST); @@ -122,6 +123,23 @@ static void do_dump(FILE *fp, const char *tag, if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; } cursor = 0; + /* dump the global preferences */ + cursor = trackdb_opencursor(trackdb_globaldb, tid); + err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), + DB_FIRST); + while(err == 0) { + if(fputc('G', fp) < 0 + || urlencode(s, k.data, k.size) + || fputc('\n', fp) < 0 + || urlencode(s, d.data, d.size) + || fputc('\n', fp) < 0) + fatal(errno, "error writing to %s", tag); + err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), + DB_NEXT); + } + if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; } + cursor = 0; + if(tracksdb) { cursor = trackdb_opencursor(trackdb_tracksdb, tid); err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), @@ -256,6 +274,8 @@ static int undump_dbt(FILE *fp, const char *tag, DBT *dbt) { static int undump_from_fp(DB_TXN *tid, FILE *fp, const char *tag) { int err, c; DBT k, d; + const char *which_name; + DB *which_db; info("undumping"); if(fseek(fp, 0, SEEK_SET) < 0) @@ -273,17 +293,25 @@ static int undump_from_fp(DB_TXN *tid, FILE *fp, const char *tag) { case 'E': return 0; case 'P': + case 'G': + if(c == 'P') { + which_db = trackdb_prefsdb; + which_name = "prefs.db"; + } else { + which_db = trackdb_globaldb; + which_name = "global.db"; + } if(undump_dbt(fp, tag, prepare_data(&k)) || undump_dbt(fp, tag, prepare_data(&d))) break; - switch(err = trackdb_prefsdb->put(trackdb_prefsdb, tid, &k, &d, 0)) { + switch(err = trackdb_prefsdb->put(which_db, tid, &k, &d, 0)) { case 0: break; case DB_LOCK_DEADLOCK: - error(0, "error updating prefs.db: %s", db_strerror(err)); + error(0, "error updating %s: %s", which_name, db_strerror(err)); return err; default: - fatal(0, "error updating prefs.db: %s", db_strerror(err)); + fatal(0, "error updating %s: %s", which_name, db_strerror(err)); } break; case 'T': diff --git a/tests/Makefile.am b/tests/Makefile.am index 962a320..56c64be 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -22,5 +22,5 @@ check: ${PYTHON} ${srcdir}/alltests EXTRA_DIST=alltests dtest.py nothing.py version.py dbversion.py search.py \ - queue.py + queue.py dump.py diff --git a/tests/dump.py b/tests/dump.py new file mode 100755 index 0000000..99d29b6 --- /dev/null +++ b/tests/dump.py @@ -0,0 +1,56 @@ +#! /usr/bin/env python +# +# This file is part of DisOrder. +# Copyright (C) 2007 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,time,disorder,re + +def test(): + """Exercise database dumper""" + dtest.start_daemon() + c = disorder.client() + track = "%s/Joe Bloggs/First Album/02:Second track.ogg" % dtest.tracks + dump = "%s/dumpfile" % dtest.testroot + print "setting a track pref" + c.set(track, "foo", "before") + assert c.get(track, "foo") == "before", "checking track foo=before" + print "setting a global pref" + c.setglobal("foo", "before"); + assert c.getglobal("foo") == "before", "checking global foo=before" + print "dumping database" + print dtest.command(["disorder-dump", "--config", disorder._configfile, + "--dump", dump]) + print "changing track pref" + c.set(track, "foo", "after"); + assert c.get(track, "foo") == "after", "checking track foo=before" + print "changing global pref" + c.setglobal("foo", "after"); + assert c.getglobal("foo") == "after", "checking global foo=before" + dtest.stop_daemon(); + print "restoring database" + print dtest.command(["disorder-dump", "--config", disorder._configfile, + "--undump", dump]) + dtest.start_daemon(); + c = disorder.client() + print "checking track pref" + assert c.get(track, "foo") == "before", "checking track foo=before after undump" + print "checking global pref" + assert c.getglobal("foo") == "before", "checking global foo=before after undump" + +if __name__ == '__main__': + dtest.run() -- [mdw]