2002-05-14 Nick Drochak <ndrochak@gol.com>
authorNick Drochak <nickd@mono-cvs.ximian.com>
Tue, 14 May 2002 10:40:23 +0000 (10:40 -0000)
committerNick Drochak <nickd@mono-cvs.ximian.com>
Tue, 14 May 2002 10:40:23 +0000 (10:40 -0000)
* File.cs: Add parameter checks to most methods. Not completely done,
but all current unit tests pass.

* Path.cs: Implement GetTempFileName().

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

mcs/class/corlib/System.IO/ChangeLog
mcs/class/corlib/System.IO/File.cs
mcs/class/corlib/System.IO/Path.cs

index 081597dc07bac06cf37eb466fefa89bbc763aca1..2be3d85be8e7fa3f5f61b9e77b638e87c9b770bd 100644 (file)
@@ -1,3 +1,10 @@
+2002-05-14 Nick Drochak  <ndrochak@gol.com>
+
+       * File.cs: Add parameter checks to most methods. Not completely done,
+       but all current unit tests pass.
+
+       * Path.cs: Implement GetTempFileName().
+
 2002-05-10  Nick Drochak  <ndrochak@gol.com>
 
        * StreamWriter.cs (Flush): Throw proper exception if internal stream
index d68841592af26a1238df41bef3f4b546360a25e6..75203ef7bd1fd9388edb9dc90e47e1f1b74643dc 100644 (file)
@@ -1,5 +1,5 @@
 // \r
-// System.IO.File.cs \r
+// System.IO.FIle.cs \r
 //\r
 // \r
 // Authors:\r
@@ -18,28 +18,56 @@ namespace System.IO
        /// <summary>\r
        /// \r
        /// </summary>\r
-       public sealed class File : Object\r
+       public sealed class File\r
        {\r
                private File () {}
 
+               \r
+               \r
                public static StreamWriter AppendText (string path)\r
                {       \r
                        return new StreamWriter (path, true);\r
                }\r
-                \r
+\r
+               [MonoTODO("Security Permision Checks")]\r
                public static void Copy (string sourceFilename, string destFilename)\r
                {\r
                        Copy (sourceFilename, destFilename, false);\r
                }\r
-                \r
+\r
                public static void Copy (string src, string dest, bool overwrite)\r
                {       \r
-                       if (src == null || dest == null)\r
-                               throw new ArgumentNullException ();\r
-                       if (src == "" || dest == "" ||\r
-                           src.IndexOfAny (Path.InvalidPathChars) != -1 ||\r
-                           dest.IndexOfAny (Path.InvalidPathChars) != -1)\r
-                               throw new ArgumentException ();\r
+                       if (src == null)\r
+                               throw new ArgumentNullException ("src");\r
+                       if (dest == null)\r
+                               throw new ArgumentNullException ("dest");\r
+                       if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)\r
+                               throw new ArgumentException ("src");\r
+                       if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)\r
+                               throw new ArgumentException ("dest");\r
+                       if (src.IndexOf(':') > 1)\r
+                               throw new NotSupportedException("src");\r
+                       if (dest.IndexOf(':') > 1)\r
+                               throw new NotSupportedException("dest");\r
+                       if (!Exists (src)) {\r
+                               throw new FileNotFoundException (src + " does not exist");\r
+                       }\r
+                       else {\r
+                               if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){\r
+                                       throw new ArgumentException(src + " is a directory");   \r
+                               }\r
+                       }\r
+                       if (Exists (dest)) {\r
+                               if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){\r
+                                       throw new ArgumentException(dest + " is a directory");  \r
+                               }\r
+                               if (!overwrite)\r
+                                       throw new IOException (dest + " already exists");\r
+                       }\r
+\r
+                       string DirName = Path.GetDirectoryName(dest);\r
+                       if (!Directory.Exists (DirName))\r
+                               throw new DirectoryNotFoundException("Destination directory not found: " + DirName);\r
 \r
                        if (!MonoIO.CopyFile (src, dest, overwrite))\r
                                throw MonoIO.GetException ();\r
@@ -52,6 +80,22 @@ namespace System.IO
 \r
                public static FileStream Create (string path, int buffersize)\r
                {\r
+                       if (null == path)\r
+                               throw new ArgumentNullException("path");\r
+                       if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)\r
+                               throw new ArgumentException("path");\r
+                       if (path.IndexOf(':') > 1)\r
+                               throw new NotSupportedException();\r
+\r
+                       string DirName = Path.GetDirectoryName(path);\r
+                       if (!Directory.Exists (DirName))\r
+                               throw new DirectoryNotFoundException("Destination directory not found: " + DirName);\r
+                       if (Exists(path)){\r
+                               if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){\r
+                                       throw new UnauthorizedAccessException(path + " is a read-only");        \r
+                               }\r
+                       }\r
+\r
                        return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,\r
                                               FileShare.None, buffersize);\r
                }\r
@@ -67,24 +111,52 @@ namespace System.IO
                \r
                public static void Delete (string path)\r
                {\r
-                       if (path == null)\r
-                               throw new ArgumentNullException ();\r
-                       if (path == "" || path.IndexOfAny (Path.InvalidPathChars) != -1)\r
-                               throw new ArgumentException ();\r
+                       if (null == path)\r
+                               throw new ArgumentNullException("path");\r
+                       if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)\r
+                               throw new ArgumentException("path");\r
+                       if (path.IndexOf(':') > 1)\r
+                               throw new NotSupportedException();\r
+                       if (Directory.Exists (path))\r
+                               throw new UnauthorizedAccessException("path is a directory");\r
+\r
+                       string DirName = Path.GetDirectoryName(path);\r
+                       if (!Directory.Exists (DirName))\r
+                               throw new DirectoryNotFoundException("Destination directory not found: " + DirName);\r
+\r
                        if (!MonoIO.DeleteFile (path)){\r
                                Exception e = MonoIO.GetException ();\r
-                               if (e != null && !(e is FileNotFoundException))\r
+                               if ((e is FileNotFoundException))\r
                                        throw e;\r
                        }\r
                }\r
                \r
                public static bool Exists (string path)\r
                {\r
+                       // For security reasons no exceptions are thrown, only false is returned if there\r
+                       // is any problem with the path or permissions.  Minimizes what information can be\r
+                       // discovered by using this method.\r
+                       if (null == path || String.Empty == path.Trim() \r
+                               || path.IndexOfAny(Path.InvalidPathChars) >= 0\r
+                               || path.IndexOf(':') > 1)\r
+                               return false;\r
+\r
                        return MonoIO.ExistsFile (path);\r
                }\r
 \r
                public static FileAttributes GetAttributes (string path)\r
                {\r
+                       if (null == path)\r
+                               throw new ArgumentNullException("path");\r
+                       if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)\r
+                               throw new ArgumentException("path");\r
+                       if (path.IndexOf(':') > 1)\r
+                               throw new NotSupportedException();\r
+\r
+                       string DirName = Path.GetDirectoryName(path);\r
+                       if (!Directory.Exists(DirName))\r
+                               throw new DirectoryNotFoundException("Directory '" + DirName + "' not found in '" + Environment.CurrentDirectory + "'.");\r
+\r
                        return MonoIO.GetFileAttributes (path);\r
                }\r
 \r
@@ -114,12 +186,30 @@ namespace System.IO
 \r
                public static void Move (string src, string dest)\r
                {\r
-                       if (src == null || dest == null)\r
-                               throw new ArgumentNullException ();\r
-                       if (src == "" || dest == "" ||\r
-                           src.IndexOfAny (Path.InvalidPathChars) != -1 ||\r
-                           dest.IndexOfAny (Path.InvalidPathChars) != -1)\r
-                               throw new ArgumentException ();\r
+                       if (src == null)\r
+                               throw new ArgumentNullException ("src");\r
+                       if (dest == null)\r
+                               throw new ArgumentNullException ("dest");\r
+                       if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)\r
+                               throw new ArgumentException ("src");\r
+                       if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)\r
+                               throw new ArgumentException ("dest");\r
+                       if (src.IndexOf(':') > 1)\r
+                               throw new NotSupportedException("src");\r
+                       if (dest.IndexOf(':') > 1)\r
+                               throw new NotSupportedException("dest");\r
+                       if (!Exists (src))\r
+                               throw new FileNotFoundException (src + " does not exist");\r
+                       if (Exists (dest) && ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory))\r
+                                       throw new ArgumentException(dest + " is a directory");  \r
+\r
+                       string DirName;\r
+                       DirName = Path.GetDirectoryName(src);\r
+                       if (!Directory.Exists (DirName))\r
+                               throw new DirectoryNotFoundException("Source directory not found: " + DirName);\r
+                       DirName = Path.GetDirectoryName(dest);\r
+                       if (!Directory.Exists (DirName))\r
+                               throw new DirectoryNotFoundException("Destination directory not found: " + DirName);\r
 \r
                        if (!MonoIO.MoveFile (src, dest))\r
                                throw MonoIO.GetException ();\r
index 46aa09454208a21c2c5e314aedd8a013f0104cc8..b0db49450202322c1ce003c262d3515dc45cc98f 100644 (file)
@@ -188,11 +188,21 @@ namespace System.IO
                        return null;
                }
 
-               [MonoTODO]
                public static string GetTempFileName ()
                {
-                       //TODO: Implement method
-                       return string.Empty;
+                       string path;
+                       Random rnd;
+                       int num;
+                       rnd = new Random ();
+                       num = rnd.Next ();
+                       path = GetTempPath() + DirectorySeparatorChar + "tmp" + num.ToString("x");
+                       while (File.Exists(path) || Directory.Exists(path)) {    
+                               num = rnd.Next ();
+                               path = GetTempPath() + DirectorySeparatorChar + "tmp" + num.ToString("x");
+                       }
+                       FileStream f = File.Create(path);
+                       f.Close();
+                       return path;
                }
 
                /// <summary>