Skip to content

Commit 0c09928

Browse files
jgiannuzzimhutch
authored andcommitted
Avoid race conditions during temporary folder generation
1 parent 89a98f8 commit 0c09928

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Mono.TextTemplating/Mono.TextTemplating/TemplatingEngine.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,12 @@ CompilerResults CompileCode (IEnumerable<string> references, TemplateSettings se
236236
// this may throw, so do it before writing source files
237237
var compiler = GetOrCreateCompiler ();
238238

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

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

287291
if (!args.Debug && !r.Errors.HasErrors) {
288292
r.TempFiles.Delete ();
293+
// we can delete our temporary file after our temporary folder is deleted.
289294
Directory.Delete (tempFolder);
295+
File.Delete (tempFile);
290296
}
291297

292298
return r;

0 commit comments

Comments
 (0)