支持加载使用gzip命令压缩的maat JSON文件。允许先压缩、后加密,不支持先加密后压缩。

This commit is contained in:
zhengchao
2021-04-28 21:44:04 +08:00
parent c5239762fe
commit 1460f891c6
8 changed files with 148 additions and 16 deletions

View File

@@ -481,21 +481,50 @@ int load_maat_json_file(_Maat_feather_t* feather, const char* maat_json_fn, char
{
int ret=0;
struct stat fstat_buf;
char* json_buff=NULL;
size_t buff_sz=0;
unsigned char* json_buff=NULL, *decrypted_buff=NULL, *uncompressed_buff=NULL;
size_t json_buff_sz=0, decrypted_buff_sz=0, uncompressed_buff_sz=0;
MESA_handle_runtime_log(feather->logger, RLOG_LV_INFO, maat_module ,
"Maat initial with JSON file %s, formating..",
maat_json_fn);
if(strlen(feather->decrypt_key)&&strlen(feather->decrypt_algo))
{
ret=decrypt_open(maat_json_fn, feather->decrypt_key, feather->decrypt_algo, (unsigned char**)&json_buff, &buff_sz, err_str, err_str_sz);
ret=decrypt_open(maat_json_fn, feather->decrypt_key, feather->decrypt_algo, (unsigned char**)&decrypted_buff, &decrypted_buff_sz, err_str, err_str_sz);
if(ret<0)
{
MESA_handle_runtime_log(feather->logger, RLOG_LV_FATAL, maat_module,
"Decrypt Maat JSON file %s failed.", maat_json_fn);
return -1;
}
json_buff=decrypted_buff;
json_buff_sz=decrypted_buff_sz;
}
if(feather->maat_json_is_gzipped)
{
ret=gzip_uncompress(json_buff, json_buff_sz, &uncompressed_buff, &uncompressed_buff_sz);
free(json_buff);
if(ret<0)
{
MESA_handle_runtime_log(feather->logger, RLOG_LV_FATAL, maat_module,
"Uncompress Maat JSON file %s failed.", maat_json_fn);
return -1;
}
json_buff=uncompressed_buff;
json_buff_sz=uncompressed_buff_sz;
}
if(json_buff==NULL)//decryption failed or no decryption.
{
ret=load_file_to_memory(maat_json_fn, (unsigned char**)&json_buff, &buff_sz);
ret=load_file_to_memory(maat_json_fn, &json_buff, &json_buff_sz);
if(ret<0)
{
MESA_handle_runtime_log(feather->logger, RLOG_LV_FATAL, maat_module,
"Read Maat JSON file %s failed.", maat_json_fn);
return -1;
}
}
ret=json2iris(json_buff,
ret=json2iris((const char*)json_buff,
maat_json_fn,
feather->compile_tn, feather->group2compile_tn, feather->group2group_tn,
NULL,
@@ -562,6 +591,8 @@ Maat_feather_t Maat_feather(int max_thread_num,const char* table_info_path,void*
feather->base_rgn_seq=0;
feather->AUTO_NUMBERING_ON=1;
feather->backgroud_update_enabled=1;
feather->maat_json_is_gzipped=0;
snprintf(feather->decrypt_algo, sizeof(feather->decrypt_algo), "aes-256-cbc");
snprintf(feather->foreign_cont_dir, sizeof(feather->foreign_cont_dir), "%s_files", table_info_path);
pthread_mutex_init(&(feather->background_update_mutex),NULL);
@@ -640,7 +671,22 @@ int Maat_set_feather_opt(Maat_feather_t feather,enum MAAT_INIT_OPT type,const vo
}
_feather->garbage_collection_timeout_ms=intval;
break;
case MAAT_OPT_JSON_IS_GZIPPED:
intval=*(const int*)value;
if(size!=sizeof(int)||intval<0)
{
return -1;
}
if(intval>0)
{
_feather->maat_json_is_gzipped=1;
}
else
{
_feather->maat_json_is_gzipped=0;
}
break;
case MAAT_OPT_FULL_CFG_DIR:
assert(_feather->input_mode==SOURCE_NONE);
if(size>(int)sizeof(_feather->iris_ctx.full_dir))
@@ -667,7 +713,6 @@ int Maat_set_feather_opt(Maat_feather_t feather,enum MAAT_INIT_OPT type,const vo
(const char*)value, err_str);
return -1;
}
break;
case MAAT_OPT_STAT_ON:
_feather->stat_on=1;

View File

@@ -5,7 +5,7 @@
#include <openssl/md5.h>
#include <sys/stat.h>
#include <openssl/evp.h>
#include <zlib.h>
#include "Maat_utils.h"
pid_t gettid()
{
@@ -221,6 +221,13 @@ int system_cmd_cp(const char* src_file,const char*dst_file)
snprintf(cmd, sizeof(cmd), "cp -f %s %s", src_file, dst_file);
return system(cmd);
}
int system_cmd_gzip(const char* src_file, const char* dst_file)
{
char cmd[MAX_SYSTEM_CMD_LEN] = { 0 };
snprintf(cmd,sizeof(cmd), "gzip -9 < %s > %s", src_file, dst_file);
return system(cmd);
}
int system_cmd_encrypt(const char* src_file, const char* dst_file, const char* password)
{
char cmd[MAX_SYSTEM_CMD_LEN] = { 0 };
@@ -390,6 +397,71 @@ int decrypt_open(const char* file_name, const char* key, const char* algorithm,
file_buff=NULL;
return ret;
}
int gzip_uncompress_one_try(const unsigned char *in_compressed_data, size_t in_compressed_sz, unsigned char **out_uncompressed_data,
size_t *out_uncompressed_sz)
{
z_stream strm;
strm.zalloc = NULL;
strm.zfree = NULL;
strm.opaque = NULL;
strm.avail_in = in_compressed_sz;
strm.avail_out = *out_uncompressed_sz;
strm.next_in = (Bytef*) in_compressed_data;
strm.next_out = *out_uncompressed_data;
int ret = -1;
ret = inflateInit2(&strm, MAX_WBITS+16);
if (ret == Z_OK)
{
ret = inflate(&strm, Z_FINISH);
if (ret == Z_STREAM_END)
{
*out_uncompressed_sz = strm.total_out;
ret = inflateEnd(&strm);
return ret;
}
}
inflateEnd(&strm);
return ret;
}
int gzip_uncompress(const unsigned char *in_compressed_data, size_t in_compressed_sz, unsigned char **out_uncompressed_data,
size_t *out_uncompressed_sz)
{
int z_result;
int ret=-1;
size_t buffer_sz=in_compressed_sz*2;
*out_uncompressed_data = (unsigned char*) malloc(buffer_sz);
do{
*out_uncompressed_sz=buffer_sz;
z_result = gzip_uncompress_one_try(
in_compressed_data,
in_compressed_sz,
out_uncompressed_data,
out_uncompressed_sz);
switch( z_result )
{
case Z_OK:
ret=0;
break;
case Z_BUF_ERROR:
buffer_sz*=2;
*out_uncompressed_data=(unsigned char*) realloc(*out_uncompressed_data, buffer_sz);
break;
default:
ret=-1;
break;
}
}while(z_result==Z_BUF_ERROR);
return ret;
}
enum MAAT_IP_FORMAT ip_format_str2int(const char* format)
{
if(0==strcasecmp(format, "range"))