From 0229d751f7e4223e81d69bc2d8e9e5b5f3b10a0e Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 5 May 2012 12:10:08 +0200 Subject: [PATCH] Introduce send_strerror(). There are many places in various command handlers where the error path contains code like sc_send_va_buffer(&cc->scc, "%s\n", para_strerror(-ret)); to send an error message to the client. This patch simplifies all these places by introducing the public function send_strerror() which takes a command context structure and an error code, and executes the above statement. All places where this was open-coded are changed to call the new function instead. --- aft.c | 6 +++--- attribute.c | 9 +++++---- command.c | 7 ++++++- command.h | 1 + 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/aft.c b/aft.c index c7246703..2c123ce3 100644 --- a/aft.c +++ b/aft.c @@ -2189,7 +2189,7 @@ int com_touch(struct command_context *cc) ret = send_option_arg_callback_request(&query, cc->argc - i, cc->argv + i, com_touch_callback, afs_cb_result_handler, cc); if (ret < 0) - sc_send_va_buffer(&cc->scc, "%s\n", para_strerror(-ret)); + send_strerror(cc, -ret); return ret; } @@ -2300,7 +2300,7 @@ int com_rm(struct command_context *cc) ret = send_option_arg_callback_request(&query, cc->argc - i, cc->argv + i, com_rm_callback, afs_cb_result_handler, cc); if (ret < 0) - sc_send_va_buffer(&cc->scc, "%s\n", para_strerror(-ret)); + send_strerror(cc, -ret); return ret; } @@ -2459,7 +2459,7 @@ int com_cpsi(struct command_context *cc) ret = send_option_arg_callback_request(&options, cc->argc - i, cc->argv + i, com_cpsi_callback, afs_cb_result_handler, cc); if (ret < 0) - sc_send_va_buffer(&cc->scc, "%s\n", para_strerror(-ret)); + send_strerror(cc, -ret); return ret; } diff --git a/attribute.c b/attribute.c index 8dcfa28f..f4e20120 100644 --- a/attribute.c +++ b/attribute.c @@ -208,11 +208,12 @@ int com_lsatt(struct command_context *cc) } ret = send_option_arg_callback_request(&options, cc->argc - i, cc->argv + i, com_lsatt_callback, afs_cb_result_handler, cc); + if (!ret) { if (cc->argc > 1) ret = sc_send_va_buffer(&cc->scc, "no matches\n"); } else if (ret < 0) - sc_send_va_buffer(&cc->scc, "%s\n", para_strerror(-ret)); + send_strerror(cc, -ret); return ret; } @@ -372,7 +373,7 @@ int com_addatt(struct command_context *cc) ret = send_standard_callback_request(cc->argc - 1, cc->argv + 1, com_addatt_callback, afs_cb_result_handler, cc); if (ret < 0) - sc_send_va_buffer(&cc->scc, "%s\n", para_strerror(-ret)); + send_strerror(cc, -ret); return ret; } @@ -417,7 +418,7 @@ int com_mvatt(struct command_context *cc) ret = send_standard_callback_request(cc->argc - 1, cc->argv + 1, com_mvatt_callback, afs_cb_result_handler, cc); if (ret < 0) - sc_send_va_buffer(&cc->scc, "%s\n", para_strerror(-ret)); + send_strerror(cc, -ret); return ret; } @@ -491,7 +492,7 @@ int com_rmatt(struct command_context *cc) ret = send_standard_callback_request(cc->argc - 1, cc->argv + 1, com_rmatt_callback, afs_cb_result_handler, cc); if (ret < 0) - sc_send_va_buffer(&cc->scc, "%s\n", para_strerror(-ret)); + send_strerror(cc, -ret); return ret; } diff --git a/command.c b/command.c index 9c175f35..82194adf 100644 --- a/command.c +++ b/command.c @@ -256,6 +256,11 @@ __printf_3_4 int send_sb_va(struct stream_cipher_context *scc, int band, return send_sb(scc, msg, ret, band, false); } +int send_strerror(struct command_context *cc, int err) +{ + return sc_send_va_buffer(&cc->scc, "%s\n", para_strerror(err)); +} + /** * Send a sideband packet through a blocking file descriptor. * @@ -1054,7 +1059,7 @@ __noreturn void handle_connect(int fd, const char *peername) if (ret >= 0) goto out; err_out: - sc_send_va_buffer(&cc->scc, "%s\n", para_strerror(-ret)); + send_strerror(cc, -ret); net_err: PARA_NOTICE_LOG("%s\n", para_strerror(-ret)); out: diff --git a/command.h b/command.h index 851b8f13..e4159e6b 100644 --- a/command.h +++ b/command.h @@ -40,6 +40,7 @@ int send_sb(struct stream_cipher_context *scc, void *buf, size_t numbytes, int band, bool dont_free); __printf_3_4 int send_sb_va(struct stream_cipher_context *scc, int band, const char *fmt, ...); +int send_strerror(struct command_context *cc, int err); int recv_sb(struct stream_cipher_context *scc, enum sb_designator expected_band, size_t max_size, struct iovec *result); -- 2.30.2