Mercurial > hg > svcore
comparison data/fileio/CachedFile.cpp @ 742:c10cb8782576 coreaudio_tests
Merge from branch "default"
author | Chris Cannam |
---|---|
date | Sun, 01 Jul 2012 11:53:00 +0100 |
parents | b99dc5465b80 |
children | e802e550a1f2 |
comparison
equal
deleted
inserted
replaced
666:4efa7429cd85 | 742:c10cb8782576 |
---|---|
76 m_reporter(reporter), | 76 m_reporter(reporter), |
77 m_ok(false) | 77 m_ok(false) |
78 { | 78 { |
79 Profiler p("CachedFile::CachedFile[1]"); | 79 Profiler p("CachedFile::CachedFile[1]"); |
80 | 80 |
81 std::cerr << "CachedFile::CachedFile: origin is \"" | 81 SVDEBUG << "CachedFile::CachedFile: origin is \"" |
82 << origin.toStdString() << "\"" << std::endl; | 82 << origin << "\"" << endl; |
83 check(); | 83 checkFile(); |
84 } | 84 } |
85 | 85 |
86 CachedFile::CachedFile(QUrl url, | 86 CachedFile::CachedFile(QUrl url, |
87 ProgressReporter *reporter, | 87 ProgressReporter *reporter, |
88 QString preferredContentType) : | 88 QString preferredContentType) : |
91 m_reporter(reporter), | 91 m_reporter(reporter), |
92 m_ok(false) | 92 m_ok(false) |
93 { | 93 { |
94 Profiler p("CachedFile::CachedFile[2]"); | 94 Profiler p("CachedFile::CachedFile[2]"); |
95 | 95 |
96 std::cerr << "CachedFile::CachedFile: url is \"" | 96 SVDEBUG << "CachedFile::CachedFile: url is \"" |
97 << url.toString().toStdString() << "\"" << std::endl; | 97 << url.toString() << "\"" << endl; |
98 check(); | 98 checkFile(); |
99 } | 99 } |
100 | 100 |
101 CachedFile::~CachedFile() | 101 CachedFile::~CachedFile() |
102 { | 102 { |
103 } | 103 } |
113 { | 113 { |
114 return m_localFilename; | 114 return m_localFilename; |
115 } | 115 } |
116 | 116 |
117 void | 117 void |
118 CachedFile::check() | 118 CachedFile::checkFile() |
119 { | 119 { |
120 //!!! n.b. obvious race condition here if different CachedFile | 120 //!!! n.b. obvious race condition here if different CachedFile |
121 // objects for same url used in more than one thread -- need to | 121 // objects for same url used in more than one thread -- need to |
122 // lock appropriately. also consider race condition between | 122 // lock appropriately. also consider race condition between |
123 // separate instances of the program! | 123 // separate instances of the program! |
130 } | 130 } |
131 | 131 |
132 m_localFilename = getLocalFilenameFor(m_origin); | 132 m_localFilename = getLocalFilenameFor(m_origin); |
133 | 133 |
134 if (!QFileInfo(m_localFilename).exists()) { | 134 if (!QFileInfo(m_localFilename).exists()) { |
135 std::cerr << "CachedFile::check: Local file does not exist, making a note that it hasn't been retrieved" << std::endl; | 135 SVDEBUG << "CachedFile::check: Local file does not exist, making a note that it hasn't been retrieved" << endl; |
136 updateLastRetrieval(false); // empirically! | 136 updateLastRetrieval(false); // empirically! |
137 } | 137 } |
138 | 138 |
139 QDateTime lastRetrieval = getLastRetrieval(); | 139 QDateTime lastRetrieval = getLastRetrieval(); |
140 | 140 |
141 if (lastRetrieval.isValid()) { | 141 if (lastRetrieval.isValid()) { |
142 std::cerr << "CachedFile::check: Valid last retrieval at " | 142 SVDEBUG << "CachedFile::check: Valid last retrieval at " |
143 << lastRetrieval.toString().toStdString() << std::endl; | 143 << lastRetrieval.toString() << endl; |
144 // this will not be the case if the file is missing, after | 144 // this will not be the case if the file is missing, after |
145 // updateLastRetrieval(false) was called above | 145 // updateLastRetrieval(false) was called above |
146 m_ok = true; | 146 m_ok = true; |
147 if (lastRetrieval.addDays(2) < QDateTime::currentDateTime()) { //!!! | 147 if (lastRetrieval.addDays(2) < QDateTime::currentDateTime()) { //!!! |
148 std::cerr << "CachedFile::check: Out of date; trying to retrieve again" << std::endl; | 148 SVDEBUG << "CachedFile::check: Out of date; trying to retrieve again" << endl; |
149 // doesn't matter if retrieval fails -- we just don't | 149 // doesn't matter if retrieval fails -- we just don't |
150 // update the last retrieval time | 150 // update the last retrieval time |
151 | 151 |
152 //!!! but we do want an additional last-attempted | 152 //!!! but we do want an additional last-attempted |
153 // timestamp so as to ensure we aren't retrying the | 153 // timestamp so as to ensure we aren't retrying the |
154 // retrieval every single time if it isn't working | 154 // retrieval every single time if it isn't working |
155 | 155 |
156 if (retrieve()) { | 156 if (retrieve()) { |
157 std::cerr << "CachedFile::check: Retrieval succeeded" << std::endl; | 157 SVDEBUG << "CachedFile::check: Retrieval succeeded" << endl; |
158 updateLastRetrieval(true); | 158 updateLastRetrieval(true); |
159 } else { | 159 } else { |
160 std::cerr << "CachedFile::check: Retrieval failed, will try again later (using existing file for now)" << std::endl; | 160 std::cerr << "CachedFile::check: Retrieval failed, will try again later (using existing file for now)" << std::endl; |
161 } | 161 } |
162 } | 162 } |
163 } else { | 163 } else { |
164 std::cerr << "CachedFile::check: No valid last retrieval" << std::endl; | 164 SVDEBUG << "CachedFile::check: No valid last retrieval" << endl; |
165 // there is no acceptable file | 165 // there is no acceptable file |
166 if (retrieve()) { | 166 if (retrieve()) { |
167 std::cerr << "CachedFile::check: Retrieval succeeded" << std::endl; | 167 SVDEBUG << "CachedFile::check: Retrieval succeeded" << endl; |
168 m_ok = true; | 168 m_ok = true; |
169 updateLastRetrieval(true); | 169 updateLastRetrieval(true); |
170 } else { | 170 } else { |
171 std::cerr << "CachedFile::check: Retrieval failed, remaining in invalid state" << std::endl; | 171 std::cerr << "CachedFile::check: Retrieval failed, remaining in invalid state" << std::endl; |
172 // again, we don't need to do anything here -- the last | 172 // again, we don't need to do anything here -- the last |
189 //!!! good enough) | 189 //!!! good enough) |
190 | 190 |
191 FileSource fs(m_origin, m_reporter, m_preferredContentType); | 191 FileSource fs(m_origin, m_reporter, m_preferredContentType); |
192 | 192 |
193 if (!fs.isOK() || !fs.isAvailable()) { | 193 if (!fs.isOK() || !fs.isAvailable()) { |
194 std::cerr << "CachedFile::retrieve: ERROR: FileSource reported unavailable or failure" << std::endl; | 194 SVDEBUG << "CachedFile::retrieve: ERROR: FileSource reported unavailable or failure" << endl; |
195 return false; | 195 return false; |
196 } | 196 } |
197 | 197 |
198 fs.waitForData(); | 198 fs.waitForData(); |
199 | 199 |
200 if (!fs.isOK()) { | 200 if (!fs.isOK()) { |
201 std::cerr << "CachedFile::retrieve: ERROR: FileSource reported failure during receive" << std::endl; | 201 SVDEBUG << "CachedFile::retrieve: ERROR: FileSource reported failure during receive" << endl; |
202 return false; | 202 return false; |
203 } | 203 } |
204 | 204 |
205 QString tempName = fs.getLocalFilename(); | 205 QString tempName = fs.getLocalFilename(); |
206 QFile tempFile(tempName); | 206 QFile tempFile(tempName); |
207 if (!tempFile.exists()) { | 207 if (!tempFile.exists()) { |
208 std::cerr << "CachedFile::retrieve: ERROR: FileSource reported success, but local temporary file \"" << tempName.toStdString() << "\" does not exist" << std::endl; | 208 SVDEBUG << "CachedFile::retrieve: ERROR: FileSource reported success, but local temporary file \"" << tempName << "\" does not exist" << endl; |
209 return false; | 209 return false; |
210 } | 210 } |
211 | 211 |
212 QFile previous(m_localFilename); | 212 QFile previous(m_localFilename); |
213 if (previous.exists()) { | 213 if (previous.exists()) { |
214 if (!previous.remove()) { | 214 if (!previous.remove()) { |
215 std::cerr << "CachedFile::retrieve: ERROR: Failed to remove previous copy of cached file at \"" << m_localFilename.toStdString() << "\"" << std::endl; | 215 std::cerr << "CachedFile::retrieve: ERROR: Failed to remove previous copy of cached file at \"" << m_localFilename << "\"" << std::endl; |
216 return false; | 216 return false; |
217 } | 217 } |
218 } | 218 } |
219 | 219 |
220 //!!! This is not ideal, could leave us with nothing (old file | 220 //!!! This is not ideal, could leave us with nothing (old file |
221 //!!! removed, new file not able to be copied in because e.g. no | 221 //!!! removed, new file not able to be copied in because e.g. no |
222 //!!! disk space left) | 222 //!!! disk space left) |
223 | 223 |
224 if (!tempFile.copy(m_localFilename)) { | 224 if (!tempFile.copy(m_localFilename)) { |
225 std::cerr << "CachedFile::retrieve: ERROR: Failed to copy newly retrieved file from \"" << tempName.toStdString() << "\" to \"" << m_localFilename.toStdString() << "\"" << std::endl; | 225 std::cerr << "CachedFile::retrieve: ERROR: Failed to copy newly retrieved file from \"" << tempName << "\" to \"" << m_localFilename << "\"" << std::endl; |
226 return false; | 226 return false; |
227 } | 227 } |
228 | 228 |
229 std::cerr << "CachedFile::retrieve: Successfully copied newly retrieved file \"" << tempName.toStdString() << "\" to its home at \"" << m_localFilename.toStdString() << "\"" << std::endl; | 229 SVDEBUG << "CachedFile::retrieve: Successfully copied newly retrieved file \"" << tempName << "\" to its home at \"" << m_localFilename << "\"" << endl; |
230 | 230 |
231 return true; | 231 return true; |
232 } | 232 } |
233 | 233 |
234 QDateTime | 234 QDateTime |