From b514587011918e35f042fbe1a3e0e946bab3a51b Mon Sep 17 00:00:00 2001 From: Daniel Golle Date: Tue, 28 Nov 2017 03:22:57 +0100 Subject: [PATCH] auc: update to version 0.0.8 * fix multiple read_data_cb calls from uclient, otherwise hickups happend when receiving a large JSON reply * add minimal help message (-h) * pretify package update output * fix infinite retry of upgrade-check which happened in some cases * add sleep in case of check-request retry * lots of cosmetics Signed-off-by: Daniel Golle --- utils/auc/Makefile | 2 +- utils/auc/src/auc.c | 128 ++++++++++++++++++++++++++++++-------------- 2 files changed, 90 insertions(+), 40 deletions(-) diff --git a/utils/auc/Makefile b/utils/auc/Makefile index 5cc328abe..fa8290d3d 100644 --- a/utils/auc/Makefile +++ b/utils/auc/Makefile @@ -5,7 +5,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=auc -PKG_VERSION:=0.0.7 +PKG_VERSION:=0.0.8 PKG_RELEASE=1 PKG_LICENSE:=GPL-3.0 diff --git a/utils/auc/src/auc.c b/utils/auc/src/auc.c index 73d150de1..21c13461d 100644 --- a/utils/auc/src/auc.c +++ b/utils/auc/src/auc.c @@ -13,7 +13,7 @@ */ #define _GNU_SOURCE -#define AUC_VERSION "0.0.7" +#define AUC_VERSION "0.0.8" #include #include @@ -335,8 +335,19 @@ free: return ret; } +struct jsonblobber { + json_tokener *tok; + struct blob_buf *outbuf; +}; + static void request_done(struct uclient *cl) { + struct jsonblobber *jsb = (struct jsonblobber *)cl->priv; + if (jsb) { + json_tokener_free(jsb->tok); + free(jsb); + }; + uclient_disconnect(cl); uloop_end(); } @@ -483,12 +494,12 @@ static void read_data_cb(struct uclient *cl) { char buf[256]; int len; - json_tokener *tok; json_object *jsobj; + struct blob_buf *outbuf = NULL; + json_tokener *tok = NULL; + struct jsonblobber *jsb = (struct jsonblobber *)cl->priv; - struct blob_buf *outbuf = (struct blob_buf *)cl->priv; - - if (!outbuf) { + if (!jsb) { while (1) { len = uclient_read(cl, buf, sizeof(buf)); if (!len) @@ -500,8 +511,9 @@ static void read_data_cb(struct uclient *cl) return; } - tok = json_tokener_new(); - + outbuf = jsb->outbuf; + tok = jsb->tok; + while (1) { len = uclient_read(cl, buf, sizeof(buf)); if (!len) @@ -526,8 +538,6 @@ static void read_data_cb(struct uclient *cl) break; } } - - json_tokener_free(tok); } static void eof_cb(struct uclient *cl) @@ -573,6 +583,7 @@ static const struct uclient_cb check_cb = { static int server_request(const char *url, struct blob_buf *inbuf, struct blob_buf *outbuf) { struct uclient *ucl; + struct jsonblobber *jsb = NULL; int rc = -1; char *post_data; out_offset = 0; @@ -582,9 +593,15 @@ static int server_request(const char *url, struct blob_buf *inbuf, struct blob_b uloop_init(); ucl = uclient_new(url, NULL, &check_cb); + if (outbuf) { + jsb = malloc(sizeof(struct jsonblobber)); + jsb->outbuf = outbuf; + jsb->tok = json_tokener_new(); + }; + uclient_http_set_ssl_ctx(ucl, ssl_ops, ssl_ctx, 1); ucl->timeout_msecs = REQ_TIMEOUT * 1000; - ucl->priv = outbuf; + ucl->priv = jsb; rc = uclient_connect(ucl); if (rc) return rc; @@ -689,8 +706,29 @@ static int ask_user(void) return 0; } +static void print_package_updates(struct blob_attr *upgrades) { + struct blob_attr *cur; + struct blob_attr *tb[2]; + int rem; + + static struct blobmsg_policy policy[2] = { + { .type = BLOBMSG_TYPE_STRING }, + { .type = BLOBMSG_TYPE_STRING }, + }; + + blobmsg_for_each_attr(cur, upgrades, rem) { + blobmsg_parse_array(policy, ARRAY_SIZE(policy), tb, blobmsg_data(cur), blobmsg_data_len(cur)); + if (!tb[0] || !tb[1]) + continue; + + fprintf(stdout, "\t%s (%s -> %s)\n", blobmsg_name(cur), + blobmsg_get_string(tb[1]), blobmsg_get_string(tb[0])); + }; +} + /* this main function is too big... todo: split */ int main(int args, char *argv[]) { + unsigned char argc=1; static struct blob_buf checkbuf, reqbuf, imgbuf, upgbuf; struct ubus_context *ctx = ubus_connect(NULL); uint32_t id; @@ -704,8 +742,22 @@ int main(int args, char *argv[]) { char *checksum = NULL; struct stat imgstat; - if (args>1 && !strncmp(argv[1], "-d", 3)) - debug = 1; + snprintf(user_agent, sizeof(user_agent), "%s (%s)", argv[0], AUC_VERSION); + fprintf(stdout, "%s\n", user_agent); + + while (argc