filex.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #include <errno.h>
  12. #ifdef AFX_CORE1_SEG
  13. #pragma code_seg(AFX_CORE1_SEG)
  14. #endif
  15. #ifdef _DEBUG
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. #ifdef _DEBUG
  20. static const LPCSTR rgszCFileExceptionCause[] =
  21. {
  22. "none",
  23. "generic",
  24. "fileNotFound",
  25. "badPath",
  26. "tooManyOpenFiles",
  27. "accessDenied",
  28. "invalidFile",
  29. "removeCurrentDir",
  30. "directoryFull",
  31. "badSeek",
  32. "hardIO",
  33. "sharingViolation",
  34. "lockViolation",
  35. "diskFull",
  36. "endOfFile",
  37. };
  38. static const char szUnknown[] = "unknown";
  39. #endif
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CFileException
  42. void PASCAL CFileException::ThrowOsError(LONG lOsError,
  43. LPCTSTR lpszFileName /* = NULL */)
  44. {
  45. if (lOsError != 0)
  46. AfxThrowFileException(CFileException::OsErrorToException(lOsError),
  47. lOsError, lpszFileName);
  48. }
  49. void PASCAL CFileException::ThrowErrno(int nErrno,
  50. LPCTSTR lpszFileName /* = NULL */)
  51. {
  52. if (nErrno != 0)
  53. AfxThrowFileException(CFileException::ErrnoToException(nErrno),
  54. _doserrno, lpszFileName);
  55. }
  56. BOOL CFileException::GetErrorMessage(LPTSTR lpszError, UINT nMaxError,
  57. PUINT pnHelpContext)
  58. {
  59. ASSERT(lpszError != NULL && AfxIsValidString(lpszError, nMaxError));
  60. if (pnHelpContext != NULL)
  61. *pnHelpContext = m_cause + AFX_IDP_FILE_NONE;
  62. CString strMessage;
  63. CString strFileName = m_strFileName;
  64. if (strFileName.IsEmpty())
  65. strFileName.LoadString(AFX_IDS_UNNAMED_FILE);
  66. AfxFormatString1(strMessage,
  67. m_cause + AFX_IDP_FILE_NONE, strFileName);
  68. lstrcpyn(lpszError, strMessage, nMaxError);
  69. return TRUE;
  70. }
  71. /////////////////////////////////////////////////////////////////////////////
  72. // CFileException diagnostics
  73. #ifdef _DEBUG
  74. void CFileException::Dump(CDumpContext& dc) const
  75. {
  76. CObject::Dump(dc);
  77. dc << "m_cause = ";
  78. if (m_cause >= 0 && m_cause < _countof(rgszCFileExceptionCause))
  79. dc << rgszCFileExceptionCause[m_cause];
  80. else
  81. dc << szUnknown;
  82. dc << "\nm_lOsError = " << (void*)m_lOsError;
  83. dc << "\n";
  84. }
  85. #endif
  86. /////////////////////////////////////////////////////////////////////////////
  87. // CFileException helpers
  88. void AFXAPI AfxThrowFileException(int cause, LONG lOsError,
  89. LPCTSTR lpszFileName /* == NULL */)
  90. {
  91. #ifdef _DEBUG
  92. LPCSTR lpsz;
  93. if (cause >= 0 && cause < _countof(rgszCFileExceptionCause))
  94. lpsz = rgszCFileExceptionCause[cause];
  95. else
  96. lpsz = szUnknown;
  97. TRACE3("CFile exception: %hs, File %s, OS error information = %ld.\n",
  98. lpsz, (lpszFileName == NULL) ? _T("Unknown") : lpszFileName, lOsError);
  99. #endif
  100. THROW(new CFileException(cause, lOsError, lpszFileName));
  101. }
  102. int PASCAL CFileException::ErrnoToException(int nErrno)
  103. {
  104. switch(nErrno)
  105. {
  106. case EPERM:
  107. case EACCES:
  108. return CFileException::accessDenied;
  109. case EBADF:
  110. return CFileException::invalidFile;
  111. case EDEADLOCK:
  112. return CFileException::sharingViolation;
  113. case EMFILE:
  114. return CFileException::tooManyOpenFiles;
  115. case ENOENT:
  116. case ENFILE:
  117. return CFileException::fileNotFound;
  118. case ENOSPC:
  119. return CFileException::diskFull;
  120. case EINVAL:
  121. case EIO:
  122. return CFileException::hardIO;
  123. default:
  124. return CFileException::generic;
  125. }
  126. }
  127. int PASCAL CFileException::OsErrorToException(LONG lOsErr)
  128. {
  129. // NT Error codes
  130. switch ((UINT)lOsErr)
  131. {
  132. case NO_ERROR:
  133. return CFileException::none;
  134. case ERROR_FILE_NOT_FOUND:
  135. return CFileException::fileNotFound;
  136. case ERROR_PATH_NOT_FOUND:
  137. return CFileException::badPath;
  138. case ERROR_TOO_MANY_OPEN_FILES:
  139. return CFileException::tooManyOpenFiles;
  140. case ERROR_ACCESS_DENIED:
  141. return CFileException::accessDenied;
  142. case ERROR_INVALID_HANDLE:
  143. return CFileException::fileNotFound;
  144. case ERROR_BAD_FORMAT:
  145. return CFileException::invalidFile;
  146. case ERROR_INVALID_ACCESS:
  147. return CFileException::accessDenied;
  148. case ERROR_INVALID_DRIVE:
  149. return CFileException::badPath;
  150. case ERROR_CURRENT_DIRECTORY:
  151. return CFileException::removeCurrentDir;
  152. case ERROR_NOT_SAME_DEVICE:
  153. return CFileException::badPath;
  154. case ERROR_NO_MORE_FILES:
  155. return CFileException::fileNotFound;
  156. case ERROR_WRITE_PROTECT:
  157. return CFileException::accessDenied;
  158. case ERROR_BAD_UNIT:
  159. return CFileException::hardIO;
  160. case ERROR_NOT_READY:
  161. return CFileException::hardIO;
  162. case ERROR_BAD_COMMAND:
  163. return CFileException::hardIO;
  164. case ERROR_CRC:
  165. return CFileException::hardIO;
  166. case ERROR_BAD_LENGTH:
  167. return CFileException::badSeek;
  168. case ERROR_SEEK:
  169. return CFileException::badSeek;
  170. case ERROR_NOT_DOS_DISK:
  171. return CFileException::invalidFile;
  172. case ERROR_SECTOR_NOT_FOUND:
  173. return CFileException::badSeek;
  174. case ERROR_WRITE_FAULT:
  175. return CFileException::accessDenied;
  176. case ERROR_READ_FAULT:
  177. return CFileException::badSeek;
  178. case ERROR_SHARING_VIOLATION:
  179. return CFileException::sharingViolation;
  180. case ERROR_LOCK_VIOLATION:
  181. return CFileException::lockViolation;
  182. case ERROR_WRONG_DISK:
  183. return CFileException::badPath;
  184. case ERROR_SHARING_BUFFER_EXCEEDED:
  185. return CFileException::tooManyOpenFiles;
  186. case ERROR_HANDLE_EOF:
  187. return CFileException::endOfFile;
  188. case ERROR_HANDLE_DISK_FULL:
  189. return CFileException::diskFull;
  190. case ERROR_DUP_NAME:
  191. return CFileException::badPath;
  192. case ERROR_BAD_NETPATH:
  193. return CFileException::badPath;
  194. case ERROR_NETWORK_BUSY:
  195. return CFileException::accessDenied;
  196. case ERROR_DEV_NOT_EXIST:
  197. return CFileException::badPath;
  198. case ERROR_ADAP_HDW_ERR:
  199. return CFileException::hardIO;
  200. case ERROR_BAD_NET_RESP:
  201. return CFileException::accessDenied;
  202. case ERROR_UNEXP_NET_ERR:
  203. return CFileException::hardIO;
  204. case ERROR_BAD_REM_ADAP:
  205. return CFileException::invalidFile;
  206. case ERROR_NO_SPOOL_SPACE:
  207. return CFileException::directoryFull;
  208. case ERROR_NETNAME_DELETED:
  209. return CFileException::accessDenied;
  210. case ERROR_NETWORK_ACCESS_DENIED:
  211. return CFileException::accessDenied;
  212. case ERROR_BAD_DEV_TYPE:
  213. return CFileException::invalidFile;
  214. case ERROR_BAD_NET_NAME:
  215. return CFileException::badPath;
  216. case ERROR_TOO_MANY_NAMES:
  217. return CFileException::tooManyOpenFiles;
  218. case ERROR_SHARING_PAUSED:
  219. return CFileException::badPath;
  220. case ERROR_REQ_NOT_ACCEP:
  221. return CFileException::accessDenied;
  222. case ERROR_FILE_EXISTS:
  223. return CFileException::accessDenied;
  224. case ERROR_CANNOT_MAKE:
  225. return CFileException::accessDenied;
  226. case ERROR_ALREADY_ASSIGNED:
  227. return CFileException::badPath;
  228. case ERROR_INVALID_PASSWORD:
  229. return CFileException::accessDenied;
  230. case ERROR_NET_WRITE_FAULT:
  231. return CFileException::hardIO;
  232. case ERROR_DISK_CHANGE:
  233. return CFileException::fileNotFound;
  234. case ERROR_DRIVE_LOCKED:
  235. return CFileException::lockViolation;
  236. case ERROR_BUFFER_OVERFLOW:
  237. return CFileException::badPath;
  238. case ERROR_DISK_FULL:
  239. return CFileException::diskFull;
  240. case ERROR_NO_MORE_SEARCH_HANDLES:
  241. return CFileException::tooManyOpenFiles;
  242. case ERROR_INVALID_TARGET_HANDLE:
  243. return CFileException::invalidFile;
  244. case ERROR_INVALID_CATEGORY:
  245. return CFileException::hardIO;
  246. case ERROR_INVALID_NAME:
  247. return CFileException::badPath;
  248. case ERROR_INVALID_LEVEL:
  249. return CFileException::badPath;
  250. case ERROR_NO_VOLUME_LABEL:
  251. return CFileException::badPath;
  252. case ERROR_NEGATIVE_SEEK:
  253. return CFileException::badSeek;
  254. case ERROR_SEEK_ON_DEVICE:
  255. return CFileException::badSeek;
  256. case ERROR_DIR_NOT_ROOT:
  257. return CFileException::badPath;
  258. case ERROR_DIR_NOT_EMPTY:
  259. return CFileException::removeCurrentDir;
  260. case ERROR_LABEL_TOO_LONG:
  261. return CFileException::badPath;
  262. case ERROR_BAD_PATHNAME:
  263. return CFileException::badPath;
  264. case ERROR_LOCK_FAILED:
  265. return CFileException::lockViolation;
  266. case ERROR_BUSY:
  267. return CFileException::accessDenied;
  268. case ERROR_INVALID_ORDINAL:
  269. return CFileException::invalidFile;
  270. case ERROR_ALREADY_EXISTS:
  271. return CFileException::accessDenied;
  272. case ERROR_INVALID_EXE_SIGNATURE:
  273. return CFileException::invalidFile;
  274. case ERROR_BAD_EXE_FORMAT:
  275. return CFileException::invalidFile;
  276. case ERROR_FILENAME_EXCED_RANGE:
  277. return CFileException::badPath;
  278. case ERROR_META_EXPANSION_TOO_LONG:
  279. return CFileException::badPath;
  280. case ERROR_DIRECTORY:
  281. return CFileException::badPath;
  282. case ERROR_OPERATION_ABORTED:
  283. return CFileException::hardIO;
  284. case ERROR_IO_INCOMPLETE:
  285. return CFileException::hardIO;
  286. case ERROR_IO_PENDING:
  287. return CFileException::hardIO;
  288. case ERROR_SWAPERROR:
  289. return CFileException::accessDenied;
  290. default:
  291. return CFileException::generic;
  292. }
  293. }
  294. #ifdef AFX_INIT_SEG
  295. #pragma code_seg(AFX_INIT_SEG)
  296. #endif
  297. IMPLEMENT_DYNAMIC(CFileException, CException)
  298. /////////////////////////////////////////////////////////////////////////////