From 3257a5f8423e42367a8e674c73f1ec0893742719 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Sat, 20 Aug 2022 09:59:36 +0800 Subject: [PATCH] function sf_get_base_path_from_conf_file impl. --- src/sf_global.c | 22 ++++++++++++++++++++-- src/sf_global.h | 5 +++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/sf_global.c b/src/sf_global.c index 43986a1..c3cf487 100644 --- a/src/sf_global.c +++ b/src/sf_global.c @@ -28,6 +28,7 @@ #include #include "fastcommon/common_define.h" #include "fastcommon/shared_func.h" +#include "fastcommon/process_ctrl.h" #include "fastcommon/logger.h" #include "sf_nio.h" #include "sf_global.h" @@ -219,13 +220,30 @@ int sf_load_slow_log_config_ex(IniFullContext *ini_ctx, LogContext *log_ctx, return 0; } +int sf_get_base_path_from_conf_file(const char *config_filename) +{ + int result; + + if (SF_G_BASE_PATH_INITED) { + return 0; + } + + result = get_base_path_from_conf_file(config_filename, + SF_G_BASE_PATH_STR, sizeof(SF_G_BASE_PATH_STR)); + if (result == 0) { + SF_G_BASE_PATH_INITED = true; + } + + return result; +} + int sf_load_global_base_path(IniFullContext *ini_ctx) { char *pBasePath; - if (!g_sf_global_vars.base_path.inited) { + if (!SF_G_BASE_PATH_INITED) { pBasePath = iniGetStrValue(NULL, "base_path", ini_ctx->context); - if (pBasePath == NULL) { + if (pBasePath == NULL || *pBasePath == '\0') { logError("file: "__FILE__", line: %d, " "conf file \"%s\" must have item " "\"base_path\"!", __LINE__, ini_ctx->filename); diff --git a/src/sf_global.h b/src/sf_global.h index 07cb1fd..4ad8265 100644 --- a/src/sf_global.h +++ b/src/sf_global.h @@ -205,12 +205,13 @@ void sf_log_config_ex(const char *other_config); #define sf_log_config_to_string(log_cfg, caption, output, size) \ sf_log_config_to_string_ex(log_cfg, caption, NULL, output, size) +int sf_get_base_path_from_conf_file(const char *config_filename); int sf_load_global_base_path(IniFullContext *ini_ctx); static inline void sf_set_global_base_path(const char *base_path) { - snprintf(SF_G_BASE_PATH_STR, sizeof(SF_G_BASE_PATH_STR), - "%s", base_path); + normalize_path(NULL, base_path, SF_G_BASE_PATH_STR, + sizeof(SF_G_BASE_PATH_STR)); SF_G_BASE_PATH_INITED = true; }