Skip to content

Commit

Permalink
Add missing bits and bobs to IFileSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
Kir-Antipov committed Jan 15, 2025
1 parent 2720833 commit 512cddb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
17 changes: 15 additions & 2 deletions src/HotAvalonia/IO/CachingFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public CachingFileSystem(IFileSystem fileSystem, IEqualityComparer<string>? file
public IFileSystemWatcher CreateFileSystemWatcher()
=> _fileSystem.CreateFileSystemWatcher();

/// <inheritdoc/>
public ValueTask<IFileSystemWatcher> CreateFileSystemWatcherAsync(CancellationToken cancellationToken = default)
=> _fileSystem.CreateFileSystemWatcherAsync(cancellationToken);

/// <inheritdoc/>
public bool DirectoryExists([NotNullWhen(true)] string? path)
=> _fileSystem.DirectoryExists(path);
Expand Down Expand Up @@ -93,11 +97,12 @@ public string GetFullPath(string path)
=> _fileSystem.GetFullPath(path);

/// <inheritdoc/>
public string GetDirectoryName(string path)
public string? GetDirectoryName(string? path)
=> _fileSystem.GetDirectoryName(path);

/// <inheritdoc/>
public string GetFileName(string path)
[return: NotNullIfNotNull(nameof(path))]
public string? GetFileName(string? path)
=> _fileSystem.GetFileName(path);

/// <inheritdoc/>
Expand Down Expand Up @@ -138,6 +143,14 @@ public async Task<Stream> OpenReadAsync(string path, CancellationToken cancellat
return await _cache.GetOrAdd(path, x => new(x, _fileSystem)).OpenReadAsync(cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc/>
public void Dispose()
=> _fileSystem.Dispose();

/// <inheritdoc/>
public ValueTask DisposeAsync()
=> _fileSystem.DisposeAsync();

/// <summary>
/// Represents a cached file entry.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/HotAvalonia/IO/FileObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@ private void Watch(string fileName)

lock (_lock)
{
string newPath = string.IsNullOrEmpty(_watcher.Path)
string? newPath = string.IsNullOrEmpty(_watcher.Path)
? FileSystem.GetDirectoryName(fileName)
: FileSystem.GetCommonPath(fileName, _watcher.Path);
if (string.IsNullOrEmpty(newPath))
if (newPath is null or { Length: 0 })
return;

_watcher.Path = newPath;
Expand Down
16 changes: 13 additions & 3 deletions src/HotAvalonia/IO/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace HotAvalonia.IO;
/// <summary>
/// Represents an abstraction for interacting with a file system.
/// </summary>
public interface IFileSystem
public interface IFileSystem : IDisposable, IAsyncDisposable
{
/// <summary>
/// Gets the <see cref="StringComparer"/> used to compare file or directory paths.
Expand Down Expand Up @@ -42,6 +42,15 @@ public interface IFileSystem
/// </returns>
IFileSystemWatcher CreateFileSystemWatcher();

/// <summary>
/// Asynchronously creates a new instance of <see cref="IFileSystemWatcher"/> to monitor changes in the file system.
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>
/// A new <see cref="IFileSystemWatcher"/> instance.
/// </returns>
ValueTask<IFileSystemWatcher> CreateFileSystemWatcherAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Returns the absolute path for the specified path string.
/// </summary>
Expand All @@ -58,7 +67,7 @@ public interface IFileSystem
/// <returns>
/// The directory information for <paramref name="path"/>.
/// </returns>
string GetDirectoryName(string path);
string? GetDirectoryName(string? path);

/// <summary>
/// Returns the file name and extension of the specified path string.
Expand All @@ -67,7 +76,8 @@ public interface IFileSystem
/// <returns>
/// The characters after the last directory separator character in <paramref name="path"/>.
/// </returns>
string GetFileName(string path);
[return: NotNullIfNotNull(nameof(path))]
string? GetFileName(string? path);

/// <summary>
/// Changes the extension of a path string.
Expand Down
15 changes: 13 additions & 2 deletions src/HotAvalonia/IO/LocalFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma warning disable RS0030 // Do not use banned APIs

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -34,6 +35,9 @@ internal sealed class LocalFileSystem : IFileSystem
/// <inheritdoc/>
public IFileSystemWatcher CreateFileSystemWatcher() => new LocalFileSystemWatcher(this);

/// <inheritdoc/>
ValueTask<IFileSystemWatcher> IFileSystem.CreateFileSystemWatcherAsync(CancellationToken cancellationToken) => new(CreateFileSystemWatcher());

/// <inheritdoc/>
public DateTime GetLastWriteTimeUtc(string path) => File.GetLastWriteTimeUtc(path);

Expand All @@ -44,10 +48,11 @@ internal sealed class LocalFileSystem : IFileSystem
public string GetFullPath(string path) => Path.GetFullPath(path);

/// <inheritdoc/>
public string GetDirectoryName(string path) => Path.GetDirectoryName(path);
public string? GetDirectoryName(string? path) => Path.GetDirectoryName(path);

/// <inheritdoc/>
public string GetFileName(string path) => Path.GetFileName(path);
[return: NotNullIfNotNull(nameof(path))]
public string? GetFileName(string? path) => Path.GetFileName(path);

/// <inheritdoc/>
public string Combine(string path1, string path2) => Path.Combine(path1, path2);
Expand Down Expand Up @@ -83,6 +88,12 @@ async IAsyncEnumerable<string> IFileSystem.EnumerateFilesAsync(string path, stri

/// <inheritdoc/>
Task<Stream> IFileSystem.OpenReadAsync(string path, CancellationToken cancellationToken) => Task.FromResult(OpenRead(path));

/// <inheritdoc/>
void IDisposable.Dispose() { }

/// <inheritdoc/>
ValueTask IAsyncDisposable.DisposeAsync() => default;
}

/// <summary>
Expand Down

0 comments on commit 512cddb

Please sign in to comment.