Skip to content
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
5 changes: 4 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
name: Tests

# The module built and tested on the core, by the workflow of the core.
# By itself only on the way into master; on any other branch by hand (Run workflow).

on:
workflow_dispatch:
push:
branches: [develop, master]
branches: [master]
pull_request:
branches: [master]

permissions:
contents: read
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"msbuild-sdks": {
"Stellar.Sdk": "0.4.1"
"Stellar.Sdk": "0.4.2"
}
}
110 changes: 75 additions & 35 deletions src/Managed/Stellar.FileSystem/Domains/DomainBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class DomainBase
: Quant<MetaQuant>, IDomain
{
private static readonly IReadOnlyDictionary<string, string> NoOptions = new Dictionary<string, string>();
private volatile bool _disposed;
private int _disposed;

/// <inheritdoc/>
public string Name { get; }
Expand All @@ -42,7 +42,7 @@ public abstract class DomainBase
/// <summary>
/// Gets whether the domain is disposed.
/// </summary>
protected bool IsDisposed => _disposed;
protected bool IsDisposed => Volatile.Read(ref _disposed) != 0;

/// <summary>
/// Makes the domain.
Expand All @@ -66,25 +66,34 @@ protected DomainBase(DomainMount mount, IFileSystem fileSystem, DomainCapabiliti
/// <summary>
/// Makes the exception for what the domain cannot do.
/// </summary>
protected NotSupportedException Unsupported(string what) =>
new($"The domain '{Name}' ({Scheme}) cannot {what}");
protected NotSupportedException Unsupported(string what)
{
return new NotSupportedException($"The domain '{Name}' ({Scheme}) cannot {what}");
}

/// <summary>
/// Makes the exception for a file that is not there.
/// </summary>
protected FileNotFoundException NotFound(string path) =>
new($"No file '{PathOf(path)}'", PathOf(path).ToString());
protected FileNotFoundException NotFound(string path)
{
return new FileNotFoundException($"No file '{PathOf(path)}'", PathOf(path).ToString());
}

/// <summary>
/// Makes the exception for a directory that is not there.
/// </summary>
protected DirectoryNotFoundException DirectoryNotFound(string path) =>
new($"No directory '{PathOf(path)}'");
protected DirectoryNotFoundException DirectoryNotFound(string path)
{
return new DirectoryNotFoundException($"No directory '{PathOf(path)}'");
}

/// <summary>
/// Throws if the domain is disposed.
/// </summary>
protected void ThrowIfDisposed() => ObjectDisposedException.ThrowIf(_disposed, this);
protected void ThrowIfDisposed()
{
ObjectDisposedException.ThrowIf(IsDisposed, this);
}

private void Require(DomainCapabilities capability, string what)
{
Expand Down Expand Up @@ -112,19 +121,28 @@ private void RequireFor(FileOpenOptions options)
protected abstract bool TryGetEntryCore(string path, out FileEntry entry);

/// <summary>The entries of a directory.</summary>
protected virtual IEnumerable<FileEntry> EnumerateCore(string directory, string pattern, bool recursive) =>
protected virtual IEnumerable<FileEntry> EnumerateCore(string directory, string pattern, bool recursive)
{
throw Unsupported("list files");
}

/// <summary>Opens a file; the options are checked against the capabilities.</summary>
protected abstract Stream OpenCore(string path, FileOpenOptions options);

/// <summary>Opens a file without blocking; by default, <see cref="OpenCore"/>.</summary>
protected virtual ValueTask<Stream> OpenAsyncCore(string path, FileOpenOptions options,
CancellationToken cancellationToken) =>
new(OpenCore(path, options));
protected virtual ValueTask<Stream> OpenAsyncCore(
string path, FileOpenOptions options,
CancellationToken cancellationToken
)
{
return new ValueTask<Stream>(OpenCore(path, options));
}

/// <summary>Opens a file for reads at offsets.</summary>
protected virtual IRandomAccessFile OpenRandomAccessCore(string path) => throw Unsupported("read at offsets");
protected virtual IRandomAccessFile OpenRandomAccessCore(string path)
{
throw Unsupported("read at offsets");
}

/// <summary>The bytes of a file in memory.</summary>
protected virtual bool TryGetMemoryCore(string path, out ReadOnlyMemory<byte> data)
Expand All @@ -134,17 +152,28 @@ protected virtual bool TryGetMemoryCore(string path, out ReadOnlyMemory<byte> da
}

/// <summary>Creates a directory.</summary>
protected virtual void CreateDirectoryCore(string path) => throw Unsupported("create directories");
protected virtual void CreateDirectoryCore(string path)
{
throw Unsupported("create directories");
}

/// <summary>Deletes a file or a directory, never the root.</summary>
protected virtual bool DeleteCore(string path, bool recursive) => throw Unsupported("delete");
protected virtual bool DeleteCore(string path, bool recursive)
{
throw Unsupported("delete");
}

/// <summary>Moves a file or a directory; neither is the root, and the destination is not inside the source.</summary>
protected virtual void MoveCore(string source, string destination, bool overwrite) => throw Unsupported("move");
protected virtual void MoveCore(string source, string destination, bool overwrite)
{
throw Unsupported("move");
}

/// <summary>Watches a directory.</summary>
protected virtual IDisposable WatchCore(string directory, bool recursive, Action<FileChange> handler) =>
protected virtual IDisposable WatchCore(string directory, bool recursive, Action<FileChange> handler)
{
throw Unsupported("watch");
}

/// <summary>
/// Releases what the domain holds: called once.
Expand All @@ -163,9 +192,9 @@ protected virtual void DisposeCore()
protected static bool GetFlag(IReadOnlyDictionary<string, string>? options, string key, bool fallback)
{
if (options is null
|| !options.TryGetValue(key, out var text))
|| !options.TryGetValue(key, out string? text))
return fallback;
return bool.TryParse(text, out var value)
return bool.TryParse(text, out bool value)
? value
: throw new ArgumentException($"The setting '{key}' is '{text}': expected true or false");
}
Expand All @@ -175,16 +204,21 @@ protected static bool GetFlag(IReadOnlyDictionary<string, string>? options, stri
/// </summary>
/// <param name="path">The path inside the domain.</param>
/// <returns>The full path.</returns>
protected FilePath PathOf(string path) => FilePath.Create(Name, path);
protected FilePath PathOf(string path)
{
return FilePath.Create(Name, path);
}

/// <summary>
/// Makes the entry of a file, its content type from the file system.
/// </summary>
protected FileEntry FileEntryOf(string path, long length,
protected FileEntry FileEntryOf(
string path, long length,
DateTime? creationTimeUtc, DateTime? lastWriteTimeUtc,
long revision)
long revision
)
{
var full = PathOf(path);
FilePath full = PathOf(path);
return new FileEntry(full, FileEntryKind.File, length,
creationTimeUtc, lastWriteTimeUtc,
FileSystem.ContentTypes.Resolve(full), revision);
Expand All @@ -193,10 +227,12 @@ protected FileEntry FileEntryOf(string path, long length,
/// <summary>
/// Makes the entry of a directory.
/// </summary>
protected FileEntry DirectoryEntryOf(string path,
DateTime? creationTimeUtc = null, DateTime? lastWriteTimeUtc = null)
protected FileEntry DirectoryEntryOf(
string path,
DateTime? creationTimeUtc = null, DateTime? lastWriteTimeUtc = null
)
{
var full = PathOf(path);
FilePath full = PathOf(path);
return new FileEntry(full, FileEntryKind.Directory, 0,
creationTimeUtc, lastWriteTimeUtc,
default, 0);
Expand Down Expand Up @@ -234,8 +270,10 @@ public Stream Open(string path, FileOpenOptions options)
}

/// <inheritdoc/>
public ValueTask<Stream> OpenAsync(string path, FileOpenOptions options,
CancellationToken cancellationToken = default)
public ValueTask<Stream> OpenAsync(
string path, FileOpenOptions options,
CancellationToken cancellationToken = default
)
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
Expand Down Expand Up @@ -273,7 +311,7 @@ public bool Delete(string path, bool recursive = false)
{
ThrowIfDisposed();
Require(DomainCapabilities.Write, "delete");
var normalized = FileSystemNames.NormalizePath(path);
string normalized = FileSystemNames.NormalizePath(path);
if (normalized.Length == 0) throw new IOException($"The root of the domain '{Name}' cannot be deleted");
return DeleteCore(normalized, recursive);
}
Expand All @@ -283,8 +321,8 @@ public void Move(string source, string destination, bool overwrite = false)
{
ThrowIfDisposed();
Require(DomainCapabilities.Write, "move");
var from = FileSystemNames.NormalizePath(source, nameof(source));
var to = FileSystemNames.NormalizePath(destination, nameof(destination));
string from = FileSystemNames.NormalizePath(source, nameof(source));
string to = FileSystemNames.NormalizePath(destination, nameof(destination));
if (from.Length == 0 || to.Length == 0)
throw new IOException($"The root of the domain '{Name}' cannot be moved");
if (from == to) return;
Expand All @@ -305,12 +343,14 @@ public IDisposable Watch(string directory, bool recursive, Action<FileChange> ha
/// <inheritdoc/>
public void Dispose()
{
if (_disposed) return;
_disposed = true;
if (Interlocked.Exchange(ref _disposed, 1) != 0) return;
DisposeCore();
GC.SuppressFinalize(this);
}

/// <inheritdoc/>
public override string ToString() => $"{Name}:// ({Scheme} {Source})";
public override string ToString()
{
return $"{Name}:// ({Scheme} {Source})";
}
}
Loading