From 5c55d7d4b9de45d3aeb931b3b34d2232a975ae8c Mon Sep 17 00:00:00 2001 From: tangjie02 Date: Sat, 12 Dec 2020 16:32:49 +0800 Subject: [PATCH 2/2] feature(mkdir): If the output directory does not exist, it will be created automatically. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 日志输出目录不存在则自动创建 Signed-off-by: tangjie02 --- src/conf.c | 7 +++++++ src/conf.h | 1 + src/rule.c | 24 ++++++++++++++++++++++++ src/rule.h | 2 ++ src/zc_util.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/zc_util.h | 1 + 6 files changed, 83 insertions(+) diff --git a/src/conf.c b/src/conf.c index c592153..a947178 100644 --- a/src/conf.c +++ b/src/conf.c @@ -30,6 +30,7 @@ #define ZLOG_CONF_DEFAULT_BUF_SIZE_MIN 1024 #define ZLOG_CONF_DEFAULT_BUF_SIZE_MAX (2 * 1024 * 1024) #define ZLOG_CONF_DEFAULT_FILE_PERMS 0600 +#define ZLOG_CONF_DEFAULT_DIRECTORY_PERMS 0755 #define ZLOG_CONF_DEFAULT_RELOAD_CONF_PERIOD 0 #define ZLOG_CONF_DEFAULT_FSYNC_PERIOD 0 #define ZLOG_CONF_BACKUP_ROTATE_LOCK_FILE "/tmp/zlog.lock" @@ -54,6 +55,7 @@ void zlog_conf_profile(zlog_conf_t * a_conf, int flag) zlog_format_profile(a_conf->default_format, flag); } zc_profile(flag, "---file perms[0%o]---", a_conf->file_perms); + zc_profile(flag, "---directory perms[0%o]---", a_conf->directory_perms); zc_profile(flag, "---reload conf period[%ld]---", a_conf->reload_conf_period); zc_profile(flag, "---fsync period[%ld]---", a_conf->fsync_period); @@ -150,6 +152,7 @@ zlog_conf_t *zlog_conf_new(const char *config) } strcpy(a_conf->default_format_line, ZLOG_CONF_DEFAULT_FORMAT); a_conf->file_perms = ZLOG_CONF_DEFAULT_FILE_PERMS; + a_conf->directory_perms = ZLOG_CONF_DEFAULT_DIRECTORY_PERMS; a_conf->reload_conf_period = ZLOG_CONF_DEFAULT_RELOAD_CONF_PERIOD; a_conf->fsync_period = ZLOG_CONF_DEFAULT_FSYNC_PERIOD; /* set default configuration end */ @@ -218,6 +221,7 @@ static int zlog_conf_build_without_file(zlog_conf_t * a_conf) a_conf->default_format, a_conf->formats, a_conf->file_perms, + a_conf->directory_perms, a_conf->fsync_period, &(a_conf->time_cache_count)); if (!default_rule) { @@ -485,6 +489,8 @@ static int zlog_conf_parse_line(zlog_conf_t * a_conf, char *line, int *section) a_conf->buf_size_max = zc_parse_byte_size(value); } else if (STRCMP(word_1, ==, "file") && STRCMP(word_2, ==, "perms")) { sscanf(value, "%o", &(a_conf->file_perms)); + } else if (STRCMP(word_1, ==, "directory") && STRCMP(word_2, ==, "perms")) { + sscanf(value, "%o", &(a_conf->directory_perms)); } else if (STRCMP(word_1, ==, "rotate") && STRCMP(word_2, ==, "lock") && STRCMP(word_3, ==, "file")) { /* may overwrite the inner default value, or last value */ @@ -531,6 +537,7 @@ static int zlog_conf_parse_line(zlog_conf_t * a_conf, char *line, int *section) a_conf->default_format, a_conf->formats, a_conf->file_perms, + a_conf->directory_perms, a_conf->fsync_period, &(a_conf->time_cache_count)); diff --git a/src/conf.h b/src/conf.h index 159325e..247bd0b 100644 --- a/src/conf.h +++ b/src/conf.h @@ -29,6 +29,7 @@ typedef struct zlog_conf_s { zlog_format_t *default_format; unsigned int file_perms; + unsigned int directory_perms; size_t fsync_period; size_t reload_conf_period; diff --git a/src/rule.c b/src/rule.c index ae3d74f..473d21f 100644 --- a/src/rule.c +++ b/src/rule.c @@ -100,6 +100,11 @@ static int zlog_rule_output_static_file_single(zlog_rule_t * a_rule, zlog_thread if (do_file_reload) { close(a_rule->static_fd); + + if (zc_mkdir_with_parents(a_rule->file_path, a_rule->directory_perms) < 0) { + return -1; + } + a_rule->static_fd = open(a_rule->file_path, O_WRONLY | O_APPEND | O_CREAT | a_rule->file_open_flags, a_rule->file_perms); @@ -168,6 +173,10 @@ static int zlog_rule_output_static_file_rotate(zlog_rule_t * a_rule, zlog_thread return -1; } + if (zc_mkdir_with_parents(a_rule->file_path, a_rule->directory_perms) < 0) { + return -1; + } + fd = open(a_rule->file_path, a_rule->file_open_flags | O_WRONLY | O_APPEND | O_CREAT, a_rule->file_perms); if (fd < 0) { @@ -249,6 +258,10 @@ static int zlog_rule_output_dynamic_file_single(zlog_rule_t * a_rule, zlog_threa return -1; } + if (zc_mkdir_with_parents(a_rule->file_path, a_rule->directory_perms) < 0) { + return -1; + } + fd = open(zlog_buf_str(a_thread->path_buf), a_rule->file_open_flags | O_WRONLY | O_APPEND | O_CREAT, a_rule->file_perms); if (fd < 0) { @@ -290,6 +303,11 @@ static int zlog_rule_output_dynamic_file_rotate(zlog_rule_t * a_rule, zlog_threa } path = zlog_buf_str(a_thread->path_buf); + + if (zc_mkdir_with_parents(path, a_rule->directory_perms) < 0) { + return -1; + } + fd = open(path, a_rule->file_open_flags | O_WRONLY | O_APPEND | O_CREAT, a_rule->file_perms); if (fd < 0) { zc_error("open file[%s] fail, errno[%d]", zlog_buf_str(a_thread->path_buf), errno); @@ -566,6 +584,7 @@ zlog_rule_t *zlog_rule_new(char *line, zlog_format_t * default_format, zc_arraylist_t * formats, unsigned int file_perms, + unsigned int directory_perms, size_t fsync_period, int * time_cache_count) { @@ -600,6 +619,7 @@ zlog_rule_t *zlog_rule_new(char *line, } a_rule->file_perms = file_perms; + a_rule->directory_perms = directory_perms; a_rule->fsync_period = fsync_period; /* line [f.INFO "%H/log/aa.log", 20MB * 12; MyTemplate] @@ -810,6 +830,10 @@ zlog_rule_t *zlog_rule_new(char *line, a_rule->output = zlog_rule_output_static_file_rotate; } + if (zc_mkdir_with_parents(a_rule->file_path, a_rule->directory_perms) < 0) { + goto err; + } + a_rule->static_fd = open(a_rule->file_path, O_WRONLY | O_APPEND | O_CREAT | a_rule->file_open_flags, a_rule->file_perms); diff --git a/src/rule.h b/src/rule.h index e562185..39417e2 100644 --- a/src/rule.h +++ b/src/rule.h @@ -40,6 +40,7 @@ struct zlog_rule_s { unsigned char level_bitmap[32]; /* for category determine whether ouput or not */ unsigned int file_perms; + unsigned int directory_perms; int file_open_flags; char file_path[MAXLEN_PATH + 1]; @@ -75,6 +76,7 @@ zlog_rule_t *zlog_rule_new(char * line, zlog_format_t * default_format, zc_arraylist_t * formats, unsigned int file_perms, + unsigned int directory_perms, size_t fsync_period, int * time_cache_count); diff --git a/src/zc_util.c b/src/zc_util.c index 07deb22..93745ae 100644 --- a/src/zc_util.c +++ b/src/zc_util.c @@ -12,6 +12,10 @@ #include #include #include +#include +#include +#include + #include "zc_defs.h" @@ -145,3 +149,47 @@ int zc_str_replace_env(char *str, size_t str_size) return 0; } + +int zc_mkdir_with_parents(const char *path, unsigned int perms) +{ + char str[MAXLEN_PATH]; + int str_len; + int i; + + str_len = snprintf(str, MAXLEN_PATH, "%s", path); + if (str_len <= 0 || str_len >= MAXLEN_PATH) + { + zc_error("snprintf fail, errno[%d], str_len[%d]", + errno, str_len); + return -1; + } + + if (str[str_len - 1] == '/') + { + --str_len; + } + + while (str_len > 0 && str[str_len - 1] != '/') + { + --str_len; + } + str[str_len] = 0; + + for (i = 1; i < str_len; i++) + { + if (str[i] == '/') + { + str[i] = 0; + if (access(str, F_OK) != 0) + { + if (mkdir(str, perms) == -1) + { + zc_error("mkdir error, errno[%d] str[%s]", errno, str); + return -1; + } + } + str[i] = '/'; + } + } + return 0; +} diff --git a/src/zc_util.h b/src/zc_util.h index 3f032cd..dbdeaf5 100644 --- a/src/zc_util.h +++ b/src/zc_util.h @@ -10,6 +10,7 @@ size_t zc_parse_byte_size(char *astring); int zc_str_replace_env(char *str, size_t str_size); +int zc_mkdir_with_parents(const char *path, unsigned int perms); #define zc_max(a,b) ((a) > (b) ? (a) : (b)) #define zc_min(a,b) ((a) < (b) ? (a) : (b)) -- 2.27.0