errors.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. // Parts of this code are taken from boost::filesystem library
  10. //
  11. //////////////////////////////////////////////////////////////////////////////
  12. //
  13. // Copyright (C) 2002 Beman Dawes
  14. // Copyright (C) 2001 Dietmar Kuehl
  15. // Use, modification, and distribution is subject to the Boost Software
  16. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy
  17. // at http://www.boost.org/LICENSE_1_0.txt)
  18. //
  19. // See library home page at http://www.boost.org/libs/filesystem
  20. //
  21. //////////////////////////////////////////////////////////////////////////////
  22. #ifndef BOOST_INTERPROCESS_ERRORS_HPP
  23. #define BOOST_INTERPROCESS_ERRORS_HPP
  24. #ifndef BOOST_CONFIG_HPP
  25. # include <boost/config.hpp>
  26. #endif
  27. #
  28. #if defined(BOOST_HAS_PRAGMA_ONCE)
  29. # pragma once
  30. #endif
  31. #include <boost/interprocess/detail/config_begin.hpp>
  32. #include <boost/interprocess/detail/workaround.hpp>
  33. #include <string>
  34. #if defined (BOOST_INTERPROCESS_WINDOWS)
  35. # include <boost/interprocess/detail/win32_api.hpp>
  36. #else
  37. # ifdef BOOST_HAS_UNISTD_H
  38. # include <cerrno> //Errors
  39. # include <cstring> //strerror
  40. # else //ifdef BOOST_HAS_UNISTD_H
  41. # error Unknown platform
  42. # endif //ifdef BOOST_HAS_UNISTD_H
  43. #endif //#if defined (BOOST_INTERPROCESS_WINDOWS)
  44. //!\file
  45. //!Describes the error numbering of interprocess classes
  46. namespace boost {
  47. namespace interprocess {
  48. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  49. inline int system_error_code() // artifact of POSIX and WINDOWS error reporting
  50. {
  51. #if defined (BOOST_INTERPROCESS_WINDOWS)
  52. return winapi::get_last_error();
  53. #else
  54. return errno; // GCC 3.1 won't accept ::errno
  55. #endif
  56. }
  57. #if defined (BOOST_INTERPROCESS_WINDOWS)
  58. inline void fill_system_message(int sys_err_code, std::string &str)
  59. {
  60. void *lpMsgBuf;
  61. unsigned long ret = winapi::format_message(
  62. winapi::format_message_allocate_buffer |
  63. winapi::format_message_from_system |
  64. winapi::format_message_ignore_inserts,
  65. 0,
  66. sys_err_code,
  67. winapi::make_lang_id(winapi::lang_neutral, winapi::sublang_default), // Default language
  68. reinterpret_cast<char *>(&lpMsgBuf),
  69. 0,
  70. 0
  71. );
  72. if (ret != 0){
  73. str += static_cast<const char*>(lpMsgBuf);
  74. winapi::local_free( lpMsgBuf ); // free the buffer
  75. while ( str.size()
  76. && (str[str.size()-1] == '\n' || str[str.size()-1] == '\r') )
  77. str.erase( str.size()-1 );
  78. }
  79. else{
  80. str += "WinApi FormatMessage returned error";
  81. }
  82. }
  83. # else
  84. inline void fill_system_message( int system_error, std::string &str)
  85. { str = std::strerror(system_error); }
  86. # endif
  87. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  88. enum error_code_t
  89. {
  90. no_error = 0,
  91. system_error, // system generated error; if possible, is translated
  92. // to one of the more specific errors below.
  93. other_error, // library generated error
  94. security_error, // includes access rights, permissions failures
  95. read_only_error,
  96. io_error,
  97. path_error,
  98. not_found_error,
  99. // not_directory_error,
  100. busy_error, // implies trying again might succeed
  101. already_exists_error,
  102. not_empty_error,
  103. is_directory_error,
  104. out_of_space_error,
  105. out_of_memory_error,
  106. out_of_resource_error,
  107. lock_error,
  108. sem_error,
  109. mode_error,
  110. size_error,
  111. corrupted_error,
  112. not_such_file_or_directory,
  113. invalid_argument,
  114. timeout_when_locking_error,
  115. timeout_when_waiting_error,
  116. owner_dead_error
  117. };
  118. typedef int native_error_t;
  119. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  120. struct ec_xlate
  121. {
  122. native_error_t sys_ec;
  123. error_code_t ec;
  124. };
  125. static const ec_xlate ec_table[] =
  126. {
  127. #if defined (BOOST_INTERPROCESS_WINDOWS)
  128. { /*ERROR_ACCESS_DENIED*/5L, security_error },
  129. { /*ERROR_INVALID_ACCESS*/12L, security_error },
  130. { /*ERROR_SHARING_VIOLATION*/32L, security_error },
  131. { /*ERROR_LOCK_VIOLATION*/33L, security_error },
  132. { /*ERROR_LOCKED*/212L, security_error },
  133. { /*ERROR_NOACCESS*/998L, security_error },
  134. { /*ERROR_WRITE_PROTECT*/19L, read_only_error },
  135. { /*ERROR_NOT_READY*/21L, io_error },
  136. { /*ERROR_SEEK*/25L, io_error },
  137. { /*ERROR_READ_FAULT*/30L, io_error },
  138. { /*ERROR_WRITE_FAULT*/29L, io_error },
  139. { /*ERROR_CANTOPEN*/1011L, io_error },
  140. { /*ERROR_CANTREAD*/1012L, io_error },
  141. { /*ERROR_CANTWRITE*/1013L, io_error },
  142. { /*ERROR_DIRECTORY*/267L, path_error },
  143. { /*ERROR_INVALID_NAME*/123L, path_error },
  144. { /*ERROR_FILE_NOT_FOUND*/2L, not_found_error },
  145. { /*ERROR_PATH_NOT_FOUND*/3L, not_found_error },
  146. { /*ERROR_DEV_NOT_EXIST*/55L, not_found_error },
  147. { /*ERROR_DEVICE_IN_USE*/2404L, busy_error },
  148. { /*ERROR_OPEN_FILES*/2401L, busy_error },
  149. { /*ERROR_BUSY_DRIVE*/142L, busy_error },
  150. { /*ERROR_BUSY*/170L, busy_error },
  151. { /*ERROR_FILE_EXISTS*/80L, already_exists_error },
  152. { /*ERROR_ALREADY_EXISTS*/183L, already_exists_error },
  153. { /*ERROR_DIR_NOT_EMPTY*/145L, not_empty_error },
  154. { /*ERROR_HANDLE_DISK_FULL*/39L, out_of_space_error },
  155. { /*ERROR_DISK_FULL*/112L, out_of_space_error },
  156. { /*ERROR_OUTOFMEMORY*/14L, out_of_memory_error },
  157. { /*ERROR_NOT_ENOUGH_MEMORY*/8L, out_of_memory_error },
  158. { /*ERROR_TOO_MANY_OPEN_FILES*/4L, out_of_resource_error },
  159. { /*ERROR_INVALID_ADDRESS*/487L, busy_error }
  160. #else //#if defined (BOOST_INTERPROCESS_WINDOWS)
  161. { EACCES, security_error },
  162. { EROFS, read_only_error },
  163. { EIO, io_error },
  164. { ENAMETOOLONG, path_error },
  165. { ENOENT, not_found_error },
  166. // { ENOTDIR, not_directory_error },
  167. { EAGAIN, busy_error },
  168. { EBUSY, busy_error },
  169. { ETXTBSY, busy_error },
  170. { EEXIST, already_exists_error },
  171. { ENOTEMPTY, not_empty_error },
  172. { EISDIR, is_directory_error },
  173. { ENOSPC, out_of_space_error },
  174. { ENOMEM, out_of_memory_error },
  175. { EMFILE, out_of_resource_error },
  176. { ENOENT, not_such_file_or_directory },
  177. { EINVAL, invalid_argument }
  178. #endif //#if defined (BOOST_INTERPROCESS_WINDOWS)
  179. };
  180. inline error_code_t lookup_error(native_error_t err)
  181. {
  182. const ec_xlate *cur = &ec_table[0],
  183. *end = cur + sizeof(ec_table)/sizeof(ec_xlate);
  184. for (;cur != end; ++cur ){
  185. if ( err == cur->sys_ec ) return cur->ec;
  186. }
  187. return system_error; // general system error code
  188. }
  189. struct error_info
  190. {
  191. error_info(error_code_t ec = other_error )
  192. : m_nat(0), m_ec(ec)
  193. {}
  194. error_info(native_error_t sys_err_code)
  195. : m_nat(sys_err_code), m_ec(lookup_error(sys_err_code))
  196. {}
  197. error_info & operator =(error_code_t ec)
  198. {
  199. m_nat = 0;
  200. m_ec = ec;
  201. return *this;
  202. }
  203. error_info & operator =(native_error_t sys_err_code)
  204. {
  205. m_nat = sys_err_code;
  206. m_ec = lookup_error(sys_err_code);
  207. return *this;
  208. }
  209. native_error_t get_native_error()const
  210. { return m_nat; }
  211. error_code_t get_error_code()const
  212. { return m_ec; }
  213. private:
  214. native_error_t m_nat;
  215. error_code_t m_ec;
  216. };
  217. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  218. } // namespace interprocess {
  219. } // namespace boost
  220. #include <boost/interprocess/detail/config_end.hpp>
  221. #endif // BOOST_INTERPROCESS_ERRORS_HPP