-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsftpsession.cpp
More file actions
233 lines (204 loc) · 6.99 KB
/
sftpsession.cpp
File metadata and controls
233 lines (204 loc) · 6.99 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* ----------------------------------------------------------------------------
* SFTP file manager for QT
*
* Secure file manager applet plug-in for QT
*
* Copyright (C) 2013 Ibrahim Can Yuce <canyuce[[at]]gmail.com>
*
* ----------------------------------------------------------------------------
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
*/
#include "sftpsession.h"
#include "sshconnection.h"
SFTPSession::SFTPSession (
SSHConnection* sshConnection)
{
this->sshConnection = sshConnection;
this->sftp_session = NULL;
this->curDir = NULL;
}
SFTPSession::~SFTPSession ()
{ }
void
SFTPSession::close ()
{
if (this->sftp_session)
closeDir(curDir);
}
int
SFTPSession::init ()
{
this->sftp_session = sftp_new(sshConnection->session);
if (!this->sftp_session) {
MainWindow::logScreen->appendText(QString(
"SFTP error: initialising channel: %1")
.arg(ssh_get_error(sshConnection->
session)));
return 1;
}
if (sftp_init(this->sftp_session)) {
MainWindow::logScreen->appendText(QString(
"SFTP error: initialising channel: %1")
.arg(ssh_get_error(sshConnection->
session)));
return 1;
}
changeDir("/");
return 0;
}
int
SFTPSession::changeDir (
const char* dir)
{
if (this->sftp_session == NULL) {
MainWindow::logScreen->appendText(
"SFTP error: Directory change operation failed. "
"SFTP session has not started yet.");
return 1;
}
// the connection is made
// opening a directory
curDir = sftp_opendir(this->sftp_session, dir);
if (!dir) {
MainWindow::logScreen->appendText(QString(
"SFTP error: Directory not opened: %1")
.arg(ssh_get_error(sshConnection->
session)));
return 1;
}
MainWindow::logScreen->appendText(QString(
"SFTP: Current directory has changed to \"%1\"")
.
arg(dir));
return 0;
}
int
SFTPSession::readDir (
SFTP_DIR* dir, QList<SFTP_ATTRIBUTES*> & fileList)
{
if (this->sftp_session == NULL) {
MainWindow::logScreen->appendText(
"SFTP error: Directory change operation failed. "
"SFTP session has not started yet.");
return 1;
}
// read the whole directory, file by file */
SFTP_ATTRIBUTES* file;
while ((file = sftp_readdir(sftp_session, dir))) {
fileList.append(file);
#if 0
fprintf(stderr, "%30s(%.8o) : %.5d.%.5d : %.10llu bytes\n",
attr->name,
attr->permissions,
attr->uid,
attr->gid,
(long long unsigned int)attr->size);
// TODO : this may cause inconvenience or defect
sftp_attributes_free(attr);
#endif
}
// when attr = NULL, an error has occured OR the directory listing
// is end of file
if (!sftp_dir_eof(dir)) {
MainWindow::logScreen->appendText(QString("Error: %1").arg(
ssh_get_error(sshConnection->
session)));
return 1;
}
return 0;
}
int
SFTPSession::moveFileToHost (
const char* file, const char* targetFileName)
{
SFTP_FILE* targetFile;
int fd = open(file, O_RDONLY);
ssize_t len;
char buffer[BUFFSIZE];
if (this->sftp_session == NULL) {
MainWindow::logScreen->appendText(
"SFTP error: Directory change operation failed. "
"SFTP session has not started yet.");
return 1;
}
if (fd < 0) {
MainWindow::logScreen->appendText(QString("Error: %1: %2").arg(file,
strerror(
errno)));
::close(fd);
return 1;
}
/* open target file */
targetFile = sftp_open(sftp_session, targetFileName, O_WRONLY | O_CREAT, 0);
if (!targetFile) {
MainWindow::logScreen->appendText(QString(
"Error on opening target file: %1: %2")
.
arg(targetFileName,
ssh_get_error(sftp_session)));
::close(fd);
return 1;
}
while ((len = read(fd, buffer, BUFFSIZE)) > 0) {
if (sftp_write(targetFile, buffer, len) != len) {
MainWindow::logScreen->appendText(QString(
"Error: writing bytes: %2").
arg(ssh_get_error(sftp_session)));
::close(fd);
sftp_close(targetFile);
return 1;
}
}
MainWindow::logScreen->appendText(QString(
"File \"%1\" has successfully moved to target host.")
.arg(file));
sftp_close(targetFile);
::close(fd);
return 0;
}
int
SFTPSession::closeDir (
SFTP_DIR* dir)
{
if (sftp_closedir(dir)) {
MainWindow::logScreen->appendText(QString("SFTP error: %1")
.arg(ssh_get_error(sshConnection->
session)));
return 1;
}
return 0;
}
SFTP_DIR*
SFTPSession::readDir (
const char* dirName, QList<SFTP_ATTRIBUTES*> & fileList)
{
SFTP_DIR* dir = sftp_opendir(sftp_session, dirName);
if (!dir) {
MainWindow::logScreen->appendText(QString(
"SFTP error: Directory not opened: %1")
.arg(ssh_get_error(sshConnection->
session)));
return NULL;
}
readDir(dir, fileList);
return dir;
}
SFTP_DIR*
SFTPSession::getCurDir ()
{
return curDir;
}