MonoIO.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. // System.IO.MonoIO.cs: static interface to native filesystem.
  2. //
  3. // Author:
  4. // Dan Lewis ([email protected])
  5. // Dick Porter ([email protected])
  6. //
  7. // (C) 2002
  8. // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Runtime.CompilerServices;
  34. using System.Runtime.InteropServices;
  35. using System.Threading;
  36. using Microsoft.Win32.SafeHandles;
  37. #if MOBILE
  38. using System.IO.IsolatedStorage;
  39. #endif
  40. namespace System.IO
  41. {
  42. unsafe static class MonoIO {
  43. public const int FileAlreadyExistsHResult = unchecked ((int) 0x80070000) | (int)MonoIOError.ERROR_FILE_EXISTS;
  44. public const FileAttributes
  45. InvalidFileAttributes = (FileAttributes)(-1);
  46. public static readonly IntPtr
  47. InvalidHandle = (IntPtr)(-1L);
  48. static bool dump_handles = Environment.GetEnvironmentVariable ("MONO_DUMP_HANDLES_ON_ERROR_TOO_MANY_OPEN_FILES") != null;
  49. // error methods
  50. public static Exception GetException (MonoIOError error)
  51. {
  52. /* This overload is currently only called from
  53. * File.MoveFile(), Directory.Move() and
  54. * Directory.GetCurrentDirectory() -
  55. * everywhere else supplies a path to format
  56. * with the error text.
  57. */
  58. switch(error) {
  59. case MonoIOError.ERROR_ACCESS_DENIED:
  60. return new UnauthorizedAccessException ("Access to the path is denied.");
  61. case MonoIOError.ERROR_FILE_EXISTS:
  62. string message = "Cannot create a file that already exist.";
  63. return new IOException (message, FileAlreadyExistsHResult);
  64. default:
  65. /* Add more mappings here if other
  66. * errors trigger the named but empty
  67. * path bug (see bug 82141.) For
  68. * everything else, fall through to
  69. * the other overload
  70. */
  71. return GetException (String.Empty, error);
  72. }
  73. }
  74. public static Exception GetException (string path,
  75. MonoIOError error)
  76. {
  77. string message;
  78. switch (error) {
  79. // FIXME: add more exception mappings here
  80. case MonoIOError.ERROR_FILE_NOT_FOUND:
  81. message = String.Format ("Could not find file \"{0}\"", path);
  82. return new FileNotFoundException (message, path);
  83. case MonoIOError.ERROR_TOO_MANY_OPEN_FILES:
  84. if (dump_handles)
  85. DumpHandles ();
  86. return new IOException ("Too many open files", unchecked((int)0x80070000) | (int)error);
  87. case MonoIOError.ERROR_PATH_NOT_FOUND:
  88. message = String.Format ("Could not find a part of the path \"{0}\"", path);
  89. return new DirectoryNotFoundException (message);
  90. case MonoIOError.ERROR_ACCESS_DENIED:
  91. message = String.Format ("Access to the path \"{0}\" is denied.", path);
  92. return new UnauthorizedAccessException (message);
  93. case MonoIOError.ERROR_INVALID_HANDLE:
  94. message = String.Format ("Invalid handle to path \"{0}\"", path);
  95. return new IOException (message, unchecked((int)0x80070000) | (int)error);
  96. case MonoIOError.ERROR_INVALID_DRIVE:
  97. message = String.Format ("Could not find the drive '{0}'. The drive might not be ready or might not be mapped.", path);
  98. #if !MOBILE
  99. return new DriveNotFoundException (message);
  100. #else
  101. return new IOException (message, unchecked((int)0x80070000) | (int)error);
  102. #endif
  103. case MonoIOError.ERROR_FILE_EXISTS:
  104. message = String.Format ("Could not create file \"{0}\". File already exists.", path);
  105. return new IOException (message, unchecked((int)0x80070000) | (int)error);
  106. case MonoIOError.ERROR_FILENAME_EXCED_RANGE:
  107. message = String.Format ("Path is too long. Path: {0}", path);
  108. return new PathTooLongException (message);
  109. case MonoIOError.ERROR_INVALID_PARAMETER:
  110. message = String.Format ("Invalid parameter");
  111. return new IOException (message, unchecked((int)0x80070000) | (int)error);
  112. case MonoIOError.ERROR_WRITE_FAULT:
  113. message = String.Format ("Write fault on path {0}", path);
  114. return new IOException (message, unchecked((int)0x80070000) | (int)error);
  115. case MonoIOError.ERROR_SHARING_VIOLATION:
  116. message = String.Format ("Sharing violation on path {0}", path);
  117. return new IOException (message, unchecked((int)0x80070000) | (int)error);
  118. case MonoIOError.ERROR_LOCK_VIOLATION:
  119. message = String.Format ("Lock violation on path {0}", path);
  120. return new IOException (message, unchecked((int)0x80070000) | (int)error);
  121. case MonoIOError.ERROR_HANDLE_DISK_FULL:
  122. message = String.Format ("Disk full. Path {0}", path);
  123. return new IOException (message, unchecked((int)0x80070000) | (int)error);
  124. case MonoIOError.ERROR_DIR_NOT_EMPTY:
  125. message = String.Format ("Directory {0} is not empty", path);
  126. return new IOException (message, unchecked((int)0x80070000) | (int)error);
  127. case MonoIOError.ERROR_ENCRYPTION_FAILED:
  128. return new IOException ("Encryption failed", unchecked((int)0x80070000) | (int)error);
  129. case MonoIOError.ERROR_CANNOT_MAKE:
  130. message = String.Format ("Path {0} is a directory", path);
  131. return new IOException (message, unchecked((int)0x80070000) | (int)error);
  132. case MonoIOError.ERROR_NOT_SAME_DEVICE:
  133. message = "Source and destination are not on the same device";
  134. return new IOException (message, unchecked((int)0x80070000) | (int)error);
  135. case MonoIOError.ERROR_DIRECTORY:
  136. message = "The directory name is invalid";
  137. return new IOException (message, unchecked((int)0x80070000) | (int)error);
  138. default:
  139. message = String.Format ("Win32 IO returned {0}. Path: {1}", error, path);
  140. return new IOException (message, unchecked((int)0x80070000) | (int)error);
  141. }
  142. }
  143. // directory methods
  144. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  145. private unsafe extern static bool CreateDirectory (char* path, out MonoIOError error);
  146. public static bool CreateDirectory (string path, out MonoIOError error)
  147. {
  148. unsafe {
  149. fixed (char* pathChars = path) {
  150. return CreateDirectory (pathChars, out error);
  151. }
  152. }
  153. }
  154. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  155. private unsafe extern static bool RemoveDirectory (char* path, out MonoIOError error);
  156. public static bool RemoveDirectory (string path, out MonoIOError error)
  157. {
  158. unsafe {
  159. fixed (char* pathChars = path) {
  160. return RemoveDirectory (pathChars, out error);
  161. }
  162. }
  163. }
  164. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  165. public extern static string GetCurrentDirectory (out MonoIOError error);
  166. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  167. private unsafe extern static bool SetCurrentDirectory (char* path, out MonoIOError error);
  168. public static bool SetCurrentDirectory (string path, out MonoIOError error)
  169. {
  170. unsafe {
  171. fixed (char* pathChars = path) {
  172. return SetCurrentDirectory (pathChars, out error);
  173. }
  174. }
  175. }
  176. // file methods
  177. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  178. private unsafe extern static bool MoveFile (char* path, char* dest,
  179. out MonoIOError error);
  180. public static bool MoveFile (string path, string dest,
  181. out MonoIOError error)
  182. {
  183. unsafe {
  184. fixed (char* pathChars = path, destChars = dest) {
  185. return MoveFile (pathChars, destChars, out error);
  186. }
  187. }
  188. }
  189. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  190. private unsafe extern static bool CopyFile (char* path, char* dest,
  191. bool overwrite,
  192. out MonoIOError error);
  193. public static bool CopyFile (string path, string dest,
  194. bool overwrite,
  195. out MonoIOError error)
  196. {
  197. unsafe {
  198. fixed (char* pathChars = path, destChars = dest) {
  199. return CopyFile (pathChars, destChars, overwrite, out error);
  200. }
  201. }
  202. }
  203. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  204. private unsafe extern static bool DeleteFile (char* path,
  205. out MonoIOError error);
  206. public static bool DeleteFile (string path,
  207. out MonoIOError error)
  208. {
  209. unsafe {
  210. fixed (char* pathChars = path) {
  211. return DeleteFile (pathChars, out error);
  212. }
  213. }
  214. }
  215. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  216. private unsafe extern static bool ReplaceFile (char* sourceFileName,
  217. char* destinationFileName,
  218. char* destinationBackupFileName,
  219. bool ignoreMetadataErrors,
  220. out MonoIOError error);
  221. public static bool ReplaceFile (string sourceFileName,
  222. string destinationFileName,
  223. string destinationBackupFileName,
  224. bool ignoreMetadataErrors,
  225. out MonoIOError error)
  226. {
  227. unsafe {
  228. fixed (char* sourceFileNameChars = sourceFileName,
  229. destinationFileNameChars = destinationFileName,
  230. destinationBackupFileNameChars = destinationBackupFileName) {
  231. return ReplaceFile (sourceFileNameChars, destinationFileNameChars, destinationBackupFileNameChars, ignoreMetadataErrors, out error);
  232. }
  233. }
  234. }
  235. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  236. private unsafe extern static FileAttributes GetFileAttributes (char* path, out MonoIOError error);
  237. public static FileAttributes GetFileAttributes (string path, out MonoIOError error)
  238. {
  239. unsafe {
  240. fixed (char* pathChars = path) {
  241. return GetFileAttributes (pathChars, out error);
  242. }
  243. }
  244. }
  245. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  246. private extern static bool SetFileAttributes (char* path, FileAttributes attrs, out MonoIOError error);
  247. public static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error)
  248. {
  249. unsafe {
  250. fixed (char* pathChars = path) {
  251. return SetFileAttributes (pathChars, attrs, out error);
  252. }
  253. }
  254. }
  255. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  256. private extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
  257. public static MonoFileType GetFileType (SafeHandle safeHandle, out MonoIOError error)
  258. {
  259. bool release = false;
  260. try {
  261. safeHandle.DangerousAddRef (ref release);
  262. return GetFileType (safeHandle.DangerousGetHandle (), out error);
  263. } finally {
  264. if (release)
  265. safeHandle.DangerousRelease ();
  266. }
  267. }
  268. //
  269. // Find file methods
  270. //
  271. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  272. private unsafe extern static IntPtr FindFirstFile (char* pathWithPattern, out string fileName, out int fileAttr, out int error);
  273. public static IntPtr FindFirstFile (string pathWithPattern, out string fileName, out int fileAttr, out int error)
  274. {
  275. unsafe {
  276. fixed (char* pathWithPatternChars = pathWithPattern) {
  277. return FindFirstFile (pathWithPatternChars, out fileName, out fileAttr, out error);
  278. }
  279. }
  280. }
  281. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  282. public extern static bool FindNextFile (IntPtr hnd, out string fileName, out int fileAttr, out int error);
  283. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  284. public extern static bool FindCloseFile (IntPtr hnd);
  285. public static bool Exists (string path, out MonoIOError error)
  286. {
  287. FileAttributes attrs = GetFileAttributes (path,
  288. out error);
  289. if (attrs == InvalidFileAttributes)
  290. return false;
  291. return true;
  292. }
  293. public static bool ExistsFile (string path,
  294. out MonoIOError error)
  295. {
  296. FileAttributes attrs = GetFileAttributes (path,
  297. out error);
  298. if (attrs == InvalidFileAttributes)
  299. return false;
  300. if ((attrs & FileAttributes.Directory) != 0)
  301. return false;
  302. return true;
  303. }
  304. public static bool ExistsDirectory (string path,
  305. out MonoIOError error)
  306. {
  307. FileAttributes attrs = GetFileAttributes (path,
  308. out error);
  309. // Actually, we are looking for a directory, not a file
  310. if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
  311. error = MonoIOError.ERROR_PATH_NOT_FOUND;
  312. if (attrs == InvalidFileAttributes)
  313. return false;
  314. if ((attrs & FileAttributes.Directory) == 0)
  315. return false;
  316. return true;
  317. }
  318. public static bool ExistsSymlink (string path,
  319. out MonoIOError error)
  320. {
  321. FileAttributes attrs = GetFileAttributes (path,
  322. out error);
  323. if (attrs == InvalidFileAttributes)
  324. return false;
  325. if ((attrs & FileAttributes.ReparsePoint) == 0)
  326. return false;
  327. return true;
  328. }
  329. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  330. private unsafe extern static bool GetFileStat (char* path,
  331. out MonoIOStat stat,
  332. out MonoIOError error);
  333. public static bool GetFileStat (string path,
  334. out MonoIOStat stat,
  335. out MonoIOError error)
  336. {
  337. unsafe {
  338. fixed (char* pathChars = path) {
  339. return GetFileStat (pathChars, out stat, out error);
  340. }
  341. }
  342. }
  343. // handle methods
  344. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  345. private unsafe extern static IntPtr Open (char* filename,
  346. FileMode mode,
  347. FileAccess access,
  348. FileShare share,
  349. FileOptions options,
  350. out MonoIOError error);
  351. public static IntPtr Open (string filename,
  352. FileMode mode,
  353. FileAccess access,
  354. FileShare share,
  355. FileOptions options,
  356. out MonoIOError error)
  357. {
  358. unsafe {
  359. fixed (char* filenameChars = filename) {
  360. return Open (filenameChars, mode, access, share, options, out error);
  361. }
  362. }
  363. }
  364. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  365. public extern static bool Close (IntPtr handle,
  366. out MonoIOError error);
  367. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  368. private extern static int Read (IntPtr handle, byte [] dest,
  369. int dest_offset, int count,
  370. out MonoIOError error);
  371. public static int Read (SafeHandle safeHandle, byte [] dest,
  372. int dest_offset, int count,
  373. out MonoIOError error)
  374. {
  375. bool release = false;
  376. try {
  377. safeHandle.DangerousAddRef (ref release);
  378. return Read (safeHandle.DangerousGetHandle (), dest, dest_offset, count, out error);
  379. } finally {
  380. if (release)
  381. safeHandle.DangerousRelease ();
  382. }
  383. }
  384. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  385. private extern static int Write (IntPtr handle, [In] byte [] src,
  386. int src_offset, int count,
  387. out MonoIOError error);
  388. public static int Write (SafeHandle safeHandle, byte [] src,
  389. int src_offset, int count,
  390. out MonoIOError error)
  391. {
  392. bool release = false;
  393. try {
  394. safeHandle.DangerousAddRef (ref release);
  395. return Write (safeHandle.DangerousGetHandle (), src, src_offset, count, out error);
  396. } finally {
  397. if (release)
  398. safeHandle.DangerousRelease ();
  399. }
  400. }
  401. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  402. private extern static long Seek (IntPtr handle, long offset,
  403. SeekOrigin origin,
  404. out MonoIOError error);
  405. public static long Seek (SafeHandle safeHandle, long offset,
  406. SeekOrigin origin,
  407. out MonoIOError error)
  408. {
  409. bool release = false;
  410. try {
  411. safeHandle.DangerousAddRef (ref release);
  412. return Seek (safeHandle.DangerousGetHandle (), offset, origin, out error);
  413. } finally {
  414. if (release)
  415. safeHandle.DangerousRelease ();
  416. }
  417. }
  418. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  419. private extern static bool Flush (IntPtr handle,
  420. out MonoIOError error);
  421. public static bool Flush (SafeHandle safeHandle,
  422. out MonoIOError error)
  423. {
  424. bool release = false;
  425. try {
  426. safeHandle.DangerousAddRef (ref release);
  427. return Flush (safeHandle.DangerousGetHandle (), out error);
  428. } finally {
  429. if (release)
  430. safeHandle.DangerousRelease ();
  431. }
  432. }
  433. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  434. private extern static long GetLength (IntPtr handle,
  435. out MonoIOError error);
  436. public static long GetLength (SafeHandle safeHandle,
  437. out MonoIOError error)
  438. {
  439. bool release = false;
  440. try {
  441. safeHandle.DangerousAddRef (ref release);
  442. return GetLength (safeHandle.DangerousGetHandle (), out error);
  443. } finally {
  444. if (release)
  445. safeHandle.DangerousRelease ();
  446. }
  447. }
  448. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  449. private extern static bool SetLength (IntPtr handle,
  450. long length,
  451. out MonoIOError error);
  452. public static bool SetLength (SafeHandle safeHandle,
  453. long length,
  454. out MonoIOError error)
  455. {
  456. bool release = false;
  457. try {
  458. safeHandle.DangerousAddRef (ref release);
  459. return SetLength (safeHandle.DangerousGetHandle (), length, out error);
  460. } finally {
  461. if (release)
  462. safeHandle.DangerousRelease ();
  463. }
  464. }
  465. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  466. private extern static bool SetFileTime (IntPtr handle,
  467. long creation_time,
  468. long last_access_time,
  469. long last_write_time,
  470. out MonoIOError error);
  471. public static bool SetFileTime (SafeHandle safeHandle,
  472. long creation_time,
  473. long last_access_time,
  474. long last_write_time,
  475. out MonoIOError error)
  476. {
  477. bool release = false;
  478. try {
  479. safeHandle.DangerousAddRef (ref release);
  480. return SetFileTime (safeHandle.DangerousGetHandle (), creation_time, last_access_time, last_write_time, out error);
  481. } finally {
  482. if (release)
  483. safeHandle.DangerousRelease ();
  484. }
  485. }
  486. public static bool SetFileTime (string path,
  487. long creation_time,
  488. long last_access_time,
  489. long last_write_time,
  490. out MonoIOError error)
  491. {
  492. return SetFileTime (path,
  493. 0,
  494. creation_time,
  495. last_access_time,
  496. last_write_time,
  497. DateTime.MinValue,
  498. out error);
  499. }
  500. public static bool SetCreationTime (string path,
  501. DateTime dateTime,
  502. out MonoIOError error)
  503. {
  504. return SetFileTime (path, 1, -1, -1, -1, dateTime, out error);
  505. }
  506. public static bool SetLastAccessTime (string path,
  507. DateTime dateTime,
  508. out MonoIOError error)
  509. {
  510. return SetFileTime (path, 2, -1, -1, -1, dateTime, out error);
  511. }
  512. public static bool SetLastWriteTime (string path,
  513. DateTime dateTime,
  514. out MonoIOError error)
  515. {
  516. return SetFileTime (path, 3, -1, -1, -1, dateTime, out error);
  517. }
  518. public static bool SetFileTime (string path,
  519. int type,
  520. long creation_time,
  521. long last_access_time,
  522. long last_write_time,
  523. DateTime dateTime,
  524. out MonoIOError error)
  525. {
  526. IntPtr handle;
  527. bool result;
  528. handle = Open (path, FileMode.Open,
  529. FileAccess.ReadWrite,
  530. FileShare.ReadWrite, FileOptions.None, out error);
  531. if (handle == MonoIO.InvalidHandle)
  532. return false;
  533. switch (type) {
  534. case 1:
  535. creation_time = dateTime.ToFileTime ();
  536. break;
  537. case 2:
  538. last_access_time = dateTime.ToFileTime ();
  539. break;
  540. case 3:
  541. last_write_time = dateTime.ToFileTime ();
  542. break;
  543. }
  544. result = SetFileTime (new SafeFileHandle(handle, false), creation_time,
  545. last_access_time,
  546. last_write_time, out error);
  547. MonoIOError ignore_error;
  548. Close (handle, out ignore_error);
  549. return result;
  550. }
  551. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  552. private extern static void Lock (IntPtr handle,
  553. long position, long length,
  554. out MonoIOError error);
  555. public static void Lock (SafeHandle safeHandle,
  556. long position, long length,
  557. out MonoIOError error)
  558. {
  559. bool release = false;
  560. try {
  561. safeHandle.DangerousAddRef (ref release);
  562. Lock (safeHandle.DangerousGetHandle (), position, length, out error);
  563. } finally {
  564. if (release)
  565. safeHandle.DangerousRelease ();
  566. }
  567. }
  568. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  569. private extern static void Unlock (IntPtr handle,
  570. long position, long length,
  571. out MonoIOError error);
  572. public static void Unlock (SafeHandle safeHandle,
  573. long position, long length,
  574. out MonoIOError error)
  575. {
  576. bool release = false;
  577. try {
  578. safeHandle.DangerousAddRef (ref release);
  579. Unlock (safeHandle.DangerousGetHandle (), position, length, out error);
  580. } finally {
  581. if (release)
  582. safeHandle.DangerousRelease ();
  583. }
  584. }
  585. // console handles
  586. public extern static IntPtr ConsoleOutput {
  587. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  588. get;
  589. }
  590. public extern static IntPtr ConsoleInput {
  591. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  592. get;
  593. }
  594. public extern static IntPtr ConsoleError {
  595. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  596. get;
  597. }
  598. // pipe handles
  599. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  600. public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle, out MonoIOError error);
  601. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  602. public extern static bool DuplicateHandle (IntPtr source_process_handle, IntPtr source_handle,
  603. IntPtr target_process_handle, out IntPtr target_handle, int access, int inherit, int options, out MonoIOError error);
  604. // path characters
  605. public extern static char VolumeSeparatorChar {
  606. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  607. get;
  608. }
  609. public extern static char DirectorySeparatorChar {
  610. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  611. get;
  612. }
  613. public extern static char AltDirectorySeparatorChar {
  614. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  615. get;
  616. }
  617. public extern static char PathSeparator {
  618. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  619. get;
  620. }
  621. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  622. extern static void DumpHandles ();
  623. }
  624. }