file_wrapper.hpp 6.3 KB

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