C#: Fix working directory structures in standalone

This commit is contained in:
Tamas Vajk
2023-12-19 12:54:00 +01:00
parent f50817e92a
commit dd64b436c0
2 changed files with 28 additions and 8 deletions

View File

@@ -52,7 +52,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
this.progressMonitor = new ProgressMonitor(logger);
this.sourceDir = new DirectoryInfo(srcDir);
packageDirectory = new TemporaryDirectory(ComputeTempDirectory(sourceDir.FullName));
packageDirectory = new TemporaryDirectory(ComputeTempDirectory(sourceDir.FullName, "packages"));
legacyPackageDirectory = new TemporaryDirectory(ComputeTempDirectory(sourceDir.FullName, "legacypackages"));
missingPackageDirectory = new TemporaryDirectory(ComputeTempDirectory(sourceDir.FullName, "missingpackages"));
@@ -467,7 +467,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
/// with this source tree. Use a SHA1 of the directory name.
/// </summary>
/// <returns>The full path of the temp directory.</returns>
private static string ComputeTempDirectory(string srcDir, string packages = "packages")
private static string ComputeTempDirectory(string srcDir, string subfolderName)
{
var bytes = Encoding.Unicode.GetBytes(srcDir);
var sha = SHA1.HashData(bytes);
@@ -475,7 +475,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
foreach (var b in sha.Take(8))
sb.AppendFormat("{0:x2}", b);
return Path.Combine(FileUtils.GetTemporaryWorkingDirectory(out var _), "GitHub", packages, sb.ToString());
return Path.Combine(FileUtils.GetTemporaryWorkingDirectory(out var _), sb.ToString(), subfolderName);
}
/// <summary>
@@ -723,7 +723,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
Parallel.ForEach(notYetDownloadedPackages, new ParallelOptions { MaxDegreeOfParallelism = options.Threads }, package =>
{
progressMonitor.NugetInstall(package);
using var tempDir = new TemporaryDirectory(ComputeTempDirectory(package));
using var tempDir = new TemporaryDirectory(ComputeTempDirectory(package, "missingpackages_workingdir"));
var success = dotnet.New(tempDir.DirInfo.FullName);
if (!success)
{

View File

@@ -144,20 +144,40 @@ namespace Semmle.Util
return nested;
}
private static string? tempFolderPath = null;
private static readonly object lockObject = new();
public static string GetTemporaryWorkingDirectory(Func<string, string?> getEnvironmentVariable, string lang, out bool shouldCleanUp)
{
shouldCleanUp = false;
var tempFolder = getEnvironmentVariable($"CODEQL_EXTRACTOR_{lang}_SCRATCH_DIR");
if (string.IsNullOrEmpty(tempFolder))
if (!string.IsNullOrEmpty(tempFolder))
{
return tempFolder;
}
if (!string.IsNullOrEmpty(tempFolderPath))
{
shouldCleanUp = true;
return tempFolderPath;
}
lock (lockObject)
{
if (!string.IsNullOrEmpty(tempFolderPath))
{
shouldCleanUp = true;
return tempFolderPath;
}
var tempPath = Path.GetTempPath();
var name = Guid.NewGuid().ToString("N").ToUpper();
tempFolder = Path.Combine(tempPath, "GitHub", name);
Directory.CreateDirectory(tempFolder);
tempFolderPath = tempFolder;
shouldCleanUp = true;
return tempFolder;
}
return tempFolder;
}
public static string GetTemporaryWorkingDirectory(out bool shouldCleanUp) =>