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

Commit 2de1073

Browse files
committed
Use Stat from System.Native shim throughout
Replace all usage of GetFileInformation from libcoreclr with newer Stat from System.Native shim.
1 parent 51f757c commit 2de1073

File tree

12 files changed

+58
-112
lines changed

12 files changed

+58
-112
lines changed

src/Common/src/Interop/Unix/System.Native/Interop.Stat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal static class FileTypes
4141
internal enum FileStatusFlags
4242
{
4343
None = 0,
44-
HasCreationTime = 1,
44+
HasBirthTime = 1,
4545
}
4646

4747
[DllImport(Libraries.SystemNative, SetLastError = true)]

src/Common/src/Interop/Unix/libcoreclr/Interop.GetFileInformation.cs

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/Common/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ internal static SafeFileHandle Open(string path, Interop.libc.OpenFlags flags, i
5151

5252
// Make sure it's not a directory; we do this after opening it once we have a file descriptor
5353
// to avoid race conditions.
54-
Interop.libcoreclr.fileinfo buf;
55-
if (Interop.libcoreclr.GetFileInformationFromFd(fd, out buf) != 0)
54+
Interop.Sys.FileStatus status;
55+
if (Interop.Sys.FStat(fd, out status) != 0)
5656
{
5757
handle.Dispose();
5858
throw Interop.GetExceptionForIoErrno(Interop.Sys.GetLastErrorInfo(), path);
5959
}
60-
if ((buf.mode & Interop.libcoreclr.FileTypes.S_IFMT) == Interop.libcoreclr.FileTypes.S_IFDIR)
60+
if ((status.Mode & Interop.Sys.FileTypes.S_IFMT) == Interop.Sys.FileTypes.S_IFDIR)
6161
{
6262
handle.Dispose();
6363
throw Interop.GetExceptionForIoErrno(Interop.Error.EACCES.Info(), path, isDirectory: true);

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,6 @@
132132
<Compile Include="$(CommonPath)\Interop\Unix\libc\Interop.snprintf.cs">
133133
<Link>Common\Interop\Unix\Interop.snprintf.cs</Link>
134134
</Compile>
135-
<Compile Include="$(CommonPath)\Interop\Unix\libcoreclr\Interop.GetFileInformation.cs">
136-
<Link>Common\Interop\Unix\Interop.GetFileInformation.cs"</Link>
137-
</Compile>
138135
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.Stat.cs">
139136
<Link>Common\Interop\Unix\Interop.Stat.cs</Link>
140137
</Compile>

src/System.Diagnostics.Debug/src/System.Diagnostics.Debug.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@
7474
<Compile Include="$(CommonPath)\Interop\Unix\libc\Interop.write.cs">
7575
<Link>Common\Interop\Unix\libc\Interop.write.cs</Link>
7676
</Compile>
77-
<Compile Include="$(CommonPath)\Interop\Unix\libcoreclr\Interop.GetFileInformation.cs">
78-
<Link>Common\Interop\Unix\Interop.GetFileInformation.cs"</Link>
77+
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.Stat.cs">
78+
<Link>Common\Interop\Unix\Interop.Stat.cs</Link>
7979
</Compile>
8080
<Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeFileHandle.Unix.cs">
8181
<Link>Common\Microsoft\Win32\SafeHandles\SafeFileHandle.Unix.cs</Link>

src/System.IO.FileSystem/src/System.IO.FileSystem.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@
334334
<Compile Include="$(CommonPath)\Interop\Unix\libc\Interop.unlink.cs">
335335
<Link>Common\Interop\Unix\Interop.unlink.cs</Link>
336336
</Compile>
337-
<Compile Include="$(CommonPath)\Interop\Unix\libcoreclr\Interop.GetFileInformation.cs">
338-
<Link>Common\Interop\Unix\Interop.GetInformation.cs</Link>
337+
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.Stat.cs">
338+
<Link>Common\Interop\Unix\Interop.Stat.cs</Link>
339339
</Compile>
340340
<Compile Include="$(CommonPath)\System\IO\PathInternal.Unix.cs">
341341
<Link>Common\System\IO\PathInternal.Unix.cs</Link>

src/System.IO.FileSystem/src/System/IO/FileSystemInfo.Unix.cs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ namespace System.IO
88
partial class FileSystemInfo : IFileSystemObject
99
{
1010
/// <summary>The last cached stat information about the file.</summary>
11-
private Interop.libcoreclr.fileinfo _fileinfo;
11+
private Interop.Sys.FileStatus _fileStatus;
1212

1313
/// <summary>
1414
/// Whether we've successfully cached a stat structure.
15-
/// -1 if we need to refresh _fileinfo, 0 if we've successfully cached one,
15+
/// -1 if we need to refresh _fileStatus, 0 if we've successfully cached one,
1616
/// or any other value that serves as an errno error code from the
17-
/// last time we tried and failed to refresh _fileinfo.
17+
/// last time we tried and failed to refresh _fileStatus.
1818
/// </summary>
19-
private int _fileinfoInitialized = -1;
19+
private int _fileStatusInitialized = -1;
2020

2121
internal IFileSystemObject FileSystemObject
2222
{
@@ -25,7 +25,7 @@ internal IFileSystemObject FileSystemObject
2525

2626
internal void Invalidate()
2727
{
28-
_fileinfoInitialized = -1;
28+
_fileStatusInitialized = -1;
2929
}
3030

3131
FileAttributes IFileSystemObject.Attributes
@@ -77,7 +77,7 @@ FileAttributes IFileSystemObject.Attributes
7777
// just changing its permissions accordingly.
7878
EnsureStatInitialized();
7979
IsReadOnlyAssumesInitialized = (value & FileAttributes.ReadOnly) != 0;
80-
_fileinfoInitialized = -1;
80+
_fileStatusInitialized = -1;
8181
}
8282
}
8383

@@ -86,15 +86,15 @@ private bool IsDirectoryAssumesInitialized
8686
{
8787
get
8888
{
89-
return (_fileinfo.mode & Interop.libcoreclr.FileTypes.S_IFMT) == Interop.libcoreclr.FileTypes.S_IFDIR;
89+
return (_fileStatus.Mode & Interop.Sys.FileTypes.S_IFMT) == Interop.Sys.FileTypes.S_IFDIR;
9090
}
9191
}
9292

9393
private bool IsLink
9494
{
9595
get
9696
{
97-
return (_fileinfo.mode & Interop.libcoreclr.FileTypes.S_IFMT) == Interop.libcoreclr.FileTypes.S_IFLNK;
97+
return (_fileStatus.Mode & Interop.Sys.FileTypes.S_IFMT) == Interop.Sys.FileTypes.S_IFLNK;
9898
}
9999
}
100100

@@ -107,12 +107,12 @@ private bool IsReadOnlyAssumesInitialized
107107
get
108108
{
109109
Interop.libc.Permissions readBit, writeBit;
110-
if (_fileinfo.uid == Interop.libc.geteuid()) // does the user effectively own the file?
110+
if (_fileStatus.Uid == Interop.libc.geteuid()) // does the user effectively own the file?
111111
{
112112
readBit = Interop.libc.Permissions.S_IRUSR;
113113
writeBit = Interop.libc.Permissions.S_IWUSR;
114114
}
115-
else if (_fileinfo.gid == Interop.libc.getegid()) // does the user belong to a group that effectively owns the file?
115+
else if (_fileStatus.Gid == Interop.libc.getegid()) // does the user belong to a group that effectively owns the file?
116116
{
117117
readBit = Interop.libc.Permissions.S_IRGRP;
118118
writeBit = Interop.libc.Permissions.S_IWGRP;
@@ -124,12 +124,12 @@ private bool IsReadOnlyAssumesInitialized
124124
}
125125

126126
return
127-
(_fileinfo.mode & (int)readBit) != 0 && // has read permission
128-
(_fileinfo.mode & (int)writeBit) == 0; // but not write permission
127+
(_fileStatus.Mode & (int)readBit) != 0 && // has read permission
128+
(_fileStatus.Mode & (int)writeBit) == 0; // but not write permission
129129
}
130130
set
131131
{
132-
int newMode = _fileinfo.mode;
132+
int newMode = _fileStatus.Mode;
133133
if (value) // true if going from writable to readable, false if going from readable to writable
134134
{
135135
// Take away all write permissions from user/group/everyone
@@ -142,7 +142,7 @@ private bool IsReadOnlyAssumesInitialized
142142
}
143143

144144
// Change the permissions on the file
145-
if (newMode != _fileinfo.mode)
145+
if (newMode != _fileStatus.Mode)
146146
{
147147
bool isDirectory = this is DirectoryInfo;
148148
while (Interop.CheckIo(Interop.libc.chmod(FullPath, newMode), FullPath, isDirectory)) ;
@@ -154,12 +154,12 @@ bool IFileSystemObject.Exists
154154
{
155155
get
156156
{
157-
if (_fileinfoInitialized == -1)
157+
if (_fileStatusInitialized == -1)
158158
{
159159
Refresh();
160160
}
161161
return
162-
_fileinfoInitialized == 0 && // avoid throwing if Refresh failed; instead just return false
162+
_fileStatusInitialized == 0 && // avoid throwing if Refresh failed; instead just return false
163163
(this is DirectoryInfo) == IsDirectoryAssumesInitialized;
164164
}
165165
}
@@ -169,8 +169,8 @@ DateTimeOffset IFileSystemObject.CreationTime
169169
get
170170
{
171171
EnsureStatInitialized();
172-
return (_fileinfo.flags & (uint)Interop.libcoreclr.FileInformationFlags.HasBTime) != 0 ?
173-
DateTimeOffset.FromUnixTimeSeconds(_fileinfo.btime) :
172+
return (_fileStatus.Flags & Interop.Sys.FileStatusFlags.HasBirthTime) != 0 ?
173+
DateTimeOffset.FromUnixTimeSeconds(_fileStatus.BirthTime) :
174174
default(DateTimeOffset);
175175
}
176176
set
@@ -188,7 +188,7 @@ DateTimeOffset IFileSystemObject.LastAccessTime
188188
get
189189
{
190190
EnsureStatInitialized();
191-
return DateTimeOffset.FromUnixTimeSeconds(_fileinfo.atime).ToLocalTime();
191+
return DateTimeOffset.FromUnixTimeSeconds(_fileStatus.ATime).ToLocalTime();
192192
}
193193
set { SetAccessWriteTimes((IntPtr)value.ToUnixTimeSeconds(), null); }
194194
}
@@ -198,29 +198,29 @@ DateTimeOffset IFileSystemObject.LastWriteTime
198198
get
199199
{
200200
EnsureStatInitialized();
201-
return DateTimeOffset.FromUnixTimeSeconds(_fileinfo.mtime).ToLocalTime();
201+
return DateTimeOffset.FromUnixTimeSeconds(_fileStatus.MTime).ToLocalTime();
202202
}
203203
set { SetAccessWriteTimes(null, (IntPtr)value.ToUnixTimeSeconds()); }
204204
}
205205

206206
private void SetAccessWriteTimes(IntPtr? accessTime, IntPtr? writeTime)
207207
{
208-
_fileinfoInitialized = -1; // force a refresh so that we have an up-to-date times for values not being overwritten
208+
_fileStatusInitialized = -1; // force a refresh so that we have an up-to-date times for values not being overwritten
209209
EnsureStatInitialized();
210210
Interop.libc.utimbuf buf;
211-
buf.actime = accessTime ?? new IntPtr(_fileinfo.atime);
212-
buf.modtime = writeTime ?? new IntPtr(_fileinfo.mtime);
211+
buf.actime = accessTime ?? new IntPtr(_fileStatus.ATime);
212+
buf.modtime = writeTime ?? new IntPtr(_fileStatus.MTime);
213213
bool isDirectory = this is DirectoryInfo;
214214
while (Interop.CheckIo(Interop.libc.utime(FullPath, ref buf), FullPath, isDirectory)) ;
215-
_fileinfoInitialized = -1;
215+
_fileStatusInitialized = -1;
216216
}
217217

218218
long IFileSystemObject.Length
219219
{
220220
get
221221
{
222222
EnsureStatInitialized();
223-
return _fileinfo.size;
223+
return _fileStatus.Size;
224224
}
225225
}
226226

@@ -231,10 +231,10 @@ void IFileSystemObject.Refresh()
231231
int result;
232232
while (true)
233233
{
234-
result = Interop.libcoreclr.GetFileInformationFromPath(FullPath, out _fileinfo);
234+
result = Interop.Sys.Stat(FullPath, out _fileStatus);
235235
if (result >= 0)
236236
{
237-
_fileinfoInitialized = 0;
237+
_fileStatusInitialized = 0;
238238
}
239239
else
240240
{
@@ -243,23 +243,23 @@ void IFileSystemObject.Refresh()
243243
{
244244
continue;
245245
}
246-
_fileinfoInitialized = errorInfo.RawErrno;
246+
_fileStatusInitialized = errorInfo.RawErrno;
247247
}
248248
break;
249249
}
250250
}
251251

252252
private void EnsureStatInitialized()
253253
{
254-
if (_fileinfoInitialized == -1)
254+
if (_fileStatusInitialized == -1)
255255
{
256256
Refresh();
257257
}
258258

259-
if (_fileinfoInitialized != 0)
259+
if (_fileStatusInitialized != 0)
260260
{
261-
int errno = _fileinfoInitialized;
262-
_fileinfoInitialized = -1;
261+
int errno = _fileStatusInitialized;
262+
_fileStatusInitialized = -1;
263263
var errorInfo = new Interop.ErrorInfo(errno);
264264

265265
// Windows distinguishes between whether the directory or the file isn't found,

src/System.IO.FileSystem/src/System/IO/UnixFileStream.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ public override long Length
281281
// Get the length of the file as reported by the OS
282282
long length = SysCall<int, int>((fd, _, __) =>
283283
{
284-
Interop.libcoreclr.fileinfo fileinfo;
285-
int result = Interop.libcoreclr.GetFileInformationFromFd(fd, out fileinfo);
286-
return result >= 0 ? fileinfo.size : result;
284+
Interop.Sys.FileStatus status;
285+
int result = Interop.Sys.FStat(fd, out status);
286+
return result >= 0 ? status.Size : result;
287287
});
288288

289289
// But we may have buffered some data to be written that puts our length

src/System.IO.FileSystem/src/System/IO/UnixFileSystem.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public override void CopyFile(string sourceFullPath, string destFullPath, bool o
4343
}
4444

4545
// Now copy over relevant read/write/execute permissions from the source to the destination
46-
Interop.libcoreclr.fileinfo fileinfo;
47-
while (Interop.CheckIo(Interop.libcoreclr.GetFileInformationFromPath(sourceFullPath, out fileinfo), sourceFullPath)) ;
48-
int newMode = fileinfo.mode & (int)Interop.libc.Permissions.Mask;
46+
Interop.Sys.FileStatus status;
47+
while (Interop.CheckIo(Interop.Sys.Stat(sourceFullPath, out status), sourceFullPath)) ;
48+
int newMode = status.Mode & (int)Interop.libc.Permissions.Mask;
4949
while (Interop.CheckIo(Interop.libc.chmod(destFullPath, newMode), destFullPath)) ;
5050
}
5151

@@ -315,22 +315,22 @@ public override bool DirectoryExists(string fullPath)
315315

316316
private static bool DirectoryExists(string fullPath, out Interop.ErrorInfo errorInfo)
317317
{
318-
return FileExists(fullPath, Interop.libcoreclr.FileTypes.S_IFDIR, out errorInfo);
318+
return FileExists(fullPath, Interop.Sys.FileTypes.S_IFDIR, out errorInfo);
319319
}
320320

321321
public override bool FileExists(string fullPath)
322322
{
323323
Interop.ErrorInfo ignored;
324-
return FileExists(fullPath, Interop.libcoreclr.FileTypes.S_IFREG, out ignored);
324+
return FileExists(fullPath, Interop.Sys.FileTypes.S_IFREG, out ignored);
325325
}
326326

327327
private static bool FileExists(string fullPath, int fileType, out Interop.ErrorInfo errorInfo)
328328
{
329-
Interop.libcoreclr.fileinfo fileinfo;
329+
Interop.Sys.FileStatus fileinfo;
330330
while (true)
331331
{
332332
errorInfo = default(Interop.ErrorInfo);
333-
int result = Interop.libcoreclr.GetFileInformationFromPath(fullPath, out fileinfo);
333+
int result = Interop.Sys.Stat(fullPath, out fileinfo);
334334
if (result < 0)
335335
{
336336
errorInfo = Interop.Sys.GetLastErrorInfo();
@@ -340,7 +340,7 @@ private static bool FileExists(string fullPath, int fileType, out Interop.ErrorI
340340
}
341341
return false;
342342
}
343-
return (fileinfo.mode & Interop.libcoreclr.FileTypes.S_IFMT) == fileType;
343+
return (fileinfo.Mode & Interop.Sys.FileTypes.S_IFMT) == fileType;
344344
}
345345
}
346346

src/System.IO.Pipes/src/System.IO.Pipes.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@
196196
<Compile Include="$(CommonPath)\Interop\Unix\libc\Interop.write.cs">
197197
<Link>Common\Interop\Unix\Interop.write.cs</Link>
198198
</Compile>
199-
<Compile Include="$(CommonPath)\Interop\Unix\libcoreclr\Interop.GetFileInformation.cs">
200-
<Link>Common\Interop\Unix\Interop.GetInformation.cs</Link>
199+
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.Stat.cs">
200+
<Link>Common\Interop\Unix\Interop.Stat.cs</Link>
201201
</Compile>
202202
</ItemGroup>
203203
<!-- Linux -->

0 commit comments

Comments
 (0)