summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/plugins/in_tail/win32/stat.c
blob: bce80274960393678d6df1c4ae70fb61814e3102 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2022 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.
 */

#include <Windows.h>
#include <stdlib.h>
#include <stdint.h>
#include <io.h>
#include "interface.h"

/*
 * NTFS stat(2) emulation tailored for in_tail's usage.
 *
 * (1) Support st_ino (inode) for Windows NTFS.
 * (2) Support NTFS symlinks.
 * (3) Support large files >= 2GB.
 *
 * To use it, include "win32.h" and it will transparently
 * replace stat(), lstat() and fstat().
 */

#define UINT64(high, low) ((uint64_t) (high) << 32 | (low))
#define WINDOWS_TICKS_TO_SECONDS_RATIO 10000000
#define WINDOWS_EPOCH_TO_UNIX_EPOCH_DELTA 11644473600

/* 
 * FILETIME timestamps are represented in 100-nanosecond intervals,
 * because of this, that's why we need to divide the number by 10000000
 * in order to convert it to seconds.
 *
 * While UNIX timestamps use January 1, 1970 as epoch Windows FILETIME
 * timestamps use January 1, 1601. Because of this we need to subtract
 * 11644473600 seconds to account for it.
 *
 * Note: Even though this does not account for leap seconds it should be
 * accurate enough.
 */

static uint64_t filetime_to_epoch(FILETIME *ft)
{
    ULARGE_INTEGER timestamp;

    if (ft == NULL) {
        return 0;
    }

    timestamp.HighPart = ft->dwHighDateTime;
    timestamp.LowPart = ft->dwLowDateTime;

    timestamp.QuadPart /= WINDOWS_TICKS_TO_SECONDS_RATIO;
    timestamp.QuadPart -= WINDOWS_EPOCH_TO_UNIX_EPOCH_DELTA;

    return timestamp.QuadPart;
}

static void reset_errno()
{
    errno = 0;
}

static void propagate_last_error_to_errno()
{
    DWORD error_code;

    error_code = GetLastError();

    switch (error_code) {
        case ERROR_INVALID_TARGET_HANDLE:
        case ERROR_INVALID_HANDLE:
            errno = EBADF;
            break;

        case ERROR_TOO_MANY_OPEN_FILES:
            errno = EMFILE;
            break;

        case ERROR_INVALID_FLAG_NUMBER:
        case ERROR_INVALID_PARAMETER:
            errno = EINVAL;
            break;

        case ERROR_NOT_ENOUGH_MEMORY:
        case ERROR_OUTOFMEMORY:
            errno = ENOMEM;
            break;

        case ERROR_SHARING_VIOLATION:
        case ERROR_LOCK_VIOLATION:
        case ERROR_PATH_BUSY:
        case ERROR_BUSY:
            errno = EBUSY;
            break;

        case ERROR_HANDLE_DISK_FULL:
        case ERROR_DISK_FULL:
            errno = ENOSPC;
            break;

        case ERROR_INVALID_ADDRESS:
            errno = EFAULT;
            break;

        case ERROR_FILE_TOO_LARGE:
            errno = EFBIG;
            break;

        case ERROR_ALREADY_EXISTS:
        case ERROR_FILE_EXISTS:
            errno = EEXIST;
            break;

        case ERROR_FILE_NOT_FOUND:
        case ERROR_PATH_NOT_FOUND:
        case ERROR_INVALID_DRIVE:
        case ERROR_BAD_PATHNAME:
        case ERROR_INVALID_NAME:
        case ERROR_BAD_UNIT:
            errno = ENOENT;
            break;

        case ERROR_SEEK_ON_DEVICE:
        case ERROR_NEGATIVE_SEEK:
            errno = ESPIPE;
            break;

        case ERROR_ACCESS_DENIED:
            errno = EACCES;
            break;

        case ERROR_DIR_NOT_EMPTY:
            errno = ENOTEMPTY;
            break;

        case ERROR_BROKEN_PIPE:
            errno = EPIPE;
            break;

        case ERROR_GEN_FAILURE:
            errno = EIO;
            break;

        case ERROR_OPEN_FAILED:
            errno = EIO;
            break;

        case ERROR_SUCCESS:
            errno = 0;
            break;

        default:
            /* This is just a canary, if you find this
             * error then it means we need to expand the
             * translation list.
             */

            errno = EOWNERDEAD;
            break;
    }
}

static int get_mode(unsigned int attr)
{
    if (attr & FILE_ATTRIBUTE_DIRECTORY) {
        return WIN32_S_IFDIR;
    }
    return WIN32_S_IFREG;
}



static int is_symlink(const char *path)
{
    WIN32_FIND_DATA data;
    HANDLE h;

    SetLastError(0);
    reset_errno();

    h = FindFirstFileA(path, &data);

    if (h == INVALID_HANDLE_VALUE) {
        propagate_last_error_to_errno();

        return 0;
    }

    FindClose(h);

    /*
     * A NTFS symlink is a file with a bit of metadata ("reparse point"),
     * So (1) check if the file has metadata and then (2) confirm that
     * it is indeed a symlink.
     */
    if (data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
        if (data.dwReserved0 == IO_REPARSE_TAG_SYMLINK) {
            return 1;
        }
    }

    return 0;
}

static int hstat(HANDLE h, struct win32_stat *wst)
{
    BY_HANDLE_FILE_INFORMATION info;
    FILE_STANDARD_INFO std;

    SetLastError(0);
    reset_errno();

    if (!GetFileInformationByHandle(h, &info)) {
        propagate_last_error_to_errno();

        return -1;
    }

    if (!GetFileInformationByHandleEx(h, FileStandardInfo,
                                      &std, sizeof(std))) {
        propagate_last_error_to_errno();

        return -1;
    }

    wst->st_nlink = std.NumberOfLinks;
    if (std.DeletePending) {
        wst->st_nlink = 0;
    }

    wst->st_mode  = get_mode(info.dwFileAttributes);
    wst->st_size  = UINT64(info.nFileSizeHigh, info.nFileSizeLow);
    wst->st_ino   = UINT64(info.nFileIndexHigh, info.nFileIndexLow);
    wst->st_mtime = filetime_to_epoch(&info.ftLastWriteTime);

    return 0;
}

int win32_stat(const char *path, struct win32_stat *wst)
{
    HANDLE h;

    SetLastError(0);
    reset_errno();

    h = CreateFileA(path,
                    GENERIC_READ,
                    FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
                    NULL,           /* lpSecurityAttributes */
                    OPEN_EXISTING,  /* dwCreationDisposition */
                    0,              /* dwFlagsAndAttributes */
                    NULL);          /* hTemplateFile */

    if (h == INVALID_HANDLE_VALUE) {
        propagate_last_error_to_errno();

        return -1;
    }

    if (hstat(h, wst)) {
        CloseHandle(h);
        return -1;
    }

    CloseHandle(h);
    return 0;
}

int win32_lstat(const char *path, struct win32_stat *wst)
{
    HANDLE h;

    SetLastError(0);
    reset_errno();

    h = CreateFileA(path,
                    GENERIC_READ,
                    FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
                    NULL,           /* lpSecurityAttributes */
                    OPEN_EXISTING,  /* dwCreationDisposition */
                    FILE_FLAG_OPEN_REPARSE_POINT,
                    NULL);          /* hTemplateFile */

    if (h == INVALID_HANDLE_VALUE) {
        propagate_last_error_to_errno();

        return -1;
    }

    if (hstat(h, wst)) {
        CloseHandle(h);
        return -1;
    }

    if (is_symlink(path)) {
        wst->st_mode = WIN32_S_IFLNK;
    }

    CloseHandle(h);
    return 0;
}

int win32_fstat(int fd, struct win32_stat *wst)
{
    HANDLE h;

    SetLastError(0);
    reset_errno();

    h = (HANDLE) _get_osfhandle(fd);

    if (h == INVALID_HANDLE_VALUE) {
        propagate_last_error_to_errno();

        return -1;
    }

    return hstat(h, wst);
}