2006-05-04 LLuis Sanchez Gual <lluis@novell.com>
authorLluis Sanchez <lluis@novell.com>
Thu, 4 May 2006 12:03:35 +0000 (12:03 -0000)
committerLluis Sanchez <lluis@novell.com>
Thu, 4 May 2006 12:03:35 +0000 (12:03 -0000)
* TempFileCollection.cs: Make sure generated file names
  are unique. Fixes bug #76125 and #78230.

svn path=/trunk/mcs/; revision=60253

mcs/class/System/System.CodeDom.Compiler/ChangeLog
mcs/class/System/System.CodeDom.Compiler/TempFileCollection.cs

index 40e1b7b82c3cdb418c8db90d7140dbe205e8251f..8e01380d10d7086997b49cb4965ea820a3a37d46 100644 (file)
@@ -1,3 +1,8 @@
+2006-05-04  LLuis Sanchez Gual  <lluis@novell.com>
+
+       * TempFileCollection.cs: Make sure generated file names
+         are unique. Fixes bug #76125 and #78230.
+
 2006-04-20  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * CodeGenerator.cs: Output empty line between types if 
index 416f1089f70a2d918a7f5ddb56ab3e2a875ae477..947e6faa656d9c22e1585bb8d2aa132a864d0a89 100644 (file)
@@ -77,9 +77,30 @@ namespace System.CodeDom.Compiler {
                                        if (rnd == null)
                                                rnd = new Random ();
 
-                                       string random = rnd.Next (10000,99999).ToString ();
-                                       basepath = Path.Combine (temp, random);
-
+                                       // Create a temporary file at the target directory. This ensures
+                                       // that the generated file name is unique.
+                                       FileStream f = null;
+                                       do {
+                                               int num = rnd.Next ();
+                                               num++;
+                                               basepath = Path.Combine (temp, num.ToString("x"));
+                                               string path = basepath + ".tmp";
+
+                                               try {
+                                                       f = new FileStream (path, FileMode.CreateNew);
+                                               }
+                                               catch (System.IO.IOException) {
+                                                       f = null;
+                                                       continue;
+                                               }
+                                               catch {
+                                                       // avoid endless loop
+                                                       throw;
+                                               }
+                                       } while (f == null);
+                                       
+                                       f.Close ();
+                                       
                                        // and you must have discovery access to the combined path
                                        // note: the cache behaviour is tested in the CAS tests
                                        if (SecurityManager.SecurityEnabled) {
@@ -178,6 +199,10 @@ namespace System.CodeDom.Compiler {
                                        filehash.Remove(file);
                                }
                        }
+                       if (basepath != null) {
+                               string tmpFile = basepath + ".tmp";
+                               File.Delete (tmpFile);
+                       }
                }
 
                IEnumerator IEnumerable.GetEnumerator ()