From 432116f76a7ffc0234bcde615566d4fc24671b4e Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Wed, 24 Jan 2024 21:50:59 -0800 Subject: [PATCH] [windows] Use fewer system calls to get the file type in the module resolver --- src/fs.zig | 5 +---- src/windows.zig | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/fs.zig b/src/fs.zig index dab45f0df6..f0d99b31e5 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -1272,10 +1272,7 @@ pub const FileSystem = struct { const absolute_path_c: [:0]const u8 = outpath[0..entry_path.len :0]; if (comptime bun.Environment.isWindows) { - var file = try std.fs.openFileAbsoluteZ(absolute_path_c, .{ .mode = .read_only }); - defer file.close(); - const metadata = try file.metadata(); - cache.kind = switch (metadata.kind()) { + cache.kind = switch (bun.windows.getFileKindFastA(absolute_path_c)) { .directory => .dir, .sym_link => .file, else => .file, diff --git a/src/windows.zig b/src/windows.zig index 38d5801c17..bab2f862b1 100644 --- a/src/windows.zig +++ b/src/windows.zig @@ -3016,3 +3016,29 @@ pub extern "kernel32" fn GetTempPath2W( nBufferLength: DWORD, // [in] lpBuffer: LPCWSTR, // [out] ) DWORD; + +pub fn getFileKindFastA( + utf8_path: []const u8, +) ?std.fs.File.Kind { + var wbuf: bun.WPathBuffer = undefined; + const path = bun.strings.toWPath(&wbuf, utf8_path); + const attrs = kernel32.GetFileAttributesW(path.ptr); + + if (attrs == INVALID_FILE_ATTRIBUTES) { + return null; + } + + if ((attrs & FILE_ATTRIBUTE_DIRECTORY) != 0) { + return .dir; + } + + if ((attrs & FILE_ATTRIBUTE_REPARSE_POINT) != 0) { + return .sym_link; + } + + if ((attrs & FILE_ATTRIBUTE_DEVICE) != 0) { + return .char_device; + } + + return .file; +}