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
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)
{
return new FileNotFoundException (message, path);
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:
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);
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);
[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);
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,
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,
break;
}
- result = SetFileTime (handle, creation_time,
+ result = SetFileTime (new SafeFileHandle(handle, false), creation_time,
last_access_time,
last_write_time, out error);
}
[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 {
// 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
}
[MethodImplAttribute (MethodImplOptions.InternalCall)]
- public extern static int GetTempPath(out string path);
+ extern static void DumpHandles ();
}
}