summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/chunkio/src/win32/dirent.c
blob: 6ea57f9626a3df26fa0085e6f98c1c76479ed425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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;
}