Author: fejj
Date: 2007-03-14 16:19:45 -0500 (Wed, 14 Mar 2007)
New Revision: 74307

Modified:
   trunk/monodevelop/Core/src/MonoDevelop.Projects/ChangeLog
   trunk/monodevelop/Core/src/MonoDevelop.Projects/Makefile.am
   
trunk/monodevelop/Core/src/MonoDevelop.Projects/MonoDevelop.Projects.Text/TextFile.cs
Log:
2007-03-14  Jeffrey Stedfast  <[EMAIL PROTECTED]>

        * MonoDevelop.Projects.Text/TextFile.cs (WriteFile): Modified to
        save to a temporary file first (using mode Create), and then
        rename() it over the original file, this way if the system is out
        of disk space we aren't clobbering the original file. Opening with
        mode Create should fix bug #80973.



Modified: trunk/monodevelop/Core/src/MonoDevelop.Projects/ChangeLog
===================================================================
--- trunk/monodevelop/Core/src/MonoDevelop.Projects/ChangeLog   2007-03-14 
21:15:47 UTC (rev 74306)
+++ trunk/monodevelop/Core/src/MonoDevelop.Projects/ChangeLog   2007-03-14 
21:19:45 UTC (rev 74307)
@@ -1,3 +1,11 @@
+2007-03-14  Jeffrey Stedfast  <[EMAIL PROTECTED]>
+
+       * MonoDevelop.Projects.Text/TextFile.cs (WriteFile): Modified to
+       save to a temporary file first (using mode Create), and then
+       rename() it over the original file, this way if the system is out
+       of disk space we aren't clobbering the original file. Opening with
+       mode Create should fix bug #80973.
+
 2007-03-04 David Makovský (Yakeen) <[EMAIL PROTECTED]>
 
        * MonoDevelop.Projects.Deployment/DirectoryDeployHandler.cs,

Modified: trunk/monodevelop/Core/src/MonoDevelop.Projects/Makefile.am
===================================================================
--- trunk/monodevelop/Core/src/MonoDevelop.Projects/Makefile.am 2007-03-14 
21:15:47 UTC (rev 74306)
+++ trunk/monodevelop/Core/src/MonoDevelop.Projects/Makefile.am 2007-03-14 
21:19:45 UTC (rev 74307)
@@ -239,9 +239,9 @@
        MonoDevelop.Projects/ProjectReferenceCollection.cs \
        MonoDevelop.Projects/ProjectReferenceEventArgs.cs \
        MonoDevelop.Projects/ProjectRenameEventArgs.cs \
-       MonoDevelop.Projects/ProjectsServices.cs \
        MonoDevelop.Projects/ProjectService.cs \
        MonoDevelop.Projects/ProjectServiceExtension.cs \
+       MonoDevelop.Projects/ProjectsServices.cs \
        MonoDevelop.Projects/TypelibImporter.cs \
        MonoDevelop.Projects/UnknownCombineEntry.cs \
        MonoDevelop.Projects/UnknownProjectVersionException.cs

Modified: 
trunk/monodevelop/Core/src/MonoDevelop.Projects/MonoDevelop.Projects.Text/TextFile.cs
===================================================================
--- 
trunk/monodevelop/Core/src/MonoDevelop.Projects/MonoDevelop.Projects.Text/TextFile.cs
       2007-03-14 21:15:47 UTC (rev 74306)
+++ 
trunk/monodevelop/Core/src/MonoDevelop.Projects/MonoDevelop.Projects.Text/TextFile.cs
       2007-03-14 21:19:45 UTC (rev 74307)
@@ -30,6 +30,8 @@
 using System.IO;
 using System.Text;
 using System.Runtime.InteropServices;
+using Mono.Unix;
+using Mono.Unix.Native;
 
 namespace MonoDevelop.Projects.Text
 {
@@ -226,28 +228,31 @@
                
                public static void WriteFile (string fileName, string content, 
string encoding)
                {
-                       if (encoding == null || encoding == "UTF-8") {
-                               using (StreamWriter sw = new StreamWriter 
(fileName)) {
-                                       sw.Write (content.ToString ());
-                               }
-                       }
-                       else {
-                               byte[] bytes = Encoding.UTF8.GetBytes (content);
+                       byte[] buf = Encoding.UTF8.GetBytes (content);
+                       
+                       if (encoding != null && encoding != "UTF-8") {
+                               int nr = 0, nw = 0;
+                               IntPtr converted = g_convert (buf, buf.Length, 
encoding, "UTF-8", 
+                                                             ref nr, ref nw, 
IntPtr.Zero);
                                
-                               int nr=0, nw=0;
-                               IntPtr cc = g_convert (bytes, bytes.Length, 
encoding, "UTF-8", ref nr, ref nw, IntPtr.Zero);
-                               bytes = null;
+                               buf = null;
                                
-                               if (cc != IntPtr.Zero) {
-                                       byte[] data = new byte [nw];
-                                       
System.Runtime.InteropServices.Marshal.Copy (cc, data, 0, nw);
-                                       g_free (cc);
-                                       using (FileStream fs = File.OpenWrite 
(fileName)) {
-                                               fs.Write (data, 0, data.Length);
-                                       }
-                               } else
+                               if (converted == IntPtr.Zero)
                                        throw new Exception ("Invalid encoding: 
" + encoding);
+                               
+                               buf = new byte [nw];
+                               System.Runtime.InteropServices.Marshal.Copy 
(converted, buf, 0, nw);
+                               g_free (converted);
                        }
+                       
+                       string tempName = Path.GetDirectoryName (fileName) + 
+                               Path.DirectorySeparatorChar + ".#" + 
Path.GetFileName (fileName);
+                       FileStream fs = new FileStream (tempName, 
FileMode.Create, FileAccess.Write);
+                       fs.Write (buf, 0, buf.Length);
+                       fs.Flush ();
+                       fs.Close ();
+                       
+                       Syscall.rename (tempName, fileName);
                }
        }
 }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to