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
2 changes: 1 addition & 1 deletion src/Filee.Engines/Archives/ArReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void Extract(string archive, string folder, CancellationToken canc
var members = List(stream, name);
ArchiveSafety.CheckListing([.. members.Select(m => new ArchiveItem(m.Name, false, m.Size))], name, stream.Length, folder);

var writer = new EntryWriter(folder, new UnpackBudget(name, stream.Length, folder), cancellationToken);
using var writer = new EntryWriter(folder, new UnpackBudget(name, stream.Length, folder), cancellationToken);
foreach (var member in members)
{
cancellationToken.ThrowIfCancellationRequested();
Expand Down
41 changes: 26 additions & 15 deletions src/Filee.Engines/Archives/ArchiveFiles.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// File system helpers for the archive engine: copying with a size cap, unique names, and moving an unpacked folder
// into place (with the "one top folder is not nested twice" rule of the Extract preset).

using System.Buffers;

namespace Filee.Engines.Archives;

/// <summary>Copy, naming and folder-moving helpers.</summary>
Expand All @@ -12,20 +14,28 @@ internal static class ArchiveFiles
public static long CopyLimited(Stream from, Stream to, long maxBytes, CancellationToken cancellationToken,
Action<long>? progress = null, UnpackBudget? budget = null, Func<Exception>? tooLarge = null)
{
var buffer = new byte[1 << 16];
long total = 0;
int read;
while ((read = from.Read(buffer, 0, buffer.Length)) > 0)
// Rented: archives of many small files call this once per file.
var buffer = ArrayPool<byte>.Shared.Rent(1 << 16);
try
{
long total = 0;
int read;
while ((read = from.Read(buffer, 0, 1 << 16)) > 0)
{
cancellationToken.ThrowIfCancellationRequested();
total += read;
if (total > maxBytes)
throw tooLarge?.Invoke() ?? new InvalidDataException("the data unpacks to more than the archive declares");
budget?.Add(read);
to.Write(buffer, 0, read);
progress?.Invoke(read);
}
return total;
}
finally
{
cancellationToken.ThrowIfCancellationRequested();
total += read;
if (total > maxBytes)
throw tooLarge?.Invoke() ?? new InvalidDataException("the data unpacks to more than the archive declares");
budget?.Add(read);
to.Write(buffer, 0, read);
progress?.Invoke(read);
ArrayPool<byte>.Shared.Return(buffer);
}
return total;
}

/// <summary>True when nothing is at <paramref name="path"/>.</summary>
Expand Down Expand Up @@ -144,17 +154,18 @@ internal sealed class UniquePaths
/// <summary>Path → the next number to try for it.</summary>
private readonly Dictionary<string, int> _next = new(StringComparer.OrdinalIgnoreCase);

public string Get(string path)
/// <param name="taken">Paths handed out already that may not be on disk yet (files still being written).</param>
public string Get(string path, IReadOnlySet<string>? taken = null)
{
if (ArchiveFiles.IsFree(path))
if (taken?.Contains(path) != true && ArchiveFiles.IsFree(path))
return path;
var dir = Path.GetDirectoryName(path)!;
var stem = Path.GetFileNameWithoutExtension(path);
var ext = Path.GetExtension(path);
for (var n = _next.GetValueOrDefault(path, 1); ; n++)
{
var candidate = Path.Combine(dir, $"{stem}_{n}{ext}");
if (ArchiveFiles.IsFree(candidate))
if (taken?.Contains(candidate) != true && ArchiveFiles.IsFree(candidate))
{
_next[path] = n + 1;
return candidate;
Expand Down
2 changes: 1 addition & 1 deletion src/Filee.Engines/Archives/ArjReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void Extract(string archive, string folder, CancellationToken canc
if (entries.FirstOrDefault(e => !e.IsFolder && e.Method > 4) is { } unknown)
throw new NotSupportedException($"the ARJ method {unknown.Method}");

var writer = new EntryWriter(folder, new UnpackBudget(name, stream.Length, folder), cancellationToken);
using var writer = new EntryWriter(folder, new UnpackBudget(name, stream.Length, folder), cancellationToken);
foreach (var entry in entries)
{
cancellationToken.ThrowIfCancellationRequested();
Expand Down
Loading
Loading