diff options
Diffstat (limited to 'src/libs/installer/genericdatacache.cpp')
-rw-r--r-- | src/libs/installer/genericdatacache.cpp | 46 |
1 files changed, 37 insertions, 9 deletions
diff --git a/src/libs/installer/genericdatacache.cpp b/src/libs/installer/genericdatacache.cpp index fd264ce63..a1e31ccfe 100644 --- a/src/libs/installer/genericdatacache.cpp +++ b/src/libs/installer/genericdatacache.cpp @@ -1,6 +1,6 @@ /************************************************************************** ** -** Copyright (C) 2022 The Qt Company Ltd. +** Copyright (C) 2023 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Installer Framework. @@ -120,6 +120,16 @@ CacheableItem::~CacheableItem() */ /*! + \enum GenericDataCache::RegisterMode + This enum holds the possible values for modes of registering items to cache. + + \value Copy + The contents of the item are copied to the cache. + \value Move + The contents of the item are move to the cache. +*/ + +/*! \fn template <typename T> QInstaller::GenericDataCache<T>::GenericDataCache() Constructs a new empty cache. The cache is invalid until set with a @@ -411,19 +421,20 @@ T *GenericDataCache<T>::itemByPath(const QString &path) const } /*! - \fn template <typename T> QInstaller::GenericDataCache<T>::registerItem(T *item, bool replace) + \fn template <typename T> QInstaller::GenericDataCache<T>::registerItem(T *item, bool replace, RegisterMode mode) Registers the \a item to the cache. If \a replace is set to \c true, the new \a item replaces a previous item with the same checksum. The cache takes ownership of the object pointed by \a item. The contents of the - item are copied to the cache with a subdirectory name that matches the checksum - of the item. + item are copied or moved to the cache with a subdirectory name that matches the checksum + of the item. The \a mode decides how the contents of the item are registered, either by + copying or moving. Returns \c true on success or \c false if the item could not be registered. */ template <typename T> -bool GenericDataCache<T>::registerItem(T *item, bool replace) +bool GenericDataCache<T>::registerItem(T *item, bool replace, RegisterMode mode) { QMutexLocker _(&m_mutex); if (m_invalidated) { @@ -455,10 +466,27 @@ bool GenericDataCache<T>::registerItem(T *item, bool replace) const QString newPath = m_path + QDir::separator() + QString::fromLatin1(item->checksum()); try { // A directory is in the way but it isn't registered to the current cache, remove. - if (QDir().exists(newPath)) + QDir dir; + if (dir.exists(newPath)) QInstaller::removeDirectory(newPath); - QInstaller::copyDirectoryContents(item->path(), newPath); + switch (mode) { + case Copy: + QInstaller::copyDirectoryContents(item->path(), newPath); + break; + case Move: + // First, try moving the top level directory + if (!dir.rename(item->path(), newPath)) { + qCDebug(lcDeveloperBuild) << "Failed to rename directory" << item->path() + << "to" << newPath << ". Trying again."; + // If that does not work, fallback to moving the contents one by one + QInstaller::moveDirectoryContents(item->path(), newPath); + } + break; + default: + throw Error(QCoreApplication::translate("GenericDataCache", + "Unknown register mode selected!")); + } } catch (const Error &e) { setErrorString(QCoreApplication::translate("GenericDataCache", "Error while copying item to path \"%1\": %2").arg(newPath, e.message())); @@ -587,8 +615,8 @@ bool GenericDataCache<T>::fromDisk() for (const auto &itemJsonValue : itemsJsonArray) { const QString checksum = itemJsonValue.toString(); - QScopedPointer<T> item(new T(m_path + QDir::separator() + checksum)); - m_items.insert(checksum.toLatin1(), item.take()); + std::unique_ptr<T> item(new T(m_path + QDir::separator() + checksum)); + m_items.insert(checksum.toLatin1(), item.release()); // The cache directory may contain other entries (unrelated directories or // invalid old cache items) which we don't care about, unless registering |