Skip to content

Commit 6edbb45

Browse files
CopilotArlodotexe
andcommitted
Refactor ShouldDispose to be a settable property instead of constructor parameter
Co-authored-by: Arlodotexe <9384894+Arlodotexe@users.noreply.github.com>
1 parent 99eb44b commit 6edbb45

14 files changed

+62
-69
lines changed

src/Extensions/CreateRelativeStorageExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public static async Task<IChildFile> CreateFileByRelativePathAsync(this IChildFi
240240

241241
throw new InvalidOperationException("Resolved item is not a file.");
242242
}
243-
243+
244244
/// <summary>
245245
/// Traverses/creates folders along a relative path and yields each folder in order as it is visited/created.
246246
/// Supports "." and ".." segments. If the last segment looks like a file (no trailing slash and contains '.'),

src/Extensions/FileOpenExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ public static partial class FileExtensions
1616
/// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param>
1717
/// <returns>A task containing the requested stream.</returns>
1818
public static Task<Stream> OpenReadAsync(this IFile file, CancellationToken cancellationToken = default) => file.OpenStreamAsync(FileAccess.Read, cancellationToken);
19-
19+
2020
/// <summary>
2121
/// Opens the file for writing.
2222
/// </summary>
2323
/// <param name="file">The file to open.</param>
2424
/// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param>
2525
/// <returns>A task containing the requested stream.</returns>
2626
public static Task<Stream> OpenWriteAsync(this IFile file, CancellationToken cancellationToken = default) => file.OpenStreamAsync(FileAccess.Write, cancellationToken);
27-
27+
2828
/// <summary>
2929
/// Opens the file for reading and writing.
3030
/// </summary>

src/Extensions/GetItemByRelativePathExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static async IAsyncEnumerable<IStorable> GetItemsAlongRelativePathAsync(t
9696
var normalized = (relativePath ?? string.Empty).Replace('\\', '/');
9797
// Split path into parts (use API available on target framework)
9898
#if NETSTANDARD2_0
99-
var parts = normalized.Split(['/'], StringSplitOptions.RemoveEmptyEntries);
99+
var parts = normalized.Split(['/'], StringSplitOptions.RemoveEmptyEntries);
100100
#else
101101
var parts = normalized.Split('/', StringSplitOptions.RemoveEmptyEntries);
102102
#endif

src/Extensions/GetRelativePathToExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static async Task<string> GetRelativePathToAsync(this IFolder from, IStor
2121
{
2222
to.Name,
2323
};
24-
24+
2525
cancellationToken.ThrowIfCancellationRequested();
2626
await RecursiveAddParentToPathAsync(to);
2727

src/IModifiableFolder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace OwlCore.Storage;
77
/// <summary>
88
/// Represents a folder that can be modified.
99
/// </summary>
10-
public interface IModifiableFolder : IMutableFolder
10+
public interface IModifiableFolder : IMutableFolder
1111
{
1212
/// <summary>
1313
/// Deletes the provided storable item from this folder.
@@ -26,7 +26,7 @@ public interface IModifiableFolder : IMutableFolder
2626
/// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param>
2727
/// <returns>The newly created (or opened if existing) folder.</returns>
2828
Task<IChildFolder> CreateFolderAsync(string name, bool overwrite = default, CancellationToken cancellationToken = default);
29-
29+
3030
/// <summary>
3131
/// Creates a new file with the desired name inside this folder.
3232
/// </summary>

src/IStorable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ public interface IStorable
2222
/// <summary>
2323
/// Gets the name of the item, with the extension (if any).
2424
/// </summary>
25-
string Name { get; }
25+
string Name { get; }
2626
}

src/IStorableChild.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ public interface IStorableChild : IStorable
1313
/// </summary>
1414
/// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param>
1515
/// <returns>The containing parent folder, if any.</returns>
16-
Task<IFolder?> GetParentAsync(CancellationToken cancellationToken = default);
16+
Task<IFolder?> GetParentAsync(CancellationToken cancellationToken = default);
1717
}

src/Memory/NonDisposableStreamWrapper.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ public NonDisposableStreamWrapper(Stream stream)
1919
{
2020
_stream = stream;
2121
}
22-
22+
2323
/// <inheritdoc />
2424
protected override void Dispose(bool disposing)
2525
{
2626
}
27-
27+
2828
/// <inheritdoc />
2929
public override void Flush() => _stream.Flush();
3030

@@ -50,16 +50,16 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
5050

5151
/// <inheritdoc />
5252
public override bool CanRead => _stream.CanRead;
53-
53+
5454
/// <inheritdoc />
5555
public override bool CanSeek => _stream.CanSeek;
56-
56+
5757
/// <inheritdoc />
5858
public override bool CanWrite => _stream.CanWrite;
59-
59+
6060
/// <inheritdoc />
6161
public override long Length => _stream.Length;
62-
62+
6363
/// <inheritdoc />
6464
public override long Position
6565
{

src/System/IO/StreamFile.cs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,20 @@ public class StreamFile : IFile
1717
public Stream Stream { get; }
1818

1919
/// <summary>
20-
/// Gets a value indicating whether the underlying stream should be disposed when the returned stream from <see cref="OpenStreamAsync"/> is disposed.
20+
/// Gets or sets a value indicating whether the underlying stream should be disposed when the returned stream from <see cref="OpenStreamAsync"/> is disposed.
2121
/// When true, the underlying stream is returned directly. When false, the stream is wrapped in a non-disposable wrapper.
2222
/// </summary>
23-
public bool ShouldDispose { get; }
23+
public bool ShouldDispose { get; set; }
2424

2525
/// <summary>
2626
/// Creates a new instance of <see cref="StreamFile"/>.
2727
/// </summary>
2828
/// <param name="stream">An existing stream which is provided as the file contents.</param>
2929
public StreamFile(Stream stream)
30-
: this(stream, $"{stream.GetHashCode()}", $"{stream.GetHashCode()}", false)
30+
: this(stream, $"{stream.GetHashCode()}", $"{stream.GetHashCode()}")
3131
{
3232
}
3333

34-
/// <summary>
35-
/// Creates a new instance of <see cref="StreamFile"/>.
36-
/// </summary>
37-
/// <param name="stream">An existing stream which is provided as the file contents.</param>
38-
/// <param name="shouldDispose">When true, the underlying stream will be disposed when the returned stream from <see cref="OpenStreamAsync"/> is disposed. When false, the stream is wrapped in a non-disposable wrapper.</param>
39-
public StreamFile(Stream stream, bool shouldDispose)
40-
: this(stream, $"{stream.GetHashCode()}", $"{stream.GetHashCode()}", shouldDispose)
41-
{
42-
}
4334

4435
/// <summary>
4536
/// Creates a new instance of <see cref="StreamFile"/>.
@@ -48,25 +39,15 @@ public StreamFile(Stream stream, bool shouldDispose)
4839
/// <param name="id">A unique and consistent identifier for this file or folder.</param>
4940
/// <param name="name">The name of the file or folder, with the extension (if any).</param>
5041
public StreamFile(Stream stream, string id, string name)
51-
: this(stream, id, name, false)
52-
{
53-
}
54-
55-
/// <summary>
56-
/// Creates a new instance of <see cref="StreamFile"/>.
57-
/// </summary>
58-
/// <param name="stream">An existing stream which is provided as the file contents.</param>
59-
/// <param name="id">A unique and consistent identifier for this file or folder.</param>
60-
/// <param name="name">The name of the file or folder, with the extension (if any).</param>
61-
/// <param name="shouldDispose">When true, the underlying stream will be disposed when the returned stream from <see cref="OpenStreamAsync"/> is disposed. When false, the stream is wrapped in a non-disposable wrapper.</param>
62-
public StreamFile(Stream stream, string id, string name, bool shouldDispose)
6342
{
6443
Stream = stream;
6544
Id = id;
6645
Name = name;
67-
ShouldDispose = shouldDispose;
46+
ShouldDispose = false; // Default to false for backward compatibility
6847
}
6948

49+
50+
7051
/// <inheritdoc />
7152
public string Id { get; }
7253

src/TruncatedStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class TruncatedStream(Stream Stream, long MaxLength) : Stream, IAsyncDisp
1414
{
1515
// For non-seekable streams, we track how many bytes have been consumed to enforce the window.
1616
private long _consumed;
17-
17+
1818
// For seekable streams, capture the starting offset to define the truncation window.
1919
private readonly long _startOffset = Stream.CanSeek ? Stream.Position : 0;
2020

0 commit comments

Comments
 (0)