diff options
author | Mårten Nordheim <marten.nordheim@qt.io> | 2023-03-03 16:52:34 +0100 |
---|---|---|
committer | Mårten Nordheim <marten.nordheim@qt.io> | 2023-03-13 17:29:11 +0100 |
commit | 9c27e165993cf673e48f6514ffc210f046c9d5fc (patch) | |
tree | 4b9c95532e58a5b5d7fb8f7a62b4896a50755ad1 /src/network | |
parent | 80eba4bb944cc08651ecb01553492786ec3f4ec1 (diff) | |
download | qtbase-9c27e165993cf673e48f6514ffc210f046c9d5fc.tar.gz |
QNetworkDiskCache: use QSaveFile
QTemporaryFile is not designed to promote temporary files
to non-temporary files. So, it, quite importantly, does not
care if the content of the files are flushed to disk before
renaming it into its 'final' destination. This is what
QSaveFile is for.
This was much more time-consuming than intended since I had to debug
this quirk about calling size().
Fixes: QTBUG-111397
Change-Id: I15b300f6a5748ad3a0be983545ea621269a12ecd
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit e5f295c8a458dcd336f7cf3768ca62aded69e659)
Diffstat (limited to 'src/network')
-rw-r--r-- | src/network/access/qnetworkdiskcache.cpp | 51 | ||||
-rw-r--r-- | src/network/access/qnetworkdiskcache_p.h | 17 |
2 files changed, 23 insertions, 45 deletions
diff --git a/src/network/access/qnetworkdiskcache.cpp b/src/network/access/qnetworkdiskcache.cpp index 6b52b8d49a..cf6f0a9b14 100644 --- a/src/network/access/qnetworkdiskcache.cpp +++ b/src/network/access/qnetworkdiskcache.cpp @@ -21,7 +21,6 @@ #include <memory> #define CACHE_POSTFIX ".d"_L1 -#define PREPARED_SLASH "prepared/"_L1 #define CACHE_VERSION 8 #define DATA_DIR "data"_L1 @@ -173,13 +172,13 @@ QIODevice *QNetworkDiskCache::prepare(const QNetworkCacheMetaData &metaData) cacheItem->data.open(QBuffer::ReadWrite); device = &(cacheItem->data); } else { - QString templateName = d->tmpCacheFileName(); + QString fileName = d->cacheFileName(cacheItem->metaData.url()); QT_TRY { - cacheItem->file = new QTemporaryFile(templateName, &cacheItem->data); + cacheItem->file = new QSaveFile(fileName, &cacheItem->data); } QT_CATCH(...) { cacheItem->file = nullptr; } - if (!cacheItem->file || !cacheItem->file->open()) { + if (!cacheItem->file || !cacheItem->file->open(QFileDevice::WriteOnly)) { qWarning("QNetworkDiskCache::prepare() unable to open temporary file"); cacheItem.reset(); return nullptr; @@ -219,7 +218,6 @@ void QNetworkDiskCache::insert(QIODevice *device) void QNetworkDiskCachePrivate::prepareLayout() { QDir helper; - helper.mkpath(cacheDirectory + PREPARED_SLASH); //Create directory and subdirectories 0-F helper.mkpath(dataDirectory); @@ -248,9 +246,8 @@ void QNetworkDiskCachePrivate::storeItem(QCacheItem *cacheItem) currentCacheSize = q->expire(); if (!cacheItem->file) { - QString templateName = tmpCacheFileName(); - cacheItem->file = new QTemporaryFile(templateName, &cacheItem->data); - if (cacheItem->file->open()) { + cacheItem->file = new QSaveFile(fileName, &cacheItem->data); + if (cacheItem->file->open(QFileDevice::WriteOnly)) { cacheItem->writeHeader(cacheItem->file); cacheItem->writeCompressedData(cacheItem->file); } @@ -258,13 +255,15 @@ void QNetworkDiskCachePrivate::storeItem(QCacheItem *cacheItem) if (cacheItem->file && cacheItem->file->isOpen() - && cacheItem->file->error() == QFile::NoError) { - cacheItem->file->setAutoRemove(false); - // ### use atomic rename rather then remove & rename - if (cacheItem->file->rename(fileName)) - currentCacheSize += cacheItem->file->size(); - else - cacheItem->file->setAutoRemove(true); + && cacheItem->file->error() == QFileDevice::NoError) { + // We have to call size() here instead of inside the if-body because + // commit() invalidates the file-engine, and size() will create a new + // one, pointing at an empty filename. + qint64 size = cacheItem->file->size(); + if (cacheItem->file->commit()) + currentCacheSize += size; + // Delete and unset the QSaveFile, it's invalid now. + delete std::exchange(cacheItem->file, nullptr); } if (cacheItem->metaData.url() == lastItem.metaData.url()) lastItem.reset(); @@ -509,16 +508,6 @@ qint64 QNetworkDiskCache::expire() QString name = i.value(); QFile file(name); - if (name.contains(PREPARED_SLASH)) { - for (QCacheItem *item : std::as_const(d->inserting)) { - if (item && item->file && item->file->fileName() == name) { - delete item->file; - item->file = nullptr; - break; - } - } - } - qint64 size = file.size(); file.remove(); totalSize -= size; @@ -569,12 +558,6 @@ QString QNetworkDiskCachePrivate::uniqueFileName(const QUrl &url) return pathFragment; } -QString QNetworkDiskCachePrivate::tmpCacheFileName() const -{ - //The subdirectory is presumed to be already read for use. - return cacheDirectory + PREPARED_SLASH + "XXXXXX"_L1 + CACHE_POSTFIX; -} - /*! Generates fully qualified path of cached resource from a URL. */ @@ -625,7 +608,7 @@ enum CurrentCacheVersion = CACHE_VERSION }; -void QCacheItem::writeHeader(QFile *device) const +void QCacheItem::writeHeader(QFileDevice *device) const { QDataStream out(device); @@ -637,7 +620,7 @@ void QCacheItem::writeHeader(QFile *device) const out << compressed; } -void QCacheItem::writeCompressedData(QFile *device) const +void QCacheItem::writeCompressedData(QFileDevice *device) const { QDataStream out(device); @@ -648,7 +631,7 @@ void QCacheItem::writeCompressedData(QFile *device) const Returns \c false if the file is a cache file, but is an older version and should be removed otherwise true. */ -bool QCacheItem::read(QFile *device, bool readData) +bool QCacheItem::read(QFileDevice *device, bool readData) { reset(); diff --git a/src/network/access/qnetworkdiskcache_p.h b/src/network/access/qnetworkdiskcache_p.h index 05c97dff08..826f0a1d7d 100644 --- a/src/network/access/qnetworkdiskcache_p.h +++ b/src/network/access/qnetworkdiskcache_p.h @@ -20,20 +20,16 @@ #include <qbuffer.h> #include <qhash.h> -#include <qtemporaryfile.h> +#include <qsavefile.h> QT_REQUIRE_CONFIG(networkdiskcache); QT_BEGIN_NAMESPACE -class QFile; - class QCacheItem { public: - QCacheItem() : file(nullptr) - { - } + QCacheItem() = default; ~QCacheItem() { reset(); @@ -41,7 +37,7 @@ public: QNetworkCacheMetaData metaData; QBuffer data; - QTemporaryFile *file; + QSaveFile *file = nullptr; inline qint64 size() const { return file ? file->size() : data.size(); } @@ -51,9 +47,9 @@ public: delete file; file = nullptr; } - void writeHeader(QFile *device) const; - void writeCompressedData(QFile *device) const; - bool read(QFile *device, bool readData); + void writeHeader(QFileDevice *device) const; + void writeCompressedData(QFileDevice *device) const; + bool read(QFileDevice *device, bool readData); bool canCompress() const; }; @@ -69,7 +65,6 @@ public: static QString uniqueFileName(const QUrl &url); QString cacheFileName(const QUrl &url) const; - QString tmpCacheFileName() const; bool removeFile(const QString &file); void storeItem(QCacheItem *item); void prepareLayout(); |