Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf856dc9

Browse files
Numpsyjeremy-visionaid
authored andcommitted
Trim non-transacted streams on flush
1 parentcc31d17 commitf856dc9

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

‎OpenMcdf.Tests/RootStorageTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,28 @@ public void SwitchTransactedStream(Version version, int subStorageCount)
207207
rootStorage.OpenStorage($"Test{i}");
208208
}
209209
}
210+
211+
[TestMethod]
212+
[DataRow(false)]
213+
[DataRow(true)]
214+
publicvoidDeleteTrimsBaseStream(boolconsolidate)
215+
{
216+
usingvarrootStorage=RootStorage.CreateInMemory(Version.V3);
217+
using(CfbStreamstream=rootStorage.CreateStream("Test"))
218+
{
219+
byte[]buffer=TestData.CreateByteArray(4096);
220+
stream.Write(buffer,0,buffer.Length);
221+
}
222+
223+
rootStorage.Flush(consolidate);
224+
225+
longoriginalLength=rootStorage.BaseStream.Length;
226+
227+
rootStorage.Delete("Test");
228+
rootStorage.Flush(consolidate);
229+
230+
longnewLength=rootStorage.BaseStream.Length;
231+
232+
Assert.IsTrue(originalLength>newLength);
233+
}
210234
}

‎OpenMcdf/Fat.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ public uint Add(FatEnumerator fatEnumerator, uint startIndex)
141141
returnentry.Index;
142142
}
143143

144+
publicSectorGetLastUsedSector()
145+
{
146+
uintlastUsedSectorIndex=uint.MaxValue;
147+
foreach(FatEntryentryinthis)
148+
{
149+
if(!entry.IsFree)
150+
lastUsedSectorIndex=entry.Index;
151+
}
152+
153+
returnnew(lastUsedSectorIndex,Context.SectorSize);
154+
}
155+
144156
publicIEnumerator<FatEntry>GetEnumerator()=>newFatEnumerator(Context.Fat);
145157

146158
IEnumeratorIEnumerable.GetEnumerator()=>GetEnumerator();

‎OpenMcdf/RootContext.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ public FatStream MiniStream
8787

8888
publicuintSectorCount=>(uint)Math.Max(0,(Length-SectorSize)/SectorSize);// TODO: Check
8989

90-
boolisDirty;
91-
9290
publicRootContext(RootContextSiterootContextSite,Streamstream,Versionversion,IOContextFlagscontextFlags=IOContextFlags.None)
9391
:base(rootContextSite)
9492
{
@@ -178,20 +176,27 @@ public void Flush()
178176
{
179177
Fat.Flush();
180178

181-
if(isDirty&&writeris notnull&&transactedStreamisnull)
179+
if(writeris notnull&&transactedStreamisnull)
182180
{
183-
// Ensure the stream is as long as expected
184-
BaseStream.SetLength(Length);
181+
TrimBaseStream();
185182
WriteHeader();
186-
isDirty=false;
187183
}
188184
}
189185

190186
publicvoidExtendStreamLength(longlength)
191187
{
192188
if(Length<length)
193189
Length=length;
194-
isDirty=true;
190+
}
191+
192+
voidTrimBaseStream()
193+
{
194+
SectorlastUsedSector=Fat.GetLastUsedSector();
195+
if(!lastUsedSector.IsValid)
196+
thrownewFileFormatException("Last used sector is invalid");
197+
198+
Length=lastUsedSector.EndPosition;
199+
BaseStream.SetLength(Length);
195200
}
196201

197202
publicvoidWriteHeader()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp