ewmscp  ..
Public Member Functions | Protected Attributes | List of all members
inputHandler::posixFile::readerPosixFile Class Reference

#include <inputHandlerPosixFile.h>

Inheritance diagram for inputHandler::posixFile::readerPosixFile:
[legend]
Collaboration diagram for inputHandler::posixFile::readerPosixFile:
[legend]

Public Member Functions

 readerPosixFile (const std::string &aPath, copyRequest::stateType &state, const genericStat &inititalStat)
 
 ~readerPosixFile () override
 
bool parallelizable () const override
 tell if this handler is capable of parallel IO. Unsually not the case More...
 
bool setupSparseRegions (const std::string &sparseHandling) override
 create a region list which maps the holes in the source file More...
 
void seek (size_t pos) override
 like the standard seek, to be used only when appending new data to a file More...
 
bool readBlock (block &b) override
 read one block from the file More...
 
void readBlockP (block &b, size_t bytesToRead, off_t offset) override
 read one block from the file, starting at offset. More...
 
void checkUnchangedness () override
 check if the file is unchanged by comparing cuttent stat to initial stat More...
 
- Public Member Functions inherited from inputHandler::base::reader
virtual ~reader () noexcept(false)=default
 
- Public Member Functions inherited from ioHandle
 ioHandle ()
 
virtual ~ioHandle () noexcept(false)
 
virtual size_t getBlockSize () const
 
virtual void setBlockSize (size_t newSize)
 
virtual std::unique_ptr< acl::listgetAclData ()
 get acls More...
 
- Public Member Functions inherited from posixFileIoCommon
 posixFileIoCommon (const std::string &aPath)
 
void setXattr (const std::string &name, const std::string &value) override
 
std::string getXattr (const std::string &name) override
 get one extended attribute value More...
 
void removeXattr (const std::string &name) override
 
std::unique_ptr< const genericStatgetStat () override
 
std::unique_ptr< ioHandle::attrDataTypegetAttrData (const outputHandler::base *aOutputHandler) override
 get attributes in the optimal way for setting with aOutputHandler More...
 

Protected Attributes

std::deque< regionTyperegions
 
- Protected Attributes inherited from inputHandler::base::reader
const genericStatreadInitialStat
 stat at beginning, needed for unchngedness check More...
 
std::remove_const< decltype(readInitialStat.size)>::type totalBytesRead = 0
 number of bytes read so far, needed for check on growing files More...
 
- Protected Attributes inherited from ioHandle
size_t blockSize
 in bytes, block size to be used when reading or writing More...
 
- Protected Attributes inherited from posixFileIoCommon
const std::string & path
 
int fd = -1
 

Additional Inherited Members

- Protected Member Functions inherited from inputHandler::base::reader
 reader (const genericStat &inititalStat)
 
- Protected Member Functions inherited from unwindCheck
 unwindCheck ()=default
 
 ~unwindCheck ()=default
 
bool isUnwinding () const
 
- Protected Member Functions inherited from ioHandle
 ioHandle ()
 
virtual ~ioHandle () noexcept(false)
 
virtual size_t getBlockSize () const
 
virtual void setBlockSize (size_t newSize)
 
virtual std::unique_ptr< acl::listgetAclData ()
 get acls More...
 

Detailed Description

Definition at line 44 of file inputHandlerPosixFile.h.

Constructor & Destructor Documentation

◆ readerPosixFile()

inputHandler::posixFile::readerPosixFile::readerPosixFile ( const std::string &  aPath,
copyRequest::stateType state,
const genericStat inititalStat 
)

Definition at line 44 of file inputHandlerPosixFile.cpp.

46  :
47  posixFileIoCommon(aPath),
48  reader(inititalStat) {
49  {
50  timerInst(open);
51  fd = open(path.c_str(), O_RDONLY);
52  }
53  if (fd == -1) {
54  if (errno == ENOENT
55  || (errno == ENOTDIR && !S_ISDIR(readInitialStat.mode))) {
57  }
58  }
59  throwcall::badval(fd, -1, "can't open ", path, " for reading");
61  {
62  timerInst(posix_fadvise);
63  throwcall::good0(posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL),
64  "can't advise ", path, " as sequential");
65  }
66  {
67  timerInst(posix_fadvise);
68  throwcall::good0(posix_fadvise(fd, 0, 0, POSIX_FADV_NOREUSE),
69  "can't advise ", path, " as use only once");
70  }
71  }

References throwcall::badval(), genericStat::blksize, ioHandle::blockSize, posixFileIoCommon::fd, throwcall::good0(), genericStat::mode, posixFileIoCommon::path, inputHandler::base::reader::readInitialStat, timerInst, and copyRequest::vanished.

Here is the call graph for this function:

◆ ~readerPosixFile()

inputHandler::posixFile::readerPosixFile::~readerPosixFile ( )
override

Definition at line 73 of file inputHandlerPosixFile.cpp.

73  {
74  if (fd != -1) {
75  if (isUnwinding()) {
76  timerInst(close);
77  if (close(fd) != 0) {
79  path, "close during unwind ",
80  std::system_category().default_error_condition(errno).message());
81  }
82  return;
83  }
84  if (preserveAtime) {
85  struct timespec times[2];
86  readInitialStat.getAtime(times[0]);
87  times[1].tv_nsec = UTIME_OMIT; // leave mtime unchanged
88  timerInst(futimens);
89  throwcall::good0(futimens(fd, times), "can't set time stamps on ", path, " to old values");
90  }
91  {
92  timerInst(close);
93  throwcall::good0(close(fd), "can't close ", path, " after reading");
94  }
95  }
96  }

References errMsg::debug, errMsg::emit(), throwcall::good0(), inputHandler::posixFile::preserveAtime, and timerInst.

Here is the call graph for this function:

Member Function Documentation

◆ checkUnchangedness()

void inputHandler::posixFile::readerPosixFile::checkUnchangedness ( )
overridevirtual

check if the file is unchanged by comparing cuttent stat to initial stat

Exceptions
delayAdvisingErrorif file has changed according to size of mtime
std::system_errorif stat fails

Implements inputHandler::base::reader.

Definition at line 252 of file inputHandlerPosixFile.cpp.

252  {
253  struct stat readFinalStatBuf;
254  {
255  timerInst(fstat);
256  throwcall::good0(fstat(fd, &readFinalStatBuf), "can't stat path file ", path);
257  }
258  genericStat readFinalStat(readFinalStatBuf, std::chrono::nanoseconds(1));
259  if (readFinalStat.size != readInitialStat.size) {
260  throw delayAdvisingError("file size has changed (" +
261  std::to_string(readInitialStat.size) +
262  " -> " +
263  std::to_string(readFinalStat.size) +
264  ") during reading on " + path);
265  }
266 
267  if (!readFinalStat.isSameMtimeAs(readInitialStat)) {
268  throw delayAdvisingError("file " + path + " was modified (" +
269  std::to_string(std::chrono::duration_cast<std::chrono::duration<double>>(readFinalStat.getMtime() - readInitialStat.getMtime()).count()) +
270  "s different mtime) during reading");
271  }
272  }

References genericStat::getMtime(), throwcall::good0(), genericStat::isSameMtimeAs(), genericStat::size, and timerInst.

Here is the call graph for this function:

◆ parallelizable()

bool inputHandler::posixFile::readerPosixFile::parallelizable ( ) const
overridevirtual

tell if this handler is capable of parallel IO. Unsually not the case

Reimplemented from ioHandle.

Definition at line 151 of file inputHandlerPosixFile.cpp.

151  {
152  return regions.empty();
153  }

◆ readBlock()

bool inputHandler::posixFile::readerPosixFile::readBlock ( block b)
overridevirtual

read one block from the file

Returns
true if the last block was read, false if more to follow
Exceptions
std::system_errorif read fails
delayAdvisingErrorif file changes during reading

Implements inputHandler::base::reader.

Definition at line 154 of file inputHandlerPosixFile.cpp.

154  {
156  bool lastblock = false;
157 
158  auto bytesToRead = b.max_size();
159  if (!regions.empty()) {
160  auto region = regions.front();
161  if (region.isHole()) {
162  b.setHoleState(true);
163  b.bump_size(region.size());
164  timerInst(lseek);
165  size_t nextData;
166  auto result = lseek(fd, totalBytesRead, SEEK_DATA);
167  if (result == -1 && errno == ENXIO) { // this is the final hole
168  nextData = region.getEnd();
169  } else {
170  throwcall::badval(result, -1, "seek in ", path, " to data after ", totalBytesRead);
171  nextData = result;
172  }
173  totalBytesRead += region.size(); // hole counts as total size...
174  if (nextData != totalBytesRead) {
175  throw delayAdvisingError(path + " hole moved druring reading, (" +
176  std::to_string(totalBytesRead) +
177  " -> " +
178  std::to_string(nextData) +
179  ")");
180  }
181  regions.pop_front();
182  return regions.empty();
183  } else { // we are somewhere in a data region...
184  if (region.getEnd() < totalBytesRead + bytesToRead) {
185  bytesToRead = region.getEnd() - totalBytesRead; // read only up to end of region
186  }
187  if (region.getEnd() == totalBytesRead + bytesToRead) { // we finish the region in this block
188  regions.pop_front();
189  }
190  }
191  }
192 
193  while (b.size() + blockSize <= bytesToRead) {
195  timerInst(read);
196  auto bytes_read = throwcall::badval(read(fd, b.bufferAt(b.size()), blockSize),
197  -1, "read failed on ", path);
198  readRateLimit.update(bytes_read);
199  if (bytes_read == 0) {
200  lastblock = true;
202  throw delayAdvisingError(path + " has shrunk while reading, (" +
203  std::to_string(readInitialStat.size) +
204  " -> " +
205  std::to_string(totalBytesRead) +
206  ")");
207  }
208  break;
209  }
210  totalBytesRead += bytes_read;
212  throw delayAdvisingError(path + " has grown while reading, (" +
213  std::to_string(readInitialStat.size) +
214  " -> " +
215  std::to_string(totalBytesRead) +
216  ")");
217  }
218 
219  b.bump_size(bytes_read);
220  }
221 
222  return lastblock;
223  }

References throwcall::badval(), block::bufferAt(), block::bump_size(), block::clear(), block::max_size(), readRateLimit, block::setHoleState(), block::size(), timerInst, throttle::watch::update(), and throttle::watch::wait().

Referenced by inputHandler::Gpfs::readerGpfs::readBlock().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readBlockP()

void inputHandler::posixFile::readerPosixFile::readBlockP ( block ,
size_t  ,
off_t   
)
overridevirtual

read one block from the file, starting at offset.

meant for pparallel reading, often not implemented only for non-sparse files

Exceptions
std::system_errorif read fails
delayAdvisingErrorif file changes during reading
std::logic_errorif attempted on sparse file

Reimplemented from inputHandler::base::reader.

Definition at line 226 of file inputHandlerPosixFile.cpp.

226  {
227  if (!regions.empty()) {
228  throw std::logic_error("parallel read attempted on sparse file");
229  }
230  b.clear(offset);
231  while (b.size() + blockSize <= b.max_size()) {
232  timerInst(pread);
233  auto bytes_read = throwcall::badval(pread(fd, b.bufferAt(b.size()), blockSize, offset + b.size()),
234  -1, "read failed on ", path);
235  if (bytes_read == 0) {
236  break;
237  }
238  b.bump_size(bytes_read);
239  if (b.size() > bytesToRead) {
240  throw delayAdvisingError(path + " has grown while reading");
241  }
242  }
243  if (b.size() < bytesToRead) {
244  throw delayAdvisingError(path + " has shrunk while reading "
245  + std::to_string(bytesToRead)
246  + " "
247  + std::to_string(b.size()));
248  }
249  }

References throwcall::badval(), block::bufferAt(), block::bump_size(), block::clear(), block::max_size(), block::size(), and timerInst.

Referenced by inputHandler::Gpfs::readerGpfs::readBlockP().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ seek()

void inputHandler::posixFile::readerPosixFile::seek ( size_t  )
overridevirtual

like the standard seek, to be used only when appending new data to a file

Exceptions
std::system_errorif seek fails
unimplementedActionErrorif seek is not implemented (most input handlers)

Reimplemented from inputHandler::base::reader.

Definition at line 139 of file inputHandlerPosixFile.cpp.

139  {
140  throwcall::badval(lseek(fd, pos, SEEK_SET),
141  -1, "can't seek ", path, " to ", pos);
142  if (! regions.empty()) {
143  while (regions.front().getEnd() < pos) {
144  regions.pop_front();
145  if (regions.empty()) {
146  break;
147  }
148  }
149  }
150  }

References throwcall::badval().

Here is the call graph for this function:

◆ setupSparseRegions()

bool inputHandler::posixFile::readerPosixFile::setupSparseRegions ( const std::string &  sparseHandling)
overridevirtual

create a region list which maps the holes in the source file

Reimplemented from inputHandler::base::reader.

Definition at line 103 of file inputHandlerPosixFile.cpp.

103  {
104  if (readInitialStat.size < readInitialStat.blksize) { // cannot be sparse
105  return false;
106  }
107  if (sparseHandling == "auto" &&
109  return false;
110  } else if (sparseHandling == "never") {
111  return false;
112  }
113  auto what = SEEK_HOLE;
114  for (std::remove_const<decltype(readInitialStat.size)>::type pos = 0;
115  pos < readInitialStat.size;
116  what = what == SEEK_HOLE ? SEEK_DATA : SEEK_HOLE) {
117  timerInst(lseek);
118  size_t next;
119  auto result = lseek(fd, pos, what);
120  if (result == -1 && errno == ENXIO) { // we found a hole at the end of the file
121  next = readInitialStat.size;
122  } else {
123  throwcall::badval(result, -1,
124  "seek ", path, " for ", what == SEEK_HOLE ? "hole" : "data",
125  ", from offset ", pos);
126  next = result;
127  }
128  if (next != pos) {
129  regions.emplace_back(pos, next, what == SEEK_DATA);
130  pos = next;
131  }
132  }
133  timerInst(lseek);
134  throwcall::good0(lseek(fd, 0, SEEK_SET), "re-seek to start of ", path);
135  return !regions.empty();
136  }

References throwcall::badval(), throwcall::good0(), and timerInst.

Here is the call graph for this function:

Member Data Documentation

◆ regions

std::deque<regionType> inputHandler::posixFile::readerPosixFile::regions
protected

Definition at line 46 of file inputHandlerPosixFile.h.


The documentation for this class was generated from the following files:
genericStat::mode
mode_t mode
Definition: genericStat.h:22
delayAdvisingError
class for exceptions that advise for delays Exceptions of this kind are to be thrown when circumstanc...
Definition: inputHandler.h:22
block::max_size
size_t max_size() const
Definition: block.h:22
errMsg::location
class for defining the location of a error message in the source code.
Definition: errMsgQueue.h:14
genericStat::getAtime
void getAtime(struct timespec &spec) const
Definition: genericStat.cpp:62
throwcall::badval
T badval(T call, t badvalue, const Args &... args)
template function to wrap system calls that return a special bad value on failure
Definition: throwcall.h:54
unwindCheck::isUnwinding
bool isUnwinding() const
Definition: unwindCheck.h:19
genericStat
generic stat abstraction class Used to abstract the variants of the stat structure.
Definition: genericStat.h:12
posixFileIoCommon::fd
int fd
Definition: posixFileCommon.h:21
genericStat::sizeOnDisk
size_t sizeOnDisk
Definition: genericStat.h:17
genericStat::getMtime
void getMtime(struct timespec &spec) const
Definition: genericStat.cpp:65
copyRequest::stateBitType::vanished
@ vanished
block::bump_size
void bump_size(size_t additionalBytes)
Definition: block.h:33
inputHandler::base::reader::readInitialStat
const genericStat & readInitialStat
stat at beginning, needed for unchngedness check
Definition: inputHandler.h:74
readRateLimit
throttle::watch readRateLimit
errMsg::level::debug
@ debug
genericStat::blksize
size_t blksize
Definition: genericStat.h:18
ioHandle::blockSize
size_t blockSize
in bytes, block size to be used when reading or writing
Definition: ioHandle.h:17
inputHandler::posixFile::preserveAtime
static options::single< bool > preserveAtime
Definition: inputHandlerPosixFile.h:17
genericStat::size
size_t size
Definition: genericStat.h:16
inputHandler::posixFile::readerPosixFile::regions
std::deque< regionType > regions
Definition: inputHandlerPosixFile.h:46
block::setHoleState
void setHoleState(bool holeState)
Definition: block.h:39
block::bufferAt
void * bufferAt(size_t offset)
only way to access the data in the block
Definition: block.cpp:28
inputHandler::base::reader::totalBytesRead
std::remove_const< decltype(readInitialStat.size)>::type totalBytesRead
number of bytes read so far, needed for check on growing files
Definition: inputHandler.h:75
inputHandler::base::reader::reader
reader(const genericStat &inititalStat)
Definition: inputHandler.h:76
throttle::watch::wait
void wait()
Definition: throttle.h:50
errMsg::emit
void emit(level aLogLevel, const location &loc, const std::string &aObject, const std::string &aAction, const Args &... args)
function to create and enqueue a message, this is the only way that messages should be created!
Definition: errMsgQueue.h:148
timerInst
#define timerInst(subfunc)
Definition: timer.h:157
block::size
size_t size() const
Definition: block.h:16
throwcall::good0
void good0(T call, const Args &... args)
template function to wrap system calls that return 0 on success
Definition: throwcall.h:40
block::clear
void clear(size_t aOffset)
Definition: block.h:28
posixFileIoCommon::posixFileIoCommon
posixFileIoCommon(const std::string &aPath)
Definition: posixFileCommon.cpp:110
throttle::watch::update
void update(double units=1.0)
Definition: throttle.h:35
posixFileIoCommon::path
const std::string & path
Definition: posixFileCommon.h:20