File: boostfd_file.cpp

package info (click to toggle)
libstxxl 1.4.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,476 kB
  • sloc: cpp: 45,101; ansic: 4,071; perl: 610; sh: 555; xml: 174; makefile: 18
file content (206 lines) | stat: -rw-r--r-- 6,031 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/***************************************************************************
 *  lib/io/boostfd_file.cpp
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2006 Roman Dementiev <dementiev@ira.uka.de>
 *  Copyright (C) 2009, 2010 Johannes Singler <singler@kit.edu>
 *  Copyright (C) 2008, 2010 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
 *  Copyright (C) 2014 Timo Bingmann <tb@panthema.net>
 *
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE_1_0.txt or copy at
 *  http://www.boost.org/LICENSE_1_0.txt)
 **************************************************************************/

#include <stxxl/bits/io/boostfd_file.h>

#if STXXL_HAVE_BOOSTFD_FILE

#include <stxxl/bits/io/iostats.h>
#include <stxxl/bits/common/error_handling.h>

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/version.hpp>

STXXL_BEGIN_NAMESPACE

void boostfd_file::serve(void* buffer, offset_type offset, size_type bytes,
                         request::request_type type)
{
    scoped_mutex_lock fd_lock(m_fd_mutex);

    try
    {
        m_file_des.seek(offset, BOOST_IOS::beg);
    }
    catch (const std::exception& ex)
    {
        STXXL_THROW_ERRNO
            (io_error,
            "Error doing seek() in boostfd_request::serve()" <<
            " offset=" << offset <<
            " this=" << this <<
            " buffer=" << buffer <<
            " bytes=" << bytes <<
            " type=" << ((type == request::READ) ? "READ" : "WRITE") <<
            " : " << ex.what());
    }

    stats::scoped_read_write_timer read_write_timer(bytes, type == request::WRITE);

    if (type == request::READ)
    {
        try
        {
            std::streamsize rc = m_file_des.read((char*)buffer, bytes);
            if (rc != std::streamsize(bytes)) {
                STXXL_THROW_ERRNO(io_error, " partial read: " << rc << " missing " << (bytes - rc) << " out of " << bytes << " bytes");
            }
        }
        catch (const std::exception& ex)
        {
            STXXL_THROW_ERRNO
                (io_error,
                "Error doing read() in boostfd_request::serve()" <<
                " offset=" << offset <<
                " this=" << this <<
                " buffer=" << buffer <<
                " bytes=" << bytes <<
                " type=" << ((type == request::READ) ? "READ" : "WRITE") <<
                " : " << ex.what());
        }
    }
    else
    {
        try
        {
            std::streamsize rc = m_file_des.write((char*)buffer, bytes);
            if (rc != std::streamsize(bytes)) {
                STXXL_THROW_ERRNO(io_error, " partial write: " << rc << " missing " << (bytes - rc) << " out of " << bytes << " bytes");
            }
        }
        catch (const std::exception& ex)
        {
            STXXL_THROW_ERRNO
                (io_error,
                "Error doing write() in boostfd_request::serve()" <<
                " offset=" << offset <<
                " this=" << this <<
                " buffer=" << buffer <<
                " bytes=" << bytes <<
                " type=" << ((type == request::READ) ? "READ" : "WRITE") <<
                " : " << ex.what());
        }
    }
}

const char* boostfd_file::io_type() const
{
    return "boostfd";
}

boostfd_file::boostfd_file(
    const std::string& filename,
    int mode,
    int queue_id, int allocator_id, unsigned int device_id)
    : file(device_id),
      disk_queued_file(queue_id, allocator_id),
      m_mode(mode)
{
    BOOST_IOS::openmode boostfd_mode =
        (mode & RDWR) ? (BOOST_IOS::out | BOOST_IOS::in) :
        (mode & WRONLY) ? (BOOST_IOS::out) :
        (mode & RDONLY) ? (BOOST_IOS::in) :
        BOOST_IOS::in;

#if defined(BOOST_FILESYSTEM_VERSION) && (BOOST_FILESYSTEM_VERSION >= 3)
    const boost::filesystem::path fspath(filename);
#else
    const boost::filesystem::path fspath(filename,
                                         boost::filesystem::native);
#endif

    if (mode & TRUNC)
    {
        if (boost::filesystem::exists(fspath))
        {
            boost::filesystem::remove(fspath);
            boost::filesystem::ofstream f(fspath);
            f.close();
            assert(boost::filesystem::exists(fspath));
        }
    }

    if (mode & CREAT)
    {
        // need to be emulated:
        if (!boost::filesystem::exists(fspath))
        {
            boost::filesystem::ofstream f(fspath);
            f.close();
            assert(boost::filesystem::exists(fspath));
        }
    }

    if (mode & DIRECT)
    {
        // direct mode not supported in Boost
        STXXL_MSG("Warning: open()ing " << filename << " without DIRECT mode, boostfd does not support it.");
    }

    if (mode & REQUIRE_DIRECT)
    {
        // direct mode not supported in Boost
        STXXL_ERRMSG("Error: open()ing " << filename << " with REQUIRE_DIRECT mode, but boostfd does not support it.");
        return;
    }

    if (mode & SYNC)
    {
        // ???
    }

#if (BOOST_VERSION >= 104100)
    m_file_des.open(filename, boostfd_mode);      // also compiles with earlier Boost versions, but differs semantically
#else
    m_file_des.open(filename, boostfd_mode, boostfd_mode);
#endif
}

boostfd_file::~boostfd_file()
{
    scoped_mutex_lock fd_lock(m_fd_mutex);
    m_file_des.close();
}

inline file::offset_type boostfd_file::_size()
{
    return m_file_des.seek(0, BOOST_IOS::end);
}

file::offset_type boostfd_file::size()
{
    scoped_mutex_lock fd_lock(m_fd_mutex);
    return _size();
}

void boostfd_file::set_size(offset_type newsize)
{
    scoped_mutex_lock fd_lock(m_fd_mutex);
    offset_type oldsize = _size();
    m_file_des.seek(newsize, BOOST_IOS::beg);
    m_file_des.seek(0, BOOST_IOS::beg); // not important ?
    STXXL_ASSERT(_size() >= oldsize);
}

void boostfd_file::lock()
{
    // FIXME: is there no locking possible/needed/... for boostfd?
}

STXXL_END_NAMESPACE

#endif  // #if STXXL_HAVE_BOOSTFD_FILE
// vim: et:ts=4:sw=4