STXXL  1.4.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  * 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 
25 
26 void syscall_file::serve(void* buffer, offset_type offset, size_type bytes,
28 {
29  scoped_mutex_lock fd_lock(fd_mutex);
30 
31  char* cbuffer = static_cast<char*>(buffer);
32 
33  stats::scoped_read_write_timer read_write_timer(bytes, type == request::WRITE);
34 
35  while (bytes > 0)
36  {
37  off_t rc = ::lseek(file_des, offset, SEEK_SET);
38  if (rc < 0)
39  {
41  (io_error,
42  " this=" << this <<
43  " call=::lseek(fd,offset,SEEK_SET)" <<
44  " path=" << filename <<
45  " fd=" << file_des <<
46  " offset=" << offset <<
47  " buffer=" << cbuffer <<
48  " bytes=" << bytes <<
49  " type=" << ((type == request::READ) ? "READ" : "WRITE") <<
50  " rc=" << rc);
51  }
52 
53  if (type == request::READ)
54  {
55 #if STXXL_MSVC
56  assert(bytes <= std::numeric_limits<unsigned int>::max());
57  if ((rc = ::read(file_des, cbuffer, (unsigned int)bytes)) <= 0)
58 #else
59  if ((rc = ::read(file_des, cbuffer, bytes)) <= 0)
60 #endif
61  {
63  (io_error,
64  " this=" << this <<
65  " call=::read(fd,buffer,bytes)" <<
66  " path=" << filename <<
67  " fd=" << file_des <<
68  " offset=" << offset <<
69  " buffer=" << buffer <<
70  " bytes=" << bytes <<
71  " type=" << "READ" <<
72  " rc=" << rc);
73  }
74  bytes = (size_type)(bytes - rc);
75  offset += rc;
76  cbuffer += rc;
77 
78  if (bytes > 0 && offset == this->_size())
79  {
80  // read request extends past end-of-file
81  // fill reminder with zeroes
82  memset(cbuffer, 0, bytes);
83  bytes = 0;
84  }
85  }
86  else
87  {
88 #if STXXL_MSVC
89  assert(bytes <= std::numeric_limits<unsigned int>::max());
90  if ((rc = ::write(file_des, cbuffer, (unsigned int)bytes)) <= 0)
91 #else
92  if ((rc = ::write(file_des, cbuffer, bytes)) <= 0)
93 #endif
94  {
96  (io_error,
97  " this=" << this <<
98  " call=::write(fd,buffer,bytes)" <<
99  " path=" << filename <<
100  " fd=" << file_des <<
101  " offset=" << offset <<
102  " buffer=" << buffer <<
103  " bytes=" << bytes <<
104  " type=" << "WRITE" <<
105  " rc=" << rc);
106  }
107  bytes = (size_type)(bytes - rc);
108  offset += rc;
109  cbuffer += rc;
110  }
111  }
112 }
113 
114 const char* syscall_file::io_type() const
115 {
116  return "syscall";
117 }
118 
120 // vim: et:ts=4:sw=4
#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:62
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:105
#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:241
#define STXXL_THROW_ERRNO(exception_type, error_message)
Throws exception_type with &quot;Error in [function] : [error_message] : [errno message]&quot;.
static const size_t bytes
number of bytes in uint_pair
Definition: uint_types.h:96
request::offset_type offset_type
the offset of a request, also the size of the file
Definition: file.h:60
#define STXXL_END_NAMESPACE
Definition: namespace.h:17