|
| 1 | +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | + |
| 3 | +/* Fluent Bit |
| 4 | + * ========== |
| 5 | + * Copyright (C) 2019-2020 The Fluent Bit Authors |
| 6 | + * Copyright (C) 2015-2018 Treasure Data Inc. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +#include <Windows.h> |
| 22 | +#include <stdlib.h> |
| 23 | +#include <stdint.h> |
| 24 | +#include <io.h> |
| 25 | +#include "interface.h" |
| 26 | + |
| 27 | +/* |
| 28 | + * NTFS stat(2) emulation tailored for in_tail's usage. |
| 29 | + * |
| 30 | + * (1) Support st_ino (inode) for Windows NTFS. |
| 31 | + * (2) Support NTFS symlinks. |
| 32 | + * (3) Support large files >= 2GB. |
| 33 | + * |
| 34 | + * To use it, include "win32.h" and it will transparently |
| 35 | + * replace stat(), lstat() and fstat(). |
| 36 | + */ |
| 37 | + |
| 38 | +#define UINT64(high, low) ((uint64_t) (high) << 32 | (low)) |
| 39 | + |
| 40 | +static int get_mode(unsigned int attr) |
| 41 | +{ |
| 42 | + if (attr & FILE_ATTRIBUTE_DIRECTORY) { |
| 43 | + return WIN32_S_IFDIR; |
| 44 | + } |
| 45 | + return WIN32_S_IFREG; |
| 46 | +} |
| 47 | + |
| 48 | +static int is_symlink(const char *path) |
| 49 | +{ |
| 50 | + WIN32_FIND_DATA data; |
| 51 | + HANDLE h; |
| 52 | + |
| 53 | + h = FindFirstFileA(path, &data); |
| 54 | + if (h == INVALID_HANDLE_VALUE) { |
| 55 | + return 0; |
| 56 | + } |
| 57 | + FindClose(h); |
| 58 | + |
| 59 | + /* |
| 60 | + * A NTFS symlink is a file with a bit of metadata ("reparse point"), |
| 61 | + * So (1) check if the file has metadata and then (2) confirm that |
| 62 | + * it is indeed a symlink. |
| 63 | + */ |
| 64 | + if (data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 65 | + if (data.dwReserved0 == IO_REPARSE_TAG_SYMLINK) { |
| 66 | + return 1; |
| 67 | + } |
| 68 | + } |
| 69 | + return 0; |
| 70 | +} |
| 71 | + |
| 72 | +static int hstat(HANDLE h, struct win32_stat *wst) |
| 73 | +{ |
| 74 | + BY_HANDLE_FILE_INFORMATION info; |
| 75 | + FILE_STANDARD_INFO std; |
| 76 | + FILETIME time; |
| 77 | + |
| 78 | + if (!GetFileInformationByHandle(h, &info)) { |
| 79 | + return -1; |
| 80 | + } |
| 81 | + |
| 82 | + if (!GetFileInformationByHandleEx(h, FileStandardInfo, |
| 83 | + &std, sizeof(std))) { |
| 84 | + return -1; |
| 85 | + } |
| 86 | + |
| 87 | + wst->st_nlink = std.NumberOfLinks; |
| 88 | + if (std.DeletePending) { |
| 89 | + wst->st_nlink = 0; |
| 90 | + } |
| 91 | + time = info.ftLastWriteTime; |
| 92 | + |
| 93 | + wst->st_mode = get_mode(info.dwFileAttributes); |
| 94 | + wst->st_size = UINT64(info.nFileSizeHigh, info.nFileSizeLow); |
| 95 | + wst->st_ino = UINT64(info.nFileIndexHigh, info.nFileIndexLow); |
| 96 | + wst->st_mtime = UINT64(time.dwHighDateTime, time.dwLowDateTime); |
| 97 | + |
| 98 | + return 0; |
| 99 | +} |
| 100 | + |
| 101 | +int win32_stat(const char *path, struct win32_stat *wst) |
| 102 | +{ |
| 103 | + HANDLE h; |
| 104 | + |
| 105 | + h = CreateFileA(path, |
| 106 | + GENERIC_READ, |
| 107 | + FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, |
| 108 | + NULL, /* lpSecurityAttributes */ |
| 109 | + OPEN_EXISTING, /* dwCreationDisposition */ |
| 110 | + 0, /* dwFlagsAndAttributes */ |
| 111 | + NULL); /* hTemplateFile */ |
| 112 | + |
| 113 | + if (h == INVALID_HANDLE_VALUE) { |
| 114 | + return -1; |
| 115 | + } |
| 116 | + |
| 117 | + if (hstat(h, wst)) { |
| 118 | + CloseHandle(h); |
| 119 | + return -1; |
| 120 | + } |
| 121 | + |
| 122 | + CloseHandle(h); |
| 123 | + return 0; |
| 124 | +} |
| 125 | + |
| 126 | +int win32_lstat(const char *path, struct win32_stat *wst) |
| 127 | +{ |
| 128 | + HANDLE h; |
| 129 | + |
| 130 | + h = CreateFileA(path, |
| 131 | + GENERIC_READ, |
| 132 | + FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, |
| 133 | + NULL, /* lpSecurityAttributes */ |
| 134 | + OPEN_EXISTING, /* dwCreationDisposition */ |
| 135 | + FILE_FLAG_OPEN_REPARSE_POINT, |
| 136 | + NULL); /* hTemplateFile */ |
| 137 | + |
| 138 | + if (h == INVALID_HANDLE_VALUE) { |
| 139 | + return -1; |
| 140 | + } |
| 141 | + |
| 142 | + if (hstat(h, wst)) { |
| 143 | + CloseHandle(h); |
| 144 | + return -1; |
| 145 | + } |
| 146 | + |
| 147 | + if (is_symlink(path)) { |
| 148 | + wst->st_mode = WIN32_S_IFLNK; |
| 149 | + } |
| 150 | + |
| 151 | + CloseHandle(h); |
| 152 | + return 0; |
| 153 | +} |
| 154 | + |
| 155 | +int win32_fstat(int fd, struct win32_stat *wst) |
| 156 | +{ |
| 157 | + HANDLE h; |
| 158 | + |
| 159 | + h = (HANDLE) _get_osfhandle(fd); |
| 160 | + if (h == INVALID_HANDLE_VALUE) { |
| 161 | + return -1; |
| 162 | + } |
| 163 | + |
| 164 | + return hstat(h, wst); |
| 165 | +} |
0 commit comments