diff --git a/plugins/restful/license_handle.c b/plugins/restful/license_handle.c index bb92a0763..cbed979ea 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,95 @@ 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 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 NEU_ERR_EINTERNAL; + } + + if (0 == stat(license_path, &statbuf)) { + free(license_path); + return 0; + } + + nlog_warn("license `%s` not found", 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; + } + + 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) { int rv = 0; diff --git a/plugins/restful/license_handle.h b/plugins/restful/license_handle.h index 00761df02..4361d28da 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 22c723c59..460452254 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);