file_wrapper.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. , m_mode(read_only)
  81. { this->swap(moved); }
  82. //!Moves the ownership of "moved"'s file to *this.
  83. //!After the call, "moved" does not represent any file.
  84. //!Does not throw
  85. file_wrapper &operator=(BOOST_RV_REF(file_wrapper) moved)
  86. {
  87. file_wrapper tmp(boost::move(moved));
  88. this->swap(tmp);
  89. return *this;
  90. }
  91. //!Swaps to file_wrappers.
  92. //!Does not throw
  93. void swap(file_wrapper &other);
  94. //!Erases a file from the system.
  95. //!Returns false on error. Never throws
  96. static bool remove(const char *name);
  97. //!Sets the size of the file
  98. void truncate(offset_t length);
  99. //!Closes the
  100. //!file
  101. ~file_wrapper();
  102. //!Returns the name of the file
  103. //!used in the constructor
  104. const char *get_name() const;
  105. //!Returns the name of the file
  106. //!used in the constructor
  107. bool get_size(offset_t &size) const;
  108. //!Returns access mode
  109. //!used in the constructor
  110. mode_t get_mode() const;
  111. //!Get mapping handle
  112. //!to use with mapped_region
  113. mapping_handle_t get_mapping_handle() const;
  114. private:
  115. //!Closes a previously opened file mapping. Never throws.
  116. void priv_close();
  117. //!Closes a previously opened file mapping. Never throws.
  118. template <class CharT>
  119. bool priv_open_or_create(ipcdetail::create_enum_t type, const CharT *filename, mode_t mode, const permissions &perm);
  120. file_handle_t m_handle;
  121. mode_t m_mode;
  122. };
  123. inline file_wrapper::file_wrapper()
  124. : m_handle(file_handle_t(ipcdetail::invalid_file()))
  125. , m_mode(read_only)
  126. {}
  127. inline file_wrapper::~file_wrapper()
  128. { this->priv_close(); }
  129. inline bool file_wrapper::get_size(offset_t &size) const
  130. { return get_file_size((file_handle_t)m_handle, size); }
  131. inline void file_wrapper::swap(file_wrapper &other)
  132. {
  133. (simple_swap)(m_handle, other.m_handle);
  134. (simple_swap)(m_mode, other.m_mode);
  135. }
  136. inline mapping_handle_t file_wrapper::get_mapping_handle() const
  137. { return mapping_handle_from_file_handle(m_handle); }
  138. inline mode_t file_wrapper::get_mode() const
  139. { return m_mode; }
  140. template <class CharT>
  141. inline bool file_wrapper::priv_open_or_create
  142. ( ipcdetail::create_enum_t type, const CharT *filename, mode_t mode, const permissions &perm)
  143. {
  144. if(mode != read_only && mode != read_write){
  145. error_info err(mode_error);
  146. throw interprocess_exception(err);
  147. }
  148. //Open file existing native API to obtain the handle
  149. switch(type){
  150. case ipcdetail::DoOpen:
  151. m_handle = open_existing_file(filename, mode);
  152. break;
  153. case ipcdetail::DoCreate:
  154. m_handle = create_new_file(filename, mode, perm);
  155. break;
  156. case ipcdetail::DoOpenOrCreate:
  157. m_handle = create_or_open_file(filename, mode, perm);
  158. break;
  159. default:
  160. {
  161. error_info err = other_error;
  162. throw interprocess_exception(err);
  163. }
  164. }
  165. //Check for error
  166. if(m_handle == invalid_file()){
  167. error_info err = system_error_code();
  168. throw interprocess_exception(err);
  169. }
  170. m_mode = mode;
  171. return true;
  172. }
  173. inline bool file_wrapper::remove(const char *filename)
  174. { return delete_file(filename); }
  175. inline void file_wrapper::truncate(offset_t length)
  176. {
  177. if(!truncate_file(m_handle, (std::size_t)length)){
  178. error_info err(system_error_code());
  179. throw interprocess_exception(err);
  180. }
  181. }
  182. inline void file_wrapper::priv_close()
  183. {
  184. if(m_handle != invalid_file()){
  185. close_file(m_handle);
  186. m_handle = invalid_file();
  187. }
  188. }
  189. } //namespace ipcdetail{
  190. } //namespace interprocess {
  191. } //namespace boost {
  192. #include <boost/interprocess/detail/config_end.hpp>
  193. #endif //BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP