Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
syscall_file.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * foxxll/io/syscall_file.cpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
5  *
6  * Copyright (C) 2002 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008, 2010 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2010 Johannes Singler <[email protected]>
9  *
10  * Distributed under the Boost Software License, Version 1.0.
11  * (See accompanying file LICENSE_1_0.txt or copy at
12  * http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #include <limits>
16 #include <mutex>
17 
19 #include <foxxll/config.hpp>
20 #include <foxxll/io/iostats.hpp>
21 #include <foxxll/io/request.hpp>
25 
26 namespace foxxll {
27 
28 void syscall_file::serve(void* buffer, offset_type offset, size_type bytes,
30 {
31  std::unique_lock<std::mutex> fd_lock(fd_mutex_);
32 
33  auto* cbuffer = static_cast<char*>(buffer);
34 
35  file_stats::scoped_read_write_timer read_write_timer(
36  file_stats_, bytes, op == request::WRITE);
37 
38  while (bytes > 0)
39  {
40  off_t rc = ::lseek(file_des_, offset, SEEK_SET);
41  if (rc < 0)
42  {
44  io_error,
45  " this=" << this <<
46  " call=::lseek(fd,offset,SEEK_SET)" <<
47  " path=" << filename_ <<
48  " fd=" << file_des_ <<
49  " offset=" << offset <<
50  " buffer=" << static_cast<void*>(cbuffer) <<
51  " bytes=" << bytes <<
52  " op=" << ((op == request::READ) ? "READ" : "WRITE") <<
53  " rc=" << rc
54  );
55  }
56 
57  if (op == request::READ)
58  {
59 #if FOXXLL_MSVC
60  assert(bytes <= std::numeric_limits<unsigned int>::max());
61  if ((rc = ::read(file_des_, cbuffer, (unsigned int)bytes)) <= 0)
62 #else
63  if ((rc = ::read(file_des_, cbuffer, bytes)) <= 0)
64 #endif
65  {
67  io_error,
68  " this=" << this <<
69  " call=::read(fd,buffer,bytes)" <<
70  " path=" << filename_ <<
71  " fd=" << file_des_ <<
72  " offset=" << offset <<
73  " buffer=" << static_cast<void*>(buffer) <<
74  " bytes=" << bytes <<
75  " op=" << "READ" <<
76  " rc=" << rc
77  );
78  }
79  bytes = static_cast<size_type>(bytes - rc);
80  offset += rc;
81  cbuffer += rc;
82 
83  if (bytes > 0 && offset == this->_size())
84  {
85  // read request extends past end-of-file
86  // fill reminder with zeroes
87  memset(cbuffer, 0, bytes);
88  bytes = 0;
89  }
90  }
91  else
92  {
93 #if FOXXLL_MSVC
94  assert(bytes <= std::numeric_limits<unsigned int>::max());
95  if ((rc = ::write(file_des_, cbuffer, (unsigned int)bytes)) <= 0)
96 #else
97  if ((rc = ::write(file_des_, cbuffer, bytes)) <= 0)
98 #endif
99  {
101  io_error,
102  " this=" << this <<
103  " call=::write(fd,buffer,bytes)" <<
104  " path=" << filename_ <<
105  " fd=" << file_des_ <<
106  " offset=" << offset <<
107  " buffer=" << static_cast<void*>(buffer) <<
108  " bytes=" << bytes <<
109  " op=" << "WRITE" <<
110  " rc=" << rc
111  );
112  }
113  bytes = static_cast<size_type>(bytes - rc);
114  offset += rc;
115  cbuffer += rc;
116  }
117  }
118 }
119 
120 const char* syscall_file::io_type() const
121 {
122  return "syscall";
123 }
124 
125 } // namespace foxxll
126 
127 /**************************************************************************/
const char * io_type() const final
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.hpp:226
request::offset_type offset_type
the offset of a request, also the size of the file
Definition: file.hpp:58
file_stats * file_stats_
Definition: file.hpp:200
request::size_type size_type
the size of a request
Definition: file.hpp:60
void serve(void *buffer, offset_type offset, size_type bytes, request::read_or_write op) final
#define FOXXLL_THROW_ERRNO(exception_type, error_message)
Throws exception_type with "Error in [function] : [error_message] : [errno message]".
static const size_t bytes
number of bytes in uint_pair
Definition: uint_types.hpp:75
const std::string filename_