PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Liquesce-PhaseII.Net4.5/LiquesceSvc/CBFS/CBFSWinUtil.cs

#
C# | 224 lines | 149 code | 29 blank | 46 comment | 9 complexity | 50be259832354a8bef91c94a351be95a MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, AGPL-1.0
  1. #region Copyright (C)
  2. // ---------------------------------------------------------------------------------------------------------------
  3. // <copyright file="CBFSWinUtil.cs" company="Smurf-IV">
  4. //
  5. // Copyright (C) 2013-2014 Simon Coghlan (Aka Smurf-IV)
  6. //
  7. // This program is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation, either version 2 of the License, or
  10. // any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program. If not, see http://www.gnu.org/licenses/.
  19. // </copyright>
  20. // <summary>
  21. // Email: http://www.codeplex.com/site/users/view/smurfiv
  22. // </summary>
  23. // --------------------------------------------------------------------------------------------------------------------
  24. #endregion Copyright (C)
  25. using System;
  26. using System.ComponentModel;
  27. using System.Diagnostics;
  28. using System.Net.Sockets;
  29. using System.Runtime.InteropServices;
  30. using CallbackFS;
  31. using LiquesceSvc;
  32. using NLog;
  33. namespace CBFS
  34. {
  35. internal static class CBFSWinUtil
  36. {
  37. static private readonly Logger Log = LogManager.GetCurrentClassLogger();
  38. public static uint HiWord(uint number)
  39. {
  40. return (((number & 0x80000000) == 0x80000000) ? number >> 16 : (number >> 16) & 0xffff);
  41. }
  42. public static uint LoWord(uint number)
  43. {
  44. return number & 0xffff;
  45. }
  46. /// <summary>
  47. /// If your application needs to report some error when processing the callback, it should throw ECBFSError exception.
  48. /// The application must pass the error code with the exception by passing the error code as a parameter to
  49. /// ECBFSError constructor.
  50. /// Callback File System API will catch ECBFSError exception and extract the error code.
  51. /// The error code will be reported to the operating system.
  52. /// </summary>
  53. /// <param name="ex"></param>
  54. [DebuggerHidden]
  55. public static void BestAttemptToECBFSError(Exception ex)
  56. {
  57. Log.ErrorException("CBFSWinError", ex);
  58. Win32Exception win32Exception = ex as Win32Exception;
  59. if (win32Exception != null)
  60. {
  61. throw new ECBFSError(ex.Message, (uint)win32Exception.NativeErrorCode);
  62. }
  63. SocketException socketException = ex.InnerException as SocketException;
  64. if (socketException != null)
  65. {
  66. throw new ECBFSError(socketException.Message, (uint)socketException.ErrorCode);
  67. }
  68. uint HrForException = (uint)Marshal.GetHRForException(ex);
  69. throw new ECBFSError(ex.Message, (HiWord(HrForException) == 0x8007) ? LoWord(HrForException) : ERROR_EXCEPTION_IN_SERVICE);
  70. }
  71. /// <summary>
  72. /// Could use reflection to find out the API call name
  73. /// But then that would be used for every call so will probably be detremental to the string handling, and the .Net calls
  74. /// </summary>
  75. /// <param name="functionName">String to log out for this call</param>
  76. /// <param name="act">What to call from the derived class</param>
  77. [DebuggerHidden]
  78. public static void Invoke(string functionName, Action act)
  79. {
  80. Log.Trace("{0} IN", functionName);
  81. try
  82. {
  83. act();
  84. }
  85. catch (Exception ex)
  86. {
  87. BestAttemptToECBFSError(ex);
  88. }
  89. finally
  90. {
  91. Log.Trace("{0} OUT", functionName);
  92. }
  93. }
  94. // ReSharper disable InconsistentNaming
  95. // ReSharper disable MemberCanBePrivate.Global
  96. #pragma warning disable 169
  97. #region File Operation Errors
  98. // ReSharper disable UnusedMember.Global
  99. // Check http://msdn.microsoft.com/en-us/library/ms819772.aspx (WinError.h) for error codes
  100. // From WinError.h -> http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspx
  101. public const int ERROR_FILE_NOT_FOUND = 2; // MessageText: The system cannot find the file specified.
  102. public const int ERROR_PATH_NOT_FOUND = 3; // MessageText: The system cannot find the path specified.
  103. public const int ERROR_ACCESS_DENIED = 5; // MessageText: Access is denied.
  104. public const int ERROR_SHARING_VIOLATION = 32;
  105. public const int ERROR_FILE_EXISTS = 80;
  106. public const int ERROR_CALL_NOT_IMPLEMENTED = 120;
  107. public const int ERROR_DISK_FULL = 112; // There is not enough space on the disk.
  108. public const int ERROR_INVALID_NAME = 123;
  109. public const int ERROR_DIR_NOT_EMPTY = 145; // MessageText: The directory is not empty.
  110. public const int ERROR_ALREADY_EXISTS = 183; // MessageText: Cannot create a file when that file already exists.
  111. public const int ERROR_EXCEPTION_IN_SERVICE = 1064;
  112. // An exception occurred in the service when handling the control request.
  113. public const int ERROR_FILE_READ_ONLY = 6009; // The specified file is read only.
  114. public const int ERROR_SUCCESS = 0;
  115. public const int ERROR_NOACCESS = 998; // Invalid access to memory location.
  116. public const int ERROR_NOT_SUPPORTED = 50; // The request is not supported.
  117. public const int ERROR_INVALID_PARAMETER = 87; // The parameter is incorrect.
  118. public const int ERROR_INVALID_HANDLE = 1609; // Handle is in an invalid state.
  119. public const int ERROR_NOT_LOCKED = 158; // The segment is already unlocked.
  120. public const int ERROR_NO_SYSTEM_RESOURCES = 1450;// Insufficient system resources exist to complete the requested service.
  121. public const int ERROR_NOT_ENOUGH_MEMORY = 8; // Not enough storage is available to process this command.
  122. public const int ERROR_MORE_DATA = 234; // More data is available.
  123. public const int ERROR_INSUFFICIENT_BUFFER = 122;// The data area passed to a system call is too small.
  124. public const int ERROR_NO_MORE_FILES = 18; // There are no more files.
  125. public const int ERROR_INVALID_FUNCTION = 1; // Incorrect function.
  126. public const int ERROR_HANDLE_EOF = 38; // Reached the end of the file.
  127. public const int ERROR_DISK_CORRUPT = 1393; // The disk structure is corrupted and unreadable.
  128. public const int ERROR_BAD_COMMAND = 22; // The device does not recognize the command.
  129. public const int ERROR_CANNOT_MAKE = 82; // The directory or file cannot be created.
  130. public const int ERROR_PROC_NOT_FOUND = 127; // The specified procedure could not be found.
  131. public const int ERROR_OPERATION_ABORTED = 995; // The I/O operation has been aborted because of either a thread exit or an application request.
  132. public const int ERROR_IO_DEVICE = 1117; // The request could not be performed because of an I/O device error.
  133. // public const uint TYPE_E_IOERROR = 0;
  134. public const int ERROR_BAD_UNIT = 20; // The system cannot find the device specified.
  135. public const int ERROR_BAD_ARGUMENTS = 160; // One or more arguments are not correct.
  136. public const int ERROR_BAD_EXE_FORMAT = 193; // %1 is not a valid Win32 application.
  137. public const int ERROR_WAIT_NO_CHILDREN = 128; // There are no child processes to wait for.
  138. public const int ERROR_RETRY = 1237; // The operation could not be completed. A retry should be performed.
  139. public const int ERROR_INVALID_ADDRESS = 487; // Attempt to access invalid address.
  140. public const int ERROR_BUSY = 170; // The requested resource is in use.
  141. public const int ERROR_DIRECTORY = 267; // The directory name is invalid.
  142. public const int ERROR_TOO_MANY_OPEN_FILES = 4; // The system cannot open the file.
  143. public const int ERROR_EA_TABLE_FULL = 277; // The extended attribute table file is full.
  144. public const int ERROR_FILE_INVALID = 1006; // The volume for a file has been externally altered so that the opened file is no longer valid.
  145. public const int ERROR_CONNECTION_UNAVAIL = 1201;// The device is not currently connected but it is a remembered connection.
  146. public const int ERROR_TOO_MANY_LINKS = 1142; // An attempt was made to create more links on a file than the file system supports.
  147. public const int ERROR_BROKEN_PIPE = 109; // The pipe has been ended.
  148. public const int ERROR_ARITHMETIC_OVERFLOW = 534;// Arithmetic result exceeded 32 bits.
  149. public const int ERROR_POSSIBLE_DEADLOCK = 1131;// A potential deadlock condition has been detected.
  150. public const int ERROR_BUFFER_OVERFLOW = 111; // The file name is too long.
  151. public const int ERROR_TOO_MANY_SEMAPHORES = 100;// Cannot create another system semaphore.
  152. public const int ERROR_ARENA_TRASHED = 7; // The storage control blocks were destroyed.
  153. public const int ERROR_INVALID_BLOCK = 9; // The storage control block address is invalid.
  154. public const int ERROR_BAD_ENVIRONMENT = 10; // The environment is incorrect.
  155. public const int ERROR_FILENAME_EXCED_RANGE = 206;// The filename or extension is too long.
  156. public const int ERROR_NOT_READY = 21; // The device is not ready.
  157. public const int ERROR_FILE_OFFLINE = 4350; // This file is currently not available for use on this computer.
  158. public const int ERROR_REMOTE_STORAGE_NOT_ACTIVE = 4351;// The remote storage service is not operational at this time.
  159. public const int ERROR_NO_SUCH_PRIVILEGE = 1313;// A specified privilege does not exist.
  160. public const int ERROR_PRIVILEGE_NOT_HELD = 1314;// A required privilege is not held by the client.
  161. public const int ERROR_CANNOT_IMPERSONATE = 1368;// Unable to impersonate using a named pipe until data has been read from that pipe.
  162. public const int ERROR_WRITE_PROTECT = 19; // The media is write protected.
  163. public const int ERROR_LOGON_FAILURE = 1326; // Logon failure: unknown user name or bad password.
  164. public const int ERROR_NO_SECURITY_ON_OBJECT = 1350; // Unable to perform a security operation on an object that has no associated security.
  165. #endregion File Operation Errors
  166. #region Win32 Constants for file controls
  167. public const uint FILE_SHARE_READ = 0x00000001;
  168. public const uint FILE_SHARE_WRITE = 0x00000002;
  169. public const uint FILE_SHARE_DELETE = 0x00000004;
  170. public const uint CREATE_NEW = 1;
  171. public const uint CREATE_ALWAYS = 2;
  172. public const uint OPEN_EXISTING = 3;
  173. public const uint OPEN_ALWAYS = 4;
  174. public const uint TRUNCATE_EXISTING = 5;
  175. #endregion Win32 Constants for file controls
  176. // ReSharper restore UnusedMember.Global
  177. #pragma warning restore 169
  178. // ReSharper restore MemberCanBePrivate.Global
  179. // ReSharper restore InconsistentNaming
  180. [DebuggerHidden]
  181. public static void ThrowNotFound(uint attributes)
  182. {
  183. bool isDirectoy = IsDirectory(attributes);
  184. throw new ECBFSError((uint)(isDirectoy ? ERROR_PATH_NOT_FOUND : ERROR_FILE_NOT_FOUND));
  185. }
  186. public static bool IsDirectory(uint attributes)
  187. {
  188. return IsDirectory((NativeFileOps.EFileAttributes)attributes);
  189. }
  190. public static bool IsDirectory(NativeFileOps.EFileAttributes attributes)
  191. {
  192. return (attributes & NativeFileOps.EFileAttributes.Directory) == NativeFileOps.EFileAttributes.Directory;
  193. }
  194. }
  195. }