]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
afs: Introduce flush_and_free_pb().
authorAndre Noll <maan@tuebingen.mpg.de>
Sun, 26 Apr 2015 16:58:22 +0000 (18:58 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Wed, 12 Aug 2015 21:23:47 +0000 (23:23 +0200)
This kills some code which is duplicated in all command handlers.

afs.c
afs.h
aft.c
attribute.c
blob.c
mood.c
playlist.c

diff --git a/afs.c b/afs.c
index eb8480890d5db6e75077b4f6d81d769b7de9db4d..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 = {
@@ -602,9 +616,7 @@ static void com_select_callback(int fd, const struct osl_object *query)
                        current_mop?  current_mop : "dummy mood",
                        num_admissible);
 out:
-       if (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)
@@ -1022,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)
diff --git a/afs.h b/afs.h
index cccb143f5bdfa1e4aaa2cdcaa1d1e73e6780f119..feada5b622c642ee7ae9fffd243551ffce2dc2f8 100644 (file)
--- a/afs.h
+++ b/afs.h
@@ -223,6 +223,7 @@ int send_standard_callback_request(int argc,  char * const * const argv,
                void *private_result_data);
 int string_compare(const struct osl_object *obj1, const struct osl_object *obj2);
 int for_each_matching_row(struct pattern_match_data *pmd);
+void flush_and_free_pb(struct para_buffer *pb);
 
 /* score */
 void score_init(struct afs_table *t);
diff --git a/aft.c b/aft.c
index 42153d46b09bcdc4198d4564290a51cc2f636ebe..16de67634677c4ba75774bc9453ae427f4581a13 100644 (file)
--- a/aft.c
+++ b/aft.c
@@ -1344,9 +1344,7 @@ static void com_ls_callback(int fd, const struct osl_object *query)
                                goto out;
                }
 out:
-       if (b.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, b.buf, b.offset);
-       free(b.buf);
+       flush_and_free_pb(&b);
        free(opts->data);
        free(opts->data_ptr);
        free(opts->patterns);
@@ -1739,9 +1737,7 @@ static void com_add_callback(int fd, const struct osl_object *query)
 out:
        if (ret < 0)
                para_printf(&msg, "%s\n", para_strerror(-ret));
-       if (msg.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, msg.buf, msg.offset);
-       free(msg.buf);
+       flush_and_free_pb(&msg);
 }
 
 /** Used by com_add(). */
@@ -2042,9 +2038,7 @@ static void com_touch_callback(int fd, const struct osl_object *query)
                para_printf(&tad.pb, "%s\n", para_strerror(-ret));
        else if (pmd.num_matches == 0)
                para_printf(&tad.pb, "no matches\n");
-       if (tad.pb.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, tad.pb.buf, tad.pb.offset);
-       free(tad.pb.buf);
+       flush_and_free_pb(&tad.pb);
 }
 
 int com_touch(struct command_context *cc)
@@ -2187,9 +2181,7 @@ static void com_rm_callback(int fd, const struct osl_object *query)
                para_printf(&crd.pb, "no matches -- nothing removed\n");
        else if (crd.flags & RM_FLAG_VERBOSE)
                para_printf(&crd.pb, "removed %u files\n", pmd.num_matches);
-       if (ret >= 0 && crd.pb.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, crd.pb.buf, crd.pb.offset);
-       free(crd.pb.buf);
+       flush_and_free_pb(&crd.pb);
 }
 
 /* TODO options: -r (recursive) */
@@ -2332,9 +2324,7 @@ out:
                                "to %u files\n", source_path, pmd.num_matches);
        } else
                para_printf(&cad.pb, "no matches - nothing copied\n");
-       if (cad.pb.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, cad.pb.buf, cad.pb.offset);
-       free(cad.pb.buf);
+       flush_and_free_pb(&cad.pb);
 }
 
 int com_cpsi(struct command_context *cc)
@@ -2479,9 +2469,7 @@ static void com_setatt_callback(int fd, const struct osl_object *query)
 out:
        if (ret < 0)
                para_printf(&cad.pb, "%s\n", para_strerror(-ret));
-       if (cad.pb.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, cad.pb.buf, cad.pb.offset);
-       free(cad.pb.buf);
+       flush_and_free_pb(&cad.pb);
 }
 
 int com_setatt(struct command_context *cc)
@@ -2582,9 +2570,7 @@ void aft_check_callback(int fd, __a_unused const struct osl_object *query)
        };
        para_printf(&pb, "checking audio file table...\n");
        audio_file_loop(&pb, check_audio_file);
-       if (pb.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
-       free(pb.buf);
+       flush_and_free_pb(&pb);
 }
 
 /**
index 435f22b5aed560f134aae6b2c9520789c39c3b36..0c23addf3228fb753f5ded3b49dafb45f4d5823e 100644 (file)
@@ -178,9 +178,7 @@ static void com_lsatt_callback(int fd, const struct osl_object *query)
        if (laad.flags & LSATT_FLAG_REVERSE)
                pmd.pm_flags |= PM_REVERSE_LOOP;
        for_each_matching_row(&pmd);
-       if (laad.pb.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, laad.pb.buf, laad.pb.offset);
-       free(laad.pb.buf);
+       flush_and_free_pb(&laad.pb);
 }
 
 int com_lsatt(struct command_context *cc)
@@ -286,9 +284,7 @@ static void com_addatt_callback(int fd, const struct osl_object *query)
 out:
        if (ret < 0)
                para_printf(&pb, "%s: %s\n", p, para_strerror(-ret));
-       if (pb.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
-       free(pb.buf);
+       flush_and_free_pb(&pb);
 }
 
 int com_addatt(struct command_context *cc)
@@ -332,9 +328,7 @@ out:
                para_printf(&pb, "%s\n", para_strerror(-ret));
        else
                afs_event(ATTRIBUTE_RENAME, &pb, NULL);
-       if (pb.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
-       free(pb.buf);
+       flush_and_free_pb(&pb);
 }
 
 int com_mvatt(struct command_context *cc)
@@ -412,9 +406,7 @@ static void com_rmatt_callback(int fd, const struct osl_object *query)
                para_printf(&raad.pb, "%s\n", para_strerror(-ret));
        else if (!raad.num_removed)
                para_printf(&raad.pb, "no match -- nothing removed\n");
-       if (raad.pb.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, raad.pb.buf, raad.pb.offset);
-       free(raad.pb.buf);
+       flush_and_free_pb(&raad.pb);
 }
 
 int com_rmatt(struct command_context *cc)
diff --git a/blob.c b/blob.c
index 4f4a034ad2d60c2e67856050671713738459c828..47253b5e08811c7fbb4a2c2aa12e6f368307454c 100644 (file)
--- a/blob.c
+++ b/blob.c
@@ -166,9 +166,7 @@ static void com_lsblob_callback(struct osl_table *table,
                para_printf(&lbad.pb, "%s\n", para_strerror(-ret));
        else if (pmd.num_matches == 0 && pmd.patterns.size > 0)
                para_printf(&lbad.pb, "no matches\n");
-       if (lbad.pb.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, lbad.pb.buf, lbad.pb.offset);
-       free(lbad.pb.buf);
+       flush_and_free_pb(&lbad.pb);
 }
 
 static int com_lsblob(callback_function *f, struct command_context *cc)
@@ -297,9 +295,7 @@ static void com_rmblob_callback(struct osl_table *table, int fd,
                para_printf(&rmbd.pb, "removed %d blobs\n", pmd.num_matches);
                afs_event(BLOB_RENAME, NULL, table);
        }
-       if (rmbd.pb.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, rmbd.pb.buf, rmbd.pb.offset);
-       free(rmbd.pb.buf);
+       flush_and_free_pb(&rmbd.pb);
 }
 
 static int com_rmblob(callback_function *f, struct command_context *cc)
@@ -499,9 +495,7 @@ static void com_mvblob_callback(struct osl_table *table, int fd,
        }
        afs_event(BLOB_RENAME, NULL, table);
 out:
-       if (pb.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
-       free(pb.buf);
+       flush_and_free_pb(&pb);
 }
 
 static int com_mvblob(callback_function *f, struct command_context *cc)
diff --git a/mood.c b/mood.c
index cbb2f438ef7d41ce22f33d6c4c927cc1227d9963..13b5b1807a7e2705ff2a8e28561550274371150a 100644 (file)
--- a/mood.c
+++ b/mood.c
@@ -447,9 +447,7 @@ void mood_check_callback(int fd, __a_unused const struct osl_object *query)
        para_printf(&pb, "checking moods...\n");
        osl_rbtree_loop(moods_table, BLOBCOL_ID, &pb,
                check_mood);
-       if (pb.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
-       free(pb.buf);
+       flush_and_free_pb(&pb);
 }
 
 static int64_t normalized_value(int64_t x, int64_t n, int64_t sum, int64_t qd)
index 46333a00eef684cac5c3f5812811551875e8ad2a..2c5b67777e2f0b41671bc436e3ef7b02f83e6740 100644 (file)
@@ -140,9 +140,7 @@ void playlist_check_callback(int fd, __a_unused const struct osl_object *query)
        para_printf(&pb, "checking playlists...\n");
        osl_rbtree_loop(playlists_table, BLOBCOL_ID, &pb,
                check_playlist);
-       if (pb.offset)
-               pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
-       free(pb.buf);
+       flush_and_free_pb(&pb);
 }
 
 /**