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 6 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 @@ -16,9 +16,9 @@ public PhysicalFileProvider(string root, Microsoft.Extensions.FileProviders.Phys
public void Dispose() { }
protected virtual void Dispose(bool disposing) { }
~PhysicalFileProvider() { }
public Microsoft.Extensions.FileProviders.IDirectoryContents GetDirectoryContents(string subpath) { throw null; }
public Microsoft.Extensions.FileProviders.IDirectoryContents GetDirectoryContents(string? subpath) { throw null; }
public Microsoft.Extensions.FileProviders.IFileInfo GetFileInfo(string subpath) { throw null; }
public Microsoft.Extensions.Primitives.IChangeToken Watch(string filter) { throw null; }
public Microsoft.Extensions.Primitives.IChangeToken Watch(string? filter) { throw null; }
}
}
namespace Microsoft.Extensions.FileProviders.Internal
Expand Down 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,7 +29,7 @@ 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();

Expand Down Expand Up @@ -145,7 +146,7 @@ internal PhysicalFilesWatcher FileWatcher
ref _fileWatcher,
ref _fileWatcherInitialized,
ref _fileWatcherLock,
_fileWatcherFactory);
_fileWatcherFactory) ?? throw new NullReferenceException(nameof(_fileWatcher));
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
}
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 All @@ -297,7 +300,7 @@ public IFileInfo GetFileInfo(string subpath)
/// <paramref name="subpath" /> is absolute, if the directory does not exist, or <paramref name="subpath" /> has invalid
/// characters.
/// </returns>
public IDirectoryContents GetDirectoryContents(string subpath)
public IDirectoryContents GetDirectoryContents(string? subpath)
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
{
try
{
Expand All @@ -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 Expand Up @@ -346,7 +349,7 @@ public IDirectoryContents GetDirectoryContents(string subpath)
/// characters or if <paramref name="filter" /> is an absolute path or outside the root directory specified in the
/// constructor <seealso cref="PhysicalFileProvider(string)" />.
/// </returns>
public IChangeToken Watch(string filter)
public IChangeToken Watch(string? filter)
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
{
if (filter == null || PathUtils.HasInvalidFilterChars(filter))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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();
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved

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

Expand All @@ -34,12 +34,12 @@ public class PhysicalFilesWatcher : IDisposable
private readonly ConcurrentDictionary<string, ChangeTokenInfo> _wildcardTokenLookup =
new ConcurrentDictionary<string, ChangeTokenInfo>(StringComparer.OrdinalIgnoreCase);

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

private Timer _timer;
private Timer? _timer;
private bool _timerInitialzed;
private object _timerLock = new object();
private Func<Timer> _timerFactory;
Expand All @@ -57,7 +57,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 +76,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,8 +369,9 @@ private void ReportChangeForMatchedEntries(string path)

foreach (System.Collections.Generic.KeyValuePair<string, ChangeTokenInfo> wildCardEntry in _wildcardTokenLookup)
{
PatternMatchingResult matchResult = wildCardEntry.Value.Matcher.Match(path);
if (matchResult.HasMatches &&
PatternMatchingResult? matchResult = wildCardEntry.Value.Matcher?.Match(path);
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
if (matchResult != null &&
matchResult.HasMatches &&
_wildcardTokenLookup.TryRemove(wildCardEntry.Key, out matchInfo))
{
CancelToken(matchInfo);
Expand Down Expand Up @@ -443,8 +444,13 @@ private static void CancelToken(ChangeTokenInfo matchInfo)
TaskScheduler.Default);
}

internal static void RaiseChangeEvents(object state)
internal static void RaiseChangeEvents(object? state)
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
{
if (state == null)
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
{
throw new ArgumentNullException(nameof(state));
}

// 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 +472,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();
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
}
catch
{
Expand All @@ -487,7 +493,7 @@ public ChangeTokenInfo(
public ChangeTokenInfo(
CancellationTokenSource tokenSource,
CancellationChangeToken changeToken,
Matcher matcher)
Matcher? matcher)
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
{
TokenSource = tokenSource;
ChangeToken = changeToken;
Expand All @@ -498,7 +504,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,9 +129,9 @@ 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)
if (!ActiveChangeCallbacks || _changeToken == null)
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
{
return EmptyDisposable.Instance;
}
Expand Down
Loading