X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FSystem.IO%2FMonoIO.cs;h=38c8cf3de735623fa52e6cd144ae626ceaed1fe7;hb=ab0b591ca59d99a2370bf9f579b091c5edf09ae5;hp=b62e1453665f5909d7a43a5fceb9aa27fd571b1a;hpb=e2b2d181084848f3c5dde2788370db1b79893c69;p=mono.git diff --git a/mcs/class/corlib/System.IO/MonoIO.cs b/mcs/class/corlib/System.IO/MonoIO.cs index b62e1453665..38c8cf3de73 100644 --- a/mcs/class/corlib/System.IO/MonoIO.cs +++ b/mcs/class/corlib/System.IO/MonoIO.cs @@ -35,7 +35,8 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; -#if NET_2_1 +using Microsoft.Win32.SafeHandles; +#if MOBILE using System.IO.IsolatedStorage; #endif @@ -50,6 +51,8 @@ namespace System.IO public static readonly IntPtr InvalidHandle = (IntPtr)(-1L); + static bool dump_handles = Environment.GetEnvironmentVariable ("MONO_DUMP_HANDLES_ON_ERROR_TOO_MANY_OPEN_FILES") != null; + // error methods public static Exception GetException (MonoIOError error) { @@ -85,22 +88,16 @@ namespace System.IO // FIXME: add more exception mappings here case MonoIOError.ERROR_FILE_NOT_FOUND: message = String.Format ("Could not find file \"{0}\"", path); -#if MOONLIGHT - return new IsolatedStorageException (message); -#else return new FileNotFoundException (message, path); -#endif case MonoIOError.ERROR_TOO_MANY_OPEN_FILES: + if (dump_handles) + DumpHandles (); return new IOException ("Too many open files", unchecked((int)0x80070000) | (int)error); case MonoIOError.ERROR_PATH_NOT_FOUND: message = String.Format ("Could not find a part of the path \"{0}\"", path); -#if MOONLIGHT - return new IsolatedStorageException (message); -#else return new DirectoryNotFoundException (message); -#endif case MonoIOError.ERROR_ACCESS_DENIED: message = String.Format ("Access to the path \"{0}\" is denied.", path); @@ -111,7 +108,7 @@ namespace System.IO return new IOException (message, unchecked((int)0x80070000) | (int)error); case MonoIOError.ERROR_INVALID_DRIVE: message = String.Format ("Could not find the drive '{0}'. The drive might not be ready or might not be mapped.", path); -#if !NET_2_1 +#if !MOBILE return new DriveNotFoundException (message); #else return new IOException (message, unchecked((int)0x80070000) | (int)error); @@ -158,6 +155,10 @@ namespace System.IO case MonoIOError.ERROR_NOT_SAME_DEVICE: message = "Source and destination are not on the same device"; return new IOException (message, unchecked((int)0x80070000) | (int)error); + + case MonoIOError.ERROR_DIRECTORY: + message = "The directory name is invalid"; + return new IOException (message, unchecked((int)0x80070000) | (int)error); default: message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path); @@ -173,9 +174,6 @@ namespace System.IO [MethodImplAttribute (MethodImplOptions.InternalCall)] public extern static bool RemoveDirectory (string path, out MonoIOError error); - [MethodImplAttribute (MethodImplOptions.InternalCall)] - public extern static string [] GetFileSystemEntries (string path, string path_with_pattern, int attrs, int mask, out MonoIOError error); - [MethodImplAttribute (MethodImplOptions.InternalCall)] public extern static string GetCurrentDirectory (out MonoIOError error); @@ -211,20 +209,33 @@ namespace System.IO public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error); [MethodImplAttribute (MethodImplOptions.InternalCall)] - public extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error); + private extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error); + + public static MonoFileType GetFileType (SafeHandle safeHandle, out MonoIOError error) + { + bool release = false; + try { + safeHandle.DangerousAddRef (ref release); + return GetFileType (safeHandle.DangerousGetHandle (), out error); + } finally { + if (release) + safeHandle.DangerousRelease (); + } + } // // Find file methods // + [MethodImplAttribute (MethodImplOptions.InternalCall)] - public extern static string FindFirst (string path, string pattern, out FileAttributes result_attr, out MonoIOError error, out IntPtr handle); - + public extern static IntPtr FindFirstFile (string path_with_pattern, out string fileName, out int fileAttr, out int error); + [MethodImplAttribute (MethodImplOptions.InternalCall)] - public extern static string FindNext (IntPtr handle, out FileAttributes result_attr, out MonoIOError error); - + public extern static bool FindNextFile (IntPtr hnd, out string fileName, out int fileAttr, out int error); + [MethodImplAttribute (MethodImplOptions.InternalCall)] - public extern static int FindClose (IntPtr handle); - + public extern static bool FindCloseFile (IntPtr hnd); + public static bool Exists (string path, out MonoIOError error) { FileAttributes attrs = GetFileAttributes (path, @@ -296,46 +307,144 @@ namespace System.IO FileShare share, FileOptions options, out MonoIOError error); - + [MethodImplAttribute (MethodImplOptions.InternalCall)] public extern static bool Close (IntPtr handle, out MonoIOError error); [MethodImplAttribute (MethodImplOptions.InternalCall)] - public extern static int Read (IntPtr handle, byte [] dest, + private extern static int Read (IntPtr handle, byte [] dest, int dest_offset, int count, out MonoIOError error); + + public static int Read (SafeHandle safeHandle, byte [] dest, + int dest_offset, int count, + out MonoIOError error) + { + bool release = false; + try { + safeHandle.DangerousAddRef (ref release); + return Read (safeHandle.DangerousGetHandle (), dest, dest_offset, count, out error); + } finally { + if (release) + safeHandle.DangerousRelease (); + } + } [MethodImplAttribute (MethodImplOptions.InternalCall)] - public extern static int Write (IntPtr handle, [In] byte [] src, + private extern static int Write (IntPtr handle, [In] byte [] src, int src_offset, int count, out MonoIOError error); + + public static int Write (SafeHandle safeHandle, byte [] src, + int src_offset, int count, + out MonoIOError error) + { + bool release = false; + try { + safeHandle.DangerousAddRef (ref release); + return Write (safeHandle.DangerousGetHandle (), src, src_offset, count, out error); + } finally { + if (release) + safeHandle.DangerousRelease (); + } + } [MethodImplAttribute (MethodImplOptions.InternalCall)] - public extern static long Seek (IntPtr handle, long offset, + private extern static long Seek (IntPtr handle, long offset, SeekOrigin origin, out MonoIOError error); + + public static long Seek (SafeHandle safeHandle, long offset, + SeekOrigin origin, + out MonoIOError error) + { + bool release = false; + try { + safeHandle.DangerousAddRef (ref release); + return Seek (safeHandle.DangerousGetHandle (), offset, origin, out error); + } finally { + if (release) + safeHandle.DangerousRelease (); + } + } [MethodImplAttribute (MethodImplOptions.InternalCall)] - public extern static bool Flush (IntPtr handle, + private extern static bool Flush (IntPtr handle, out MonoIOError error); + public static bool Flush (SafeHandle safeHandle, + out MonoIOError error) + { + bool release = false; + try { + safeHandle.DangerousAddRef (ref release); + return Flush (safeHandle.DangerousGetHandle (), out error); + } finally { + if (release) + safeHandle.DangerousRelease (); + } + } + [MethodImplAttribute (MethodImplOptions.InternalCall)] - public extern static long GetLength (IntPtr handle, + private extern static long GetLength (IntPtr handle, out MonoIOError error); + public static long GetLength (SafeHandle safeHandle, + out MonoIOError error) + { + bool release = false; + try { + safeHandle.DangerousAddRef (ref release); + return GetLength (safeHandle.DangerousGetHandle (), out error); + } finally { + if (release) + safeHandle.DangerousRelease (); + } + } + [MethodImplAttribute (MethodImplOptions.InternalCall)] - public extern static bool SetLength (IntPtr handle, + private extern static bool SetLength (IntPtr handle, long length, out MonoIOError error); + public static bool SetLength (SafeHandle safeHandle, + long length, + out MonoIOError error) + { + bool release = false; + try { + safeHandle.DangerousAddRef (ref release); + return SetLength (safeHandle.DangerousGetHandle (), length, out error); + } finally { + if (release) + safeHandle.DangerousRelease (); + } + } + [MethodImplAttribute (MethodImplOptions.InternalCall)] - public extern static bool SetFileTime (IntPtr handle, + private extern static bool SetFileTime (IntPtr handle, long creation_time, long last_access_time, long last_write_time, out MonoIOError error); + public static bool SetFileTime (SafeHandle safeHandle, + long creation_time, + long last_access_time, + long last_write_time, + out MonoIOError error) + { + bool release = false; + try { + safeHandle.DangerousAddRef (ref release); + return SetFileTime (safeHandle.DangerousGetHandle (), creation_time, last_access_time, last_write_time, out error); + } finally { + if (release) + safeHandle.DangerousRelease (); + } + } + public static bool SetFileTime (string path, long creation_time, long last_access_time, @@ -401,7 +510,7 @@ namespace System.IO break; } - result = SetFileTime (handle, creation_time, + result = SetFileTime (new SafeFileHandle(handle, false), creation_time, last_access_time, last_write_time, out error); @@ -412,15 +521,43 @@ namespace System.IO } [MethodImplAttribute (MethodImplOptions.InternalCall)] - public extern static void Lock (IntPtr handle, + private extern static void Lock (IntPtr handle, long position, long length, out MonoIOError error); + public static void Lock (SafeHandle safeHandle, + long position, long length, + out MonoIOError error) + { + bool release = false; + try { + safeHandle.DangerousAddRef (ref release); + Lock (safeHandle.DangerousGetHandle (), position, length, out error); + } finally { + if (release) + safeHandle.DangerousRelease (); + } + } + [MethodImplAttribute (MethodImplOptions.InternalCall)] - public extern static void Unlock (IntPtr handle, + private extern static void Unlock (IntPtr handle, long position, long length, out MonoIOError error); + public static void Unlock (SafeHandle safeHandle, + long position, long length, + out MonoIOError error) + { + bool release = false; + try { + safeHandle.DangerousAddRef (ref release); + Unlock (safeHandle.DangerousGetHandle (), position, length, out error); + } finally { + if (release) + safeHandle.DangerousRelease (); + } + } + // console handles public extern static IntPtr ConsoleOutput { @@ -441,11 +578,11 @@ namespace System.IO // pipe handles [MethodImplAttribute (MethodImplOptions.InternalCall)] - public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle); + public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle, out MonoIOError error); [MethodImplAttribute (MethodImplOptions.InternalCall)] public extern static bool DuplicateHandle (IntPtr source_process_handle, IntPtr source_handle, - IntPtr target_process_handle, out IntPtr target_handle, int access, int inherit, int options); + IntPtr target_process_handle, out IntPtr target_handle, int access, int inherit, int options, out MonoIOError error); // path characters @@ -470,7 +607,7 @@ namespace System.IO } [MethodImplAttribute (MethodImplOptions.InternalCall)] - public extern static int GetTempPath(out string path); + extern static void DumpHandles (); } }