2004-09-04 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.IO / FileInfo.cs
index ed032372939a3312e6014251d84c849c95758fc8..3345f61dc79dd7ea66f06fd38a75affae389c49a 100644 (file)
 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved\r
 // \r
 // Author:         Jim Richardson, develop@wtfo-guru.com\r
+//                 Dan Lewis (dihlewis@yahoo.co.uk)\r
 // Created:        Monday, August 13, 2001 \r
 //\r
 //------------------------------------------------------------------------------\r
-\r
-using System;
-using System.PAL;
-using System.Diagnostics;
-using System.Security.Permissions;
-
-namespace System.IO\r
-{\r
-       /// <summary>\r
-       /// \r
-       /// </summary>\r
-       public sealed class FileInfo : FileSystemInfo\r
-       {\r
-               private OpSys _os = Platform.OS;
 
-               public FileInfo(string fileName)\r
-               {
-                       CheckArgument.Path(fileName, false);
-                       //LAMESPEC: Does not throw security exception in constructor
-                       OriginalPath = fileName;
-               }
-
-               private bool existsOnDisk(bool exNotFound, bool exIsDirectory)
-               {
-                       bool bRetCode;
-                       
-                       try
-                       {
-                               Refresh();
-                               if((getAttributes() & FileAttributes.Directory) != 0)
-                               {
-                                       if(exIsDirectory)
-                                       {
-                                               throw new UnauthorizedAccessException();
-                                       }
-                                       bRetCode = false;
-                               }
-                               else
-                               {
-                                       bRetCode = true;
-                               }
-                       }
-                       catch(ArgumentException ex)                             
-                       {
-                               Debug.WriteLine(ex); // eliminates not used warning
-                               if(exNotFound)
-                               {
-                                       throw new FileNotFoundException();
-                               }
-                               bRetCode = false;
-                       }
-                       return bRetCode;
-               }
-               
-               public override bool Exists\r
-               {
-                       get
-                       {
-                               return existsOnDisk(false, false);      
-                       }
-               }\r
-\r
-               public override string Name\r
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+\r
+using System;\r
+\r
+namespace System.IO {\r
+\r
+       [Serializable]\r
+       public sealed class FileInfo : FileSystemInfo {\r
+       \r
+\r
+               private bool exists;\r
+\r
+               public FileInfo (string path) {\r
+                       CheckPath (path);\r
+               \r
+                       OriginalPath = path;\r
+                       FullPath = Path.GetFullPath (path);\r
+               }\r
+               \r
+               internal override void InternalRefresh ()\r
                {\r
-                       get\r
-                       {
-                               return Path.GetFileName(getPathName());\r
+                       exists = File.Exists (FullPath);\r
+               }\r
+\r
+\r
+               // public properties\r
+\r
+               public override bool Exists {\r
+                       get {\r
+                               Refresh (false);\r
+\r
+                               if (stat.Attributes == MonoIO.InvalidFileAttributes)\r
+                                       return false;\r
+\r
+                               if ((stat.Attributes & FileAttributes.Directory) != 0)\r
+                                       return false;\r
+\r
+                               return exists;\r
                        }\r
                }\r
 \r
-               /// <summary>\r
-               /// Gets the parent directory info\r
-               /// </summary>\r
-               public DirectoryInfo Directory\r
-               {\r
-                       get\r
-                       {\r
-                               return new DirectoryInfo(Path.GetDirectoryName(getPathName()));\r
+               public override string Name {\r
+                       get {\r
+                               return Path.GetFileName (FullPath);\r
                        }\r
                }\r
 \r
-               /// <summary>\r
-               /// Get the path of the file\r
-               /// </summary>\r
-               public string DirectoryName\r
-               {\r
-                       get\r
-                       {\r
-                               return Path.GetDirectoryName(getPathName());\r
+               public long Length {\r
+                       get {\r
+                               if (!Exists)\r
+                                       throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);\r
+\r
+                               return stat.Length;\r
                        }\r
                }\r
 \r
-               /// <summary>\r
-               /// Get the length of the file\r
-               /// </summary>\r
-               public long Length\r
-               {\r
-                       get\r
-                       {
-                               try
-                               {
-                                       Refresh();
-                               }
-                               catch(ArgumentException ex)
-                               {
-                                       Debug.WriteLine(ex); // eliminates not used compiler warning
-                                       throw new FileNotFoundException();
-                               }
-                               return _os.FileLength(getPathName());
+               public string DirectoryName {\r
+                       get {\r
+                               return Path.GetDirectoryName (FullPath);\r
                        }\r
                }\r
-               
-               public StreamWriter AppendText()\r
-               {       // TODO: verify using correct FileMode here might be Create & Append
-                       return new StreamWriter(Open(FileMode.Append, FileAccess.Write));
+\r
+               public DirectoryInfo Directory {\r
+                       get {\r
+                               return new DirectoryInfo (DirectoryName);\r
+                       }\r
                }\r
-               \r
-               public FileStream Create()\r
-               {
-                       // TODO: verify using correct FileMode here
-                       return Open(FileMode.OpenOrCreate, FileAccess.ReadWrite);\r
+\r
+               // streamreader methods\r
+\r
+               public StreamReader OpenText () {\r
+                       return new StreamReader (Open (FileMode.Open, FileAccess.Read));\r
                }\r
 \r
-               public StreamWriter CreateText()\r
-               {       //TODO: According to doc even CreateText throws a file not found ex
-                       //      sounds suspicious so i'll have to check it out later
-                       //existsOnDisk(true, true); // throw not found, is directory
-                       return new StreamWriter(Open(FileMode.Create, FileAccess.Write));
+               public StreamWriter CreateText () {\r
+                       return new StreamWriter (Open (FileMode.Create, FileAccess.Write));\r
                }\r
                \r
-               public FileStream Open(FileMode mode)\r
-               {\r
-                       return Open(mode, FileAccess.ReadWrite);\r
+               public StreamWriter AppendText () {\r
+                       return new StreamWriter (Open (FileMode.Append, FileAccess.Write));\r
                }\r
 \r
-               public FileStream Open(FileMode mode, FileAccess access)\r
+               // filestream methods\r
+\r
+               public FileStream Create ()\r
                {\r
-                       return Open(mode, access, FileShare.None);\r
-               }
-               
-               public FileStream Open(FileMode mode, FileAccess access, FileShare share)\r
-               {
-                       bool bExists = existsOnDisk(false, true); // throw is directory;
-                       string path = getPathName();
-                   CheckPermission.ModeAccess(mode, access, path, bExists);                    \r
-                       return new FileStream(path, mode, access, share);\r
+                       return File.Create (FullPath);\r
+               }\r
+               \r
+               \r
+               public FileStream OpenRead () {\r
+                       return Open (FileMode.Open, FileAccess.Read);\r
                }\r
 \r
-               public FileStream OpenRead()\r
-               {       // TODO: find out what default share should be\r
-                       return Open(FileMode.Open, FileAccess.Read, FileShare.Read);\r
+               public FileStream OpenWrite () {\r
+                       return Open (FileMode.OpenOrCreate, FileAccess.Write);\r
                }\r
 \r
-               public StreamReader OpenText()\r
-               {       // TODO: verify mode and access values
-                       return new StreamReader(Open(FileMode.OpenOrCreate, FileAccess.ReadWrite));
+               public FileStream Open (FileMode mode) {\r
+                       return Open (mode, FileAccess.ReadWrite);\r
                }\r
 \r
-               public FileStream OpenWrite()\r
-               {\r
-                       return Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);\r
+               public FileStream Open (FileMode mode, FileAccess access) {\r
+                       return Open (mode, access, FileShare.None);\r
                }\r
 \r
-               public FileInfo CopyTo(string destFile)\r
-               {\r
-                       return CopyTo(destFile, false);\r
+               public FileStream Open (FileMode mode, FileAccess access, FileShare share) {\r
+                       return new FileStream (FullPath, mode, access, share);\r
+               }\r
+\r
+               // file methods\r
+\r
+               public override void Delete () {\r
+                       MonoIOError error;\r
+                                               \r
+                       if (!MonoIO.Exists (FullPath, out error)) {\r
+                               // a weird MS.NET behaviour\r
+                               return;\r
+                       }\r
+\r
+                       if (MonoIO.ExistsDirectory (FullPath, out error)) {\r
+                               throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");\r
+                       }\r
+                       \r
+                       if (!MonoIO.DeleteFile (FullPath, out error)) {\r
+                               throw MonoIO.GetException (OriginalPath,\r
+                                                          error);\r
+                       }\r
+               }\r
+               \r
+               public void MoveTo (string dest) {\r
+\r
+                       if (dest == null)\r
+                               throw new ArgumentNullException ();\r
+\r
+                        if (dest == Name || dest == FullName)\r
+                                return;\r
+\r
+                       MonoIOError error;\r
+                       if (MonoIO.Exists (dest, out error) ||\r
+                               MonoIO.ExistsDirectory (dest, out error))\r
+                               throw new IOException ();\r
+                       File.Move (FullPath, dest);\r
+                       this.FullPath = Path.GetFullPath (dest);\r
                }\r
 \r
-               public FileInfo CopyTo(string destFile, bool bOverwrite)\r
-               {       // TODO: Implement\r
-                       return null;\r
+               public FileInfo CopyTo (string path) {\r
+                       return CopyTo (path, false);\r
                }\r
 \r
-               public override void Delete()\r
-               {
-                       existsOnDisk(true, true); // throw not found, is directory
-                       CheckPermission.Demand(FileIOPermissionAccess.AllAccess, getPathName());
-                       _os.DeleteFile(getPathName());
+               public FileInfo CopyTo (string path, bool overwrite) {\r
+                       string dest = Path.GetFullPath (path);\r
+\r
+                       if (overwrite && File.Exists (path))\r
+                               File.Delete (path);\r
+\r
+                       File.Copy (FullPath, dest);\r
+               \r
+                       return new FileInfo (dest);\r
                }\r
 \r
-               public void MoveTo(string destName)\r
-               {       // TODO: Implement\r
+               public override string ToString () {\r
+                       return OriginalPath;\r
                }\r
        }\r
 }\r