errors.hpp 7.3 KB

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