]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - aft.c
make send_callback_request() and friends take a result handler.
[paraslash.git] / aft.c
diff --git a/aft.c b/aft.c
index 0efd285c2eb716df2aeeb47e4dfd06aa1d293e5f..d6abc13d9c22854f7a2f12092f5a79e70998231c 100644 (file)
--- a/aft.c
+++ b/aft.c
@@ -835,13 +835,13 @@ static int print_chunk_table(struct ls_data *d, struct para_buffer *b)
                AFTCOL_CHUNKS, &chunk_table_obj);
        if (ret < 0)
                return ret;
-       buf = chunk_table_obj.data;
        para_printf(b, "%s\n"
                "chunk_time: %lu:%lu\nchunk_offsets: ",
                d->path,
                (long unsigned) d->afhi.chunk_tv.tv_sec,
                (long unsigned) d->afhi.chunk_tv.tv_usec
        );
+       buf = chunk_table_obj.data;
        for (i = 0; i <= d->afhi.chunks_total; i++)
                para_printf(b, "%u ",
                        (unsigned) read_u32(buf + 4 * i));
@@ -1284,7 +1284,7 @@ static int com_ls_callback(const struct osl_object *query,
        ret = 1;
 out:
        ls_output->data = b.buf;
-       ls_output->size = b.size;
+       ls_output->size = b.offset;
        free(opts->data);
        free(opts->data_ptr);
        free(opts->patterns);
@@ -1301,8 +1301,7 @@ int com_ls(int fd, int argc, char * const * const argv)
        enum ls_sorting_method sort = LS_SORT_BY_PATH;
        enum ls_listing_mode mode = LS_MODE_SHORT;
        struct ls_options opts = {.patterns = NULL};
-       struct osl_object query = {.data = &opts, .size = sizeof(opts)},
-               ls_output;
+       struct osl_object query = {.data = &opts, .size = sizeof(opts)};
 
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
@@ -1400,11 +1399,7 @@ int com_ls(int fd, int argc, char * const * const argv)
        opts.mode = mode;
        opts.num_patterns = argc - i;
        ret = send_option_arg_callback_request(&query, opts.num_patterns,
-               argv + i, com_ls_callback, &ls_output);
-       if (ret > 0) {
-               ret = send_buffer(fd, (char *)ls_output.data);
-               free(ls_output.data);
-       }
+               argv + i, com_ls_callback, send_result, &fd);
        return ret;
 }
 
@@ -1671,7 +1666,7 @@ out:
        if (!msg.buf)
                return 0;
        result->data = msg.buf;
-       result->size = msg.size;
+       result->size = msg.offset;
        return 1;
 }
 
@@ -1712,30 +1707,32 @@ static int hash_sister_callback(const struct osl_object *query,
        return 1;
 }
 
-static int add_one_audio_file(const char *path, const void *private_data)
+int get_row_pointer_from_result(struct osl_object *result, void *private)
+{
+       struct osl_row **row = private;
+       *row = result->data;
+       return 1;
+}
+
+static int add_one_audio_file(const char *path, void *private_data)
 {
        int ret, send_ret = 1;
        uint8_t format_num = -1;
-       const struct private_add_data *pad = private_data;
+       struct private_add_data *pad = private_data;
        struct afh_info afhi, *afhi_ptr = NULL;
        struct osl_row *pb = NULL, *hs = NULL; /* path brother/hash sister */
-       struct osl_object map, obj = {.data = NULL}, query, result = {.data = NULL};
+       struct osl_object map, obj = {.data = NULL}, query;
        HASH_TYPE hash[HASH_SIZE];
 
-       afhi.header_offset = 0;
-       afhi.header_len = 0;
        ret = guess_audio_format(path);
        if (ret < 0 && !(pad->flags & ADD_FLAG_ALL))
                goto out_free;
        query.data = (char *)path;
        query.size = strlen(path) + 1;
-       ret = send_callback_request(path_brother_callback, &query, &result);
+       ret = send_callback_request(path_brother_callback, &query,
+               get_row_pointer_from_result, &pb);
        if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
                goto out_free;
-       if (ret >= 0) {
-               pb = *(struct osl_row **)result.data;
-               free(result.data);
-       }
        ret = 1;
        if (pb && (pad->flags & ADD_FLAG_LAZY)) { /* lazy is really cheap */
                if (pad->flags & ADD_FLAG_VERBOSE)
@@ -1751,13 +1748,10 @@ static int add_one_audio_file(const char *path, const void *private_data)
        /* Check whether the database contains a file with the same hash. */
        query.data = hash;
        query.size = HASH_SIZE;
-       ret = send_callback_request(hash_sister_callback, &query, &result);
+       ret = send_callback_request(hash_sister_callback, &query,
+               get_row_pointer_from_result, &hs);
        if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
                goto out_unmap;
-       if (ret >= 0) {
-               hs = *(struct osl_row **)result.data;
-               free(result.data);
-       }
        /* Return success if we already know this file. */
        ret = 1;
        if (pb && hs && hs == pb && !(pad->flags & ADD_FLAG_FORCE)) {
@@ -1785,11 +1779,7 @@ static int add_one_audio_file(const char *path, const void *private_data)
        }
        save_add_callback_buffer(hash, path, afhi_ptr, pad->flags, format_num, &obj);
        /* Ask afs to consider this entry for adding. */
-       ret = send_callback_request(com_add_callback, &obj, &result);
-       if (ret > 0) {
-               send_ret = send_va_buffer(pad->fd, "%s", (char *)result.data);
-               free(result.data);
-       }
+       ret = send_callback_request(com_add_callback, &obj, send_result, &pad->fd);
        goto out_free;
 
 out_unmap:
@@ -1981,7 +1971,7 @@ static int com_touch_callback(const struct osl_object *query,
                        para_printf(&tad.pb, "no matches\n");
        if (tad.pb.buf) {
                result->data = tad.pb.buf;
-               result->size = tad.pb.size;
+               result->size = tad.pb.offset;
                return 1;
        }
        return ret < 0? ret : 0;
@@ -1995,8 +1985,7 @@ int com_touch(int fd, int argc, char * const * const argv)
                .lyrics_id = -1,
                .image_id = -1
        };
-       struct osl_object query = {.data = &cto, .size = sizeof(cto)},
-               result;
+       struct osl_object query = {.data = &cto, .size = sizeof(cto)};
        int i, ret;
 
 
@@ -2045,15 +2034,9 @@ int com_touch(int fd, int argc, char * const * const argv)
        if (i >= argc)
                return -E_AFT_SYNTAX;
        ret = send_option_arg_callback_request(&query, argc - i,
-               argv + i, com_touch_callback, &result);
-       if (!ret)
-               return 0;
-       if (ret < 0) {
+               argv + i, com_touch_callback, send_result, &fd);
+       if (ret < 0)
                send_va_buffer(fd, "%s\n", para_strerror(-ret));
-               return ret;
-       }
-       ret = send_buffer(fd, (char *)result.data);
-       free(result.data);
        return ret;
 }
 
@@ -2095,7 +2078,7 @@ static int remove_audio_file(__a_unused struct osl_table *table,
 }
 
 static int com_rm_callback(const struct osl_object *query,
-               __a_unused struct osl_object *result)
+               struct osl_object *result)
 {
        struct com_rm_action_data crd = {.flags = *(uint32_t *)query->data};
        int ret;
@@ -2121,7 +2104,7 @@ static int com_rm_callback(const struct osl_object *query,
        }
        if (crd.pb.buf) {
                result->data = crd.pb.buf;
-               result->size = crd.pb.size;
+               result->size = crd.pb.offset;
                return 1;
        }
        return ret < 0? ret : 0;
@@ -2131,8 +2114,7 @@ static int com_rm_callback(const struct osl_object *query,
 int com_rm(int fd, int argc,  char * const * const argv)
 {
        uint32_t flags = 0;
-       struct osl_object query = {.data = &flags, .size = sizeof(flags)},
-               result;
+       struct osl_object query = {.data = &flags, .size = sizeof(flags)};
        int i, ret;
 
        for (i = 1; i < argc; i++) {
@@ -2160,15 +2142,9 @@ int com_rm(int fd, int argc,  char * const * const argv)
        if (i >= argc)
                return -E_AFT_SYNTAX;
        ret = send_option_arg_callback_request(&query, argc - i, argv + i,
-               com_rm_callback, &result);
-       if (!ret)
-               return 0;
-       if (ret < 0) {
+               com_rm_callback, send_result, &fd);
+       if (ret < 0)
                send_va_buffer(fd, "%s\n", para_strerror(-ret));
-               return ret;
-       }
-       ret = send_buffer(fd, (char *)result.data);
-       free(result.data);
        return ret;
 }
 
@@ -2272,7 +2248,7 @@ out:
        }
        if (cad.pb.buf) {
                result->data = cad.pb.buf;
-               result->size = cad.pb.size;
+               result->size = cad.pb.offset;
                return 1;
        }
        return ret < 0? ret : 0;
@@ -2282,8 +2258,7 @@ int com_cpsi(int fd, int argc,  char * const * const argv)
 {
        unsigned flags = 0;
        int i, ret;
-       struct osl_object options = {.data = &flags, .size = sizeof(flags)},
-               result;
+       struct osl_object options = {.data = &flags, .size = sizeof(flags)};
 
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
@@ -2324,15 +2299,9 @@ int com_cpsi(int fd, int argc,  char * const * const argv)
        if (!(flags & ~CPSI_FLAG_VERBOSE)) /* no copy flags given */
                flags = ~(unsigned)CPSI_FLAG_VERBOSE | flags;
        ret = send_option_arg_callback_request(&options, argc - i, argv + i,
-               com_cpsi_callback, &result);
-       if (!ret)
-               return 0;
-       if (ret < 0) {
+               com_cpsi_callback, send_result, &fd);
+       if (ret < 0)
                send_va_buffer(fd, "%s\n", para_strerror(-ret));
-               return ret;
-       }
-       ret = send_buffer(fd, (char *)result.data);
-       free(result.data);
        return ret;
 }
 
@@ -2389,7 +2358,7 @@ int aft_check_callback(__a_unused const struct osl_object *query, struct osl_obj
        para_printf(&pb, "checking audio file table...\n");
        audio_file_loop(&pb, check_audio_file);
        result->data = pb.buf;
-       result->size = pb.size;
+       result->size = pb.offset;
        return 1;
 
 }