diff --git a/src/gui/accountmanager.cpp b/src/gui/accountmanager.cpp index e78d760960a12..2923fd7eee504 100644 --- a/src/gui/accountmanager.cpp +++ b/src/gui/accountmanager.cpp @@ -132,6 +132,7 @@ AccountManager::AccountsRestoreResult AccountManager::restore(const bool alsoRes const auto jar = qobject_cast(acc->_networkAccessManager->cookieJar()); Q_ASSERT(jar); if (jar) { + acc->tryMigrateCookieJar(); jar->restore(acc->cookieJarPath()); } addAccountState(accState); diff --git a/src/gui/syncrunfilelog.cpp b/src/gui/syncrunfilelog.cpp index 828b9a4f37005..aefbc04d909a7 100644 --- a/src/gui/syncrunfilelog.cpp +++ b/src/gui/syncrunfilelog.cpp @@ -24,7 +24,7 @@ void SyncRunFileLog::start(const QString &folderPath) { const qint64 logfileMaxSize = 10 * 1024 * 1024; // 10MiB - const QString logpath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); + const QString logpath = QStandardPaths::writableLocation(QStandardPaths::StateLocation); if(!QDir(logpath).exists()) { QDir().mkdir(logpath); } diff --git a/src/gui/updater/ocupdater.cpp b/src/gui/updater/ocupdater.cpp index 4d43357fa80c0..26bb60cb6b875 100644 --- a/src/gui/updater/ocupdater.cpp +++ b/src/gui/updater/ocupdater.cpp @@ -219,7 +219,7 @@ void OCUpdater::slotStartInstaller() return QDir::toNativeSeparators(path); }; - QString msiLogFile = cfg.configPath() + "msi.log"; + QString msiLogFile = cfg.logDir() + "msi.log"; QString command = QStringLiteral("&{msiexec /i '%1' /L*V '%2'| Out-Null ; &'%3'}") .arg(preparePathForPowershell(updateFile)) .arg(preparePathForPowershell(msiLogFile)) diff --git a/src/libsync/account.cpp b/src/libsync/account.cpp index b720cb80001c1..af75a45c5db44 100644 --- a/src/libsync/account.cpp +++ b/src/libsync/account.cpp @@ -400,9 +400,25 @@ void Account::lendCookieJarTo(QNetworkAccessManager *guest) jar->setParent(oldParent); // takes it back } +void Account::tryMigrateCookieJar() +{ + QString oldCookieJarPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/cookies" + id() + ".db"; + if (QFile::exists(oldCookieJarPath)) + { + if (QFile::exists(cookieJarPath())) + { + qWarning() << "Both old cookie jar and new cookie jar exists. Abort migration"; + return; + } + qDebug() << "Migrating cookie jar from "<< oldCookieJarPath << "to " << cookieJarPath(); + if (!QFile::rename(oldCookieJarPath, cookieJarPath())) + qWarning() << "Failed to migrate cookie jar"; + } +} + QString Account::cookieJarPath() { - return QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/cookies" + id() + ".db"; + return QStandardPaths::writableLocation(QStandardPaths::StateLocation) + "/cookies" + id() + ".db"; } void Account::resetNetworkAccessManager() diff --git a/src/libsync/account.h b/src/libsync/account.h index bbb198762d3c1..f831bbc3be8f7 100644 --- a/src/libsync/account.h +++ b/src/libsync/account.h @@ -301,6 +301,7 @@ class OWNCLOUDSYNC_EXPORT Account : public QObject void clearCookieJar(); void lendCookieJarTo(QNetworkAccessManager *guest); + void tryMigrateCookieJar(); QString cookieJarPath(); void resetNetworkAccessManager(); diff --git a/src/libsync/configfile.cpp b/src/libsync/configfile.cpp index 71ac126d465bd..0b20a87d983a2 100644 --- a/src/libsync/configfile.cpp +++ b/src/libsync/configfile.cpp @@ -1118,7 +1118,7 @@ void ConfigFile::setAutomaticLogDir(bool enabled) QString ConfigFile::logDir() const { - const auto defaultLogDir = QString(configPath() + QStringLiteral("/logs")); + const auto defaultLogDir = QString(QStandardPaths::writableLocation(QStandardPaths::StateLocation) + QStringLiteral("/logs")); QSettings settings(configFile(), QSettings::IniFormat); return settings.value(QLatin1String(logDirC), defaultLogDir).toString(); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8cfe502b861f8..7daef989e3986 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -98,6 +98,7 @@ nextcloud_add_test(FileTagModel) nextcloud_add_test(SyncConflictsModel) nextcloud_add_test(DateFieldBackend) nextcloud_add_test(ClientStatusReporting) +nextcloud_add_test(CookieJarMigration) nextcloud_add_test(FileSystem) diff --git a/test/testcookiejarmigration.cpp b/test/testcookiejarmigration.cpp new file mode 100644 index 0000000000000..4d072be7afc2e --- /dev/null +++ b/test/testcookiejarmigration.cpp @@ -0,0 +1,85 @@ +/* + This software is in the public domain, furnished "as is", without technical + support, and with no warranty, express or implied, as to its usefulness for + any purpose. +*/ + +#include + +#include "account.h" +#include "accountmanager.h" +#include "logger.h" + +using namespace OCC; + +class TestCookieJarMigration : public QObject +{ + Q_OBJECT + AccountPtr _account; + QString oldCookieJarPath; + +private slots: + void initTestCase() + { + OCC::Logger::instance()->setLogFlush(true); + OCC::Logger::instance()->setLogDebug(true); + + QStandardPaths::setTestModeEnabled(true); + // Create directories used in test, since Qt doesn't create its automatically + QDir().mkpath(QStandardPaths::writableLocation(QStandardPaths::StateLocation)); + QDir().mkpath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)); + + _account = Account::create(); + AccountManager::instance()->addAccount(_account); + oldCookieJarPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/cookies" + _account->id() + ".db"; + } + void testNoAction() + { + QFile jarFile(_account->cookieJarPath()); + jarFile.open(QFile::WriteOnly); + jarFile.write("1", 1); // Write one byte to file + jarFile.close(); + _account->tryMigrateCookieJar(); + + QVERIFY(!QFile::exists(oldCookieJarPath)); // Check that old file doesn't exits + QCOMPARE(QFileInfo(_account->cookieJarPath()).size(), 1); // Check that this byte present in new file + QFile::remove(_account->cookieJarPath()); // Cleanup + } + void testSimpleMigration() + { + QFile oldJarFile(oldCookieJarPath); + oldJarFile.open(QFile::WriteOnly); + oldJarFile.write("1", 1); // Write one byte to file + oldJarFile.close(); + + _account->tryMigrateCookieJar(); + QVERIFY(!QFile::exists(oldCookieJarPath)); // Check that old file is deleted + + QCOMPARE(QFileInfo(_account->cookieJarPath()).size(), 1); // Check that this byte present in new file + QFile::remove(_account->cookieJarPath()); // Cleanup + } + void testNotOverwrite() + { + QFile oldJarFile(oldCookieJarPath); + oldJarFile.open(QFile::WriteOnly); + oldJarFile.write("1", 1); // Write one byte to file + oldJarFile.close(); + + QFile newJarFile(_account->cookieJarPath()); + oldJarFile.open(QFile::WriteOnly); + oldJarFile.write("123", 3); // Write three bytes to file + oldJarFile.close(); + + + _account->tryMigrateCookieJar(); + + QCOMPARE(QFileInfo(_account->cookieJarPath()).size(), 3); // Check that these bytes still present + + // Cleanup + QFile::remove(_account->cookieJarPath()); + QFile::remove(oldCookieJarPath); + } +}; + +QTEST_APPLESS_MAIN(TestCookieJarMigration) +#include "testcookiejarmigration.moc"