diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 02:57:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 02:57:58 +0000 |
commit | be1c7e50e1e8809ea56f2c9d472eccd8ffd73a97 (patch) | |
tree | 9754ff1ca740f6346cf8483ec915d4054bc5da2d /fluent-bit/lib/chunkio/src/win32 | |
parent | Initial commit. (diff) | |
download | netdata-be1c7e50e1e8809ea56f2c9d472eccd8ffd73a97.tar.xz netdata-be1c7e50e1e8809ea56f2c9d472eccd8ffd73a97.zip |
Adding upstream version 1.44.3.upstream/1.44.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'fluent-bit/lib/chunkio/src/win32')
-rw-r--r-- | fluent-bit/lib/chunkio/src/win32/dirent.c | 135 | ||||
-rw-r--r-- | fluent-bit/lib/chunkio/src/win32/dirent.h | 59 |
2 files changed, 194 insertions, 0 deletions
diff --git a/fluent-bit/lib/chunkio/src/win32/dirent.c b/fluent-bit/lib/chunkio/src/win32/dirent.c new file mode 100644 index 00000000..6ea57f96 --- /dev/null +++ b/fluent-bit/lib/chunkio/src/win32/dirent.c @@ -0,0 +1,135 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +/* Chunk I/O + * ========= + * Copyright 2018-2019 Eduardo Silva <eduardo@monkey.io> + * + * 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. + */ + +/* + * This module implements <dirent.h> emulation layer based on + * Win32's FIndFirstFile/FindNextFile API. + */ + +#include <Windows.h> +#include <shlwapi.h> + +#include "dirent.h" + +struct CIO_WIN32_DIR { + HANDLE h; + char *pattern; + int count; + WIN32_FIND_DATA find_data; + struct cio_win32_dirent dir; +}; + +/* + * Guess POSIX flle type from Win32 file attributes. + */ +static unsigned char get_filetype(int dwFileAttributes) +{ + if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + return DT_DIR; + } + else if (dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { + return DT_LNK; + } + + return DT_REG; +} + +/* + * Construct a match pattern (e.g. 'c:\var\data\*') + */ +static char *create_pattern(const char *path) +{ + char *buf; + size_t len = strlen(path); + size_t buflen = len + 3; + + buf = malloc(buflen); + if (buf == NULL) { + return NULL; + } + + strcpy_s(buf, buflen, path); + + if (path[len - 1] == '\\') { + strcat_s(buf, buflen, "*"); + } + else { + strcat_s(buf, buflen, "\\*"); + } + return buf; +} + +struct CIO_WIN32_DIR *cio_win32_opendir(const char *path) +{ + struct CIO_WIN32_DIR *d; + + if (!PathIsDirectoryA(path)) { + return NULL; + } + + d = calloc(1, sizeof(struct CIO_WIN32_DIR)); + if (d == NULL) { + return NULL; + } + + d->pattern = create_pattern(path); + if (d->pattern == NULL) { + free(d); + return NULL; + } + + d->h = FindFirstFileA(d->pattern, &d->find_data); + if (d->h == INVALID_HANDLE_VALUE) { + return d; + } + return d; +} + +struct cio_win32_dirent *cio_win32_readdir(struct CIO_WIN32_DIR *d) +{ + if (d->h == INVALID_HANDLE_VALUE) { + return NULL; + } + + /* + * The initial entry should be retrieved by FindFirstFile(), + * so we can skip FindNextFile() on the first call. + */ + if (d->count > 0) { + if (FindNextFile(d->h, &d->find_data) == 0) { + return NULL; + } + } + + d->count++; + d->dir.d_name = d->find_data.cFileName; + d->dir.d_type = get_filetype(d->find_data.dwFileAttributes); + + return &d->dir; +} + +int cio_win32_closedir(struct CIO_WIN32_DIR *d) +{ + if (d->h != INVALID_HANDLE_VALUE) { + FindClose(d->h); + } + free(d->pattern); + free(d); + return 0; +} diff --git a/fluent-bit/lib/chunkio/src/win32/dirent.h b/fluent-bit/lib/chunkio/src/win32/dirent.h new file mode 100644 index 00000000..b21148a6 --- /dev/null +++ b/fluent-bit/lib/chunkio/src/win32/dirent.h @@ -0,0 +1,59 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +/* Chunk I/O + * ========= + * Copyright 2018-2019 Eduardo Silva <eduardo@monkey.io> + * + * 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. + */ + +/* + * POSIX <dirent.h> emulation for Windows. + * + * This header file provies a drop-in replacement of opendir(), + * readdir() and closedir() for Windows platform. + */ + +#ifndef CIO_WIN32_DIRENT +#define CIO_WIN32_DIRENT + +struct CIO_WIN32_DIR; + +struct cio_win32_dirent { + int d_ino; + int d_off; + unsigned short d_reclen; + unsigned char d_type; + char *d_name; +}; + +struct CIO_WIN32_DIR *cio_win32_opendir(const char *path); +struct cio_win32_dirent *cio_win32_readdir(struct CIO_WIN32_DIR *d); +int cio_win32_closedir(struct CIO_WIN32_DIR *d); + +#define DIR struct CIO_WIN32_DIR +#define dirent cio_win32_dirent +#define closedir cio_win32_closedir +#define opendir cio_win32_opendir +#define readdir cio_win32_readdir + +#define DT_UNKNOWN -1 +#define DT_BLK 1 +#define DT_CHR 2 +#define DT_DIR 3 +#define DT_FIFO 4 +#define DT_LNK 5 +#define DT_REG 6 +#define DT_SOCK 7 + +#endif |