diff --git a/include/fluent-bit/flb_glob_win32.h b/include/fluent-bit/flb_glob_win32.h new file mode 100644 index 00000000000..692755a660b --- /dev/null +++ b/include/fluent-bit/flb_glob_win32.h @@ -0,0 +1,95 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +/* Fluent Bit + * ========== + * Copyright (C) 2015-2024 The Fluent Bit Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLB_GLOB_WIN32_H +#define FLB_GLOB_WIN32_H + +#include +#include +#include + +#ifdef FLB_SYSTEM_WINDOWS + +#include + +#define FLB_FILE_GLOB_ABORT_ON_ERROR (((uint64_t) 1) << 0) +#define FLB_FILE_GLOB_MARK_DIRECTORIES (((uint64_t) 1) << 1) +#define FLB_FILE_GLOB_DO_NOT_SORT (((uint64_t) 1) << 2) +#define FLB_FILE_GLOB_EXPAND_TILDE (((uint64_t) 1) << 3) + +#define FLB_FILE_GLOB_ERROR_SUCCESS 0 +#define FLB_FILE_GLOB_ERROR_ABORTED 1 +#define FLB_FILE_GLOB_ERROR_NO_MEMORY 2 +#define FLB_FILE_GLOB_ERROR_NO_FILE 3 +#define FLB_FILE_GLOB_ERROR_NO_ACCESS 4 +#define FLB_FILE_GLOB_ERROR_NO_MATCHES 5 +#define FLB_FILE_GLOB_ERROR_NO_MORE_RESULTS 6 +#define FLB_FILE_GLOB_ERROR_OVERSIZED_PATH 7 +#define FLB_FILE_GLOB_ERROR_INVALID_ARGUMENT 8 + +#ifndef GLOB_NOSPACE +#define GLOB_NOSPACE FLB_FILE_GLOB_ERROR_NO_MEMORY +#endif + +#ifndef GLOB_ABORTED +#define GLOB_ABORTED FLB_FILE_GLOB_ERROR_ABORTED +#endif + +#ifndef GLOB_NOMATCH +#define GLOB_NOMATCH FLB_FILE_GLOB_ERROR_NO_MATCHES +#endif + +#ifndef GLOB_ERR +#define GLOB_ERR FLB_FILE_GLOB_ABORT_ON_ERROR +#endif + +#define FLB_FILE_MAX_PATH_LENGTH PATH_MAX + +struct flb_file_glob_inner_entry { + char *path; + struct cfl_list _head; +}; + +struct flb_file_glob_inner_context { + struct flb_file_glob_inner_entry *current_entry; + struct cfl_list results; + size_t entries; + size_t index; + uint64_t flags; +}; + +struct flb_file_glob_context { + struct flb_file_glob_inner_context *inner_context; + uint64_t flags; + char *path; +}; + +typedef struct { + struct flb_file_glob_context inner_context; + char **gl_pathv; + size_t gl_pathc; +} glob_t; + +int glob(const char *path, uint64_t flags, void *unused, glob_t *context); +void globfree(glob_t *context); +int is_directory(char *path, struct stat *fs_entry_metadata); + + +#endif /* FLB_SYSTEM_WINDOWS */ +#endif /* FLB_GLOB_WIN32_H */ \ No newline at end of file diff --git a/plugins/in_blob/blob.c b/plugins/in_blob/blob.c index 5281386e4fe..a479c026dc2 100644 --- a/plugins/in_blob/blob.c +++ b/plugins/in_blob/blob.c @@ -43,7 +43,9 @@ #include "blob_db.h" #include "blob_file.h" -#include "win32_glob.c" +#ifdef FLB_SYSTEM_WINDOWS +#include +#endif /* Define missing GLOB_TILDE if not exists */ #ifndef GLOB_TILDE diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8ba8440ce0c..77fb11f69f1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -131,6 +131,7 @@ if(FLB_SYSTEM_WINDOWS) set(src ${src} flb_dlfcn_win32.c + win32/flb_glob.c ) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W2") endif() diff --git a/plugins/in_blob/win32_glob.c b/src/win32/flb_glob.c similarity index 84% rename from plugins/in_blob/win32_glob.c rename to src/win32/flb_glob.c index 9bba4ca1e1c..8bb0582158c 100644 --- a/plugins/in_blob/win32_glob.c +++ b/src/win32/flb_glob.c @@ -18,7 +18,7 @@ * limitations under the License. */ -#if defined(FLB_SYSTEM_WINDOWS) && !defined(FLB_FILE_GLOB_ERROR_SUCCESS) +#if defined(FLB_SYSTEM_WINDOWS) #include #include @@ -28,73 +28,14 @@ #include #include #include +#include #include #include #include -#define FLB_FILE_GLOB_ABORT_ON_ERROR (((uint64_t) 1) << 0) -#define FLB_FILE_GLOB_MARK_DIRECTORIES (((uint64_t) 1) << 1) -#define FLB_FILE_GLOB_DO_NOT_SORT (((uint64_t) 1) << 2) -#define FLB_FILE_GLOB_EXPAND_TILDE (((uint64_t) 1) << 3) - -#define FLB_FILE_GLOB_ERROR_SUCCESS 0 -#define FLB_FILE_GLOB_ERROR_ABORTED 1 -#define FLB_FILE_GLOB_ERROR_NO_MEMORY 2 -#define FLB_FILE_GLOB_ERROR_NO_FILE 3 -#define FLB_FILE_GLOB_ERROR_NO_ACCESS 4 -#define FLB_FILE_GLOB_ERROR_NO_MATCHES 5 -#define FLB_FILE_GLOB_ERROR_NO_MORE_RESULTS 6 -#define FLB_FILE_GLOB_ERROR_OVERSIZED_PATH 7 -#define FLB_FILE_GLOB_ERROR_INVALID_ARGUMENT 8 - -#ifndef GLOB_NOSPACE -#define GLOB_NOSPACE FLB_FILE_GLOB_ERROR_NO_MEMORY -#endif - -#ifndef GLOB_ABORTED -#define GLOB_ABORTED FLB_FILE_GLOB_ERROR_ABORTED -#endif - -#ifndef GLOB_NOMATCH -#define GLOB_NOMATCH FLB_FILE_GLOB_ERROR_NO_MATCHES -#endif - -#ifndef GLOB_ERR -#define GLOB_ERR FLB_FILE_GLOB_ABORT_ON_ERROR -#endif - -#define FLB_FILE_MAX_PATH_LENGTH PATH_MAX - #define FLB_FILE_ISTYPE(m, t) (((m) & 0170000) == t) -struct flb_file_glob_inner_entry { - char *path; - struct cfl_list _head; -}; - -struct flb_file_glob_inner_context { - struct flb_file_glob_inner_entry *current_entry; - struct cfl_list results; - size_t entries; - size_t index; - uint64_t flags; -}; - -struct flb_file_glob_context { - struct flb_file_glob_inner_context *inner_context; - uint64_t flags; - char *path; -}; - -struct glob_t { - struct flb_file_glob_context inner_context; - char **gl_pathv; - size_t gl_pathc; -}; - -typedef struct glob_t glob_t; - static int flb_file_glob_start(struct flb_file_glob_context *context, const char *path, uint64_t flags); @@ -104,7 +45,7 @@ static void flb_file_glob_clean(struct flb_file_glob_context *context); static int flb_file_glob_fetch(struct flb_file_glob_context *context, char **result); -static void globfree(glob_t *context) +void globfree(glob_t *context) { size_t index; @@ -116,7 +57,7 @@ static void globfree(glob_t *context) flb_file_glob_clean(&context->inner_context); } -static int glob(const char *path, +int glob(const char *path, uint64_t flags, void *unused, glob_t *context) @@ -127,7 +68,7 @@ static int glob(const char *path, (void) unused; - result = flb_file_glob_start(context, path, flags); + result = flb_file_glob_start(&context->inner_context, path, flags); if (result == FLB_FILE_GLOB_ERROR_SUCCESS) { entries = cfl_list_size(&context->inner_context.inner_context->results); @@ -150,14 +91,15 @@ static int glob(const char *path, return result; } } + context->gl_pathc = entries; } return result; } -static int is_directory(char *path, struct stat *fs_entry_metadata) +int is_directory(char *path, struct stat *fs_entry_metadata) { - return (fs_entry_metadata->st_mode & S_IFDIR != 0); + return ((fs_entry_metadata->st_mode & S_IFDIR) != 0); } static void reset_errno() @@ -485,9 +427,9 @@ static int limited_win32_glob(struct flb_file_glob_inner_context *context, return ret; } -int flb_file_glob_start(struct flb_file_glob_context *context, - const char *path, - uint64_t flags) +static int flb_file_glob_start(struct flb_file_glob_context *context, + const char *path, + uint64_t flags) { int tilde_expansion_attempted; @@ -528,7 +470,7 @@ int flb_file_glob_start(struct flb_file_glob_context *context, context->path); } -void flb_file_glob_clean(struct flb_file_glob_context *context) +static void flb_file_glob_clean(struct flb_file_glob_context *context) { struct cfl_list *iterator_backup; struct cfl_list *iterator; @@ -564,8 +506,8 @@ void flb_file_glob_clean(struct flb_file_glob_context *context) } -int flb_file_glob_fetch(struct flb_file_glob_context *context, - char **result) +static int flb_file_glob_fetch(struct flb_file_glob_context *context, + char **result) { if (context == NULL) { diff --git a/tests/internal/CMakeLists.txt b/tests/internal/CMakeLists.txt index 6984f69d80f..0793a3f59c7 100644 --- a/tests/internal/CMakeLists.txt +++ b/tests/internal/CMakeLists.txt @@ -83,6 +83,13 @@ if(FLB_HAVE_LIBYAML) ) endif() +if (WIN32) + set(UNIT_TESTS_FILES + ${UNIT_TESTS_FILES} + win32_glob.c + ) +endif() + if (NOT WIN32) set(UNIT_TESTS_FILES ${UNIT_TESTS_FILES} diff --git a/tests/internal/win32_glob.c b/tests/internal/win32_glob.c new file mode 100644 index 00000000000..0beae798250 --- /dev/null +++ b/tests/internal/win32_glob.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include "flb_tests_internal.h" + +#ifdef FLB_SYSTEM_WINDOWS +#include + +void test_glob_basic() +{ + glob_t glob_data; + int ret; + FILE *fp; + + /* Create some dummy files */ + fp = fopen("test_glob_1.txt", "w"); + if (fp) fclose(fp); + fp = fopen("test_glob_2.txt", "w"); + if (fp) fclose(fp); + + ret = glob("test_glob_*.txt", 0, NULL, &glob_data); + TEST_CHECK(ret == 0); + TEST_CHECK(glob_data.gl_pathc == 2); + + globfree(&glob_data); + + /* Cleanup */ + unlink("test_glob_1.txt"); + unlink("test_glob_2.txt"); +} + +void test_glob_nomatch() +{ + glob_t glob_data = {0}; + int ret; + + ret = glob("non_existent_*.txt", 0, NULL, &glob_data); + TEST_CHECK(ret == GLOB_NOMATCH); + + globfree(&glob_data); +} + +void test_glob_wildcard() +{ + glob_t glob_data; + int ret; + FILE *fp; + + /* Create dummy file */ + fp = fopen("test_wildcard.txt", "w"); + if (fp) fclose(fp); + + ret = glob("test_wild*.txt", 0, NULL, &glob_data); + TEST_CHECK(ret == 0); + TEST_CHECK(glob_data.gl_pathc == 1); + if (glob_data.gl_pathc > 0) { + TEST_CHECK(strstr(glob_data.gl_pathv[0], "test_wildcard.txt") != NULL); + } + + globfree(&glob_data); + unlink("test_wildcard.txt"); +} + +TEST_LIST = { + { "basic", test_glob_basic }, + { "nomatch", test_glob_nomatch }, + { "wildcard", test_glob_wildcard }, + { 0 } +}; +#else +TEST_LIST = { + { 0 } +}; +#endif \ No newline at end of file