From 6708ba8937e2ffd652927bf1cf15eb3e41ae7985 Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Fri, 23 Sep 2022 10:26:24 +0800 Subject: [PATCH 1/2] chore: upload license to persistence directory --- plugins/restful/license_handle.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/plugins/restful/license_handle.c b/plugins/restful/license_handle.c index bb92a076..6f8239a4 100644 --- a/plugins/restful/license_handle.c +++ b/plugins/restful/license_handle.c @@ -19,6 +19,7 @@ #define _POSIX_C_SOURCE 200809L #include +#include #include #include @@ -43,10 +44,34 @@ int get_plugin_names(const license_t *lic, UT_array *plugin_names); static inline char *get_license_path() { char *s = NULL; - neu_asprintf(&s, "%s/%s", g_config_dir, LICENSE_FNAME); + neu_asprintf(&s, "persistence/%s", LICENSE_FNAME); return s; } +static inline char *get_license_path_or_fallback() +{ + struct stat statbuf = {}; + char * license_path = get_license_path(); + if (NULL == license_path) { + return NULL; + } + + if (0 == stat(license_path, &statbuf)) { + return license_path; + } + + nlog_warn("license `%s` not found", license_path); + free(license_path); + license_path = NULL; + + neu_asprintf(&license_path, "%s/%s", g_config_dir, LICENSE_FNAME); + if (NULL != license_path) { + nlog_warn("license fallback to `%s`", license_path); + } + + return license_path; +} + void handle_get_license(nng_aio *aio) { int rv = 0; @@ -65,7 +90,7 @@ void handle_get_license(nng_aio *aio) utarray_new(plugin_names, &ut_ptr_icd); - license_path = get_license_path(); + license_path = get_license_path_or_fallback(); if (NULL == license_path) { rv = NEU_ERR_EINTERNAL; goto final; From 4d6999cec09926aad869d9648415f30d9f841146 Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Fri, 23 Sep 2022 16:06:48 +0800 Subject: [PATCH 2/2] chore: copy license file for backward compatiblity --- plugins/restful/license_handle.c | 81 ++++++++++++++++++++++++++++---- plugins/restful/license_handle.h | 1 + plugins/restful/rest.c | 7 +++ 3 files changed, 79 insertions(+), 10 deletions(-) diff --git a/plugins/restful/license_handle.c b/plugins/restful/license_handle.c index 6f8239a4..cbed979e 100644 --- a/plugins/restful/license_handle.c +++ b/plugins/restful/license_handle.c @@ -48,28 +48,89 @@ static inline char *get_license_path() return s; } -static inline char *get_license_path_or_fallback() +static int copy_file(const char *from, const char *to) +{ + int rv = 0; + char * buf = NULL; + FILE * fp = NULL; + struct stat statbuf = {}; + + if (0 != stat(from, &statbuf)) { + return NEU_ERR_EINTERNAL; + } + + buf = malloc(statbuf.st_size); + if (NULL == buf) { + return NEU_ERR_EINTERNAL; + } + + fp = fopen(from, "rb"); + if (NULL == fp) { + rv = NEU_ERR_EINTERNAL; + goto end; + } + + if ((size_t) statbuf.st_size != fread(buf, 1, statbuf.st_size, fp)) { + rv = NEU_ERR_EINTERNAL; + goto end; + } + + fclose(fp); + fp = fopen(to, "w"); + if (NULL == fp) { + rv = NEU_ERR_EINTERNAL; + goto end; + } + + if ((size_t) statbuf.st_size != fwrite(buf, 1, statbuf.st_size, fp)) { + rv = NEU_ERR_EINTERNAL; + goto end; + } + +end: + if (fp) { + fclose(fp); + } + free(buf); + return rv; +} + +int copy_license_file_if_necessary() { struct stat statbuf = {}; char * license_path = get_license_path(); + if (NULL == license_path) { - return NULL; + return NEU_ERR_EINTERNAL; } if (0 == stat(license_path, &statbuf)) { - return license_path; + free(license_path); + return 0; } nlog_warn("license `%s` not found", license_path); - free(license_path); - license_path = NULL; - neu_asprintf(&license_path, "%s/%s", g_config_dir, LICENSE_FNAME); - if (NULL != license_path) { - nlog_warn("license fallback to `%s`", license_path); + char *fallback_path = NULL; + neu_asprintf(&fallback_path, "%s/%s", g_config_dir, LICENSE_FNAME); + if (NULL == fallback_path) { + free(license_path); + return NEU_ERR_EINTERNAL; } - return license_path; + if (0 != stat(fallback_path, &statbuf)) { + // no fallback license file + free(license_path); + free(fallback_path); + return 0; + } + + nlog_warn("fallback to `%s`", fallback_path); + int rv = copy_file(fallback_path, license_path); + + free(license_path); + free(fallback_path); + return rv; } void handle_get_license(nng_aio *aio) @@ -90,7 +151,7 @@ void handle_get_license(nng_aio *aio) utarray_new(plugin_names, &ut_ptr_icd); - license_path = get_license_path_or_fallback(); + license_path = get_license_path(); if (NULL == license_path) { rv = NEU_ERR_EINTERNAL; goto final; diff --git a/plugins/restful/license_handle.h b/plugins/restful/license_handle.h index 00761df0..4361d28d 100644 --- a/plugins/restful/license_handle.h +++ b/plugins/restful/license_handle.h @@ -22,6 +22,7 @@ #include +int copy_license_file_if_necessary(); void handle_get_license(nng_aio *aio); void handle_set_license(nng_aio *aio); diff --git a/plugins/restful/rest.c b/plugins/restful/rest.c index 89f37b40..cf962dd9 100644 --- a/plugins/restful/rest.c +++ b/plugins/restful/rest.c @@ -31,6 +31,7 @@ #include "group_config_handle.h" #include "handle.h" #include "http.h" +#include "license_handle.h" #include "plugin_handle.h" #include "proxy.h" #include "rest.h" @@ -145,6 +146,12 @@ static neu_plugin_t *dashb_plugin_open(void) const struct neu_rest_handler *rest_handlers = NULL; const struct neu_rest_handler *cors = NULL; + // copy license file for backward compatibility + if (0 != copy_license_file_if_necessary()) { + free(plugin); + return NULL; + } + neu_plugin_common_init(&plugin->common); plugin->handle_ctx = neu_rest_init_ctx(plugin);