From db20f3f1bb8595633a7e16c8900fd401a453a6b5 Mon Sep 17 00:00:00 2001 From: Jiyoung Yun Date: Tue, 27 Dec 2016 16:46:08 +0900 Subject: Imported Upstream version 1.0.0.9127 --- src/mscorlib/corefx/Interop/Unix/Interop.Errors.cs | 207 +++++++++++++++++++++ .../corefx/Interop/Unix/Interop.IOErrors.cs | 170 +++++++++++++++++ .../corefx/Interop/Unix/Interop.Libraries.cs | 7 + .../Interop.Collation.cs | 5 +- .../System.Globalization.Native/Interop.Idna.cs | 21 +++ .../System.Globalization.Native/Interop.Locale.cs | 3 + .../Interop.Normalization.cs | 19 ++ .../Interop.ResultCode.cs | 1 + .../Interop/Unix/System.Native/Interop.Close.cs | 15 ++ .../Interop/Unix/System.Native/Interop.FLock.cs | 31 +++ .../Interop/Unix/System.Native/Interop.FSync.cs | 15 ++ .../Unix/System.Native/Interop.FTruncate.cs | 15 ++ .../Interop/Unix/System.Native/Interop.Fcntl.cs | 21 +++ .../Interop/Unix/System.Native/Interop.GetCwd.cs | 74 ++++++++ .../Unix/System.Native/Interop.GetUnixName.cs | 21 +++ .../Interop/Unix/System.Native/Interop.LSeek.cs | 22 +++ .../Interop/Unix/System.Native/Interop.MksTemps.cs | 17 ++ .../Interop/Unix/System.Native/Interop.Open.cs | 15 ++ .../Unix/System.Native/Interop.OpenFlags.cs | 27 +++ .../Interop/Unix/System.Native/Interop.PathConf.cs | 73 ++++++++ .../Unix/System.Native/Interop.Permissions.cs | 32 ++++ .../Unix/System.Native/Interop.PosixFAdvise.cs | 36 ++++ .../Interop/Unix/System.Native/Interop.Read.cs | 25 +++ .../Interop/Unix/System.Native/Interop.Stat.cs | 59 ++++++ .../Interop/Unix/System.Native/Interop.Unlink.cs | 15 ++ .../Interop/Unix/System.Native/Interop.Write.cs | 27 +++ .../Windows/BCrypt/Interop.BCryptGenRandom.cs | 26 +++ .../Interop/Windows/BCrypt/Interop.NTSTATUS.cs | 19 ++ .../Windows/Crypt32/Interop.CryptProtectMemory.cs | 22 +++ .../corefx/Interop/Windows/Interop.BOOL.cs | 21 +++ .../corefx/Interop/Windows/Interop.Libraries.cs | 82 ++++++++ .../Interop/Windows/NtDll/Interop.ZeroMemory.cs | 19 ++ .../Interop/Windows/mincore/Interop.CancelIoEx.cs | 16 ++ .../Interop/Windows/mincore/Interop.CloseHandle.cs | 16 ++ .../Interop/Windows/mincore/Interop.CreateFile.cs | 40 ++++ .../Interop/Windows/mincore/Interop.Errors.cs | 74 ++++++++ .../Windows/mincore/Interop.FileOperations.cs | 35 ++++ .../Interop/Windows/mincore/Interop.FileTypes.cs | 16 ++ .../Windows/mincore/Interop.FlushFileBuffers.cs | 17 ++ .../Windows/mincore/Interop.FormatMessage.cs | 112 +++++++++++ .../Interop.GetFileInformationByHandleEx.cs | 26 +++ .../mincore/Interop.GetFileType_SafeHandle.cs | 15 ++ .../Windows/mincore/Interop.GetFullPathNameW.cs | 18 ++ .../Windows/mincore/Interop.GetLongPathNameW.cs | 18 ++ .../Windows/mincore/Interop.GetTempFileNameW.cs | 16 ++ .../Windows/mincore/Interop.GetTempPathW.cs | 16 ++ .../corefx/Interop/Windows/mincore/Interop.Idna.cs | 37 ++++ .../Interop/Windows/mincore/Interop.LockFile.cs | 20 ++ .../mincore/Interop.ReadFile_SafeHandle_IntPtr.cs | 21 +++ ...Interop.ReadFile_SafeHandle_NativeOverlapped.cs | 22 +++ .../Windows/mincore/Interop.SECURITY_ATTRIBUTES.cs | 21 +++ .../Windows/mincore/Interop.SafeCreateFile.cs | 45 +++++ .../Windows/mincore/Interop.SecurityOptions.cs | 18 ++ .../Windows/mincore/Interop.SetEndOfFile.cs | 15 ++ .../Windows/mincore/Interop.SetErrorMode.cs | 14 ++ .../mincore/Interop.SetFileInformationByHandle.cs | 72 +++++++ .../Windows/mincore/Interop.SetFilePointerEx.cs | 15 ++ .../Windows/mincore/Interop.ThreadPoolIO.cs | 27 +++ .../Windows/mincore/Interop.UnsafeCreateFile.cs | 25 +++ .../Windows/mincore/Interop.WideCharToMultiByte.cs | 22 +++ .../mincore/Interop.WriteFile_SafeHandle_IntPtr.cs | 24 +++ ...nterop.WriteFile_SafeHandle_NativeOverlapped.cs | 22 +++ .../Windows/oleaut32/Interop.SysAllocStringLen.cs | 16 ++ .../Windows/oleaut32/Interop.SysStringLen.cs | 22 +++ 64 files changed, 2054 insertions(+), 1 deletion(-) create mode 100644 src/mscorlib/corefx/Interop/Unix/Interop.Errors.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/Interop.IOErrors.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Idna.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Normalization.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Close.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FLock.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FSync.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FTruncate.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Fcntl.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.GetCwd.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.GetUnixName.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.LSeek.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.MksTemps.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Open.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.OpenFlags.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.PathConf.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Permissions.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.PosixFAdvise.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Read.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Stat.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Unlink.cs create mode 100644 src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Write.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/BCrypt/Interop.BCryptGenRandom.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/BCrypt/Interop.NTSTATUS.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/Crypt32/Interop.CryptProtectMemory.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/Interop.BOOL.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/Interop.Libraries.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/NtDll/Interop.ZeroMemory.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.CancelIoEx.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.CloseHandle.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.CreateFile.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.Errors.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.FileOperations.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.FileTypes.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.FlushFileBuffers.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.FormatMessage.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetFileInformationByHandleEx.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetFileType_SafeHandle.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetFullPathNameW.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetLongPathNameW.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetTempFileNameW.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetTempPathW.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.Idna.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.LockFile.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.ReadFile_SafeHandle_IntPtr.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.ReadFile_SafeHandle_NativeOverlapped.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.SECURITY_ATTRIBUTES.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.SafeCreateFile.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.SecurityOptions.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.SetEndOfFile.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.SetErrorMode.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.SetFileInformationByHandle.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.SetFilePointerEx.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.ThreadPoolIO.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.UnsafeCreateFile.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.WideCharToMultiByte.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.WriteFile_SafeHandle_IntPtr.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/mincore/Interop.WriteFile_SafeHandle_NativeOverlapped.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/oleaut32/Interop.SysAllocStringLen.cs create mode 100644 src/mscorlib/corefx/Interop/Windows/oleaut32/Interop.SysStringLen.cs (limited to 'src/mscorlib/corefx/Interop') diff --git a/src/mscorlib/corefx/Interop/Unix/Interop.Errors.cs b/src/mscorlib/corefx/Interop/Unix/Interop.Errors.cs new file mode 100644 index 0000000000..4248434db3 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/Interop.Errors.cs @@ -0,0 +1,207 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + /// Common Unix errno error codes. + internal enum Error + { + // These values were defined in src/Native/System.Native/fxerrno.h + // + // They compare against values obtained via Interop.Sys.GetLastError() not Marshal.GetLastWin32Error() + // which obtains the raw errno that varies between unixes. The strong typing as an enum is meant to + // prevent confusing the two. Casting to or from int is suspect. Use GetLastErrorInfo() if you need to + // correlate these to the underlying platform values or obtain the corresponding error message. + // + + SUCCESS = 0, + + E2BIG = 0x10001, // Argument list too long. + EACCES = 0x10002, // Permission denied. + EADDRINUSE = 0x10003, // Address in use. + EADDRNOTAVAIL = 0x10004, // Address not available. + EAFNOSUPPORT = 0x10005, // Address family not supported. + EAGAIN = 0x10006, // Resource unavailable, try again (same value as EWOULDBLOCK), + EALREADY = 0x10007, // Connection already in progress. + EBADF = 0x10008, // Bad file descriptor. + EBADMSG = 0x10009, // Bad message. + EBUSY = 0x1000A, // Device or resource busy. + ECANCELED = 0x1000B, // Operation canceled. + ECHILD = 0x1000C, // No child processes. + ECONNABORTED = 0x1000D, // Connection aborted. + ECONNREFUSED = 0x1000E, // Connection refused. + ECONNRESET = 0x1000F, // Connection reset. + EDEADLK = 0x10010, // Resource deadlock would occur. + EDESTADDRREQ = 0x10011, // Destination address required. + EDOM = 0x10012, // Mathematics argument out of domain of function. + EDQUOT = 0x10013, // Reserved. + EEXIST = 0x10014, // File exists. + EFAULT = 0x10015, // Bad address. + EFBIG = 0x10016, // File too large. + EHOSTUNREACH = 0x10017, // Host is unreachable. + EIDRM = 0x10018, // Identifier removed. + EILSEQ = 0x10019, // Illegal byte sequence. + EINPROGRESS = 0x1001A, // Operation in progress. + EINTR = 0x1001B, // Interrupted function. + EINVAL = 0x1001C, // Invalid argument. + EIO = 0x1001D, // I/O error. + EISCONN = 0x1001E, // Socket is connected. + EISDIR = 0x1001F, // Is a directory. + ELOOP = 0x10020, // Too many levels of symbolic links. + EMFILE = 0x10021, // File descriptor value too large. + EMLINK = 0x10022, // Too many links. + EMSGSIZE = 0x10023, // Message too large. + EMULTIHOP = 0x10024, // Reserved. + ENAMETOOLONG = 0x10025, // Filename too long. + ENETDOWN = 0x10026, // Network is down. + ENETRESET = 0x10027, // Connection aborted by network. + ENETUNREACH = 0x10028, // Network unreachable. + ENFILE = 0x10029, // Too many files open in system. + ENOBUFS = 0x1002A, // No buffer space available. + ENODEV = 0x1002C, // No such device. + ENOENT = 0x1002D, // No such file or directory. + ENOEXEC = 0x1002E, // Executable file format error. + ENOLCK = 0x1002F, // No locks available. + ENOLINK = 0x10030, // Reserved. + ENOMEM = 0x10031, // Not enough space. + ENOMSG = 0x10032, // No message of the desired type. + ENOPROTOOPT = 0x10033, // Protocol not available. + ENOSPC = 0x10034, // No space left on device. + ENOSYS = 0x10037, // Function not supported. + ENOTCONN = 0x10038, // The socket is not connected. + ENOTDIR = 0x10039, // Not a directory or a symbolic link to a directory. + ENOTEMPTY = 0x1003A, // Directory not empty. + ENOTSOCK = 0x1003C, // Not a socket. + ENOTSUP = 0x1003D, // Not supported (same value as EOPNOTSUP). + ENOTTY = 0x1003E, // Inappropriate I/O control operation. + ENXIO = 0x1003F, // No such device or address. + EOVERFLOW = 0x10040, // Value too large to be stored in data type. + EPERM = 0x10042, // Operation not permitted. + EPIPE = 0x10043, // Broken pipe. + EPROTO = 0x10044, // Protocol error. + EPROTONOSUPPORT = 0x10045, // Protocol not supported. + EPROTOTYPE = 0x10046, // Protocol wrong type for socket. + ERANGE = 0x10047, // Result too large. + EROFS = 0x10048, // Read-only file system. + ESPIPE = 0x10049, // Invalid seek. + ESRCH = 0x1004A, // No such process. + ESTALE = 0x1004B, // Reserved. + ETIMEDOUT = 0x1004D, // Connection timed out. + ETXTBSY = 0x1004E, // Text file busy. + EXDEV = 0x1004F, // Cross-device link. + ESOCKTNOSUPPORT = 0x1005E, // Socket type not supported. + EPFNOSUPPORT = 0x10060, // Protocol family not supported. + ESHUTDOWN = 0x1006C, // Socket shutdown. + EHOSTDOWN = 0x10070, // Host is down. + ENODATA = 0x10071, // No data available. + + // POSIX permits these to have the same value and we make them always equal so + // that CoreFX cannot introduce a dependency on distinguishing between them that + // would not work on all platforms. + EOPNOTSUPP = ENOTSUP, // Operation not supported on socket. + EWOULDBLOCK = EAGAIN, // Operation would block. + } + + + // Represents a platform-agnostic Error and underlying platform-specific errno + internal struct ErrorInfo + { + private Error _error; + private int _rawErrno; + + internal ErrorInfo(int errno) + { + _error = Interop.Sys.ConvertErrorPlatformToPal(errno); + _rawErrno = errno; + } + + internal ErrorInfo(Error error) + { + _error = error; + _rawErrno = -1; + } + + internal Error Error + { + get { return _error; } + } + + internal int RawErrno + { + get { return _rawErrno == -1 ? (_rawErrno = Interop.Sys.ConvertErrorPalToPlatform(_error)) : _rawErrno; } + } + + internal string GetErrorMessage() + { + return Interop.Sys.StrError(RawErrno); + } + + public override string ToString() + { + return string.Format( + "RawErrno: {0} Error: {1} GetErrorMessage: {2}", // No localization required; text is member names used for debugging purposes + RawErrno, Error, GetErrorMessage()); + } + } + + internal partial class Sys + { + internal static Error GetLastError() + { + return ConvertErrorPlatformToPal(Marshal.GetLastWin32Error()); + } + + internal static ErrorInfo GetLastErrorInfo() + { + return new ErrorInfo(Marshal.GetLastWin32Error()); + } + + internal static unsafe string StrError(int platformErrno) + { + int maxBufferLength = 1024; // should be long enough for most any UNIX error + byte* buffer = stackalloc byte[maxBufferLength]; + byte* message = StrErrorR(platformErrno, buffer, maxBufferLength); + + if (message == null) + { + // This means the buffer was not large enough, but still contains + // as much of the error message as possible and is guaranteed to + // be null-terminated. We're not currently resizing/retrying because + // maxBufferLength is large enough in practice, but we could do + // so here in the future if necessary. + message = buffer; + } + + return Marshal.PtrToStringAnsi((IntPtr)message); + } + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_ConvertErrorPlatformToPal")] + internal static extern Error ConvertErrorPlatformToPal(int platformErrno); + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_ConvertErrorPalToPlatform")] + internal static extern int ConvertErrorPalToPlatform(Error error); + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_StrErrorR")] + private static unsafe extern byte* StrErrorR(int platformErrno, byte* buffer, int bufferSize); + } +} + +// NOTE: extension method can't be nested inside Interop class. +internal static class InteropErrorExtensions +{ + // Intended usage is e.g. Interop.Error.EFAIL.Info() for brevity + // vs. new Interop.ErrorInfo(Interop.Error.EFAIL) for synthesizing + // errors. Errors originated from the system should be obtained + // via GetLastErrorInfo(), not GetLastError().Info() as that will + // convert twice, which is not only inefficient but also lossy if + // we ever encounter a raw errno that no equivalent in the Error + // enum. + public static Interop.ErrorInfo Info(this Interop.Error error) + { + return new Interop.ErrorInfo(error); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/Interop.IOErrors.cs b/src/mscorlib/corefx/Interop/Unix/Interop.IOErrors.cs new file mode 100644 index 0000000000..e9d6ce61d6 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/Interop.IOErrors.cs @@ -0,0 +1,170 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Diagnostics; +using System.IO; +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + private static void ThrowExceptionForIoErrno(ErrorInfo errorInfo, string path, bool isDirectory, Func errorRewriter) + { + Debug.Assert(errorInfo.Error != Error.SUCCESS); + Debug.Assert(errorInfo.Error != Error.EINTR, "EINTR errors should be handled by the native shim and never bubble up to managed code"); + + if (errorRewriter != null) + { + errorInfo = errorRewriter(errorInfo); + } + + throw Interop.GetExceptionForIoErrno(errorInfo, path, isDirectory); + } + + internal static void CheckIo(Error error, string path = null, bool isDirectory = false, Func errorRewriter = null) + { + if (error != Interop.Error.SUCCESS) + { + ThrowExceptionForIoErrno(error.Info(), path, isDirectory, errorRewriter); + } + } + + /// + /// Validates the result of system call that returns greater than or equal to 0 on success + /// and less than 0 on failure, with errno set to the error code. + /// If the system call failed for any reason, an exception is thrown. Otherwise, the system call succeeded. + /// + /// The result of the system call. + /// The path with which this error is associated. This may be null. + /// true if the is known to be a directory; otherwise, false. + /// Optional function to change an error code prior to processing it. + /// + /// On success, returns the non-negative result long that was validated. + /// + internal static long CheckIo(long result, string path = null, bool isDirectory = false, Func errorRewriter = null) + { + if (result < 0) + { + ThrowExceptionForIoErrno(Sys.GetLastErrorInfo(), path, isDirectory, errorRewriter); + } + + return result; + } + + /// + /// Validates the result of system call that returns greater than or equal to 0 on success + /// and less than 0 on failure, with errno set to the error code. + /// If the system call failed for any reason, an exception is thrown. Otherwise, the system call succeeded. + /// + /// + /// On success, returns the non-negative result int that was validated. + /// + internal static int CheckIo(int result, string path = null, bool isDirectory = false, Func errorRewriter = null) + { + CheckIo((long)result, path, isDirectory, errorRewriter); + + return result; + } + + /// + /// Validates the result of system call that returns greater than or equal to 0 on success + /// and less than 0 on failure, with errno set to the error code. + /// If the system call failed for any reason, an exception is thrown. Otherwise, the system call succeeded. + /// + /// + /// On success, returns the non-negative result IntPtr that was validated. + /// + internal static IntPtr CheckIo(IntPtr result, string path = null, bool isDirectory = false, Func errorRewriter = null) + { + CheckIo((long)result, path, isDirectory, errorRewriter); + + return result; + } + + /// + /// Validates the result of system call that returns greater than or equal to 0 on success + /// and less than 0 on failure, with errno set to the error code. + /// If the system call failed for any reason, an exception is thrown. Otherwise, the system call succeeded. + /// + /// + /// On success, returns the valid SafeFileHandle that was validated. + /// + internal static TSafeHandle CheckIo(TSafeHandle handle, string path = null, bool isDirectory = false, Func errorRewriter = null) + where TSafeHandle : SafeHandle + { + if (handle.IsInvalid) + { + ThrowExceptionForIoErrno(Sys.GetLastErrorInfo(), path, isDirectory, errorRewriter); + } + + return handle; + } + + /// + /// Gets an Exception to represent the supplied error info. + /// + /// The error info + /// The path with which this error is associated. This may be null. + /// true if the is known to be a directory; otherwise, false. + /// + internal static Exception GetExceptionForIoErrno(ErrorInfo errorInfo, string path = null, bool isDirectory = false) + { + // Translate the errno into a known set of exception types. For cases where multiple errnos map + // to the same exception type, include an inner exception with the details. + switch (errorInfo.Error) + { + case Error.ENOENT: + if (isDirectory) + { + return !string.IsNullOrEmpty(path) ? + new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, path)) : + new DirectoryNotFoundException(SR.IO_PathNotFound_NoPathName); + } + else + { + return !string.IsNullOrEmpty(path) ? + new FileNotFoundException(SR.Format(SR.IO_FileNotFound_FileName, path), path) : + new FileNotFoundException(SR.IO_FileNotFound); + } + + case Error.EACCES: + case Error.EBADF: + case Error.EPERM: + Exception inner = GetIOException(errorInfo); + return !string.IsNullOrEmpty(path) ? + new UnauthorizedAccessException(SR.Format(SR.UnauthorizedAccess_IODenied_Path, path), inner) : + new UnauthorizedAccessException(SR.UnauthorizedAccess_IODenied_NoPathName, inner); + + case Error.ENAMETOOLONG: + return new PathTooLongException(SR.IO_PathTooLong); + + case Error.EWOULDBLOCK: + return !string.IsNullOrEmpty(path) ? + new IOException(SR.Format(SR.IO_SharingViolation_File, path), errorInfo.RawErrno) : + new IOException(SR.IO_SharingViolation_NoFileName, errorInfo.RawErrno); + + case Error.ECANCELED: + return new OperationCanceledException(); + + case Error.EFBIG: + return new ArgumentOutOfRangeException("value", SR.ArgumentOutOfRange_FileLengthTooBig); + + case Error.EEXIST: + if (!string.IsNullOrEmpty(path)) + { + return new IOException(SR.Format(SR.IO_FileExists_Name, path), errorInfo.RawErrno); + } + goto default; + + default: + return GetIOException(errorInfo); + } + } + + internal static Exception GetIOException(Interop.ErrorInfo errorInfo) + { + return new IOException(errorInfo.GetErrorMessage(), errorInfo.RawErrno); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/Interop.Libraries.cs b/src/mscorlib/corefx/Interop/Unix/Interop.Libraries.cs index f8c5b26e44..a11a23ce8c 100644 --- a/src/mscorlib/corefx/Interop/Unix/Interop.Libraries.cs +++ b/src/mscorlib/corefx/Interop/Unix/Interop.Libraries.cs @@ -7,5 +7,12 @@ internal static partial class Interop private static partial class Libraries { internal const string GlobalizationInterop = "System.Globalization.Native"; // CoreFX wrappers for ICU + // Shims + internal const string SystemNative = "System.Native"; + internal const string HttpNative = "System.Net.Http.Native"; + internal const string NetSecurityNative = "System.Net.Security.Native"; + internal const string CryptoNative = "System.Security.Cryptography.Native.OpenSsl"; + internal const string GlobalizationNative = "System.Globalization.Native"; + internal const string CompressionNative = "System.IO.Compression.Native"; } } diff --git a/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Collation.cs b/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Collation.cs index 6acf55e17b..eb9e0743bc 100644 --- a/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Collation.cs +++ b/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Collation.cs @@ -13,7 +13,7 @@ internal static partial class Interop { [SecurityCritical] [DllImport(Libraries.GlobalizationInterop, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_GetSortHandle")] - internal unsafe static extern SafeSortHandle GetSortHandle(byte[] localeName); + internal unsafe static extern ResultCode GetSortHandle(byte[] localeName, out SafeSortHandle sortHandle); [SecurityCritical] [DllImport(Libraries.GlobalizationInterop, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_CloseSortHandle")] @@ -53,6 +53,9 @@ internal static partial class Interop [DllImport(Libraries.GlobalizationInterop, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_CompareStringOrdinalIgnoreCase")] internal unsafe static extern int CompareStringOrdinalIgnoreCase(char* lpStr1, int cwStr1Len, char* lpStr2, int cwStr2Len); + [DllImport(Libraries.GlobalizationInterop, EntryPoint = "GlobalizationNative_GetSortVersion")] + internal static extern int GetSortVersion(); + [SecurityCritical] internal class SafeSortHandle : SafeHandle { diff --git a/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Idna.cs b/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Idna.cs new file mode 100644 index 0000000000..43c72281ae --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Idna.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class GlobalizationInterop + { + internal const int AllowUnassigned = 0x1; + internal const int UseStd3AsciiRules = 0x2; + + [DllImport(Libraries.GlobalizationInterop, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_ToAscii")] + internal static unsafe extern int ToAscii(uint flags, char* src, int srcLen, char* dstBuffer, int dstBufferCapacity); + + [DllImport(Libraries.GlobalizationInterop, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_ToUnicode")] + internal static unsafe extern int ToUnicode(uint flags, char* src, int srcLen, char* dstBuffer, int dstBufferCapacity); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Locale.cs b/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Locale.cs index 3912581c73..fcea708ee8 100644 --- a/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Locale.cs +++ b/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Locale.cs @@ -33,5 +33,8 @@ internal static partial class Interop [DllImport(Libraries.GlobalizationInterop, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_GetLocaleInfoGroupingSizes")] [return: MarshalAs(UnmanagedType.Bool)] internal unsafe static extern bool GetLocaleInfoGroupingSizes(string localeName, uint localeGroupingData, ref int primaryGroupSize, ref int secondaryGroupSize); + + [DllImport(Libraries.GlobalizationInterop, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_GetLocales")] + internal unsafe static extern int GetLocales([Out] Char[] value, int valueLength); } } diff --git a/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Normalization.cs b/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Normalization.cs new file mode 100644 index 0000000000..c4cb9fb851 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Normalization.cs @@ -0,0 +1,19 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using System.Text; + +internal static partial class Interop +{ + internal static partial class GlobalizationInterop + { + [DllImport(Libraries.GlobalizationInterop, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_IsNormalized")] + internal static extern int IsNormalized(NormalizationForm normalizationForm, string src, int srcLen); + + [DllImport(Libraries.GlobalizationInterop, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_NormalizeString")] + internal static extern int NormalizeString(NormalizationForm normalizationForm, string src, int srcLen, [Out] char[] dstBuffer, int dstBufferCapacity); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.ResultCode.cs b/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.ResultCode.cs index 4621580063..cca6ae4dcb 100644 --- a/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.ResultCode.cs +++ b/src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.ResultCode.cs @@ -12,6 +12,7 @@ internal static partial class Interop Success = 0, UnknownError = 1, InsufficentBuffer = 2, + OutOfMemory = 3 } } } diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Close.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Close.cs new file mode 100644 index 0000000000..8d192398a0 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Close.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Sys + { + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Close", SetLastError = true)] + internal static extern int Close(IntPtr fd); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FLock.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FLock.cs new file mode 100644 index 0000000000..22934a3e77 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FLock.cs @@ -0,0 +1,31 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + internal enum LockOperations : int + { + LOCK_SH = 1, /* shared lock */ + LOCK_EX = 2, /* exclusive lock */ + LOCK_NB = 4, /* don't block when locking*/ + LOCK_UN = 8, /* unlock */ + } + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FLock", SetLastError = true)] + internal static extern int FLock(SafeFileHandle fd, LockOperations operation); + + /// + /// Exposing this for SafeFileHandle.ReleaseHandle() to call. + /// Normal callers should use FLock(SafeFileHandle fd). + /// + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FLock", SetLastError = true)] + internal static extern int FLock(IntPtr fd, LockOperations operation); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FSync.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FSync.cs new file mode 100644 index 0000000000..e3ab970931 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FSync.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FSync", SetLastError = true)] + internal static extern int FSync(SafeFileHandle fd); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FTruncate.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FTruncate.cs new file mode 100644 index 0000000000..5dad650362 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FTruncate.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FTruncate", SetLastError = true)] + internal static extern int FTruncate(SafeFileHandle fd, long length); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Fcntl.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Fcntl.cs new file mode 100644 index 0000000000..23b48a4f5d --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Fcntl.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Sys + { + internal enum LockType : short + { + F_WRLCK = 1, // exclusive or write lock + F_UNLCK = 2 // unlock + } + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_LockFileRegion", SetLastError=true)] + internal static extern int LockFileRegion(SafeHandle fd, long offset, long length, LockType lockType); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.GetCwd.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.GetCwd.cs new file mode 100644 index 0000000000..724e342342 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.GetCwd.cs @@ -0,0 +1,74 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Sys + { + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetCwd", SetLastError = true)] + private static unsafe extern byte* GetCwd(byte* buffer, int bufferLength); + + internal static unsafe string GetCwd() + { + const int StackLimit = 256; + + // First try to get the path into a buffer on the stack + byte* stackBuf = stackalloc byte[StackLimit]; + string result = GetCwdHelper(stackBuf, StackLimit); + if (result != null) + { + return result; + } + + // If that was too small, try increasing large buffer sizes + // until we get one that works or until we hit MaxPath. + int maxPath = Interop.Sys.MaxPath; + if (StackLimit < maxPath) + { + int bufferSize = StackLimit; + do + { + checked { bufferSize *= 2; } + var buf = new byte[Math.Min(bufferSize, maxPath)]; + fixed (byte* ptr = buf) + { + result = GetCwdHelper(ptr, buf.Length); + if (result != null) + { + return result; + } + } + } + while (bufferSize < maxPath); + } + + // If we couldn't get the cwd with a MaxPath-sized buffer, something's wrong. + throw Interop.GetExceptionForIoErrno(new ErrorInfo(Interop.Error.ENAMETOOLONG)); + } + + private static unsafe string GetCwdHelper(byte* ptr, int bufferSize) + { + // Call the real getcwd + byte* result = GetCwd(ptr, bufferSize); + + // If it returned non-null, the null-terminated path is in the buffer + if (result != null) + { + return Marshal.PtrToStringAnsi((IntPtr)ptr); + } + + // Otherwise, if it failed due to the buffer being too small, return null; + // for anything else, throw. + ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo(); + if (errorInfo.Error == Interop.Error.ERANGE) + { + return null; + } + throw Interop.GetExceptionForIoErrno(errorInfo); + } + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.GetUnixName.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.GetUnixName.cs new file mode 100644 index 0000000000..33664c4d39 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.GetUnixName.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Sys + { + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetUnixName")] + private static extern IntPtr GetUnixNamePrivate(); + + internal static string GetUnixName() + { + IntPtr ptr = GetUnixNamePrivate(); + return Marshal.PtrToStringAnsi(ptr); + } + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.LSeek.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.LSeek.cs new file mode 100644 index 0000000000..7f8df7c6bf --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.LSeek.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + internal enum SeekWhence + { + SEEK_SET = 0, + SEEK_CUR = 1, + SEEK_END = 2 + } + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_LSeek", SetLastError = true)] + internal static extern long LSeek(SafeFileHandle fd, long offset, SeekWhence whence); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.MksTemps.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.MksTemps.cs new file mode 100644 index 0000000000..b8694d9007 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.MksTemps.cs @@ -0,0 +1,17 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Sys + { + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_MksTemps", SetLastError = true)] + internal static extern IntPtr MksTemps( + byte[] template, + int suffixlen); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Open.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Open.cs new file mode 100644 index 0000000000..a9a994c78c --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Open.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Open", SetLastError = true)] + internal static extern SafeFileHandle Open(string filename, OpenFlags flags, int mode); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.OpenFlags.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.OpenFlags.cs new file mode 100644 index 0000000000..f9e54c3cbc --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.OpenFlags.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; + +internal static partial class Interop +{ + internal static partial class Sys + { + [Flags] + internal enum OpenFlags + { + // Access modes (mutually exclusive) + O_RDONLY = 0x0000, + O_WRONLY = 0x0001, + O_RDWR = 0x0002, + + // Flags (combinable) + O_CLOEXEC = 0x0010, + O_CREAT = 0x0020, + O_EXCL = 0x0040, + O_TRUNC = 0x0080, + O_SYNC = 0x0100, + } + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.PathConf.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.PathConf.cs new file mode 100644 index 0000000000..4a1fcf67d0 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.PathConf.cs @@ -0,0 +1,73 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Sys + { + internal static int DEFAULT_PC_NAME_MAX = 255; + + internal enum PathConfName : int + { + PC_LINK_MAX = 1, + PC_MAX_CANON = 2, + PC_MAX_INPUT = 3, + PC_NAME_MAX = 4, + PC_PATH_MAX = 5, + PC_PIPE_BUF = 6, + PC_CHOWN_RESTRICTED = 7, + PC_NO_TRUNC = 8, + PC_VDISABLE = 9, + } + + /// The maximum path length for the system. -1 if it hasn't yet been initialized. + private static int s_maxPath = -1; + + /// The maximum name length for the system. -1 if it hasn't yet been initialized. + private static int s_maxName = -1; + + internal static int MaxPath + { + get + { + // Benign race condition on cached value + if (s_maxPath < 0) + { + // GetMaximumPath returns a long from PathConf + // but our callers expect an int so we need to convert. + long temp = GetMaximumPath(); + if (temp > int.MaxValue) + s_maxPath = int.MaxValue; + else + s_maxPath = Convert.ToInt32(temp); + } + return s_maxPath; + } + } + + internal static int MaxName + { + get + { + // Benign race condition on cached value + if (s_maxName < 0) + { + int result = PathConf("/", PathConfName.PC_NAME_MAX); + s_maxName = result >= 0 ? result : DEFAULT_PC_NAME_MAX; + } + + return s_maxName; + } + } + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_PathConf", SetLastError = true)] + private static extern int PathConf(string path, PathConfName name); + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetMaximumPath")] + private static extern long GetMaximumPath(); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Permissions.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Permissions.cs new file mode 100644 index 0000000000..f1d13787d2 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Permissions.cs @@ -0,0 +1,32 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; + +internal static partial class Interop +{ + internal static partial class Sys + { + [Flags] + internal enum Permissions + { + Mask = S_IRWXU | S_IRWXG | S_IRWXO, + + S_IRWXU = S_IRUSR | S_IWUSR | S_IXUSR, + S_IRUSR = 0x100, + S_IWUSR = 0x80, + S_IXUSR = 0x40, + + S_IRWXG = S_IRGRP | S_IWGRP | S_IXGRP, + S_IRGRP = 0x20, + S_IWGRP = 0x10, + S_IXGRP = 0x8, + + S_IRWXO = S_IROTH | S_IWOTH | S_IXOTH, + S_IROTH = 0x4, + S_IWOTH = 0x2, + S_IXOTH = 0x1, + } + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.PosixFAdvise.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.PosixFAdvise.cs new file mode 100644 index 0000000000..69e39b30d2 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.PosixFAdvise.cs @@ -0,0 +1,36 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + internal enum FileAdvice : int + { + POSIX_FADV_NORMAL = 0, /* no special advice, the default value */ + POSIX_FADV_RANDOM = 1, /* random I/O access */ + POSIX_FADV_SEQUENTIAL = 2, /* sequential I/O access */ + POSIX_FADV_WILLNEED = 3, /* will need specified pages */ + POSIX_FADV_DONTNEED = 4, /* don't need the specified pages */ + POSIX_FADV_NOREUSE = 5, /* data will only be acessed once */ + } + + /// + /// Notifies the OS kernel that the specified file will be accessed in a particular way soon; this allows the kernel to + /// potentially optimize the access pattern of the file. + /// + /// The file descriptor of the file + /// The start of the region to advise about + /// The number of bytes of the region (until the end of the file if 0) + /// The type of advice to give the kernel about the specified region + /// + /// Returns 0 on success; otherwise, the error code is returned + /// + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_PosixFAdvise", SetLastError = false /* this is explicitly called out in the man page */)] + internal static extern int PosixFAdvise(SafeFileHandle fd, long offset, long length, FileAdvice advice); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Read.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Read.cs new file mode 100644 index 0000000000..812ae348dc --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Read.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + /// + /// Reads a number of bytes from an open file descriptor into a specified buffer. + /// + /// The open file descriptor to try to read from + /// The buffer to read info into + /// The size of the buffer + /// + /// Returns the number of bytes read on success; otherwise, -1 is returned + /// Note - on fail. the position of the stream may change depending on the platform; consult man 2 read for more info + /// + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Read", SetLastError = true)] + internal static unsafe extern int Read(SafeFileHandle fd, byte* buffer, int count); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Stat.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Stat.cs new file mode 100644 index 0000000000..a8bc2ec7d1 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Stat.cs @@ -0,0 +1,59 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + // Even though csc will by default use a sequential layout, a CS0649 warning as error + // is produced for un-assigned fields when no StructLayout is specified. + // + // Explicitly saying Sequential disables that warning/error for consumers which only + // use Stat in debug builds. + [StructLayout(LayoutKind.Sequential)] + internal struct FileStatus + { + internal FileStatusFlags Flags; + internal int Mode; + internal uint Uid; + internal uint Gid; + internal long Size; + internal long ATime; + internal long MTime; + internal long CTime; + internal long BirthTime; + } + + internal static class FileTypes + { + internal const int S_IFMT = 0xF000; + internal const int S_IFIFO = 0x1000; + internal const int S_IFCHR = 0x2000; + internal const int S_IFDIR = 0x4000; + internal const int S_IFREG = 0x8000; + internal const int S_IFLNK = 0xA000; + internal const int S_IFSOCK = 0xC000; + } + + [Flags] + internal enum FileStatusFlags + { + None = 0, + HasBirthTime = 1, + } + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FStat", SetLastError = true)] + internal static extern int FStat(SafeFileHandle fd, out FileStatus output); + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Stat", SetLastError = true)] + internal static extern int Stat(string path, out FileStatus output); + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_LStat", SetLastError = true)] + internal static extern int LStat(string path, out FileStatus output); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Unlink.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Unlink.cs new file mode 100644 index 0000000000..829210fa7e --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Unlink.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Sys + { + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Unlink", SetLastError = true)] + internal static extern int Unlink(string pathname); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Write.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Write.cs new file mode 100644 index 0000000000..c14fc26263 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Write.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + /// + /// Writes the specified buffer to the provided open file descriptor + /// + /// The file descriptor to try and write to + /// The data to attempt to write + /// The amount of data to write, in bytes + /// + /// Returns the number of bytes written on success; otherwise, returns -1 and sets errno + /// + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Write", SetLastError = true)] + internal static unsafe extern int Write(SafeFileHandle fd, byte* buffer, int bufferSize); + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Write", SetLastError = true)] + internal static unsafe extern int Write(int fd, byte* buffer, int bufferSize); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/BCrypt/Interop.BCryptGenRandom.cs b/src/mscorlib/corefx/Interop/Windows/BCrypt/Interop.BCryptGenRandom.cs new file mode 100644 index 0000000000..d2ce4131b0 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/BCrypt/Interop.BCryptGenRandom.cs @@ -0,0 +1,26 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class BCrypt + { + internal static unsafe NTSTATUS BCryptGenRandom(byte* pbBuffer, int count) + { + Debug.Assert(pbBuffer != null); + Debug.Assert(count >= 0); + + return BCryptGenRandom(IntPtr.Zero, pbBuffer, count, BCRYPT_USE_SYSTEM_PREFERRED_RNG); + } + + private const int BCRYPT_USE_SYSTEM_PREFERRED_RNG = 0x00000002; + + [DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)] + private static unsafe extern NTSTATUS BCryptGenRandom(IntPtr hAlgorithm, byte* pbBuffer, int cbBuffer, int dwFlags); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/BCrypt/Interop.NTSTATUS.cs b/src/mscorlib/corefx/Interop/Windows/BCrypt/Interop.NTSTATUS.cs new file mode 100644 index 0000000000..49d674f399 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/BCrypt/Interop.NTSTATUS.cs @@ -0,0 +1,19 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; + +internal partial class Interop +{ + internal partial class BCrypt + { + internal enum NTSTATUS : uint + { + STATUS_SUCCESS = 0x0, + STATUS_NOT_FOUND = 0xc0000225, + STATUS_INVALID_PARAMETER = 0xc000000d, + STATUS_NO_MEMORY = 0xc0000017, + } + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/Crypt32/Interop.CryptProtectMemory.cs b/src/mscorlib/corefx/Interop/Windows/Crypt32/Interop.CryptProtectMemory.cs new file mode 100644 index 0000000000..b10cb6a041 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/Crypt32/Interop.CryptProtectMemory.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using System.Security; + +internal partial class Interop +{ + internal partial class Crypt32 + { + internal const uint CRYPTPROTECTMEMORY_BLOCK_SIZE = 16; + internal const uint CRYPTPROTECTMEMORY_SAME_PROCESS = 0; + + [DllImport(Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)] + internal static extern bool CryptProtectMemory(SafeBSTRHandle pData, uint cbData, uint dwFlags); + + [DllImport(Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)] + internal static extern bool CryptUnprotectMemory(SafeBSTRHandle pData, uint cbData, uint dwFlags); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/Interop.BOOL.cs b/src/mscorlib/corefx/Interop/Windows/Interop.BOOL.cs new file mode 100644 index 0000000000..9f4dab8935 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/Interop.BOOL.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +internal partial class Interop +{ + /// + /// Blittable version of Windows BOOL type. It is convenient in situations where + /// manual marshalling is required, or to avoid overhead of regular bool marshalling. + /// + /// + /// Some Windows APIs return arbitrary integer values although the return type is defined + /// as BOOL. It is best to never compare BOOL to TRUE. Always use bResult != BOOL.FALSE + /// or bResult == BOOL.FALSE . + /// + internal enum BOOL : int + { + FALSE = 0, + TRUE = 1, + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/Interop.Libraries.cs b/src/mscorlib/corefx/Interop/Windows/Interop.Libraries.cs new file mode 100644 index 0000000000..1165a2da95 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/Interop.Libraries.cs @@ -0,0 +1,82 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +internal static partial class Interop +{ + internal static class Libraries + { + internal const string Advapi32 = "advapi32.dll"; + internal const string BCrypt = "BCrypt.dll"; + internal const string Combase = "combase.dll"; + internal const string Console_L1 = "api-ms-win-core-console-l1-1-0.dll"; + internal const string Console_L2 = "api-ms-win-core-console-l2-1-0.dll"; + internal const string CoreFile_L1 = "api-ms-win-core-file-l1-1-0.dll"; + internal const string CoreFile_L1_2 = "api-ms-win-core-file-l1-2-0.dll"; + internal const string CoreFile_L2 = "api-ms-win-core-file-l2-1-0.dll"; + internal const string Crypt32 = "crypt32.dll"; + internal const string Debug = "api-ms-win-core-debug-l1-1-0.dll"; + internal const string Error_L1 = "api-ms-win-core-winrt-error-l1-1-0.dll"; + internal const string ErrorHandling = "api-ms-win-core-errorhandling-l1-1-0.dll"; + internal const string Eventing = "api-ms-win-eventing-provider-l1-1-0.dll"; + internal const string Handle = "api-ms-win-core-handle-l1-1-0.dll"; + internal const string Heap = "api-ms-win-core-heap-obsolete-l1-1-0.dll"; + internal const string Heap_L1 = "api-ms-win-core-heap-l1-1-0.dll"; + internal const string IO = "api-ms-win-core-io-l1-1-0.dll"; + internal const string IpHlpApi = "iphlpapi.dll"; + internal const string Kernel32 = "kernel32.dll"; + internal const string Kernel32_L1 = "api-ms-win-core-kernel32-legacy-l1-1-1.dll"; + internal const string Kernel32_L2 = "api-ms-win-core-kernel32-legacy-l1-1-0.dll"; + internal const string Keyboard = "ext-ms-win-ntuser-keyboard-l1-2-1.dll"; + internal const string LibraryLoader = "api-ms-win-core-libraryloader-l1-1-0.dll"; + internal const string Localization = "api-ms-win-core-localization-l1-2-0.dll"; + internal const string Memory_L1_0 = "api-ms-win-core-memory-l1-1-0.dll"; + internal const string Memory_L1_1 = "api-ms-win-core-memory-l1-1-1.dll"; + internal const string Memory_L1_2 = "api-ms-win-core-memory-l1-1-2.dll"; + internal const string Memory_L1_3 = "api-ms-win-core-memory-l1-1-3.dll"; + internal const string NCrypt = "ncrypt.dll"; + internal const string NtDll = "ntdll.dll"; + internal const string OleAut32 = "oleaut32.dll"; + internal const string Pipe = "api-ms-win-core-namedpipe-l1-1-0.dll"; + internal const string Pipe_L2 = "api-ms-win-core-namedpipe-l1-2-1.dll"; + internal const string ProcessEnvironment = "api-ms-win-core-processenvironment-l1-1-0.dll"; + internal const string ProcessThread_L1 = "api-ms-win-core-processthreads-l1-1-0.dll"; + internal const string ProcessThread_L1_1 = "api-ms-win-core-processthreads-l1-1-1.dll"; + internal const string ProcessThread_L1_2 = "api-ms-win-core-processthreads-l1-1-2.dll"; + internal const string ProcessTopology = "api-ms-win-core-processtopology-obsolete-l1-1-0.dll"; + internal const string Profile = "api-ms-win-core-profile-l1-1-0.dll"; + internal const string Psapi = "api-ms-win-core-psapi-l1-1-0.dll"; + internal const string Psapi_Obsolete = "api-ms-win-core-psapi-obsolete-l1-1-0.dll"; + internal const string Registry_L1 = "api-ms-win-core-registry-l1-1-0.dll"; + internal const string Registry_L2 = "api-ms-win-core-registry-l2-1-0.dll"; + internal const string RoBuffer = "api-ms-win-core-winrt-robuffer-l1-1-0.dll"; + internal const string SecurityBase = "api-ms-win-security-base-l1-1-0.dll"; + internal const string SecurityCpwl = "api-ms-win-security-cpwl-l1-1-0.dll"; + internal const string SecurityCryptoApi = "api-ms-win-security-cryptoapi-l1-1-0.dll"; + internal const string SecurityLsa = "api-ms-win-security-lsalookup-l2-1-0.dll"; + internal const string SecurityLsaPolicy = "api-ms-win-security-lsapolicy-l1-1-0.dll"; + internal const string SecurityProvider = "api-ms-win-security-provider-l1-1-0.dll"; + internal const string SecuritySddl = "api-ms-win-security-sddl-l1-1-0.dll"; + internal const string ServiceCore = "api-ms-win-service-core-l1-1-1.dll"; + internal const string ServiceMgmt_L1 = "api-ms-win-service-management-l1-1-0.dll"; + internal const string ServiceMgmt_L2 = "api-ms-win-service-management-l2-1-0.dll"; + internal const string ServiceWinSvc = "api-ms-win-service-winsvc-l1-1-0.dll"; + internal const string Shell = "shell32.dll"; + internal const string ShellFolders = "ext-ms-win-shell32-shellfolders-l1-1-0.dll"; + internal const string Sspi = "sspicli.dll"; + internal const string String_L1 = "api-ms-win-core-string-l1-1-0.dll"; + internal const string Synch = "api-ms-win-core-synch-l1-1-0.dll"; + internal const string SystemInfo_L1_1 = "api-ms-win-core-sysinfo-l1-1-0.dll"; + internal const string SystemInfo_L1_2 = "api-ms-win-core-sysinfo-l1-2-0.dll"; + internal const string SystemInfo_L2_1 = "api-ms-win-core-sysinfo-l2-1-0.dll"; + internal const string ThreadPool = "api-ms-win-core-threadpool-l1-2-0.dll"; + internal const string User32 = "user32.dll"; + internal const string Util = "api-ms-win-core-util-l1-1-0.dll"; + internal const string Version = "api-ms-win-core-version-l1-1-0.dll"; + internal const string WinHttp = "winhttp.dll"; + internal const string Winsock = "Ws2_32.dll"; + internal const string Wow64 = "api-ms-win-core-wow64-l1-1-0.dll"; + internal const string Ws2_32 = "ws2_32.dll"; + internal const string Zlib = "clrcompression.dll"; + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/NtDll/Interop.ZeroMemory.cs b/src/mscorlib/corefx/Interop/Windows/NtDll/Interop.ZeroMemory.cs new file mode 100644 index 0000000000..caa0329bd0 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/NtDll/Interop.ZeroMemory.cs @@ -0,0 +1,19 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using System.Security; + +internal partial class Interop +{ + internal partial class NtDll + { + [DllImport(Libraries.NtDll, CharSet = CharSet.Unicode, EntryPoint = "RtlZeroMemory")] + internal static extern void ZeroMemory(SafeBSTRHandle address, uint length); + + [DllImport(Libraries.NtDll, CharSet = CharSet.Unicode, EntryPoint = "RtlZeroMemory")] + internal static extern void ZeroMemory(IntPtr address, UIntPtr length); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.CancelIoEx.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.CancelIoEx.cs new file mode 100644 index 0000000000..868d409321 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.CancelIoEx.cs @@ -0,0 +1,16 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System.Runtime.InteropServices; +using System.Threading; + +internal partial class Interop +{ + internal partial class mincore + { + [DllImport(Libraries.IO, SetLastError = true)] + internal static unsafe extern bool CancelIoEx(SafeHandle handle, NativeOverlapped* lpOverlapped); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.CloseHandle.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.CloseHandle.cs new file mode 100644 index 0000000000..029937b6d5 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.CloseHandle.cs @@ -0,0 +1,16 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + [DllImport(Libraries.Handle, SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + internal static extern bool CloseHandle(IntPtr handle); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.CreateFile.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.CreateFile.cs new file mode 100644 index 0000000000..670037d52e --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.CreateFile.cs @@ -0,0 +1,40 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.IO; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + /// + /// WARNING: This method does not implicitly handle long paths. Use CreateFile. + /// + [DllImport(Libraries.CoreFile_L1, EntryPoint = "CreateFileW", SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false)] + private static extern SafeFileHandle CreateFilePrivate( + string lpFileName, + int dwDesiredAccess, + System.IO.FileShare dwShareMode, + [In] ref SECURITY_ATTRIBUTES securityAttrs, + System.IO.FileMode dwCreationDisposition, + int dwFlagsAndAttributes, + IntPtr hTemplateFile); + + internal static SafeFileHandle CreateFile( + string lpFileName, + int dwDesiredAccess, + System.IO.FileShare dwShareMode, + [In] ref SECURITY_ATTRIBUTES securityAttrs, + System.IO.FileMode dwCreationDisposition, + int dwFlagsAndAttributes, + IntPtr hTemplateFile) + { + lpFileName = PathInternal.EnsureExtendedPrefixOverMaxPath(lpFileName); + return CreateFilePrivate(lpFileName, dwDesiredAccess, dwShareMode, ref securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); + } + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.Errors.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.Errors.cs new file mode 100644 index 0000000000..05b2250830 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.Errors.cs @@ -0,0 +1,74 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +internal partial class Interop +{ + internal partial class mincore + { + internal partial class Errors + { + internal const int ERROR_SUCCESS = 0x0; + internal const int ERROR_INVALID_FUNCTION = 0x1; + internal const int ERROR_FILE_NOT_FOUND = 0x2; + internal const int ERROR_PATH_NOT_FOUND = 0x3; + internal const int ERROR_ACCESS_DENIED = 0x5; + internal const int ERROR_INVALID_HANDLE = 0x6; + internal const int ERROR_NOT_ENOUGH_MEMORY = 0x8; + internal const int ERROR_INVALID_DATA = 0xD; + internal const int ERROR_INVALID_DRIVE = 0xF; + internal const int ERROR_NO_MORE_FILES = 0x12; + internal const int ERROR_NOT_READY = 0x15; + internal const int ERROR_BAD_LENGTH = 0x18; + internal const int ERROR_SHARING_VIOLATION = 0x20; + internal const int ERROR_LOCK_VIOLATION = 0x21; + internal const int ERROR_HANDLE_EOF = 0x26; + internal const int ERROR_FILE_EXISTS = 0x50; + internal const int ERROR_INVALID_PARAMETER = 0x57; + internal const int ERROR_BROKEN_PIPE = 0x6D; + internal const int ERROR_INSUFFICIENT_BUFFER = 0x7A; + internal const int ERROR_INVALID_NAME = 0x7B; + internal const int ERROR_NEGATIVE_SEEK = 0x83; + internal const int ERROR_DIR_NOT_EMPTY = 0x91; + internal const int ERROR_BAD_PATHNAME = 0xA1; + internal const int ERROR_LOCK_FAILED = 0xA7; + internal const int ERROR_BUSY = 0xAA; + internal const int ERROR_ALREADY_EXISTS = 0xB7; + internal const int ERROR_BAD_EXE_FORMAT = 0xC1; + internal const int ERROR_ENVVAR_NOT_FOUND = 0xCB; + internal const int ERROR_FILENAME_EXCED_RANGE = 0xCE; + internal const int ERROR_EXE_MACHINE_TYPE_MISMATCH = 0xD8; + internal const int ERROR_PIPE_BUSY = 0xE7; + internal const int ERROR_NO_DATA = 0xE8; + internal const int ERROR_PIPE_NOT_CONNECTED = 0xE9; + internal const int ERROR_MORE_DATA = 0xEA; + internal const int ERROR_NO_MORE_ITEMS = 0x103; + internal const int ERROR_PARTIAL_COPY = 0x12B; + internal const int ERROR_ARITHMETIC_OVERFLOW = 0x216; + internal const int ERROR_PIPE_CONNECTED = 0x217; + internal const int ERROR_PIPE_LISTENING = 0x218; + internal const int ERROR_OPERATION_ABORTED = 0x3E3; + internal const int ERROR_IO_PENDING = 0x3E5; + internal const int ERROR_NO_TOKEN = 0x3f0; + internal const int ERROR_DLL_INIT_FAILED = 0x45A; + internal const int ERROR_NOT_FOUND = 0x490; + internal const int ERROR_NON_ACCOUNT_SID = 0x4E9; + internal const int ERROR_NOT_ALL_ASSIGNED = 0x514; + internal const int ERROR_UNKNOWN_REVISION = 0x519; + internal const int ERROR_INVALID_OWNER = 0x51B; + internal const int ERROR_INVALID_PRIMARY_GROUP = 0x51C; + internal const int ERROR_NO_SUCH_PRIVILEGE = 0x521; + internal const int ERROR_PRIVILEGE_NOT_HELD = 0x522; + internal const int ERROR_INVALID_ACL = 0x538; + internal const int ERROR_INVALID_SECURITY_DESCR = 0x53A; + internal const int ERROR_INVALID_SID = 0x539; + internal const int ERROR_BAD_IMPERSONATION_LEVEL = 0x542; + internal const int ERROR_CANT_OPEN_ANONYMOUS = 0x543; + internal const int ERROR_NO_SECURITY_ON_OBJECT = 0x546; + internal const int ERROR_TRUSTED_RELATIONSHIP_FAILURE = 0x6FD; + internal const int ERROR_RESOURCE_LANG_NOT_FOUND = 0x717; + internal const int EFail = unchecked((int)0x80004005); + internal const int E_FILENOTFOUND = unchecked((int)0x80070002); + } + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.FileOperations.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.FileOperations.cs new file mode 100644 index 0000000000..4369760042 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.FileOperations.cs @@ -0,0 +1,35 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +internal partial class Interop +{ + internal partial class mincore + { + internal partial class IOReparseOptions + { + internal const uint IO_REPARSE_TAG_FILE_PLACEHOLDER = 0x80000015; + internal const uint IO_REPARSE_TAG_MOUNT_POINT = 0xA0000003; + } + + internal partial class FileOperations + { + internal const int OPEN_EXISTING = 3; + internal const int COPY_FILE_FAIL_IF_EXISTS = 0x00000001; + + internal const int FILE_ACTION_ADDED = 1; + internal const int FILE_ACTION_REMOVED = 2; + internal const int FILE_ACTION_MODIFIED = 3; + internal const int FILE_ACTION_RENAMED_OLD_NAME = 4; + internal const int FILE_ACTION_RENAMED_NEW_NAME = 5; + + internal const int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000; + internal const int FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000; + internal const int FILE_FLAG_OVERLAPPED = 0x40000000; + + internal const int FILE_LIST_DIRECTORY = 0x0001; + } + + internal const uint SEM_FAILCRITICALERRORS = 1; + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.FileTypes.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.FileTypes.cs new file mode 100644 index 0000000000..a24813e8d6 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.FileTypes.cs @@ -0,0 +1,16 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +internal partial class Interop +{ + internal partial class mincore + { + internal partial class FileTypes + { + internal const int FILE_TYPE_DISK = 0x0001; + internal const int FILE_TYPE_CHAR = 0x0002; + internal const int FILE_TYPE_PIPE = 0x0003; + } + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.FlushFileBuffers.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.FlushFileBuffers.cs new file mode 100644 index 0000000000..69f4fe07ce --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.FlushFileBuffers.cs @@ -0,0 +1,17 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + [DllImport(Libraries.CoreFile_L1, SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + internal static extern bool FlushFileBuffers(SafeHandle hHandle); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.FormatMessage.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.FormatMessage.cs new file mode 100644 index 0000000000..02ecbb8a63 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.FormatMessage.cs @@ -0,0 +1,112 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Text; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + private const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200; + private const int FORMAT_MESSAGE_FROM_HMODULE = 0x00000800; + private const int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000; + private const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000; + + + private const int ERROR_INSUFFICIENT_BUFFER = 0x7A; + + [DllImport(Libraries.Localization, CharSet = CharSet.Unicode, EntryPoint = "FormatMessageW", SetLastError = true, BestFitMapping = true)] + private static extern int FormatMessage( + int dwFlags, + IntPtr lpSource, + uint dwMessageId, + int dwLanguageId, + [Out] StringBuilder lpBuffer, + int nSize, + IntPtr[] arguments); + + /// + /// Returns a string message for the specified Win32 error code. + /// + internal static string GetMessage(int errorCode) + { + return GetMessage(IntPtr.Zero, errorCode); + } + + internal static string GetMessage(IntPtr moduleHandle, int errorCode) + { + var sb = new StringBuilder(InitialBufferSize); + do + { + string errorMsg; + if (TryGetErrorMessage(moduleHandle, errorCode, sb, out errorMsg)) + { + return errorMsg; + } + else + { + // increase the capacity of the StringBuilder. + sb.Capacity *= BufferSizeIncreaseFactor; + } + } + while (sb.Capacity < MaxAllowedBufferSize); + + // If you come here then a size as large as 65K is also not sufficient and so we give the generic errorMsg. + return string.Format("Unknown error (0x{0:x})", errorCode); + } + + private static bool TryGetErrorMessage(IntPtr moduleHandle, int errorCode, StringBuilder sb, out string errorMsg) + { + errorMsg = ""; + + int flags = FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY; + if (moduleHandle != IntPtr.Zero) + { + flags |= FORMAT_MESSAGE_FROM_HMODULE; + } + + int result = FormatMessage(flags, moduleHandle, (uint)errorCode, 0, sb, sb.Capacity, null); + if (result != 0) + { + int i = sb.Length; + while (i > 0) + { + char ch = sb[i - 1]; + if (ch > 32 && ch != '.') break; + i--; + } + errorMsg = sb.ToString(0, i); + } + else if (Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER) + { + return false; + } + else + { + errorMsg = string.Format("Unknown error (0x{0:x})", errorCode); + } + + return true; + } + + // Windows API FormatMessage lets you format a message string given an errorcode. + // Unlike other APIs this API does not support a way to query it for the total message size. + // + // So the API can only be used in one of these two ways. + // a. You pass a buffer of appropriate size and get the resource. + // b. Windows creates a buffer and passes the address back and the onus of releasing the buffer lies on the caller. + // + // Since the error code is coming from the user, it is not possible to know the size in advance. + // Unfortunately we can't use option b. since the buffer can only be freed using LocalFree and it is a private API on onecore. + // Also, using option b is ugly for the managed code and could cause memory leak in situations where freeing is unsuccessful. + // + // As a result we use the following approach. + // We initially call the API with a buffer size of 256 and then gradually increase the size in case of failure until we reach the maximum allowed limit of 65K. + private const int InitialBufferSize = 256; + private const int BufferSizeIncreaseFactor = 4; + private const int MaxAllowedBufferSize = 65 * 1024; + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetFileInformationByHandleEx.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetFileInformationByHandleEx.cs new file mode 100644 index 0000000000..c4739a5ddc --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetFileInformationByHandleEx.cs @@ -0,0 +1,26 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + [DllImport(Libraries.CoreFile_L2, SetLastError = true)] + internal static extern bool GetFileInformationByHandleEx(SafeFileHandle hFile, FILE_INFO_BY_HANDLE_CLASS FileInformationClass, out FILE_STANDARD_INFO lpFileInformation, uint dwBufferSize); + + internal partial struct FILE_STANDARD_INFO + { + internal long AllocationSize; + internal long EndOfFile; + internal uint NumberOfLinks; + internal BOOL DeletePending; + internal BOOL Directory; + } + + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetFileType_SafeHandle.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetFileType_SafeHandle.cs new file mode 100644 index 0000000000..3e2567b6bf --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetFileType_SafeHandle.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + [DllImport(Libraries.CoreFile_L1, SetLastError = true)] + internal extern static int GetFileType(SafeHandle hFile); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetFullPathNameW.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetFullPathNameW.cs new file mode 100644 index 0000000000..a34cc33db3 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetFullPathNameW.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + /// + /// WARNING: This method does not implicitly handle long paths. Use GetFullPathName or PathHelper. + /// + [DllImport(Libraries.CoreFile_L1, SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false, ExactSpelling = true)] + unsafe internal static extern uint GetFullPathNameW(char* path, uint numBufferChars, SafeHandle buffer, IntPtr mustBeZero); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetLongPathNameW.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetLongPathNameW.cs new file mode 100644 index 0000000000..d50db6650b --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetLongPathNameW.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +partial class Interop +{ + partial class mincore + { + /// + /// WARNING: This method does not implicitly handle long paths. Use GetFullPath/PathHelper. + /// + [DllImport(Libraries.CoreFile_L1, SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false, ExactSpelling = true)] + internal static extern uint GetLongPathNameW(SafeHandle lpszShortPath, SafeHandle lpszLongPath, uint cchBuffer); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetTempFileNameW.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetTempFileNameW.cs new file mode 100644 index 0000000000..f06d11be52 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetTempFileNameW.cs @@ -0,0 +1,16 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Text; +using System.Runtime.InteropServices; + +partial class Interop +{ + partial class mincore + { + [DllImport(Libraries.CoreFile_L1, CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = false)] + internal static extern uint GetTempFileNameW(string tmpPath, string prefix, uint uniqueIdOrZero, [Out]StringBuilder tmpFileName); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetTempPathW.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetTempPathW.cs new file mode 100644 index 0000000000..0ccc27c9ec --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.GetTempPathW.cs @@ -0,0 +1,16 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.IO; +using System.Text; +using System.Runtime.InteropServices; + +partial class Interop +{ + partial class mincore + { + [DllImport(Libraries.CoreFile_L1_2, CharSet = CharSet.Unicode, BestFitMapping = false)] + internal static extern uint GetTempPathW(int bufferLen, [Out]StringBuilder buffer); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.Idna.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.Idna.cs new file mode 100644 index 0000000000..e14f16b048 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.Idna.cs @@ -0,0 +1,37 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + // + // Idn APIs + // + + [DllImport("api-ms-win-core-localization-l1-2-0.dll", CharSet = CharSet.Unicode, SetLastError = true)] + internal static extern int IdnToAscii( + uint dwFlags, + IntPtr lpUnicodeCharStr, + int cchUnicodeChar, + [System.Runtime.InteropServices.OutAttribute()] + IntPtr lpASCIICharStr, + int cchASCIIChar); + + [DllImport("api-ms-win-core-localization-l1-2-0.dll", CharSet = CharSet.Unicode, SetLastError = true)] + internal static extern int IdnToUnicode( + uint dwFlags, + IntPtr lpASCIICharStr, + int cchASCIIChar, + [System.Runtime.InteropServices.OutAttribute()] + IntPtr lpUnicodeCharStr, + int cchUnicodeChar); + + internal const int IDN_ALLOW_UNASSIGNED = 0x1; + internal const int IDN_USE_STD3_ASCII_RULES = 0x2; + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.LockFile.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.LockFile.cs new file mode 100644 index 0000000000..ee9a98ecce --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.LockFile.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.IO; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + [DllImport(Libraries.CoreFile_L1, SetLastError = true)] + internal static extern bool LockFile(SafeFileHandle handle, int offsetLow, int offsetHigh, int countLow, int countHigh); + + [DllImport(Libraries.CoreFile_L1, SetLastError = true)] + internal static extern bool UnlockFile(SafeFileHandle handle, int offsetLow, int offsetHigh, int countLow, int countHigh); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.ReadFile_SafeHandle_IntPtr.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.ReadFile_SafeHandle_IntPtr.cs new file mode 100644 index 0000000000..093a993c10 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.ReadFile_SafeHandle_IntPtr.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + [DllImport(Libraries.CoreFile_L1, SetLastError = true)] + unsafe internal static extern int ReadFile( + SafeHandle handle, + byte* bytes, + int numBytesToRead, + out int numBytesRead, + IntPtr mustBeZero); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.ReadFile_SafeHandle_NativeOverlapped.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.ReadFile_SafeHandle_NativeOverlapped.cs new file mode 100644 index 0000000000..ac238cb802 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.ReadFile_SafeHandle_NativeOverlapped.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; +using System.Threading; + +internal partial class Interop +{ + internal partial class mincore + { + [DllImport(Libraries.CoreFile_L1, SetLastError = true)] + unsafe internal static extern int ReadFile( + SafeHandle handle, + byte* bytes, + int numBytesToRead, + IntPtr numBytesRead_mustBeZero, + NativeOverlapped* overlapped); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SECURITY_ATTRIBUTES.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SECURITY_ATTRIBUTES.cs new file mode 100644 index 0000000000..0f5c224022 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SECURITY_ATTRIBUTES.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + [StructLayout(LayoutKind.Sequential)] + internal struct SECURITY_ATTRIBUTES + { + internal uint nLength; + internal IntPtr lpSecurityDescriptor; + internal BOOL bInheritHandle; + } + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SafeCreateFile.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SafeCreateFile.cs new file mode 100644 index 0000000000..edfc66d2c9 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SafeCreateFile.cs @@ -0,0 +1,45 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.IO; +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal partial class Interop +{ + internal partial class mincore + { + internal static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); // WinBase.h + + /// + /// Does not allow access to non-file devices. This disallows DOS devices like "con:", "com1:", + /// "lpt1:", etc. Use this to avoid security problems, like allowing a web client asking a server + /// for "http://server/com1.aspx" and then causing a worker process to hang. + /// + internal static SafeFileHandle SafeCreateFile( + String lpFileName, + int dwDesiredAccess, + System.IO.FileShare dwShareMode, + ref Interop.mincore.SECURITY_ATTRIBUTES securityAttrs, + FileMode dwCreationDisposition, + int dwFlagsAndAttributes, + IntPtr hTemplateFile) + { + SafeFileHandle handle = UnsafeCreateFile(lpFileName, dwDesiredAccess, dwShareMode, ref securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); + + if (!handle.IsInvalid) + { + int fileType = Interop.mincore.GetFileType(handle); + if (fileType != Interop.mincore.FileTypes.FILE_TYPE_DISK) + { + handle.Dispose(); + throw new NotSupportedException(SR.NotSupported_FileStreamOnNonFiles); + } + } + + return handle; + } + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SecurityOptions.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SecurityOptions.cs new file mode 100644 index 0000000000..767d7f528f --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SecurityOptions.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +internal partial class Interop +{ + internal partial class mincore + { + internal partial class SecurityOptions + { + internal const int SECURITY_SQOS_PRESENT = 0x00100000; + internal const int SECURITY_ANONYMOUS = 0 << 16; + internal const int SECURITY_IDENTIFICATION = 1 << 16; + internal const int SECURITY_IMPERSONATION = 2 << 16; + internal const int SECURITY_DELEGATION = 3 << 16; + } + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SetEndOfFile.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SetEndOfFile.cs new file mode 100644 index 0000000000..ee0d3b4bc8 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SetEndOfFile.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + [DllImport(Libraries.CoreFile_L1, SetLastError = true)] + internal static extern bool SetEndOfFile(SafeFileHandle hFile); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SetErrorMode.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SetErrorMode.cs new file mode 100644 index 0000000000..a845990ded --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SetErrorMode.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + [DllImport(Libraries.ErrorHandling, SetLastError = false, EntryPoint = "SetErrorMode", ExactSpelling = true)] + internal static extern uint SetErrorMode(uint newMode); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SetFileInformationByHandle.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SetFileInformationByHandle.cs new file mode 100644 index 0000000000..0519219132 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SetFileInformationByHandle.cs @@ -0,0 +1,72 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + [DllImport(Libraries.CoreFile_L1, SetLastError = true)] + internal static extern bool SetFileInformationByHandle(SafeFileHandle hFile, FILE_INFO_BY_HANDLE_CLASS FileInformationClass, ref FILE_BASIC_INFO lpFileInformation, uint dwBufferSize); + + // Default values indicate "no change". Use defaults so that we don't force callsites to be aware of the default values + internal unsafe static bool SetFileTime( + SafeFileHandle hFile, + long creationTime = -1, + long lastAccessTime = -1, + long lastWriteTime = -1, + long changeTime = -1, + uint fileAttributes = 0) + { + FILE_BASIC_INFO basicInfo = new FILE_BASIC_INFO() + { + CreationTime = creationTime, + LastAccessTime = lastAccessTime, + LastWriteTime = lastWriteTime, + ChangeTime = changeTime, + FileAttributes = fileAttributes + }; + + return SetFileInformationByHandle(hFile, FILE_INFO_BY_HANDLE_CLASS.FileBasicInfo, ref basicInfo, (uint)Marshal.SizeOf()); + } + + internal struct FILE_BASIC_INFO + { + internal long CreationTime; + internal long LastAccessTime; + internal long LastWriteTime; + internal long ChangeTime; + internal uint FileAttributes; + } + + internal enum FILE_INFO_BY_HANDLE_CLASS : uint + { + FileBasicInfo = 0x0u, + FileStandardInfo = 0x1u, + FileNameInfo = 0x2u, + FileRenameInfo = 0x3u, + FileDispositionInfo = 0x4u, + FileAllocationInfo = 0x5u, + FileEndOfFileInfo = 0x6u, + FileStreamInfo = 0x7u, + FileCompressionInfo = 0x8u, + FileAttributeTagInfo = 0x9u, + FileIdBothDirectoryInfo = 0xAu, + FileIdBothDirectoryRestartInfo = 0xBu, + FileIoPriorityHintInfo = 0xCu, + FileRemoteProtocolInfo = 0xDu, + FileFullDirectoryInfo = 0xEu, + FileFullDirectoryRestartInfo = 0xFu, + FileStorageInfo = 0x10u, + FileAlignmentInfo = 0x11u, + FileIdInfo = 0x12u, + FileIdExtdDirectoryInfo = 0x13u, + FileIdExtdDirectoryRestartInfo = 0x14u, + MaximumFileInfoByHandleClass = 0x15u, + } + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SetFilePointerEx.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SetFilePointerEx.cs new file mode 100644 index 0000000000..09f8e1feb3 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.SetFilePointerEx.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + [DllImport(Libraries.CoreFile_L1, SetLastError = true)] + internal static extern bool SetFilePointerEx(SafeFileHandle hFile, long liDistanceToMove, out long lpNewFilePointer, uint dwMoveMethod); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.ThreadPoolIO.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.ThreadPoolIO.cs new file mode 100644 index 0000000000..a0afed5d1a --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.ThreadPoolIO.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class mincore + { + [DllImport(Libraries.ThreadPool, SetLastError = true)] + internal static unsafe extern SafeThreadPoolIOHandle CreateThreadpoolIo(SafeHandle fl, [MarshalAs(UnmanagedType.FunctionPtr)] NativeIoCompletionCallback pfnio, IntPtr context, IntPtr pcbe); + + [DllImport(Libraries.ThreadPool)] + internal static unsafe extern void CloseThreadpoolIo(IntPtr pio); + + [DllImport(Libraries.ThreadPool)] + internal static unsafe extern void StartThreadpoolIo(SafeThreadPoolIOHandle pio); + + [DllImport(Libraries.ThreadPool)] + internal static unsafe extern void CancelThreadpoolIo(SafeThreadPoolIOHandle pio); + } + + internal delegate void NativeIoCompletionCallback(IntPtr instance, IntPtr context, IntPtr overlapped, uint ioResult, UIntPtr numberOfBytesTransferred, IntPtr io); +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.UnsafeCreateFile.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.UnsafeCreateFile.cs new file mode 100644 index 0000000000..e7e4f0513a --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.UnsafeCreateFile.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.IO; +using Microsoft.Win32.SafeHandles; + +internal partial class Interop +{ + internal partial class mincore + { + internal static SafeFileHandle UnsafeCreateFile( + string lpFileName, + int dwDesiredAccess, + FileShare dwShareMode, + ref Interop.mincore.SECURITY_ATTRIBUTES securityAttrs, + FileMode dwCreationDisposition, + int dwFlagsAndAttributes, + IntPtr hTemplateFile) + { + return CreateFile(lpFileName, dwDesiredAccess, dwShareMode, ref securityAttrs, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); + } + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.WideCharToMultiByte.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.WideCharToMultiByte.cs new file mode 100644 index 0000000000..b1a2975696 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.WideCharToMultiByte.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + [DllImport(Libraries.String_L1)] + internal static extern unsafe int WideCharToMultiByte( + uint CodePage, uint dwFlags, + char* lpWideCharStr, int cchWideChar, + byte* lpMultiByteStr, int cbMultiByte, + IntPtr lpDefaultChar, IntPtr lpUsedDefaultChar); + + internal const uint CP_ACP = 0; + internal const uint WC_NO_BEST_FIT_CHARS = 0x00000400; + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.WriteFile_SafeHandle_IntPtr.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.WriteFile_SafeHandle_IntPtr.cs new file mode 100644 index 0000000000..052ba3ce7d --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.WriteFile_SafeHandle_IntPtr.cs @@ -0,0 +1,24 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class mincore + { + // Note there are two different WriteFile prototypes - this is to use + // the type system to force you to not trip across a "feature" in + // Win32's async IO support. You can't do the following three things + // simultaneously: overlapped IO, free the memory for the overlapped + // struct in a callback (or an EndWrite method called by that callback), + // and pass in an address for the numBytesRead parameter. + + [DllImport(Libraries.CoreFile_L1, SetLastError = true)] + internal static unsafe extern int WriteFile(SafeHandle handle, byte* bytes, int numBytesToWrite, out int numBytesWritten, IntPtr mustBeZero); + + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/mincore/Interop.WriteFile_SafeHandle_NativeOverlapped.cs b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.WriteFile_SafeHandle_NativeOverlapped.cs new file mode 100644 index 0000000000..e9d2953045 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/mincore/Interop.WriteFile_SafeHandle_NativeOverlapped.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; +using System.Threading; +internal partial class Interop +{ + internal partial class mincore + { + // Note there are two different WriteFile prototypes - this is to use + // the type system to force you to not trip across a "feature" in + // Win32's async IO support. You can't do the following three things + // simultaneously: overlapped IO, free the memory for the overlapped + // struct in a callback (or an EndWrite method called by that callback), + // and pass in an address for the numBytesRead parameter. + [DllImport(Libraries.CoreFile_L1, SetLastError = true)] + internal static unsafe extern int WriteFile(SafeHandle handle, byte* bytes, int numBytesToWrite, IntPtr numBytesWritten_mustBeZero, NativeOverlapped* lpOverlapped); + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/oleaut32/Interop.SysAllocStringLen.cs b/src/mscorlib/corefx/Interop/Windows/oleaut32/Interop.SysAllocStringLen.cs new file mode 100644 index 0000000000..65da4eaaea --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/oleaut32/Interop.SysAllocStringLen.cs @@ -0,0 +1,16 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using System.Security; + +internal partial class Interop +{ + internal partial class OleAut32 + { + [DllImport(Libraries.OleAut32, CharSet = CharSet.Unicode)] + internal static extern SafeBSTRHandle SysAllocStringLen(IntPtr src, uint len); // BSTR + } +} diff --git a/src/mscorlib/corefx/Interop/Windows/oleaut32/Interop.SysStringLen.cs b/src/mscorlib/corefx/Interop/Windows/oleaut32/Interop.SysStringLen.cs new file mode 100644 index 0000000000..027d8eece9 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Windows/oleaut32/Interop.SysStringLen.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using System.Security; + +internal partial class Interop +{ + internal partial class OleAut32 + { + [DllImport(Libraries.OleAut32)] + internal static extern uint SysStringLen(SafeBSTRHandle bstr); + + [DllImport(Libraries.OleAut32)] + internal static extern uint SysStringLen(IntPtr bstr); + + [DllImport(Libraries.OleAut32)] + internal static extern void SysFreeString(IntPtr bstr); + } +} -- cgit v1.2.3