]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
Implement ls --admissible=m/foo.
authorAndre Noll <maan@tuebingen.mpg.de>
Wed, 23 Mar 2022 22:19:55 +0000 (23:19 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Fri, 12 May 2023 10:16:23 +0000 (12:16 +0200)
Currently there can be only one score table at a time because the
functions of score.c refer to the global score_table variable.
To implement the new feature, we need to overcome this restriction
so that the callback of the ls command can populate an independent
score table to print its output without interfering with the score
table that is currently active.

This commit changes most functions of score.c to receive an additional
table pointer argument. All current users of the score table pass a
NULL pointer to instruct the functions to operate on the global score
table as before.

However, if the ls command is invoked with an optional mood argument
to -a. the callback calls mood_load(), followed by mood_loop() and
mood_unload(). The former returns an opaque handle which is then
passed to the other two functions to instruct them to operate on the
temporary score table instead of the global one.

To make the feature work for playlists as well, analogous functionality
is implemented in playlist.c. The new mop_loop() of aft.c performs
the disambiguation in a similar way as the activate_mood_or_playlist()
does. It is a bit simpler though, since the ls command does not have
to deal with NULL arguments and does not need to fall back to the
dummy mood.

afs.c
afs.h
aft.c
m4/lls/server_cmd.suite.m4
mood.c
playlist.c
score.c

diff --git a/afs.c b/afs.c
index 78e42eaec54b9a1cbb87a3c349a0fa12ae98601f..9e679dcf14ebee2a36a83aa02882ba96950e7b21 100644 (file)
--- a/afs.c
+++ b/afs.c
@@ -437,14 +437,14 @@ static int activate_mood_or_playlist(const char *arg, struct para_buffer *pb)
        int ret;
        char *msg;
 
-       if (!arg) {
-               ret = mood_load(NULL, &msg);
+       if (!arg) { /* load dummy mood */
+               ret = mood_load(NULL, NULL, &msg);
                mode = PLAY_MODE_MOOD;
        } else if (!strncmp(arg, "p/", 2)) {
-               ret = playlist_load(arg + 2, &msg);
+               ret = playlist_load(arg + 2, NULL, &msg);
                mode = PLAY_MODE_PLAYLIST;
        } else if (!strncmp(arg, "m/", 2)) {
-               ret = mood_load(arg + 2, &msg);
+               ret = mood_load(arg + 2, NULL, &msg);
                mode = PLAY_MODE_MOOD;
        } else {
                ret = -ERRNO_TO_PARA_ERROR(EINVAL);
@@ -962,7 +962,7 @@ __noreturn void afs_init(int socket_fd)
        }
        ret = schedule(&s);
        sched_shutdown(&s);
-       mood_unload();
+       mood_unload(NULL);
 out_close:
        close_afs_tables();
 out:
@@ -986,9 +986,9 @@ static int com_select_callback(struct afs_callback_arg *aca)
        arg = lls_input(0, aca->lpr);
        score_clear();
        if (current_play_mode == PLAY_MODE_MOOD)
-               mood_unload();
+               mood_unload(NULL);
        else
-               playlist_unload();
+               playlist_unload(NULL);
        ret = activate_mood_or_playlist(arg, &aca->pbout);
        if (ret >= 0)
                goto free_lpr;
diff --git a/afs.h b/afs.h
index 9a1d7d9ca0f130c4590f55c23abc181b632c6483..e8b8c865bda36b9f905b2c1cb435d03bc12d3cbd 100644 (file)
--- a/afs.h
+++ b/afs.h
@@ -238,10 +238,12 @@ int for_each_matching_row(struct pattern_match_data *pmd);
 
 /* score */
 extern const struct afs_table_operations score_ops;
-int score_loop(osl_rbtree_loop_func *func, void *data);
+void score_open(struct osl_table **result);
+void score_close(struct osl_table *t);
+int score_loop(osl_rbtree_loop_func *func, struct osl_table *t, void *data);
 int score_get_best(struct osl_row **aft_row, long *score);
 int get_score_and_aft_row(struct osl_row *score_row, long *score, struct osl_row **aft_row);
-int score_add(const struct osl_row *row, long score);
+int score_add(const struct osl_row *aft_row, long score, struct osl_table *t);
 int score_update(const struct osl_row *aft_row, long new_score);
 int score_delete(const struct osl_row *aft_row);
 void score_clear(void);
@@ -268,13 +270,17 @@ int aft_check_callback(struct afs_callback_arg *aca);
 void free_status_items(void);
 
 /* mood */
-int mood_load(const char *mood_name, char **msg);
-void mood_unload(void);
+struct mood_instance;
+int mood_load(const char *mood_name, struct mood_instance **result, char **msg);
+int mood_loop(struct mood_instance *m, osl_rbtree_loop_func *func, void *data);
+void mood_unload(struct mood_instance *m);
 int mood_check_callback(struct afs_callback_arg *aca);
 
 /* playlist */
-int playlist_load(const char *name, char **msg);
-void playlist_unload(void);
+struct playlist_instance;
+int playlist_load(const char *name, struct playlist_instance **result, char **msg);
+int playlist_loop(struct playlist_instance *pi, osl_rbtree_loop_func *func, void *data);
+void playlist_unload(struct playlist_instance *pi);
 int playlist_check_callback(struct afs_callback_arg *aca);
 
 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
diff --git a/aft.c b/aft.c
index b4d8c2dd359bbfbe143c8be75a89b016b0bfe505..5ecd7720fab896ac17c346e425da61f40b1ce13e 100644 (file)
--- a/aft.c
+++ b/aft.c
@@ -1361,23 +1361,60 @@ err:
        return ret;
 }
 
+static int mop_loop(const char *arg, struct afs_callback_arg *aca,
+               struct ls_options *opts)
+{
+       int ret;
+       char *msg;
+
+       if (!arg || strcmp(arg, ".") == 0)
+               return score_loop(prepare_ls_row, NULL, opts);
+       if (!strncmp(arg, "m/", 2)) {
+               struct mood_instance *m;
+               ret = mood_load(arg + 2, &m, &msg);
+               if (ret < 0)
+                       afs_error(aca, "%s", msg);
+               free(msg);
+               if (ret < 0)
+                       return ret;
+               ret = mood_loop(m, prepare_ls_row, opts);
+               mood_unload(m);
+               return ret;
+       }
+       if (!strncmp(arg, "p/", 2)) {
+               struct playlist_instance *pi;
+               ret = playlist_load(arg + 2, &pi, &msg);
+               if (ret < 0)
+                       afs_error(aca, "%s", msg);
+               free(msg);
+               if (ret < 0)
+                       return ret;
+               ret = playlist_loop(pi, prepare_ls_row, opts);
+               playlist_unload(pi);
+               return ret;
+       }
+       afs_error(aca, "bad mood/playlist specifier: %s\n", arg);
+       return -ERRNO_TO_PARA_ERROR(EINVAL);
+}
+
 static int com_ls_callback(struct afs_callback_arg *aca)
 {
        const struct lls_command *cmd = SERVER_CMD_CMD_PTR(LS);
        struct ls_options *opts = aca->query.data;
        int i = 0, ret;
        time_t current_time;
-       const struct lls_opt_result *r_r;
+       const struct lls_opt_result *r_r, *r_a;
 
        ret = lls_deserialize_parse_result(
                (char *)aca->query.data + sizeof(*opts), cmd, &opts->lpr);
        assert(ret >= 0);
        r_r = SERVER_CMD_OPT_RESULT(LS, REVERSE, opts->lpr);
-
+       r_a = SERVER_CMD_OPT_RESULT(LS, ADMISSIBLE, opts->lpr);
        aca->pbout.flags = (opts->mode == LS_MODE_PARSER)? PBF_SIZE_PREFIX : 0;
-       if (admissible_only(opts))
-               ret = score_loop(prepare_ls_row, opts);
-       else
+       if (admissible_only(opts)) {
+               const char *arg = lls_string_val(0, r_a);
+               ret = mop_loop(arg, aca, opts);
+       } else
                ret = osl(osl_rbtree_loop(audio_file_table, AFTCOL_PATH, opts,
                        prepare_ls_row));
        if (ret < 0)
index 8200c6249449f81b5743775f04796462fc912b83..1cec68164a13d46fdd96bfcb50c2b0ea160a0585 100644 (file)
@@ -233,9 +233,19 @@ m4_include(`com_ll.m4')
        [option admissible]
                short_opt = a
                summary = list only admissible files
+               arg_type = string
+               arg_info = optional_arg
+               typestr = specifier/name
+               default_val = .
                [help]
-                       List only files which are admissible with respect to the current mood
-                       or playlist.
+                       If the optional argument is supplied, it must be of the form "p/foo"
+                       or "m/bar" (which refer to the playlist named "foo" and the mood named
+                       "bar", respectively). The command then restricts its output to the set
+                       of files which are admissible with respect to the thusly identified
+                       mood or playlist.
+
+                       If no argument is given, or if the argument is the special value ".",
+                       the current mood or playlist is assumed.
                [/help]
        [option reverse]
                short_opt = r
diff --git a/mood.c b/mood.c
index 9cdfd011a6ef4f2ab9fb180892f6412247eb68fe..804fb57667563461229d5b9e37aa8a16c6320b67 100644 (file)
--- a/mood.c
+++ b/mood.c
@@ -59,6 +59,8 @@ struct mood_instance {
        struct mp_context *parser_context;
        /** To compute the score. */
        struct afs_statistics stats;
+       /** NULL means to operate on the global score table. */
+       struct osl_table *score_table;
 };
 
 /*
@@ -437,7 +439,7 @@ static void update_afs_statistics(struct afs_info *old_afsi,
 }
 
 static int add_to_score_table(const struct osl_row *aft_row,
-               const struct afs_statistics *stats)
+               struct mood_instance *m)
 {
        long score;
        struct afs_info afsi;
@@ -445,8 +447,8 @@ static int add_to_score_table(const struct osl_row *aft_row,
 
        if (ret < 0)
                return ret;
-       score = compute_score(&afsi, stats);
-       return score_add(aft_row, score);
+       score = compute_score(&afsi, &m->stats);
+       return score_add(aft_row, score, m->score_table);
 }
 
 static int delete_from_statistics_and_score_table(const struct osl_row *aft_row)
@@ -504,7 +506,7 @@ static int mood_update_audio_file(const struct osl_row *aft_row,
                ret = add_afs_statistics(aft_row, &current_mood->stats);
                if (ret < 0)
                        return ret;
-               return add_to_score_table(aft_row, &current_mood->stats);
+               return add_to_score_table(aft_row, current_mood);
        }
        /* update score */
        ret = get_afsi_of_row(aft_row, &afsi);
@@ -544,9 +546,17 @@ static char *get_statistics(struct mood_instance *m, int64_t sse)
        );
 }
 
-/** Free all resources of the current mood, if any. */
-void mood_unload(void)
+/**
+ * Free all resources of a mood instance.
+ *
+ * \param m As obtained by \ref mood_load(). If NULL, unload the current mood.
+ *
+ * It's OK to call this with m == NULL even if no current mood is loaded.
+ */
+void mood_unload(struct mood_instance *m)
 {
+       if (m)
+               return destroy_mood(m);
        destroy_mood(current_mood);
        current_mood = NULL;
 }
@@ -568,23 +578,42 @@ static void compute_correction_factors(int64_t sse, struct afs_statistics *s)
 }
 
 /**
- * Change the current mood.
+ * Populate a score table with admissible files for the given mood.
+ *
+ * This consults the mood table to initialize the mood parser with the mood
+ * expression stored in the blob object which corresponds to the given name. A
+ * score table is allocated and populated with references to those entries of
+ * the audio file table which evaluate as admissible with respect to the mood
+ * expression. For each audio file a score value is computed and stored along
+ * with the file reference.
  *
  * \param mood_name The name of the mood to load.
+ * \param result Opaque, refers to the mood parser and the score table.
  * \param msg Error message or mood info is returned here.
  *
- * If \a mood_name is \a NULL, load the dummy mood that accepts every audio file
- * and uses a scoring method based only on the \a last_played information.
+ * If the mood name is NULL, the dummy mood is loaded. This mood regards every
+ * audio file as admissible.
+ *
+ * A NULL result pointer instructs the function to operate on the current mood.
+ * That is, on the mood instance which is used by the server to select the next
+ * audio file for streaming. In this mode of operation, the mood which was
+ * active before the call, if any, is unloaded on success.
+ *
+ * If result is not NULL, the current mood is unaffected and *result points to
+ * an initialized mood instance on success. The caller can pass this reference
+ * to \ref mood_loop() to iterate over the admissible files, and should call
+ * \ref mood_unload() to free the mood instance afterwards.
  *
  * If the message pointer is not NULL, a suitable message is returned there in
  * all cases. The caller must free this string.
  *
- * \return The number of admissible files on success, negative on errors. It is
+ * \return The number of admissible files on success, negative on errors. On
+ * errors, the current mood remains unaffected even if result is NULL. It is
  * not considered an error if no files are admissible.
  *
- * \sa struct \ref afs_info::last_played, \ref mp_eval_row().
+ * \sa \ref mp_eval_row().
  */
-int mood_load(const char *mood_name, char **msg)
+int mood_load(const char *mood_name, struct mood_instance **result, char **msg)
 {
        int i, ret;
        struct admissible_array aa = {.size = 0};
@@ -615,8 +644,10 @@ int mood_load(const char *mood_name, char **msg)
                ret = 0;
                goto out;
        }
+       if (result)
+               score_open(&aa.m->score_table);
        for (i = 0; i < aa.m->stats.num; i++) {
-               ret = add_to_score_table(aa.array[i], &aa.m->stats);
+               ret = add_to_score_table(aa.array[i], aa.m);
                if (ret < 0) {
                        if (msg)
                                *msg = make_message(
@@ -628,8 +659,12 @@ int mood_load(const char *mood_name, char **msg)
        if (msg)
                *msg = get_statistics(aa.m, rnow.tv_sec);
        ret = aa.m->stats.num;
-       mood_unload();
-       current_mood = aa.m;
+       if (result)
+               *result = aa.m;
+       else {
+               mood_unload(NULL);
+               current_mood = aa.m;
+       }
 out:
        free(aa.array);
        if (ret < 0)
@@ -637,12 +672,29 @@ out:
        return ret;
 }
 
+/**
+ * Iterate over the admissible files of a mood instance.
+ *
+ * This wrapper around \ref score_loop() is the mood counterpart of \ref
+ * playlist_loop().
+ *
+ * \param m Determines the score table to iterate. Must not be NULL.
+ * \param func See \ref score_loop().
+ * \param data See \ref score_loop().
+ *
+ * \return See \ref score_loop(), \ref playlist_loop().
+ */
+int mood_loop(struct mood_instance *m, osl_rbtree_loop_func *func, void *data)
+{
+       return score_loop(func, m->score_table, data);
+}
+
 /*
  * Empty the score table and start over.
  *
- * This function is called on events which render the current list of
- * admissible files useless, for example if an attribute is removed from the
- * attribute table.
+ * This function is called on events which render the current set of admissible
+ * files invalid, for example if an attribute is removed from the attribute
+ * table.
  */
 static int reload_current_mood(void)
 {
@@ -655,8 +707,8 @@ static int reload_current_mood(void)
                current_mood->name : "(dummy)");
        if (current_mood->name)
                mood_name = para_strdup(current_mood->name);
-       mood_unload();
-       ret = mood_load(mood_name, NULL);
+       mood_unload(NULL);
+       ret = mood_load(mood_name, NULL, NULL);
        free(mood_name);
        return ret;
 }
index d02ade3ba911a186f0f7e5a57f419ce9074092d0..cd5fc5ad4f0731cba4a8d4a6cf649a3997dd93c2 100644 (file)
 
 /** \file playlist.c Functions for loading and saving playlists. */
 
-/** Structure used for adding entries to a playlist. */
+/**
+ * The state of a playlist instance.
+ *
+ * A structure of this type is allocated and initialized at playlist load time.
+ */
 struct playlist_instance {
        /** The name of the playlist. */
        char *name;
        /** The number of entries currently in the playlist. */
        unsigned length;
+       /** Contains all valid paths of the playlist. */
+       struct osl_table *score_table;
 };
 static struct playlist_instance current_playlist;
 
@@ -38,7 +44,7 @@ static int playlist_update_audio_file(const struct osl_row *aft_row)
 
 static int add_playlist_entry(char *path, void *data)
 {
-       struct playlist_instance *playlist = data;
+       struct playlist_instance *pi = data;
        struct osl_row *aft_row;
        int ret = aft_get_row_of_path(path, &aft_row);
 
@@ -46,12 +52,12 @@ static int add_playlist_entry(char *path, void *data)
                PARA_NOTICE_LOG("%s: %s\n", path, para_strerror(-ret));
                return 1;
        }
-       ret = score_add(aft_row, -playlist->length);
+       ret = score_add(aft_row, -pi->length, pi->score_table);
        if (ret < 0) {
                PARA_ERROR_LOG("failed to add %s: %s\n", path, para_strerror(-ret));
                return ret;
        }
-       playlist->length++;
+       pi->length++;
        return 1;
 }
 
@@ -103,11 +109,22 @@ int playlist_check_callback(struct afs_callback_arg *aca)
                check_playlist));
 }
 
-/** Free all resources of the current playlist, if any. */
-void playlist_unload(void)
+/**
+ * Free all resources of the given/current playlist.
+ *
+ * \param pi NULL means to unload the current playlist.
+ */
+void playlist_unload(struct playlist_instance *pi)
 {
+       if (pi) {
+               score_close(pi->score_table);
+               free(pi->name);
+               free(pi);
+               return;
+       }
        if (!current_playlist.name)
                return;
+       score_clear();
        free(current_playlist.name);
        current_playlist.name = NULL;
        current_playlist.length = 0;
@@ -122,43 +139,77 @@ void playlist_unload(void)
  * corresponding row of the audio file table is added to the score table.
  *
  * \param name The name of the playlist to load.
+ * \param result Opaque, refers to the underlying score table.
  * \param msg Error message or playlist info is returned here.
  *
  * \return The length of the loaded playlist on success, negative error code
  * else. Files which are listed in the playlist, but are not contained in the
  * database are ignored. This is not considered an error.
  */
-int playlist_load(const char *name, char **msg)
+int playlist_load(const char *name, struct playlist_instance **result, char **msg)
 {
        int ret;
-       struct playlist_instance *playlist = &current_playlist;
+       struct playlist_instance *pi;
        struct osl_object playlist_def;
 
-       ret = pl_get_def_by_name(name, &playlist_def);
-       if (ret < 0) {
-               *msg = make_message("could not read playlist %s\n", name);
-               return ret;
+       if (!name || !*name) {
+               if (msg)
+                       *msg = make_message("empty playlist name\n");
+               return -ERRNO_TO_PARA_ERROR(EINVAL);
        }
-       playlist_unload();
+       ret = pl_get_def_by_name(name, &playlist_def);
+       if (ret < 0)
+               goto err;
+       pi = zalloc(sizeof(*pi));
+       if (result)
+               score_open(&pi->score_table);
        ret = for_each_line(FELF_READ_ONLY, playlist_def.data,
-               playlist_def.size, add_playlist_entry, playlist);
+               playlist_def.size, add_playlist_entry, pi);
        osl_close_disk_object(&playlist_def);
        if (ret < 0)
-               goto err;
+               goto close_score_table;
        ret = -E_PLAYLIST_EMPTY;
-       if (!playlist->length)
-               goto err;
-       playlist->name = para_strdup(name);
-       *msg = make_message("loaded playlist %s (%u files)\n", playlist->name,
-               playlist->length);
+       if (pi->length == 0)
+               goto close_score_table;
        /* success */
-       return current_playlist.length;
+       if (msg)
+               *msg = make_message("loaded playlist %s (%u files)\n", name,
+                       pi->length);
+       pi->name = para_strdup(name);
+       if (result)
+               *result = pi;
+       else {
+               playlist_unload(NULL);
+               current_playlist = *pi;
+       }
+       return pi->length;
+close_score_table:
+       score_close(pi->score_table);
+       free(pi);
 err:
        PARA_NOTICE_LOG("unable to load playlist %s\n", name);
-       *msg = make_message("unable to load playlist %s\n", name);
+       if (msg)
+               *msg = make_message("unable to load playlist %s\n", name);
        return ret;
 }
 
+/**
+ * Iterate over all admissible audio files of a playlist instance.
+ *
+ * This wrapper around \ref score_loop() is the playlist counterpart of \ref
+ * mood_loop().
+ *
+ * \param pi Determines the score table to iterate. Must not be NULL.
+ * \param func See \ref score_loop().
+ * \param data See \ref score_loop().
+ *
+ * \return See \ref score_loop(), \ref mood_loop().
+ */
+int playlist_loop(struct playlist_instance *pi, osl_rbtree_loop_func *func, void *data)
+{
+       return score_loop(func, pi->score_table, data);
+}
+
 static int search_path(char *path, void *data)
 {
        if (strcmp(path, data))
@@ -196,7 +247,7 @@ static int handle_audio_file_event(enum afs_events event, void *data)
        }
        /* !was_admissible && is_admissible */
        current_playlist.length++;
-       return score_add(row, 0); /* play it immediately */
+       return score_add(row, 0, NULL); /* play it immediately */
 }
 
 /**
diff --git a/score.c b/score.c
index 10cd254a8dba2926614d0a76ffa29cad90e74792..c03e3472da306a1d535b201ea2d23c90ccf8f90a 100644 (file)
--- a/score.c
+++ b/score.c
@@ -77,10 +77,10 @@ static struct osl_table_description score_table_desc = {
 };
 
 /* On errors (negative return value) the content of score is undefined. */
-static int get_score_of_row(void *score_row, long *score)
+static int get_score_of_row(struct osl_table *t, void *score_row, long *score)
 {
        struct osl_object obj;
-       int ret = osl(osl_get_object(score_table, score_row, SCORECOL_SCORE, &obj));
+       int ret = osl(osl_get_object(t, score_row, SCORECOL_SCORE, &obj));
 
        if (ret >= 0)
                *score = *(long *)obj.data;
@@ -88,14 +88,15 @@ static int get_score_of_row(void *score_row, long *score)
 }
 
 /**
- * Add an entry to the table of admissible files.
+ * Add a (row, score) pair to the score table.
  *
- * \param aft_row The audio file to be added.
- * \param score The score for this file.
+ * \param aft_row Identifies the audio file to be added.
+ * \param score The score value of the audio file.
+ * \param t NULL means to operate on the currently active table.
  *
  * \return The return value of the underlying call to osl_add_row().
  */
-int score_add(const struct osl_row *aft_row, long score)
+int score_add(const struct osl_row *aft_row, long score, struct osl_table *t)
 {
        int ret;
        struct osl_object score_objs[NUM_SCORE_COLUMNS];
@@ -112,7 +113,7 @@ int score_add(const struct osl_row *aft_row, long score)
        *(long *)(score_objs[SCORECOL_SCORE].data) = score;
 
 //     PARA_DEBUG_LOG("adding %p\n", *(void **) (score_objs[SCORECOL_AFT_ROW].data));
-       ret = osl(osl_add_row(score_table, score_objs));
+       ret = osl(osl_add_row(t? t : score_table, score_objs));
        if (ret < 0) {
                PARA_ERROR_LOG("%s\n", para_strerror(-ret));
                free(score_objs[SCORECOL_SCORE].data);
@@ -152,7 +153,7 @@ int score_update(const struct osl_row *aft_row, long percent)
        ret = osl(osl_get_nth_row(score_table, SCORECOL_SCORE, new_pos, &rrow));
        if (ret < 0)
                return ret;
-       ret = get_score_of_row(rrow, &new_score);
+       ret = get_score_of_row(score_table, rrow, &new_score);
        if (ret < 0)
                return ret;
        new_score--;
@@ -176,7 +177,7 @@ int get_score_and_aft_row(struct osl_row *score_row, long *score,
                struct osl_row **aft_row)
 {
        struct osl_object obj;
-       int ret = get_score_of_row(score_row, score);
+       int ret = get_score_of_row(score_table, score_row, score);
 
        if (ret < 0)
                return ret;
@@ -187,26 +188,28 @@ int get_score_and_aft_row(struct osl_row *score_row, long *score,
        return 1;
 }
 
-static int get_score_row_from_aft_row(const struct osl_row *aft_row,
-               struct osl_row **score_row)
+static int get_score_row_from_aft_row(struct osl_table *t,
+               const struct osl_row *aft_row, struct osl_row **score_row)
 {
        struct osl_object obj = {.data = (struct osl_row *)aft_row,
                .size = sizeof(aft_row)};
-       return osl(osl_get_row(score_table, SCORECOL_AFT_ROW, &obj, score_row));
+       return osl(osl_get_row(t, SCORECOL_AFT_ROW, &obj, score_row));
 }
 
 /**
  * Call the given function for each row of the score table.
  *
  * \param func Callback, called once per row.
+ * \param t NULL means to use the currently active score table.
  * \param data Passed verbatim to the callback.
  *
  * \return The return value of the underlying call to osl_rbtree_loop(). The
  * loop terminates early if the callback returns negative.
  */
-int score_loop(osl_rbtree_loop_func *func, void *data)
+int score_loop(osl_rbtree_loop_func *func, struct osl_table *t, void *data)
 {
-       return osl(osl_rbtree_loop(score_table, SCORECOL_SCORE, data, func));
+       return osl(osl_rbtree_loop(t? t : score_table, SCORECOL_SCORE, data,
+               func));
 }
 
 /**
@@ -229,7 +232,7 @@ int score_get_best(struct osl_row **aft_row, long *score)
        if (ret < 0)
                return ret;
        *aft_row = obj.data;
-       return get_score_of_row(row, score);
+       return get_score_of_row(score_table, row, score);
 }
 
 /**
@@ -244,7 +247,7 @@ int score_get_best(struct osl_row **aft_row, long *score)
 int score_delete(const struct osl_row *aft_row)
 {
        struct osl_row *score_row;
-       int ret = get_score_row_from_aft_row(aft_row, &score_row);
+       int ret = get_score_row_from_aft_row(score_table, aft_row, &score_row);
 
        if (ret < 0)
                return ret;
@@ -263,7 +266,7 @@ int score_delete(const struct osl_row *aft_row)
 bool row_belongs_to_score_table(const struct osl_row *aft_row)
 {
        struct osl_row *score_row;
-       int ret = get_score_row_from_aft_row(aft_row, &score_row);
+       int ret = get_score_row_from_aft_row(score_table, aft_row, &score_row);
 
        if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
                return false;
@@ -271,29 +274,56 @@ bool row_belongs_to_score_table(const struct osl_row *aft_row)
        return true;
 }
 
-static void score_close(void)
+/**
+ * Free all volatile objects, then close the table.
+ *
+ * \param t As returned from \ref score_open().
+ *
+ * This either succeeds or terminates the calling process.
+ */
+void score_close(struct osl_table *t)
+{
+       assert(osl_close_table(t? t : score_table, OSL_FREE_VOLATILE) >= 0);
+}
+
+static void close_global_table(void)
 {
-       osl_close_table(score_table, OSL_FREE_VOLATILE);
-       score_table = NULL;
+       score_close(NULL);
 }
 
-static int score_open(__a_unused const char *dir)
+static int open_global_table(__a_unused const char *dir)
 {
-       assert(osl_open_table(&score_table_desc, &score_table) >= 0);
+       assert(osl(osl_open_table(&score_table_desc, &score_table)) >= 0);
        return 1;
 }
 
+/**
+ * Allocate a score table instance.
+ *
+ * \param result NULL means to open the currently active score table.
+ *
+ * Since the score table does no filesystem I/O, this function always succeeds.
+ * \sa \ref score_close().
+ */
+void score_open(struct osl_table **result)
+{
+       if (result)
+               assert(osl(osl_open_table(&score_table_desc, result)) >= 0);
+       else
+               open_global_table(NULL);
+}
+
 /**
  * Remove all entries from the score table, but keep the table open.
  */
 void score_clear(void)
 {
-       score_close();
-       score_open(NULL);
+       close_global_table();
+       open_global_table(NULL);
 }
 
 /** The score table stores (aft row, score) pairs in memory. */
 const struct afs_table_operations score_ops = {
-       .open = score_open,
-       .close = score_close,
+       .open = open_global_table,
+       .close = close_global_table,
 };