Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 4ae994a

Browse files
committed
Move Console usage of stat wrapper to new IO shim
1 parent dfe7b80 commit 4ae994a

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

src/Common/src/Interop/Unix/Interop.Libraries.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private static partial class Libraries
99
internal const string LibCoreClr= "libcoreclr"; // CoreCLR runtime
1010
internal const string LibCrypto = "libcrypto"; // OpenSSL crypto library
1111
internal const string Zlib = "libz"; // zlib compression library
12+
internal const string IOInterop = "System.IO.Native";
1213
internal const string CryptoInterop = "System.Security.Cryptography.Native";
1314
}
1415
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
internal static partial class NativeIO
10+
{
11+
internal struct FileStats
12+
{
13+
private FileStatsFlags Flags;
14+
internal int Mode;
15+
internal int Uid;
16+
internal int Gid;
17+
internal int Size;
18+
internal int AccessTime;
19+
internal int ModificationTime;
20+
internal int StatusChangeTime;
21+
internal int CreationTime;
22+
}
23+
24+
internal static class FileTypes
25+
{
26+
internal const int S_IFMT = 0xF000;
27+
internal const int S_IFIFO = 0x1000;
28+
internal const int S_IFCHR = 0x2000;
29+
internal const int S_IFDIR = 0x4000;
30+
internal const int S_IFREG = 0x8000;
31+
internal const int S_IFLNK = 0xA000;
32+
}
33+
34+
[Flags]
35+
internal enum FileStatsFlags
36+
{
37+
None = 0,
38+
HasCreationTime = 1,
39+
}
40+
41+
[DllImport(Libraries.IOInterop, SetLastError = true)]
42+
internal static extern int GetFileStatsFromDescriptor(int fileDescriptor, out FileStats output);
43+
44+
[DllImport(Libraries.IOInterop, SetLastError = true)]
45+
internal static extern int GetFileStatsFromPath(string path, out FileStats output);
46+
}
47+
}

src/System.Console/src/System.Console.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@
138138
<Compile Include="$(CommonPath)\Interop\Unix\libcoreclr\Interop.GetFileInformation.cs">
139139
<Link>Common\Interop\Unix\Interop.GetFileInformation.cs"</Link>
140140
</Compile>
141+
<Compile Include="$(CommonPath)\Interop\Unix\System.IO.Native\Interop.NativeIO.cs">
142+
<Link>Common\Interop\Unix\Interop.NativeIO.cs"</Link>
143+
</Compile>
141144
<Compile Include="$(CommonPath)\Interop\Unix\libcoreclr\Interop.SetConsoleCtrlHandler.cs">
142145
<Link>Common\Interop\Unix\Interop.SetConsoleCtrlHandler.cs"</Link>
143146
</Compile>

src/System.Console/src/System/ConsolePal.Unix.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private static bool ConsoleOutIsTerminal
9191
UnixConsoleStream ucs = sw.BaseStream as UnixConsoleStream;
9292
if (ucs != null)
9393
{
94-
return ucs._handleType == Interop.libcoreclr.FileTypes.S_IFCHR;
94+
return ucs._handleType == Interop.NativeIO.FileTypes.S_IFCHR;
9595
}
9696
}
9797
}
@@ -378,11 +378,11 @@ internal UnixConsoleStream(string devPath, FileAccess access)
378378
try
379379
{
380380
_handle.DangerousAddRef(ref gotFd);
381-
Interop.libcoreclr.fileinfo buf;
381+
Interop.NativeIO.FileStats buf;
382382
_handleType =
383-
Interop.libcoreclr.GetFileInformationFromFd((int)_handle.DangerousGetHandle(), out buf) == 0 ?
384-
(buf.mode & Interop.libcoreclr.FileTypes.S_IFMT) :
385-
Interop.libcoreclr.FileTypes.S_IFREG; // if something goes wrong, don't fail, just say it's a regular file
383+
Interop.NativeIO.GetFileStatsFromDescriptor((int)_handle.DangerousGetHandle(), out buf) == 0 ?
384+
(buf.Mode & Interop.NativeIO.FileTypes.S_IFMT) :
385+
Interop.NativeIO.FileTypes.S_IFREG; // if something goes wrong, don't fail, just say it's a regular file
386386
}
387387
finally
388388
{

0 commit comments

Comments
 (0)