MegaBites-AI/Windows-powershell
0308
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4#nullable enable5 6using System.Diagnostics.CodeAnalysis;7using System.IO;8using System.Management.Automation;9using System.Runtime.InteropServices;10 11using Microsoft.Win32.SafeHandles;12 13internal static partial class Interop14{15 [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "Keep native struct names.")]16 [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter", Justification = "Keep native struct names.")]17 internal static unsafe partial class Windows18 {19 internal const int MAX_PATH = 260;20 21 internal struct FILE_TIME22 {23 public uint dwLowDateTime;24 public uint dwHighDateTime;25 }26 27 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]28 internal unsafe struct WIN32_FIND_DATA29 {30 internal uint dwFileAttributes;31 internal FILE_TIME ftCreationTime;32 internal FILE_TIME ftLastAccessTime;33 internal FILE_TIME ftLastWriteTime;34 internal uint nFileSizeHigh;35 internal uint nFileSizeLow;36 internal uint dwReserved0;37 internal uint dwReserved1;38 internal fixed char cFileName[MAX_PATH];39 internal fixed char cAlternateFileName[14];40 }41 42 internal sealed class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid43 {44 // .NET 8 requires the default constructor to be public45 public SafeFindHandle() : base(true) { }46 47 protected override bool ReleaseHandle()48 {49 return Interop.Windows.FindClose(this.handle);50 }51 }52 53 // We use 'FindFirstFileW' instead of 'FindFirstFileExW' because the latter doesn't work correctly with Unicode file names on FAT32.54 // See https://github.com/PowerShell/PowerShell/issues/1680455 [LibraryImport("api-ms-win-core-file-l1-1-0.dll", EntryPoint = "FindFirstFileW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]56 private static partial SafeFindHandle FindFirstFileW(string lpFileName, ref WIN32_FIND_DATA lpFindFileData);57 58 internal static SafeFindHandle FindFirstFile(string lpFileName, ref WIN32_FIND_DATA lpFindFileData)59 {60 lpFileName = Path.TrimEndingDirectorySeparator(lpFileName);61 lpFileName = PathUtils.EnsureExtendedPrefixIfNeeded(lpFileName);62 63 return FindFirstFileW(lpFileName, ref lpFindFileData);64 }65 }66}67 