17 #if STXXL_HAVE_BOOSTFD_FILE
22 #include <boost/filesystem/operations.hpp>
23 #include <boost/filesystem/fstream.hpp>
24 #include <boost/version.hpp>
30 void boostfd_file::serve(
const request* req)
throw (io_error)
32 scoped_mutex_lock fd_lock(fd_mutex);
33 assert(req->get_file() ==
this);
34 offset_type offset = req->get_offset();
35 void* buffer = req->get_buffer();
36 size_type
bytes = req->get_size();
37 request::request_type type = req->get_type();
41 file_des.seek(offset, BOOST_IOS::beg);
43 catch (
const std::exception& ex)
47 "Error doing seek() in boostfd_request::serve()" <<
48 " offset=" << offset <<
50 " buffer=" << buffer <<
52 " type=" << ((type == request::READ) ?
"READ" :
"WRITE") <<
56 stats::scoped_read_write_timer read_write_timer(bytes, type == request::WRITE);
58 if (type == request::READ)
62 std::streamsize rc = file_des.read((
char*)buffer, bytes);
63 if (rc != std::streamsize(bytes)) {
64 STXXL_THROW_ERRNO(io_error,
" partial read: " << rc <<
" missing " << (bytes - rc) <<
" out of " << bytes <<
" bytes");
67 catch (
const std::exception& ex)
71 "Error doing read() in boostfd_request::serve()" <<
72 " offset=" << offset <<
74 " buffer=" << buffer <<
76 " type=" << ((type == request::READ) ?
"READ" :
"WRITE") <<
84 std::streamsize rc = file_des.write((
char*)buffer, bytes);
85 if (rc != std::streamsize(bytes)) {
86 STXXL_THROW_ERRNO(io_error,
" partial write: " << rc <<
" missing " << (bytes - rc) <<
" out of " << bytes <<
" bytes");
89 catch (
const std::exception& ex)
93 "Error doing write() in boostfd_request::serve()" <<
94 " offset=" << offset <<
96 " buffer=" << buffer <<
98 " type=" << ((type == request::READ) ?
"READ" :
"WRITE") <<
104 const char* boostfd_file::io_type()
const
109 boostfd_file::boostfd_file(
110 const std::string& filename,
112 int queue_id,
int allocator_id) : disk_queued_file(queue_id, allocator_id), mode_(mode)
114 BOOST_IOS::openmode boostfd_mode =
115 (mode & RDWR) ? (BOOST_IOS::out | BOOST_IOS::in) :
116 (mode & WRONLY) ? (BOOST_IOS::out) :
117 (mode & RDONLY) ? (BOOST_IOS::in) :
120 #if defined(BOOST_FILESYSTEM_VERSION) && (BOOST_FILESYSTEM_VERSION >= 3)
121 const boost::filesystem::path fspath(filename);
123 const boost::filesystem::path fspath(filename,
124 boost::filesystem::native);
129 if (boost::filesystem::exists(fspath))
131 boost::filesystem::remove(fspath);
132 boost::filesystem::ofstream f(fspath);
134 assert(boost::filesystem::exists(fspath));
141 if (!boost::filesystem::exists(fspath))
143 boost::filesystem::ofstream f(fspath);
145 assert(boost::filesystem::exists(fspath));
152 STXXL_MSG(
"Warning: open()ing " << filename <<
" without DIRECT mode, boostfd does not support it.");
155 if (mode & REQUIRE_DIRECT)
158 STXXL_ERRMSG(
"Error: open()ing " << filename <<
" with REQUIRE_DIRECT mode, but boostfd does not support it.");
167 #if (BOOST_VERSION >= 104100)
168 file_des.open(filename, boostfd_mode);
170 file_des.open(filename, boostfd_mode, boostfd_mode);
174 boostfd_file::~boostfd_file()
176 scoped_mutex_lock fd_lock(fd_mutex);
180 inline file::offset_type boostfd_file::_size()
182 return file_des.seek(0, BOOST_IOS::end);
185 file::offset_type boostfd_file::size()
187 scoped_mutex_lock fd_lock(fd_mutex);
191 void boostfd_file::set_size(offset_type newsize)
193 scoped_mutex_lock fd_lock(fd_mutex);
195 offset_type oldsize = _size();
197 file_des.seek(newsize, BOOST_IOS::beg);
198 file_des.seek(0, BOOST_IOS::beg);
199 assert(_size() >= oldsize);
202 void boostfd_file::lock()
209 #endif // #if STXXL_HAVE_BOOSTFD_FILE
static const int bytes
number of bytes in uint_pair
#define STXXL_BEGIN_NAMESPACE
#define STXXL_THROW_ERRNO(exception_type, error_message)
Throws exception_type with "Error in [function] : [error_message] : [errno message]".
#define STXXL_END_NAMESPACE