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

#include <outputHandlerLibssh.h>

Inheritance diagram for outputHandler::libssh::writerLibssh:
[legend]
Collaboration diagram for outputHandler::libssh::writerLibssh:
[legend]

Public Member Functions

 writerLibssh (const std::string &aPath, libsshCommon &aHandler, bool mightAppend, size_t sourceSize, size_t readBlockSize, copyRequest::stateType &state, bool noWrite, std::unique_ptr< ioHandle::attrDataType > attrData, std::unique_ptr< acl::list > aclData)
 
 ~writerLibssh () noexcept(false) override
 
void closeAndRemoveBadCopy () override
 
void seek (size_t position) override
 
size_t getSize () const override
 
void writeBlock (const block &b) override
 
void doAttributePreservations (const genericStat &readInitialStat) override
 
void sync () override
 
- Public Member Functions inherited from libsshIoCommon
 libsshIoCommon (const std::string &aPath, libsshCommon &aHandler)
 
std::unique_ptr< const genericStatgetStat () override
 
- Public Member Functions inherited from ioHandle
 ioHandle ()
 
virtual ~ioHandle () noexcept(false)
 
virtual bool parallelizable () const
 tell if this handler is capable of parallel IO. Unsually not the case More...
 
virtual void setXattr (const std::string &, const std::string &)
 
virtual std::string getXattr (const std::string &)
 get one extended attribute value More...
 
virtual void removeXattr (const std::string &)
 
virtual size_t getBlockSize () const
 
virtual void setBlockSize (size_t newSize)
 
virtual std::unique_ptr< attrDataTypegetAttrData (const outputHandler::base *)
 get attributes in the optimal way for setting with aOutputHandler More...
 
virtual std::unique_ptr< acl::listgetAclData ()
 get acls More...
 

Protected Attributes

sftp_attributes writeInitialStat
 
std::unique_ptr< ioHandle::attrDataTypeattrData
 
- Protected Attributes inherited from libsshIoCommon
libsshCommonhandler
 
const std::string path
 
sftp_file file
 
- Protected Attributes inherited from ioHandle
size_t blockSize
 in bytes, block size to be used when reading or writing More...
 

Detailed Description

Definition at line 13 of file outputHandlerLibssh.h.

Constructor & Destructor Documentation

◆ writerLibssh()

outputHandler::libssh::writerLibssh::writerLibssh ( const std::string &  aPath,
libsshCommon aHandler,
bool  mightAppend,
size_t  sourceSize,
size_t  readBlockSize,
copyRequest::stateType state,
bool  noWrite,
std::unique_ptr< ioHandle::attrDataType attrData,
std::unique_ptr< acl::list aclData 
)

Definition at line 220 of file outputHandlerLibssh.cpp.

228  :
229  libsshIoCommon(aPath, aHandler),
230  writeInitialStat(nullptr),
231  attrData(std::move(aAttrData)) {
232  if (noWrite) {
233  file = nullptr;
234  blockSize = 1024 * 1024;
235  } else {
236  auto openMode = O_CREAT | O_TRUNC | O_WRONLY;
237  state.clear(copyRequest::stateBitType::append); // clear any old append bits, may be left over from former attempts
238  if (mightAppend) {
239  writeInitialStat = sftp_stat(handler.sftp, path.c_str());
240  if (writeInitialStat == nullptr) {
241  if (sftp_get_error(handler.sftp) != SSH_FX_NO_SUCH_FILE) {
242  throwcall::sftp::good0(1, handler, "can't stat destination file ", path);
243  }
244  } else {
245  if (static_cast<size_t>(writeInitialStat->size) < sourceSize &&
246  sourceSize > readBlockSize) {
248  openMode = O_WRONLY;
249  }
250  }
251  }
252  static timer::anchor a("sftp_open");
254  file = throwcall::sftp::badval(sftp_open(handler.sftp, path.c_str(), openMode, S_IWUSR),
255  nullptr, handler,
256  "can't open ", path, " for writing");
257  i.stop();
258  if (writeInitialStat == nullptr) {
259  timerInst(sftp_fstat);
260  writeInitialStat = throwcall::sftp::badval(sftp_fstat(file), nullptr,
261  handler,
262  "can't stat destination file ", path);
263  }
264  blockSize = 16 * 1024;
265  }
266  };

References copyRequest::append, throwcall::sftp::badval(), ioHandle::blockSize, enumAsBitmask< T >::clear(), libsshIoCommon::file, throwcall::sftp::good0(), libsshIoCommon::handler, libsshIoCommon::path, libsshCommon::sftp, timer::instanceUnscoped::stop(), timerInst, and writeInitialStat.

Here is the call graph for this function:

◆ ~writerLibssh()

outputHandler::libssh::writerLibssh::~writerLibssh ( )
overridenoexcept

Definition at line 290 of file outputHandlerLibssh.cpp.

290  {
291  if (writeInitialStat != nullptr) {
292  sftp_attributes_free(writeInitialStat);
293  }
294  if (file != nullptr) {
295  if (isUnwinding()) {
297  return;
298  }
299  if (attrData) {
300  try {
301  attrData->set(this);
302  } catch (const std::exception& e) {
304  path, "set attr at close ",
305  e.what());
307  throw;
308  }
309  }
310  timerInst(sftp_close);
311  throwcall::badval(sftp_close(file), SSH_ERROR, "can't close ", path);
312  }
313  };

References throwcall::badval(), errMsg::debug, errMsg::emit(), and timerInst.

Here is the call graph for this function:

Member Function Documentation

◆ closeAndRemoveBadCopy()

void outputHandler::libssh::writerLibssh::closeAndRemoveBadCopy ( )
override

Definition at line 268 of file outputHandlerLibssh.cpp.

268  {
269  static timer::anchor a("sftp_close");
271  if (sftp_close(file) == SSH_ERROR) {
273  path, "close during unwind ",
274  "sftp error ", sftp_get_error(handler.sftp),
275  ", ssh error ", ssh_get_error(handler.session));
276  }
277  i.stop();
279  path, "unlink failed copy", "due to exception");
280  timerInst(sftp_unlink);
281  auto retval = sftp_unlink(handler.sftp, path.c_str());
282  if (retval && sftp_get_error(handler.sftp) != SSH_FX_NO_SUCH_FILE) {
284  path, "can't remove bad copy ",
285  "sftp error ", sftp_get_error(handler.sftp),
286  ", ssh error ", ssh_get_error(handler.session));
287  }
288  };

References errMsg::debug, errMsg::emit(), errMsg::err, timer::instanceUnscoped::stop(), and timerInst.

Here is the call graph for this function:

◆ doAttributePreservations()

void outputHandler::libssh::writerLibssh::doAttributePreservations ( const genericStat readInitialStat)
override

Definition at line 353 of file outputHandlerLibssh.cpp.

353  {
354  if (preserve.timestamps) {
355  struct timeval times[2];
356  readInitialStat.getAtime(times[0]);
357  readInitialStat.getMtime(times[1]);
358  timerInst(sftp_utimes);
359  throwcall::sftp::good0(sftp_utimes(handler.sftp, path.c_str(), times), handler,
360  "can't set time stamp of ", path);
361  }
362 
363  if (preserve.mode) {
364  timerInst(sftp_chmod);
365  throwcall::sftp::good0(sftp_chmod(handler.sftp, path.c_str(), readInitialStat.mode), handler,
366  "can't set mode of ", path);
367  } else {
368  auto oldmask = umask(0);
369  timerInst(sftp_chmod);
370  throwcall::sftp::good0(sftp_chmod(handler.sftp, path.c_str(), modeBits & ~oldmask), handler,
371  "can't set mode of ", path);
372  umask(oldmask);
373  }
374 
375  if (gid != -1 || uid != -1) {
376  timerInst(sftp_chown);
377  throwcall::sftp::good0(sftp_chown(handler.sftp, path.c_str(), uid, gid), handler,
378  "can't set owner/group (", uid, ",", gid, ") of ", path);
379  } else if (preserve.ownership) {
380  timerInst(sftp_chown);
381  throwcall::sftp::good0(sftp_chown(handler.sftp, path.c_str(),
382  readInitialStat.ownerUid, readInitialStat.ownerGid),
383  handler,
384  "can't set owner/group (", readInitialStat.ownerUid, ",", readInitialStat.ownerGid, ") of ",
385  path);
386  }
387  }

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

Here is the call graph for this function:

◆ getSize()

size_t outputHandler::libssh::writerLibssh::getSize ( ) const
override

Definition at line 320 of file outputHandlerLibssh.cpp.

320  {
321  return writeInitialStat->size;
322  }

◆ seek()

void outputHandler::libssh::writerLibssh::seek ( size_t  position)
override

Definition at line 316 of file outputHandlerLibssh.cpp.

316  {
317  throwcall::sftp::good0(sftp_seek64(file, position), handler,
318  "can't seek ", path, " to ", position);
319  }

References throwcall::sftp::good0().

Here is the call graph for this function:

◆ sync()

void outputHandler::libssh::writerLibssh::sync ( )
override

Definition at line 390 of file outputHandlerLibssh.cpp.

390  {
391  // old versions of libssh do not have fsync...
392  #if LIBSFTP_VERSION > 3
394  "can't sync ", path);
395  #endif
396  }

References throwcall::sftp::good0().

Here is the call graph for this function:

◆ writeBlock()

void outputHandler::libssh::writerLibssh::writeBlock ( const block b)
override

Definition at line 324 of file outputHandlerLibssh.cpp.

324  {
325  if (b.isHole()) { // just write a hole...
326  auto currentPosition = sftp_tell64(file); // cannot fail, in constrast to docs
327  auto holeEnd = currentPosition + b.size();
328  throwcall::sftp::good0(sftp_seek64(file, holeEnd), handler,
329  "can't seek to hole end (", holeEnd, ") in ", path);
330  return;
331  }
332 
333  size_t bytes_writen_so_far = 0;
334 
335  while (bytes_writen_so_far < b.size()) {
336  auto count = b.size() - bytes_writen_so_far;
337 
338  if (count > blockSize) {
339  count = blockSize;
340  }
342  timerInst(sftp_write);
343  auto bytes_writen = sftp_write(file, b.bufferAt(bytes_writen_so_far), count);
344  if (bytes_writen < 0) {
345  throwcall::sftp::good0(-1, handler, "write failed on ", path);
346  }
347  writeRateLimit.update(bytes_writen);
348  bytes_writen_so_far += bytes_writen;
349  }
350  }

References block::bufferAt(), throwcall::sftp::good0(), block::isHole(), block::size(), timerInst, throttle::watch::update(), throttle::watch::wait(), and writeRateLimit.

Here is the call graph for this function:

Member Data Documentation

◆ attrData

std::unique_ptr<ioHandle::attrDataType> outputHandler::libssh::writerLibssh::attrData
protected

Definition at line 16 of file outputHandlerLibssh.h.

◆ writeInitialStat

sftp_attributes outputHandler::libssh::writerLibssh::writeInitialStat
protected

Definition at line 15 of file outputHandlerLibssh.h.

Referenced by writerLibssh().


The documentation for this class was generated from the following files:
genericStat::ownerGid
gid_t ownerGid
Definition: genericStat.h:24
genericStat::mode
mode_t mode
Definition: genericStat.h:22
libsshCommon::session
ssh_session session
Definition: libsshCommon.h:27
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
libsshIoCommon::file
sftp_file file
Definition: libsshCommon.h:60
outputHandler::libssh::writerLibssh::closeAndRemoveBadCopy
void closeAndRemoveBadCopy() override
Definition: outputHandlerLibssh.cpp:268
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
libsshIoCommon::path
const std::string path
Definition: libsshCommon.h:59
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
libsshIoCommon::handler
libsshCommon & handler
Definition: libsshCommon.h:58
libsshIoCommon::libsshIoCommon
libsshIoCommon(const std::string &aPath, libsshCommon &aHandler)
Definition: libsshCommon.cpp:198
outputHandler::libssh::writerLibssh::writeInitialStat
sftp_attributes writeInitialStat
Definition: outputHandlerLibssh.h:15
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
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
libsshCommon::sftp
sftp_session sftp
Definition: libsshCommon.h:28
enumAsBitmask::clear
void clear(const T aBits)
Definition: enumAsBitmask.h:31
throwcall::sftp::badval
T badval(T call, t badvalue, libsshCommon &handler, const Args &... args)
Definition: libsshCommon.h:82
throttle::watch::update
void update(double units=1.0)
Definition: throttle.h:35
outputHandler::libssh::writerLibssh::attrData
std::unique_ptr< ioHandle::attrDataType > attrData
Definition: outputHandlerLibssh.h:16
copyRequest::stateBitType::append
@ append
genericStat::ownerUid
uid_t ownerUid
Definition: genericStat.h:23
modeBits
options::single< modeBitType > modeBits
throwcall::sftp::good0
void good0(T call, libsshCommon &handler, const Args &... args)
Definition: libsshCommon.h:70