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

#include <outputHandlerPosixFile.h>

Inheritance diagram for outputHandler::posixFile::writerPosixFile:
[legend]
Collaboration diagram for outputHandler::posixFile::writerPosixFile:
[legend]

Public Member Functions

 writerPosixFile (const std::string &aPath, bool mightAppend, size_t sourceSize, size_t readBlockSize, copyRequest::stateType &state, bool noWrite, std::unique_ptr< ioHandle::attrDataType > aAttrData, std::unique_ptr< acl::list > aAclData)
 
 writerPosixFile (std::string &aPath, bool noWrite, std::unique_ptr< ioHandle::attrDataType > aAttrData, std::unique_ptr< acl::list > aAclData, base &handler)
 
void closeAndRemoveBadCopy () override
 
 ~writerPosixFile () noexcept(false) override
 
void seek (size_t position) override
 
bool parallelizable () const override
 tell if this handler is capable of parallel IO. Unsually not the case More...
 
size_t getSize () const override
 
void writeBlock (const block &b) override
 
void writeBlockP (const block &b) override
 
void doAttributePreservations (const genericStat &readInitialStat) override
 
void sync () override
 
- Public Member Functions inherited from outputHandler::base::writer
 writer ()=default
 
virtual ~writer ()=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

struct stat writeInitialStat
 
std::unique_ptr< ioHandle::attrDataTypeattrData
 
std::unique_ptr< acl::listaclData
 
- 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 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 13 of file outputHandlerPosixFile.h.

Constructor & Destructor Documentation

◆ writerPosixFile() [1/2]

outputHandler::posixFile::writerPosixFile::writerPosixFile ( const std::string &  aPath,
bool  mightAppend,
size_t  sourceSize,
size_t  readBlockSize,
copyRequest::stateType state,
bool  noWrite,
std::unique_ptr< ioHandle::attrDataType aAttrData,
std::unique_ptr< acl::list aAclData 
)

Definition at line 231 of file outputHandlerPosixFile.cpp.

237  :
238  posixFileIoCommon(aPath),
239  attrData(std::move(aAttrData)),
240  aclData(std::move(aAclData)) {
241  bool haveWriteStat = false;
242  if (noWrite) {
243  fd = -1;
244  blockSize = 4096;
245  } else {
246  auto openMode = O_CREAT | O_TRUNC | O_WRONLY;
247  state.clear(copyRequest::stateBitType::append); // clear any old append bits, may be left over from former attempts
248  if (mightAppend) {
249  auto retval = stat(path.c_str(), &writeInitialStat);
250  if (retval) {
251  if (errno != ENOENT) {
252  throwcall::good0(retval, "can't stat destination file ", path);
253  }
254  } else {
255  haveWriteStat = true;
256  if (static_cast<size_t>(writeInitialStat.st_size) < sourceSize &&
257  sourceSize > readBlockSize) {
259  openMode = O_WRONLY;
260  }
261  }
262  }
264  fd = throwcall::badval(open(path.c_str(), openMode, S_IWUSR),
265  -1, "can't open ", path, " for writing");
266  if (!haveWriteStat) {
267  throwcall::good0(fstat(fd, &writeInitialStat), "can't stat destination file ", path);
268  }
269  blockSize = writeInitialStat.st_blksize;
270  throwcall::good0(posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL),
271  "can't advise ", path, " as sequential");
272  throwcall::good0(posix_fadvise(fd, 0, 0, POSIX_FADV_NOREUSE),
273  "can't advise ", path, " as use only once");
274  }
275  };

References copyRequest::append, throwcall::badval(), ioHandle::blockSize, enumAsBitmask< T >::clear(), posixFileIoCommon::fd, throwcall::good0(), posixFileIoCommon::path, outputHandler::slownessThreshold, timerInstTO, and writeInitialStat.

Here is the call graph for this function:

◆ writerPosixFile() [2/2]

outputHandler::posixFile::writerPosixFile::writerPosixFile ( std::string &  aPath,
bool  noWrite,
std::unique_ptr< ioHandle::attrDataType aAttrData,
std::unique_ptr< acl::list aAclData,
base handler 
)

Definition at line 276 of file outputHandlerPosixFile.cpp.

280  :
281  posixFileIoCommon(aPath),
282  attrData(std::move(aAttrData)),
283  aclData(std::move(aAclData)) {
284  if (noWrite) {
285  fd = -1;
286  blockSize = 4096;
287  } else {
288  static const std::string pattern("XXXXXX.tmp");
289  std::vector<char> tmpName;
290  tmpName.reserve(path.size() + pattern.size() + 1);
291  tmpName.insert(tmpName.end(), path.cbegin(), path.cend());
292  handler.shortenNameToMax(path,tmpName,pattern);
293  tmpName.insert(tmpName.end(), pattern.cbegin(), pattern.cend());
294  tmpName.push_back('\0');
295  timerInstTO(mkstemps,slownessThreshold,tmpName);
296  fd = throwcall::badval(mkstemps(tmpName.data(), 4),
297  -1, "can't open ", tmpName.data(), " for writing");
298  aPath.replace(0, path.size(), tmpName.data(), tmpName.size());
299  throwcall::good0(fstat(fd, &writeInitialStat), "can't stat destination file ", path);
300  blockSize = writeInitialStat.st_blksize;
301  throwcall::good0(posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL),
302  "can't advise ", path, " as sequential");
303  throwcall::good0(posix_fadvise(fd, 0, 0, POSIX_FADV_NOREUSE),
304  "can't advise ", path, " as use only once");
305  }
306  };

References throwcall::badval(), ioHandle::blockSize, posixFileIoCommon::fd, throwcall::good0(), posixFileIoCommon::path, outputHandler::base::shortenNameToMax(), outputHandler::slownessThreshold, timerInstTO, and writeInitialStat.

Here is the call graph for this function:

◆ ~writerPosixFile()

outputHandler::posixFile::writerPosixFile::~writerPosixFile ( )
overridenoexcept

Definition at line 323 of file outputHandlerPosixFile.cpp.

323  {
324  if (fd != -1) {
325  if (isUnwinding()) {
327  return;
328  } else {
329  if (attrData) {
330  try {
331  attrData->set(this);
332  } catch (const std::exception& e) {
334  path, "set attr at close ",
335  e.what());
337  throw;
338  }
339  }
341  throwcall::good0(close(fd), "can't close ", path);
342  }
343  }
344  };

References errMsg::debug, errMsg::emit(), throwcall::good0(), outputHandler::slownessThreshold, and timerInstTO.

Here is the call graph for this function:

Member Function Documentation

◆ closeAndRemoveBadCopy()

void outputHandler::posixFile::writerPosixFile::closeAndRemoveBadCopy ( )
overridevirtual

Implements outputHandler::base::writer.

Definition at line 307 of file outputHandlerPosixFile.cpp.

307  {
308  if (close(fd) != 0) {
310  path, "close during unwind ",
311  std::system_category().default_error_condition(errno).message());
312  }
314  path, "unlink failed copy", "due to exception");
315  auto retval = unlink(path.c_str());
316  if (retval && errno != ENOENT) {
318  path, "can't remove bad copy ",
319  std::system_category().default_error_condition(errno).message());
320  }
321  }

References errMsg::debug, errMsg::emit(), and errMsg::err.

Here is the call graph for this function:

◆ doAttributePreservations()

void outputHandler::posixFile::writerPosixFile::doAttributePreservations ( const genericStat readInitialStat)
overridevirtual

Implements outputHandler::base::writer.

Definition at line 410 of file outputHandlerPosixFile.cpp.

410  {
411  if (preserve.timestamps) {
412  struct timespec times[2];
413  readInitialStat.getAtime(times[0]);
414  readInitialStat.getMtime(times[1]);
415  timerInst(futimens);
416  throwcall::good0(futimens(fd, times), "can't set time stamp of ", path);
417  }
418 
419  if (preserve.mode) {
420  timerInst(fchmod);
421  throwcall::good0(fchmod(fd, readInitialStat.mode), "can't set mode of ", path);
422  } else {
423  auto oldmask = umask(0);
424  timerInst(fchmod);
425  throwcall::good0(fchmod(fd, modeBits & ~oldmask), "can't set mode of ", path);
426  umask(oldmask);
427  }
428 
429  if (gid != -1 || uid != -1) {
430  timerInst(fchown);
431  throwcall::good0(fchown(fd, uid, gid), "can't set owner/group (", uid, ",", gid, ") of ", path);
432  } else if (preserve.ownership) {
433  timerInst(fchown);
434  throwcall::good0(fchown(fd, readInitialStat.ownerUid, readInitialStat.ownerGid),
435  "can't set owner/group (", readInitialStat.ownerUid, ",", readInitialStat.ownerGid, ") of ",
436  path);
437  }
438  }

References genericStat::getAtime(), genericStat::getMtime(), gid, throwcall::good0(), genericStat::mode, modeBits, genericStat::ownerGid, genericStat::ownerUid, preserve, timerInst, and uid.

Here is the call graph for this function:

◆ getSize()

size_t outputHandler::posixFile::writerPosixFile::getSize ( ) const
overridevirtual

Reimplemented from outputHandler::base::writer.

Definition at line 354 of file outputHandlerPosixFile.cpp.

354  {
355  return writeInitialStat.st_size;
356  }

◆ parallelizable()

bool outputHandler::posixFile::writerPosixFile::parallelizable ( ) const
overridevirtual

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

Reimplemented from ioHandle.

Definition at line 351 of file outputHandlerPosixFile.cpp.

351  {
352  return writeInitialStat.st_size == 0;
353  }

◆ seek()

void outputHandler::posixFile::writerPosixFile::seek ( size_t  position)
overridevirtual

Reimplemented from outputHandler::base::writer.

Definition at line 346 of file outputHandlerPosixFile.cpp.

346  {
347  timerInst(lseek);
348  throwcall::badval(lseek(fd, position, SEEK_SET),
349  -1, "can't seek ", path, " to ", position);
350  }

References throwcall::badval(), and timerInst.

Here is the call graph for this function:

◆ sync()

void outputHandler::posixFile::writerPosixFile::sync ( )
overridevirtual

Implements outputHandler::base::writer.

Definition at line 477 of file outputHandlerPosixFile.cpp.

477  {
478  timerInst(fsync);
479  throwcall::good0(fsync(fd), "can't sync ", path);
480  }

References throwcall::good0(), and timerInst.

Here is the call graph for this function:

◆ writeBlock()

void outputHandler::posixFile::writerPosixFile::writeBlock ( const block b)
overridevirtual

Implements outputHandler::base::writer.

Definition at line 358 of file outputHandlerPosixFile.cpp.

358  {
359  if (b.isHole()) { // just write a hole...
360  static timer::anchor a("lseek");
362  auto currentPosition = throwcall::badval(lseek(fd, 0, SEEK_CUR), -1, "lseek to determine position on", path);
363  i.stop();
364  auto holeEnd = currentPosition + b.size();
365  {
366  timerInst(ftruncate);
367  throwcall::good0(ftruncate(fd, holeEnd),
368  "fruncate ", path, " to ", holeEnd);
369  }
370  timerInst(lseek);
371  throwcall::badval(lseek(fd, holeEnd, SEEK_SET), -1,
372  "can't seek to hole end (", holeEnd, ") in ", path);
373  return;
374  }
375 
376  size_t bytes_writen_so_far = 0;
377 
378  while (bytes_writen_so_far < b.size()) {
379  auto count = b.size() - bytes_writen_so_far;
380 
381  if (count > blockSize) {
382  count = blockSize;
383  }
385  timerInst(write);
386  auto bytes_writen = throwcall::badval(write(fd, b.bufferAt(bytes_writen_so_far), count),
387  -1, "write failed on ", path);
388  writeRateLimit.update(bytes_writen);
389  bytes_writen_so_far += bytes_writen;
390  }
391  }

References throwcall::badval(), block::bufferAt(), throwcall::good0(), block::isHole(), block::size(), timer::instanceUnscoped::stop(), timerInst, throttle::watch::update(), throttle::watch::wait(), and writeRateLimit.

Referenced by outputHandler::Gpfs::writerGpfs::writeBlock().

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

◆ writeBlockP()

void outputHandler::posixFile::writerPosixFile::writeBlockP ( const block b)
overridevirtual

Reimplemented from outputHandler::base::writer.

Definition at line 394 of file outputHandlerPosixFile.cpp.

394  {
395  size_t bytes_writen_so_far = 0;
396 
397  while (bytes_writen_so_far < b.size()) {
398  auto count = b.size() - bytes_writen_so_far;
399 
400  if (count > blockSize) {
401  count = blockSize;
402  }
403  timerInst(pwrite);
404  auto bytes_writen = throwcall::badval(pwrite(fd, b.bufferAt(bytes_writen_so_far), count, b.offset() + bytes_writen_so_far),
405  -1, "write failed on ", path);
406  bytes_writen_so_far += bytes_writen;
407  }
408  }

References throwcall::badval(), block::bufferAt(), block::offset(), block::size(), and timerInst.

Referenced by outputHandler::Gpfs::writerGpfs::writeBlockP().

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

Member Data Documentation

◆ aclData

std::unique_ptr<acl::list> outputHandler::posixFile::writerPosixFile::aclData
protected

Definition at line 17 of file outputHandlerPosixFile.h.

◆ attrData

std::unique_ptr<ioHandle::attrDataType> outputHandler::posixFile::writerPosixFile::attrData
protected

Definition at line 16 of file outputHandlerPosixFile.h.

◆ writeInitialStat

struct stat outputHandler::posixFile::writerPosixFile::writeInitialStat
protected

Definition at line 15 of file outputHandlerPosixFile.h.

Referenced by writerPosixFile().


The documentation for this class was generated from the following files:
genericStat::ownerGid
gid_t ownerGid
Definition: genericStat.h:24
outputHandler::posixFile::writerPosixFile::writeInitialStat
struct stat writeInitialStat
Definition: outputHandlerPosixFile.h:15
outputHandler::posixFile::writerPosixFile::aclData
std::unique_ptr< acl::list > aclData
Definition: outputHandlerPosixFile.h:17
genericStat::mode
mode_t mode
Definition: genericStat.h:22
block::offset
size_t offset() const
Definition: block.h:25
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
outputHandler::slownessThreshold
static options::single< timer::clock_type::duration > slownessThreshold('\0',"daosFsSlownessThreshold", "duration after which file operations are considered slow", std::chrono::milliseconds(10))
posixFileIoCommon::fd
int fd
Definition: posixFileCommon.h:21
genericStat::getMtime
void getMtime(struct timespec &spec) const
Definition: genericStat.cpp:65
gid
options::single< int > gid
block::isHole
bool isHole() const
Definition: block.h:36
errMsg::level::debug
@ debug
ioHandle::blockSize
size_t blockSize
in bytes, block size to be used when reading or writing
Definition: ioHandle.h:17
outputHandler::posixFile::writerPosixFile::attrData
std::unique_ptr< ioHandle::attrDataType > attrData
Definition: outputHandlerPosixFile.h:16
block::bufferAt
void * bufferAt(size_t offset)
only way to access the data in the block
Definition: block.cpp:28
timer::instanceUnscoped
Definition: timer.h:95
timer::anchor
Definition: timer.h:22
preserve
decltype(preserve) preserve
set of properties to preserve in the copy
Definition: ewmscp.cpp:111
uid
options::single< int > uid
errMsg::level::err
@ err
throttle::watch::wait
void wait()
Definition: throttle.h:50
outputHandler::posixFile::writerPosixFile::closeAndRemoveBadCopy
void closeAndRemoveBadCopy() override
Definition: outputHandlerPosixFile.cpp:307
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
writeRateLimit
throttle::watch writeRateLimit
timerInstTO
#define timerInstTO(subfunc, timeout, object)
Definition: timer.h:158
throwcall::good0
void good0(T call, const Args &... args)
template function to wrap system calls that return 0 on success
Definition: throwcall.h:40
posixFileIoCommon::posixFileIoCommon
posixFileIoCommon(const std::string &aPath)
Definition: posixFileCommon.cpp:110
enumAsBitmask::clear
void clear(const T aBits)
Definition: enumAsBitmask.h:31
throttle::watch::update
void update(double units=1.0)
Definition: throttle.h:35
posixFileIoCommon::path
const std::string & path
Definition: posixFileCommon.h:20
copyRequest::stateBitType::append
@ append
genericStat::ownerUid
uid_t ownerUid
Definition: genericStat.h:23
modeBits
options::single< modeBitType > modeBits