From 0e628290af2bd3cf35c322e22bf17bad59187ecf Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Tue, 19 Jan 2021 19:44:22 +0100 Subject: Write database to the DataLocation --- src/Cache.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/Cache.cpp') diff --git a/src/Cache.cpp b/src/Cache.cpp index 7e25fed2..6b3067db 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -169,13 +169,35 @@ Cache::setup() nhlog::db()->debug("setting up cache"); + // Previous location of the cache directory + auto oldCache = QString("%1/%2%3") + .arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)) + .arg(QString::fromUtf8(localUserId_.toUtf8().toHex())) + .arg(QString::fromUtf8(settings->profile().toUtf8().toHex())); + cacheDirectory_ = QString("%1/%2%3") - .arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)) + .arg(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)) .arg(QString::fromUtf8(localUserId_.toUtf8().toHex())) .arg(QString::fromUtf8(settings->profile().toUtf8().toHex())); bool isInitial = !QFile::exists(cacheDirectory_); + // NOTE: If both cache directories exist it's better to do nothing: it + // could mean a previous migration failed or was interrupted. + bool needsMigration = isInitial && QFile::exists(oldCache); + + if (needsMigration) { + nhlog::db()->info("found old state directory, migrating"); + if (!QDir().rename(oldCache, cacheDirectory_)) { + throw std::runtime_error(("Unable to migrate the old state directory (" + + oldCache + ") to the new location (" + + cacheDirectory_ + ")") + .toStdString() + .c_str()); + } + nhlog::db()->info("completed state migration"); + } + env_ = lmdb::env::create(); env_.set_mapsize(DB_SIZE); env_.set_max_dbs(MAX_DBS); -- cgit 1.5.1