]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/interprocess/include/boost/interprocess/detail/file_wrapper.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / interprocess / include / boost / interprocess / detail / file_wrapper.hpp
CommitLineData
7c673cae
FG
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
30namespace boost {
31namespace interprocess {
32namespace ipcdetail{
33
34class 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.
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.
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.
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
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
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
120inline file_wrapper::file_wrapper()
121 : m_handle(file_handle_t(ipcdetail::invalid_file()))
122{}
123
124inline file_wrapper::~file_wrapper()
125{ this->priv_close(); }
126
127inline const char *file_wrapper::get_name() const
128{ return m_filename.c_str(); }
129
130inline bool file_wrapper::get_size(offset_t &size) const
131{ return get_file_size((file_handle_t)m_handle, size); }
132
133inline void file_wrapper::swap(file_wrapper &other)
134{
135 (simple_swap)(m_handle, other.m_handle);
136 (simple_swap)(m_mode, other.m_mode);
137 m_filename.swap(other.m_filename);
138}
139
140inline mapping_handle_t file_wrapper::get_mapping_handle() const
141{ return mapping_handle_from_file_handle(m_handle); }
142
143inline mode_t file_wrapper::get_mode() const
144{ return m_mode; }
145
146inline bool file_wrapper::priv_open_or_create
147 (ipcdetail::create_enum_t type,
148 const char *filename,
149 mode_t mode,
150 const permissions &perm = permissions())
151{
152 m_filename = filename;
153
154 if(mode != read_only && mode != read_write){
155 error_info err(mode_error);
156 throw interprocess_exception(err);
157 }
158
159 //Open file existing native API to obtain the handle
160 switch(type){
161 case ipcdetail::DoOpen:
162 m_handle = open_existing_file(filename, mode);
163 break;
164 case ipcdetail::DoCreate:
165 m_handle = create_new_file(filename, mode, perm);
166 break;
167 case ipcdetail::DoOpenOrCreate:
168 m_handle = create_or_open_file(filename, mode, perm);
169 break;
170 default:
171 {
172 error_info err = other_error;
173 throw interprocess_exception(err);
174 }
175 }
176
177 //Check for error
178 if(m_handle == invalid_file()){
179 error_info err = system_error_code();
180 throw interprocess_exception(err);
181 }
182
183 m_mode = mode;
184 return true;
185}
186
187inline bool file_wrapper::remove(const char *filename)
188{ return delete_file(filename); }
189
190inline void file_wrapper::truncate(offset_t length)
191{
192 if(!truncate_file(m_handle, length)){
193 error_info err(system_error_code());
194 throw interprocess_exception(err);
195 }
196}
197
198inline void file_wrapper::priv_close()
199{
200 if(m_handle != invalid_file()){
201 close_file(m_handle);
202 m_handle = invalid_file();
203 }
204}
205
206} //namespace ipcdetail{
207} //namespace interprocess {
208} //namespace boost {
209
210#include <boost/interprocess/detail/config_end.hpp>
211
212#endif //BOOST_INTERPROCESS_DETAIL_FILE_WRAPPER_HPP