![]() |
RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* sftp.h -*- C++ -*- 00002 Jeremy Barnes, 21 June 2012 00003 Copyright (c) 2012 Datacratic. All rights reserved. 00004 00005 Sftp functionality. 00006 */ 00007 00008 #pragma once 00009 00010 #include <string> 00011 #include <libssh2.h> 00012 #include <libssh2_sftp.h> 00013 #include <functional> 00014 00015 00016 namespace Datacratic { 00017 00018 00019 /*****************************************************************************/ 00020 /* SOCKET CONNECTION */ 00021 /*****************************************************************************/ 00022 00023 struct SocketConnection { 00024 int sock; 00025 00026 SocketConnection(); 00027 00028 ~SocketConnection(); 00029 00030 void connect(const std::string & hostname, 00031 const std::string & port); 00032 00033 void close(); 00034 }; 00035 00036 00037 /*****************************************************************************/ 00038 /* SSH CONNECTION */ 00039 /*****************************************************************************/ 00040 00041 struct SshConnection : public SocketConnection { 00042 LIBSSH2_SESSION *session; 00043 00044 SshConnection(); 00045 00046 ~SshConnection(); 00047 00048 void connect(const std::string & hostname, 00049 const std::string & port); 00050 00051 void passwordAuth(const std::string & username, 00052 const std::string & password); 00053 00054 void publicKeyAuth(const std::string & username, 00055 const std::string & publicKeyFile, 00056 const std::string & privateKeyFile); 00057 00058 void setBlocking(); 00059 00060 std::string lastError() const; 00061 00062 void close(); 00063 }; 00064 00065 00066 /*****************************************************************************/ 00067 /* SFTP CONNECTION */ 00068 /*****************************************************************************/ 00069 00070 struct SftpConnection : public SshConnection { 00071 LIBSSH2_SFTP *sftp_session; 00072 00073 SftpConnection(); 00074 00075 ~SftpConnection(); 00076 00077 void connectPasswordAuth(const std::string & hostname, 00078 const std::string & username, 00079 const std::string & password, 00080 const std::string & port = "ssh"); 00081 00082 void connectPublicKeyAuth(const std::string & hostname, 00083 const std::string & username, 00084 const std::string & publicKeyFile, 00085 const std::string & privateKeyFile, 00086 const std::string & port = "ssh"); 00087 00088 struct Attributes : public LIBSSH2_SFTP_ATTRIBUTES { 00089 }; 00090 00091 struct File { 00092 std::string path; 00093 LIBSSH2_SFTP_HANDLE *handle; 00094 SftpConnection * owner; 00095 00096 File(const std::string & path, 00097 LIBSSH2_SFTP_HANDLE * handle, 00098 SftpConnection * owner); 00099 00100 ~File(); 00101 00102 Attributes getAttr() const; 00103 00104 uint64_t size() const; 00105 00106 void downloadTo(const std::string & filename) const; 00107 }; 00108 00109 struct Directory { 00110 std::string path; 00111 LIBSSH2_SFTP_HANDLE *handle; 00112 SftpConnection * owner; 00113 00114 Directory(const std::string & path, 00115 LIBSSH2_SFTP_HANDLE * handle, 00116 SftpConnection * owner); 00117 00118 ~Directory(); 00119 00120 void ls() const; 00121 00122 typedef std::function<void (std::string, Attributes)> OnFile; 00123 00124 void forEachFile(const OnFile & onFile) const; 00125 }; 00126 00127 Directory getDirectory(const std::string & path); 00128 00129 File openFile(const std::string & path); 00130 00131 void uploadFile(const char * start, 00132 size_t size, 00133 const std::string & path); 00134 00135 void close(); 00136 }; 00137 00138 00139 00140 00141 } // namespace Datacratic