Skip to content

Commit 34d6c12

Browse files
Copilotithewei
andcommitted
Fix main_ctx_init: increase MAX_PATH, remove premature dir creation, add config fallback
1. Increase MAX_PATH from 260 to 1024 to handle longer file paths 2. Remove premature hv_mkdir(logdir) and hlog_set_file() calls from main_ctx_init to avoid creating directories before user customization 3. Add config file path fallback: run_dir/etc/prog.conf -> run_dir/prog.conf -> /usr/local/etc/prog.conf -> /etc/prog.conf (Unix only for last two) Co-authored-by: ithewei <26049660+ithewei@users.noreply.github.com>
1 parent b15b816 commit 34d6c12

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

base/hdef.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
#endif
184184

185185
#ifndef MAX_PATH
186-
#define MAX_PATH 260
186+
#define MAX_PATH 1024
187187
#endif
188188

189189
#ifndef NULL

base/hmain.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,39 @@ int main_ctx_init(int argc, char** argv) {
8383
}
8484
#endif
8585
//printf("program_name=%s\n", g_main_ctx.program_name);
86-
char logdir[MAX_PATH] = {0};
87-
snprintf(logdir, sizeof(logdir), "%s/logs", g_main_ctx.run_dir);
88-
hv_mkdir(logdir);
89-
snprintf(g_main_ctx.confile, sizeof(g_main_ctx.confile), "%s/etc/%s.conf", g_main_ctx.run_dir, g_main_ctx.program_name);
86+
// confile: run_dir/etc/${program}.conf -> run_dir/${program}.conf -> /usr/local/etc/${program}.conf -> /etc/${program}.conf
87+
{
88+
char confile[MAX_PATH] = {0};
89+
const char* confdirs[] = {
90+
"%s/etc/%s.conf",
91+
"%s/%s.conf",
92+
#ifdef OS_UNIX
93+
"/usr/local/etc/%s.conf",
94+
"/etc/%s.conf",
95+
#endif
96+
};
97+
int nconfdir = sizeof(confdirs) / sizeof(confdirs[0]);
98+
int i;
99+
int found = 0;
100+
for (i = 0; i < nconfdir; ++i) {
101+
if (i < 2) {
102+
snprintf(confile, sizeof(confile), confdirs[i], g_main_ctx.run_dir, g_main_ctx.program_name);
103+
} else {
104+
snprintf(confile, sizeof(confile), confdirs[i], g_main_ctx.program_name);
105+
}
106+
if (hv_exists(confile)) {
107+
snprintf(g_main_ctx.confile, sizeof(g_main_ctx.confile), "%s", confile);
108+
found = 1;
109+
break;
110+
}
111+
}
112+
if (!found) {
113+
// default
114+
snprintf(g_main_ctx.confile, sizeof(g_main_ctx.confile), "%s/etc/%s.conf", g_main_ctx.run_dir, g_main_ctx.program_name);
115+
}
116+
}
90117
snprintf(g_main_ctx.pidfile, sizeof(g_main_ctx.pidfile), "%s/logs/%s.pid", g_main_ctx.run_dir, g_main_ctx.program_name);
91118
snprintf(g_main_ctx.logfile, sizeof(g_main_ctx.logfile), "%s/logs/%s.log", g_main_ctx.run_dir, g_main_ctx.program_name);
92-
hlog_set_file(g_main_ctx.logfile);
93119

94120
g_main_ctx.pid = getpid();
95121
g_main_ctx.oldpid = getpid_from_pidfile();

0 commit comments

Comments
 (0)