| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2013 by Eran Ifrah |
| 5 |
// file name : cl_sftp.h |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
#ifndef CLSCP_H |
| 27 |
#define CLSCP_H |
| 28 |
|
| 29 |
#if USE_SFTP |
| 30 |
|
| 31 |
#include "cl_exception.h" |
| 32 |
#include "cl_sftp_attribute.h" |
| 33 |
#include "cl_ssh.h" |
| 34 |
#include "codelite_exports.h" |
| 35 |
#include "ssh_account_info.h" |
| 36 |
#include <memory> |
| 37 |
#include <wx/buffer.h> |
| 38 |
#include <wx/filename.h> |
| 39 |
|
| 40 |
// We do it this way to avoid exposing the include to <libssh/sftp.h> to files including this header |
| 41 |
struct sftp_session_struct; |
| 42 |
typedef struct sftp_session_struct* SFTPSession_t; |
| 43 |
|
| 44 |
class WXDLLIMPEXP_CL clSFTP |
| 45 |
{ |
| 46 |
clSSH::Ptr_t m_ssh; |
| 47 |
SFTPSession_t m_sftp; |
| 48 |
bool m_connected; |
| 49 |
wxString m_currentFolder; |
| 50 |
wxString m_account; |
| 51 |
|
| 52 |
public: |
| 53 |
typedef std::shared_ptr<clSFTP> Ptr_t; |
| 54 |
enum { |
| 55 |
SFTP_BROWSE_FILES = 0x00000001, |
| 56 |
SFTP_BROWSE_FOLDERS = 0x00000002, |
| 57 |
}; |
| 58 |
|
| 59 |
protected: |
| 60 |
wxString GetErrorString() const; |
| 61 |
wxString ExecuteCommand(const wxString& command); |
| 62 |
|
| 63 |
public: |
| 64 |
clSFTP(clSSH::Ptr_t ssh); |
| 65 |
virtual ~clSFTP(); |
| 66 |
|
| 67 |
/** |
| 68 |
* @brief return the default download folder |
| 69 |
*/ |
| 70 |
static wxString GetDefaultDownloadFolder(const SSHAccountInfo& accountInfo); |
| 71 |
|
| 72 |
/** |
| 73 |
* @brief given a remote file path, return the path to the local |
| 74 |
* file used by SFTP |
| 75 |
*/ |
| 76 |
static wxFileName GetLocalFileName(const SSHAccountInfo& accountInfo, const wxString& remotePath, |
| 77 |
bool mkdirRecrusive = false); |
| 78 |
|
| 79 |
/** |
| 80 |
* @brief return the underlying ssh session |
| 81 |
*/ |
| 82 |
clSSH::Ptr_t GetSsh() const { return m_ssh; } |
| 83 |
|
| 84 |
bool IsConnected() const { return m_connected; } |
| 85 |
|
| 86 |
void SetAccount(const wxString& account) { this->m_account = account; } |
| 87 |
const wxString& GetAccount() const { return m_account; } |
| 88 |
/** |
| 89 |
* @brief intialize the scp over ssh |
| 90 |
*/ |
| 91 |
void Initialize(); |
| 92 |
|
| 93 |
/** |
| 94 |
* @brief close the scp channel |
| 95 |
*/ |
| 96 |
void Close(); |
| 97 |
|
| 98 |
/** |
| 99 |
* @brief return checksum of a remote file |
| 100 |
* @throws clException |
| 101 |
*/ |
| 102 |
bool GetChecksum(const wxString& remoteFile, size_t* checksum); |
| 103 |
|
| 104 |
/** |
| 105 |
* @brief write the content of local file into a remote file |
| 106 |
* @param localFile the local file |
| 107 |
* @param remotePath the remote path (abs path) |
| 108 |
*/ |
| 109 |
void Write(const wxFileName& localFile, const wxString& remotePath); |
| 110 |
|
| 111 |
/** |
| 112 |
* @brief write the content of 'fileContent' into the remote file represented by remotePath |
| 113 |
*/ |
| 114 |
void Write(const wxMemoryBuffer& fileContent, const wxString& remotePath); |
| 115 |
|
| 116 |
/** |
| 117 |
* @brief create an empty remote file |
| 118 |
*/ |
| 119 |
void CreateEmptyFile(const wxString& remotePath); |
| 120 |
|
| 121 |
/** |
| 122 |
* @brief read remote file and return its content |
| 123 |
* @return the file content + the file attributes |
| 124 |
*/ |
| 125 |
SFTPAttribute::Ptr_t Read(const wxString& remotePath, wxMemoryBuffer& buffer); |
| 126 |
|
| 127 |
/** |
| 128 |
* @brief list the content of a folder |
| 129 |
* @param folder |
| 130 |
* @param foldersOnly |
| 131 |
* @param filter filter out files that do not match the filter |
| 132 |
* @throw clException incase an error occurred |
| 133 |
*/ |
| 134 |
SFTPAttribute::List_t List(const wxString& folder, size_t flags, const wxString& filter = ""); |
| 135 |
|
| 136 |
/** |
| 137 |
* @brief create a directory |
| 138 |
* @param dirname |
| 139 |
*/ |
| 140 |
void CreateDir(const wxString& dirname); |
| 141 |
|
| 142 |
/** |
| 143 |
* @brief create a file. This function also creates the path to the file (by calling internally to Mkpath) |
| 144 |
*/ |
| 145 |
void CreateRemoteFile(const wxString& remoteFullPath, const wxString& content); |
| 146 |
|
| 147 |
/** |
| 148 |
* @brief this version create a copy of the local file on the remote server. Similar to the previous |
| 149 |
* version, it also creates the path to the file if needed |
| 150 |
*/ |
| 151 |
void CreateRemoteFile(const wxString& remoteFullPath, const wxFileName& localFile); |
| 152 |
|
| 153 |
/** |
| 154 |
* @brief create path . If the directory does not exist, create it (all sub paths if needed) |
| 155 |
*/ |
| 156 |
void Mkpath(const wxString& remoteDirFullpath); |
| 157 |
|
| 158 |
/** |
| 159 |
* @brief Remove a directoy. |
| 160 |
* @param dirname |
| 161 |
*/ |
| 162 |
void RemoveDir(const wxString& dirname); |
| 163 |
|
| 164 |
/** |
| 165 |
* @brief Unlink (delete) a file. |
| 166 |
* @param dirname |
| 167 |
*/ |
| 168 |
void UnlinkFile(const wxString& path); |
| 169 |
|
| 170 |
/** |
| 171 |
* @brief Rename or move a file or directory |
| 172 |
* @param oldpath |
| 173 |
* @param newpath |
| 174 |
*/ |
| 175 |
void Rename(const wxString& oldpath, const wxString& newpath); |
| 176 |
/** |
| 177 |
* @brief cd up and list the content of the directory |
| 178 |
* @return |
| 179 |
*/ |
| 180 |
SFTPAttribute::List_t CdUp(size_t flags, const wxString& filter); |
| 181 |
|
| 182 |
/** |
| 183 |
* @brief stat the path |
| 184 |
*/ |
| 185 |
SFTPAttribute::Ptr_t Stat(const wxString& path); |
| 186 |
|
| 187 |
/** |
| 188 |
* @brief change remote file permissions |
| 189 |
*/ |
| 190 |
void Chmod(const wxString& remotePath, size_t permissions); |
| 191 |
|
| 192 |
/** |
| 193 |
* @brief change remote directory |
| 194 |
* @param remotePath |
| 195 |
*/ |
| 196 |
SFTPAttribute::List_t Chdir(const wxString& remotePath); |
| 197 |
|
| 198 |
/** |
| 199 |
* @brief return the current folder |
| 200 |
*/ |
| 201 |
const wxString& GetCurrentFolder() const { return m_currentFolder; } |
| 202 |
|
| 203 |
/** |
| 204 |
* @brief send keep alive message |
| 205 |
*/ |
| 206 |
void SendKeepAlive(); |
| 207 |
}; |
| 208 |
|
| 209 |
#endif // USE_SFTP |
| 210 |
#endif // CLSCP_H |