Skip to content

Commit

Permalink
Fixed failure copying reference when multiple process accessing.
Browse files Browse the repository at this point in the history
  • Loading branch information
kekyo committed May 8, 2024
1 parent aeab549 commit caa490e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions chibild/chibild.core/CilLinker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ injectToAssemblyPath is { } injectPath ?
Path.GetFileName(module.FileName));
if (tp != module.FileName)
{
File.Copy(
Utilities.SafeCopy(
module.FileName,
tp);

Expand All @@ -524,7 +524,7 @@ injectToAssemblyPath is { } injectPath ?
Path.GetFileNameWithoutExtension(module.FileName) + ext);
if (File.Exists(fp) && fp != tp)
{
File.Copy(fp, tp);
Utilities.SafeCopy(fp, tp);

this.logger.Information(
$"Copied: {Path.GetFileName(fp)}");
Expand Down
52 changes: 52 additions & 0 deletions chibild/chibild.core/Internal/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,56 @@ public static bool UpdateBytes(
}
return false;
}

public static void SafeCopy(string from, string to)
{
// Note that although named 'Safe',
// atomic replacement of files is not realized.

var to1 = Guid.NewGuid().ToString("N");
var to2 = Guid.NewGuid().ToString("N");

try
{
File.Copy(from, to1);
}
catch
{
File.Delete(to1);
throw;
}

var isExistTo = File.Exists(to);
if (isExistTo)
{
try
{
File.Move(to, to2);
}
catch
{
File.Delete(to1);
throw;
}
}

try
{
File.Move(to1, to);
}
catch
{
File.Delete(to1);
if (isExistTo)
{
File.Move(to2, to);
}
throw;
}

if (isExistTo)
{
File.Delete(to2);
}
}
}

0 comments on commit caa490e

Please sign in to comment.