# HG changeset patch # User Chris Cannam # Date 1225120055 0 # Node ID f35bfa88f0b53c3bc0b5b40feec21a354fbe13e4 # Parent 63b8ba45d9531abdb723093fcd7d0b2137474f65 * flesh out CachedFile a bit more diff -r 63b8ba45d953 -r f35bfa88f0b5 data/fileio/CachedFile.cpp --- a/data/fileio/CachedFile.cpp Mon Oct 27 11:53:35 2008 +0000 +++ b/data/fileio/CachedFile.cpp Mon Oct 27 15:07:35 2008 +0000 @@ -19,6 +19,8 @@ #include "base/ProgressReporter.h" #include "base/Exceptions.h" +#include "FileSource.h" + #include #include #include @@ -26,6 +28,8 @@ #include #include +#include + QString CachedFile::getLocalFilenameFor(QUrl url) { @@ -60,6 +64,7 @@ CachedFile::CachedFile(QUrl url, ProgressReporter *reporter) : m_url(url), m_localFilename(getLocalFilenameFor(url)), + m_reporter(reporter), m_ok(false) { refresh(); @@ -128,9 +133,43 @@ //!!! using Qt classes, but a plain delete then copy is probably //!!! good enough) + FileSource fs(m_url, m_reporter); + if (!fs.isOK() || !fs.isAvailable()) { + return false; + } + fs.waitForData(); + if (!fs.isOK()) { + return false; + } + + QString tempName = fs.getLocalFilename(); + QFile tempFile(tempName); + if (!tempFile.exists()) { + std::cerr << "CachedFile::retrieve: ERROR: FileSource reported success, but local temporary file \"" << tempName.toStdString() << "\" does not exist" << std::endl; + return false; + } + + QFile previous(m_localFilename); + if (previous.exists()) { + if (!previous.remove()) { + std::cerr << "CachedFile::retrieve: ERROR: Failed to remove previous copy of cached file at \"" << m_localFilename.toStdString() << "\"" << std::endl; + return false; + } + } + + //!!! This is not ideal, could leave us with nothing (old file + //!!! removed, new file not able to be copied in because e.g. no + //!!! disk space left) + + if (!tempFile.copy(m_localFilename)) { + std::cerr << "CachedFile::retrieve: ERROR: Failed to copy newly retrieved file from \"" << tempName.toStdString() << "\" to \"" << m_localFilename.toStdString() << "\"" << std::endl; + return false; + } + + return true; } QDateTime diff -r 63b8ba45d953 -r f35bfa88f0b5 data/fileio/CachedFile.h --- a/data/fileio/CachedFile.h Mon Oct 27 11:53:35 2008 +0000 +++ b/data/fileio/CachedFile.h Mon Oct 27 15:07:35 2008 +0000 @@ -36,6 +36,7 @@ protected: QUrl m_url; QString m_localFilename; + ProgressReporter *m_reporter; bool m_ok; void refresh();