Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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.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  LOG1 << "Using a block size larger than 32 MiB may not work with the " << io_type() << " filetype";
39  }
40 
41  HANDLE handle = file_des_;
42  LARGE_INTEGER desired_pos;
43  desired_pos.QuadPart = offset;
44  if (!SetFilePointerEx(handle, desired_pos, nullptr, FILE_BEGIN))
45  {
46  FOXXLL_THROW_WIN_LASTERROR(
47  io_error,
48  "SetFilePointerEx in wincall_request::serve()" <<
49  " offset=" << offset <<
50  " this=" << this <<
51  " buffer=" << buffer <<
52  " bytes=" << bytes <<
53  " op=" << ((op == request::READ) ? "READ" : "WRITE")
54  );
55  }
56  else
57  {
58  file_stats::scoped_read_write_timer read_write_timer(
59  file_stats_, bytes, op == request::WRITE);
60 
61  if (op == request::READ)
62  {
63  DWORD NumberOfBytesRead = 0;
64  assert(bytes <= std::numeric_limits<DWORD>::max());
65  if (!ReadFile(handle, buffer, (DWORD)bytes, &NumberOfBytesRead, nullptr))
66  {
67  FOXXLL_THROW_WIN_LASTERROR(
68  io_error,
69  "ReadFile" <<
70  " this=" << this <<
71  " offset=" << offset <<
72  " buffer=" << buffer <<
73  " bytes=" << bytes <<
74  " op=" << ((op == request::READ) ? "READ" : "WRITE") <<
75  " NumberOfBytesRead= " << NumberOfBytesRead
76  );
77  }
78  else if (NumberOfBytesRead != bytes) {
79  FOXXLL_THROW_WIN_LASTERROR(io_error, " partial read: missing " << (bytes - NumberOfBytesRead) << " out of " << bytes << " bytes");
80  }
81  }
82  else
83  {
84  DWORD NumberOfBytesWritten = 0;
85  assert(bytes <= std::numeric_limits<DWORD>::max());
86  if (!WriteFile(handle, buffer, (DWORD)bytes, &NumberOfBytesWritten, nullptr))
87  {
88  FOXXLL_THROW_WIN_LASTERROR(
89  io_error,
90  "WriteFile" <<
91  " this=" << this <<
92  " offset=" << offset <<
93  " buffer=" << buffer <<
94  " bytes=" << bytes <<
95  " op=" << ((op == request::READ) ? "READ" : "WRITE") <<
96  " NumberOfBytesWritten= " << NumberOfBytesWritten
97  );
98  }
99  else if (NumberOfBytesWritten != bytes) {
100  FOXXLL_THROW_WIN_LASTERROR(io_error, " partial write: missing " << (bytes - NumberOfBytesWritten) << " out of " << bytes << " bytes");
101  }
102  }
103  }
104 }
105 
106 const char* wincall_file::io_type() const
107 {
108  return "wincall";
109 }
110 
111 } // namespace foxxll
112 
113 #endif // #if FOXXLL_HAVE_WINCALL_FILE
114 
115 /**************************************************************************/
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.hpp:226
#define LOG1
Definition: logger.hpp:145
static const size_t bytes
number of bytes in uint_pair
Definition: uint_types.hpp:75