forked from allanrbo/filesremote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsftpconnection.h
169 lines (116 loc) · 3.61 KB
/
sftpconnection.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2020 Allan Riordan Boll
#ifndef SRC_SFTPCONNECTION_H_
#define SRC_SFTPCONNECTION_H_
#include <wx/secretstore.h>
#include <future> // NOLINT
#include <optional>
#include <string>
#include <vector>
#include "src/direntry.h"
#include "src/hostdesc.h"
#include "src/string.h"
using std::exception;
using std::function;
using std::optional;
using std::string;
using std::vector;
class DownloadFailed : public exception {
public:
string remote_path_;
explicit DownloadFailed(string remote_path) : remote_path_(remote_path) {}
};
class DownloadFailedPermission : public exception {
public:
string remote_path_;
explicit DownloadFailedPermission(string remote_path) : remote_path_(remote_path) {}
};
class UploadFailed : public exception {
public:
string remote_path_;
explicit UploadFailed(string remote_path) : remote_path_(remote_path) {}
};
class FailedPermission : public exception {
public:
string remote_path_;
explicit FailedPermission(string remote_path) : remote_path_(remote_path) {}
};
class UploadFailedSpace : public exception {
public:
string remote_path_;
explicit UploadFailedSpace(string remote_path) : remote_path_(remote_path) {}
};
class DirListFailedPermission : public exception {
public:
string remote_path_;
explicit DirListFailedPermission(string remote_path) : remote_path_(remote_path) {}
};
class DeleteFailed : public exception {
public:
string remote_path_;
string err_;
explicit DeleteFailed(string remote_path, string err) : remote_path_(remote_path), err_(err) {}
};
class FileNotFound : public exception {
public:
string remote_path_;
explicit FileNotFound(string remote_path) : remote_path_(remote_path) {}
};
class ConnectionError : public exception {
public:
string msg_;
explicit ConnectionError(string msg) : msg_(msg) {}
};
class SudoFailed : public exception {
public:
string msg_;
explicit SudoFailed(string msg) : msg_(msg) {}
};
class SftpConnection {
private:
LIBSSH2_SESSION *session_ = NULL;
LIBSSH2_SFTP *sftp_session_ = NULL;
int sock_ = 0;
bool sudo_ = false;
char *userauth_list = NULL;
LIBSSH2_CHANNEL *sudo_channel_ = NULL;
LIBSSH2_CHANNEL *non_sudo_channel_ = NULL;
public:
string home_dir_ = "";
HostDesc host_desc_;
string fingerprint_ = "";
wxSecretValue sudo_passwd_ = wxSecretValue();
explicit SftpConnection(HostDesc host_desc);
vector<DirEntry> GetDir(string path);
bool DownloadFile(
string remote_src_path,
string local_dst_path,
function<bool(void)> cancelled,
function<void(string, uint64_t, uint64_t, uint64_t)> progress);
bool UploadFile(
string local_src_path,
string remote_dst_path,
function<bool(void)> cancelled,
function<void(string, uint64_t, uint64_t, uint64_t)> progress);
optional<DirEntry> Stat(string remote_path);
~SftpConnection();
void Rename(string remote_old_path, string remote_new_path);
void Delete(string remote_path);
void Mkdir(string remote_path);
void Mkfile(string remote_path);
string RealPath(string remote_path);
bool PasswordAuth(wxSecretValue passwd);
bool AgentAuth();
bool KeyAuth();
void SendKeepAlive();
bool CheckSudoInstalled();
bool CheckSudoNeedsPasswd();
void VerifySudoPasswd();
void SftpSubsystemInit();
void SudoEnter(bool needs_passwd_again);
void SudoExit();
private:
string GetLastErrorMsg();
void SendSudoPasswd(LIBSSH2_CHANNEL *channel);
void VerifySudoStillValid();
};
#endif // SRC_SFTPCONNECTION_H_