Skip to content

Commit

Permalink
Merge pull request #816 from eeff/eeff/2.2
Browse files Browse the repository at this point in the history
Pick to v2.2: upload license to persistence directory
  • Loading branch information
fengzeroz authored Sep 23, 2022
2 parents 57d5a51 + d031a0e commit 05d8387
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
88 changes: 87 additions & 1 deletion plugins/restful/license_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#define _POSIX_C_SOURCE 200809L
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions plugins/restful/license_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
7 changes: 7 additions & 0 deletions plugins/restful/rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 05d8387

Please sign in to comment.