Thrill  0.1
wincall_file.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * foxxll/io/wincall_file.cpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
5  *
6  * Copyright (C) 2005-2006 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008-2010 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
15 
16 #if FOXXLL_HAVE_WINCALL_FILE
17 
18 #include <limits>
19 
20 #ifndef NOMINMAX
21  #define NOMINMAX
22 #endif
23 #include <windows.h>
24 
25 #include <tlx/logger/core.hpp>
26 
28 #include <foxxll/io/iostats.hpp>
29 
30 namespace foxxll {
31 
32 void wincall_file::serve(void* buffer, offset_type offset, size_type bytes,
34 {
35  std::unique_lock<std::mutex> fd_lock(fd_mutex_);
36 
37  if (bytes > 32 * 1024 * 1024) {
38  TLX_LOG1 << "Using a block size larger than 32 MiB"
39  << " may not work with the " << io_type() << " filetype";
40  }
41 
42  HANDLE handle = file_des_;
43  LARGE_INTEGER desired_pos;
44  desired_pos.QuadPart = offset;
45  if (!SetFilePointerEx(handle, desired_pos, nullptr, FILE_BEGIN))
46  {
47  FOXXLL_THROW_WIN_LASTERROR(
48  io_error,
49  "SetFilePointerEx in wincall_request::serve()" <<
50  " offset=" << offset <<
51  " this=" << this <<
52  " buffer=" << buffer <<
53  " bytes=" << bytes <<
54  " op=" << ((op == request::READ) ? "READ" : "WRITE")
55  );
56  }
57  else
58  {
59  file_stats::scoped_read_write_timer read_write_timer(
60  file_stats_, bytes, op == request::WRITE);
61 
62  if (op == request::READ)
63  {
64  DWORD NumberOfBytesRead = 0;
65  assert(bytes <= std::numeric_limits<DWORD>::max());
66  if (!ReadFile(handle, buffer, (DWORD)bytes, &NumberOfBytesRead, nullptr))
67  {
68  FOXXLL_THROW_WIN_LASTERROR(
69  io_error,
70  "ReadFile" <<
71  " this=" << this <<
72  " offset=" << offset <<
73  " buffer=" << buffer <<
74  " bytes=" << bytes <<
75  " op=" << ((op == request::READ) ? "READ" : "WRITE") <<
76  " NumberOfBytesRead= " << NumberOfBytesRead
77  );
78  }
79  else if (NumberOfBytesRead != bytes) {
80  FOXXLL_THROW_WIN_LASTERROR(io_error, " partial read: missing " << (bytes - NumberOfBytesRead) << " out of " << bytes << " bytes");
81  }
82  }
83  else
84  {
85  DWORD NumberOfBytesWritten = 0;
86  assert(bytes <= std::numeric_limits<DWORD>::max());
87  if (!WriteFile(handle, buffer, (DWORD)bytes, &NumberOfBytesWritten, nullptr))
88  {
89  FOXXLL_THROW_WIN_LASTERROR(
90  io_error,
91  "WriteFile" <<
92  " this=" << this <<
93  " offset=" << offset <<
94  " buffer=" << buffer <<
95  " bytes=" << bytes <<
96  " op=" << ((op == request::READ) ? "READ" : "WRITE") <<
97  " NumberOfBytesWritten= " << NumberOfBytesWritten
98  );
99  }
100  else if (NumberOfBytesWritten != bytes) {
101  FOXXLL_THROW_WIN_LASTERROR(io_error, " partial write: missing " << (bytes - NumberOfBytesWritten) << " out of " << bytes << " bytes");
102  }
103  }
104  }
105 }
106 
107 const char* wincall_file::io_type() const
108 {
109  return "wincall";
110 }
111 
112 } // namespace foxxll
113 
114 #endif // #if FOXXLL_HAVE_WINCALL_FILE
115 
116 /**************************************************************************/
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.hpp:226
FOXXLL library namespace
static const size_t bytes
number of bytes in uint_pair
Definition: uint_types.hpp:75
#define TLX_LOG1
Definition: core.hpp:145