Skip to content

Commit

Permalink
Avoid race conditions during temporary folder generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jgiannuzzi authored and mhutch committed Mar 8, 2021
1 parent 89a98f8 commit 0c09928
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Mono.TextTemplating/Mono.TextTemplating/TemplatingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,12 @@ CompilerResults CompileCode (IEnumerable<string> references, TemplateSettings se
// this may throw, so do it before writing source files
var compiler = GetOrCreateCompiler ();

var tempFolder = Path.GetTempFileName ();
File.Delete (tempFolder);
// GetTempFileName guarantees that the returned file name is unique, but
// there are no equivalent for directories, so we create a directory
// based on the file name, which *should* be unique as long as the file
// exists.
var tempFile = Path.GetTempFileName ();
var tempFolder = tempFile + "dir";
Directory.CreateDirectory (tempFolder);

if (settings.Log != null) {
Expand Down Expand Up @@ -286,7 +290,9 @@ CompilerResults CompileCode (IEnumerable<string> references, TemplateSettings se

if (!args.Debug && !r.Errors.HasErrors) {
r.TempFiles.Delete ();
// we can delete our temporary file after our temporary folder is deleted.
Directory.Delete (tempFolder);
File.Delete (tempFile);
}

return r;
Expand Down

0 comments on commit 0c09928

Please # to comment.