Skip to content

Commit 40ecf31

Browse files
Test consolidation with files
1 parent 13a3c05 commit 40ecf31

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

OpenMcdf.Tests/StorageTests.cs

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,16 +277,13 @@ public void DeleteStream(Version version)
277277
[TestMethod]
278278
[DataRow(Version.V3)]
279279
[DataRow(Version.V4)]
280-
public void Consolidate(Version version)
280+
public void ConsolidateMemoryStream(Version version)
281281
{
282282
byte[] buffer = new byte[4096];
283283

284-
string fileName = Path.GetTempFileName();
285-
286-
try
284+
using MemoryStream memoryStream = new();
285+
using (var rootStorage = RootStorage.Create(memoryStream, version, StorageModeFlags.LeaveOpen))
287286
{
288-
using MemoryStream memoryStream = new();
289-
using var rootStorage = RootStorage.Create(memoryStream, version, StorageModeFlags.LeaveOpen);
290287
using (CfbStream stream = rootStorage.CreateStream("Test"))
291288
stream.Write(buffer, 0, buffer.Length);
292289

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

303300
Assert.IsTrue(originalMemoryStreamLength > memoryStream.Length);
304301
}
302+
303+
using (var rootStorage = RootStorage.Create(memoryStream, version, StorageModeFlags.LeaveOpen))
304+
{
305+
Assert.AreEqual(0, rootStorage.EnumerateEntries().Count());
306+
}
307+
}
308+
309+
[TestMethod]
310+
[DataRow(Version.V3)]
311+
[DataRow(Version.V4)]
312+
public void ConsolidateFile(Version version)
313+
{
314+
byte[] buffer = new byte[4096];
315+
316+
string fileName = Path.GetTempFileName();
317+
318+
try
319+
{
320+
using (var rootStorage = RootStorage.Create(fileName, version))
321+
{
322+
using (CfbStream stream = rootStorage.CreateStream("Test"))
323+
stream.Write(buffer, 0, buffer.Length);
324+
325+
Assert.AreEqual(1, rootStorage.EnumerateEntries().Count());
326+
327+
rootStorage.Flush(true);
328+
329+
long originalLength = new FileInfo(fileName).Length;
330+
331+
rootStorage.Delete("Test");
332+
333+
rootStorage.Flush(true);
334+
335+
long consolidatedLength = new FileInfo(fileName).Length;
336+
Assert.IsTrue(originalLength > consolidatedLength);
337+
}
338+
339+
using (var rootStorage = RootStorage.OpenRead(fileName))
340+
{
341+
Assert.AreEqual(0, rootStorage.EnumerateEntries().Count());
342+
}
343+
}
305344
finally
306345
{
307346
File.Delete(fileName);

OpenMcdf/RootContext.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ public void ExtendStreamLength(long length)
188188
isDirty = true;
189189
}
190190

191+
public void Consolidate(long length)
192+
{
193+
BaseStream.SetLength(length);
194+
Length = length;
195+
}
196+
191197
public void WriteHeader()
192198
{
193199
CfbBinaryWriter writer = Writer;

OpenMcdf/RootStorage.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,23 +165,26 @@ void Consolidate()
165165
else
166166
throw new NotSupportedException("Unsupported stream type for consolidation.");
167167

168-
using (RootStorage destinationStorage = Create(destinationStream, Context.Version, storageModeFlags))
168+
using (RootStorage destinationStorage = Create(destinationStream, Context.Version, storageModeFlags | StorageModeFlags.LeaveOpen))
169169
CopyTo(destinationStorage);
170170

171171
Context.BaseStream.Position = 0;
172172
destinationStream.Position = 0;
173173

174174
destinationStream.CopyTo(Context.BaseStream);
175-
Context.BaseStream.SetLength(destinationStream.Length);
175+
Context.Consolidate(destinationStream.Length);
176176
}
177177
catch
178178
{
179+
destinationStream?.Dispose();
180+
179181
if (destinationStream is FileStream fs)
180182
{
181183
string fileName = fs.Name;
182-
fs.Dispose();
183184
File.Delete(fileName);
184185
}
186+
187+
throw;
185188
}
186189
}
187190

0 commit comments

Comments
 (0)