Skip to content

Commit 90fc54c

Browse files
committed
Apply review feedback: use Lazy<> instead of locking
1 parent dc36cdb commit 90fc54c

File tree

1 file changed

+11
-25
lines changed

1 file changed

+11
-25
lines changed

csharp/extractor/Semmle.Util/FileUtils.cs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -144,40 +144,26 @@ public static string NestPaths(ILogger logger, string? outerpath, string innerpa
144144
return nested;
145145
}
146146

147-
private static string? tempFolderPath = null;
148-
private static readonly object lockObject = new();
147+
private static Lazy<string> tempFolderPath = new Lazy<string>(() =>
148+
{
149+
var tempPath = Path.GetTempPath();
150+
var name = Guid.NewGuid().ToString("N").ToUpper();
151+
var tempFolder = Path.Combine(tempPath, "GitHub", name);
152+
Directory.CreateDirectory(tempFolder);
153+
return tempFolder;
154+
});
149155

150156
public static string GetTemporaryWorkingDirectory(Func<string, string?> getEnvironmentVariable, string lang, out bool shouldCleanUp)
151157
{
152-
shouldCleanUp = false;
153158
var tempFolder = getEnvironmentVariable($"CODEQL_EXTRACTOR_{lang}_SCRATCH_DIR");
154159
if (!string.IsNullOrEmpty(tempFolder))
155160
{
161+
shouldCleanUp = false;
156162
return tempFolder;
157163
}
158164

159-
if (!string.IsNullOrEmpty(tempFolderPath))
160-
{
161-
shouldCleanUp = true;
162-
return tempFolderPath;
163-
}
164-
165-
lock (lockObject)
166-
{
167-
if (!string.IsNullOrEmpty(tempFolderPath))
168-
{
169-
shouldCleanUp = true;
170-
return tempFolderPath;
171-
}
172-
173-
var tempPath = Path.GetTempPath();
174-
var name = Guid.NewGuid().ToString("N").ToUpper();
175-
tempFolder = Path.Combine(tempPath, "GitHub", name);
176-
Directory.CreateDirectory(tempFolder);
177-
tempFolderPath = tempFolder;
178-
shouldCleanUp = true;
179-
return tempFolder;
180-
}
165+
shouldCleanUp = true;
166+
return tempFolderPath.Value;
181167
}
182168

183169
public static string GetTemporaryWorkingDirectory(out bool shouldCleanUp) =>

0 commit comments

Comments
 (0)