Skip to content

Commit

Permalink
Add SwitchTo filename overload
Browse files Browse the repository at this point in the history
Fixes: #237
  • Loading branch information
jeremy-visionaid committed Nov 20, 2024
1 parent 532b8b8 commit 622dc29
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
38 changes: 38 additions & 0 deletions OpenMcdf.Tests/RootStorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,42 @@ public void SwitchStream(Version version, int subStorageCount)
rootStorage.OpenStorage($"Test{i}");
}
}

[TestMethod]
[DataRow(Version.V3, 0)]
[DataRow(Version.V3, 1)]
[DataRow(Version.V3, 2)]
[DataRow(Version.V3, 4)] // Required 2 sectors including root
[DataRow(Version.V4, 0)]
[DataRow(Version.V4, 1)]
[DataRow(Version.V4, 2)]
[DataRow(Version.V4, 32)] // Required 2 sectors including root
public void SwitchFile(Version version, int subStorageCount)
{
string fileName = Path.GetTempFileName();

try
{
using (var rootStorage = RootStorage.CreateInMemory(version))
{
for (int i = 0; i < subStorageCount; i++)
rootStorage.CreateStorage($"Test{i}");

rootStorage.SwitchTo(fileName);
}

using (var rootStorage = RootStorage.OpenRead(fileName))
{
IEnumerable<EntryInfo> entries = rootStorage.EnumerateEntries();
Assert.AreEqual(subStorageCount, entries.Count());

for (int i = 0; i < subStorageCount; i++)
rootStorage.OpenStorage($"Test{i}");
}
}
finally
{
File.Delete(fileName);
}
}
}
15 changes: 14 additions & 1 deletion OpenMcdf/RootStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void Revert()
Context.Revert();
}

public void SwitchTo(Stream stream)
private void SwitchToCore(Stream stream, bool allowLeaveOpen)
{
Flush();

Expand All @@ -173,9 +173,22 @@ public void SwitchTo(Stream stream)
Context.Dispose();

IOContextFlags contextFlags = ToIOContextFlags(storageModeFlags);
if (!allowLeaveOpen)
contextFlags &= ~IOContextFlags.LeaveOpen;
_ = new RootContext(ContextSite, stream, Version.Unknown, contextFlags);
}

public void SwitchTo(Stream stream)
{
SwitchToCore(stream, true);
}

public void SwitchTo(string fileName)
{
FileStream stream = File.Create(fileName);
SwitchToCore(stream, false);
}

[ExcludeFromCodeCoverage]
internal void Trace(TextWriter writer)
{
Expand Down

0 comments on commit 622dc29

Please sign in to comment.