errors.hpp 7.2 KB

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