file_wrapper.hpp 6.2 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. {}
  101. inline file_wrapper::~file_wrapper()
  102. { this->priv_close(); }
  103. inline const char *file_wrapper::get_name() const
  104. { return m_filename.c_str(); }
  105. inline bool file_wrapper::get_size(offset_t &size) const
  106. { return get_file_size((file_handle_t)m_handle, size); }
  107. inline void file_wrapper::swap(file_wrapper &other)
  108. {
  109. (simple_swap)(m_handle, other.m_handle);
  110. (simple_swap)(m_mode, other.m_mode);
  111. m_filename.swap(other.m_filename);
  112. }
  113. inline mapping_handle_t file_wrapper::get_mapping_handle() const
  114. { return mapping_handle_from_file_handle(m_handle); }
  115. inline mode_t file_wrapper::get_mode() const
  116. { return m_mode; }
  117. inline bool file_wrapper::priv_open_or_create
  118. (ipcdetail::create_enum_t type,
  119. const char *filename,
  120. mode_t mode,
  121. const permissions &perm = permissions())
  122. {
  123. m_filename = filename;
  124. if(mode != read_only && mode != read_write){
  125. error_info err(mode_error);
  126. throw interprocess_exception(err);
  127. }
  128. //Open file existing native API to obtain the handle
  129. switch(type){
  130. case ipcdetail::DoOpen:
  131. m_handle = open_existing_file(filename, mode);
  132. break;
  133. case ipcdetail::DoCreate:
  134. m_handle = create_new_file(filename, mode, perm);
  135. break;
  136. case ipcdetail::DoOpenOrCreate:
  137. m_handle = create_or_open_file(filename, mode, perm);
  138. break;
  139. default:
  140. {
  141. error_info err = other_error;
  142. throw interprocess_exception(err);
  143. }
  144. }
  145. //Check for error
  146. if(m_handle == invalid_file()){
  147. error_info err = system_error_code();
  148. throw interprocess_exception(err);
  149. }
  150. m_mode = mode;
  151. return true;
  152. }
  153. inline bool file_wrapper::remove(const char *filename)
  154. { return delete_file(filename); }
  155. inline void file_wrapper::truncate(offset_t length)
  156. {
  157. if(!truncate_file(m_handle, length)){
  158. error_info err(system_error_code());
  159. throw interprocess_exception(err);
  160. }
  161. }
  162. inline void file_wrapper::priv_close()
  163. {
  164. if(m_handle != invalid_file()){
  165. close_file(m_handle);
  166. m_handle = invalid_file();
  167. }
  168. }
  169. } //namespace ipcdetail{
  170. } //namespace interprocess {
  171. } //namespace boost {
  172. #include <boost/interprocess/detail/config_end.hpp>
  173. #endif //BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP