summaryrefslogtreecommitdiff
path: root/boost/beast/core/impl/file_win32.ipp
blob: 1183e3853f2e4656c46424fb4b9cb13816c9fd6c (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//
// Copyright (c) 2015-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// 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)
//
// Official repository: https://github.com/boostorg/beast
//

#ifndef BOOST_BEAST_CORE_IMPL_FILE_WIN32_IPP
#define BOOST_BEAST_CORE_IMPL_FILE_WIN32_IPP

#include <boost/core/exchange.hpp>
#include <boost/winapi/access_rights.hpp>
#include <boost/winapi/error_codes.hpp>
#include <boost/winapi/file_management.hpp>
#include <boost/winapi/get_last_error.hpp>
#include <limits>
#include <utility>

namespace boost {
namespace beast {

namespace detail {

// VFALCO Can't seem to get boost/detail/winapi to work with
//        this so use the non-Ex version for now.
inline
boost::winapi::BOOL_
set_file_pointer_ex(
    boost::winapi::HANDLE_ hFile,
    boost::winapi::LARGE_INTEGER_ lpDistanceToMove,
    boost::winapi::PLARGE_INTEGER_ lpNewFilePointer,
    boost::winapi::DWORD_ dwMoveMethod)
{
    auto dwHighPart = lpDistanceToMove.u.HighPart;
    auto dwLowPart = boost::winapi::SetFilePointer(
        hFile,
        lpDistanceToMove.u.LowPart,
        &dwHighPart,
        dwMoveMethod);
    if(dwLowPart == boost::winapi::INVALID_SET_FILE_POINTER_)
        return 0;
    if(lpNewFilePointer)
    {
        lpNewFilePointer->u.LowPart = dwLowPart;
        lpNewFilePointer->u.HighPart = dwHighPart;
    }
    return 1;
}

} // detail

inline
file_win32::
~file_win32()
{
    if(h_ != boost::winapi::INVALID_HANDLE_VALUE_)
        boost::winapi::CloseHandle(h_);
}

inline
file_win32::
file_win32(file_win32&& other)
    : h_(boost::exchange(other.h_, boost::winapi::INVALID_HANDLE_VALUE_))
{
}

inline
file_win32&
file_win32::
operator=(file_win32&& other)
{
    if(&other == this)
        return *this;
    if(h_)
        boost::winapi::CloseHandle(h_);
    h_ = other.h_;
    other.h_ = boost::winapi::INVALID_HANDLE_VALUE_;
    return *this;
}

inline
void
file_win32::
native_handle(native_handle_type h)
{
     if(h_ != boost::winapi::INVALID_HANDLE_VALUE_)
        boost::winapi::CloseHandle(h_);
    h_ = h;
}

inline
void
file_win32::
close(error_code& ec)
{
    if(h_ != boost::winapi::INVALID_HANDLE_VALUE_)
    {
        if(! boost::winapi::CloseHandle(h_))
            ec.assign(boost::winapi::GetLastError(),
                system_category());
        else
            ec.assign(0, ec.category());
        h_ = boost::winapi::INVALID_HANDLE_VALUE_;
    }
    else
    {
        ec.assign(0, ec.category());
    }
}

inline
void
file_win32::
open(char const* path, file_mode mode, error_code& ec)
{
    if(h_ != boost::winapi::INVALID_HANDLE_VALUE_)
    {
        boost::winapi::CloseHandle(h_);
        h_ = boost::winapi::INVALID_HANDLE_VALUE_;
    }
    boost::winapi::DWORD_ share_mode = 0;
    boost::winapi::DWORD_ desired_access = 0;
    boost::winapi::DWORD_ creation_disposition = 0;
    boost::winapi::DWORD_ flags_and_attributes = 0;
/*
                             |                    When the file...
    This argument:           |             Exists            Does not exist
    -------------------------+------------------------------------------------------
    CREATE_ALWAYS            |            Truncates             Creates
    CREATE_NEW         +-----------+        Fails               Creates
    OPEN_ALWAYS     ===| does this |===>    Opens               Creates
    OPEN_EXISTING      +-----------+        Opens                Fails
    TRUNCATE_EXISTING        |            Truncates              Fails
*/
    switch(mode)
    {
    default:
    case file_mode::read:
        desired_access = boost::winapi::GENERIC_READ_;
        share_mode = boost::winapi::FILE_SHARE_READ_;
        creation_disposition = boost::winapi::OPEN_EXISTING_;
        flags_and_attributes = 0x10000000; // FILE_FLAG_RANDOM_ACCESS
        break;

    case file_mode::scan:           
        desired_access = boost::winapi::GENERIC_READ_;
        share_mode = boost::winapi::FILE_SHARE_READ_;
        creation_disposition = boost::winapi::OPEN_EXISTING_;
        flags_and_attributes = 0x08000000; // FILE_FLAG_SEQUENTIAL_SCAN
        break;

    case file_mode::write:          
        desired_access = boost::winapi::GENERIC_READ_ |
                         boost::winapi::GENERIC_WRITE_;
        creation_disposition = boost::winapi::CREATE_ALWAYS_;
        flags_and_attributes = 0x10000000; // FILE_FLAG_RANDOM_ACCESS
        break;

    case file_mode::write_new:      
        desired_access = boost::winapi::GENERIC_READ_ |
                         boost::winapi::GENERIC_WRITE_;
        creation_disposition = boost::winapi::CREATE_NEW_;
        flags_and_attributes = 0x10000000; // FILE_FLAG_RANDOM_ACCESS
        break;

    case file_mode::write_existing: 
        desired_access = boost::winapi::GENERIC_READ_ |
                         boost::winapi::GENERIC_WRITE_;
        creation_disposition = boost::winapi::OPEN_EXISTING_;
        flags_and_attributes = 0x10000000; // FILE_FLAG_RANDOM_ACCESS
        break;

    case file_mode::append:         
        desired_access = boost::winapi::GENERIC_READ_ |
                         boost::winapi::GENERIC_WRITE_;

        creation_disposition = boost::winapi::CREATE_ALWAYS_;
        flags_and_attributes = 0x08000000; // FILE_FLAG_SEQUENTIAL_SCAN
        break;

    case file_mode::append_new:     
        desired_access = boost::winapi::GENERIC_READ_ |
                         boost::winapi::GENERIC_WRITE_;
        creation_disposition = boost::winapi::CREATE_NEW_;
        flags_and_attributes = 0x08000000; // FILE_FLAG_SEQUENTIAL_SCAN
        break;

    case file_mode::append_existing:
        desired_access = boost::winapi::GENERIC_READ_ |
                         boost::winapi::GENERIC_WRITE_;
        creation_disposition = boost::winapi::OPEN_EXISTING_;
        flags_and_attributes = 0x08000000; // FILE_FLAG_SEQUENTIAL_SCAN
        break;
    }
    h_ = ::CreateFileA(
        path,
        desired_access,
        share_mode,
        NULL,
        creation_disposition,
        flags_and_attributes,
        NULL);
    if(h_ == boost::winapi::INVALID_HANDLE_VALUE_)
        ec.assign(boost::winapi::GetLastError(),
            system_category());
    else
        ec.assign(0, ec.category());
}

inline
std::uint64_t
file_win32::
size(error_code& ec) const
{
    if(h_ == boost::winapi::INVALID_HANDLE_VALUE_)
    {
        ec = make_error_code(errc::invalid_argument);
        return 0;
    }
    boost::winapi::LARGE_INTEGER_ fileSize;
    if(! boost::winapi::GetFileSizeEx(h_, &fileSize))
    {
        ec.assign(boost::winapi::GetLastError(),
            system_category());
        return 0;
    }
    ec.assign(0, ec.category());
    return fileSize.QuadPart;
}

inline
std::uint64_t
file_win32::
pos(error_code& ec)
{
    if(h_ == boost::winapi::INVALID_HANDLE_VALUE_)
    {
        ec = make_error_code(errc::invalid_argument);
        return 0;
    }
    boost::winapi::LARGE_INTEGER_ in;
    boost::winapi::LARGE_INTEGER_ out;
    in.QuadPart = 0;
    if(! detail::set_file_pointer_ex(h_, in, &out,
        boost::winapi::FILE_CURRENT_))
    {
        ec.assign(boost::winapi::GetLastError(),
            system_category());
        return 0;
    }
    ec.assign(0, ec.category());
    return out.QuadPart;
}

inline
void
file_win32::
seek(std::uint64_t offset, error_code& ec)
{
    if(h_ == boost::winapi::INVALID_HANDLE_VALUE_)
    {
        ec = make_error_code(errc::invalid_argument);
        return;
    }
    boost::winapi::LARGE_INTEGER_ in;
    in.QuadPart = offset;
    if(! detail::set_file_pointer_ex(h_, in, 0,
        boost::winapi::FILE_BEGIN_))
    {
        ec.assign(boost::winapi::GetLastError(),
            system_category());
        return;
    }
    ec.assign(0, ec.category());
}

inline
std::size_t
file_win32::
read(void* buffer, std::size_t n, error_code& ec)
{
    if(h_ == boost::winapi::INVALID_HANDLE_VALUE_)
    {
        ec = make_error_code(errc::invalid_argument);
        return 0;
    }
    std::size_t nread = 0;
    while(n > 0)
    {
        boost::winapi::DWORD_ amount;
        if(n > (std::numeric_limits<
                boost::winapi::DWORD_>::max)())
            amount = (std::numeric_limits<
                boost::winapi::DWORD_>::max)();
        else
            amount = static_cast<
                boost::winapi::DWORD_>(n);
        boost::winapi::DWORD_ bytesRead;
        if(! ::ReadFile(h_, buffer, amount, &bytesRead, 0))
        {
            auto const dwError = boost::winapi::GetLastError();
            if(dwError != boost::winapi::ERROR_HANDLE_EOF_)
                ec.assign(dwError, system_category());
            else
                ec.assign(0, ec.category());
            return nread;
        }
        if(bytesRead == 0)
            return nread;
        n -= bytesRead;
        nread += bytesRead;
        buffer = static_cast<char*>(buffer) + bytesRead;
    }
    ec.assign(0, ec.category());
    return nread;
}

inline
std::size_t
file_win32::
write(void const* buffer, std::size_t n, error_code& ec)
{
    if(h_ == boost::winapi::INVALID_HANDLE_VALUE_)
    {
        ec = make_error_code(errc::invalid_argument);
        return 0;
    }
    std::size_t nwritten = 0;
    while(n > 0)
    {
        boost::winapi::DWORD_ amount;
        if(n > (std::numeric_limits<
                boost::winapi::DWORD_>::max)())
            amount = (std::numeric_limits<
                boost::winapi::DWORD_>::max)();
        else
            amount = static_cast<
                boost::winapi::DWORD_>(n);
        boost::winapi::DWORD_ bytesWritten;
        if(! ::WriteFile(h_, buffer, amount, &bytesWritten, 0))
        {
            auto const dwError = boost::winapi::GetLastError();
            if(dwError != boost::winapi::ERROR_HANDLE_EOF_)
                ec.assign(dwError, system_category());
            else
                ec.assign(0, ec.category());
            return nwritten;
        }
        if(bytesWritten == 0)
            return nwritten;
        n -= bytesWritten;
        nwritten += bytesWritten;
        buffer = static_cast<char const*>(buffer) + bytesWritten;
    }
    ec.assign(0, ec.category());
    return nwritten;
}

} // beast
} // boost

#endif