Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Group 3] Enable nullable annotations for Microsoft.Extensions.FileProviders.Physical #57409

Merged
merged 9 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public PhysicalFileInfo(System.IO.FileInfo info) { }
}
public partial class PhysicalFilesWatcher : System.IDisposable
{
public PhysicalFilesWatcher(string root, System.IO.FileSystemWatcher fileSystemWatcher, bool pollForChanges) { }
public PhysicalFilesWatcher(string root, System.IO.FileSystemWatcher fileSystemWatcher, bool pollForChanges, Microsoft.Extensions.FileProviders.Physical.ExclusionFilters filters) { }
public PhysicalFilesWatcher(string root, System.IO.FileSystemWatcher? fileSystemWatcher, bool pollForChanges) { }
public PhysicalFilesWatcher(string root, System.IO.FileSystemWatcher? fileSystemWatcher, bool pollForChanges, Microsoft.Extensions.FileProviders.Physical.ExclusionFilters filters) { }
public Microsoft.Extensions.Primitives.IChangeToken CreateFileChangeToken(string filter) { throw null; }
public void Dispose() { }
protected virtual void Dispose(bool disposing) { }
Expand All @@ -79,14 +79,14 @@ public partial class PollingFileChangeToken : Microsoft.Extensions.Primitives.IC
public PollingFileChangeToken(System.IO.FileInfo fileInfo) { }
public bool ActiveChangeCallbacks { get { throw null; } }
public bool HasChanged { get { throw null; } }
public System.IDisposable RegisterChangeCallback(System.Action<object> callback, object state) { throw null; }
public System.IDisposable RegisterChangeCallback(System.Action<object?> callback, object? state) { throw null; }
}
public partial class PollingWildCardChangeToken : Microsoft.Extensions.Primitives.IChangeToken
{
public PollingWildCardChangeToken(string root, string pattern) { }
public bool ActiveChangeCallbacks { get { throw null; } }
public bool HasChanged { get { throw null; } }
protected virtual System.DateTime GetLastWriteUtc(string path) { throw null; }
System.IDisposable Microsoft.Extensions.Primitives.IChangeToken.RegisterChangeCallback(System.Action<object> callback, object state) { throw null; }
System.IDisposable Microsoft.Extensions.Primitives.IChangeToken.RegisterChangeCallback(System.Action<object?> callback, object? state) { throw null; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);netstandard2.0;net461</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="Microsoft.Extensions.FileProviders.Physical.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ namespace Microsoft.Extensions.FileProviders
{
internal interface IPollingChangeToken : IChangeToken
{
CancellationTokenSource CancellationTokenSource { get; }
CancellationTokenSource? CancellationTokenSource { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static bool IsExcluded(FileSystemInfo fileSystemInfo, ExclusionFilters fi
{
try
{
FileSystemInfo targetInfo = fileInfo.ResolveLinkTarget(returnFinalTarget: true);
FileSystemInfo? targetInfo = fileInfo.ResolveLinkTarget(returnFinalTarget: true);
if (targetInfo != null && targetInfo.Exists)
{
return targetInfo.LastWriteTimeUtc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using Microsoft.Extensions.FileProviders.Physical;
Expand All @@ -15,7 +16,7 @@ namespace Microsoft.Extensions.FileProviders.Internal
/// </summary>
public class PhysicalDirectoryContents : IDirectoryContents
{
private IEnumerable<IFileInfo> _entries;
private IEnumerable<IFileInfo>? _entries;
private readonly string _directory;
private readonly ExclusionFilters _filters;

Expand Down Expand Up @@ -54,6 +55,7 @@ IEnumerator IEnumerable.GetEnumerator()
return _entries.GetEnumerator();
}

[MemberNotNull(nameof(_entries))]
private void EnsureInitialized()
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<RootNamespace>Microsoft.Extensions.FileProviders</RootNamespace>
<TargetFrameworks>$(NetCoreAppCurrent);netstandard2.0;net461</TargetFrameworks>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableDefaultItems>true</EnableDefaultItems>
<PackageDescription>File provider for physical files for Microsoft.Extensions.FileProviders.</PackageDescription>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading;
using Microsoft.Extensions.FileProviders.Internal;
Expand All @@ -28,9 +29,9 @@ public class PhysicalFileProvider : IFileProvider, IDisposable
private readonly ExclusionFilters _filters;

private readonly Func<PhysicalFilesWatcher> _fileWatcherFactory;
private PhysicalFilesWatcher _fileWatcher;
private PhysicalFilesWatcher? _fileWatcher;
private bool _fileWatcherInitialized;
private object _fileWatcherLock = new object();
private object _fileWatcherLock = new();

private bool? _usePollingFileWatcher;
private bool? _useActivePolling;
Expand Down Expand Up @@ -145,7 +146,7 @@ internal PhysicalFilesWatcher FileWatcher
ref _fileWatcher,
ref _fileWatcherInitialized,
ref _fileWatcherLock,
_fileWatcherFactory);
_fileWatcherFactory)!;
}
set
{
Expand All @@ -160,7 +161,7 @@ internal PhysicalFilesWatcher CreateFileWatcher()
{
string root = PathUtils.EnsureTrailingSlash(Path.GetFullPath(Root));

FileSystemWatcher watcher;
FileSystemWatcher? watcher;
#if NETCOREAPP
// For browser we will proactively fallback to polling since FileSystemWatcher is not supported.
if (OperatingSystem.IsBrowser())
Expand All @@ -182,9 +183,11 @@ internal PhysicalFilesWatcher CreateFileWatcher()
};
}

[MemberNotNull(nameof(_usePollingFileWatcher))]
[MemberNotNull(nameof(_useActivePolling))]
private void ReadPollingEnvironmentVariables()
{
string environmentValue = Environment.GetEnvironmentVariable(PollingEnvironmentKey);
string? environmentValue = Environment.GetEnvironmentVariable(PollingEnvironmentKey);
bool pollForChanges = string.Equals(environmentValue, "1", StringComparison.Ordinal) ||
string.Equals(environmentValue, "true", StringComparison.OrdinalIgnoreCase);

Expand Down Expand Up @@ -222,7 +225,7 @@ protected virtual void Dispose(bool disposing)
/// </summary>
public string Root { get; }

private string GetFullPath(string path)
private string? GetFullPath(string path)
{
if (PathUtils.PathNavigatesAboveRoot(path))
{
Expand Down Expand Up @@ -273,7 +276,7 @@ public IFileInfo GetFileInfo(string subpath)
return new NotFoundFileInfo(subpath);
}

string fullPath = GetFullPath(subpath);
string? fullPath = GetFullPath(subpath);
if (fullPath == null)
{
return new NotFoundFileInfo(subpath);
Expand Down Expand Up @@ -315,7 +318,7 @@ public IDirectoryContents GetDirectoryContents(string subpath)
return NotFoundDirectoryContents.Singleton;
}

string fullPath = GetFullPath(subpath);
string? fullPath = GetFullPath(subpath);
if (fullPath == null || !Directory.Exists(fullPath))
{
return NotFoundDirectoryContents.Singleton;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Runtime.Versioning;
using System.Security;
Expand All @@ -25,23 +26,21 @@ namespace Microsoft.Extensions.FileProviders.Physical
/// </summary>
public class PhysicalFilesWatcher : IDisposable
{
private static readonly Action<object> _cancelTokenSource = state => ((CancellationTokenSource)state).Cancel();
private static readonly Action<object?> _cancelTokenSource = state => ((CancellationTokenSource?)state)!.Cancel();

internal static TimeSpan DefaultPollingInterval = TimeSpan.FromSeconds(4);

private readonly ConcurrentDictionary<string, ChangeTokenInfo> _filePathTokenLookup =
new ConcurrentDictionary<string, ChangeTokenInfo>(StringComparer.OrdinalIgnoreCase);
private readonly ConcurrentDictionary<string, ChangeTokenInfo> _wildcardTokenLookup =
new ConcurrentDictionary<string, ChangeTokenInfo>(StringComparer.OrdinalIgnoreCase);
private readonly ConcurrentDictionary<string, ChangeTokenInfo> _filePathTokenLookup = new(StringComparer.OrdinalIgnoreCase);
private readonly ConcurrentDictionary<string, ChangeTokenInfo> _wildcardTokenLookup = new(StringComparer.OrdinalIgnoreCase);

private readonly FileSystemWatcher _fileWatcher;
private readonly object _fileWatcherLock = new object();
private readonly FileSystemWatcher? _fileWatcher;
private readonly object _fileWatcherLock = new();
private readonly string _root;
private readonly ExclusionFilters _filters;

private Timer _timer;
private Timer? _timer;
private bool _timerInitialzed;
private object _timerLock = new object();
private object _timerLock = new();
private Func<Timer> _timerFactory;
private bool _disposed;

Expand All @@ -57,7 +56,7 @@ public class PhysicalFilesWatcher : IDisposable
/// </param>
public PhysicalFilesWatcher(
string root,
FileSystemWatcher fileSystemWatcher,
FileSystemWatcher? fileSystemWatcher,
bool pollForChanges)
: this(root, fileSystemWatcher, pollForChanges, ExclusionFilters.Sensitive)
{
Expand All @@ -76,7 +75,7 @@ public PhysicalFilesWatcher(
/// <param name="filters">Specifies which files or directories are excluded. Notifications of changes to are not raised to these.</param>
public PhysicalFilesWatcher(
string root,
FileSystemWatcher fileSystemWatcher,
FileSystemWatcher? fileSystemWatcher,
bool pollForChanges,
ExclusionFilters filters)
{
Expand Down Expand Up @@ -369,7 +368,7 @@ private void ReportChangeForMatchedEntries(string path)

foreach (System.Collections.Generic.KeyValuePair<string, ChangeTokenInfo> wildCardEntry in _wildcardTokenLookup)
{
PatternMatchingResult matchResult = wildCardEntry.Value.Matcher.Match(path);
PatternMatchingResult matchResult = wildCardEntry.Value.Matcher!.Match(path);
if (matchResult.HasMatches &&
_wildcardTokenLookup.TryRemove(wildCardEntry.Key, out matchInfo))
{
Expand Down Expand Up @@ -443,8 +442,10 @@ private static void CancelToken(ChangeTokenInfo matchInfo)
TaskScheduler.Default);
}

internal static void RaiseChangeEvents(object state)
internal static void RaiseChangeEvents(object? state)
{
Debug.Assert(state != null);

// Iterating over a concurrent bag gives us a point in time snapshot making it safe
// to remove items from it.
var changeTokens = (ConcurrentDictionary<IPollingChangeToken, IPollingChangeToken>)state;
Expand All @@ -466,7 +467,7 @@ internal static void RaiseChangeEvents(object state)
// We're already on a background thread, don't need to spawn a background Task to cancel the CTS
try
{
token.CancellationTokenSource.Cancel();
token.CancellationTokenSource!.Cancel();
}
catch
{
Expand All @@ -487,7 +488,7 @@ public ChangeTokenInfo(
public ChangeTokenInfo(
CancellationTokenSource tokenSource,
CancellationChangeToken changeToken,
Matcher matcher)
Matcher? matcher)
{
TokenSource = tokenSource;
ChangeToken = changeToken;
Expand All @@ -498,7 +499,7 @@ public ChangeTokenInfo(

public CancellationChangeToken ChangeToken { get; }

public Matcher Matcher { get; }
public Matcher? Matcher { get; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading;
using Microsoft.Extensions.Primitives;
Expand All @@ -28,8 +29,8 @@ public class PollingFileChangeToken : IPollingChangeToken
private DateTime _previousWriteTimeUtc;
private DateTime _lastCheckedTimeUtc;
private bool _hasChanged;
private CancellationTokenSource _tokenSource;
private CancellationChangeToken _changeToken;
private CancellationTokenSource? _tokenSource;
private CancellationChangeToken? _changeToken;

/// <summary>
/// Initializes a new instance of <see cref="PollingFileChangeToken" /> that polls the specified file for changes as
Expand Down Expand Up @@ -73,7 +74,8 @@ private DateTime GetLastWriteTimeUtc()
/// </summary>
public bool ActiveChangeCallbacks { get; internal set; }

internal CancellationTokenSource CancellationTokenSource
[DisallowNull]
internal CancellationTokenSource? CancellationTokenSource
{
get => _tokenSource;
set
Expand All @@ -85,7 +87,7 @@ internal CancellationTokenSource CancellationTokenSource
}
}

CancellationTokenSource IPollingChangeToken.CancellationTokenSource => CancellationTokenSource;
CancellationTokenSource? IPollingChangeToken.CancellationTokenSource => CancellationTokenSource;

/// <summary>
/// True when the file has changed since the change token was created. Once the file changes, this value is always true
Expand Down Expand Up @@ -127,14 +129,14 @@ public bool HasChanged
/// <param name="callback">This parameter is ignored</param>
/// <param name="state">This parameter is ignored</param>
/// <returns>A disposable object that noops when disposed</returns>
public IDisposable RegisterChangeCallback(Action<object> callback, object state)
public IDisposable RegisterChangeCallback(Action<object?> callback, object? state)
{
if (!ActiveChangeCallbacks)
{
return EmptyDisposable.Instance;
}

return _changeToken.RegisterChangeCallback(callback, state);
return _changeToken!.RegisterChangeCallback(callback, state);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
Expand All @@ -20,15 +21,15 @@ namespace Microsoft.Extensions.FileProviders.Physical
public class PollingWildCardChangeToken : IPollingChangeToken
{
private static readonly byte[] Separator = Encoding.Unicode.GetBytes("|");
private readonly object _enumerationLock = new object();
private readonly object _enumerationLock = new();
private readonly DirectoryInfoBase _directoryInfo;
private readonly Matcher _matcher;
private bool _changed;
private DateTime? _lastScanTimeUtc;
private byte[] _byteBuffer;
private byte[] _previousHash;
private CancellationTokenSource _tokenSource;
private CancellationChangeToken _changeToken;
private byte[]? _byteBuffer;
private byte[]? _previousHash;
private CancellationTokenSource? _tokenSource;
private CancellationChangeToken? _changeToken;

/// <summary>
/// Initializes a new instance of <see cref="PollingWildCardChangeToken"/>.
Expand Down Expand Up @@ -65,7 +66,8 @@ internal PollingWildCardChangeToken(
// Internal for unit testing.
internal TimeSpan PollingInterval { get; set; } = PhysicalFilesWatcher.DefaultPollingInterval;

internal CancellationTokenSource CancellationTokenSource
[DisallowNull]
internal CancellationTokenSource? CancellationTokenSource
{
get => _tokenSource;
set
Expand All @@ -77,7 +79,7 @@ internal CancellationTokenSource CancellationTokenSource
}
}

CancellationTokenSource IPollingChangeToken.CancellationTokenSource => CancellationTokenSource;
CancellationTokenSource? IPollingChangeToken.CancellationTokenSource => CancellationTokenSource;

private IClock Clock { get; }

Expand Down Expand Up @@ -147,7 +149,7 @@ protected virtual DateTime GetLastWriteUtc(string path)
return FileSystemInfoHelper.GetFileLinkTargetLastWriteTimeUtc(filePath) ?? File.GetLastWriteTimeUtc(filePath);
}

private static bool ArrayEquals(byte[] previousHash, byte[] currentHash)
private static bool ArrayEquals(byte[]? previousHash, byte[] currentHash)
{
if (previousHash == null)
{
Expand Down Expand Up @@ -191,14 +193,14 @@ private void ComputeHash(IncrementalHash sha256, string path, DateTime lastChang
sha256.AppendData(Separator, 0, Separator.Length);
}

IDisposable IChangeToken.RegisterChangeCallback(Action<object> callback, object state)
IDisposable IChangeToken.RegisterChangeCallback(Action<object?> callback, object? state)
{
if (!ActiveChangeCallbacks)
{
return EmptyDisposable.Instance;
}

return _changeToken.RegisterChangeCallback(callback, state);
return _changeToken!.RegisterChangeCallback(callback, state);
}
}
}