Skip to content

Commit 9d3f10e

Browse files
cosmo0920edsiper
authored andcommitted
out_file: Replace SHCreateDirectoryExA to work for recursive directories correctly
It's been a while this issue could be a long standing issue and avoding to use SHCreateDirectoryExA is a removing nonexisting API for Windows Nano Server. This API is not listed in the supported API list here: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/mt588480(v=vs.85) Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 9def01d commit 9d3f10e

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

plugins/out_file/file.c

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
#define NEWLINE "\n"
4646
#endif
4747

48+
#ifdef FLB_SYSTEM_WINDOWS
49+
#define FLB_PATH_SEPARATOR "\\"
50+
#else
51+
#define FLB_PATH_SEPARATOR "/"
52+
#endif
53+
4854
struct flb_file_conf {
4955
const char *out_path;
5056
const char *out_file;
@@ -368,7 +374,11 @@ static int mkpath(struct flb_output_instance *ins, const char *dir)
368374
#ifdef FLB_SYSTEM_MACOS
369375
char *parent_dir = NULL;
370376
#endif
371-
377+
#ifdef FLB_SYSTEM_WINDOWS
378+
char parent_path[MAX_PATH];
379+
DWORD err;
380+
char *p;
381+
#endif
372382
int ret;
373383

374384
if (!dir) {
@@ -391,15 +401,40 @@ static int mkpath(struct flb_output_instance *ins, const char *dir)
391401
}
392402

393403
#ifdef FLB_SYSTEM_WINDOWS
394-
char path[MAX_PATH];
395-
396-
if (_fullpath(path, dir, MAX_PATH) == NULL) {
404+
if (strncpy_s(parent_path, MAX_PATH, dir, _TRUNCATE) != 0) {
405+
flb_plg_error(ins, "path is too long: %s", dir);
397406
return -1;
398407
}
399408

400-
if (SHCreateDirectoryExA(NULL, path, NULL) != ERROR_SUCCESS) {
401-
return -1;
409+
/* Normalize all forward slashes to backslashes */
410+
for (p = parent_path; *p; ++p) {
411+
if (*p == '/') {
412+
*p = '\\';
413+
}
402414
}
415+
416+
flb_plg_debug(ins, "starting to create directory %s", parent_path);
417+
p = strstr(parent_path, FLB_PATH_SEPARATOR);
418+
if (p != NULL && PathRemoveFileSpecA(parent_path)) {
419+
flb_plg_debug(ins, "creating directory (recursive) %s", parent_path);
420+
ret = mkpath(ins, parent_path);
421+
if (ret != 0) {
422+
/* If creating the parent failed, we cannot continue. */
423+
return -1;
424+
}
425+
}
426+
427+
flb_plg_debug(ins, "creating directory %s", dir);
428+
if (!CreateDirectoryA(dir, NULL)) {
429+
err = GetLastError();
430+
431+
if (err != ERROR_ALREADY_EXISTS) {
432+
flb_plg_error(ins, "could not create directory '%s' (error=%lu)",
433+
dir, err);
434+
return -1;
435+
}
436+
}
437+
403438
return 0;
404439
#elif FLB_SYSTEM_MACOS
405440
dup_dir = strdup(dir);
@@ -471,11 +506,11 @@ static void cb_file_flush(struct flb_event_chunk *event_chunk,
471506
/* Set the right output file */
472507
if (ctx->out_path) {
473508
if (ctx->out_file) {
474-
snprintf(out_file, PATH_MAX - 1, "%s/%s",
509+
snprintf(out_file, PATH_MAX - 1, "%s" FLB_PATH_SEPARATOR "%s",
475510
ctx->out_path, ctx->out_file);
476511
}
477512
else {
478-
snprintf(out_file, PATH_MAX - 1, "%s/%s",
513+
snprintf(out_file, PATH_MAX - 1, "%s" FLB_PATH_SEPARATOR "%s",
479514
ctx->out_path, event_chunk->tag);
480515
}
481516
}

0 commit comments

Comments
 (0)