SmbException.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System;
  18. using System.IO;
  19. using SharpCifs.Util;
  20. using SharpCifs.Util.Sharpen;
  21. namespace SharpCifs.Smb
  22. {
  23. /// <summary>
  24. /// There are hundreds of error codes that may be returned by a CIFS
  25. /// server.
  26. /// </summary>
  27. /// <remarks>
  28. /// There are hundreds of error codes that may be returned by a CIFS
  29. /// server. Rather than represent each with it's own <code>Exception</code>
  30. /// class, this class represents all of them. For many of the popular
  31. /// error codes, constants and text messages like "The device is not ready"
  32. /// are provided.
  33. /// <p>
  34. /// The jCIFS client maps DOS error codes to NTSTATUS codes. This means that
  35. /// the user may recieve a different error from a legacy server than that of
  36. /// a newer varient such as Windows NT and above. If you should encounter
  37. /// such a case, please report it to jcifs at samba dot org and we will
  38. /// change the mapping.
  39. /// </remarks>
  40. public class SmbException : IOException
  41. {
  42. internal static string GetMessageByCode(int errcode)
  43. {
  44. if (errcode == 0)
  45. {
  46. return "NT_STATUS_SUCCESS";
  47. }
  48. if ((errcode & unchecked((int)(0xC0000000))) == unchecked((int)(0xC0000000)))
  49. {
  50. int min = 1;
  51. int max = NtStatus.NtStatusCodes.Length - 1;
  52. while (max >= min)
  53. {
  54. int mid = (min + max) / 2;
  55. if (errcode > NtStatus.NtStatusCodes[mid])
  56. {
  57. min = mid + 1;
  58. }
  59. else
  60. {
  61. if (errcode < NtStatus.NtStatusCodes[mid])
  62. {
  63. max = mid - 1;
  64. }
  65. else
  66. {
  67. return NtStatus.NtStatusMessages[mid];
  68. }
  69. }
  70. }
  71. }
  72. else
  73. {
  74. int min = 0;
  75. int max = DosError.DosErrorCodes.Length - 1;
  76. while (max >= min)
  77. {
  78. int mid = (min + max) / 2;
  79. if (errcode > DosError.DosErrorCodes[mid][0])
  80. {
  81. min = mid + 1;
  82. }
  83. else
  84. {
  85. if (errcode < DosError.DosErrorCodes[mid][0])
  86. {
  87. max = mid - 1;
  88. }
  89. else
  90. {
  91. return DosError.DosErrorMessages[mid];
  92. }
  93. }
  94. }
  95. }
  96. return "0x" + Hexdump.ToHexString(errcode, 8);
  97. }
  98. internal static int GetStatusByCode(int errcode)
  99. {
  100. if ((errcode & unchecked((int)(0xC0000000))) != 0)
  101. {
  102. return errcode;
  103. }
  104. int min = 0;
  105. int max = DosError.DosErrorCodes.Length - 1;
  106. while (max >= min)
  107. {
  108. int mid = (min + max) / 2;
  109. if (errcode > DosError.DosErrorCodes[mid][0])
  110. {
  111. min = mid + 1;
  112. }
  113. else
  114. {
  115. if (errcode < DosError.DosErrorCodes[mid][0])
  116. {
  117. max = mid - 1;
  118. }
  119. else
  120. {
  121. return DosError.DosErrorCodes[mid][1];
  122. }
  123. }
  124. }
  125. return NtStatus.NtStatusUnsuccessful;
  126. }
  127. internal static string GetMessageByWinerrCode(int errcode)
  128. {
  129. int min = 0;
  130. int max = WinError.WinerrCodes.Length - 1;
  131. while (max >= min)
  132. {
  133. int mid = (min + max) / 2;
  134. if (errcode > WinError.WinerrCodes[mid])
  135. {
  136. min = mid + 1;
  137. }
  138. else
  139. {
  140. if (errcode < WinError.WinerrCodes[mid])
  141. {
  142. max = mid - 1;
  143. }
  144. else
  145. {
  146. return WinError.WinerrMessages[mid];
  147. }
  148. }
  149. }
  150. return errcode + string.Empty;
  151. }
  152. private int _status;
  153. private Exception _rootCause;
  154. public SmbException()
  155. {
  156. }
  157. internal SmbException(int errcode, Exception rootCause) : base(GetMessageByCode(errcode
  158. ))
  159. {
  160. _status = GetStatusByCode(errcode);
  161. this._rootCause = rootCause;
  162. }
  163. public SmbException(string msg) : base(msg)
  164. {
  165. _status = NtStatus.NtStatusUnsuccessful;
  166. }
  167. public SmbException(string msg, Exception rootCause) : base(msg)
  168. {
  169. this._rootCause = rootCause;
  170. _status = NtStatus.NtStatusUnsuccessful;
  171. }
  172. public SmbException(int errcode, bool winerr) : base(winerr ? GetMessageByWinerrCode
  173. (errcode) : GetMessageByCode(errcode))
  174. {
  175. _status = winerr ? errcode : GetStatusByCode(errcode);
  176. }
  177. public virtual int GetNtStatus()
  178. {
  179. return _status;
  180. }
  181. public virtual Exception GetRootCause()
  182. {
  183. return _rootCause;
  184. }
  185. public override string ToString()
  186. {
  187. if (_rootCause != null)
  188. {
  189. StringWriter sw = new StringWriter();
  190. PrintWriter pw = new PrintWriter(sw);
  191. Runtime.PrintStackTrace(_rootCause, pw);
  192. return base.ToString() + "\n" + sw;
  193. }
  194. return base.ToString();
  195. }
  196. }
  197. }