STXXL  1.4.0
 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  * lib/io/syscall_file.cpp
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
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 
17 #include <stxxl/bits/config.h>
18 #include <stxxl/bits/io/iostats.h>
19 #include <stxxl/bits/io/request.h>
22 #include "ufs_platform.h"
23 
24 
26 
27 void syscall_file::serve(const request* req) throw (io_error)
28 {
29  scoped_mutex_lock fd_lock(fd_mutex);
30  assert(req->get_file() == this);
31  offset_type offset = req->get_offset();
32  char* buffer = static_cast<char*>(req->get_buffer());
33  size_type bytes = req->get_size();
34  request::request_type type = req->get_type();
35 
36  stats::scoped_read_write_timer read_write_timer(bytes, type == 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=" << (void*)buffer <<
51  " bytes=" << bytes <<
52  " type=" << ((type == request::READ) ? "READ" : "WRITE") <<
53  " rc=" << rc);
54  }
55 
56  if (type == request::READ)
57  {
58 #if STXXL_MSVC
59  assert(bytes <= std::numeric_limits<unsigned int>::max());
60  if ((rc = ::read(file_des, buffer, (unsigned int)bytes)) <= 0)
61 #else
62  if ((rc = ::read(file_des, buffer, bytes)) <= 0)
63 #endif
64  {
66  (io_error,
67  " this=" << this <<
68  " call=::read(fd,buffer,bytes)" <<
69  " path=" << filename <<
70  " fd=" << file_des <<
71  " offset=" << offset <<
72  " buffer=" << (void*)buffer <<
73  " bytes=" << bytes <<
74  " type=" << "READ" <<
75  " rc=" << rc);
76  }
77  bytes -= rc;
78  offset += rc;
79  buffer += rc;
80 
81  if (bytes > 0 && offset == this->_size())
82  {
83  // read request extends past end-of-file
84  // fill reminder with zeroes
85  memset(buffer, 0, bytes);
86  bytes = 0;
87  }
88  }
89  else
90  {
91 #if STXXL_MSVC
92  assert(bytes <= std::numeric_limits<unsigned int>::max());
93  if ((rc = ::write(file_des, buffer, (unsigned int)bytes)) <= 0)
94 #else
95  if ((rc = ::write(file_des, buffer, bytes)) <= 0)
96 #endif
97  {
99  (io_error,
100  " this=" << this <<
101  " call=::write(fd,buffer,bytes)" <<
102  " path=" << filename <<
103  " fd=" << file_des <<
104  " offset=" << offset <<
105  " buffer=" << (void*)buffer <<
106  " bytes=" << bytes <<
107  " type=" << "WRITE" <<
108  " rc=" << rc);
109  }
110  bytes -= rc;
111  offset += rc;
112  buffer += rc;
113  }
114  }
115 }
116 
117 const char* syscall_file::io_type() const
118 {
119  return "syscall";
120 }
121 
123 // vim: et:ts=4:sw=4
static const int bytes
number of bytes in uint_pair
Definition: uint_types.h:99
#define lseek
Definition: ufs_platform.h:72
#define off_t
Definition: ufs_platform.h:75
request::size_type size_type
the size of a request
Definition: file.h:66
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:106
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.h:244
#define STXXL_THROW_ERRNO(exception_type, error_message)
Throws exception_type with &quot;Error in [function] : [error_message] : [errno message]&quot;.
request::offset_type offset_type
the offset of a request, also the size of the file
Definition: file.h:64
Request with basic properties like file and offset.
Definition: request.h:39
#define STXXL_END_NAMESPACE
Definition: namespace.h:17