forked from DonVla/xbmc-svn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsftp.diff
276 lines (250 loc) · 8.51 KB
/
sftp.diff
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
--- xbmc/FileSystem/FileSFTP.cpp 2010-03-27 15:34:09.373562840 +0100
+++ xbmc/FileSystem/FileSFTP.cpp 2010-03-27 15:34:31.046890651 +0100
@@ -30,6 +30,15 @@
using namespace XFILE;
using namespace std;
+
+static CStdString CorrectPath(const CStdString path)
+{
+ if(path == "~" || path.Left(2) == "~/")
+ return "./" + path.Mid(2);
+ else
+ return "/" + path;
+}
+
CSFTPSession::CSFTPSession(const CStdString &host, const CStdString &username, const CStdString &password)
{
CLog::Log(LOGINFO, "SFTPSession: Creating new session on host '%s' with user '%s'", host.c_str(), username.c_str());
@@ -46,13 +55,13 @@
Disconnect();
}
-SFTP_FILE *CSFTPSession::CreateFileHande(const CStdString &file)
+sftp_file CSFTPSession::CreateFileHande(const CStdString &file)
{
if (m_connected)
{
CSingleLock lock(m_critSect);
m_LastActive = CTimeUtils::GetTimeMS();
- SFTP_FILE *handle = sftp_open(m_sftp_session, file.c_str(), O_RDONLY, 0);
+ sftp_file handle = sftp_open(m_sftp_session, CorrectPath(file).c_str(), O_RDONLY, 0);
if (handle)
{
sftp_file_set_blocking(handle);
@@ -67,7 +76,7 @@
return NULL;
}
-void CSFTPSession::CloseFileHandle(SFTP_FILE *handle)
+void CSFTPSession::CloseFileHandle(sftp_file handle)
{
CSingleLock lock(m_critSect);
sftp_close(handle);
@@ -77,12 +86,12 @@
{
if (m_connected)
{
- SFTP_DIR *dir = NULL;
+ sftp_dir dir = NULL;
{
CSingleLock lock(m_critSect);
m_LastActive = CTimeUtils::GetTimeMS();
- dir = sftp_opendir(m_sftp_session, folder.size() == 0 ? "./" : folder.c_str());
+ dir = sftp_opendir(m_sftp_session, CorrectPath(folder).c_str());
}
if (dir)
@@ -90,7 +99,7 @@
bool read = true;
while (read)
{
- SFTP_ATTRIBUTES *attributes = NULL;
+ sftp_attributes attributes = NULL;
{
CSingleLock lock(m_critSect);
@@ -98,7 +107,7 @@
attributes = sftp_readdir(m_sftp_session, dir);
}
- if (attributes && (strcmp(attributes->name, "..") == 0 || strcmp(attributes->name, ".") == 0))
+ if (attributes && (attributes->name == NULL || strcmp(attributes->name, "..") == 0 || strcmp(attributes->name, ".") == 0))
continue;
if (attributes)
{
@@ -109,11 +118,15 @@
{
CSingleLock lock(m_critSect);
sftp_attributes_free(attributes);
- char *realpath = sftp_readlink(m_sftp_session, localPath.c_str());
+ char *realpath = sftp_readlink(m_sftp_session, CorrectPath(localPath).c_str());
+ if(realpath == NULL)
+ continue;
attributes = sftp_stat(m_sftp_session, realpath);
free(realpath);
if (attributes == NULL)
continue;
+ if (attributes && (attributes->name == NULL || strcmp(attributes->name, "..") == 0 || strcmp(attributes->name, ".") == 0))
+ continue;
}
CFileItemPtr pItem(new CFileItem);
@@ -159,8 +172,7 @@
bool CSFTPSession::Exists(const char *path)
{
CSingleLock lock(m_critSect);
-
- SFTP_ATTRIBUTES *attributes = sftp_stat(m_sftp_session, path);
+ sftp_attributes attributes = sftp_stat(m_sftp_session, CorrectPath(path).c_str());
bool exists = attributes != NULL;
if (attributes)
@@ -174,7 +186,7 @@
CSingleLock lock(m_critSect);
memset(buffer, 0, sizeof (buffer));
m_LastActive = CTimeUtils::GetTimeMS();
- SFTP_ATTRIBUTES *attributes = sftp_stat(m_sftp_session, path);
+ sftp_attributes attributes = sftp_stat(m_sftp_session, CorrectPath(path).c_str());
if (attributes)
{
@@ -192,21 +204,21 @@
}
}
-void CSFTPSession::Seek(SFTP_FILE *handle, u64 position)
+void CSFTPSession::Seek(sftp_file handle, uint64_t position)
{
CSingleLock lock(m_critSect);
m_LastActive = CTimeUtils::GetTimeMS();
sftp_seek64(handle, position);
}
-int CSFTPSession::Read(SFTP_FILE *handle, void *buffer, int64_t length)
+int CSFTPSession::Read(sftp_file handle, void *buffer, size_t length)
{
CSingleLock lock(m_critSect);
m_LastActive = CTimeUtils::GetTimeMS();
return sftp_read(handle, buffer, length);
}
-int64_t CSFTPSession::GetPosition(SFTP_FILE *handle)
+int64_t CSFTPSession::GetPosition(sftp_file handle)
{
CSingleLock lock(m_critSect);
m_LastActive = CTimeUtils::GetTimeMS();
@@ -218,7 +230,7 @@
return (CTimeUtils::GetTimeMS() - m_LastActive) > 90000;
}
-bool CSFTPSession::VerifyKnownHost(ssh_session *session)
+bool CSFTPSession::VerifyKnownHost(ssh_session session)
{
switch (ssh_is_server_known(session))
{
@@ -262,7 +274,23 @@
return false;
}
- SSH_OPTIONS *options = ssh_options_new();
+#if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0,4,0)
+ if (ssh_options_set(m_session, SSH_OPTIONS_USER, username.c_str()) < 0)
+ {
+ CLog::Log(LOGERROR, "SFTPSession: Failed to set username '%s' for session", username.c_str());
+ return false;
+ }
+
+ if (ssh_options_set(m_session, SSH_OPTIONS_HOST, host.c_str()) < 0)
+ {
+ CLog::Log(LOGERROR, "SFTPSession: Failed to set host '%s' for session", host.c_str());
+ return false;
+ }
+
+ ssh_options_set(m_session, SSH_OPTIONS_LOG_VERBOSITY, 0);
+
+#else
+ SSH_OPTIONS* options = ssh_options_new();
if (ssh_options_set_username(options, username.c_str()) < 0)
{
@@ -279,6 +307,7 @@
ssh_options_set_log_verbosity(options, 0);
ssh_set_options(m_session, options);
+#endif
if(ssh_connect(m_session))
{
@@ -289,6 +318,7 @@
if (!VerifyKnownHost(m_session))
return false;
+
int noAuth = SSH_AUTH_DENIED;
if ((noAuth = ssh_userauth_none(m_session, NULL)) == SSH_AUTH_ERROR)
{
@@ -308,7 +338,7 @@
// Try to authenticate with password
int passwordAuth = SSH_AUTH_DENIED;
- if (method & SSH_AUTH_METHOD_PASSWORD && (passwordAuth = ssh_userauth_password(m_session, username.c_str(), password.c_str())) == SSH_AUTH_ERROR)
+ if (method & SSH_AUTH_METHOD_PASSWORD && publicKeyAuth != SSH_AUTH_SUCCESS && (passwordAuth = ssh_userauth_password(m_session, username.c_str(), password.c_str())) == SSH_AUTH_ERROR)
{
CLog::Log(LOGERROR, "SFTPSession: Failed to authenticate via password '%s'", ssh_get_error(m_session));
return false;
@@ -430,7 +460,7 @@
if (m_session && m_sftp_handle)
{
- u64 position = 0;
+ uint64_t position = 0;
if (iWhence == SEEK_SET)
position = iFilePosition;
else if (iWhence == SEEK_CUR)
@@ -452,7 +482,7 @@
{
if (m_session && m_sftp_handle)
{
- int rc = m_session->Read(m_sftp_handle, lpBuf, uiBufSize);
+ int rc = m_session->Read(m_sftp_handle, lpBuf, (size_t)uiBufSize);
if (rc > 0)
return rc;
--- xbmc/FileSystem/FileSFTP.h 2010-03-27 15:33:53.906895676 +0100
+++ xbmc/FileSystem/FileSFTP.h 2010-03-27 15:33:19.143567868 +0100
@@ -34,30 +34,42 @@
#include <map>
#include <boost/shared_ptr.hpp>
+#if LIBSSH_VERSION_INT < SSH_VERSION_INT(0,3,2)
+#define ssh_session SSH_SESSION
+#endif
+
+#if LIBSSH_VERSION_INT < SSH_VERSION_INT(0,4,0)
+#define sftp_file SFTP_FILE*
+#define sftp_session SFTP_SESSION*
+#define sftp_attributes SFTP_ATTRIBUTES*
+#define sftp_dir SFTP_DIR*
+#define ssh_session ssh_session*
+#endif
+
class CSFTPSession
{
public:
CSFTPSession(const CStdString &host, const CStdString &username, const CStdString &password);
virtual ~CSFTPSession();
- SFTP_FILE *CreateFileHande(const CStdString &file);
- void CloseFileHandle(SFTP_FILE *handle);
+ sftp_file CreateFileHande(const CStdString &file);
+ void CloseFileHandle(sftp_file handle);
bool GetDirectory(const CStdString &base, const CStdString &folder, CFileItemList &items);
bool Exists(const char *path);
int Stat(const char *path, struct __stat64* buffer);
- void Seek(SFTP_FILE *handle, u64 position);
- int Read(SFTP_FILE *handle, void *buffer, int64_t length);
- int64_t GetPosition(SFTP_FILE *handle);
+ void Seek(sftp_file handle, uint64_t position);
+ int Read(sftp_file handle, void *buffer, size_t length);
+ int64_t GetPosition(sftp_file handle);
bool IsIdle();
private:
- bool VerifyKnownHost(ssh_session *session);
+ bool VerifyKnownHost(ssh_session session);
bool Connect(const CStdString &host, const CStdString &username, const CStdString &password);
void Disconnect();
CCriticalSection m_critSect;
bool m_connected;
- ssh_session *m_session;
- SFTP_SESSION *m_sftp_session;
+ ssh_session m_session;
+ sftp_session m_sftp_session;
int m_LastActive;
};
@@ -94,7 +106,7 @@
private:
CStdString m_file;
CSFTPSessionPtr m_session;
- SFTP_FILE *m_sftp_handle;
+ sftp_file m_sftp_handle;
};
}
#endif