Author: holmes
Date: 2007-08-20 14:48:55 -0400 (Mon, 20 Aug 2007)
New Revision: 84485

Modified:
   trunk/mcs/class/corlib/System.IO/ChangeLog
   trunk/mcs/class/corlib/System.IO/File.cs
   trunk/mcs/class/corlib/System.IO/MonoIO.cs
   trunk/mcs/class/corlib/Test/System.IO/ChangeLog
   trunk/mcs/class/corlib/Test/System.IO/FileTest.cs
Log:
2007-08-20  William Holmes  <[EMAIL PROTECTED]>

        Implementing System.IO.File.Replace
        *File.cs:  Add implementation for IO.File.Replace methods.
        *MonoIO.cs: Declared an internal call for ReplaceFile
        *FileTest.cs:  Added a test for IO.File.Replace.  

        Code is contributed under MIT/X11 license.


Modified: trunk/mcs/class/corlib/System.IO/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/System.IO/ChangeLog  2007-08-20 18:47:52 UTC (rev 
84484)
+++ trunk/mcs/class/corlib/System.IO/ChangeLog  2007-08-20 18:48:55 UTC (rev 
84485)
@@ -1,3 +1,10 @@
+2007-08-20  William Holmes  <[EMAIL PROTECTED]>
+
+       *File.cs:  Add implementation for IO.File.Replace methods.
+       *MonoIO.cs: Declared an internal call for ReplaceFile
+
+       Code is contributed under MIT/X11 license.
+
 2007-07-31  Dick Porter  <[EMAIL PROTECTED]>
 
        * MonoIO.cs: Fix formatting of 'access denied' exception when the

Modified: trunk/mcs/class/corlib/System.IO/File.cs
===================================================================
--- trunk/mcs/class/corlib/System.IO/File.cs    2007-08-20 18:47:52 UTC (rev 
84484)
+++ trunk/mcs/class/corlib/System.IO/File.cs    2007-08-20 18:48:55 UTC (rev 
84485)
@@ -399,7 +399,7 @@
                                            string destinationFileName,
                                            string destinationBackupFileName)
                {
-                       throw new NotImplementedException ();
+                       Replace (sourceFileName, destinationFileName, 
destinationBackupFileName, false);
                }
                
                public static void Replace (string sourceFileName,
@@ -407,7 +407,53 @@
                                            string destinationBackupFileName,
                                            bool ignoreMetadataErrors)
                {
-                       throw new NotImplementedException ();
+                       MonoIOError error;
+
+                       if (sourceFileName == null)
+                               throw new ArgumentNullException 
("sourceFileName");
+                       if (destinationFileName == null)
+                               throw new ArgumentNullException 
("destinationFileName");
+                       if (sourceFileName.Trim () == "" || 
sourceFileName.IndexOfAny (Path.InvalidPathChars) != -1)
+                               throw new ArgumentException ("sourceFileName");
+                       if (destinationFileName.Trim () == "" || 
destinationFileName.IndexOfAny (Path.InvalidPathChars) != -1)
+                               throw new ArgumentException 
("destinationFileName");
+
+                       string fullSource = Path.GetFullPath (sourceFileName);
+                       string fullDest = Path.GetFullPath 
(destinationFileName);
+                       if (MonoIO.ExistsDirectory (fullSource, out error))
+                               throw new IOException (Locale.GetText ("{0} is 
a directory", sourceFileName));
+                       if (MonoIO.ExistsDirectory (fullDest, out error))
+                               throw new IOException (Locale.GetText ("{0} is 
a directory", destinationFileName));
+
+                       if (!Exists (fullSource))
+                               throw new FileNotFoundException (Locale.GetText 
("{0} does not exist", sourceFileName), 
+                                                                
sourceFileName);
+                       if (!Exists (fullDest))
+                               throw new FileNotFoundException (Locale.GetText 
("{0} does not exist", destinationFileName), 
+                                                                
destinationFileName);
+                       if (fullSource == fullDest) 
+                               throw new IOException (Locale.GetText ("Source 
and destination arguments are the same file."));
+
+                       string fullBackup = null;
+                       if (destinationBackupFileName != null) {
+                               if (destinationBackupFileName.Trim () == "" || 
+                                   destinationBackupFileName.IndexOfAny 
(Path.InvalidPathChars) != -1)
+                                       throw new ArgumentException 
("destinationBackupFileName");
+
+                               fullBackup = Path.GetFullPath 
(destinationBackupFileName);
+                               if (MonoIO.ExistsDirectory (fullBackup, out 
error))
+                                       throw new IOException (Locale.GetText 
("{0} is a directory", destinationBackupFileName));
+                               if (fullSource == fullBackup)
+                                       throw new IOException (Locale.GetText 
("Source and backup arguments are the same file."));
+                               if (fullDest == fullBackup)
+                                       throw new IOException (Locale.GetText (
+                                                              "Destination and 
backup arguments are the same file."));
+                       }
+
+                       if (!MonoIO.ReplaceFile (fullSource, fullDest, 
fullBackup, 
+                                                ignoreMetadataErrors, out 
error)) {
+                               throw MonoIO.GetException (error);
+                       }
                }
                
                public static void SetAccessControl (string path,

Modified: trunk/mcs/class/corlib/System.IO/MonoIO.cs
===================================================================
--- trunk/mcs/class/corlib/System.IO/MonoIO.cs  2007-08-20 18:47:52 UTC (rev 
84484)
+++ trunk/mcs/class/corlib/System.IO/MonoIO.cs  2007-08-20 18:48:55 UTC (rev 
84485)
@@ -178,6 +178,13 @@
                                                      out MonoIOError error);
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               public extern static bool ReplaceFile (string sourceFileName, 
+                                                      string 
destinationFileName, 
+                                                      string 
destinationBackupFileName, 
+                                                      bool 
ignoreMetadataErrors,
+                                                      out MonoIOError error);
+
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
                public extern static FileAttributes GetFileAttributes (string 
path, out MonoIOError error);
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]

Modified: trunk/mcs/class/corlib/Test/System.IO/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/Test/System.IO/ChangeLog     2007-08-20 18:47:52 UTC 
(rev 84484)
+++ trunk/mcs/class/corlib/Test/System.IO/ChangeLog     2007-08-20 18:48:55 UTC 
(rev 84485)
@@ -1,3 +1,9 @@
+2007-08-20  William Holmes  <[EMAIL PROTECTED]>
+
+       *FileTest.cs:  Added a test for IO.File.Replace.  
+
+       Code is contributed under MIT/X11 license.
+
 2007-08-01  Dick Porter  <[EMAIL PROTECTED]>
 
        * DirectoryTest.cs: Test for bug 82212 is now working.

Modified: trunk/mcs/class/corlib/Test/System.IO/FileTest.cs
===================================================================
--- trunk/mcs/class/corlib/Test/System.IO/FileTest.cs   2007-08-20 18:47:52 UTC 
(rev 84484)
+++ trunk/mcs/class/corlib/Test/System.IO/FileTest.cs   2007-08-20 18:48:55 UTC 
(rev 84485)
@@ -1638,5 +1638,38 @@
                        if (File.Exists (path))
                                File.Delete (path);
                }
+
+#if NET_2_0
+               [Test]
+               public void ReplaceTest ()
+               {
+                       string tmp = Path.Combine (TempFolder, "ReplaceTest");
+                       Directory.CreateDirectory (tmp);
+                       string origFile = Path.Combine (tmp, "origFile");
+                       string replaceFile = Path.Combine (tmp, "replaceFile");
+                       string backupFile = Path.Combine (tmp, "backupFile");
+
+                       using (StreamWriter sw = File.CreateText (origFile)) {
+                               sw.WriteLine ("origFile");
+                       }
+                       using (StreamWriter sw = File.CreateText (replaceFile)) 
{
+                               sw.WriteLine ("replaceFile");
+                       }
+                       using (StreamWriter sw = File.CreateText (backupFile)) {
+                               sw.WriteLine ("backupFile");
+                       }
+
+                       File.Replace (origFile, replaceFile, backupFile);
+                       Assertion.Assert ("origFile should not exists", 
!File.Exists (origFile));
+                       using (StreamReader sr = File.OpenText (replaceFile)) {
+                               string txt = sr.ReadLine ();
+                               Assertion.AssertEquals ("#2", "origFile", txt);
+                       }
+                       using (StreamReader sr = File.OpenText (backupFile)) {
+                               string txt = sr.ReadLine ();
+                               Assertion.AssertEquals ("#3", "replaceFile", 
txt);
+                       }
+               }
+#endif
        }
 }

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

Reply via email to