Skip to content

Commit

Permalink
Test consolidation with files
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-visionaid committed Nov 28, 2024
1 parent 13a3c05 commit 40ecf31
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
51 changes: 45 additions & 6 deletions OpenMcdf.Tests/StorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,13 @@ public void DeleteStream(Version version)
[TestMethod]
[DataRow(Version.V3)]
[DataRow(Version.V4)]
public void Consolidate(Version version)
public void ConsolidateMemoryStream(Version version)
{
byte[] buffer = new byte[4096];

string fileName = Path.GetTempFileName();

try
using MemoryStream memoryStream = new();
using (var rootStorage = RootStorage.Create(memoryStream, version, StorageModeFlags.LeaveOpen))
{
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);

Expand All @@ -302,6 +299,48 @@ public void Consolidate(Version version)

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);
Expand Down
6 changes: 6 additions & 0 deletions OpenMcdf/RootContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ 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
9 changes: 6 additions & 3 deletions OpenMcdf/RootStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,23 +165,26 @@ void Consolidate()
else
throw new NotSupportedException("Unsupported stream type for consolidation.");

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

Context.BaseStream.Position = 0;
destinationStream.Position = 0;

destinationStream.CopyTo(Context.BaseStream);
Context.BaseStream.SetLength(destinationStream.Length);
Context.Consolidate(destinationStream.Length);
}
catch
{
destinationStream?.Dispose();

if (destinationStream is FileStream fs)
{
string fileName = fs.Name;
fs.Dispose();
File.Delete(fileName);
}

throw;
}
}

Expand Down

0 comments on commit 40ecf31

Please sign in to comment.