Skip to content

Commit 0447f28

Browse files
authored
Merge pull request #480 from GeneralLibrary/fix/copy-unknown-files-atomic-replace
fix(DiffPipeline): CopyUnknownFiles 使用原子替换避免文件占用冲突
2 parents 829c4a0 + 01faadc commit 0447f28

3 files changed

Lines changed: 16 additions & 16 deletions

File tree

src/c#/GeneralUpdate.Core/Pipeline/DiffPipeline.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,22 @@ private static Task CopyUnknownFiles(string appPath, string patchPath)
507507
if (parentFolder?.Exists == false)
508508
parentFolder.Create();
509509

510-
File.Copy(file.FullName, targetPath, true);
510+
// Atomic replace via temp file, same strategy as ApplyPatch.
511+
// Avoids file-in-use errors when the process just exited.
512+
var safeName = targetFileName.Replace(Path.DirectorySeparatorChar, '_');
513+
var tempPath = Path.Combine(appPath, $"{Path.GetRandomFileName()}_{safeName}");
514+
File.Copy(file.FullName, tempPath, true);
515+
516+
// Use File.Replace for atomic overwrite (handles locked targets gracefully)
517+
if (File.Exists(targetPath))
518+
{
519+
File.SetAttributes(targetPath, FileAttributes.Normal);
520+
File.Replace(tempPath, targetPath, null, true);
521+
}
522+
else
523+
{
524+
File.Move(tempPath, targetPath);
525+
}
511526
}
512527

513528
if (Directory.Exists(patchPath))

tests/ClientTest/ClientTest.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,4 @@
1111
<ProjectReference Include="..\..\src\c#\GeneralUpdate.Core\GeneralUpdate.Core.csproj" />
1212
</ItemGroup>
1313

14-
<ItemGroup>
15-
<None Update="generalupdate.manifest.json">
16-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
17-
</None>
18-
</ItemGroup>
19-
2014
</Project>

tests/ClientTest/generalupdate.manifest.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)