Skip to content

Commit

Permalink
add two fallbacks to CoreRun copying (#2073)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik authored Aug 17, 2022
1 parent a5b0e9a commit cab43e1
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/BenchmarkDotNet/Toolchains/CoreRun/CoreRunToolchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,44 @@ public bool IsSupported(BenchmarkCase benchmark, ILogger logger, IResolver resol
}

private static FileInfo GetShadowCopyPath(FileInfo coreRunPath)
=> coreRunPath.Directory.Parent != null
? new FileInfo(Path.Combine(coreRunPath.Directory.Parent.FullName, Guid.NewGuid().ToString(), coreRunPath.Name))
: new FileInfo(Path.Combine(coreRunPath.Directory.FullName, Guid.NewGuid().ToString(), coreRunPath.Name)); // C:\CoreRun.exe case
{
string randomSubfolderName = Guid.NewGuid().ToString();

FileInfo coreRunCopy = coreRunPath.Directory.Parent != null
? new FileInfo(Path.Combine(coreRunPath.Directory.Parent.FullName, randomSubfolderName, coreRunPath.Name))
: new FileInfo(Path.Combine(coreRunPath.Directory.FullName, randomSubfolderName, coreRunPath.Name)); // C:\CoreRun.exe case

if (!TryToCreateSubfolder(coreRunCopy.Directory))
{
// we are most likely missing permissions to write to given folder (it can be readonly etc)
// in such case, CoreRun copy is going to be stored in TEMP
coreRunCopy = new FileInfo(Path.Combine(Path.GetTempPath(), randomSubfolderName, coreRunPath.Name));

if (!TryToCreateSubfolder(coreRunCopy.Directory))
{
// if even that is impossible, we return the original path and nothing is going to be copied
return coreRunPath;
}
}

return coreRunCopy;

static bool TryToCreateSubfolder(DirectoryInfo directory)
{
try
{
if (!directory.Exists)
{
directory.Create();
}

return true;
}
catch
{
return false;
}
}
}
}
}

0 comments on commit cab43e1

Please sign in to comment.