More error handling of propagator, some cleanups.

This commit is contained in:
Klaas Freitag 2013-05-04 17:14:25 +02:00
parent 7df23a1b19
commit c956d11183
3 changed files with 79 additions and 51 deletions

View file

@ -381,7 +381,7 @@ void CSyncThread::startSync()
performedActions.insert(item._file, a); performedActions.insert(item._file, a);
continue; continue;
} }
propagator.etag.clear(); // FIXME : set to the right one propagator._etag.clear(); // FIXME : set to the right one
a.instruction = propagator.propagate(item); a.instruction = propagator.propagate(item);
if (item._isDirectory && item._instruction == CSYNC_INSTRUCTION_REMOVE if (item._isDirectory && item._instruction == CSYNC_INSTRUCTION_REMOVE
@ -391,7 +391,7 @@ void CSyncThread::startSync()
lastDeleted.clear(); lastDeleted.clear();
} }
a.etag = propagator.etag; a.etag = propagator._etag;
performedActions.insert(item._file, a); performedActions.insert(item._file, a);
if (item._instruction == CSYNC_INSTRUCTION_RENAME) { if (item._instruction == CSYNC_INSTRUCTION_RENAME) {

View file

@ -20,6 +20,7 @@
#include <qdiriterator.h> #include <qdiriterator.h>
#include <qtemporaryfile.h> #include <qtemporaryfile.h>
#include <qabstractfileengine.h> #include <qabstractfileengine.h>
#include <qdebug.h>
#include <neon/ne_basic.h> #include <neon/ne_basic.h>
#include <neon/ne_socket.h> #include <neon/ne_socket.h>
@ -101,7 +102,7 @@ csync_instructions_e OwncloudPropagator::localRemove(const SyncFileItem& item)
_errorString = file.errorString(); _errorString = file.errorString();
} }
//FIXME: we should update the md5 //FIXME: we should update the md5
etag.clear(); _etag.clear();
//FIXME: we should update the mtime //FIXME: we should update the mtime
return CSYNC_INSTRUCTION_NONE; // not ERROR so it is still written to the database return CSYNC_INSTRUCTION_NONE; // not ERROR so it is still written to the database
} }
@ -133,15 +134,18 @@ csync_instructions_e OwncloudPropagator::remoteRemove(const SyncFileItem &item)
csync_instructions_e OwncloudPropagator::remoteMkdir(const SyncFileItem &item) csync_instructions_e OwncloudPropagator::remoteMkdir(const SyncFileItem &item)
{ {
QScopedPointer<char, QScopedPointerPodDeleter> uri(ne_path_escape((_remoteDir + item._file).toUtf8())); QScopedPointer<char, QScopedPointerPodDeleter> uri(ne_path_escape((_remoteDir + item._file).toUtf8()));
bool error = false;
int rc = ne_mkcol(_session, uri.data()); int rc = ne_mkcol(_session, uri.data());
if (rc != NE_OK) { error = updateErrorFromSession( rc );
updateErrorFromSession();
if( error ) {
/* Special for mkcol: it returns 405 if the directory already exists. /* Special for mkcol: it returns 405 if the directory already exists.
* Ignre that error */ * Ignre that error */
if (_errorCode != 405) if (_httpStatusCode != 405) {
return CSYNC_INSTRUCTION_ERROR; return CSYNC_INSTRUCTION_ERROR;
} }
}
return CSYNC_INSTRUCTION_UPDATED; return CSYNC_INSTRUCTION_UPDATED;
} }
@ -205,7 +209,7 @@ csync_instructions_e OwncloudPropagator::uploadFile(const SyncFileItem &item)
if( finished ) { if( finished ) {
_errorString = hbf_error_string(state); _errorString = hbf_error_string(state);
// errorCode = hbf_fail_http_code(trans); _httpStatusCode = hbf_fail_http_code(trans.data());
// if (dav_session.chunk_info) { // if (dav_session.chunk_info) {
// dav_session.chunk_info->start_id = trans->start_id; // dav_session.chunk_info->start_id = trans->start_id;
// dav_session.chunk_info->transfer_id = trans->transfer_id; // dav_session.chunk_info->transfer_id = trans->transfer_id;
@ -239,29 +243,39 @@ void OwncloudPropagator::updateMTimeAndETag(const char* uri, time_t mtime)
ops[1].name = NULL; ops[1].name = NULL;
int rc = ne_proppatch( _session, uri, ops ); int rc = ne_proppatch( _session, uri, ops );
if( rc != NE_OK ) { bool error = updateErrorFromSession( rc );
//FIXME if( error ) {
// FIXME: We could not set the mtime. Error or not?
} }
// get the etag // get the etag
QScopedPointer<ne_request, ScopedPointerHelpers> req(ne_request_create(_session, "HEAD", uri)); QScopedPointer<ne_request, ScopedPointerHelpers> req(ne_request_create(_session, "HEAD", uri));
int neon_stat = ne_request_dispatch(req.data()); int neon_stat = ne_request_dispatch(req.data());
if (neon_stat != NE_OK) {
//FIXME if( neon_stat != NE_OK ) {
updateErrorFromSession(neon_stat);
} else { } else {
const ne_status *stat = ne_get_status( req.data() );
if( stat && stat->klass != 2 ) {
_httpStatusCode = stat->code;
_errorCode = CSYNC_ERR_HTTP;
_errorString = QString::fromUtf8( stat->reason_phrase );
}
}
if( _errorCode == CSYNC_ERR_NONE ) {
const char *header = ne_get_response_header(req.data(), "etag"); const char *header = ne_get_response_header(req.data(), "etag");
if(header && header [0] == '"' && header[ strlen(header)-1] == '"') { if(header && header [0] == '"' && header[ strlen(header)-1] == '"') {
etag = QByteArray(header + 1, strlen(header)-2); _etag = QByteArray(header + 1, strlen(header)-2);
} else { } else {
etag = header; _etag = header;
} }
} }
} }
class DownloadContext {
public:
struct DownloadContext {
QFile *file; QFile *file;
QScopedPointer<ne_decompress, ScopedPointerHelpers> decompress; QScopedPointer<ne_decompress, ScopedPointerHelpers> decompress;
@ -269,7 +283,7 @@ struct DownloadContext {
static int content_reader(void *userdata, const char *buf, size_t len) static int content_reader(void *userdata, const char *buf, size_t len)
{ {
DownloadContext *writeCtx = reinterpret_cast<DownloadContext *>(userdata); DownloadContext *writeCtx = static_cast<DownloadContext *>(userdata);
size_t written = 0; size_t written = 0;
if(buf) { if(buf) {
@ -291,7 +305,7 @@ struct DownloadContext {
*/ */
static void install_content_reader( ne_request *req, void *userdata, const ne_status *status ) static void install_content_reader( ne_request *req, void *userdata, const ne_status *status )
{ {
DownloadContext *writeCtx = reinterpret_cast<DownloadContext *>(userdata); DownloadContext *writeCtx = static_cast<DownloadContext *>(userdata);
Q_UNUSED(status); Q_UNUSED(status);
@ -330,6 +344,7 @@ csync_instructions_e OwncloudPropagator::downloadFile(const SyncFileItem &item)
QTemporaryFile tmpFile(_localDir + item._file); QTemporaryFile tmpFile(_localDir + item._file);
if (!tmpFile.open()) { if (!tmpFile.open()) {
_errorString = tmpFile.errorString(); _errorString = tmpFile.errorString();
_errorCode = CSYNC_ERR_FILESYSTEM;
return CSYNC_INSTRUCTION_ERROR; return CSYNC_INSTRUCTION_ERROR;
} }
@ -364,8 +379,9 @@ csync_instructions_e OwncloudPropagator::downloadFile(const SyncFileItem &item)
int neon_stat = ne_request_dispatch(req.data()); int neon_stat = ne_request_dispatch(req.data());
if (neon_stat == NE_TIMEOUT && (++retry) < 3) if (neon_stat == NE_TIMEOUT && (++retry) < 3) {
continue; continue;
}
/* delete the hook again, otherwise they get chained as they are with the session */ /* delete the hook again, otherwise they get chained as they are with the session */
ne_unhook_post_headers( _session, DownloadContext::install_content_reader, &writeCtx ); ne_unhook_post_headers( _session, DownloadContext::install_content_reader, &writeCtx );
@ -374,8 +390,8 @@ csync_instructions_e OwncloudPropagator::downloadFile(const SyncFileItem &item)
/* possible return codes are: /* possible return codes are:
* NE_OK, NE_AUTH, NE_CONNECT, NE_TIMEOUT, NE_ERROR (from ne_request.h) * NE_OK, NE_AUTH, NE_CONNECT, NE_TIMEOUT, NE_ERROR (from ne_request.h)
*/ */
if( neon_stat != NE_OK ) {
if( neon_stat != NE_OK ) {
updateErrorFromSession(neon_stat); updateErrorFromSession(neon_stat);
qDebug("Error GET: Neon: %d", neon_stat); qDebug("Error GET: Neon: %d", neon_stat);
return CSYNC_INSTRUCTION_ERROR; return CSYNC_INSTRUCTION_ERROR;
@ -468,39 +484,51 @@ bool OwncloudPropagator::check_neon_session()
bool OwncloudPropagator::updateErrorFromSession(int neon_code) bool OwncloudPropagator::updateErrorFromSession(int neon_code)
{ {
bool re = false;
if( neon_code != NE_OK ) { if( neon_code != NE_OK ) {
qDebug("Neon error code was %d", neon_code); qDebug("Neon error code was %d", neon_code);
} }
#if 0
switch(neon_code) { switch(neon_code) {
case NE_OK: /* Success, but still the possiblity of problems */ case NE_OK: /* Success, but still the possiblity of problems */
check_neon_session(); if( check_neon_session() ) {
re = true;
}
break;
case NE_ERROR: /* Generic error; use ne_get_error(session) for message */ case NE_ERROR: /* Generic error; use ne_get_error(session) for message */
_errorString = QString::fromUtf8( ne_get_error(_session) ); _errorString = QString::fromUtf8( ne_get_error(_session) );
_errorCode = CSYNC_ERR_HTTP;
break; break;
case NE_LOOKUP: /* Server or proxy hostname lookup failed */ case NE_LOOKUP: /* Server or proxy hostname lookup failed */
_errorString = QString::fromUtf8( ne_get_error(_session) );
_errorCode = CSYNC_ERR_LOOKUP;
break; break;
case NE_AUTH: /* User authentication failed on server */ case NE_AUTH: /* User authentication failed on server */
_errorString = QString::fromUtf8( ne_get_error(_session) );
_errorCode = CSYNC_ERR_AUTH_SERVER;
break; break;
case NE_PROXYAUTH: /* User authentication failed on proxy */ case NE_PROXYAUTH: /* User authentication failed on proxy */
_errorString = QString::fromUtf8( ne_get_error(_session) );
_errorCode = CSYNC_ERR_AUTH_PROXY;
break; break;
case NE_CONNECT: /* Could not connect to server */ case NE_CONNECT: /* Could not connect to server */
_errorString = QString::fromUtf8( ne_get_error(_session) );
_errorCode = CSYNC_ERR_CONNECT;
break; break;
case NE_TIMEOUT: /* Connection timed out */ case NE_TIMEOUT: /* Connection timed out */
_errorString = QString::fromUtf8( ne_get_error(_session) );
_errorCode = CSYNC_ERR_TIMEOUT;
break; break;
case NE_FAILED: /* The precondition failed */ case NE_FAILED: /* The precondition failed */
break;
case NE_RETRY: /* Retry request (ne_end_request ONLY) */ case NE_RETRY: /* Retry request (ne_end_request ONLY) */
break;
case NE_REDIRECT: /* See ne_redirect.h */ case NE_REDIRECT: /* See ne_redirect.h */
break;
default: default:
_errorString = QString::fromUtf8( ne_get_error(_session) );
_errorCode = CSYNC_ERR_HTTP;
break;
} }
#endif return re;
qFatal("unimplemented");
//don't forget to update errorCode to the http code
} }

View file

@ -58,7 +58,7 @@ public:
CSYNC_ERROR_CODE _errorCode; CSYNC_ERROR_CODE _errorCode;
int _httpStatusCode; int _httpStatusCode;
QByteArray etag; QByteArray _etag;
}; };