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

Fix consolidating transacted streams #255

Merged
merged 2 commits into from
Nov 29, 2024
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 Directory.csproj.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<!-- Packaging -->
<PropertyGroup>
<Version>3.0.0.0</Version>
<PackageVersion>$(Version)-preview.3</PackageVersion>
<PackageVersion>$(Version)-preview.4</PackageVersion>
<Authors>ironfede,jeremy-visionaid</Authors>
<Copyright>Copyright © 2010-2024, Federico Blaseotto, Jeremy Powell</Copyright>
<PackageProjectUrl>https://github.com/ironfede/openmcdf</PackageProjectUrl>
Expand Down
77 changes: 77 additions & 0 deletions OpenMcdf.Tests/RootStorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,83 @@ public void Open(string fileName)
Assert.ThrowsException<NotSupportedException>(() => rootStorage.StateBits = 0);
}

[TestMethod]
[DataRow(Version.V3)]
[DataRow(Version.V4)]
public void ConsolidateMemoryStream(Version version)
{
byte[] buffer = new byte[4096];

using MemoryStream memoryStream = new();
using (var rootStorage = RootStorage.Create(memoryStream, version, StorageModeFlags.LeaveOpen))
{
using (CfbStream stream = rootStorage.CreateStream("Test"))
stream.Write(buffer, 0, buffer.Length);

Assert.AreEqual(1, rootStorage.EnumerateEntries().Count());

rootStorage.Flush(true);

int originalMemoryStreamLength = (int)memoryStream.Length;

rootStorage.Delete("Test");

rootStorage.Flush(true);

Assert.IsTrue(originalMemoryStreamLength > memoryStream.Length);
}

using (var rootStorage = RootStorage.Create(memoryStream, version, StorageModeFlags.LeaveOpen))
{
Assert.AreEqual(0, rootStorage.EnumerateEntries().Count());
}
}

[TestMethod]
[DataRow(Version.V3, StorageModeFlags.None)]
[DataRow(Version.V4, StorageModeFlags.Transacted)]
public void ConsolidateFile(Version version, StorageModeFlags flags)
{
byte[] buffer = new byte[4096];

string fileName = Path.GetTempFileName();

try
{
using (var rootStorage = RootStorage.Create(fileName, version, flags))
{
using (CfbStream stream = rootStorage.CreateStream("Test"))
stream.Write(buffer, 0, buffer.Length);

Assert.AreEqual(1, rootStorage.EnumerateEntries().Count());

if (flags.HasFlag(StorageModeFlags.Transacted))
rootStorage.Commit();
rootStorage.Flush(true);

long originalLength = new FileInfo(fileName).Length;

rootStorage.Delete("Test");

if (flags.HasFlag(StorageModeFlags.Transacted))
rootStorage.Commit();
rootStorage.Flush(true);

long consolidatedLength = new FileInfo(fileName).Length;
Assert.IsTrue(originalLength > consolidatedLength);
}

using (var rootStorage = RootStorage.OpenRead(fileName))
{
Assert.AreEqual(0, rootStorage.EnumerateEntries().Count());
}
}
finally
{
File.Delete(fileName);
}
}

[TestMethod]
[DataRow(Version.V3, 0)]
[DataRow(Version.V3, 1)]
Expand Down
73 changes: 0 additions & 73 deletions OpenMcdf.Tests/StorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,79 +274,6 @@ public void DeleteStream(Version version)
}
}

[TestMethod]
[DataRow(Version.V3)]
[DataRow(Version.V4)]
public void ConsolidateMemoryStream(Version version)
{
byte[] buffer = new byte[4096];

using MemoryStream memoryStream = new();
using (var rootStorage = RootStorage.Create(memoryStream, version, StorageModeFlags.LeaveOpen))
{
using (CfbStream stream = rootStorage.CreateStream("Test"))
stream.Write(buffer, 0, buffer.Length);

Assert.AreEqual(1, rootStorage.EnumerateEntries().Count());

rootStorage.Flush(true);

int originalMemoryStreamLength = (int)memoryStream.Length;

rootStorage.Delete("Test");

rootStorage.Flush(true);

Assert.IsTrue(originalMemoryStreamLength > memoryStream.Length);
}

using (var rootStorage = RootStorage.Create(memoryStream, version, StorageModeFlags.LeaveOpen))
{
Assert.AreEqual(0, rootStorage.EnumerateEntries().Count());
}
}

[TestMethod]
[DataRow(Version.V3)]
[DataRow(Version.V4)]
public void ConsolidateFile(Version version)
{
byte[] buffer = new byte[4096];

string fileName = Path.GetTempFileName();

try
{
using (var rootStorage = RootStorage.Create(fileName, version))
{
using (CfbStream stream = rootStorage.CreateStream("Test"))
stream.Write(buffer, 0, buffer.Length);

Assert.AreEqual(1, rootStorage.EnumerateEntries().Count());

rootStorage.Flush(true);

long originalLength = new FileInfo(fileName).Length;

rootStorage.Delete("Test");

rootStorage.Flush(true);

long consolidatedLength = new FileInfo(fileName).Length;
Assert.IsTrue(originalLength > consolidatedLength);
}

using (var rootStorage = RootStorage.OpenRead(fileName))
{
Assert.AreEqual(0, rootStorage.EnumerateEntries().Count());
}
}
finally
{
File.Delete(fileName);
}
}

[TestMethod]
[DataRow(Version.V3)]
[DataRow(Version.V4)]
Expand Down
7 changes: 1 addition & 6 deletions OpenMcdf/RootContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OpenMcdf;

[Flags]
enum IOContextFlags
{
None = 0,
Expand Down Expand Up @@ -188,12 +189,6 @@ public void ExtendStreamLength(long length)
isDirty = true;
}

public void Consolidate(long length)
{
BaseStream.SetLength(length);
Length = length;
}

public void WriteHeader()
{
CfbBinaryWriter writer = Writer;
Expand Down
16 changes: 8 additions & 8 deletions OpenMcdf/RootStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,25 +154,25 @@ void Consolidate()
{
// TODO: Consolidate by defragmentation instead of copy

Stream baseStream = Context.BaseStream;
Stream? destinationStream = null;

try
{
if (Context.BaseStream is MemoryStream)
destinationStream = new MemoryStream((int)Context.BaseStream.Length);
else if (Context.BaseStream is FileStream)
if (baseStream is MemoryStream)
destinationStream = new MemoryStream((int)baseStream.Length);
else if (baseStream is FileStream)
destinationStream = File.Create(Path.GetTempFileName());
else
throw new NotSupportedException("Unsupported stream type for consolidation.");

using (RootStorage destinationStorage = Create(destinationStream, Context.Version, storageModeFlags | StorageModeFlags.LeaveOpen))
using (RootStorage destinationStorage = Create(destinationStream, Context.Version, StorageModeFlags.LeaveOpen))
CopyTo(destinationStorage);

Context.BaseStream.Position = 0;
destinationStream.Position = 0;
destinationStream.CopyAllTo(baseStream);

destinationStream.CopyTo(Context.BaseStream);
Context.Consolidate(destinationStream.Length);
IOContextFlags contextFlags = ToIOContextFlags(storageModeFlags);
_ = new RootContext(ContextSite, baseStream, Version.Unknown, contextFlags);
}
catch
{
Expand Down