Skip to content

Commit c3d814f

Browse files
committed
Handle locked files during archival of old versions too
1 parent f92cbf9 commit c3d814f

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

AsyncToSyncCodeRoundtripSynchroniserMonitorNet.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<DefineConstants>DEBUG;TRACE</DefineConstants>
2424
<ErrorReport>prompt</ErrorReport>
2525
<WarningLevel>4</WarningLevel>
26-
<NoWarn>SEC0122;S125;S1125;S1135;S1199;S2589;S3881;S3358;S4136;CA1034;CA1063;CCN0011;CCN0021;CCN0031;1701;1702;AsyncFixed01;MS002;MS003;IDE0018;AD0001;SEC0112</NoWarn>
26+
<NoWarn>SEC0122;S125;S1125;S1135;S1199;S2589;S3881;S3358;S4136;S4457;CA1034;CA1063;CCN0011;CCN0021;CCN0031;1701;1702;AsyncFixed01;MS002;MS003;IDE0018;AD0001;SEC0112</NoWarn>
2727
<WarningsAsErrors>NU1605</WarningsAsErrors>
2828
</PropertyGroup>
2929
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
@@ -34,7 +34,7 @@
3434
<DefineConstants>TRACE</DefineConstants>
3535
<ErrorReport>prompt</ErrorReport>
3636
<WarningLevel>4</WarningLevel>
37-
<NoWarn>SEC0122;S125;S1125;S1135;S1199;S2589;S3881;S3358;S4136;CA1034;CA1063;CCN0011;CCN0021;CCN0031;1701;1702;AsyncFixed01;MS002;MS003;IDE0018;AD0001;SEC0112</NoWarn>
37+
<NoWarn>SEC0122;S125;S1125;S1135;S1199;S2589;S3881;S3358;S4136;S4457;CA1034;CA1063;CCN0011;CCN0021;CCN0031;1701;1702;AsyncFixed01;MS002;MS003;IDE0018;AD0001;SEC0112</NoWarn>
3838
<WarningsAsErrors>NU1605</WarningsAsErrors>
3939
<DebugSymbols>true</DebugSymbols>
4040
</PropertyGroup>

Program.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,13 +392,36 @@ public static async Task DeleteFile(string fullName, Context context)
392392
{
393393
try
394394
{
395-
if (File.Exists(fullName + "~"))
395+
while (true)
396+
{
397+
context.Token.ThrowIfCancellationRequested();
398+
399+
try
400+
{
401+
if (File.Exists(fullName + "~"))
402+
{
396403
#pragma warning disable SEC0116 //Warning SEC0116 Unvalidated file paths are passed to a file delete API, which can allow unauthorized file system operations (e.g. read, write, delete) to be performed on unintended server files.
397-
File.Delete(fullName + "~");
404+
File.Delete(fullName + "~");
398405
#pragma warning restore SEC0116
406+
}
399407

400-
if (File.Exists(fullName))
401-
File.Move(fullName, fullName + "~");
408+
if (File.Exists(fullName))
409+
{
410+
File.Move(fullName, fullName + "~");
411+
}
412+
413+
return;
414+
}
415+
catch (IOException)
416+
{
417+
//retry after delay
418+
#if !NOASYNC
419+
await Task.Delay(1000, context.Token); //TODO: config file?
420+
#else
421+
context.Token.WaitHandle.WaitOne(1000);
422+
#endif
423+
}
424+
}
402425
}
403426
catch (Exception ex)
404427
{

0 commit comments

Comments
 (0)