file_wrapper.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-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. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. #include <boost/interprocess/detail/os_file_functions.hpp>
  22. #include <boost/interprocess/creation_tags.hpp>
  23. #include <boost/move/utility_core.hpp>
  24. #include <boost/interprocess/creation_tags.hpp>
  25. #include <boost/interprocess/detail/simple_swap.hpp>
  26. namespace boost {
  27. namespace interprocess {
  28. namespace ipcdetail{
  29. class file_wrapper
  30. {
  31. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  32. BOOST_MOVABLE_BUT_NOT_COPYABLE(file_wrapper)
  33. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  34. public:
  35. //!Default constructor.
  36. //!Represents an empty file_wrapper.
  37. file_wrapper();
  38. //!Creates a file object with name "name" and mode "mode", with the access mode "mode"
  39. //!If the file previously exists, throws an error.
  40. file_wrapper(create_only_t, const char *name, mode_t mode, const permissions &perm = permissions())
  41. { this->priv_open_or_create(ipcdetail::DoCreate, name, mode, perm); }
  42. //!Tries to create a file with name "name" and mode "mode", with the
  43. //!access mode "mode". If the file previously exists, it tries to open it with mode "mode".
  44. //!Otherwise throws an error.
  45. file_wrapper(open_or_create_t, const char *name, mode_t mode, const permissions &perm = permissions())
  46. { this->priv_open_or_create(ipcdetail::DoOpenOrCreate, name, mode, perm); }
  47. //!Tries to open a file with name "name", with the access mode "mode".
  48. //!If the file does not previously exist, it throws an error.
  49. file_wrapper(open_only_t, const char *name, mode_t mode)
  50. { this->priv_open_or_create(ipcdetail::DoOpen, name, mode, permissions()); }
  51. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  52. //!Creates a file object with name "name" and mode "mode", with the access mode "mode"
  53. //!If the file previously exists, throws an error.
  54. //!
  55. //!Note: This function is only available on operating systems with
  56. //! native wchar_t APIs (e.g. Windows).
  57. file_wrapper(create_only_t, const wchar_t *name, mode_t mode, const permissions &perm = permissions())
  58. { this->priv_open_or_create(ipcdetail::DoCreate, name, mode, perm); }
  59. //!Tries to create a file with name "name" and mode "mode", with the
  60. //!access mode "mode". If the file previously exists, it tries to open it with mode "mode".
  61. //!Otherwise throws an error.
  62. //!
  63. //!Note: This function is only available on operating systems with
  64. //! native wchar_t APIs (e.g. Windows).
  65. file_wrapper(open_or_create_t, const wchar_t *name, mode_t mode, const permissions &perm = permissions())
  66. { this->priv_open_or_create(ipcdetail::DoOpenOrCreate, name, mode, perm); }
  67. //!Tries to open a file with name "name", with the access mode "mode".
  68. //!If the file does not previously exist, it throws an error.
  69. //!
  70. //!Note: This function is only available on operating systems with
  71. //! native wchar_t APIs (e.g. Windows).
  72. file_wrapper(open_only_t, const wchar_t *name, mode_t mode)
  73. { this->priv_open_or_create(ipcdetail::DoOpen, name, mode, permissions()); }
  74. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  75. //!Moves the ownership of "moved"'s file to *this.
  76. //!After the call, "moved" does not represent any file.
  77. //!Does not throw
  78. file_wrapper(BOOST_RV_REF(file_wrapper) moved)
  79. : m_handle(file_handle_t(ipcdetail::invalid_file()))
  80. { this->swap(moved); }
  81. //!Moves the ownership of "moved"'s file to *this.
  82. //!After the call, "moved" does not represent any file.
  83. //!Does not throw
  84. file_wrapper &operator=(BOOST_RV_REF(file_wrapper) moved)
  85. {
  86. file_wrapper tmp(boost::move(moved));
  87. this->swap(tmp);
  88. return *this;
  89. }
  90. //!Swaps to file_wrappers.
  91. //!Does not throw
  92. void swap(file_wrapper &other);
  93. //!Erases a file from the system.
  94. //!Returns false on error. Never throws
  95. static bool remove(const char *name);
  96. //!Sets the size of the file
  97. void truncate(offset_t length);
  98. //!Closes the
  99. //!file
  100. ~file_wrapper();
  101. //!Returns the name of the file
  102. //!used in the constructor
  103. const char *get_name() const;
  104. //!Returns the name of the file
  105. //!used in the constructor
  106. bool get_size(offset_t &size) const;
  107. //!Returns access mode
  108. //!used in the constructor
  109. mode_t get_mode() const;
  110. //!Get mapping handle
  111. //!to use with mapped_region
  112. mapping_handle_t get_mapping_handle() const;
  113. private:
  114. //!Closes a previously opened file mapping. Never throws.
  115. void priv_close();
  116. //!Closes a previously opened file mapping. Never throws.
  117. template <class CharT>
  118. bool priv_open_or_create(ipcdetail::create_enum_t type, const CharT *filename, mode_t mode, const permissions &perm);
  119. file_handle_t m_handle;
  120. mode_t m_mode;
  121. };
  122. inline file_wrapper::file_wrapper()
  123. : m_handle(file_handle_t(ipcdetail::invalid_file()))
  124. , m_mode(read_only)
  125. {}
  126. inline file_wrapper::~file_wrapper()
  127. { this->priv_close(); }
  128. inline bool file_wrapper::get_size(offset_t &size) const
  129. { return get_file_size((file_handle_t)m_handle, size); }
  130. inline void file_wrapper::swap(file_wrapper &other)
  131. {
  132. (simple_swap)(m_handle, other.m_handle);
  133. (simple_swap)(m_mode, other.m_mode);
  134. }
  135. inline mapping_handle_t file_wrapper::get_mapping_handle() const
  136. { return mapping_handle_from_file_handle(m_handle); }
  137. inline mode_t file_wrapper::get_mode() const
  138. { return m_mode; }
  139. template <class CharT>
  140. inline bool file_wrapper::priv_open_or_create
  141. ( ipcdetail::create_enum_t type, const CharT *filename, mode_t mode, const permissions &perm)
  142. {
  143. if(mode != read_only && mode != read_write){
  144. error_info err(mode_error);
  145. throw interprocess_exception(err);
  146. }
  147. //Open file existing native API to obtain the handle
  148. switch(type){
  149. case ipcdetail::DoOpen:
  150. m_handle = open_existing_file(filename, mode);
  151. break;
  152. case ipcdetail::DoCreate:
  153. m_handle = create_new_file(filename, mode, perm);
  154. break;
  155. case ipcdetail::DoOpenOrCreate:
  156. m_handle = create_or_open_file(filename, mode, perm);
  157. break;
  158. default:
  159. {
  160. error_info err = other_error;
  161. throw interprocess_exception(err);
  162. }
  163. }
  164. //Check for error
  165. if(m_handle == invalid_file()){
  166. error_info err = system_error_code();
  167. throw interprocess_exception(err);
  168. }
  169. m_mode = mode;
  170. return true;
  171. }
  172. inline bool file_wrapper::remove(const char *filename)
  173. { return delete_file(filename); }
  174. inline void file_wrapper::truncate(offset_t length)
  175. {
  176. if(!truncate_file(m_handle, length)){
  177. error_info err(system_error_code());
  178. throw interprocess_exception(err);
  179. }
  180. }
  181. inline void file_wrapper::priv_close()
  182. {
  183. if(m_handle != invalid_file()){
  184. close_file(m_handle);
  185. m_handle = invalid_file();
  186. }
  187. }
  188. } //namespace ipcdetail{
  189. } //namespace interprocess {
  190. } //namespace boost {
  191. #include <boost/interprocess/detail/config_end.hpp>
  192. #endif //BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP