45
45
#define NEWLINE "\n"
46
46
#endif
47
47
48
+ #ifdef FLB_SYSTEM_WINDOWS
49
+ #define FLB_PATH_SEPARATOR "\\"
50
+ #else
51
+ #define FLB_PATH_SEPARATOR "/"
52
+ #endif
53
+
48
54
struct flb_file_conf {
49
55
const char * out_path ;
50
56
const char * out_file ;
@@ -368,7 +374,11 @@ static int mkpath(struct flb_output_instance *ins, const char *dir)
368
374
#ifdef FLB_SYSTEM_MACOS
369
375
char * parent_dir = NULL ;
370
376
#endif
371
-
377
+ #ifdef FLB_SYSTEM_WINDOWS
378
+ char parent_path [MAX_PATH ];
379
+ DWORD err ;
380
+ char * p ;
381
+ #endif
372
382
int ret ;
373
383
374
384
if (!dir ) {
@@ -391,15 +401,40 @@ static int mkpath(struct flb_output_instance *ins, const char *dir)
391
401
}
392
402
393
403
#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 );
397
406
return -1 ;
398
407
}
399
408
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
+ }
402
414
}
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
+
403
438
return 0 ;
404
439
#elif FLB_SYSTEM_MACOS
405
440
dup_dir = strdup (dir );
@@ -471,11 +506,11 @@ static void cb_file_flush(struct flb_event_chunk *event_chunk,
471
506
/* Set the right output file */
472
507
if (ctx -> out_path ) {
473
508
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" ,
475
510
ctx -> out_path , ctx -> out_file );
476
511
}
477
512
else {
478
- snprintf (out_file , PATH_MAX - 1 , "%s/ %s" ,
513
+ snprintf (out_file , PATH_MAX - 1 , "%s" FLB_PATH_SEPARATOR " %s" ,
479
514
ctx -> out_path , event_chunk -> tag );
480
515
}
481
516
}
0 commit comments