From 050003ccbd8b314038352f55d9fae53245670e78 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 4 Apr 2015 21:45:11 +0000 Subject: [PATCH] Let afs callbacks return an error code. It was a design mistake that callbacks have no way to tell whether they were successful. This commit changes the callback_function typedef so that callbacks return int instead of void. Naturally, every callback must be adjusted accordingly. Doing so would make the patch a bit large, so as a first step we make all callbacks (except path_brother_callback() and hash_sister_callback() which are special) return zero. The return value is ignored at the moment, so the changes of this commit have no effect yet. --- afs.c | 6 ++++-- afs.h | 6 +++--- aft.c | 43 +++++++++++++++++++++++++------------------ attribute.c | 12 ++++++++---- blob.c | 17 +++++++++++------ mood.c | 5 ++++- mood.h | 2 +- playlist.c | 5 ++++- 8 files changed, 60 insertions(+), 36 deletions(-) diff --git a/afs.c b/afs.c index 0b2a4765..24e737b6 100644 --- a/afs.c +++ b/afs.c @@ -578,7 +578,7 @@ void flush_and_free_pb(struct para_buffer *pb) free(pb->buf); } -static void com_select_callback(int fd, const struct osl_object *query) +static int com_select_callback(int fd, const struct osl_object *query) { struct para_buffer pb = { .max_size = shm_get_shmmax(), @@ -617,6 +617,7 @@ static void com_select_callback(int fd, const struct osl_object *query) num_admissible); out: flush_and_free_pb(&pb); + return 0; } int com_select(struct command_context *cc) @@ -1005,7 +1006,7 @@ out: exit(EXIT_FAILURE); } -static void create_tables_callback(int fd, const struct osl_object *query) +static int create_tables_callback(int fd, const struct osl_object *query) { uint32_t table_mask = *(uint32_t *)query->data; int i, ret; @@ -1035,6 +1036,7 @@ out: if (ret < 0) para_printf(&pb, "%s\n", para_strerror(-ret)); flush_and_free_pb(&pb); + return 0; } int com_init(struct command_context *cc) diff --git a/afs.h b/afs.h index feada5b6..efd7fbac 100644 --- a/afs.h +++ b/afs.h @@ -166,7 +166,7 @@ struct pattern_match_data { * * \sa send_callback_request(). */ -typedef void callback_function(int fd, const struct osl_object *); +typedef int callback_function(int fd, const struct osl_object *); /** * Callbacks send chunks to data back to the command handler. Pointers to @@ -252,12 +252,12 @@ int get_afsi_of_row(const struct osl_row *row, struct afs_info *afsi); int get_afhi_of_row(const struct osl_row *row, struct afh_info *afhi); int get_audio_file_path_of_row(const struct osl_row *row, char **path); int audio_file_loop(void *private_data, osl_rbtree_loop_func *func); -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); /* playlist */ int playlist_open(char *name); void playlist_close(void); -void playlist_check_callback(int fd, __a_unused const struct osl_object *query); +int playlist_check_callback(int fd, __a_unused const struct osl_object *query); /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */ #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y))) diff --git a/aft.c b/aft.c index 16de6763..acb6c6bf 100644 --- 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); @@ -1348,6 +1348,7 @@ out: free(opts->data); free(opts->data_ptr); free(opts->patterns); + return 0; } /* @@ -1612,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; @@ -1738,6 +1739,7 @@ out: if (ret < 0) para_printf(&msg, "%s\n", para_strerror(-ret)); flush_and_free_pb(&msg); + return 0; } /** Used by com_add(). */ @@ -1748,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)); } @@ -2009,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 = { @@ -2039,6 +2041,7 @@ static void com_touch_callback(int fd, const struct osl_object *query) else if (pmd.num_matches == 0) para_printf(&tad.pb, "no matches\n"); flush_and_free_pb(&tad.pb); + return 0; } int com_touch(struct command_context *cc) @@ -2148,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 = { @@ -2175,13 +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); flush_and_free_pb(&crd.pb); + return 0; } /* TODO options: -r (recursive) */ @@ -2285,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, @@ -2325,6 +2329,7 @@ out: } else para_printf(&cad.pb, "no matches - nothing copied\n"); flush_and_free_pb(&cad.pb); + return 0; } int com_cpsi(struct command_context *cc) @@ -2409,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; @@ -2470,6 +2475,7 @@ out: if (ret < 0) para_printf(&cad.pb, "%s\n", para_strerror(-ret)); flush_and_free_pb(&cad.pb); + return 0; } int com_setatt(struct command_context *cc) @@ -2480,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)); } /** @@ -2554,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(), @@ -2571,6 +2577,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); flush_and_free_pb(&pb); + return 0; } /** diff --git a/attribute.c b/attribute.c index 0c23addf..7da525eb 100644 --- a/attribute.c +++ b/attribute.c @@ -149,7 +149,7 @@ static int print_attribute(struct osl_table *table, struct osl_row *row, return 1; } -static void com_lsatt_callback(int fd, const struct osl_object *query) +static int com_lsatt_callback(int fd, const struct osl_object *query) { struct lsatt_action_data laad = { .flags = *(unsigned *) query->data, @@ -179,6 +179,7 @@ static void com_lsatt_callback(int fd, const struct osl_object *query) pmd.pm_flags |= PM_REVERSE_LOOP; for_each_matching_row(&pmd); flush_and_free_pb(&laad.pb); + return 0; } int com_lsatt(struct command_context *cc) @@ -223,7 +224,7 @@ struct addatt_event_data { }; -static void com_addatt_callback(int fd, const struct osl_object *query) +static int com_addatt_callback(int fd, const struct osl_object *query) { char *p; int ret = 1; @@ -285,6 +286,7 @@ out: if (ret < 0) para_printf(&pb, "%s: %s\n", p, para_strerror(-ret)); flush_and_free_pb(&pb); + return 0; } int com_addatt(struct command_context *cc) @@ -300,7 +302,7 @@ int com_addatt(struct command_context *cc) return ret; } -static void com_mvatt_callback(int fd, const struct osl_object *query) +static int com_mvatt_callback(int fd, const struct osl_object *query) { char *old = query->data; size_t size = strlen(old) + 1; @@ -329,6 +331,7 @@ out: else afs_event(ATTRIBUTE_RENAME, &pb, NULL); flush_and_free_pb(&pb); + return 0; } int com_mvatt(struct command_context *cc) @@ -379,7 +382,7 @@ static int remove_attribute(struct osl_table *table, struct osl_row *row, return 1; } -static void com_rmatt_callback(int fd, const struct osl_object *query) +static int com_rmatt_callback(int fd, const struct osl_object *query) { struct remove_attribute_action_data raad = { .num_removed = 0, @@ -407,6 +410,7 @@ static void com_rmatt_callback(int fd, const struct osl_object *query) else if (!raad.num_removed) para_printf(&raad.pb, "no match -- nothing removed\n"); flush_and_free_pb(&raad.pb); + return 0; } int com_rmatt(struct command_context *cc) diff --git a/blob.c b/blob.c index 47253b5e..5eb125c8 100644 --- a/blob.c +++ b/blob.c @@ -130,7 +130,7 @@ static int print_blob(struct osl_table *table, struct osl_row *row, return 1; } -static void com_lsblob_callback(struct osl_table *table, +static int com_lsblob_callback(struct osl_table *table, int fd, const struct osl_object *query) { struct lsblob_action_data lbad = { @@ -167,6 +167,7 @@ static void com_lsblob_callback(struct osl_table *table, else if (pmd.num_matches == 0 && pmd.patterns.size > 0) para_printf(&lbad.pb, "no matches\n"); flush_and_free_pb(&lbad.pb); + return 0; } static int com_lsblob(callback_function *f, struct command_context *cc) @@ -218,7 +219,7 @@ static int cat_blob(struct osl_table *table, struct osl_row *row, return (ret < 0)? ret : ret2; } -static void com_catblob_callback(struct osl_table *table, int fd, +static int com_catblob_callback(struct osl_table *table, int fd, const struct osl_object *query) { struct pattern_match_data pmd = { @@ -235,6 +236,7 @@ static void com_catblob_callback(struct osl_table *table, int fd, char err_msg[] = "no matches\n"; pass_buffer_as_shm(fd, SBD_OUTPUT, err_msg, sizeof(err_msg)); } + return 0; } static int com_catblob(callback_function *f, struct command_context *cc) @@ -263,7 +265,7 @@ static int remove_blob(struct osl_table *table, struct osl_row *row, return 1; } -static void com_rmblob_callback(struct osl_table *table, int fd, +static int com_rmblob_callback(struct osl_table *table, int fd, const struct osl_object *query) { int ret; @@ -296,6 +298,7 @@ static void com_rmblob_callback(struct osl_table *table, int fd, afs_event(BLOB_RENAME, NULL, table); } flush_and_free_pb(&rmbd.pb); + return 0; } static int com_rmblob(callback_function *f, struct command_context *cc) @@ -306,7 +309,7 @@ static int com_rmblob(callback_function *f, struct command_context *cc) afs_cb_result_handler, cc); } -static void com_addblob_callback(struct osl_table *table, int fd, +static int com_addblob_callback(struct osl_table *table, int fd, const struct osl_object *query) { struct osl_object objs[NUM_BLOB_COLUMNS]; @@ -382,6 +385,7 @@ out: msg_len = xasprintf(&msg, "added %s as id %u\n", name, id); pass_buffer_as_shm(fd, SBD_OUTPUT, msg, msg_len); free(msg); + return 0; } /* Write input from fd to dynamically allocated buffer, but maximal 10M. */ @@ -466,7 +470,7 @@ static int com_addblob(callback_function *f, struct command_context *cc) return stdin_command(cc, &arg_obj, f); } -static void com_mvblob_callback(struct osl_table *table, int fd, +static int com_mvblob_callback(struct osl_table *table, int fd, const struct osl_object *query) { char *src = (char *) query->data; @@ -496,6 +500,7 @@ static void com_mvblob_callback(struct osl_table *table, int fd, afs_event(BLOB_RENAME, NULL, table); out: flush_and_free_pb(&pb); + return 0; } static int com_mvblob(callback_function *f, struct command_context *cc) @@ -512,7 +517,7 @@ static int com_mvblob(callback_function *f, struct command_context *cc) } #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \ - static void com_ ## cmd_name ## cmd_prefix ## _callback(int fd, const struct osl_object *query) \ + static int com_ ## cmd_name ## cmd_prefix ## _callback(int fd, const struct osl_object *query) \ { \ return com_ ## cmd_name ## blob_callback(table_name ## _table, fd, query); \ } \ diff --git a/mood.c b/mood.c index 13b5b180..a98f68b2 100644 --- a/mood.c +++ b/mood.c @@ -432,8 +432,10 @@ out: * * \param fd The afs socket. * \param query Unused. + * + * \return Currently this function always returns zero. */ -void mood_check_callback(int fd, __a_unused const struct osl_object *query) +int mood_check_callback(int fd, __a_unused const struct osl_object *query) { struct para_buffer pb = { .max_size = shm_get_shmmax(), @@ -448,6 +450,7 @@ void mood_check_callback(int fd, __a_unused const struct osl_object *query) osl_rbtree_loop(moods_table, BLOBCOL_ID, &pb, check_mood); flush_and_free_pb(&pb); + return 0; } static int64_t normalized_value(int64_t x, int64_t n, int64_t sum, int64_t qd) diff --git a/mood.h b/mood.h index 2630ba16..4ea10ef8 100644 --- a/mood.h +++ b/mood.h @@ -8,4 +8,4 @@ int change_current_mood(char *mood_name); void close_current_mood(void); -void mood_check_callback(int fd, __a_unused const struct osl_object *query); +int mood_check_callback(int fd, __a_unused const struct osl_object *query); diff --git a/playlist.c b/playlist.c index 2c5b6777..e3569358 100644 --- a/playlist.c +++ b/playlist.c @@ -126,8 +126,10 @@ static int check_playlist(struct osl_row *row, void *data) * * \param fd The afs socket. * \param query Unused. + * + * \return Currently this function always returns zero. */ -void playlist_check_callback(int fd, __a_unused const struct osl_object *query) +int playlist_check_callback(int fd, __a_unused const struct osl_object *query) { struct para_buffer pb = { .max_size = shm_get_shmmax(), @@ -141,6 +143,7 @@ void playlist_check_callback(int fd, __a_unused const struct osl_object *query) osl_rbtree_loop(playlists_table, BLOBCOL_ID, &pb, check_playlist); flush_and_free_pb(&pb); + return 0; } /** -- 2.39.2