1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP
12 #define BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
24 #include <boost/interprocess/detail/os_file_functions.hpp>
25 #include <boost/interprocess/creation_tags.hpp>
26 #include <boost/move/utility_core.hpp>
27 #include <boost/interprocess/creation_tags.hpp>
28 #include <boost/interprocess/detail/simple_swap.hpp>
31 namespace interprocess {
36 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
37 BOOST_MOVABLE_BUT_NOT_COPYABLE(file_wrapper)
38 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
41 //!Default constructor.
42 //!Represents an empty file_wrapper.
45 //!Creates a file object with name "name" and mode "mode", with the access mode "mode"
46 //!If the file previously exists, throws an error.
47 file_wrapper(create_only_t, const char *name, mode_t mode, const permissions &perm = permissions())
48 { this->priv_open_or_create(ipcdetail::DoCreate, name, mode, perm); }
50 //!Tries to create a file with name "name" and mode "mode", with the
51 //!access mode "mode". If the file previously exists, it tries to open it with mode "mode".
52 //!Otherwise throws an error.
53 file_wrapper(open_or_create_t, const char *name, mode_t mode, const permissions &perm = permissions())
54 { this->priv_open_or_create(ipcdetail::DoOpenOrCreate, name, mode, perm); }
56 //!Tries to open a file with name "name", with the access mode "mode".
57 //!If the file does not previously exist, it throws an error.
58 file_wrapper(open_only_t, const char *name, mode_t mode)
59 { this->priv_open_or_create(ipcdetail::DoOpen, name, mode, permissions()); }
61 //!Moves the ownership of "moved"'s file to *this.
62 //!After the call, "moved" does not represent any file.
64 file_wrapper(BOOST_RV_REF(file_wrapper) moved)
65 : m_handle(file_handle_t(ipcdetail::invalid_file()))
66 { this->swap(moved); }
68 //!Moves the ownership of "moved"'s file to *this.
69 //!After the call, "moved" does not represent any file.
71 file_wrapper &operator=(BOOST_RV_REF(file_wrapper) moved)
73 file_wrapper tmp(boost::move(moved));
78 //!Swaps to file_wrappers.
80 void swap(file_wrapper &other);
82 //!Erases a file from the system.
83 //!Returns false on error. Never throws
84 static bool remove(const char *name);
86 //!Sets the size of the file
87 void truncate(offset_t length);
93 //!Returns the name of the file
94 //!used in the constructor
95 const char *get_name() const;
97 //!Returns the name of the file
98 //!used in the constructor
99 bool get_size(offset_t &size) const;
101 //!Returns access mode
102 //!used in the constructor
103 mode_t get_mode() const;
105 //!Get mapping handle
106 //!to use with mapped_region
107 mapping_handle_t get_mapping_handle() const;
110 //!Closes a previously opened file mapping. Never throws.
112 //!Closes a previously opened file mapping. Never throws.
113 bool priv_open_or_create(ipcdetail::create_enum_t type, const char *filename, mode_t mode, const permissions &perm);
115 file_handle_t m_handle;
117 std::string m_filename;
120 inline file_wrapper::file_wrapper()
121 : m_handle(file_handle_t(ipcdetail::invalid_file()))
124 inline file_wrapper::~file_wrapper()
125 { this->priv_close(); }
127 inline const char *file_wrapper::get_name() const
128 { return m_filename.c_str(); }
130 inline bool file_wrapper::get_size(offset_t &size) const
131 { return get_file_size((file_handle_t)m_handle, size); }
133 inline void file_wrapper::swap(file_wrapper &other)
135 (simple_swap)(m_handle, other.m_handle);
136 (simple_swap)(m_mode, other.m_mode);
137 m_filename.swap(other.m_filename);
140 inline mapping_handle_t file_wrapper::get_mapping_handle() const
141 { return mapping_handle_from_file_handle(m_handle); }
143 inline mode_t file_wrapper::get_mode() const
146 inline bool file_wrapper::priv_open_or_create
147 (ipcdetail::create_enum_t type,
148 const char *filename,
150 const permissions &perm = permissions())
152 m_filename = filename;
154 if(mode != read_only && mode != read_write){
155 error_info err(mode_error);
156 throw interprocess_exception(err);
159 //Open file existing native API to obtain the handle
161 case ipcdetail::DoOpen:
162 m_handle = open_existing_file(filename, mode);
164 case ipcdetail::DoCreate:
165 m_handle = create_new_file(filename, mode, perm);
167 case ipcdetail::DoOpenOrCreate:
168 m_handle = create_or_open_file(filename, mode, perm);
172 error_info err = other_error;
173 throw interprocess_exception(err);
178 if(m_handle == invalid_file()){
179 error_info err = system_error_code();
180 throw interprocess_exception(err);
187 inline bool file_wrapper::remove(const char *filename)
188 { return delete_file(filename); }
190 inline void file_wrapper::truncate(offset_t length)
192 if(!truncate_file(m_handle, length)){
193 error_info err(system_error_code());
194 throw interprocess_exception(err);
198 inline void file_wrapper::priv_close()
200 if(m_handle != invalid_file()){
201 close_file(m_handle);
202 m_handle = invalid_file();
206 } //namespace ipcdetail{
207 } //namespace interprocess {
208 } //namespace boost {
210 #include <boost/interprocess/detail/config_end.hpp>
212 #endif //BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP