]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - afs.c
afs: Introduce flush_and_free_pb().
[paraslash.git] / afs.c
diff --git a/afs.c b/afs.c
index 5fa76adcc1bf5e4a5ff93b5b2f22e1345da805c7..0b2a4765b16f82fbde4674ac230bda4d2b5f9e18 100644 (file)
--- a/afs.c
+++ b/afs.c
@@ -564,6 +564,20 @@ int afs_cb_result_handler(struct osl_object *result, uint8_t band,
        return send_sb(&cc->scc, result->data, result->size, band, true);
 }
 
+void flush_and_free_pb(struct para_buffer *pb)
+{
+       int ret;
+       struct afs_max_size_handler_data *amshd = pb->private_data;
+
+       if (pb->buf && pb->size > 0) {
+               ret = pass_buffer_as_shm(amshd->fd, amshd->band, pb->buf,
+                       pb->offset);
+               if (ret < 0)
+                       PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+       }
+       free(pb->buf);
+}
+
 static void com_select_callback(int fd, const struct osl_object *query)
 {
        struct para_buffer pb = {
@@ -575,11 +589,11 @@ static void com_select_callback(int fd, const struct osl_object *query)
                .max_size_handler = afs_max_size_handler,
        };
        char *arg = query->data;
-       int num_admissible, ret, ret2;
+       int num_admissible, ret;
 
        ret = clear_score_table();
        if (ret < 0) {
-               ret2 = para_printf(&pb, "%s\n", para_strerror(-ret));
+               para_printf(&pb, "%s\n", para_strerror(-ret));
                goto out;
        }
        if (current_play_mode == PLAY_MODE_MOOD)
@@ -588,23 +602,21 @@ static void com_select_callback(int fd, const struct osl_object *query)
                playlist_close();
        ret = activate_mood_or_playlist(arg, &num_admissible);
        if (ret < 0) {
-               ret2 = para_printf(&pb, "%s\nswitching back to %s\n",
+               para_printf(&pb, "%s\nswitching back to %s\n",
                        para_strerror(-ret), current_mop?
                        current_mop : "dummy");
                ret = activate_mood_or_playlist(current_mop, &num_admissible);
                if (ret < 0) {
-                       if (ret2 >= 0)
-                               ret2 = para_printf(&pb, "failed, switching to dummy\n");
+                       para_printf(&pb, "failed (%s), switching to dummy\n",
+                               para_strerror(-ret));
                        activate_mood_or_playlist(NULL, &num_admissible);
                }
        } else
-               ret2 = para_printf(&pb, "activated %s (%d admissible files)\n",
+               para_printf(&pb, "activated %s (%d admissible files)\n",
                        current_mop?  current_mop : "dummy mood",
                        num_admissible);
 out:
-       if (ret2 >= 0 && pb.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
-       free(pb.buf);
+       flush_and_free_pb(&pb);
 }
 
 int com_select(struct command_context *cc)
@@ -820,11 +832,6 @@ err:
        return ret;
 }
 
-/*
- * On errors, negative value is written to fd.
- * On success: If query produced a result, the result_shmid is written to fd.
- * Otherwise, zero is written.
- */
 static int call_callback(int fd, int query_shmid)
 {
        void *query_shm;
@@ -1027,9 +1034,7 @@ static void create_tables_callback(int fd, const struct osl_object *query)
 out:
        if (ret < 0)
                para_printf(&pb, "%s\n", para_strerror(-ret));
-       if (pb.buf)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
-       free(pb.buf);
+       flush_and_free_pb(&pb);
 }
 
 int com_init(struct command_context *cc)