Skip to content
This repository was archived by the owner on Aug 9, 2025. It is now read-only.

Commit 8b6d874

Browse files
committed
Merge pull request #137 from gkelly-si/master
improved temp file handling to make sure we cleanup old files in the event of failure
2 parents 5c63b28 + 4635782 commit 8b6d874

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

CPPCheckPlugin/AnalyzerCppcheck.cs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,40 @@ namespace VSPackage.CPPCheckPlugin
1111
{
1212
class AnalyzerCppcheck : ICodeAnalyzer
1313
{
14+
private const string tempFilePrefix = "CPPCheckPlugin";
15+
16+
public AnalyzerCppcheck()
17+
{
18+
// Perform some cleanup of old temporary files
19+
string tempPath = Path.GetTempPath();
20+
21+
try
22+
{
23+
// Get all files that have our unique prefix
24+
string[] oldFiles = Directory.GetFiles(tempPath, tempFilePrefix + "*");
25+
26+
foreach (string file in oldFiles)
27+
{
28+
DateTime fileModifiedDate = File.GetLastWriteTime(file);
29+
30+
if (fileModifiedDate.AddMinutes(60) < DateTime.Now)
31+
{
32+
// File hasn't been written to in the last 60 mins, so it must be
33+
// from an earlier instance which didn't exit gracefully.
34+
File.Delete(file);
35+
}
36+
}
37+
}
38+
catch (System.Exception) {}
39+
}
40+
41+
~AnalyzerCppcheck()
42+
{
43+
// Delete the temp file. Doesn't throw an exception if the file was never
44+
// created, so we don't need to worry about that.
45+
File.Delete(tempFileName);
46+
}
47+
1448
private string getCPPCheckArgs(ConfiguredFiles configuredFiles, bool analysisOnSavedFile, bool multipleProjects, StreamWriter tempFile)
1549
{
1650
Debug.Assert(_numCores > 0);
@@ -174,11 +208,12 @@ public override void analyze(List<ConfiguredFiles> allConfiguredFiles, OutputWin
174208
if (!allConfiguredFiles.Any())
175209
return;
176210

177-
StreamWriter tempFile = new StreamWriter(tempFileName);
178-
179211
List<string> cppheckargs = new List<string>();
180-
foreach (var configuredFiles in allConfiguredFiles)
181-
cppheckargs.Add(getCPPCheckArgs(configuredFiles, analysisOnSavedFile, allConfiguredFiles.Count > 1, tempFile));
212+
using( StreamWriter tempFile = new StreamWriter(tempFileName) )
213+
{
214+
foreach (var configuredFiles in allConfiguredFiles)
215+
cppheckargs.Add(getCPPCheckArgs(configuredFiles, analysisOnSavedFile, allConfiguredFiles.Count > 1, tempFile));
216+
}
182217

183218
string analyzerPath = Properties.Settings.Default.CPPcheckPath;
184219
while (!File.Exists(analyzerPath))
@@ -191,8 +226,6 @@ public override void analyze(List<ConfiguredFiles> allConfiguredFiles, OutputWin
191226
analyzerPath = dialog.FileName;
192227
}
193228

194-
tempFile.Close();
195-
196229
Properties.Settings.Default.CPPcheckPath = analyzerPath;
197230
Properties.Settings.Default.Save();
198231

@@ -337,8 +370,8 @@ protected override void analysisFinished()
337370
// created, so we don't need to worry about that.
338371
File.Delete(tempFileName);
339372
}
340-
373+
341374
private Problem _unfinishedProblem = null;
342-
string tempFileName = Path.GetTempFileName();
375+
private string tempFileName = Path.GetTempPath() + tempFilePrefix + "_" + Path.GetRandomFileName();
343376
}
344377
}

0 commit comments

Comments
 (0)