ewmscp  ..
libsshCommon.h
Go to the documentation of this file.
1 #ifndef __libsshCommon_h__
2 #define __libsshCommon_h__
3 
4 #include <iostream>
5 
6 #include "ioHandle.h"
7 #include "pathHandler.h"
8 #include "Options.h"
9 #include <libssh/libssh.h>
10 #include <libssh/sftp.h>
11 #include <throwcall.h>
12 #include <mutex>
13 
14 #include "errMsgQueue.h"
15 
17  public:
18  genericSftpStat(const sftp_attributes_struct& attrs,
19  const sftp_statvfs_struct& vfsStat);
20 };
21 
22 
23 
24 
25 class libsshCommon: virtual public pathHandler {
26  public:
27  ssh_session session;
28  sftp_session sftp;
29 
30  class sshOptions {
31  public:
32  sshOptions(const std::string& optPrefix);
37  };
38  protected:
40  void openSessions();
41  void closeSessions();
43  public:
44  void reconnectSessions();
45  template<typename T> std::unique_ptr<T, void(*)(T*)>getUniquePtr(T* obj, void(*deleter)(T*)) {
46  return std::unique_ptr<T, void(*)(T*)>(obj, deleter);
47  };
48  libsshCommon(sshOptions& aOpt);
49  ~libsshCommon() override;
50  bool pathExists(const std::string& path) override;
51  std::unique_ptr<const genericStat> getStat(const std::string& path,
52  bool followLink) override;
53  const sftp_statvfs_struct& getVfsStat(const std::string& path);
54 };
55 
56 class libsshIoCommon: virtual public ioHandle {
57  protected:
59  const std::string path;
60  sftp_file file;
61  public:
62  libsshIoCommon(const std::string& aPath,
63  libsshCommon& aHandler);
64  std::unique_ptr<const genericStat> getStat() override;
65 };
66 
67 
68 namespace throwcall {
69  namespace sftp {
70  template <typename T, typename ... Args> inline void good0(T call,
71  libsshCommon& handler,
72  const Args& ... args) {
73  if (__builtin_expect(call, 0)) {
74  handler.reconnectSessions();
75  std::ostringstream msg;
76  conCatString(msg, "sftp error ", sftp_get_error(handler.sftp),
77  ", ssh error ", ssh_get_error(handler.session),
78  " ", args...);
79  throw std::runtime_error(msg.str());
80  }
81  };
82  template <typename T, typename t, typename ... Args> inline T badval(T call, t badvalue,
83  libsshCommon& handler,
84  const Args& ... args) {
85  if (__builtin_expect(call == badvalue, false)) {
86  std::ostringstream msg;
87  conCatString(msg, "sftp error ", sftp_get_error(handler.sftp),
88  ", ssh error ", ssh_get_error(handler.session),
89  " ", args...);
90  handler.reconnectSessions();
91  throw std::runtime_error(msg.str());
92  }
93  return call;
94  };
95  } // end namespace sftp
96  namespace ssh {
97  template <typename T, typename ... Args> inline void good0(T call,
98  libsshCommon& handler,
99  const Args& ... args) {
100  if (__builtin_expect(call != 0, false)) {
101  std::ostringstream msg;
102  conCatString(msg, "ssh error ", ssh_get_error(handler.session), " ", args...);
103  handler.reconnectSessions();
104  throw std::runtime_error(msg.str());
105  }
106  };
107 
108  template <typename T, typename t, typename ... Args> inline T goodval(T call, t goodvalue,
109  libsshCommon& handler,
110  const Args& ... args) {
111  if (__builtin_expect(call != goodvalue, false)) {
112  std::ostringstream msg;
113  conCatString(msg, "ssh error ", ssh_get_error(handler.session), " ", args...);
114  handler.reconnectSessions();
115  throw std::runtime_error(msg.str());
116  }
117  return call;
118  };
119 
120  template <typename T, typename t, typename ... Args> inline T badval(T call, t badvalue,
121  libsshCommon& handler,
122  const Args& ... args) {
123  if (__builtin_expect(call == badvalue, false)) {
124  std::ostringstream msg;
125  conCatString(msg, "ssh error ", ssh_get_error(handler.session), " ", args...);
126  handler.reconnectSessions();
127  throw std::runtime_error(msg.str());
128  }
129  return call;
130  };
131  } // end namespace ssh
132 } // end namespace throwcall
133 
134 
135 #endif
options::single< std::string >
libsshCommon::session
ssh_session session
Definition: libsshCommon.h:27
errMsgQueue.h
throwcall::conCatString
void conCatString(std::ostringstream &msg, const T &begin)
Definition: throwcall.h:26
libsshCommon::opt
sshOptions & opt
Definition: libsshCommon.h:39
throwcall
Definition: throwcall.h:25
genericStat
generic stat abstraction class Used to abstract the variants of the stat structure.
Definition: genericStat.h:12
libsshCommon
Definition: libsshCommon.h:25
libsshCommon::pathExists
bool pathExists(const std::string &path) override
Definition: libsshCommon.cpp:120
libsshCommon::sshOptions::sshOptions
sshOptions(const std::string &optPrefix)
Definition: libsshCommon.cpp:26
libsshIoCommon::file
sftp_file file
Definition: libsshCommon.h:60
libsshCommon::reconnecting
bool reconnecting
Definition: libsshCommon.h:42
libsshIoCommon::getStat
std::unique_ptr< const genericStat > getStat() override
Definition: libsshCommon.cpp:208
Options.h
libsshIoCommon
Definition: libsshCommon.h:56
libsshCommon::sshOptions
Definition: libsshCommon.h:30
libsshCommon::openSessions
void openSessions()
Definition: libsshCommon.cpp:42
libsshIoCommon::path
const std::string path
Definition: libsshCommon.h:59
libsshCommon::closeSessions
void closeSessions()
Definition: libsshCommon.cpp:91
genericSftpStat::genericSftpStat
genericSftpStat(const sftp_attributes_struct &attrs, const sftp_statvfs_struct &vfsStat)
Definition: libsshCommon.cpp:10
throwcall.h
ioHandle.h
libsshIoCommon::handler
libsshCommon & handler
Definition: libsshCommon.h:58
libsshIoCommon::libsshIoCommon
libsshIoCommon(const std::string &aPath, libsshCommon &aHandler)
Definition: libsshCommon.cpp:198
libsshCommon::sshOptions::knownHostFile
options::single< std::string > knownHostFile
Definition: libsshCommon.h:34
throwcall::ssh::badval
T badval(T call, t badvalue, libsshCommon &handler, const Args &... args)
Definition: libsshCommon.h:120
libsshCommon::sshOptions::verbosity
options::single< std::string > verbosity
Definition: libsshCommon.h:36
pathHandler
Definition: pathHandler.h:6
libsshCommon::getUniquePtr
std::unique_ptr< T, void(*)(T *)> getUniquePtr(T *obj, void(*deleter)(T *))
Definition: libsshCommon.h:45
libsshCommon::getStat
std::unique_ptr< const genericStat > getStat(const std::string &path, bool followLink) override
Definition: libsshCommon.cpp:130
pathHandler.h
libsshCommon::sftp
sftp_session sftp
Definition: libsshCommon.h:28
genericSftpStat
Definition: libsshCommon.h:16
libsshCommon::sshOptions::configFile
options::single< std::string > configFile
Definition: libsshCommon.h:35
libsshCommon::~libsshCommon
~libsshCommon() override
Definition: libsshCommon.cpp:116
ioHandle
class as base for inputHandler::base::reader and outputHandler::base::writer containing the common pa...
Definition: ioHandle.h:15
libsshCommon::sshOptions::host
options::single< std::string > host
Definition: libsshCommon.h:33
throwcall::sftp::badval
T badval(T call, t badvalue, libsshCommon &handler, const Args &... args)
Definition: libsshCommon.h:82
libsshCommon::getVfsStat
const sftp_statvfs_struct & getVfsStat(const std::string &path)
Definition: libsshCommon.cpp:152
throwcall::ssh::goodval
T goodval(T call, t goodvalue, libsshCommon &handler, const Args &... args)
Definition: libsshCommon.h:108
throwcall::ssh::good0
void good0(T call, libsshCommon &handler, const Args &... args)
Definition: libsshCommon.h:97
libsshCommon::libsshCommon
libsshCommon(sshOptions &aOpt)
Definition: libsshCommon.cpp:113
libsshCommon::reconnectSessions
void reconnectSessions()
Definition: libsshCommon.cpp:98
throwcall::sftp::good0
void good0(T call, libsshCommon &handler, const Args &... args)
Definition: libsshCommon.h:70