Skip to content

Commit 96d9a47

Browse files
authored
Annotate System.IO.FileSystem for nullable (dotnet#590)
Annotate System.IO.FileSystem for nullable
1 parent f08b853 commit 96d9a47

26 files changed

Lines changed: 126 additions & 113 deletions

src/libraries/Common/src/Interop/Windows/Kernel32/Interop.ReplaceFile.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
56
using System;
67
using System.IO;
78
using System.Runtime.InteropServices;
@@ -12,11 +13,11 @@ internal partial class Kernel32
1213
{
1314
[DllImport(Libraries.Kernel32, EntryPoint = "ReplaceFileW", SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false)]
1415
private static extern bool ReplaceFilePrivate(
15-
string replacedFileName, string replacementFileName, string backupFileName,
16+
string replacedFileName, string replacementFileName, string? backupFileName,
1617
int dwReplaceFlags, IntPtr lpExclude, IntPtr lpReserved);
1718

1819
internal static bool ReplaceFile(
19-
string replacedFileName, string replacementFileName, string backupFileName,
20+
string replacedFileName, string replacementFileName, string? backupFileName,
2021
int dwReplaceFlags, IntPtr lpExclude, IntPtr lpReserved)
2122
{
2223
replacedFileName = PathInternal.EnsureExtendedPrefixIfNeeded(replacedFileName);

src/libraries/Common/src/System/IO/FileSystem.Attributes.Windows.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
56
using Microsoft.Win32.SafeHandles;
67
using System;
78
using System.Collections.Generic;
@@ -18,12 +19,12 @@ namespace System.IO
1819
{
1920
internal static partial class FileSystem
2021
{
21-
public static bool DirectoryExists(string fullPath)
22+
public static bool DirectoryExists(string? fullPath)
2223
{
2324
return DirectoryExists(fullPath, out int lastError);
2425
}
2526

26-
private static bool DirectoryExists(string path, out int lastError)
27+
private static bool DirectoryExists(string? path, out int lastError)
2728
{
2829
Interop.Kernel32.WIN32_FILE_ATTRIBUTE_DATA data = default;
2930
lastError = FillAttributeInfo(path, ref data, returnErrorOnNotFound: true);
@@ -52,7 +53,7 @@ public static bool FileExists(string fullPath)
5253
/// <param name="path">The file path from which the file attribute information will be filled.</param>
5354
/// <param name="data">A struct that will contain the attribute information.</param>
5455
/// <param name="returnErrorOnNotFound">Return the error code for not found errors?</param>
55-
internal static int FillAttributeInfo(string path, ref Interop.Kernel32.WIN32_FILE_ATTRIBUTE_DATA data, bool returnErrorOnNotFound)
56+
internal static int FillAttributeInfo(string? path, ref Interop.Kernel32.WIN32_FILE_ATTRIBUTE_DATA data, bool returnErrorOnNotFound)
5657
{
5758
int errorCode = Interop.Errors.ERROR_SUCCESS;
5859

@@ -97,7 +98,7 @@ internal static int FillAttributeInfo(string path, ref Interop.Kernel32.WIN32_FI
9798
// cases that we know we don't want to retry on.
9899

99100
Interop.Kernel32.WIN32_FIND_DATA findData = default;
100-
using (SafeFindHandle handle = Interop.Kernel32.FindFirstFile(path, ref findData))
101+
using (SafeFindHandle handle = Interop.Kernel32.FindFirstFile(path!, ref findData))
101102
{
102103
if (handle.IsInvalid)
103104
{

src/libraries/Common/src/System/IO/FileSystem.DirectoryCreation.Windows.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
56
using Microsoft.Win32.SafeHandles;
67
using System;
78
using System.Collections.Generic;
@@ -18,7 +19,7 @@ namespace System.IO
1819
{
1920
internal static partial class FileSystem
2021
{
21-
public static unsafe void CreateDirectory(string fullPath, byte[] securityDescriptor = null)
22+
public static unsafe void CreateDirectory(string fullPath, byte[]? securityDescriptor = null)
2223
{
2324
// We can save a bunch of work if the directory we want to create already exists. This also
2425
// saves us in the case where sub paths are inaccessible (due to ERROR_ACCESS_DENIED) but the
@@ -126,7 +127,7 @@ public static unsafe void CreateDirectory(string fullPath, byte[] securityDescri
126127
// Handle CreateDirectory("X:\\") when X: doesn't exist. Similarly for n/w paths.
127128
if ((count == 0) && !somepathexists)
128129
{
129-
string root = Path.GetPathRoot(fullPath);
130+
string? root = Path.GetPathRoot(fullPath);
130131

131132
if (!DirectoryExists(root))
132133
{

src/libraries/System.IO.FileSystem/ref/System.IO.FileSystem.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static void Delete(string path, bool recursive) { }
2424
public static System.Collections.Generic.IEnumerable<string> EnumerateFileSystemEntries(string path, string searchPattern) { throw null; }
2525
public static System.Collections.Generic.IEnumerable<string> EnumerateFileSystemEntries(string path, string searchPattern, System.IO.EnumerationOptions enumerationOptions) { throw null; }
2626
public static System.Collections.Generic.IEnumerable<string> EnumerateFileSystemEntries(string path, string searchPattern, System.IO.SearchOption searchOption) { throw null; }
27-
public static bool Exists(string path) { throw null; }
27+
public static bool Exists(string? path) { throw null; }
2828
public static System.DateTime GetCreationTime(string path) { throw null; }
2929
public static System.DateTime GetCreationTimeUtc(string path) { throw null; }
3030
public static string GetCurrentDirectory() { throw null; }
@@ -46,7 +46,7 @@ public static void Delete(string path, bool recursive) { }
4646
public static System.DateTime GetLastWriteTime(string path) { throw null; }
4747
public static System.DateTime GetLastWriteTimeUtc(string path) { throw null; }
4848
public static string[] GetLogicalDrives() { throw null; }
49-
public static System.IO.DirectoryInfo GetParent(string path) { throw null; }
49+
public static System.IO.DirectoryInfo? GetParent(string path) { throw null; }
5050
public static void Move(string sourceDirName, string destDirName) { }
5151
public static void SetCreationTime(string path, System.DateTime creationTime) { }
5252
public static void SetCreationTimeUtc(string path, System.DateTime creationTimeUtc) { }
@@ -61,7 +61,7 @@ public sealed partial class DirectoryInfo : System.IO.FileSystemInfo
6161
public DirectoryInfo(string path) { }
6262
public override bool Exists { get { throw null; } }
6363
public override string Name { get { throw null; } }
64-
public System.IO.DirectoryInfo Parent { get { throw null; } }
64+
public System.IO.DirectoryInfo? Parent { get { throw null; } }
6565
public System.IO.DirectoryInfo Root { get { throw null; } }
6666
public void Create() { }
6767
public System.IO.DirectoryInfo CreateSubdirectory(string path) { throw null; }
@@ -111,10 +111,10 @@ public static void AppendAllLines(string path, System.Collections.Generic.IEnume
111111
public static void AppendAllLines(string path, System.Collections.Generic.IEnumerable<string> contents, System.Text.Encoding encoding) { }
112112
public static System.Threading.Tasks.Task AppendAllLinesAsync(string path, System.Collections.Generic.IEnumerable<string> contents, System.Text.Encoding encoding, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
113113
public static System.Threading.Tasks.Task AppendAllLinesAsync(string path, System.Collections.Generic.IEnumerable<string> contents, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
114-
public static void AppendAllText(string path, string contents) { }
115-
public static void AppendAllText(string path, string contents, System.Text.Encoding encoding) { }
116-
public static System.Threading.Tasks.Task AppendAllTextAsync(string path, string contents, System.Text.Encoding encoding, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
117-
public static System.Threading.Tasks.Task AppendAllTextAsync(string path, string contents, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
114+
public static void AppendAllText(string path, string? contents) { }
115+
public static void AppendAllText(string path, string? contents, System.Text.Encoding encoding) { }
116+
public static System.Threading.Tasks.Task AppendAllTextAsync(string path, string? contents, System.Text.Encoding encoding, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
117+
public static System.Threading.Tasks.Task AppendAllTextAsync(string path, string? contents, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
118118
public static System.IO.StreamWriter AppendText(string path) { throw null; }
119119
public static void Copy(string sourceFileName, string destFileName) { }
120120
public static void Copy(string sourceFileName, string destFileName, bool overwrite) { }
@@ -125,7 +125,7 @@ public static void Copy(string sourceFileName, string destFileName, bool overwri
125125
public static void Decrypt(string path) { }
126126
public static void Delete(string path) { }
127127
public static void Encrypt(string path) { }
128-
public static bool Exists(string path) { throw null; }
128+
public static bool Exists(string? path) { throw null; }
129129
public static System.IO.FileAttributes GetAttributes(string path) { throw null; }
130130
public static System.DateTime GetCreationTime(string path) { throw null; }
131131
public static System.DateTime GetCreationTimeUtc(string path) { throw null; }
@@ -153,8 +153,8 @@ public static void Move(string sourceFileName, string destFileName, bool overwri
153153
public static System.Threading.Tasks.Task<string> ReadAllTextAsync(string path, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
154154
public static System.Collections.Generic.IEnumerable<string> ReadLines(string path) { throw null; }
155155
public static System.Collections.Generic.IEnumerable<string> ReadLines(string path, System.Text.Encoding encoding) { throw null; }
156-
public static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName) { }
157-
public static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors) { }
156+
public static void Replace(string sourceFileName, string destinationFileName, string? destinationBackupFileName) { }
157+
public static void Replace(string sourceFileName, string destinationFileName, string? destinationBackupFileName, bool ignoreMetadataErrors) { }
158158
public static void SetAttributes(string path, System.IO.FileAttributes fileAttributes) { }
159159
public static void SetCreationTime(string path, System.DateTime creationTime) { }
160160
public static void SetCreationTimeUtc(string path, System.DateTime creationTimeUtc) { }
@@ -170,16 +170,16 @@ public static void WriteAllLines(string path, string[] contents) { }
170170
public static void WriteAllLines(string path, string[] contents, System.Text.Encoding encoding) { }
171171
public static System.Threading.Tasks.Task WriteAllLinesAsync(string path, System.Collections.Generic.IEnumerable<string> contents, System.Text.Encoding encoding, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
172172
public static System.Threading.Tasks.Task WriteAllLinesAsync(string path, System.Collections.Generic.IEnumerable<string> contents, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
173-
public static void WriteAllText(string path, string contents) { }
174-
public static void WriteAllText(string path, string contents, System.Text.Encoding encoding) { }
175-
public static System.Threading.Tasks.Task WriteAllTextAsync(string path, string contents, System.Text.Encoding encoding, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
176-
public static System.Threading.Tasks.Task WriteAllTextAsync(string path, string contents, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
173+
public static void WriteAllText(string path, string? contents) { }
174+
public static void WriteAllText(string path, string? contents, System.Text.Encoding encoding) { }
175+
public static System.Threading.Tasks.Task WriteAllTextAsync(string path, string? contents, System.Text.Encoding encoding, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
176+
public static System.Threading.Tasks.Task WriteAllTextAsync(string path, string? contents, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
177177
}
178178
public sealed partial class FileInfo : System.IO.FileSystemInfo
179179
{
180180
public FileInfo(string fileName) { }
181-
public System.IO.DirectoryInfo Directory { get { throw null; } }
182-
public string DirectoryName { get { throw null; } }
181+
public System.IO.DirectoryInfo? Directory { get { throw null; } }
182+
public string? DirectoryName { get { throw null; } }
183183
public override bool Exists { get { throw null; } }
184184
public bool IsReadOnly { get { throw null; } set { } }
185185
public long Length { get { throw null; } }
@@ -200,8 +200,8 @@ public void MoveTo(string destFileName, bool overwrite) { }
200200
public System.IO.FileStream OpenRead() { throw null; }
201201
public System.IO.StreamReader OpenText() { throw null; }
202202
public System.IO.FileStream OpenWrite() { throw null; }
203-
public System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName) { throw null; }
204-
public System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors) { throw null; }
203+
public System.IO.FileInfo Replace(string destinationFileName, string? destinationBackupFileName) { throw null; }
204+
public System.IO.FileInfo Replace(string destinationFileName, string? destinationBackupFileName, bool ignoreMetadataErrors) { throw null; }
205205
public override string ToString() { throw null; }
206206
}
207207
public abstract partial class FileSystemInfo : System.MarshalByRefObject, System.Runtime.Serialization.ISerializable
@@ -266,19 +266,19 @@ public ref partial struct FileSystemEntry
266266
}
267267
public partial class FileSystemEnumerable<TResult> : System.Collections.Generic.IEnumerable<TResult>, System.Collections.IEnumerable
268268
{
269-
public FileSystemEnumerable(string directory, System.IO.Enumeration.FileSystemEnumerable<TResult>.FindTransform transform, System.IO.EnumerationOptions options = null) { }
270-
public System.IO.Enumeration.FileSystemEnumerable<TResult>.FindPredicate ShouldIncludePredicate { get { throw null; } set { } }
271-
public System.IO.Enumeration.FileSystemEnumerable<TResult>.FindPredicate ShouldRecursePredicate { get { throw null; } set { } }
269+
public FileSystemEnumerable(string directory, System.IO.Enumeration.FileSystemEnumerable<TResult>.FindTransform transform, System.IO.EnumerationOptions? options = null) { }
270+
public System.IO.Enumeration.FileSystemEnumerable<TResult>.FindPredicate? ShouldIncludePredicate { get { throw null; } set { } }
271+
public System.IO.Enumeration.FileSystemEnumerable<TResult>.FindPredicate? ShouldRecursePredicate { get { throw null; } set { } }
272272
public System.Collections.Generic.IEnumerator<TResult> GetEnumerator() { throw null; }
273273
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; }
274274
public delegate bool FindPredicate(ref System.IO.Enumeration.FileSystemEntry entry);
275275
public delegate TResult FindTransform(ref System.IO.Enumeration.FileSystemEntry entry);
276276
}
277277
public abstract partial class FileSystemEnumerator<TResult> : System.Runtime.ConstrainedExecution.CriticalFinalizerObject, System.Collections.Generic.IEnumerator<TResult>, System.Collections.IEnumerator, System.IDisposable
278278
{
279-
public FileSystemEnumerator(string directory, System.IO.EnumerationOptions options = null) { }
279+
public FileSystemEnumerator(string directory, System.IO.EnumerationOptions? options = null) { }
280280
public TResult Current { get { throw null; } }
281-
object System.Collections.IEnumerator.Current { get { throw null; } }
281+
object? System.Collections.IEnumerator.Current { get { throw null; } }
282282
protected virtual bool ContinueOnError(int error) { throw null; }
283283
public void Dispose() { }
284284
protected virtual void Dispose(bool disposing) { }
@@ -293,6 +293,6 @@ public static partial class FileSystemName
293293
{
294294
public static bool MatchesSimpleExpression(System.ReadOnlySpan<char> expression, System.ReadOnlySpan<char> name, bool ignoreCase = true) { throw null; }
295295
public static bool MatchesWin32Expression(System.ReadOnlySpan<char> expression, System.ReadOnlySpan<char> name, bool ignoreCase = true) { throw null; }
296-
public static string TranslateWin32Expression(string expression) { throw null; }
296+
public static string TranslateWin32Expression(string? expression) { throw null; }
297297
}
298298
}

src/libraries/System.IO.FileSystem/ref/System.IO.FileSystem.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Configurations>netcoreapp-Debug;netcoreapp-Release</Configurations>
4+
<Nullable>enable</Nullable>
45
</PropertyGroup>
56
<ItemGroup>
67
<Compile Include="System.IO.FileSystem.cs" />

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<NoWarn>$(NoWarn);CS1573</NoWarn>
66
<IsPartialFacadeAssembly>true</IsPartialFacadeAssembly>
77
<Configurations>netcoreapp-Unix-Debug;netcoreapp-Unix-Release;netcoreapp-Windows_NT-Debug;netcoreapp-Windows_NT-Release</Configurations>
8+
<Nullable>enable</Nullable>
89
</PropertyGroup>
910
<PropertyGroup Condition="'$(TargetsUnix)' == 'true'">
1011
<NoWarn>$(NoWarn);414</NoWarn>

0 commit comments

Comments
 (0)