[mono/mono] 66d4b82a: Ensure that CodeDom generated temporary directory is always created. Fixes #12202

"Marek Safar ([email protected])" <[email protected]>
Newsgroups gmane.comp.gnome.mono.patches
Message-ID <00000141a1cb339c-9355ff99-cb9c-41c0-8311-1ae9016024f7-000000@email.amazonses.com>
   Branch: refs/heads/master
     Home: https://github.com/mono/mono
  Compare: https://github.com/mono/mono/compare/a812e9a24b85...66d4b82a8716

   Commit: 66d4b82a871667b3317d4e43b855d1fc1db22108
   Author: Marek Safar <[email protected]> (marek-safar)
     Date: 2013-10-10 09:54:20 GMT
      URL: https://github.com/mono/mono/commit/66d4b82a871667b3317d4e43b855d1fc1db22108

Ensure that CodeDom generated temporary directory is always created. Fixes #12202

Changed paths:
  M mcs/class/System/System.CodeDom.Compiler/TempFileCollection.cs
  M mcs/class/System/Test/Microsoft.CSharp/CSharpCodeProviderTest.cs

Modified: mcs/class/System/System.CodeDom.Compiler/TempFileCollection.cs
===================================================================
@@ -73,33 +73,36 @@ public string BasePath
 
 					// note: this property *cannot* change TempDir property
 					string temp = tempdir;
-					if (temp.Length == 0)
-						temp = GetOwnTempDir ();
+					if (temp.Length == 0) {
+						if (ownTempDir != null) {
+							temp = ownTempDir;
+							Directory.CreateDirectory (temp);
+						} else {
+							temp = CreateOwnTempDir ();
+						}
+					}
 
 					// Create a temporary file at the target directory. This ensures
 					// that the generated file name is unique.
-					FileStream f = null;
-					do {
+					int test_counter = 1000;
+					while (true) {
 						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
+							using (var f = new FileStream (path, FileMode.CreateNew)) {
+								break;
+							}
+						} catch (IOException) {
+							if (test_counter-- > 0)
+								continue;
+
 							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) {
@@ -110,12 +113,9 @@ public string BasePath
 				return(basepath);
 			}
 		}
-		
-		string GetOwnTempDir ()
-		{
-			if (ownTempDir != null)
-				return ownTempDir;
 
+		string CreateOwnTempDir ()
+		{
 			// this call ensure the Environment permissions check
 			string basedir = Path.GetTempPath ();
 			
@@ -247,7 +247,6 @@ public void Delete()
 			}
 			if (allDeleted && ownTempDir != null) {
 				Directory.Delete (ownTempDir, true);
-				ownTempDir = null;
 			}
 		}
 

Modified: mcs/class/System/Test/Microsoft.CSharp/CSharpCodeProviderTest.cs
===================================================================
@@ -359,6 +359,32 @@ public void CompileFromSource_InMemory ()
 			Assert.AreEqual (tempFile, tempFiles[0], "#5");
 		}
 
+
+		[Test]
+		public void CompileFromSource_InMemory_Twice ()
+		{
+			CompilerParameters options = new CompilerParameters ();
+			options.GenerateExecutable = false;
+			options.GenerateInMemory = true;
+
+			ICodeCompiler compiler = _codeProvider.CreateCompiler ();
+
+			var src_1 = "class X { ";
+
+			CompilerResults results_1 = compiler.CompileAssemblyFromSource (options, src_1);
+			var output_1 = options.OutputAssembly;
+
+			var src_2 = "class X { }";
+
+			CompilerResults results_2 = compiler.CompileAssemblyFromSource (options, src_2);
+			var output_2 = options.OutputAssembly;
+
+			// verify compilation was successful
+			AssertCompileResults (results_2, true);
+
+			Assert.AreEqual (output_1, output_2, "#1");
+		}
+
 		[Test]
 		public void CompileFromSourceBatch_InMemory ()
 		{


_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.