Changeset View
Changeset View
Standalone View
Standalone View
sftp/kio_sftp.cpp
Show All 20 Lines | |||||
21 | 21 | | |||
22 | #include "kio_sftp.h" | 22 | #include "kio_sftp.h" | ||
23 | 23 | | |||
24 | #include <config-runtime.h> | 24 | #include <config-runtime.h> | ||
25 | #include "kio_smtp_debug.h" | 25 | #include "kio_smtp_debug.h" | ||
26 | #include <cerrno> | 26 | #include <cerrno> | ||
27 | #include <cstring> | 27 | #include <cstring> | ||
28 | #include <unistd.h> | 28 | #include <unistd.h> | ||
29 | #include <utime.h> | ||||
29 | 30 | | |||
30 | #include <QtCore/QCoreApplication> | 31 | #include <QtCore/QCoreApplication> | ||
31 | #include <QtCore/QBuffer> | | |||
32 | #include <QtCore/QByteArray> | | |||
33 | #include <QtCore/QDir> | 32 | #include <QtCore/QDir> | ||
34 | #include <QtCore/QFile> | 33 | #include <QtCore/QFile> | ||
35 | #include <QtCore/QObject> | | |||
36 | #include <QtCore/QString> | | |||
37 | #include <QtCore/QVarLengthArray> | 34 | #include <QtCore/QVarLengthArray> | ||
35 | #include <QtCore/QMimeType> | ||||
36 | #include <QtCore/QMimeDatabase> | ||||
38 | 37 | | |||
39 | #include <kuser.h> | 38 | #include <kuser.h> | ||
40 | #include <kmessagebox.h> | 39 | #include <kmessagebox.h> | ||
41 | #include <kcomponentdata.h> | | |||
42 | #include <kglobal.h> | | |||
43 | 40 | | |||
44 | #include <KMimeType> | 41 | #include <klocalizedstring.h> | ||
45 | #include <klocale.h> | | |||
46 | #include <kurl.h> | | |||
47 | #include <kde_file.h> | | |||
48 | #include <kconfiggroup.h> | 42 | #include <kconfiggroup.h> | ||
49 | #include <kio/ioslave_defaults.h> | 43 | #include <kio/ioslave_defaults.h> | ||
50 | 44 | | |||
51 | 45 | | |||
52 | #define KIO_SFTP_SPECIAL_TIMEOUT 30 | 46 | #define KIO_SFTP_SPECIAL_TIMEOUT 30 | ||
53 | #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0) | 47 | #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0) | ||
54 | 48 | | |||
55 | // How big should each data packet be? Definitely not bigger than 64kb or | 49 | // How big should each data packet be? Definitely not bigger than 64kb or | ||
56 | // you will overflow the 2 byte size variable in a sftp packet. | 50 | // you will overflow the 2 byte size variable in a sftp packet. | ||
57 | #define MAX_XFER_BUF_SIZE (60 * 1024) | 51 | #define MAX_XFER_BUF_SIZE (60 * 1024) | ||
58 | #define KIO_SFTP_DB 7120 | 52 | #define KIO_SFTP_DB 7120 | ||
59 | // Maximum amount of data which can be sent from the KIOSlave in one chunk | 53 | // Maximum amount of data which can be sent from the KIOSlave in one chunk | ||
60 | // see TransferJob::slotDataReq (max_size variable) for the value | 54 | // see TransferJob::slotDataReq (max_size variable) for the value | ||
61 | #define MAX_TRANSFER_SIZE (14 * 1024 * 1024) | 55 | #define MAX_TRANSFER_SIZE (14 * 1024 * 1024) | ||
62 | 56 | | |||
63 | using namespace KIO; | 57 | using namespace KIO; | ||
64 | extern "C" | 58 | extern "C" | ||
65 | { | 59 | { | ||
66 | int Q_DECL_EXPORT kdemain( int argc, char **argv ) | 60 | int Q_DECL_EXPORT kdemain( int argc, char **argv ) | ||
67 | { | 61 | { | ||
68 | QCoreApplication app(argc, argv); | 62 | QCoreApplication app(argc, argv); | ||
69 | KComponentData componentData( "kio_sftp" ); | 63 | app.setApplicationName("kio_sftp"); | ||
70 | (void) KLocale::global(); | | |||
71 | 64 | | |||
72 | qCDebug(KIO_SFTP_LOG) << "*** Starting kio_sftp "; | 65 | qCDebug(KIO_SFTP_LOG) << "*** Starting kio_sftp "; | ||
73 | 66 | | |||
74 | if (argc != 4) { | 67 | if (argc != 4) { | ||
75 | qCDebug(KIO_SFTP_LOG) << "Usage: kio_sftp protocol domain-socket1 domain-socket2"; | 68 | qCDebug(KIO_SFTP_LOG) << "Usage: kio_sftp protocol domain-socket1 domain-socket2"; | ||
76 | exit(-1); | 69 | exit(-1); | ||
77 | } | 70 | } | ||
78 | 71 | | |||
79 | sftpProtocol slave(argv[2], argv[3]); | 72 | sftpProtocol slave(argv[2], argv[3]); | ||
80 | slave.dispatchLoop(); | 73 | slave.dispatchLoop(); | ||
81 | 74 | | |||
82 | qCDebug(KIO_SFTP_LOG) << "*** kio_sftp Done"; | 75 | qCDebug(KIO_SFTP_LOG) << "*** kio_sftp Done"; | ||
83 | return 0; | 76 | return 0; | ||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Line(s) | 108 | while (len > 0) { | |||
134 | } | 127 | } | ||
135 | } | 128 | } | ||
136 | return 0; | 129 | return 0; | ||
137 | } | 130 | } | ||
138 | 131 | | |||
139 | static int seekPos(int fd, KIO::fileoffset_t pos, int mode) | 132 | static int seekPos(int fd, KIO::fileoffset_t pos, int mode) | ||
140 | { | 133 | { | ||
141 | KIO::fileoffset_t offset = -1; | 134 | KIO::fileoffset_t offset = -1; | ||
142 | while ((offset=KDE_lseek(fd, pos, mode)) == EAGAIN); | 135 | while ((offset = QT_LSEEK(fd, pos, mode)) == EAGAIN); | ||
143 | return offset; | 136 | return offset; | ||
144 | } | 137 | } | ||
145 | 138 | | |||
146 | static bool wasUsernameChanged(const QString& username, const KIO::AuthInfo& info) | 139 | static bool wasUsernameChanged(const QString& username, const KIO::AuthInfo& info) | ||
147 | { | 140 | { | ||
148 | QString loginName (username); | 141 | QString loginName (username); | ||
149 | // If username is empty, assume the current logged in username. Why ? | 142 | // If username is empty, assume the current logged in username. Why ? | ||
150 | // Because libssh's SSH_OPTIONS_USER will default to that when it is not | 143 | // Because libssh's SSH_OPTIONS_USER will default to that when it is not | ||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | 182 | int sftpProtocol::auth_callback(const char *prompt, char *buf, size_t len, | |||
203 | 196 | | |||
204 | mPublicKeyAuthInfo->url.setScheme(QLatin1String("sftp")); | 197 | mPublicKeyAuthInfo->url.setScheme(QLatin1String("sftp")); | ||
205 | mPublicKeyAuthInfo->url.setHost(mHost); | 198 | mPublicKeyAuthInfo->url.setHost(mHost); | ||
206 | if (mPort > 0 && mPort != DEFAULT_SFTP_PORT) { | 199 | if (mPort > 0 && mPort != DEFAULT_SFTP_PORT) { | ||
207 | mPublicKeyAuthInfo->url.setPort(mPort); | 200 | mPublicKeyAuthInfo->url.setPort(mPort); | ||
208 | } | 201 | } | ||
209 | mPublicKeyAuthInfo->url.setUserName(mUsername); | 202 | mPublicKeyAuthInfo->url.setUserName(mUsername); | ||
210 | 203 | | |||
211 | KUrl u (mPublicKeyAuthInfo->url); | 204 | QUrl u (mPublicKeyAuthInfo->url); | ||
212 | u.setPath(QString()); | 205 | u.setPath(QString()); | ||
213 | mPublicKeyAuthInfo->comment = u.url(); | 206 | mPublicKeyAuthInfo->comment = u.url(); | ||
214 | mPublicKeyAuthInfo->readOnly = true; | 207 | mPublicKeyAuthInfo->readOnly = true; | ||
215 | mPublicKeyAuthInfo->prompt = QString::fromUtf8(prompt); | 208 | mPublicKeyAuthInfo->prompt = QString::fromUtf8(prompt); | ||
216 | mPublicKeyAuthInfo->keepPassword = false; // don't save passwords for public key, | 209 | mPublicKeyAuthInfo->keepPassword = false; // don't save passwords for public key, | ||
217 | // that's the task of ssh-agent. | 210 | // that's the task of ssh-agent. | ||
218 | mPublicKeyAuthInfo->setExtraField(QLatin1String("hide-username-line"), true); | 211 | mPublicKeyAuthInfo->setExtraField(QLatin1String("hide-username-line"), true); | ||
219 | mPublicKeyAuthInfo->setModified(false); | 212 | mPublicKeyAuthInfo->setModified(false); | ||
220 | 213 | | |||
221 | qCDebug(KIO_SFTP_LOG) << "Entering authentication callback, prompt=" << mPublicKeyAuthInfo->prompt; | 214 | qCDebug(KIO_SFTP_LOG) << "Entering authentication callback, prompt=" << mPublicKeyAuthInfo->prompt; | ||
222 | 215 | | |||
223 | if (!openPasswordDialog(*mPublicKeyAuthInfo, errMsg)) { | 216 | if (openPasswordDialogV2(*mPublicKeyAuthInfo, errMsg) != 0) { | ||
224 | qCDebug(KIO_SFTP_LOG) << "User canceled public key passpharse dialog"; | 217 | qCDebug(KIO_SFTP_LOG) << "User canceled public key passpharse dialog"; | ||
225 | return -1; | 218 | return -1; | ||
226 | } | 219 | } | ||
227 | 220 | | |||
228 | strncpy(buf, mPublicKeyAuthInfo->password.toUtf8().constData(), len - 1); | 221 | strncpy(buf, mPublicKeyAuthInfo->password.toUtf8().constData(), len - 1); | ||
229 | 222 | | |||
230 | mPublicKeyAuthInfo->password.fill('x'); | 223 | mPublicKeyAuthInfo->password.fill('x'); | ||
231 | mPublicKeyAuthInfo->password.clear(); | 224 | mPublicKeyAuthInfo->password.clear(); | ||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Line(s) | 285 | if (!instruction.isEmpty()) { | |||
293 | newPrompt = instruction + "<br /><br />"; | 286 | newPrompt = instruction + "<br /><br />"; | ||
294 | } | 287 | } | ||
295 | newPrompt.append(prompt); | 288 | newPrompt.append(prompt); | ||
296 | infoKbdInt.prompt = newPrompt; | 289 | infoKbdInt.prompt = newPrompt; | ||
297 | 290 | | |||
298 | infoKbdInt.readOnly = false; | 291 | infoKbdInt.readOnly = false; | ||
299 | infoKbdInt.keepPassword = false; | 292 | infoKbdInt.keepPassword = false; | ||
300 | 293 | | |||
301 | if (openPasswordDialog(infoKbdInt, i18n("Use the username input field to answer this question."))) { | 294 | if (openPasswordDialogV2(infoKbdInt, i18n("Use the username input field to answer this question.")) == 0) { | ||
302 | qCDebug(KIO_SFTP_LOG) << "Got the answer from the password dialog"; | 295 | qCDebug(KIO_SFTP_LOG) << "Got the answer from the password dialog"; | ||
303 | answer = info.username.toUtf8().constData(); | 296 | answer = info.username.toUtf8().constData(); | ||
304 | } | 297 | } | ||
305 | 298 | | |||
306 | if (ssh_userauth_kbdint_setanswer(mSession, i, answer) < 0) { | 299 | if (ssh_userauth_kbdint_setanswer(mSession, i, answer) < 0) { | ||
307 | qCDebug(KIO_SFTP_LOG) << "An error occurred setting the answer: " | 300 | qCDebug(KIO_SFTP_LOG) << "An error occurred setting the answer: " | ||
308 | << ssh_get_error(mSession); | 301 | << ssh_get_error(mSession); | ||
309 | return SSH_AUTH_ERROR; | 302 | return SSH_AUTH_ERROR; | ||
310 | } | 303 | } | ||
311 | break; | 304 | break; | ||
312 | } else { | 305 | } else { | ||
313 | if (prompt.startsWith(QLatin1String("password:"), Qt::CaseInsensitive)) { | 306 | if (prompt.startsWith(QLatin1String("password:"), Qt::CaseInsensitive)) { | ||
314 | info.prompt = i18n("Please enter your password."); | 307 | info.prompt = i18n("Please enter your password."); | ||
315 | } else { | 308 | } else { | ||
316 | info.prompt = prompt; | 309 | info.prompt = prompt; | ||
317 | } | 310 | } | ||
318 | info.comment = info.url.url(); | 311 | info.comment = info.url.url(); | ||
319 | info.commentLabel = i18n("Site:"); | 312 | info.commentLabel = i18n("Site:"); | ||
320 | info.setExtraField(QLatin1String("hide-username-line"), true); | 313 | info.setExtraField(QLatin1String("hide-username-line"), true); | ||
321 | 314 | | |||
322 | if (openPasswordDialog(info)) { | 315 | if (openPasswordDialogV2(info) == 0) { | ||
323 | qCDebug(KIO_SFTP_LOG) << "Got the answer from the password dialog"; | 316 | qCDebug(KIO_SFTP_LOG) << "Got the answer from the password dialog"; | ||
324 | answer = info.password.toUtf8().constData(); | 317 | answer = info.password.toUtf8().constData(); | ||
325 | } | 318 | } | ||
326 | 319 | | |||
327 | if (ssh_userauth_kbdint_setanswer(mSession, i, answer) < 0) { | 320 | if (ssh_userauth_kbdint_setanswer(mSession, i, answer) < 0) { | ||
328 | qCDebug(KIO_SFTP_LOG) << "An error occurred setting the answer: " | 321 | qCDebug(KIO_SFTP_LOG) << "An error occurred setting the answer: " | ||
329 | << ssh_get_error(mSession); | 322 | << ssh_get_error(mSession); | ||
330 | return SSH_AUTH_ERROR; | 323 | return SSH_AUTH_ERROR; | ||
331 | } | 324 | } | ||
332 | } | 325 | } | ||
333 | } | 326 | } | ||
334 | err = ssh_userauth_kbdint(mSession, NULL, NULL); | 327 | err = ssh_userauth_kbdint(mSession, NULL, NULL); | ||
335 | } | 328 | } | ||
336 | 329 | | |||
337 | return err; | 330 | return err; | ||
338 | } | 331 | } | ||
339 | 332 | | |||
340 | void sftpProtocol::reportError(const KUrl &url, const int err) { | 333 | void sftpProtocol::reportError(const QUrl &url, const int err) { | ||
341 | qCDebug(KIO_SFTP_LOG) << "url = " << url << " - err=" << err; | 334 | qCDebug(KIO_SFTP_LOG) << "url = " << url << " - err=" << err; | ||
342 | 335 | | |||
343 | const int kioError = toKIOError(err); | 336 | const int kioError = toKIOError(err); | ||
344 | if (kioError) | 337 | if (kioError) | ||
345 | error(kioError, url.toDisplayString()); | 338 | error(kioError, url.toDisplayString()); | ||
346 | } | 339 | } | ||
347 | 340 | | |||
348 | bool sftpProtocol::createUDSEntry(const QString &filename, const QByteArray &path, | 341 | bool sftpProtocol::createUDSEntry(const QString &filename, const QByteArray &path, | ||
▲ Show 20 Lines • Show All 486 Lines • ▼ Show 20 Line(s) | 825 | if (!isFirstLoginAttempt || info.password.isEmpty()) { | |||
835 | 828 | | |||
836 | QString username (info.username); | 829 | QString username (info.username); | ||
837 | const QString errMsg(isFirstLoginAttempt ? QString() : i18n("Incorrect username or password")); | 830 | const QString errMsg(isFirstLoginAttempt ? QString() : i18n("Incorrect username or password")); | ||
838 | 831 | | |||
839 | qCDebug(KIO_SFTP_LOG) << "Username:" << username << "first attempt?" | 832 | qCDebug(KIO_SFTP_LOG) << "Username:" << username << "first attempt?" | ||
840 | << isFirstLoginAttempt << "error:" << errMsg; | 833 | << isFirstLoginAttempt << "error:" << errMsg; | ||
841 | 834 | | |||
842 | // Handle user canceled or dialog failed to open... | 835 | // Handle user canceled or dialog failed to open... | ||
843 | if (!openPasswordDialog(info, errMsg)) { | 836 | | ||
dfaure: This should store the return value from openPasswordDialogV2 and use it in the error call 3… | |||||
837 | int errCode = openPasswordDialogV2(info, errMsg); | ||||
838 | if (errCode != 0) { | ||||
844 | qCDebug(KIO_SFTP_LOG) << "User canceled password/retry dialog"; | 839 | qCDebug(KIO_SFTP_LOG) << "User canceled password/retry dialog"; | ||
845 | closeConnection(); | 840 | closeConnection(); | ||
846 | error(KIO::ERR_USER_CANCELED, QString()); | 841 | error(errCode, QString()); | ||
847 | return; | 842 | return; | ||
848 | } | 843 | } | ||
849 | 844 | | |||
850 | // If the user name changes, we have to restablish connection again | 845 | // If the user name changes, we have to restablish connection again | ||
851 | // since the user name must always be set before calling ssh_connect. | 846 | // since the user name must always be set before calling ssh_connect. | ||
852 | if (wasUsernameChanged(username, info)) { | 847 | if (wasUsernameChanged(username, info)) { | ||
853 | qCDebug(KIO_SFTP_LOG) << "Username changed to" << info.username; | 848 | qCDebug(KIO_SFTP_LOG) << "Username changed to" << info.username; | ||
854 | if (!info.url.userName().isEmpty()) { | 849 | if (!info.url.userName().isEmpty()) { | ||
▲ Show 20 Lines • Show All 189 Lines • ▼ Show 20 Line(s) | 1035 | if (mode & QIODevice::ReadOnly) { | |||
1044 | 1039 | | |||
1045 | bytesRead = sftp_read(mOpenFile, buffer.data(), bytesRequested); | 1040 | bytesRead = sftp_read(mOpenFile, buffer.data(), bytesRequested); | ||
1046 | if (bytesRead < 0) { | 1041 | if (bytesRead < 0) { | ||
1047 | error(KIO::ERR_COULD_NOT_READ, mOpenUrl.toDisplayString()); | 1042 | error(KIO::ERR_COULD_NOT_READ, mOpenUrl.toDisplayString()); | ||
1048 | close(); | 1043 | close(); | ||
1049 | return; | 1044 | return; | ||
1050 | } else { | 1045 | } else { | ||
1051 | QByteArray fileData = QByteArray::fromRawData(buffer.data(), bytesRead); | 1046 | QByteArray fileData = QByteArray::fromRawData(buffer.data(), bytesRead); | ||
1052 | KMimeType::Ptr p_mimeType = KMimeType::findByNameAndContent(mOpenUrl.fileName(), fileData); | 1047 | QMimeDatabase db; | ||
1053 | emit mimeType(p_mimeType->name()); | 1048 | QMimeType mime = db.mimeTypeForFileNameAndData(mOpenUrl.fileName(), fileData); | ||
1049 | emit mimeType(mime.name()); | ||||
1054 | 1050 | | |||
1055 | // Go back to the beginning of the file. | 1051 | // Go back to the beginning of the file. | ||
1056 | sftp_rewind(mOpenFile); | 1052 | sftp_rewind(mOpenFile); | ||
1057 | } | 1053 | } | ||
1058 | } | 1054 | } | ||
1059 | 1055 | | |||
1060 | mOpenUrl = url; | 1056 | mOpenUrl = url; | ||
1061 | 1057 | | |||
▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Line(s) | 1121 | void sftpProtocol::get(const QUrl& url) { | |||
1135 | 1131 | | |||
1136 | // The call to sftpGet should only return server side errors since the file | 1132 | // The call to sftpGet should only return server side errors since the file | ||
1137 | // descriptor parameter is set to -1. | 1133 | // descriptor parameter is set to -1. | ||
1138 | if (cs == sftpProtocol::ServerError && errorCode) { | 1134 | if (cs == sftpProtocol::ServerError && errorCode) { | ||
1139 | error(errorCode, url.toDisplayString()); | 1135 | error(errorCode, url.toDisplayString()); | ||
1140 | } | 1136 | } | ||
1141 | } | 1137 | } | ||
1142 | 1138 | | |||
1143 | sftpProtocol::StatusCode sftpProtocol::sftpGet(const KUrl& url, int& errorCode, KIO::fileoffset_t offset, int fd) { | 1139 | sftpProtocol::StatusCode sftpProtocol::sftpGet(const QUrl& url, int& errorCode, KIO::fileoffset_t offset, int fd) { | ||
1144 | 1140 | | |||
1145 | qCDebug(KIO_SFTP_LOG) << url; | 1141 | qCDebug(KIO_SFTP_LOG) << url; | ||
1146 | 1142 | | |||
1147 | if (!sftpLogin()) { | 1143 | if (!sftpLogin()) { | ||
1148 | return sftpProtocol::ServerError; | 1144 | return sftpProtocol::ServerError; | ||
1149 | } | 1145 | } | ||
1150 | 1146 | | |||
1151 | QByteArray path = url.path().toUtf8(); | 1147 | QByteArray path = url.path().toUtf8(); | ||
Show All 33 Lines | |||||
1185 | 1181 | | |||
1186 | char mimeTypeBuf[1024]; | 1182 | char mimeTypeBuf[1024]; | ||
1187 | ssize_t bytesread = sftp_read(file, mimeTypeBuf, sizeof(mimeTypeBuf)); | 1183 | ssize_t bytesread = sftp_read(file, mimeTypeBuf, sizeof(mimeTypeBuf)); | ||
1188 | 1184 | | |||
1189 | if (bytesread < 0) { | 1185 | if (bytesread < 0) { | ||
1190 | errorCode = KIO::ERR_COULD_NOT_READ; | 1186 | errorCode = KIO::ERR_COULD_NOT_READ; | ||
1191 | return sftpProtocol::ServerError; | 1187 | return sftpProtocol::ServerError; | ||
1192 | } else { | 1188 | } else { | ||
1193 | int accuracy = 0; | 1189 | QMimeDatabase db; | ||
1194 | KMimeType::Ptr mime = KMimeType::findByNameAndContent(url.fileName(), QByteArray(mimeTypeBuf, bytesread), 0, &accuracy); | 1190 | QMimeType mime = db.mimeTypeForFileNameAndData(url.fileName(), QByteArray(mimeTypeBuf, bytesread)); | ||
1195 | if (!mime->isDefault() && accuracy == 100) { | 1191 | if (!mime.isDefault()) { | ||
1196 | emit mimeType(mime->name()); | 1192 | emit mimeType(mime.name()); | ||
1197 | } else { | 1193 | } else { | ||
1198 | accuracy = 0; | 1194 | mime = db.mimeTypeForUrl(url); | ||
1199 | mime = KMimeType::findByUrl(url, 0, false, true, &accuracy); | 1195 | emit mimeType(mime.name()); | ||
1200 | emit mimeType(mime->name()); | | |||
1201 | } | 1196 | } | ||
1202 | sftp_rewind(file); | 1197 | sftp_rewind(file); | ||
1203 | } | 1198 | } | ||
1204 | 1199 | | |||
1205 | // Set the total size | 1200 | // Set the total size | ||
1206 | totalSize(sb->size); | 1201 | totalSize(sb->size); | ||
1207 | 1202 | | |||
1208 | // If offset is not specified, check the "resume" meta-data. | 1203 | // If offset is not specified, check the "resume" meta-data. | ||
▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Line(s) | 1266 | void sftpProtocol::put(const QUrl& url, int permissions, KIO::JobFlags flags) { | |||
1285 | 1280 | | |||
1286 | // The call to sftpPut should only return server side errors since the file | 1281 | // The call to sftpPut should only return server side errors since the file | ||
1287 | // descriptor parameter is set to -1. | 1282 | // descriptor parameter is set to -1. | ||
1288 | if (cs == sftpProtocol::ServerError && errorCode) { | 1283 | if (cs == sftpProtocol::ServerError && errorCode) { | ||
1289 | error(errorCode, url.toDisplayString()); | 1284 | error(errorCode, url.toDisplayString()); | ||
1290 | } | 1285 | } | ||
1291 | } | 1286 | } | ||
1292 | 1287 | | |||
1293 | sftpProtocol::StatusCode sftpProtocol::sftpPut(const KUrl& url, int permissions, JobFlags flags, int& errorCode, int fd) { | 1288 | sftpProtocol::StatusCode sftpProtocol::sftpPut(const QUrl& url, int permissions, JobFlags flags, int& errorCode, int fd) { | ||
1294 | qCDebug(KIO_SFTP_LOG) << url << ", permissions =" << permissions | 1289 | qCDebug(KIO_SFTP_LOG) << url << ", permissions =" << permissions | ||
1295 | << ", overwrite =" << (flags & KIO::Overwrite) | 1290 | << ", overwrite =" << (flags & KIO::Overwrite) | ||
1296 | << ", resume =" << (flags & KIO::Resume); | 1291 | << ", resume =" << (flags & KIO::Resume); | ||
1297 | 1292 | | |||
1298 | if (!sftpLogin()) { | 1293 | if (!sftpLogin()) { | ||
1299 | return sftpProtocol::ServerError; | 1294 | return sftpProtocol::ServerError; | ||
1300 | } | 1295 | } | ||
1301 | 1296 | | |||
▲ Show 20 Lines • Show All 275 Lines • ▼ Show 20 Line(s) | 1568 | if (cs == sftpProtocol::Success || errorCode < 0) { | |||
1577 | return; | 1572 | return; | ||
1578 | } | 1573 | } | ||
1579 | 1574 | | |||
1580 | if (errorCode) { | 1575 | if (errorCode) { | ||
1581 | error(errorCode, sCopyFile); | 1576 | error(errorCode, sCopyFile); | ||
1582 | } | 1577 | } | ||
1583 | } | 1578 | } | ||
1584 | 1579 | | |||
1585 | sftpProtocol::StatusCode sftpProtocol::sftpCopyGet(const KUrl& url, const QString& sCopyFile, int permissions, KIO::JobFlags flags, int& errorCode) | 1580 | sftpProtocol::StatusCode sftpProtocol::sftpCopyGet(const QUrl& url, const QString& sCopyFile, int permissions, KIO::JobFlags flags, int& errorCode) | ||
1586 | { | 1581 | { | ||
1587 | qCDebug(KIO_SFTP_LOG) << url << "->" << sCopyFile << ", permissions=" << permissions; | 1582 | qCDebug(KIO_SFTP_LOG) << url << "->" << sCopyFile << ", permissions=" << permissions; | ||
1588 | 1583 | | |||
1589 | // check if destination is ok ... | 1584 | // check if destination is ok ... | ||
1590 | KDE_struct_stat buff; | 1585 | QT_STATBUF buff; | ||
1591 | const bool bDestExists = (KDE::stat(sCopyFile, &buff) != -1); | 1586 | const bool bDestExists = (QT_STAT(QFile::encodeName(sCopyFile), &buff) != -1); | ||
1592 | 1587 | | |||
1593 | if(bDestExists) { | 1588 | if(bDestExists) { | ||
1594 | if(S_ISDIR(buff.st_mode)) { | 1589 | if(S_ISDIR(buff.st_mode)) { | ||
1595 | errorCode = ERR_IS_DIRECTORY; | 1590 | errorCode = ERR_IS_DIRECTORY; | ||
1596 | return sftpProtocol::ClientError; | 1591 | return sftpProtocol::ClientError; | ||
1597 | } | 1592 | } | ||
1598 | 1593 | | |||
1599 | if(!(flags & KIO::Overwrite)) { | 1594 | if(!(flags & KIO::Overwrite)) { | ||
1600 | errorCode = ERR_FILE_ALREADY_EXIST; | 1595 | errorCode = ERR_FILE_ALREADY_EXIST; | ||
1601 | return sftpProtocol::ClientError; | 1596 | return sftpProtocol::ClientError; | ||
1602 | } | 1597 | } | ||
1603 | } | 1598 | } | ||
1604 | 1599 | | |||
1605 | bool bResume = false; | 1600 | bool bResume = false; | ||
1606 | const QString sPart = sCopyFile + QLatin1String(".part"); // do we have a ".part" file? | 1601 | const QString sPart = sCopyFile + QLatin1String(".part"); // do we have a ".part" file? | ||
1607 | const bool bPartExists = (KDE::stat(sPart, &buff) != -1); | 1602 | const bool bPartExists = (QT_STAT(QFile::encodeName(sPart), &buff) != -1); | ||
1608 | const bool bMarkPartial = config()->readEntry("MarkPartial", true); | 1603 | const bool bMarkPartial = config()->readEntry("MarkPartial", true); | ||
1609 | const QString dest = (bMarkPartial ? sPart : sCopyFile); | 1604 | const QString dest = (bMarkPartial ? sPart : sCopyFile); | ||
1610 | 1605 | | |||
1611 | if (bMarkPartial && bPartExists && buff.st_size > 0) { | 1606 | if (bMarkPartial && bPartExists && buff.st_size > 0) { | ||
1612 | if (S_ISDIR(buff.st_mode)) { | 1607 | if (S_ISDIR(buff.st_mode)) { | ||
1613 | errorCode = ERR_DIR_ALREADY_EXIST; | 1608 | errorCode = ERR_DIR_ALREADY_EXIST; | ||
1614 | return sftpProtocol::ClientError; // client side error | 1609 | return sftpProtocol::ClientError; // client side error | ||
1615 | } | 1610 | } | ||
Show All 10 Lines | 1620 | if (permissions != -1) | |||
1626 | initialMode = permissions | S_IWUSR; | 1621 | initialMode = permissions | S_IWUSR; | ||
1627 | else | 1622 | else | ||
1628 | initialMode = 0666; | 1623 | initialMode = 0666; | ||
1629 | 1624 | | |||
1630 | // open the output file ... | 1625 | // open the output file ... | ||
1631 | int fd = -1; | 1626 | int fd = -1; | ||
1632 | KIO::fileoffset_t offset = 0; | 1627 | KIO::fileoffset_t offset = 0; | ||
1633 | if (bResume) { | 1628 | if (bResume) { | ||
1634 | fd = KDE::open( sPart, O_RDWR ); // append if resuming | 1629 | fd = QT_OPEN( QFile::encodeName(sPart), O_RDWR ); // append if resuming | ||
1635 | offset = seekPos(fd, 0, SEEK_END); | 1630 | offset = seekPos(fd, 0, SEEK_END); | ||
1636 | if(offset < 0) { | 1631 | if(offset < 0) { | ||
1637 | errorCode = ERR_CANNOT_RESUME; | 1632 | errorCode = ERR_CANNOT_RESUME; | ||
1638 | ::close(fd); | 1633 | ::close(fd); | ||
1639 | return sftpProtocol::ClientError; // client side error | 1634 | return sftpProtocol::ClientError; // client side error | ||
1640 | } | 1635 | } | ||
1641 | qCDebug(KIO_SFTP_LOG) << "resuming at" << offset; | 1636 | qCDebug(KIO_SFTP_LOG) << "resuming at" << offset; | ||
1642 | } | 1637 | } | ||
1643 | else { | 1638 | else { | ||
1644 | fd = KDE::open(dest, O_CREAT | O_TRUNC | O_WRONLY, initialMode); | 1639 | fd = QT_OPEN(QFile::encodeName(dest), O_CREAT | O_TRUNC | O_WRONLY, initialMode); | ||
1645 | } | 1640 | } | ||
1646 | 1641 | | |||
1647 | if (fd == -1) { | 1642 | if (fd == -1) { | ||
1648 | qCDebug(KIO_SFTP_LOG) << "could not write to" << sCopyFile; | 1643 | qCDebug(KIO_SFTP_LOG) << "could not write to" << sCopyFile; | ||
1649 | errorCode = (errno == EACCES) ? ERR_WRITE_ACCESS_DENIED : ERR_CANNOT_OPEN_FOR_WRITING; | 1644 | errorCode = (errno == EACCES) ? ERR_WRITE_ACCESS_DENIED : ERR_CANNOT_OPEN_FOR_WRITING; | ||
1650 | return sftpProtocol::ClientError; | 1645 | return sftpProtocol::ClientError; | ||
1651 | } | 1646 | } | ||
1652 | 1647 | | |||
1653 | StatusCode result = sftpGet(url, errorCode, offset, fd); | 1648 | StatusCode result = sftpGet(url, errorCode, offset, fd); | ||
1654 | 1649 | | |||
1655 | if( ::close(fd) && result == sftpProtocol::Success ) { | 1650 | if( ::close(fd) && result == sftpProtocol::Success ) { | ||
1656 | errorCode = ERR_COULD_NOT_WRITE; | 1651 | errorCode = ERR_COULD_NOT_WRITE; | ||
1657 | result = sftpProtocol::ClientError; | 1652 | result = sftpProtocol::ClientError; | ||
1658 | } | 1653 | } | ||
1659 | 1654 | | |||
1660 | // handle renaming or deletion of a partial file ... | 1655 | // handle renaming or deletion of a partial file ... | ||
1661 | if (bMarkPartial) { | 1656 | if (bMarkPartial) { | ||
1662 | if (result == sftpProtocol::Success) { // rename ".part" on success | 1657 | if (result == sftpProtocol::Success) { // rename ".part" on success | ||
1663 | if ( KDE::rename( sPart, sCopyFile ) ) { | 1658 | if ( !QFile::rename( QFile::encodeName(sPart), sCopyFile ) ) { | ||
1664 | // If rename fails, try removing the destination first if it exists. | 1659 | // If rename fails, try removing the destination first if it exists. | ||
1665 | if (!bDestExists || !(QFile::remove(sCopyFile) && KDE::rename(sPart, sCopyFile) == 0)) { | 1660 | if (!bDestExists || !QFile::remove(sCopyFile) || !QFile::rename(sPart, sCopyFile)) { | ||
1666 | qCDebug(KIO_SFTP_LOG) << "cannot rename " << sPart << " to " << sCopyFile; | 1661 | qCDebug(KIO_SFTP_LOG) << "cannot rename " << sPart << " to " << sCopyFile; | ||
1667 | errorCode = ERR_CANNOT_RENAME_PARTIAL; | 1662 | errorCode = ERR_CANNOT_RENAME_PARTIAL; | ||
1668 | result = sftpProtocol::ClientError; | 1663 | result = sftpProtocol::ClientError; | ||
1669 | } | 1664 | } | ||
1670 | } | 1665 | } | ||
1671 | } | 1666 | } | ||
1672 | else if (KDE::stat( sPart, &buff ) == 0) { // should a very small ".part" be deleted? | 1667 | else if (QT_STAT( QFile::encodeName(sPart), &buff ) == 0) { // should a very small ".part" be deleted? | ||
1673 | const int size = config()->readEntry("MinimumKeepSize", DEFAULT_MINIMUM_KEEP_SIZE); | 1668 | const int size = config()->readEntry("MinimumKeepSize", DEFAULT_MINIMUM_KEEP_SIZE); | ||
1674 | if (buff.st_size < size) | 1669 | if (buff.st_size < size) | ||
1675 | QFile::remove(sPart); | 1670 | QFile::remove(sPart); | ||
1676 | } | 1671 | } | ||
1677 | } | 1672 | } | ||
1678 | 1673 | | |||
1679 | const QString mtimeStr = metaData("modified"); | 1674 | const QString mtimeStr = metaData("modified"); | ||
1680 | if (!mtimeStr.isEmpty()) { | 1675 | if (!mtimeStr.isEmpty()) { | ||
1681 | QDateTime dt = QDateTime::fromString(mtimeStr, Qt::ISODate); | 1676 | QDateTime dt = QDateTime::fromString(mtimeStr, Qt::ISODate); | ||
1682 | if (dt.isValid()) { | 1677 | if (dt.isValid()) { | ||
1683 | struct utimbuf utbuf; | 1678 | struct utimbuf utbuf; | ||
1684 | utbuf.actime = buff.st_atime; // access time, unchanged | 1679 | utbuf.actime = buff.st_atime; // access time, unchanged | ||
1685 | utbuf.modtime = dt.toTime_t(); // modification time | 1680 | utbuf.modtime = dt.toTime_t(); // modification time | ||
1686 | KDE::utime(sCopyFile, &utbuf); | 1681 | utime(QFile::encodeName(sCopyFile), &utbuf); | ||
1687 | } | 1682 | } | ||
1688 | } | 1683 | } | ||
1689 | 1684 | | |||
1690 | return result; | 1685 | return result; | ||
1691 | } | 1686 | } | ||
1692 | 1687 | | |||
1693 | sftpProtocol::StatusCode sftpProtocol::sftpCopyPut(const KUrl& url, const QString& sCopyFile, int permissions, JobFlags flags, int& errorCode) | 1688 | sftpProtocol::StatusCode sftpProtocol::sftpCopyPut(const QUrl& url, const QString& sCopyFile, int permissions, JobFlags flags, int& errorCode) | ||
1694 | { | 1689 | { | ||
1695 | qCDebug(KIO_SFTP_LOG) << sCopyFile << "->" << url << ", permissions=" << permissions << ", flags" << flags; | 1690 | qCDebug(KIO_SFTP_LOG) << sCopyFile << "->" << url << ", permissions=" << permissions << ", flags" << flags; | ||
1696 | 1691 | | |||
1697 | // check if source is ok ... | 1692 | // check if source is ok ... | ||
1698 | KDE_struct_stat buff; | 1693 | QT_STATBUF buff; | ||
1699 | bool bSrcExists = (KDE::stat(sCopyFile, &buff) != -1); | 1694 | bool bSrcExists = (QT_STAT(QFile::encodeName(sCopyFile), &buff) != -1); | ||
1700 | 1695 | | |||
1701 | if (bSrcExists) { | 1696 | if (bSrcExists) { | ||
1702 | if (S_ISDIR(buff.st_mode)) { | 1697 | if (S_ISDIR(buff.st_mode)) { | ||
1703 | errorCode = ERR_IS_DIRECTORY; | 1698 | errorCode = ERR_IS_DIRECTORY; | ||
1704 | return sftpProtocol::ClientError; | 1699 | return sftpProtocol::ClientError; | ||
1705 | } | 1700 | } | ||
1706 | } else { | 1701 | } else { | ||
1707 | errorCode = ERR_DOES_NOT_EXIST; | 1702 | errorCode = ERR_DOES_NOT_EXIST; | ||
1708 | return sftpProtocol::ClientError; | 1703 | return sftpProtocol::ClientError; | ||
1709 | } | 1704 | } | ||
1710 | 1705 | | |||
1711 | const int fd = KDE::open(sCopyFile, O_RDONLY); | 1706 | const int fd = QT_OPEN(QFile::encodeName(sCopyFile), O_RDONLY); | ||
1712 | if(fd == -1) | 1707 | if(fd == -1) | ||
1713 | { | 1708 | { | ||
1714 | errorCode = ERR_CANNOT_OPEN_FOR_READING; | 1709 | errorCode = ERR_CANNOT_OPEN_FOR_READING; | ||
1715 | return sftpProtocol::ClientError; | 1710 | return sftpProtocol::ClientError; | ||
1716 | } | 1711 | } | ||
1717 | 1712 | | |||
1718 | totalSize(buff.st_size); | 1713 | totalSize(buff.st_size); | ||
1719 | 1714 | | |||
Show All 20 Lines | 1729 | if (url.path().isEmpty() || QDir::isRelativePath(url.path()) || | |||
1740 | } else { | 1735 | } else { | ||
1741 | cPath = canonicalizePath(QLatin1String(".")); | 1736 | cPath = canonicalizePath(QLatin1String(".")); | ||
1742 | } | 1737 | } | ||
1743 | 1738 | | |||
1744 | if (cPath.isEmpty()) { | 1739 | if (cPath.isEmpty()) { | ||
1745 | error(KIO::ERR_MALFORMED_URL, url.toDisplayString()); | 1740 | error(KIO::ERR_MALFORMED_URL, url.toDisplayString()); | ||
1746 | return; | 1741 | return; | ||
1747 | } | 1742 | } | ||
1748 | KUrl redir(url); | 1743 | QUrl redir(url); | ||
1749 | redir.setPath(cPath); | 1744 | redir.setPath(cPath); | ||
1750 | redirection(redir); | 1745 | redirection(redir); | ||
1751 | 1746 | | |||
1752 | qCDebug(KIO_SFTP_LOG) << "redirecting to " << redir.url(); | 1747 | qCDebug(KIO_SFTP_LOG) << "redirecting to " << redir.url(); | ||
1753 | 1748 | | |||
1754 | finished(); | 1749 | finished(); | ||
1755 | return; | 1750 | return; | ||
1756 | } | 1751 | } | ||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Line(s) | 1791 | if (url.path().isEmpty() || QDir::isRelativePath(url.path()) || | |||
1802 | } else { | 1797 | } else { | ||
1803 | cPath = canonicalizePath(QStringLiteral(".")); | 1798 | cPath = canonicalizePath(QStringLiteral(".")); | ||
1804 | } | 1799 | } | ||
1805 | 1800 | | |||
1806 | if (cPath.isEmpty()) { | 1801 | if (cPath.isEmpty()) { | ||
1807 | error(KIO::ERR_MALFORMED_URL, url.toDisplayString()); | 1802 | error(KIO::ERR_MALFORMED_URL, url.toDisplayString()); | ||
1808 | return; | 1803 | return; | ||
1809 | } | 1804 | } | ||
1810 | KUrl redir(url); | 1805 | QUrl redir(url); | ||
1811 | redir.setPath(cPath); | 1806 | redir.setPath(cPath); | ||
1812 | redirection(redir); | 1807 | redirection(redir); | ||
1813 | 1808 | | |||
1814 | qCDebug(KIO_SFTP_LOG) << "redirecting to " << redir.url(); | 1809 | qCDebug(KIO_SFTP_LOG) << "redirecting to " << redir.url(); | ||
1815 | 1810 | | |||
1816 | finished(); | 1811 | finished(); | ||
1817 | return; | 1812 | return; | ||
1818 | } | 1813 | } | ||
▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Line(s) | 1895 | if (details > 0) { | |||
1911 | } | 1906 | } | ||
1912 | 1907 | | |||
1913 | entry.insert(KIO::UDSEntry::UDS_ACCESS_TIME, dirent->atime); | 1908 | entry.insert(KIO::UDSEntry::UDS_ACCESS_TIME, dirent->atime); | ||
1914 | entry.insert(KIO::UDSEntry::UDS_MODIFICATION_TIME, dirent->mtime); | 1909 | entry.insert(KIO::UDSEntry::UDS_MODIFICATION_TIME, dirent->mtime); | ||
1915 | entry.insert(KIO::UDSEntry::UDS_CREATION_TIME, dirent->createtime); | 1910 | entry.insert(KIO::UDSEntry::UDS_CREATION_TIME, dirent->createtime); | ||
1916 | } | 1911 | } | ||
1917 | 1912 | | |||
1918 | sftp_attributes_free(dirent); | 1913 | sftp_attributes_free(dirent); | ||
1919 | listEntry(entry, false); | 1914 | listEntry(entry); | ||
1920 | } // for ever | 1915 | } // for ever | ||
1921 | sftp_closedir(dp); | 1916 | sftp_closedir(dp); | ||
1922 | listEntry(entry, true); // ready | | |||
1923 | | ||||
1924 | finished(); | 1917 | finished(); | ||
1925 | } | 1918 | } | ||
1926 | 1919 | | |||
1927 | void sftpProtocol::mkdir(const QUrl &url, int permissions) { | 1920 | void sftpProtocol::mkdir(const QUrl &url, int permissions) { | ||
1928 | qCDebug(KIO_SFTP_LOG) << "create directory: " << url; | 1921 | qCDebug(KIO_SFTP_LOG) << "create directory: " << url; | ||
1929 | 1922 | | |||
1930 | if (!sftpLogin()) { | 1923 | if (!sftpLogin()) { | ||
1931 | return; | 1924 | return; | ||
▲ Show 20 Lines • Show All 287 Lines • ▼ Show 20 Line(s) | 2203 | sftpProtocol::GetRequest::~GetRequest() { | |||
2219 | 2212 | | |||
2220 | // Close channel & free attributes | 2213 | // Close channel & free attributes | ||
2221 | sftp_close(mFile); | 2214 | sftp_close(mFile); | ||
2222 | sftp_attributes_free(mSb); | 2215 | sftp_attributes_free(mSb); | ||
2223 | } | 2216 | } | ||
2224 | 2217 | | |||
2225 | void sftpProtocol::requiresUserNameRedirection() | 2218 | void sftpProtocol::requiresUserNameRedirection() | ||
2226 | { | 2219 | { | ||
2227 | KUrl redirectUrl; | 2220 | QUrl redirectUrl; | ||
2228 | redirectUrl.setProtocol( QLatin1String("sftp") ); | 2221 | redirectUrl.setScheme( QLatin1String("sftp") ); | ||
2229 | redirectUrl.setUser( mUsername ); | 2222 | redirectUrl.setUserName( mUsername ); | ||
2230 | redirectUrl.setPass( mPassword ); | 2223 | redirectUrl.setPassword( mPassword ); | ||
2231 | redirectUrl.setHost( mHost ); | 2224 | redirectUrl.setHost( mHost ); | ||
2232 | if (mPort > 0 && mPort != DEFAULT_SFTP_PORT) { | 2225 | if (mPort > 0 && mPort != DEFAULT_SFTP_PORT) { | ||
2233 | redirectUrl.setPort( mPort ); | 2226 | redirectUrl.setPort( mPort ); | ||
2234 | } | 2227 | } | ||
2235 | qCDebug(KIO_SFTP_LOG) << "redirecting to" << redirectUrl; | 2228 | qCDebug(KIO_SFTP_LOG) << "redirecting to" << redirectUrl; | ||
2236 | redirection( redirectUrl ); | 2229 | redirection( redirectUrl ); | ||
2237 | } | 2230 | } | ||
2238 | 2231 | | |||
▲ Show 20 Lines • Show All 60 Lines • Show Last 20 Lines |
This should store the return value from openPasswordDialogV2 and use it in the error call 3 lines below, that's the whole point of the port to V2.
Same for the calls above, if possible (the patch doesn't show what the error handling is).