[PATCH]validate log_handle in maat_new
This commit is contained in:
44
deps/log/log.c
vendored
44
deps/log/log.c
vendored
@@ -59,18 +59,14 @@ static int log_create_dir(const char *dir_path, int path_len)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *buf = (char *)calloc(path_len + 1, 1);
|
char *buf = ALLOC(char, path_len + 1);
|
||||||
int ret = -1;
|
int ret = 0;
|
||||||
|
|
||||||
memcpy(buf, dir_path, path_len);
|
memcpy(buf, dir_path, path_len);
|
||||||
if (access(buf, R_OK) != 0) {
|
if (access(buf, R_OK) != 0) {
|
||||||
if (mkdir(buf, 0755) != 0) {
|
if (mkdir(buf, 0755) != 0) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
} else {
|
|
||||||
ret = 0;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
ret = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(buf);
|
free(buf);
|
||||||
@@ -105,22 +101,22 @@ int log_open_file(char *file_name, struct log_handle *handle)
|
|||||||
|
|
||||||
static int log_create_path(const char *file_path)
|
static int log_create_path(const char *file_path)
|
||||||
{
|
{
|
||||||
FILE *fp = NULL;
|
if (NULL == file_path) {
|
||||||
|
return -1;
|
||||||
if (file_path == NULL)
|
}
|
||||||
return 0;
|
|
||||||
|
|
||||||
char *p_path = rindex(file_path, '/');
|
char *p_path = rindex(file_path, '/');
|
||||||
if (p_path == 0) {
|
if (NULL == p_path) {
|
||||||
return 0;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *p_cur = file_path;
|
const char *p_cur = file_path;
|
||||||
int path_len = p_path - file_path;
|
int path_len = p_path - file_path;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
if (log_create_dir(file_path, path_len) >= 0)
|
if (log_create_dir(file_path, path_len) < 0) {
|
||||||
return 0;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
for (; i <= path_len; i++, p_cur++) {
|
for (; i <= path_len; i++, p_cur++) {
|
||||||
if (*p_cur == '/') {
|
if (*p_cur == '/') {
|
||||||
@@ -129,13 +125,14 @@ static int log_create_path(const char *file_path)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (NULL == (fp = fopen(file_path, "w"))) {
|
FILE *fp = fopen(file_path, "w");
|
||||||
return 0;
|
if (NULL == fp) {
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int log_create_log_file(struct log_handle *handle)
|
int log_create_log_file(struct log_handle *handle)
|
||||||
@@ -225,17 +222,20 @@ void log_handle_set_enable(struct log_handle * handle, int enable)
|
|||||||
struct log_handle *log_handle_create(const char *file_path, int level)
|
struct log_handle *log_handle_create(const char *file_path, int level)
|
||||||
{
|
{
|
||||||
struct log_handle *handle = ALLOC(struct log_handle, 1);
|
struct log_handle *handle = ALLOC(struct log_handle, 1);
|
||||||
if (!handle) {
|
if (NULL == handle) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
handle->enable = 1;
|
handle->enable = 1;
|
||||||
handle->level = level;
|
handle->level = level;
|
||||||
strncpy(handle->defined_log_fn, file_path, 1024);
|
strncpy(handle->defined_log_fn, file_path, 1024);
|
||||||
pthread_mutex_init(&handle->mutex, NULL);
|
|
||||||
|
int ret = log_create_path(handle->defined_log_fn);
|
||||||
if (handle->enable) {
|
if (ret < 0) {
|
||||||
log_create_path(handle->defined_log_fn);
|
free(handle);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
pthread_mutex_init(&handle->mutex, NULL);
|
||||||
|
|
||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -347,6 +347,7 @@ struct maat *maat_new(struct maat_options *opts, const char *table_info_path)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int garbage_gc_timeout_s = 0;
|
||||||
struct maat *maat_inst = ALLOC(struct maat, 1);
|
struct maat *maat_inst = ALLOC(struct maat, 1);
|
||||||
|
|
||||||
maat_inst->opts = *opts;
|
maat_inst->opts = *opts;
|
||||||
@@ -364,6 +365,11 @@ struct maat *maat_new(struct maat_options *opts, const char *table_info_path)
|
|||||||
maat_inst->logger = log_handle_create(log_path, maat_inst->opts.log_level);
|
maat_inst->logger = log_handle_create(log_path, maat_inst->opts.log_level);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (NULL == maat_inst->logger) {
|
||||||
|
fprintf(stderr, "create log handle failed.\n");
|
||||||
|
goto failed;
|
||||||
|
}
|
||||||
|
|
||||||
if (0 == strlen(maat_inst->opts.foreign_cont_dir)) {
|
if (0 == strlen(maat_inst->opts.foreign_cont_dir)) {
|
||||||
snprintf(maat_inst->opts.foreign_cont_dir, sizeof(maat_inst->opts.foreign_cont_dir),
|
snprintf(maat_inst->opts.foreign_cont_dir, sizeof(maat_inst->opts.foreign_cont_dir),
|
||||||
"%s_files", table_info_path);
|
"%s_files", table_info_path);
|
||||||
@@ -381,8 +387,8 @@ struct maat *maat_new(struct maat_options *opts, const char *table_info_path)
|
|||||||
maat_inst->maat_version = 0;
|
maat_inst->maat_version = 0;
|
||||||
maat_inst->last_full_version = 0;
|
maat_inst->last_full_version = 0;
|
||||||
|
|
||||||
int garbage_gc_timeout_s = (maat_inst->opts.rule_effect_interval_ms / 1000) +
|
garbage_gc_timeout_s = (maat_inst->opts.rule_effect_interval_ms / 1000) +
|
||||||
(maat_inst->opts.gc_timeout_ms / 1000);
|
(maat_inst->opts.gc_timeout_ms / 1000);
|
||||||
|
|
||||||
if (maat_inst->opts.input_mode != DATA_SOURCE_IRIS_FILE &&
|
if (maat_inst->opts.input_mode != DATA_SOURCE_IRIS_FILE &&
|
||||||
maat_inst->opts.input_mode != DATA_SOURCE_JSON_FILE &&
|
maat_inst->opts.input_mode != DATA_SOURCE_JSON_FILE &&
|
||||||
|
|||||||
Reference in New Issue
Block a user