From 22c35c4d15d31ab6fca3dfe4949ae807182bc481 Mon Sep 17 00:00:00 2001 From: Markus Goetz Date: Fri, 8 May 2015 16:41:41 +0200 Subject: [PATCH] Propagator: Limit length of temporary file name #2789 --- src/libsync/propagatedownload.cpp | 23 +++++++++++++++++------ test/testowncloudpropagator.h | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/libsync/propagatedownload.cpp b/src/libsync/propagatedownload.cpp index 9e2f5fe07..98a966e00 100644 --- a/src/libsync/propagatedownload.cpp +++ b/src/libsync/propagatedownload.cpp @@ -30,6 +30,22 @@ namespace OCC { + +QString createDownloadTmpFileName(const QString &previous) { + QString tmpFileName = previous; + //add a dot at the begining of the filename to hide the file on OS X and Linux + int slashPos = tmpFileName.lastIndexOf('/'); + tmpFileName.insert(slashPos+1, '.'); + //add the suffix + tmpFileName += ".~" + QString::number(uint(qrand()), 16); + + if (tmpFileName.length() > 254) { // https://github.com/owncloud/client/issues/2789 + tmpFileName = tmpFileName.left(100) + "_" + tmpFileName.right(153); + } + + return tmpFileName; +} + // DOES NOT take owncership of the device. GETFileJob::GETFileJob(AccountPtr account, const QString& path, QFile *device, const QMap &headers, const QByteArray &expectedEtagForResume, @@ -307,12 +323,7 @@ void PropagateDownloadFileQNAM::start() } if (tmpFileName.isEmpty()) { - tmpFileName = _item._file; - //add a dot at the begining of the filename to hide the file. - int slashPos = tmpFileName.lastIndexOf('/'); - tmpFileName.insert(slashPos+1, '.'); - //add the suffix - tmpFileName += ".~" + QString::number(uint(qrand()), 16); + tmpFileName = createDownloadTmpFileName(_item._file); } _tmpFile.setFileName(_propagator->getFilePath(tmpFileName)); diff --git a/test/testowncloudpropagator.h b/test/testowncloudpropagator.h index bbbd26350..71e3a0d3f 100644 --- a/test/testowncloudpropagator.h +++ b/test/testowncloudpropagator.h @@ -8,7 +8,14 @@ #define MIRALL_TESTOWNCLOUDPROPAGATOR_H #include +#include +#include "propagatedownload.h" + +using namespace OCC; +namespace OCC { +QString createDownloadTmpFileName(const QString &previous); +} class TestOwncloudPropagator : public QObject { @@ -20,6 +27,17 @@ private slots: // OwncloudPropagator propagator( NULL, QLatin1String("test1"), QLatin1String("test2"), new ProgressDatabase); QVERIFY( true ); } + + void testTmpDownloadFileNameGeneration() + { + QString fn; + for (int i = 1; i < 1000; i++) { + fn+="F"; + QString tmpFileName = createDownloadTmpFileName(fn); + QVERIFY( tmpFileName.length() > 0); + QVERIFY( tmpFileName.length() <= 254); + } + } }; #endif -- 2.30.2