Merge pull request #813 from eeff/eeff/dev

chore: upload license to persistence directory
This commit is contained in:
fengzeroz
2022-09-23 16:21:49 +08:00
committed by GitHub
3 changed files with 95 additions and 1 deletions
+87 -1
View File
@@ -19,6 +19,7 @@
#define _POSIX_C_SOURCE 200809L
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
@@ -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;
+1
View File
@@ -22,6 +22,7 @@
#include <nng/nng.h>
int copy_license_file_if_necessary();
void handle_get_license(nng_aio *aio);
void handle_set_license(nng_aio *aio);
+7
View File
@@ -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);