]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - aft.c
Let afs callbacks return an error code.
[paraslash.git] / aft.c
diff --git a/aft.c b/aft.c
index 42153d46b09bcdc4198d4564290a51cc2f636ebe..acb6c6bf879f838976652ca3c48cfbf3ff75f70c 100644 (file)
--- a/aft.c
+++ b/aft.c
@@ -1291,7 +1291,7 @@ err:
        return ret;
 }
 
-static void com_ls_callback(int fd, const struct osl_object *query)
+static int com_ls_callback(int fd, const struct osl_object *query)
 {
        struct ls_options *opts = query->data;
        char *p, *pattern_start = (char *)query->data + sizeof(*opts);
@@ -1344,12 +1344,11 @@ 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);
+       return 0;
 }
 
 /*
@@ -1614,7 +1613,7 @@ enum com_add_flags {
        ADD_FLAG_ALL = 8,
 };
 
-static void com_add_callback(int fd, const struct osl_object *query)
+static int com_add_callback(int fd, const struct osl_object *query)
 {
        char *buf = query->data, *path;
        struct osl_row *pb, *aft_row;
@@ -1739,9 +1738,8 @@ 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);
+       return 0;
 }
 
 /** Used by com_add(). */
@@ -1752,26 +1750,26 @@ struct private_add_data {
        uint32_t flags;
 };
 
-static void path_brother_callback(int fd, const struct osl_object *query)
+static int path_brother_callback(int fd, const struct osl_object *query)
 {
        char *path = query->data;
        struct osl_row *path_brother;
        int ret = aft_get_row_of_path(path, &path_brother);
        if (ret < 0)
-               return;
-       pass_buffer_as_shm(fd, SBD_OUTPUT, (char *)&path_brother,
+               return ret;
+       return pass_buffer_as_shm(fd, SBD_OUTPUT, (char *)&path_brother,
                sizeof(path_brother));
 }
 
-static void hash_sister_callback(int fd, const struct osl_object *query)
+static int hash_sister_callback(int fd, const struct osl_object *query)
 {
        unsigned char *hash = query->data;
        struct osl_row *hash_sister;
 
        hash_sister = find_hash_sister(hash);
        if (!hash_sister)
-               return;
-       pass_buffer_as_shm(fd, SBD_OUTPUT, (char *)&hash_sister,
+               return 0;
+       return pass_buffer_as_shm(fd, SBD_OUTPUT, (char *)&hash_sister,
                sizeof(hash_sister));
 }
 
@@ -2013,7 +2011,7 @@ static int touch_audio_file(__a_unused struct osl_table *table,
        return 1;
 }
 
-static void com_touch_callback(int fd, const struct osl_object *query)
+static int com_touch_callback(int fd, const struct osl_object *query)
 {
        struct touch_action_data tad = {.cto = query->data,
                .pb = {
@@ -2042,9 +2040,8 @@ 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);
+       return 0;
 }
 
 int com_touch(struct command_context *cc)
@@ -2154,7 +2151,7 @@ static int remove_audio_file(__a_unused struct osl_table *table,
        return ret;
 }
 
-static void com_rm_callback(int fd, const struct osl_object *query)
+static int com_rm_callback(int fd, const struct osl_object *query)
 {
        struct com_rm_action_data crd = {.flags = *(uint32_t *)query->data,
                .pb = {
@@ -2181,15 +2178,14 @@ static void com_rm_callback(int fd, const struct osl_object *query)
        ret = for_each_matching_row(&pmd);
        if (ret < 0) {
                para_printf(&crd.pb, "%s\n", para_strerror(-ret));
-               return;
+               return 0;
        }
        if ((pmd.num_matches == 0) && !(crd.flags & RM_FLAG_FORCE))
                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);
+       return 0;
 }
 
 /* TODO options: -r (recursive) */
@@ -2293,7 +2289,7 @@ static int copy_selector_info(__a_unused struct osl_table *table,
        return 1;
 }
 
-static void com_cpsi_callback(int fd, const struct osl_object *query)
+static int com_cpsi_callback(int fd, const struct osl_object *query)
 {
        struct cpsi_action_data cad = {
                .flags = *(unsigned *)query->data,
@@ -2332,9 +2328,8 @@ 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);
+       return 0;
 }
 
 int com_cpsi(struct command_context *cc)
@@ -2419,7 +2414,7 @@ static int change_atts(__a_unused struct osl_table *table,
        return 1;
 }
 
-static void com_setatt_callback(int fd, const struct osl_object *query)
+static int com_setatt_callback(int fd, const struct osl_object *query)
 {
        char *p;
        int ret;
@@ -2479,9 +2474,8 @@ 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);
+       return 0;
 }
 
 int com_setatt(struct command_context *cc)
@@ -2492,15 +2486,15 @@ int com_setatt(struct command_context *cc)
                com_setatt_callback, afs_cb_result_handler, cc);
 }
 
-static void afs_stat_callback(int fd, const struct osl_object *query)
+static int afs_stat_callback(int fd, const struct osl_object *query)
 {
        int *parser_friendly = query->data;
        char *buf = *parser_friendly?
                parser_friendly_status_items : status_items;
 
        if (!buf)
-               return;
-       pass_buffer_as_shm(fd, SBD_OUTPUT, buf, strlen(buf));
+               return 0;
+       return pass_buffer_as_shm(fd, SBD_OUTPUT, buf, strlen(buf));
 }
 
 /**
@@ -2566,11 +2560,11 @@ static int check_audio_file(struct osl_row *row, void *data)
  * \param fd The afs socket.
  * \param query Unused.
  *
- * This function always succeeds.
+ * \return This function always returns zero.
  *
  * \sa com_check().
  */
-void aft_check_callback(int fd, __a_unused const struct osl_object *query)
+int aft_check_callback(int fd, __a_unused const struct osl_object *query)
 {
        struct para_buffer pb = {
                .max_size = shm_get_shmmax(),
@@ -2582,9 +2576,8 @@ 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);
+       return 0;
 }
 
 /**