file_wrapper.hpp 6.1 KB

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