Skip to content

Commit cab43e1

Browse files
authored
add two fallbacks to CoreRun copying (#2073)
1 parent a5b0e9a commit cab43e1

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

src/BenchmarkDotNet/Toolchains/CoreRun/CoreRunToolchain.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,44 @@ public bool IsSupported(BenchmarkCase benchmark, ILogger logger, IResolver resol
7272
}
7373

7474
private static FileInfo GetShadowCopyPath(FileInfo coreRunPath)
75-
=> coreRunPath.Directory.Parent != null
76-
? new FileInfo(Path.Combine(coreRunPath.Directory.Parent.FullName, Guid.NewGuid().ToString(), coreRunPath.Name))
77-
: new FileInfo(Path.Combine(coreRunPath.Directory.FullName, Guid.NewGuid().ToString(), coreRunPath.Name)); // C:\CoreRun.exe case
75+
{
76+
string randomSubfolderName = Guid.NewGuid().ToString();
77+
78+
FileInfo coreRunCopy = coreRunPath.Directory.Parent != null
79+
? new FileInfo(Path.Combine(coreRunPath.Directory.Parent.FullName, randomSubfolderName, coreRunPath.Name))
80+
: new FileInfo(Path.Combine(coreRunPath.Directory.FullName, randomSubfolderName, coreRunPath.Name)); // C:\CoreRun.exe case
81+
82+
if (!TryToCreateSubfolder(coreRunCopy.Directory))
83+
{
84+
// we are most likely missing permissions to write to given folder (it can be readonly etc)
85+
// in such case, CoreRun copy is going to be stored in TEMP
86+
coreRunCopy = new FileInfo(Path.Combine(Path.GetTempPath(), randomSubfolderName, coreRunPath.Name));
87+
88+
if (!TryToCreateSubfolder(coreRunCopy.Directory))
89+
{
90+
// if even that is impossible, we return the original path and nothing is going to be copied
91+
return coreRunPath;
92+
}
93+
}
94+
95+
return coreRunCopy;
96+
97+
static bool TryToCreateSubfolder(DirectoryInfo directory)
98+
{
99+
try
100+
{
101+
if (!directory.Exists)
102+
{
103+
directory.Create();
104+
}
105+
106+
return true;
107+
}
108+
catch
109+
{
110+
return false;
111+
}
112+
}
113+
}
78114
}
79115
}

0 commit comments

Comments
 (0)