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
11 #ifndef BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP
12 #define BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP
13
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
16 #endif
17 #
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 # pragma once
20 #endif
21
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>
29
30 namespace boost {
31 namespace interprocess {
32 namespace ipcdetail{
33
34 class file_wrapper
35 {
36 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
37 BOOST_MOVABLE_BUT_NOT_COPYABLE(file_wrapper)
38 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
39 public:
40
41 //!Default constructor.
42 //!Represents an empty file_wrapper.
43 file_wrapper();
44
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.
file_wrapper(create_only_t,const char * name,mode_t mode,const permissions & perm=permissions ())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); }
49
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.
file_wrapper(open_or_create_t,const char * name,mode_t mode,const permissions & perm=permissions ())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); }
55
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.
file_wrapper(open_only_t,const char * name,mode_t mode)58 file_wrapper(open_only_t, const char *name, mode_t mode)
59 { this->priv_open_or_create(ipcdetail::DoOpen, name, mode, permissions()); }
60
61 //!Moves the ownership of "moved"'s file to *this.
62 //!After the call, "moved" does not represent any file.
63 //!Does not throw
file_wrapper(BOOST_RV_REF (file_wrapper)moved)64 file_wrapper(BOOST_RV_REF(file_wrapper) moved)
65 : m_handle(file_handle_t(ipcdetail::invalid_file()))
66 { this->swap(moved); }
67
68 //!Moves the ownership of "moved"'s file to *this.
69 //!After the call, "moved" does not represent any file.
70 //!Does not throw
operator =(BOOST_RV_REF (file_wrapper)moved)71 file_wrapper &operator=(BOOST_RV_REF(file_wrapper) moved)
72 {
73 file_wrapper tmp(boost::move(moved));
74 this->swap(tmp);
75 return *this;
76 }
77
78 //!Swaps to file_wrappers.
79 //!Does not throw
80 void swap(file_wrapper &other);
81
82 //!Erases a file from the system.
83 //!Returns false on error. Never throws
84 static bool remove(const char *name);
85
86 //!Sets the size of the file
87 void truncate(offset_t length);
88
89 //!Closes the
90 //!file
91 ~file_wrapper();
92
93 //!Returns the name of the file
94 //!used in the constructor
95 const char *get_name() const;
96
97 //!Returns the name of the file
98 //!used in the constructor
99 bool get_size(offset_t &size) const;
100
101 //!Returns access mode
102 //!used in the constructor
103 mode_t get_mode() const;
104
105 //!Get mapping handle
106 //!to use with mapped_region
107 mapping_handle_t get_mapping_handle() const;
108
109 private:
110 //!Closes a previously opened file mapping. Never throws.
111 void priv_close();
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);
114
115 file_handle_t m_handle;
116 mode_t m_mode;
117 std::string m_filename;
118 };
119
file_wrapper()120 inline file_wrapper::file_wrapper()
121 : m_handle(file_handle_t(ipcdetail::invalid_file()))
122 , m_mode(read_only), m_filename()
123 {}
124
~file_wrapper()125 inline file_wrapper::~file_wrapper()
126 { this->priv_close(); }
127
get_name() const128 inline const char *file_wrapper::get_name() const
129 { return m_filename.c_str(); }
130
get_size(offset_t & size) const131 inline bool file_wrapper::get_size(offset_t &size) const
132 { return get_file_size((file_handle_t)m_handle, size); }
133
swap(file_wrapper & other)134 inline void file_wrapper::swap(file_wrapper &other)
135 {
136 (simple_swap)(m_handle, other.m_handle);
137 (simple_swap)(m_mode, other.m_mode);
138 m_filename.swap(other.m_filename);
139 }
140
get_mapping_handle() const141 inline mapping_handle_t file_wrapper::get_mapping_handle() const
142 { return mapping_handle_from_file_handle(m_handle); }
143
get_mode() const144 inline mode_t file_wrapper::get_mode() const
145 { return m_mode; }
146
priv_open_or_create(ipcdetail::create_enum_t type,const char * filename,mode_t mode,const permissions & perm=permissions ())147 inline bool file_wrapper::priv_open_or_create
148 (ipcdetail::create_enum_t type,
149 const char *filename,
150 mode_t mode,
151 const permissions &perm = permissions())
152 {
153 m_filename = filename;
154
155 if(mode != read_only && mode != read_write){
156 error_info err(mode_error);
157 throw interprocess_exception(err);
158 }
159
160 //Open file existing native API to obtain the handle
161 switch(type){
162 case ipcdetail::DoOpen:
163 m_handle = open_existing_file(filename, mode);
164 break;
165 case ipcdetail::DoCreate:
166 m_handle = create_new_file(filename, mode, perm);
167 break;
168 case ipcdetail::DoOpenOrCreate:
169 m_handle = create_or_open_file(filename, mode, perm);
170 break;
171 default:
172 {
173 error_info err = other_error;
174 throw interprocess_exception(err);
175 }
176 }
177
178 //Check for error
179 if(m_handle == invalid_file()){
180 error_info err = system_error_code();
181 throw interprocess_exception(err);
182 }
183
184 m_mode = mode;
185 return true;
186 }
187
remove(const char * filename)188 inline bool file_wrapper::remove(const char *filename)
189 { return delete_file(filename); }
190
truncate(offset_t length)191 inline void file_wrapper::truncate(offset_t length)
192 {
193 if(!truncate_file(m_handle, length)){
194 error_info err(system_error_code());
195 throw interprocess_exception(err);
196 }
197 }
198
priv_close()199 inline void file_wrapper::priv_close()
200 {
201 if(m_handle != invalid_file()){
202 close_file(m_handle);
203 m_handle = invalid_file();
204 }
205 }
206
207 } //namespace ipcdetail{
208 } //namespace interprocess {
209 } //namespace boost {
210
211 #include <boost/interprocess/detail/config_end.hpp>
212
213 #endif //BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP
214