2014-02-06 10:50:16 +00:00
/*
* Copyright ( C ) by Olivier Goffart < ogoffart @ owncloud . 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 .
*/
2015-05-15 08:50:55 +00:00
# include "config.h"
2014-11-11 11:16:14 +00:00
# include "owncloudpropagator_p.h"
# include "propagatedownload.h"
2014-02-06 10:50:16 +00:00
# include "networkjobs.h"
# include "account.h"
2017-09-01 16:11:43 +00:00
# include "common/syncjournaldb.h"
# include "common/syncjournalfilerecord.h"
2017-08-16 06:36:52 +00:00
# include "common/utility.h"
2014-02-18 11:54:40 +00:00
# include "filesystem.h"
2014-04-29 14:47:07 +00:00
# include "propagatorjobs.h"
2021-07-14 12:36:25 +00:00
# include <common/asserts.h>
# include <common/constants.h>
2018-01-05 20:37:31 +00:00
# include "clientsideencryptionjobs.h"
2018-01-21 20:25:06 +00:00
# include "propagatedownloadencrypted.h"
2018-08-15 08:46:16 +00:00
# include "common/vfs.h"
2015-05-15 13:39:26 +00:00
2017-05-09 12:24:11 +00:00
# include <QLoggingCategory>
2014-02-06 10:50:16 +00:00
# include <QNetworkAccessManager>
2014-02-17 12:48:56 +00:00
# include <QFileInfo>
2014-05-23 16:55:44 +00:00
# include <QDir>
2014-02-13 13:02:05 +00:00
# include <cmath>
2014-02-06 10:50:16 +00:00
2016-09-14 13:31:05 +00:00
# ifdef Q_OS_UNIX
# include <unistd.h>
# endif
2014-11-09 21:34:07 +00:00
namespace OCC {
2014-02-06 10:50:16 +00:00
2017-12-28 19:33:10 +00:00
Q_LOGGING_CATEGORY ( lcGetJob , " nextcloud.sync.networkjob.get " , QtInfoMsg )
Q_LOGGING_CATEGORY ( lcPropagateDownload , " nextcloud.sync.propagator.download " , QtInfoMsg )
2017-05-09 12:24:11 +00:00
2015-05-11 13:41:56 +00:00
// Always coming in with forward slashes.
// In csync_excluded_no_ctx we ignore all files with longer than 254 chars
2015-10-05 03:20:09 +00:00
// This function also adds a dot at the beginning of the filename to hide the file on OS X and Linux
2015-05-22 07:17:24 +00:00
QString OWNCLOUDSYNC_EXPORT createDownloadTmpFileName ( const QString & previous )
{
2015-05-11 13:41:56 +00:00
QString tmpFileName ;
QString tmpPath ;
int slashPos = previous . lastIndexOf ( ' / ' ) ;
// work with both pathed filenames and only filenames
if ( slashPos = = - 1 ) {
tmpFileName = previous ;
tmpPath = QString ( ) ;
} else {
tmpFileName = previous . mid ( slashPos + 1 ) ;
tmpPath = previous . left ( slashPos ) ;
}
int overhead = 1 + 1 + 2 + 8 ; // slash dot dot-tilde ffffffff"
int spaceForFileName = qMin ( 254 , tmpFileName . length ( ) + overhead ) - overhead ;
if ( tmpPath . length ( ) > 0 ) {
2021-09-03 19:37:46 +00:00
return tmpPath + ' / ' + ' . ' + tmpFileName . left ( spaceForFileName ) + " .~ " + ( QString : : number ( uint ( Utility : : rand ( ) % 0xFFFFFFFF ) , 16 ) ) ;
2015-05-11 13:41:56 +00:00
} else {
2021-09-03 19:37:46 +00:00
return ' . ' + tmpFileName . left ( spaceForFileName ) + " .~ " + ( QString : : number ( uint ( Utility : : rand ( ) % 0xFFFFFFFF ) , 16 ) ) ;
2015-05-08 14:41:41 +00:00
}
}
2015-10-05 03:20:09 +00:00
// DOES NOT take ownership of the device.
2018-08-15 08:46:16 +00:00
GETFileJob : : GETFileJob ( AccountPtr account , const QString & path , QIODevice * device ,
2014-12-18 11:13:12 +00:00
const QMap < QByteArray , QByteArray > & headers , const QByteArray & expectedEtagForResume ,
2019-02-13 09:15:33 +00:00
qint64 resumeStart , QObject * parent )
2014-06-03 09:50:13 +00:00
: AbstractNetworkJob ( account , path , parent )
2021-07-14 12:36:25 +00:00
, _device ( device )
2014-12-18 11:13:12 +00:00
, _headers ( headers )
, _expectedEtagForResume ( expectedEtagForResume )
2018-08-15 08:46:16 +00:00
, _expectedContentLength ( - 1 )
2014-12-18 11:13:12 +00:00
, _resumeStart ( resumeStart )
, _errorStatus ( SyncFileItem : : NoStatus )
2014-11-11 11:16:14 +00:00
, _bandwidthLimited ( false )
, _bandwidthChoked ( false )
, _bandwidthQuota ( 0 )
2018-11-12 17:46:39 +00:00
, _bandwidthManager ( nullptr )
2015-02-06 09:20:10 +00:00
, _hasEmittedFinishedSignal ( false )
, _lastModified ( )
2021-09-10 13:51:48 +00:00
, _contentLength ( - 1 )
2014-06-03 09:50:13 +00:00
{
}
2018-08-15 08:46:16 +00:00
GETFileJob : : GETFileJob ( AccountPtr account , const QUrl & url , QIODevice * device ,
2015-01-08 14:33:39 +00:00
const QMap < QByteArray , QByteArray > & headers , const QByteArray & expectedEtagForResume ,
2019-02-13 09:15:33 +00:00
qint64 resumeStart , QObject * parent )
2014-06-03 09:50:13 +00:00
: AbstractNetworkJob ( account , url . toEncoded ( ) , parent )
2021-09-10 13:51:48 +00:00
, _device ( device )
2014-12-18 11:13:12 +00:00
, _headers ( headers )
, _expectedEtagForResume ( expectedEtagForResume )
2018-08-15 08:46:16 +00:00
, _expectedContentLength ( - 1 )
2014-12-18 11:13:12 +00:00
, _resumeStart ( resumeStart )
, _errorStatus ( SyncFileItem : : NoStatus )
, _directDownloadUrl ( url )
2014-11-11 11:16:14 +00:00
, _bandwidthLimited ( false )
, _bandwidthChoked ( false )
, _bandwidthQuota ( 0 )
2018-11-12 17:46:39 +00:00
, _bandwidthManager ( nullptr )
2015-02-06 09:20:10 +00:00
, _hasEmittedFinishedSignal ( false )
, _lastModified ( )
2021-09-10 13:51:48 +00:00
, _contentLength ( - 1 )
2014-06-03 09:50:13 +00:00
{
}
2014-02-17 12:48:56 +00:00
void GETFileJob : : start ( )
{
2014-10-08 10:04:17 +00:00
if ( _resumeStart > 0 ) {
_headers [ " Range " ] = " bytes= " + QByteArray : : number ( _resumeStart ) + ' - ' ;
_headers [ " Accept-Ranges " ] = " bytes " ;
2017-03-30 11:46:20 +00:00
qCDebug ( lcGetJob ) < < " Retry with range " < < _headers [ " Range " ] ;
2014-10-08 10:04:17 +00:00
}
2014-02-17 12:48:56 +00:00
QNetworkRequest req ;
for ( QMap < QByteArray , QByteArray > : : const_iterator it = _headers . begin ( ) ; it ! = _headers . end ( ) ; + + it ) {
req . setRawHeader ( it . key ( ) , it . value ( ) ) ;
}
2017-05-22 12:41:06 +00:00
req . setPriority ( QNetworkRequest : : LowPriority ) ; // Long downloads must not block non-propagation jobs.
2014-06-03 09:50:13 +00:00
if ( _directDownloadUrl . isEmpty ( ) ) {
2017-03-03 10:20:53 +00:00
sendRequest ( " GET " , makeDavUrl ( path ( ) ) , req ) ;
2014-06-03 09:50:13 +00:00
} else {
// Use direct URL
2017-03-03 10:20:53 +00:00
sendRequest ( " GET " , _directDownloadUrl , req ) ;
2014-06-03 09:50:13 +00:00
}
2014-11-11 11:16:14 +00:00
2017-03-30 11:46:20 +00:00
qCDebug ( lcGetJob ) < < _bandwidthManager < < _bandwidthChoked < < _bandwidthLimited ;
2014-11-11 11:16:14 +00:00
if ( _bandwidthManager ) {
_bandwidthManager - > registerDownloadJob ( this ) ;
}
2014-02-17 12:48:56 +00:00
2017-09-20 08:14:48 +00:00
connect ( this , & AbstractNetworkJob : : networkActivity , account ( ) . data ( ) , & Account : : propagatorNetworkActivity ) ;
2014-02-17 12:48:56 +00:00
AbstractNetworkJob : : start ( ) ;
}
2017-11-14 10:44:02 +00:00
void GETFileJob : : newReplyHook ( QNetworkReply * reply )
{
reply - > setReadBufferSize ( 16 * 1024 ) ; // keep low so we can easier limit the bandwidth
connect ( reply , & QNetworkReply : : metaDataChanged , this , & GETFileJob : : slotMetaDataChanged ) ;
connect ( reply , & QIODevice : : readyRead , this , & GETFileJob : : slotReadyRead ) ;
2018-06-13 12:20:21 +00:00
connect ( reply , & QNetworkReply : : finished , this , & GETFileJob : : slotReadyRead ) ;
2017-11-14 10:44:02 +00:00
connect ( reply , & QNetworkReply : : downloadProgress , this , & GETFileJob : : downloadProgress ) ;
}
2014-03-20 12:26:40 +00:00
void GETFileJob : : slotMetaDataChanged ( )
{
2014-11-11 11:16:14 +00:00
// For some reason setting the read buffer in GETFileJob::start doesn't seem to go
// through the HTTP layer thread(?)
reply ( ) - > setReadBufferSize ( 16 * 1024 ) ;
2014-10-08 12:09:57 +00:00
int httpStatus = reply ( ) - > attribute ( QNetworkRequest : : HttpStatusCodeAttribute ) . toInt ( ) ;
2018-10-17 12:49:00 +00:00
if ( httpStatus = = 301 | | httpStatus = = 302 | | httpStatus = = 303 | | httpStatus = = 307
| | httpStatus = = 308 | | httpStatus = = 401 ) {
// Redirects and auth failures (oauth token renew) are handled by AbstractNetworkJob and
// will end up restarting the job. We do not want to process further data from the initial
// request. newReplyHook() will reestablish signal connections for the follow-up request.
bool ok = disconnect ( reply ( ) , & QNetworkReply : : finished , this , & GETFileJob : : slotReadyRead )
& & disconnect ( reply ( ) , & QNetworkReply : : readyRead , this , & GETFileJob : : slotReadyRead ) ;
ASSERT ( ok ) ;
2017-11-14 10:44:02 +00:00
return ;
2018-10-17 12:49:00 +00:00
}
2017-11-14 10:44:02 +00:00
2014-10-08 12:09:57 +00:00
// If the status code isn't 2xx, don't write the reply body to the file.
// For any error: handle it when the job is finished, not here.
if ( httpStatus / 100 ! = 2 ) {
2018-05-30 14:29:29 +00:00
// Disable the buffer limit, as we don't limit the bandwidth for error messages.
// (We are only going to do a readAll() at the end.)
reply ( ) - > setReadBufferSize ( 0 ) ;
2014-10-08 12:09:57 +00:00
return ;
}
if ( reply ( ) - > error ( ) ! = QNetworkReply : : NoError ) {
2014-04-22 10:34:03 +00:00
return ;
}
2014-11-11 11:16:14 +00:00
_etag = getEtagFromReply ( reply ( ) ) ;
2014-04-22 10:34:03 +00:00
2014-06-03 09:50:13 +00:00
if ( ! _directDownloadUrl . isEmpty ( ) & & ! _etag . isEmpty ( ) ) {
2017-03-30 11:46:20 +00:00
qCInfo ( lcGetJob ) < < " Direct download used, ignoring server ETag " < < _etag ;
2014-06-03 09:50:13 +00:00
_etag = QByteArray ( ) ; // reset received ETag
} else if ( ! _directDownloadUrl . isEmpty ( ) ) {
// All fine, ETag empty and directDownloadUrl used
} else if ( _etag . isEmpty ( ) ) {
2017-03-30 11:46:20 +00:00
qCWarning ( lcGetJob ) < < " No E-Tag reply by server, considering it invalid " ;
2014-03-20 12:26:40 +00:00
_errorString = tr ( " No E-Tag received from server, check Proxy/Gateway " ) ;
2014-04-22 10:34:03 +00:00
_errorStatus = SyncFileItem : : NormalError ;
2014-03-20 12:26:40 +00:00
reply ( ) - > abort ( ) ;
return ;
2014-06-03 09:50:13 +00:00
} else if ( ! _expectedEtagForResume . isEmpty ( ) & & _expectedEtagForResume ! = _etag ) {
2017-03-30 11:46:20 +00:00
qCWarning ( lcGetJob ) < < " We received a different E-Tag for resuming! "
2014-06-03 09:50:13 +00:00
< < _expectedEtagForResume < < " vs " < < _etag ;
2014-03-20 12:26:40 +00:00
_errorString = tr ( " We received a different E-Tag for resuming. Retrying next time. " ) ;
2014-04-22 10:34:03 +00:00
_errorStatus = SyncFileItem : : NormalError ;
2014-03-20 12:26:40 +00:00
reply ( ) - > abort ( ) ;
return ;
}
2014-07-18 10:02:57 +00:00
2020-12-10 18:52:58 +00:00
bool ok = false ;
2020-12-01 15:58:19 +00:00
_contentLength = reply ( ) - > header ( QNetworkRequest : : ContentLengthHeader ) . toLongLong ( & ok ) ;
if ( ok & & _expectedContentLength ! = - 1 & & _contentLength ! = _expectedContentLength ) {
2018-08-15 08:46:16 +00:00
qCWarning ( lcGetJob ) < < " We received a different content length than expected! "
< < _expectedContentLength < < " vs " < < _contentLength ;
_errorString = tr ( " We received an unexpected download Content-Length. " ) ;
_errorStatus = SyncFileItem : : NormalError ;
reply ( ) - > abort ( ) ;
return ;
}
2019-02-13 09:15:33 +00:00
qint64 start = 0 ;
2014-08-29 17:23:08 +00:00
QByteArray ranges = reply ( ) - > rawHeader ( " Content-Range " ) ;
2014-07-18 10:02:57 +00:00
if ( ! ranges . isEmpty ( ) ) {
2022-12-19 16:26:15 +00:00
static const QRegularExpression rx ( " bytes ( \\ d+) - " ) ;
2021-10-29 14:56:29 +00:00
const auto rxMatch = rx . match ( ranges ) ;
if ( rxMatch . hasMatch ( ) ) {
start = rxMatch . captured ( 1 ) . toLongLong ( ) ;
2014-07-18 10:02:57 +00:00
}
}
if ( start ! = _resumeStart ) {
2017-03-30 11:46:20 +00:00
qCWarning ( lcGetJob ) < < " Wrong content-range: " < < ranges < < " while expecting start was " < < _resumeStart ;
2015-01-15 16:58:43 +00:00
if ( ranges . isEmpty ( ) ) {
2015-10-05 03:20:09 +00:00
// device doesn't support range, just try again from scratch
2014-07-18 10:02:57 +00:00
_device - > close ( ) ;
if ( ! _device - > open ( QIODevice : : WriteOnly ) ) {
_errorString = _device - > errorString ( ) ;
_errorStatus = SyncFileItem : : NormalError ;
reply ( ) - > abort ( ) ;
return ;
}
2014-08-29 17:23:08 +00:00
_resumeStart = 0 ;
2014-07-18 10:02:57 +00:00
} else {
_errorString = tr ( " Server returned wrong content-range " ) ;
_errorStatus = SyncFileItem : : NormalError ;
reply ( ) - > abort ( ) ;
return ;
}
}
2015-02-06 09:20:10 +00:00
auto lastModified = reply ( ) - > header ( QNetworkRequest : : LastModifiedHeader ) ;
if ( ! lastModified . isNull ( ) ) {
_lastModified = Utility : : qDateTimeToTime_t ( lastModified . toDateTime ( ) ) ;
}
2017-11-14 10:44:02 +00:00
_saveBodyToFile = true ;
2014-03-20 12:26:40 +00:00
}
2014-11-11 11:16:14 +00:00
void GETFileJob : : setBandwidthManager ( BandwidthManager * bwm )
{
_bandwidthManager = bwm ;
}
void GETFileJob : : setChoked ( bool c )
{
_bandwidthChoked = c ;
QMetaObject : : invokeMethod ( this , " slotReadyRead " , Qt : : QueuedConnection ) ;
}
void GETFileJob : : setBandwidthLimited ( bool b )
{
_bandwidthLimited = b ;
QMetaObject : : invokeMethod ( this , " slotReadyRead " , Qt : : QueuedConnection ) ;
}
void GETFileJob : : giveBandwidthQuota ( qint64 q )
{
_bandwidthQuota = q ;
2017-03-30 11:46:20 +00:00
qCDebug ( lcGetJob ) < < " Got " < < q < < " bytes " ;
2014-11-11 11:16:14 +00:00
QMetaObject : : invokeMethod ( this , " slotReadyRead " , Qt : : QueuedConnection ) ;
}
qint64 GETFileJob : : currentDownloadPosition ( )
{
if ( _device & & _device - > pos ( ) > 0 & & _device - > pos ( ) > qint64 ( _resumeStart ) ) {
return _device - > pos ( ) ;
}
return _resumeStart ;
}
2021-07-14 12:36:25 +00:00
qint64 GETFileJob : : writeToDevice ( const QByteArray & data )
2021-05-19 06:52:08 +00:00
{
2021-07-14 12:36:25 +00:00
return _device - > write ( data ) ;
2021-05-19 06:52:08 +00:00
}
2014-02-17 12:48:56 +00:00
void GETFileJob : : slotReadyRead ( )
{
PropagateDownload: fix possible crash
Backtrace looks like this:
Crash: EXCEPTION_ACCESS_VIOLATION_READ at 0x0
File "propagatedownload.cpp", line 234, in OCC::GETFileJob::slotReadyRead
File "moc_propagatedownl_CA5CFSHZDTX34X.cpp", line 86, in OCC::GETFileJob::qt_static_metacall
File "qobject.cpp", line 495, in QMetaCallEvent::placeMetaCall
File "qobject.cpp", line 1256, in QObject::event
File "qapplication.cpp", line 3804, in QApplicationPrivate::notify_helper
GETFileJob::slotReadyRead can be called with a QueuedConnection when the
bendwith manager is involved. In that case, if the QNAM was reset
in between, the reply might have been destroyed.
(This is only speculation based on the backtrace)
2017-03-28 13:21:59 +00:00
if ( ! reply ( ) )
return ;
2014-07-08 13:30:53 +00:00
int bufferSize = qMin ( 1024 * 8ll , reply ( ) - > bytesAvailable ( ) ) ;
2014-03-19 14:19:09 +00:00
QByteArray buffer ( bufferSize , Qt : : Uninitialized ) ;
2018-05-30 14:29:29 +00:00
while ( reply ( ) - > bytesAvailable ( ) > 0 & & _saveBodyToFile ) {
2014-11-11 11:16:14 +00:00
if ( _bandwidthChoked ) {
2017-03-30 11:46:20 +00:00
qCWarning ( lcGetJob ) < < " Download choked " ;
2014-11-11 11:16:14 +00:00
break ;
}
qint64 toRead = bufferSize ;
if ( _bandwidthLimited ) {
toRead = qMin ( qint64 ( bufferSize ) , _bandwidthQuota ) ;
if ( toRead = = 0 ) {
2017-03-30 11:46:20 +00:00
qCWarning ( lcGetJob ) < < " Out of quota " ;
2014-11-11 11:16:14 +00:00
break ;
}
_bandwidthQuota - = toRead ;
}
2021-07-14 12:36:25 +00:00
const qint64 readBytes = reply ( ) - > read ( buffer . data ( ) , toRead ) ;
if ( readBytes < 0 ) {
2017-03-23 14:53:22 +00:00
_errorString = networkReplyErrorString ( * reply ( ) ) ;
2014-04-22 10:34:03 +00:00
_errorStatus = SyncFileItem : : NormalError ;
2017-03-30 11:46:20 +00:00
qCWarning ( lcGetJob ) < < " Error while reading from device: " < < _errorString ;
2014-03-19 14:19:09 +00:00
reply ( ) - > abort ( ) ;
return ;
}
2021-07-14 12:36:25 +00:00
const qint64 writtenBytes = writeToDevice ( QByteArray : : fromRawData ( buffer . constData ( ) , readBytes ) ) ;
if ( writtenBytes ! = readBytes ) {
2019-06-05 13:24:57 +00:00
_errorString = _device - > errorString ( ) ;
_errorStatus = SyncFileItem : : NormalError ;
2021-07-14 12:36:25 +00:00
qCWarning ( lcGetJob ) < < " Error while writing to file " < < writtenBytes < < readBytes < < _errorString ;
2019-06-05 13:24:57 +00:00
reply ( ) - > abort ( ) ;
return ;
2014-03-19 14:19:09 +00:00
}
}
2014-11-11 11:16:14 +00:00
2018-05-30 14:29:29 +00:00
if ( reply ( ) - > isFinished ( ) & & ( reply ( ) - > bytesAvailable ( ) = = 0 | | ! _saveBodyToFile ) ) {
2017-03-30 11:46:20 +00:00
qCDebug ( lcGetJob ) < < " Actually finished! " ;
2014-11-11 11:16:14 +00:00
if ( _bandwidthManager ) {
_bandwidthManager - > unregisterDownloadJob ( this ) ;
}
if ( ! _hasEmittedFinishedSignal ) {
2017-03-30 11:46:20 +00:00
qCInfo ( lcGetJob ) < < " GET of " < < reply ( ) - > request ( ) . url ( ) . toString ( ) < < " FINISHED WITH STATUS "
2018-04-17 18:21:49 +00:00
< < replyStatusString ( )
2017-03-30 11:46:20 +00:00
< < reply ( ) - > rawHeader ( " Content-Range " ) < < reply ( ) - > rawHeader ( " Content-Length " ) ;
2014-11-11 11:16:14 +00:00
emit finishedSignal ( ) ;
}
_hasEmittedFinishedSignal = true ;
deleteLater ( ) ;
}
2014-02-17 12:48:56 +00:00
}
2021-03-16 12:31:12 +00:00
void GETFileJob : : cancel ( )
{
2021-06-09 11:40:15 +00:00
const auto networkReply = reply ( ) ;
if ( networkReply & & networkReply - > isRunning ( ) ) {
networkReply - > abort ( ) ;
}
if ( _device & & _device - > isOpen ( ) ) {
_device - > close ( ) ;
2021-03-16 12:31:12 +00:00
}
}
2017-03-23 14:53:22 +00:00
void GETFileJob : : onTimedOut ( )
2014-06-03 09:50:13 +00:00
{
2017-03-30 11:46:20 +00:00
qCWarning ( lcGetJob ) < < " Timeout " < < ( reply ( ) ? reply ( ) - > request ( ) . url ( ) : path ( ) ) ;
2016-02-24 18:52:14 +00:00
if ( ! reply ( ) )
return ;
2014-06-03 09:50:13 +00:00
_errorString = tr ( " Connection Timeout " ) ;
_errorStatus = SyncFileItem : : FatalError ;
reply ( ) - > abort ( ) ;
}
2015-02-26 14:52:07 +00:00
QString GETFileJob : : errorString ( ) const
{
if ( ! _errorString . isEmpty ( ) ) {
return _errorString ;
}
2017-03-23 14:53:22 +00:00
return AbstractNetworkJob : : errorString ( ) ;
2015-02-26 14:52:07 +00:00
}
2021-05-19 06:52:08 +00:00
GETEncryptedFileJob : : GETEncryptedFileJob ( AccountPtr account , const QString & path , QIODevice * device ,
const QMap < QByteArray , QByteArray > & headers , const QByteArray & expectedEtagForResume ,
qint64 resumeStart , EncryptedFile encryptedInfo , QObject * parent )
: GETFileJob ( account , path , device , headers , expectedEtagForResume , resumeStart , parent )
, _encryptedFileInfo ( encryptedInfo )
{
}
GETEncryptedFileJob : : GETEncryptedFileJob ( AccountPtr account , const QUrl & url , QIODevice * device ,
const QMap < QByteArray , QByteArray > & headers , const QByteArray & expectedEtagForResume ,
qint64 resumeStart , EncryptedFile encryptedInfo , QObject * parent )
: GETFileJob ( account , url , device , headers , expectedEtagForResume , resumeStart , parent )
, _encryptedFileInfo ( encryptedInfo )
{
}
2021-07-14 12:36:25 +00:00
qint64 GETEncryptedFileJob : : writeToDevice ( const QByteArray & data )
2021-05-19 06:52:08 +00:00
{
2021-07-14 12:36:25 +00:00
if ( ! _decryptor ) {
// only initialize the decryptor once, because, according to Qt documentation, metadata might get changed during the processing of the data sometimes
// https://doc.qt.io/qt-5/qnetworkreply.html#metaDataChanged
_decryptor . reset ( new EncryptionHelper : : StreamingDecryptor ( _encryptedFileInfo . encryptionKey , _encryptedFileInfo . initializationVector , _contentLength ) ) ;
}
2021-05-19 06:52:08 +00:00
if ( ! _decryptor - > isInitialized ( ) ) {
return - 1 ;
}
2021-07-14 12:36:25 +00:00
const auto bytesRemaining = _contentLength - _processedSoFar - data . length ( ) ;
if ( bytesRemaining ! = 0 & & bytesRemaining < OCC : : Constants : : e2EeTagSize ) {
// decryption is going to fail if last chunk does not include or does not equal to OCC::Constants::e2EeTagSize bytes tag
// we may end up receiving packets beyond OCC::Constants::e2EeTagSize bytes tag at the end
// in that case, we don't want to try and decrypt less than OCC::Constants::e2EeTagSize ending bytes of tag, we will accumulate all the incoming data till the end
// and then, we are going to decrypt the entire chunk containing OCC::Constants::e2EeTagSize bytes at the end
_pendingBytes + = QByteArray ( data . constData ( ) , data . length ( ) ) ;
_processedSoFar + = data . length ( ) ;
if ( _processedSoFar ! = _contentLength ) {
return data . length ( ) ;
}
}
if ( ! _pendingBytes . isEmpty ( ) ) {
const auto decryptedChunk = _decryptor - > chunkDecryption ( _pendingBytes . constData ( ) , _pendingBytes . size ( ) ) ;
if ( decryptedChunk . isEmpty ( ) ) {
qCCritical ( lcPropagateDownload ) < < " Decryption failed! " ;
return - 1 ;
}
GETFileJob : : writeToDevice ( decryptedChunk ) ;
return data . length ( ) ;
}
const auto decryptedChunk = _decryptor - > chunkDecryption ( data . constData ( ) , data . length ( ) ) ;
2021-05-19 06:52:08 +00:00
2021-07-14 12:36:25 +00:00
if ( decryptedChunk . isEmpty ( ) ) {
2021-05-19 06:52:08 +00:00
qCCritical ( lcPropagateDownload ) < < " Decryption failed! " ;
return - 1 ;
}
2021-07-14 12:36:25 +00:00
GETFileJob : : writeToDevice ( decryptedChunk ) ;
2021-05-19 06:52:08 +00:00
2021-07-14 12:36:25 +00:00
_processedSoFar + = data . length ( ) ;
return data . length ( ) ;
2021-05-19 06:52:08 +00:00
}
2016-05-18 14:42:55 +00:00
void PropagateDownloadFile : : start ( )
2014-02-17 12:48:56 +00:00
{
2020-02-05 11:57:09 +00:00
if ( propagator ( ) - > _abortRequested )
2014-02-17 12:48:56 +00:00
return ;
2018-01-21 20:25:06 +00:00
_isEncrypted = false ;
2014-02-17 12:48:56 +00:00
2017-05-09 12:24:11 +00:00
qCDebug ( lcPropagateDownload ) < < _item - > _file < < propagator ( ) - > _activeJobList . count ( ) ;
2018-01-21 20:25:06 +00:00
2020-12-07 17:42:24 +00:00
const auto path = _item - > _file ;
const auto slashPosition = path . lastIndexOf ( ' / ' ) ;
const auto parentPath = slashPosition > = 0 ? path . left ( slashPosition ) : QString ( ) ;
SyncJournalFileRecord parentRec ;
2022-08-04 13:47:39 +00:00
if ( ! propagator ( ) - > _journal - > getFileRecord ( parentPath , & parentRec ) ) {
qCWarning ( lcPropagateDownload ) < < " could not get file from local DB " < < parentPath ;
2022-08-05 10:07:55 +00:00
done ( SyncFileItem : : NormalError , tr ( " could not get file %1 from local DB " ) . arg ( parentPath ) ) ;
return ;
2022-08-04 13:47:39 +00:00
}
2020-06-26 07:43:06 +00:00
const auto account = propagator ( ) - > account ( ) ;
if ( ! account - > capabilities ( ) . clientSideEncryptionAvailable ( ) | |
2020-12-07 17:42:24 +00:00
! parentRec . isValid ( ) | |
! parentRec . _isE2eEncrypted ) {
2020-06-26 07:43:06 +00:00
startAfterIsEncryptedIsChecked ( ) ;
} else {
_downloadEncryptedHelper = new PropagateDownloadEncrypted ( propagator ( ) , parentPath , _item , this ) ;
2020-12-08 15:20:29 +00:00
connect ( _downloadEncryptedHelper , & PropagateDownloadEncrypted : : fileMetadataFound , [ this ] {
2018-01-21 20:25:06 +00:00
_isEncrypted = true ;
startAfterIsEncryptedIsChecked ( ) ;
2018-01-05 20:37:31 +00:00
} ) ;
2018-03-28 21:12:48 +00:00
connect ( _downloadEncryptedHelper , & PropagateDownloadEncrypted : : failed , [ this ] {
done ( SyncFileItem : : NormalError ,
2021-04-12 10:38:15 +00:00
tr ( " File %1 cannot be downloaded because encryption information is missing. " ) . arg ( QDir : : toNativeSeparators ( _item - > _file ) ) ) ;
2018-03-28 21:12:48 +00:00
} ) ;
2018-01-22 13:08:32 +00:00
_downloadEncryptedHelper - > start ( ) ;
2018-01-05 20:37:31 +00:00
}
}
void PropagateDownloadFile : : startAfterIsEncryptedIsChecked ( )
{
2016-02-25 16:40:24 +00:00
_stopwatch . start ( ) ;
2014-02-17 12:48:56 +00:00
2018-08-15 08:46:16 +00:00
auto & syncOptions = propagator ( ) - > syncOptions ( ) ;
2018-11-21 11:23:08 +00:00
auto & vfs = syncOptions . _vfs ;
2018-08-15 08:46:16 +00:00
2019-02-01 10:23:00 +00:00
// For virtual files just dehydrate or create the file and be done
2018-08-15 08:46:16 +00:00
if ( _item - > _type = = ItemTypeVirtualFileDehydration ) {
2020-09-22 09:47:40 +00:00
QString fsPath = propagator ( ) - > fullLocalPath ( _item - > _file ) ;
2019-02-01 10:23:00 +00:00
if ( ! FileSystem : : verifyFileUnchanged ( fsPath , _item - > _previousSize , _item - > _previousModtime ) ) {
propagator ( ) - > _anotherSyncNeeded = true ;
done ( SyncFileItem : : SoftError , tr ( " File has changed since discovery " ) ) ;
return ;
2018-08-15 08:46:16 +00:00
}
2019-02-01 10:23:00 +00:00
qCDebug ( lcPropagateDownload ) < < " dehydrating file " < < _item - > _file ;
2019-10-30 12:16:32 +00:00
auto r = vfs - > dehydratePlaceholder ( * _item ) ;
if ( ! r ) {
done ( SyncFileItem : : NormalError , r . error ( ) ) ;
return ;
}
2022-08-04 13:47:39 +00:00
if ( ! propagator ( ) - > _journal - > deleteFileRecord ( _item - > _originalFile ) ) {
qCWarning ( lcPropagateDownload ) < < " could not delete file from local DB " < < _item - > _originalFile ;
2022-08-05 10:07:55 +00:00
done ( SyncFileItem : : NormalError , tr ( " Could not delete file record %1 from local DB " ) . arg ( _item - > _originalFile ) ) ;
return ;
2022-08-04 13:47:39 +00:00
}
2019-02-01 10:23:00 +00:00
updateMetadata ( false ) ;
2021-04-30 15:44:23 +00:00
if ( ! _item - > _remotePerm . isNull ( ) & & ! _item - > _remotePerm . hasPermission ( RemotePermissions : : CanWrite ) ) {
// make sure ReadOnly flag is preserved for placeholder, similarly to regular files
2021-05-05 17:41:28 +00:00
FileSystem : : setFileReadOnly ( propagator ( ) - > fullLocalPath ( _item - > _file ) , true ) ;
2021-04-30 15:44:23 +00:00
}
2019-02-01 10:23:00 +00:00
return ;
2018-08-15 08:46:16 +00:00
}
2018-11-21 11:23:08 +00:00
if ( vfs - > mode ( ) = = Vfs : : Off & & _item - > _type = = ItemTypeVirtualFile ) {
qCWarning ( lcPropagateDownload ) < < " ignored virtual file type of " < < _item - > _file ;
_item - > _type = ItemTypeFile ;
}
2018-05-18 06:29:40 +00:00
if ( _item - > _type = = ItemTypeVirtualFile ) {
2021-04-30 11:02:17 +00:00
if ( propagator ( ) - > localFileNameClash ( _item - > _file ) ) {
2022-09-21 17:13:54 +00:00
done ( SyncFileItem : : FileNameClash , tr ( " File %1 cannot be downloaded because of a local file name clash! " ) . arg ( QDir : : toNativeSeparators ( _item - > _file ) ) ) ;
2021-04-30 11:02:17 +00:00
return ;
}
2019-02-01 10:23:00 +00:00
qCDebug ( lcPropagateDownload ) < < " creating virtual file " < < _item - > _file ;
2021-01-13 13:38:13 +00:00
// do a klaas' case clash check.
if ( propagator ( ) - > localFileNameClash ( _item - > _file ) ) {
2022-09-21 17:13:54 +00:00
done ( SyncFileItem : : FileNameClash , tr ( " File %1 can not be downloaded because of a local file name clash! " ) . arg ( QDir : : toNativeSeparators ( _item - > _file ) ) ) ;
2021-01-13 13:38:13 +00:00
return ;
}
2019-10-30 12:16:32 +00:00
auto r = vfs - > createPlaceholder ( * _item ) ;
if ( ! r ) {
done ( SyncFileItem : : NormalError , r . error ( ) ) ;
return ;
}
2017-12-13 17:04:58 +00:00
updateMetadata ( false ) ;
2021-04-30 15:44:23 +00:00
if ( ! _item - > _remotePerm . isNull ( ) & & ! _item - > _remotePerm . hasPermission ( RemotePermissions : : CanWrite ) ) {
// make sure ReadOnly flag is preserved for placeholder, similarly to regular files
2021-05-05 17:41:28 +00:00
FileSystem : : setFileReadOnly ( propagator ( ) - > fullLocalPath ( _item - > _file ) , true ) ;
2021-04-30 15:44:23 +00:00
}
2017-12-13 17:04:58 +00:00
return ;
}
2016-01-06 09:01:22 +00:00
if ( _deleteExisting ) {
deleteExistingFolder ( ) ;
// check for error with deletion
if ( _state = = Finished ) {
return ;
}
}
2017-11-16 09:13:21 +00:00
// If we have a conflict where size of the file is unchanged,
2017-06-14 10:14:46 +00:00
// compare the remote checksum to the local one.
// Maybe it's not a real conflict and no download is necessary!
2017-11-16 09:13:21 +00:00
// If the hashes are collision safe and identical, we assume the content is too.
// For weak checksums, we only do that if the mtimes are also identical.
2020-10-19 12:47:58 +00:00
const auto csync_is_collision_safe_hash = [ ] ( const QByteArray & checksum_header )
{
return checksum_header . startsWith ( " SHA " )
| | checksum_header . startsWith ( " MD5: " ) ;
} ;
2021-12-07 14:44:15 +00:00
if ( _item - > _modtime < = 0 ) {
qCWarning ( lcPropagateDownload ( ) ) < < " invalid modified time " < < _item - > _file < < _item - > _modtime ;
}
2017-06-14 10:14:46 +00:00
if ( _item - > _instruction = = CSYNC_INSTRUCTION_CONFLICT
2017-08-24 15:07:22 +00:00
& & _item - > _size = = _item - > _previousSize
2017-11-16 09:13:21 +00:00
& & ! _item - > _checksumHeader . isEmpty ( )
& & ( csync_is_collision_safe_hash ( _item - > _checksumHeader )
| | _item - > _modtime = = _item - > _previousModtime ) ) {
2017-06-14 10:14:46 +00:00
qCDebug ( lcPropagateDownload ) < < _item - > _file < < " may not need download, computing checksum " ;
auto computeChecksum = new ComputeChecksum ( this ) ;
computeChecksum - > setChecksumType ( parseChecksumHeaderType ( _item - > _checksumHeader ) ) ;
2017-09-20 08:14:48 +00:00
connect ( computeChecksum , & ComputeChecksum : : done ,
this , & PropagateDownloadFile : : conflictChecksumComputed ) ;
2019-08-12 06:23:40 +00:00
propagator ( ) - > _activeJobList . append ( this ) ;
2020-09-22 09:47:40 +00:00
computeChecksum - > start ( propagator ( ) - > fullLocalPath ( _item - > _file ) ) ;
2017-06-14 10:14:46 +00:00
return ;
}
startDownload ( ) ;
}
void PropagateDownloadFile : : conflictChecksumComputed ( const QByteArray & checksumType , const QByteArray & checksum )
{
2019-08-12 06:23:40 +00:00
propagator ( ) - > _activeJobList . removeOne ( this ) ;
2017-06-14 10:14:46 +00:00
if ( makeChecksumHeader ( checksumType , checksum ) = = _item - > _checksumHeader ) {
2017-11-16 09:13:21 +00:00
// No download necessary, just update fs and journal metadata
2017-06-14 10:14:46 +00:00
qCDebug ( lcPropagateDownload ) < < _item - > _file < < " remote and local checksum match " ;
2017-11-16 09:13:21 +00:00
// Apply the server mtime locally if necessary, ensuring the journal
// and local mtimes end up identical
2020-09-22 09:47:40 +00:00
auto fn = propagator ( ) - > fullLocalPath ( _item - > _file ) ;
2021-12-06 14:07:57 +00:00
Q_ASSERT ( _item - > _modtime > 0 ) ;
2021-12-07 14:44:15 +00:00
if ( _item - > _modtime < = 0 ) {
qCWarning ( lcPropagateDownload ( ) ) < < " invalid modified time " < < _item - > _file < < _item - > _modtime ;
2021-12-16 14:28:46 +00:00
return ;
2021-12-07 14:44:15 +00:00
}
2017-11-16 09:13:21 +00:00
if ( _item - > _modtime ! = _item - > _previousModtime ) {
2021-12-06 14:07:57 +00:00
Q_ASSERT ( _item - > _modtime > 0 ) ;
2017-11-16 09:13:21 +00:00
FileSystem : : setModTime ( fn , _item - > _modtime ) ;
emit propagator ( ) - > touchedFile ( fn ) ;
}
_item - > _modtime = FileSystem : : getModTime ( fn ) ;
2021-12-06 14:07:57 +00:00
Q_ASSERT ( _item - > _modtime > 0 ) ;
2021-12-07 14:44:15 +00:00
if ( _item - > _modtime < = 0 ) {
qCWarning ( lcPropagateDownload ( ) ) < < " invalid modified time " < < _item - > _file < < _item - > _modtime ;
2021-12-16 14:28:46 +00:00
return ;
2021-12-07 14:44:15 +00:00
}
2017-06-14 10:14:46 +00:00
updateMetadata ( /*isConflict=*/ false ) ;
return ;
}
startDownload ( ) ;
}
void PropagateDownloadFile : : startDownload ( )
{
2020-02-05 11:57:09 +00:00
if ( propagator ( ) - > _abortRequested )
2017-06-14 10:14:46 +00:00
return ;
2014-06-03 09:50:13 +00:00
// do a klaas' case clash check.
2017-01-17 13:29:12 +00:00
if ( propagator ( ) - > localFileNameClash ( _item - > _file ) ) {
2022-09-21 17:13:54 +00:00
done ( SyncFileItem : : FileNameClash , tr ( " File %1 cannot be downloaded because of a local file name clash! " ) . arg ( QDir : : toNativeSeparators ( _item - > _file ) ) ) ;
2014-05-23 16:55:44 +00:00
return ;
}
2017-02-14 11:46:44 +00:00
propagator ( ) - > reportProgress ( * _item , 0 ) ;
2014-02-17 12:48:56 +00:00
QString tmpFileName ;
2014-03-20 12:26:40 +00:00
QByteArray expectedEtagForResume ;
2017-01-17 13:29:12 +00:00
const SyncJournalDb : : DownloadInfo progressInfo = propagator ( ) - > _journal - > getDownloadInfo ( _item - > _file ) ;
2014-02-17 12:48:56 +00:00
if ( progressInfo . _valid ) {
// if the etag has changed meanwhile, remove the already downloaded part.
2015-04-15 13:19:11 +00:00
if ( progressInfo . _etag ! = _item - > _etag ) {
2020-09-22 09:47:40 +00:00
FileSystem : : remove ( propagator ( ) - > fullLocalPath ( progressInfo . _tmpfile ) ) ;
2017-01-17 13:29:12 +00:00
propagator ( ) - > _journal - > setDownloadInfo ( _item - > _file , SyncJournalDb : : DownloadInfo ( ) ) ;
2014-02-17 12:48:56 +00:00
} else {
tmpFileName = progressInfo . _tmpfile ;
2014-03-20 12:26:40 +00:00
expectedEtagForResume = progressInfo . _etag ;
2014-02-17 12:48:56 +00:00
}
}
if ( tmpFileName . isEmpty ( ) ) {
2015-05-12 08:35:28 +00:00
tmpFileName = createDownloadTmpFileName ( _item - > _file ) ;
2014-02-17 12:48:56 +00:00
}
2020-09-22 09:47:40 +00:00
_tmpFile . setFileName ( propagator ( ) - > fullLocalPath ( tmpFileName ) ) ;
2019-08-23 12:49:24 +00:00
_resumeStart = _tmpFile . size ( ) ;
if ( _resumeStart > 0 & & _resumeStart = = _item - > _size ) {
qCInfo ( lcPropagateDownload ) < < " File is already complete, no need to download " ;
downloadFinished ( ) ;
return ;
}
// Can't open(Append) read-only files, make sure to make
// file writable if it exists.
if ( _tmpFile . exists ( ) )
FileSystem : : setFileReadOnly ( _tmpFile . fileName ( ) , false ) ;
2014-02-17 12:48:56 +00:00
if ( ! _tmpFile . open ( QIODevice : : Append | QIODevice : : Unbuffered ) ) {
2019-08-23 12:49:24 +00:00
qCWarning ( lcPropagateDownload ) < < " could not open temporary file " < < _tmpFile . fileName ( ) ;
2014-02-17 12:48:56 +00:00
done ( SyncFileItem : : NormalError , _tmpFile . errorString ( ) ) ;
return ;
}
2019-08-23 12:49:24 +00:00
// Hide temporary after creation
2014-02-18 11:54:40 +00:00
FileSystem : : setFileHidden ( _tmpFile . fileName ( ) , true ) ;
2014-02-17 12:48:56 +00:00
2015-10-01 09:39:09 +00:00
// If there's not enough space to fully download this file, stop.
2017-01-17 13:29:12 +00:00
const auto diskSpaceResult = propagator ( ) - > diskSpaceCheck ( ) ;
2017-05-04 13:12:14 +00:00
if ( diskSpaceResult ! = OwncloudPropagator : : DiskSpaceOk ) {
if ( diskSpaceResult = = OwncloudPropagator : : DiskSpaceFailure ) {
2017-07-12 10:38:53 +00:00
// Using DetailError here will make the error not pop up in the account
2017-06-28 10:45:54 +00:00
// tab: instead we'll generate a general "disk space low" message and show
// these detail errors only in the error view.
2017-07-12 10:38:53 +00:00
done ( SyncFileItem : : DetailError ,
2017-06-28 10:45:54 +00:00
tr ( " The download would reduce free local disk space below the limit " ) ) ;
emit propagator ( ) - > insufficientLocalStorage ( ) ;
2017-05-04 13:12:14 +00:00
} else if ( diskSpaceResult = = OwncloudPropagator : : DiskSpaceCritical ) {
done ( SyncFileItem : : FatalError ,
tr ( " Free space on disk is less than %1 " ) . arg ( Utility : : octetsToString ( criticalFreeSpaceLimit ( ) ) ) ) ;
}
// Remove the temporary, if empty.
if ( _resumeStart = = 0 ) {
_tmpFile . remove ( ) ;
}
2015-10-01 09:39:09 +00:00
return ;
}
2014-02-17 12:48:56 +00:00
{
SyncJournalDb : : DownloadInfo pi ;
2015-04-15 13:19:11 +00:00
pi . _etag = _item - > _etag ;
2014-02-17 12:48:56 +00:00
pi . _tmpfile = tmpFileName ;
pi . _valid = true ;
2017-01-17 13:29:12 +00:00
propagator ( ) - > _journal - > setDownloadInfo ( _item - > _file , pi ) ;
propagator ( ) - > _journal - > commit ( " download file start " ) ;
2014-02-17 12:48:56 +00:00
}
QMap < QByteArray , QByteArray > headers ;
2015-04-15 13:19:11 +00:00
if ( _item - > _directDownloadUrl . isEmpty ( ) ) {
2014-06-03 09:50:13 +00:00
// Normal job, download from oC instance
2017-01-17 13:29:12 +00:00
_job = new GETFileJob ( propagator ( ) - > account ( ) ,
2020-09-22 09:47:40 +00:00
propagator ( ) - > fullRemotePath ( _isEncrypted ? _item - > _encryptedFileName : _item - > _file ) ,
Propagator: make sure every network job has a parent
This could make sure that the network job gets deleted if the parent job gets
deleted, and would avoid crashes like:
Crash: EXCEPTION_ACCESS_VIOLATION_READ at 0xffffffff8b008a04
File "qiodevice.cpp", line 1617, in QIODevice::errorString
File "propagatedownload.cpp", line 264, in OCC::GETFileJob::slotReadyRead
File "moc_propagatedownload.cpp", line 85, in OCC::GETFileJob::qt_static_metacall
File "qobject.cpp", line 3716, in QMetaObject::activate
File "moc_qiodevice.cpp", line 154, in QIODevice::readyRead
File "qnetworkreplyhttpimpl.cpp", line 1045, in QNetworkReplyHttpImplPrivate::replyDownloadData
(#5329)
2016-11-29 15:20:19 +00:00
& _tmpFile , headers , expectedEtagForResume , _resumeStart , this ) ;
2014-06-03 09:50:13 +00:00
} else {
// We were provided a direct URL, use that one
2017-03-30 11:46:20 +00:00
qCInfo ( lcPropagateDownload ) < < " directDownloadUrl given for " < < _item - > _file < < _item - > _directDownloadUrl ;
2014-10-08 10:04:17 +00:00
2015-04-15 13:19:11 +00:00
if ( ! _item - > _directDownloadCookies . isEmpty ( ) ) {
headers [ " Cookie " ] = _item - > _directDownloadCookies . toUtf8 ( ) ;
2014-06-03 09:50:13 +00:00
}
2014-10-08 10:04:17 +00:00
2015-04-15 13:19:11 +00:00
QUrl url = QUrl : : fromUserInput ( _item - > _directDownloadUrl ) ;
2017-01-17 13:29:12 +00:00
_job = new GETFileJob ( propagator ( ) - > account ( ) ,
2014-06-03 09:50:13 +00:00
url ,
Propagator: make sure every network job has a parent
This could make sure that the network job gets deleted if the parent job gets
deleted, and would avoid crashes like:
Crash: EXCEPTION_ACCESS_VIOLATION_READ at 0xffffffff8b008a04
File "qiodevice.cpp", line 1617, in QIODevice::errorString
File "propagatedownload.cpp", line 264, in OCC::GETFileJob::slotReadyRead
File "moc_propagatedownload.cpp", line 85, in OCC::GETFileJob::qt_static_metacall
File "qobject.cpp", line 3716, in QMetaObject::activate
File "moc_qiodevice.cpp", line 154, in QIODevice::readyRead
File "qnetworkreplyhttpimpl.cpp", line 1045, in QNetworkReplyHttpImplPrivate::replyDownloadData
(#5329)
2016-11-29 15:20:19 +00:00
& _tmpFile , headers , expectedEtagForResume , _resumeStart , this ) ;
2014-06-03 09:50:13 +00:00
}
2017-01-17 13:29:12 +00:00
_job - > setBandwidthManager ( & propagator ( ) - > _bandwidthManager ) ;
2017-09-20 08:14:48 +00:00
connect ( _job . data ( ) , & GETFileJob : : finishedSignal , this , & PropagateDownloadFile : : slotGetFinished ) ;
connect ( _job . data ( ) , & GETFileJob : : downloadProgress , this , & PropagateDownloadFile : : slotDownloadProgress ) ;
2017-01-17 13:29:12 +00:00
propagator ( ) - > _activeJobList . append ( this ) ;
2014-02-17 12:48:56 +00:00
_job - > start ( ) ;
}
2016-05-18 14:42:55 +00:00
qint64 PropagateDownloadFile : : committedDiskSpace ( ) const
2015-10-01 09:39:09 +00:00
{
if ( _state = = Running ) {
2019-02-13 09:15:33 +00:00
return qBound ( 0LL , _item - > _size - _resumeStart - _downloadProgress , _item - > _size ) ;
2015-10-01 09:39:09 +00:00
}
return 0 ;
}
2016-05-18 14:42:55 +00:00
void PropagateDownloadFile : : setDeleteExistingFolder ( bool enabled )
2016-01-06 09:01:22 +00:00
{
_deleteExisting = enabled ;
}
2015-03-17 22:31:30 +00:00
const char owncloudCustomSoftErrorStringC [ ] = " owncloud-custom-soft-error-string " ;
2016-05-18 14:42:55 +00:00
void PropagateDownloadFile : : slotGetFinished ( )
2014-02-17 12:48:56 +00:00
{
2017-01-17 13:29:12 +00:00
propagator ( ) - > _activeJobList . removeOne ( this ) ;
2014-02-17 12:48:56 +00:00
2017-12-02 10:40:43 +00:00
GETFileJob * job = _job ;
2017-02-07 12:52:15 +00:00
ASSERT ( job ) ;
2014-02-17 12:48:56 +00:00
2018-04-04 14:27:08 +00:00
_item - > _httpErrorCode = job - > reply ( ) - > attribute ( QNetworkRequest : : HttpStatusCodeAttribute ) . toInt ( ) ;
_item - > _requestId = job - > requestId ( ) ;
2014-02-17 12:48:56 +00:00
QNetworkReply : : NetworkError err = job - > reply ( ) - > error ( ) ;
if ( err ! = QNetworkReply : : NoError ) {
2014-10-08 10:04:17 +00:00
// If we sent a 'Range' header and get 416 back, we want to retry
// without the header.
2015-06-05 09:12:21 +00:00
const bool badRangeHeader = job - > resumeStart ( ) > 0 & & _item - > _httpErrorCode = = 416 ;
2014-10-08 10:04:17 +00:00
if ( badRangeHeader ) {
2017-03-30 11:46:20 +00:00
qCWarning ( lcPropagateDownload ) < < " server replied 416 to our range request, trying again without " ;
2017-01-17 13:29:12 +00:00
propagator ( ) - > _anotherSyncNeeded = true ;
2014-10-08 10:04:17 +00:00
}
2015-06-05 09:12:21 +00:00
// Getting a 404 probably means that the file was deleted on the server.
const bool fileNotFound = _item - > _httpErrorCode = = 404 ;
if ( fileNotFound ) {
2017-03-30 11:46:20 +00:00
qCWarning ( lcPropagateDownload ) < < " server replied 404, assuming file was deleted " ;
2015-06-05 09:12:21 +00:00
}
2019-06-03 15:50:16 +00:00
// Getting a 423 means that the file is locked
const bool fileLocked = _item - > _httpErrorCode = = 423 ;
if ( fileLocked ) {
qCWarning ( lcPropagateDownload ) < < " server replied 423, file is Locked " ;
}
2014-10-08 10:04:17 +00:00
// Don't keep the temporary file if it is empty or we
2015-06-05 09:12:21 +00:00
// used a bad range header or the file's not on the server anymore.
2018-08-15 08:46:16 +00:00
if ( _tmpFile . exists ( ) & & ( _tmpFile . size ( ) = = 0 | | badRangeHeader | | fileNotFound ) ) {
2014-02-17 12:48:56 +00:00
_tmpFile . close ( ) ;
2016-01-05 10:58:18 +00:00
FileSystem : : remove ( _tmpFile . fileName ( ) ) ;
2017-01-17 13:29:12 +00:00
propagator ( ) - > _journal - > setDownloadInfo ( _item - > _file , SyncJournalDb : : DownloadInfo ( ) ) ;
2014-02-17 12:48:56 +00:00
}
2014-10-08 10:04:17 +00:00
2015-04-15 13:19:11 +00:00
if ( ! _item - > _directDownloadUrl . isEmpty ( ) & & err ! = QNetworkReply : : OperationCanceledError ) {
2014-12-17 13:28:13 +00:00
// If this was with a direct download, retry without direct download
2017-05-09 12:24:11 +00:00
qCWarning ( lcPropagateDownload ) < < " Direct download of " < < _item - > _directDownloadUrl < < " failed. Retrying through owncloud. " ;
2015-04-15 13:19:11 +00:00
_item - > _directDownloadUrl . clear ( ) ;
2014-12-17 13:28:13 +00:00
start ( ) ;
return ;
}
2015-03-17 22:31:30 +00:00
// This gives a custom QNAM (by the user of libowncloudsync) to abort() a QNetworkReply in its metaDataChanged() slot and
// set a custom error string to make this a soft error. In contrast to the default hard error this won't bring down
// the whole sync and allows for a custom error message.
QNetworkReply * reply = job - > reply ( ) ;
if ( err = = QNetworkReply : : OperationCanceledError & & reply - > property ( owncloudCustomSoftErrorStringC ) . isValid ( ) ) {
job - > setErrorString ( reply - > property ( owncloudCustomSoftErrorStringC ) . toString ( ) ) ;
job - > setErrorStatus ( SyncFileItem : : SoftError ) ;
2015-06-05 09:12:21 +00:00
} else if ( badRangeHeader ) {
// Can't do this in classifyError() because 416 without a
// Range header should result in NormalError.
job - > setErrorStatus ( SyncFileItem : : SoftError ) ;
} else if ( fileNotFound ) {
job - > setErrorString ( tr ( " File was deleted from server " ) ) ;
job - > setErrorStatus ( SyncFileItem : : SoftError ) ;
2017-05-29 10:05:22 +00:00
// As a precaution against bugs that cause our database and the
// reality on the server to diverge, rediscover this folder on the
// next sync run.
2019-02-13 13:18:54 +00:00
propagator ( ) - > _journal - > schedulePathForRemoteDiscovery ( _item - > _file ) ;
2015-03-17 22:31:30 +00:00
}
2018-06-20 13:46:58 +00:00
QByteArray errorBody ;
QString errorString = _item - > _httpErrorCode > = 400 ? job - > errorStringParsingBody ( & errorBody )
: job - > errorString ( ) ;
2014-04-22 10:34:03 +00:00
SyncFileItem : : Status status = job - > errorStatus ( ) ;
if ( status = = SyncFileItem : : NoStatus ) {
2015-09-30 13:34:50 +00:00
status = classifyError ( err , _item - > _httpErrorCode ,
2018-06-20 13:46:58 +00:00
& propagator ( ) - > _anotherSyncNeeded , errorBody ) ;
2014-04-22 10:34:03 +00:00
}
2015-06-05 09:12:21 +00:00
2018-06-20 13:46:58 +00:00
done ( status , errorString ) ;
2014-02-17 12:48:56 +00:00
return ;
}
2021-04-20 15:38:36 +00:00
_item - > _responseTimeStamp = job - > responseTimestamp ( ) ;
2014-06-03 09:50:13 +00:00
if ( ! job - > etag ( ) . isEmpty ( ) ) {
// The etag will be empty if we used a direct download URL.
// (If it was really empty by the server, the GETFileJob will have errored
2015-04-15 13:19:11 +00:00
_item - > _etag = parseEtag ( job - > etag ( ) ) ;
2014-06-03 09:50:13 +00:00
}
2015-02-06 09:20:10 +00:00
if ( job - > lastModified ( ) ) {
// It is possible that the file was modified on the server since we did the discovery phase
// so make sure we have the up-to-date time
2015-04-15 13:19:11 +00:00
_item - > _modtime = job - > lastModified ( ) ;
2021-12-06 14:07:57 +00:00
Q_ASSERT ( _item - > _modtime > 0 ) ;
2021-12-07 14:44:15 +00:00
if ( _item - > _modtime < = 0 ) {
qCWarning ( lcPropagateDownload ( ) ) < < " invalid modified time " < < _item - > _file < < _item - > _modtime ;
}
2015-02-06 09:20:10 +00:00
}
2014-03-26 16:58:32 +00:00
2014-02-17 12:48:56 +00:00
_tmpFile . close ( ) ;
_tmpFile . flush ( ) ;
2014-11-20 17:49:34 +00:00
/* Check that the size of the GET reply matches the file size. There have been cases
* reported that if a server breaks behind a proxy , the GET is still a 200 but is
* truncated , as described here : https : //github.com/owncloud/mirall/issues/2528
*/
const QByteArray sizeHeader ( " Content-Length " ) ;
2019-02-13 09:15:33 +00:00
qint64 bodySize = job - > reply ( ) - > rawHeader ( sizeHeader ) . toLongLong ( ) ;
2019-01-30 04:43:08 +00:00
bool hasSizeHeader = ! job - > reply ( ) - > rawHeader ( sizeHeader ) . isEmpty ( ) ;
2014-11-20 17:49:34 +00:00
2019-01-30 04:43:08 +00:00
// Qt removes the content-length header for transparently decompressed HTTP1 replies
// but not for HTTP2 or SPDY replies. For these it remains and contains the size
// of the compressed data. See QTBUG-73364.
const auto contentEncoding = job - > reply ( ) - > rawHeader ( " content-encoding " ) . toLower ( ) ;
if ( ( contentEncoding = = " gzip " | | contentEncoding = = " deflate " )
2022-08-04 16:17:34 +00:00
& & ( job - > reply ( ) - > attribute ( QNetworkRequest : : Http2WasUsedAttribute ) . toBool ( )
2019-01-30 04:43:08 +00:00
| | job - > reply ( ) - > attribute ( QNetworkRequest : : SpdyWasUsedAttribute ) . toBool ( ) ) ) {
bodySize = 0 ;
hasSizeHeader = false ;
}
if ( hasSizeHeader & & _tmpFile . size ( ) > 0 & & bodySize = = 0 ) {
2015-07-30 12:32:33 +00:00
// Strange bug with broken webserver or webfirewall https://github.com/owncloud/client/issues/3373#issuecomment-122672322
// This happened when trying to resume a file. The Content-Range header was files, Content-Length was == 0
2017-05-09 12:24:11 +00:00
qCDebug ( lcPropagateDownload ) < < bodySize < < _item - > _size < < _tmpFile . size ( ) < < job - > resumeStart ( ) ;
2016-01-05 10:58:18 +00:00
FileSystem : : remove ( _tmpFile . fileName ( ) ) ;
2015-10-26 11:31:07 +00:00
done ( SyncFileItem : : SoftError , QLatin1String ( " Broken webserver returning empty content length for non-empty file on resume " ) ) ;
2015-07-30 12:32:33 +00:00
return ;
}
2014-11-27 13:50:49 +00:00
if ( bodySize > 0 & & bodySize ! = _tmpFile . size ( ) - job - > resumeStart ( ) ) {
2017-05-09 12:24:11 +00:00
qCDebug ( lcPropagateDownload ) < < bodySize < < _tmpFile . size ( ) < < job - > resumeStart ( ) ;
2017-01-17 13:29:12 +00:00
propagator ( ) - > _anotherSyncNeeded = true ;
2014-11-20 17:49:34 +00:00
done ( SyncFileItem : : SoftError , tr ( " The file could not be downloaded completely. " ) ) ;
return ;
}
2016-04-29 06:49:27 +00:00
if ( _tmpFile . size ( ) = = 0 & & _item - > _size > 0 ) {
FileSystem : : remove ( _tmpFile . fileName ( ) ) ;
done ( SyncFileItem : : NormalError ,
2021-05-27 04:43:18 +00:00
tr ( " The downloaded file is empty, but the server said it should have been %1. " )
2016-04-29 06:49:27 +00:00
. arg ( Utility : : octetsToString ( _item - > _size ) ) ) ;
return ;
}
2017-12-02 10:40:43 +00:00
// Did the file come with conflict headers? If so, store them now!
// If we download conflict files but the server doesn't send conflict
// headers, the record will be established by SyncEngine::conflictRecordMaintenance.
// (we can't reliably determine the file id of the base file here,
// it might still be downloaded in a parallel job and not exist in
// the database yet!)
if ( job - > reply ( ) - > rawHeader ( " OC-Conflict " ) = = " 1 " ) {
_conflictRecord . path = _item - > _file . toUtf8 ( ) ;
2018-08-21 09:24:45 +00:00
_conflictRecord . initialBasePath = job - > reply ( ) - > rawHeader ( " OC-ConflictInitialBasePath " ) ;
2017-12-02 10:40:43 +00:00
_conflictRecord . baseFileId = job - > reply ( ) - > rawHeader ( " OC-ConflictBaseFileId " ) ;
2018-08-14 11:35:04 +00:00
_conflictRecord . baseEtag = job - > reply ( ) - > rawHeader ( " OC-ConflictBaseEtag " ) ;
2017-12-02 10:40:43 +00:00
2018-08-14 11:35:04 +00:00
auto mtimeHeader = job - > reply ( ) - > rawHeader ( " OC-ConflictBaseMtime " ) ;
2017-12-02 10:40:43 +00:00
if ( ! mtimeHeader . isEmpty ( ) )
_conflictRecord . baseModtime = mtimeHeader . toLongLong ( ) ;
// We don't set it yet. That will only be done when the download finished
// successfully, much further down. Here we just grab the headers because the
// job will be deleted later.
}
2015-05-20 20:44:12 +00:00
// Do checksum validation for the download. If there is no checksum header, the validator
2016-03-02 13:20:36 +00:00
// will also emit the validated() signal to continue the flow in slot transmissionChecksumValidated()
2015-05-20 20:44:12 +00:00
// as this is (still) also correct.
2020-05-18 18:54:23 +00:00
auto * validator = new ValidateChecksumHeader ( this ) ;
2017-09-20 08:14:48 +00:00
connect ( validator , & ValidateChecksumHeader : : validated ,
this , & PropagateDownloadFile : : transmissionChecksumValidated ) ;
connect ( validator , & ValidateChecksumHeader : : validationFailed ,
this , & PropagateDownloadFile : : slotChecksumFail ) ;
2017-11-20 07:18:52 +00:00
auto checksumHeader = findBestChecksum ( job - > reply ( ) - > rawHeader ( checkSumHeaderC ) ) ;
2017-10-10 11:24:41 +00:00
auto contentMd5Header = job - > reply ( ) - > rawHeader ( contentMd5HeaderC ) ;
if ( checksumHeader . isEmpty ( ) & & ! contentMd5Header . isEmpty ( ) )
checksumHeader = " MD5: " + contentMd5Header ;
2015-10-15 07:39:49 +00:00
validator - > start ( _tmpFile . fileName ( ) , checksumHeader ) ;
2015-05-15 08:50:55 +00:00
}
2021-12-20 12:59:08 +00:00
void PropagateDownloadFile : : slotChecksumFail ( const QString & errMsg ,
2021-12-23 10:07:02 +00:00
const QByteArray & calculatedChecksumType , const QByteArray & calculatedChecksum , const ValidateChecksumHeader : : FailureReason reason )
2021-12-20 09:13:48 +00:00
{
2021-12-23 10:07:02 +00:00
if ( reason = = ValidateChecksumHeader : : FailureReason : : ChecksumMismatch & & propagator ( ) - > account ( ) - > isChecksumRecalculateRequestSupported ( ) ) {
2021-12-20 12:59:08 +00:00
const QByteArray calculatedChecksumHeader ( calculatedChecksumType + ' : ' + calculatedChecksum ) ;
2021-12-20 09:13:48 +00:00
const QString fullRemotePathForFile ( propagator ( ) - > fullRemotePath ( _isEncrypted ? _item - > _encryptedFileName : _item - > _file ) ) ;
2021-12-20 12:59:08 +00:00
auto * job = new SimpleFileJob ( propagator ( ) - > account ( ) , fullRemotePathForFile ) ;
2021-12-23 10:07:02 +00:00
QObject : : connect ( job , & SimpleFileJob : : finishedSignal , this ,
[ this , calculatedChecksumHeader , errMsg ] ( const QNetworkReply * reply ) { processChecksumRecalculate ( reply , calculatedChecksumHeader , errMsg ) ;
2021-12-20 09:13:48 +00:00
} ) ;
qCWarning ( lcPropagateDownload ) < < " Checksum validation has failed for file: " < < fullRemotePathForFile
< < " Requesting checksum recalculation on the server... " ;
QNetworkRequest req ;
2021-12-23 10:07:02 +00:00
req . setRawHeader ( checksumRecalculateOnServerHeaderC , calculatedChecksumType ) ;
2021-12-20 09:13:48 +00:00
job - > startRequest ( QByteArrayLiteral ( " PATCH " ) , req ) ;
return ;
}
2021-12-23 10:07:02 +00:00
checksumValidateFailedAbortDownload ( errMsg ) ;
}
void PropagateDownloadFile : : processChecksumRecalculate ( const QNetworkReply * reply , const QByteArray & originalChecksumHeader , const QString & errorMessage )
{
if ( reply - > error ( ) ! = QNetworkReply : : NoError ) {
qCCritical ( lcPropagateDownload ) < < " Checksum recalculation has failed for file: " < < reply - > url ( )
< < " Recalculation request has finished with error: " < < reply - > errorString ( ) ;
checksumValidateFailedAbortDownload ( errorMessage ) ;
return ;
}
const auto newChecksumHeaderFromServer = reply - > rawHeader ( checkSumHeaderC ) ;
if ( newChecksumHeaderFromServer = = originalChecksumHeader ) {
const auto newChecksumHeaderFromServerSplit = newChecksumHeaderFromServer . split ( ' : ' ) ;
if ( newChecksumHeaderFromServerSplit . size ( ) > 1 ) {
transmissionChecksumValidated ( newChecksumHeaderFromServerSplit . first ( ) , newChecksumHeaderFromServerSplit . last ( ) ) ;
return ;
}
}
qCCritical ( lcPropagateDownload ) < < " Checksum recalculation has failed for file: " < < reply - > url ( ) < < " "
< < checkSumHeaderC < < " received is: " < < newChecksumHeaderFromServer ;
checksumValidateFailedAbortDownload ( errorMessage ) ;
}
void PropagateDownloadFile : : checksumValidateFailedAbortDownload ( const QString & errMsg )
{
FileSystem : : remove ( _tmpFile . fileName ( ) ) ;
propagator ( ) - > _anotherSyncNeeded = true ;
done ( SyncFileItem : : SoftError , errMsg ) ; // tr("The file downloaded with a broken checksum, will be redownloaded."));
2014-02-17 12:48:56 +00:00
}
2016-05-18 14:42:55 +00:00
void PropagateDownloadFile : : deleteExistingFolder ( )
2016-01-06 09:01:22 +00:00
{
2020-09-22 09:47:40 +00:00
QString existingDir = propagator ( ) - > fullLocalPath ( _item - > _file ) ;
2016-01-06 09:01:22 +00:00
if ( ! QFileInfo ( existingDir ) . isDir ( ) ) {
return ;
}
// Delete the directory if it is empty!
QDir dir ( existingDir ) ;
if ( dir . entryList ( QDir : : NoDotAndDotDot | QDir : : AllEntries ) . count ( ) = = 0 ) {
if ( dir . rmdir ( existingDir ) ) {
return ;
}
// on error, just try to move it away...
}
2018-01-17 09:59:47 +00:00
QString error ;
if ( ! propagator ( ) - > createConflict ( _item , _associatedComposite , & error ) ) {
done ( SyncFileItem : : NormalError , error ) ;
2016-01-06 09:01:22 +00:00
}
}
2015-05-21 14:23:54 +00:00
namespace { // Anonymous namespace for the recall feature
2015-05-15 13:45:50 +00:00
static QString makeRecallFileName ( const QString & fn )
2015-04-09 13:25:34 +00:00
{
QString recallFileName ( fn ) ;
2015-10-05 03:20:09 +00:00
// Add _recall-XXXX before the extension.
2015-04-09 13:25:34 +00:00
int dotLocation = recallFileName . lastIndexOf ( ' . ' ) ;
2015-10-05 03:20:09 +00:00
// If no extension, add it at the end (take care of cases like foo/.hidden or foo.bar/file)
2015-04-09 13:25:34 +00:00
if ( dotLocation < = recallFileName . lastIndexOf ( ' / ' ) + 1 ) {
dotLocation = recallFileName . size ( ) ;
}
2017-09-25 09:49:11 +00:00
QString timeString = QDateTime : : currentDateTimeUtc ( ) . toString ( " yyyyMMdd-hhmmss " ) ;
2015-05-15 13:45:50 +00:00
recallFileName . insert ( dotLocation , " _.sys.admin#recall#- " + timeString ) ;
2015-04-09 13:25:34 +00:00
return recallFileName ;
}
2016-09-14 13:42:19 +00:00
void handleRecallFile ( const QString & filePath , const QString & folderPath , SyncJournalDb & journal )
2015-05-21 14:23:54 +00:00
{
2017-05-09 12:24:11 +00:00
qCDebug ( lcPropagateDownload ) < < " handleRecallFile: " < < filePath ;
2015-05-21 14:23:54 +00:00
2016-09-14 13:42:19 +00:00
FileSystem : : setFileHidden ( filePath , true ) ;
2015-05-21 14:23:54 +00:00
2016-09-14 13:42:19 +00:00
QFile file ( filePath ) ;
2015-05-21 14:23:54 +00:00
if ( ! file . open ( QIODevice : : ReadOnly ) ) {
2017-05-09 12:24:11 +00:00
qCWarning ( lcPropagateDownload ) < < " Could not open recall file " < < file . errorString ( ) ;
2015-05-21 14:23:54 +00:00
return ;
}
2016-09-14 13:42:19 +00:00
QFileInfo existingFile ( filePath ) ;
QDir baseDir = existingFile . dir ( ) ;
2015-05-21 14:23:54 +00:00
while ( ! file . atEnd ( ) ) {
QByteArray line = file . readLine ( ) ;
line . chop ( 1 ) ; // remove trailing \n
2016-09-14 13:42:19 +00:00
QString recalledFile = QDir : : cleanPath ( baseDir . filePath ( line ) ) ;
if ( ! recalledFile . startsWith ( folderPath ) | | ! recalledFile . startsWith ( baseDir . path ( ) ) ) {
2017-03-30 11:46:20 +00:00
qCWarning ( lcPropagateDownload ) < < " Ignoring recall of " < < recalledFile ;
2016-09-14 13:42:19 +00:00
continue ;
}
// Path of the recalled file in the local folder
QString localRecalledFile = recalledFile . mid ( folderPath . size ( ) ) ;
2017-09-13 17:02:38 +00:00
SyncJournalFileRecord record ;
if ( ! journal . getFileRecord ( localRecalledFile , & record ) | | ! record . isValid ( ) ) {
2017-03-30 11:46:20 +00:00
qCWarning ( lcPropagateDownload ) < < " No db entry for recall of " < < localRecalledFile ;
2016-09-14 13:42:19 +00:00
continue ;
}
2017-06-14 10:14:46 +00:00
qCInfo ( lcPropagateDownload ) < < " Recalling " < < localRecalledFile < < " Checksum: " < < record . _checksumHeader ;
2016-09-14 13:42:19 +00:00
QString targetPath = makeRecallFileName ( recalledFile ) ;
2017-05-09 12:24:11 +00:00
qCDebug ( lcPropagateDownload ) < < " Copy recall file: " < < recalledFile < < " -> " < < targetPath ;
2016-09-06 08:42:59 +00:00
// Remove the target first, QFile::copy will not overwrite it.
2016-09-14 13:42:19 +00:00
FileSystem : : remove ( targetPath ) ;
QFile : : copy ( recalledFile , targetPath ) ;
2017-05-17 08:55:42 +00:00
}
2015-05-21 14:23:54 +00:00
}
2016-03-03 10:50:06 +00:00
static void preserveGroupOwnership ( const QString & fileName , const QFileInfo & fi )
{
# ifdef Q_OS_UNIX
2019-09-09 16:06:29 +00:00
int chownErr = chown ( fileName . toLocal8Bit ( ) . constData ( ) , - 1 , fi . groupId ( ) ) ;
if ( chownErr ) {
2019-09-16 18:26:18 +00:00
// TODO: Consider further error handling!
qCWarning ( lcPropagateDownload ) < < QString ( " preserveGroupOwnership: chown error %1: setting group %2 failed on file %3 " ) . arg ( chownErr ) . arg ( fi . groupId ( ) ) . arg ( fileName ) ;
2019-09-09 16:06:29 +00:00
}
2016-03-03 10:50:06 +00:00
# else
Q_UNUSED ( fileName ) ;
Q_UNUSED ( fi ) ;
# endif
}
2015-05-21 14:23:54 +00:00
} // end namespace
2016-05-18 14:42:55 +00:00
void PropagateDownloadFile : : transmissionChecksumValidated ( const QByteArray & checksumType , const QByteArray & checksum )
2014-02-17 12:48:56 +00:00
{
2020-07-24 15:16:28 +00:00
const QByteArray theContentChecksumType = propagator ( ) - > account ( ) - > capabilities ( ) . preferredUploadChecksumType ( ) ;
2016-03-02 13:20:36 +00:00
// Reuse transmission checksum as content checksum.
//
// We could do this more aggressively and accept both MD5 and SHA1
// instead of insisting on the exactly correct checksum type.
if ( theContentChecksumType = = checksumType | | theContentChecksumType . isEmpty ( ) ) {
return contentChecksumComputed ( checksumType , checksum ) ;
}
2021-07-27 12:02:12 +00:00
// Compute the content checksum.
auto computeChecksum = new ComputeChecksum ( this ) ;
computeChecksum - > setChecksumType ( theContentChecksumType ) ;
2016-03-02 13:20:36 +00:00
2021-07-27 12:02:12 +00:00
connect ( computeChecksum , & ComputeChecksum : : done ,
this , & PropagateDownloadFile : : contentChecksumComputed ) ;
computeChecksum - > start ( _tmpFile . fileName ( ) ) ;
2016-03-02 13:20:36 +00:00
}
2016-02-25 16:17:14 +00:00
2016-05-18 14:42:55 +00:00
void PropagateDownloadFile : : contentChecksumComputed ( const QByteArray & checksumType , const QByteArray & checksum )
2016-03-02 13:20:36 +00:00
{
2017-06-14 10:14:46 +00:00
_item - > _checksumHeader = makeChecksumHeader ( checksumType , checksum ) ;
2016-03-02 13:20:36 +00:00
2022-12-20 18:49:35 +00:00
const auto localFilePath = propagator ( ) - > fullLocalPath ( _item - > _file ) ;
SyncJournalFileRecord record ;
if ( _item - > _instruction ! = CSYNC_INSTRUCTION_CONFLICT & & FileSystem : : fileExists ( localFilePath )
& & ( propagator ( ) - > _journal - > getFileRecord ( _item - > _file , & record ) & & record . isValid ( ) )
& & ( record . _modtime = = _item - > _modtime & & record . _etag ! = _item - > _etag ) ) {
const auto computeChecksum = new ComputeChecksum ( this ) ;
computeChecksum - > setChecksumType ( checksumType ) ;
connect ( computeChecksum , & ComputeChecksum : : done , this , & PropagateDownloadFile : : localFileContentChecksumComputed ) ;
computeChecksum - > start ( localFilePath ) ;
return ;
}
finalizeDownload ( ) ;
}
void PropagateDownloadFile : : localFileContentChecksumComputed ( const QByteArray & checksumType , const QByteArray & checksum )
{
if ( _item - > _checksumHeader = = makeChecksumHeader ( checksumType , checksum ) ) {
FileSystem : : remove ( _tmpFile . fileName ( ) ) ;
updateMetadata ( false ) ;
return ;
}
finalizeDownload ( ) ;
}
void PropagateDownloadFile : : finalizeDownload ( )
{
2018-01-05 20:37:31 +00:00
if ( _isEncrypted ) {
2018-01-21 20:25:06 +00:00
if ( _downloadEncryptedHelper - > decryptFile ( _tmpFile ) ) {
2022-12-20 18:49:35 +00:00
downloadFinished ( ) ;
2018-01-21 20:25:06 +00:00
} else {
2022-12-20 18:49:35 +00:00
done ( SyncFileItem : : NormalError , _downloadEncryptedHelper - > errorString ( ) ) ;
2018-01-21 20:25:06 +00:00
}
2018-01-05 20:37:31 +00:00
} else {
downloadFinished ( ) ;
}
2016-03-02 13:20:36 +00:00
}
2016-05-18 14:42:55 +00:00
void PropagateDownloadFile : : downloadFinished ( )
2016-03-02 13:20:36 +00:00
{
2020-09-22 09:47:40 +00:00
ASSERT ( ! _tmpFile . isOpen ( ) ) ;
QString fn = propagator ( ) - > fullLocalPath ( _item - > _file ) ;
2014-02-17 12:48:56 +00:00
2014-10-29 10:23:44 +00:00
// In case of file name clash, report an error
// This can happen if another parallel download saved a clashing file.
2017-01-17 13:29:12 +00:00
if ( propagator ( ) - > localFileNameClash ( _item - > _file ) ) {
2022-09-21 17:13:54 +00:00
done ( SyncFileItem : : FileNameClash , tr ( " File %1 cannot be saved because of a local file name clash! " ) . arg ( QDir : : toNativeSeparators ( _item - > _file ) ) ) ;
2014-10-29 10:23:44 +00:00
return ;
}
2014-02-17 12:48:56 +00:00
2021-12-07 10:33:43 +00:00
if ( _item - > _modtime < = 0 ) {
2021-12-08 17:33:56 +00:00
FileSystem : : remove ( _tmpFile . fileName ( ) ) ;
2021-12-07 10:33:43 +00:00
done ( SyncFileItem : : NormalError , tr ( " File %1 has invalid modified time reported by server. Do not save it. " ) . arg ( QDir : : toNativeSeparators ( _item - > _file ) ) ) ;
return ;
}
2021-12-06 14:07:57 +00:00
Q_ASSERT ( _item - > _modtime > 0 ) ;
2021-12-07 14:44:15 +00:00
if ( _item - > _modtime < = 0 ) {
qCWarning ( lcPropagateDownload ( ) ) < < " invalid modified time " < < _item - > _file < < _item - > _modtime ;
}
2015-05-12 08:35:28 +00:00
FileSystem : : setModTime ( _tmpFile . fileName ( ) , _item - > _modtime ) ;
2015-10-05 03:20:09 +00:00
// We need to fetch the time again because some file systems such as FAT have worse than a second
2015-04-15 13:31:47 +00:00
// Accuracy, and we really need the time from the file system. (#3103)
2015-04-15 13:19:11 +00:00
_item - > _modtime = FileSystem : : getModTime ( _tmpFile . fileName ( ) ) ;
2021-12-07 10:33:43 +00:00
if ( _item - > _modtime < = 0 ) {
2021-12-08 17:33:56 +00:00
FileSystem : : remove ( _tmpFile . fileName ( ) ) ;
2021-12-07 10:33:43 +00:00
done ( SyncFileItem : : NormalError , tr ( " File %1 has invalid modified time reported by server. Do not save it. " ) . arg ( QDir : : toNativeSeparators ( _item - > _file ) ) ) ;
return ;
}
2021-12-06 14:07:57 +00:00
Q_ASSERT ( _item - > _modtime > 0 ) ;
2021-12-07 14:44:15 +00:00
if ( _item - > _modtime < = 0 ) {
qCWarning ( lcPropagateDownload ( ) ) < < " invalid modified time " < < _item - > _file < < _item - > _modtime ;
}
2014-02-17 12:48:56 +00:00
2019-02-06 14:12:57 +00:00
bool previousFileExists = FileSystem : : fileExists ( fn ) ;
if ( previousFileExists ) {
2015-05-07 09:41:29 +00:00
// Preserve the existing file permissions.
QFileInfo existingFile ( fn ) ;
if ( existingFile . permissions ( ) ! = _tmpFile . permissions ( ) ) {
_tmpFile . setPermissions ( existingFile . permissions ( ) ) ;
}
2016-03-03 10:50:06 +00:00
preserveGroupOwnership ( _tmpFile . fileName ( ) , existingFile ) ;
2015-05-07 09:41:29 +00:00
2019-02-05 10:42:15 +00:00
// Make the file a hydrated placeholder if possible
2020-12-30 11:53:03 +00:00
const auto result = propagator ( ) - > syncOptions ( ) . _vfs - > convertToPlaceholder ( _tmpFile . fileName ( ) , * _item , fn ) ;
if ( ! result ) {
done ( SyncFileItem : : NormalError , result . error ( ) ) ;
return ;
}
2015-05-07 09:41:29 +00:00
}
2015-10-29 13:10:11 +00:00
// Apply the remote permissions
2017-09-19 08:53:51 +00:00
FileSystem : : setFileReadOnlyWeak ( _tmpFile . fileName ( ) , ! _item - > _remotePerm . isNull ( ) & & ! _item - > _remotePerm . hasPermission ( RemotePermissions : : CanWrite ) ) ;
2015-10-29 13:10:11 +00:00
2019-02-05 10:42:15 +00:00
bool isConflict = _item - > _instruction = = CSYNC_INSTRUCTION_CONFLICT
& & ( QFileInfo ( fn ) . isDir ( ) | | ! FileSystem : : fileEquals ( fn , _tmpFile . fileName ( ) ) ) ;
if ( isConflict ) {
QString error ;
if ( ! propagator ( ) - > createConflict ( _item , _associatedComposite , & error ) ) {
done ( SyncFileItem : : SoftError , error ) ;
return ;
}
2019-02-06 14:12:57 +00:00
previousFileExists = false ;
}
2021-01-04 15:23:47 +00:00
const auto vfs = propagator ( ) - > syncOptions ( ) . _vfs ;
// In the case of an hydration, this size is likely to change for placeholders
// (except with the cfapi backend)
const auto isVirtualDownload = _item - > _type = = ItemTypeVirtualFileDownload ;
const auto isCfApiVfs = vfs & & vfs - > mode ( ) = = Vfs : : WindowsCfApi ;
if ( previousFileExists & & ( isCfApiVfs | | ! isVirtualDownload ) ) {
2019-02-06 14:12:57 +00:00
// Check whether the existing file has changed since the discovery
// phase by comparing size and mtime to the previous values. This
// is necessary to avoid overwriting user changes that happened between
// the discovery phase and now.
const qint64 expectedSize = _item - > _previousSize ;
const time_t expectedMtime = _item - > _previousModtime ;
if ( ! FileSystem : : verifyFileUnchanged ( fn , expectedSize , expectedMtime ) ) {
propagator ( ) - > _anotherSyncNeeded = true ;
done ( SyncFileItem : : SoftError , tr ( " File has changed since discovery " ) ) ;
return ;
}
2019-02-05 10:42:15 +00:00
}
2019-01-25 10:11:28 +00:00
2014-02-18 13:05:29 +00:00
QString error ;
2017-01-17 13:29:12 +00:00
emit propagator ( ) - > touchedFile ( fn ) ;
2015-05-07 09:41:29 +00:00
// The fileChanged() check is done above to generate better error messages.
if ( ! FileSystem : : uncheckedRenameReplace ( _tmpFile . fileName ( ) , fn , & error ) ) {
2017-03-30 11:46:20 +00:00
qCWarning ( lcPropagateDownload ) < < QString ( " Rename failed: %1 => %2 " ) . arg ( _tmpFile . fileName ( ) ) . arg ( fn ) ;
2016-04-29 14:14:18 +00:00
// If the file is locked, we want to retry this sync when it
// becomes available again, otherwise try again directly
if ( FileSystem : : isFileLocked ( fn ) ) {
2017-01-17 13:29:12 +00:00
emit propagator ( ) - > seenLockedFile ( fn ) ;
2016-04-29 14:14:18 +00:00
} else {
2017-01-17 13:29:12 +00:00
propagator ( ) - > _anotherSyncNeeded = true ;
2016-04-29 14:14:18 +00:00
}
2015-01-15 19:49:52 +00:00
done ( SyncFileItem : : SoftError , error ) ;
2014-02-17 12:48:56 +00:00
return ;
}
2018-08-15 08:46:16 +00:00
2022-04-08 08:23:50 +00:00
qCInfo ( lcPropagateDownload ( ) ) < < propagator ( ) - > account ( ) - > davUser ( ) < < propagator ( ) - > account ( ) - > davDisplayName ( ) < < propagator ( ) - > account ( ) - > displayName ( ) ;
if ( _item - > _locked = = SyncFileItem : : LockStatus : : LockedItem & & ( _item - > _lockOwnerType ! = SyncFileItem : : LockOwnerType : : UserLock | | _item - > _lockOwnerId ! = propagator ( ) - > account ( ) - > davUser ( ) ) ) {
qCInfo ( lcPropagateDownload ( ) ) < < " file is locked: making it read only " ;
FileSystem : : setFileReadOnly ( fn , true ) ;
}
2015-05-07 09:41:29 +00:00
FileSystem : : setFileHidden ( fn , false ) ;
2014-02-18 13:05:29 +00:00
2014-09-05 12:01:26 +00:00
// Maybe we downloaded a newer version of the file than we thought we would...
// Get up to date information for the journal.
2015-04-15 13:19:11 +00:00
_item - > _size = FileSystem : : getSize ( fn ) ;
2014-02-17 12:48:56 +00:00
2017-12-02 10:40:43 +00:00
// Maybe what we downloaded was a conflict file? If so, set a conflict record.
// (the data was prepared in slotGetFinished above)
if ( _conflictRecord . isValid ( ) )
propagator ( ) - > _journal - > setConflictRecord ( _conflictRecord ) ;
2019-07-24 13:25:02 +00:00
if ( vfs & & vfs - > mode ( ) = = Vfs : : WithSuffix ) {
2018-08-15 08:46:16 +00:00
// If the virtual file used to have a different name and db
2019-07-24 13:25:02 +00:00
// entry, remove it transfer its old pin state.
if ( _item - > _type = = ItemTypeVirtualFileDownload ) {
2018-08-15 08:46:16 +00:00
QString virtualFile = _item - > _file + vfs - > fileSuffix ( ) ;
2020-09-22 09:47:40 +00:00
auto fn = propagator ( ) - > fullLocalPath ( virtualFile ) ;
2018-08-15 08:46:16 +00:00
qCDebug ( lcPropagateDownload ) < < " Download of previous virtual file finished " < < fn ;
QFile : : remove ( fn ) ;
2022-08-04 13:47:39 +00:00
if ( ! propagator ( ) - > _journal - > deleteFileRecord ( virtualFile ) ) {
qCWarning ( lcPropagateDownload ) < < " could not delete file from local DB " < < virtualFile ;
2022-08-05 10:07:55 +00:00
done ( SyncFileItem : : NormalError , tr ( " Could not delete file record %1 from local DB " ) . arg ( virtualFile ) ) ;
return ;
2022-08-04 13:47:39 +00:00
}
2019-06-27 13:47:04 +00:00
// Move the pin state to the new location
2019-07-18 13:30:40 +00:00
auto pin = propagator ( ) - > _journal - > internalPinStates ( ) . rawForPath ( virtualFile . toUtf8 ( ) ) ;
2019-06-27 13:47:04 +00:00
if ( pin & & * pin ! = PinState : : Inherited ) {
2021-07-23 15:30:48 +00:00
if ( ! vfs - > setPinState ( _item - > _file , * pin ) ) {
qCWarning ( lcPropagateDownload ) < < " Could not set pin state of " < < _item - > _file ;
}
if ( ! vfs - > setPinState ( virtualFile , PinState : : Inherited ) ) {
qCWarning ( lcPropagateDownload ) < < " Could not set pin state of " < < virtualFile < < " to inherited " ;
}
2019-06-27 13:47:04 +00:00
}
2018-08-15 08:46:16 +00:00
}
2019-07-24 13:25:02 +00:00
// Ensure the pin state isn't contradictory
auto pin = vfs - > pinState ( _item - > _file ) ;
if ( pin & & * pin = = PinState : : OnlineOnly )
2021-07-23 15:30:48 +00:00
if ( ! vfs - > setPinState ( _item - > _file , PinState : : Unspecified ) ) {
qCWarning ( lcPropagateDownload ) < < " Could not set pin state of " < < _item - > _file < < " to unspecified " ;
}
2018-05-30 08:33:32 +00:00
}
2017-06-14 10:14:46 +00:00
updateMetadata ( isConflict ) ;
}
void PropagateDownloadFile : : updateMetadata ( bool isConflict )
{
2021-06-28 10:32:57 +00:00
const QString fn = propagator ( ) - > fullLocalPath ( _item - > _file ) ;
const auto result = propagator ( ) - > updateMetadata ( * _item ) ;
if ( ! result ) {
done ( SyncFileItem : : FatalError , tr ( " Error updating metadata: %1 " ) . arg ( result . error ( ) ) ) ;
return ;
} else if ( * result = = Vfs : : ConvertToPlaceholderResult : : Locked ) {
done ( SyncFileItem : : SoftError , tr ( " The file %1 is currently in use " ) . arg ( _item - > _file ) ) ;
2016-04-07 09:47:04 +00:00
return ;
}
2018-02-12 11:50:51 +00:00
2018-01-28 22:50:12 +00:00
if ( _isEncrypted ) {
2018-02-12 11:50:51 +00:00
propagator ( ) - > _journal - > setDownloadInfo ( _item - > _file , SyncJournalDb : : DownloadInfo ( ) ) ;
} else {
propagator ( ) - > _journal - > setDownloadInfo ( _item - > _encryptedFileName , SyncJournalDb : : DownloadInfo ( ) ) ;
2018-01-28 22:50:12 +00:00
}
2017-01-17 13:29:12 +00:00
propagator ( ) - > _journal - > commit ( " download file start2 " ) ;
2019-01-21 10:19:45 +00:00
2014-02-17 12:48:56 +00:00
done ( isConflict ? SyncFileItem : : Conflict : SyncFileItem : : Success ) ;
2015-04-09 13:25:34 +00:00
// handle the special recall file
2017-09-19 08:53:51 +00:00
if ( ! _item - > _remotePerm . hasPermission ( RemotePermissions : : IsShared )
2016-09-14 13:42:19 +00:00
& & ( _item - > _file = = QLatin1String ( " .sys.admin#recall# " )
2020-09-22 09:47:40 +00:00
| | _item - > _file . endsWith ( QLatin1String ( " /.sys.admin#recall# " ) ) ) ) {
handleRecallFile ( fn , propagator ( ) - > localPath ( ) , * propagator ( ) - > _journal ) ;
2015-04-09 13:25:34 +00:00
}
2016-02-25 16:40:24 +00:00
qint64 duration = _stopwatch . elapsed ( ) ;
if ( isLikelyFinishedQuickly ( ) & & duration > 5 * 1000 ) {
2017-03-30 11:46:20 +00:00
qCWarning ( lcPropagateDownload ) < < " WARNING: Unexpectedly slow connection, took " < < duration < < " msec for " < < _item - > _size - _resumeStart < < " bytes for " < < _item - > _file ;
2016-02-25 16:40:24 +00:00
}
2014-02-17 12:48:56 +00:00
}
2016-05-18 14:42:55 +00:00
void PropagateDownloadFile : : slotDownloadProgress ( qint64 received , qint64 )
2014-03-14 12:03:16 +00:00
{
2014-08-29 17:23:08 +00:00
if ( ! _job )
return ;
2015-10-01 09:39:09 +00:00
_downloadProgress = received ;
2017-02-14 11:46:44 +00:00
propagator ( ) - > reportProgress ( * _item , _resumeStart + received ) ;
2014-03-14 12:03:16 +00:00
}
2017-08-10 22:03:03 +00:00
void PropagateDownloadFile : : abort ( PropagatorJob : : AbortType abortType )
2014-02-17 12:48:56 +00:00
{
if ( _job & & _job - > reply ( ) )
_job - > reply ( ) - > abort ( ) ;
2017-08-10 22:03:03 +00:00
if ( abortType = = AbortType : : Asynchronous ) {
emit abortFinished ( ) ;
}
2014-02-17 12:48:56 +00:00
}
2014-02-06 10:50:16 +00:00
}