comparison data/fileio/CachedFile.cpp @ 466:f35bfa88f0b5

* flesh out CachedFile a bit more
author Chris Cannam
date Mon, 27 Oct 2008 15:07:35 +0000
parents 63b8ba45d953
children c9b055f84326
comparison
equal deleted inserted replaced
465:63b8ba45d953 466:f35bfa88f0b5
17 17
18 #include "base/TempDirectory.h" 18 #include "base/TempDirectory.h"
19 #include "base/ProgressReporter.h" 19 #include "base/ProgressReporter.h"
20 #include "base/Exceptions.h" 20 #include "base/Exceptions.h"
21 21
22 #include "FileSource.h"
23
22 #include <QFileInfo> 24 #include <QFileInfo>
23 #include <QSettings> 25 #include <QSettings>
24 #include <QVariant> 26 #include <QVariant>
25 #include <QMap> 27 #include <QMap>
26 #include <QDir> 28 #include <QDir>
27 #include <QCryptographicHash> 29 #include <QCryptographicHash>
28 30
31 #include <iostream>
32
29 QString 33 QString
30 CachedFile::getLocalFilenameFor(QUrl url) 34 CachedFile::getLocalFilenameFor(QUrl url)
31 { 35 {
32 QDir dir(getCacheDirectory()); 36 QDir dir(getCacheDirectory());
33 37
58 } 62 }
59 63
60 CachedFile::CachedFile(QUrl url, ProgressReporter *reporter) : 64 CachedFile::CachedFile(QUrl url, ProgressReporter *reporter) :
61 m_url(url), 65 m_url(url),
62 m_localFilename(getLocalFilenameFor(url)), 66 m_localFilename(getLocalFilenameFor(url)),
67 m_reporter(reporter),
63 m_ok(false) 68 m_ok(false)
64 { 69 {
65 refresh(); 70 refresh();
66 } 71 }
67 72
126 //!!! then "atomically" moving it to its proper place (I'm not 131 //!!! then "atomically" moving it to its proper place (I'm not
127 //!!! sure we can do an atomic move to replace an existing file 132 //!!! sure we can do an atomic move to replace an existing file
128 //!!! using Qt classes, but a plain delete then copy is probably 133 //!!! using Qt classes, but a plain delete then copy is probably
129 //!!! good enough) 134 //!!! good enough)
130 135
131 136 FileSource fs(m_url, m_reporter);
132 137
133 138 if (!fs.isOK() || !fs.isAvailable()) {
139 return false;
140 }
141
142 fs.waitForData();
143
144 if (!fs.isOK()) {
145 return false;
146 }
147
148 QString tempName = fs.getLocalFilename();
149 QFile tempFile(tempName);
150 if (!tempFile.exists()) {
151 std::cerr << "CachedFile::retrieve: ERROR: FileSource reported success, but local temporary file \"" << tempName.toStdString() << "\" does not exist" << std::endl;
152 return false;
153 }
154
155 QFile previous(m_localFilename);
156 if (previous.exists()) {
157 if (!previous.remove()) {
158 std::cerr << "CachedFile::retrieve: ERROR: Failed to remove previous copy of cached file at \"" << m_localFilename.toStdString() << "\"" << std::endl;
159 return false;
160 }
161 }
162
163 //!!! This is not ideal, could leave us with nothing (old file
164 //!!! removed, new file not able to be copied in because e.g. no
165 //!!! disk space left)
166
167 if (!tempFile.copy(m_localFilename)) {
168 std::cerr << "CachedFile::retrieve: ERROR: Failed to copy newly retrieved file from \"" << tempName.toStdString() << "\" to \"" << m_localFilename.toStdString() << "\"" << std::endl;
169 return false;
170 }
171
172 return true;
134 } 173 }
135 174
136 QDateTime 175 QDateTime
137 CachedFile::getLastRetrieval() 176 CachedFile::getLastRetrieval()
138 { 177 {