Mercurial > hg > svcore
diff data/fileio/FileSource.cpp @ 469:a8a7b8f698c8
* Revert revisions 1244 and 1245 on FileSource -- backing out its
built-in persistent cache support, as CachedFile now does the
same thing and will do so more properly
author | Chris Cannam |
---|---|
date | Tue, 28 Oct 2008 11:07:08 +0000 |
parents | 70b333085952 |
children | dddd4ab77068 |
line wrap: on
line diff
--- a/data/fileio/FileSource.cpp Mon Oct 27 20:29:55 2008 +0000 +++ b/data/fileio/FileSource.cpp Tue Oct 28 11:07:08 2008 +0000 @@ -24,13 +24,12 @@ #include <QFileInfo> #include <QDir> #include <QCoreApplication> -#include <QCryptographicHash> #include <QHttpResponseHeader> #include <iostream> #include <cstdlib> -#define DEBUG_FILE_SOURCE 1 +//#define DEBUG_FILE_SOURCE 1 int FileSource::m_count = 0; @@ -47,10 +46,8 @@ QMutex FileSource::m_mapMutex; -FileSource::FileSource(QString fileOrUrl, ProgressReporter *reporter, - LocalCacheMode cacheMode) : +FileSource::FileSource(QString fileOrUrl, ProgressReporter *reporter) : m_url(fileOrUrl), - m_cacheMode(cacheMode), m_ftp(0), m_http(0), m_localFile(0), @@ -69,7 +66,7 @@ } #ifdef DEBUG_FILE_SOURCE - std::cerr << "FileSource::FileSource(" << fileOrUrl.toStdString() << ", " << cacheMode << ")" << std::endl; + std::cerr << "FileSource::FileSource(" << fileOrUrl.toStdString() << ")" << std::endl; #endif if (!canHandleScheme(m_url)) { @@ -119,10 +116,8 @@ } } -FileSource::FileSource(QUrl url, ProgressReporter *reporter, - LocalCacheMode cacheMode) : +FileSource::FileSource(QUrl url, ProgressReporter *reporter) : m_url(url), - m_cacheMode(cacheMode), m_ftp(0), m_http(0), m_localFile(0), @@ -156,7 +151,6 @@ FileSource::FileSource(const FileSource &rf) : QObject(), m_url(rf.m_url), - m_cacheMode(rf.m_cacheMode), m_ftp(0), m_http(0), m_localFile(0), @@ -178,14 +172,8 @@ return; } - if (m_cacheMode == PersistentCache) { - + if (!isRemote()) { m_localFilename = rf.m_localFilename; - - } else if (!isRemote()) { - - m_localFilename = rf.m_localFilename; - } else { QMutexLocker locker(&m_mapMutex); #ifdef DEBUG_FILE_SOURCE @@ -216,9 +204,7 @@ cleanup(); - if (isRemote() && (m_cacheMode == TemporaryCache) && !m_leaveLocalFile) { - deleteCacheFile(); - } + if (isRemote() && !m_leaveLocalFile) deleteCacheFile(); } void @@ -264,8 +250,7 @@ if (createCacheFile()) { #ifdef DEBUG_FILE_SOURCE - std::cerr << "FileSource::init: Already have this one at " - << m_localFilename.toStdString() << std::endl; + std::cerr << "FileSource::init: Already have this one" << std::endl; #endif m_ok = true; if (!QFileInfo(m_localFilename).exists()) { @@ -285,8 +270,7 @@ #ifdef DEBUG_FILE_SOURCE std::cerr << "FileSource::init: Don't have local copy of \"" - << m_url.toString().toStdString() << "\", retrieving to " - << m_localFilename.toStdString() << std::endl; + << m_url.toString().toStdString() << "\", retrieving" << std::endl; #endif if (scheme == "http") { @@ -433,10 +417,6 @@ void FileSource::cleanup() { - if (m_done) { - delete m_localFile; // does not actually delete the file - m_localFile = 0; - } m_done = true; if (m_http) { QHttp *h = m_http; @@ -450,10 +430,8 @@ f->abort(); f->deleteLater(); } - if (m_localFile) { - delete m_localFile; // does not actually delete the file - m_localFile = 0; - } + delete m_localFile; // does not actually delete the file + m_localFile = 0; } bool @@ -678,8 +656,7 @@ if (error) { #ifdef DEBUG_FILE_SOURCE - std::cerr << "FileSource::done: error is " << error << " (\"" - << m_errorString.toStdString() << "\"), deleting cache file" << std::endl; + std::cerr << "FileSource::done: error is " << error << ", deleting cache file" << std::endl; #endif deleteCacheFile(); } @@ -728,15 +705,10 @@ m_fileCreationMutex.lock(); - // We always delete the file here, even in PersistentCache mode, - // because this function is also used if retrieval failed (in - // which case we want to delete the cache so that subsequent users - // won't trust it). It's up to the calling code to determine - // whether we actually want to delete the cache or not, e.g. on - // destruction. - if (!QFile(m_localFilename).remove()) { +#ifdef DEBUG_FILE_SOURCE std::cerr << "FileSource::deleteCacheFile: ERROR: Failed to delete file \"" << m_localFilename.toStdString() << "\"" << std::endl; +#endif } else { #ifdef DEBUG_FILE_SOURCE std::cerr << "FileSource::deleteCacheFile: Deleted cache file \"" << m_localFilename.toStdString() << "\"" << std::endl; @@ -749,46 +721,9 @@ m_done = true; } -QString -FileSource::getPersistentCacheDirectory() -{ - QDir dir = TempDirectory::getInstance()->getContainingPath(); - - QString cacheDirName("cache"); - - QFileInfo fi(dir.filePath(cacheDirName)); - - if ((fi.exists() && !fi.isDir()) || - (!fi.exists() && !dir.mkdir(cacheDirName))) { - - throw DirectoryCreationFailed(fi.filePath()); - } - - return fi.filePath(); -} - -QString -FileSource::getPersistentCacheFilePath(QUrl url) -{ - QDir dir(getPersistentCacheDirectory()); - - QString filename = - QString::fromLocal8Bit - (QCryptographicHash::hash(url.toString().toLocal8Bit(), - QCryptographicHash::Sha1).toHex()); - - return dir.filePath(filename); -} - bool FileSource::createCacheFile() { - if (m_cacheMode == PersistentCache) { - m_localFilename = getPersistentCacheFilePath(m_url); - if (QFileInfo(m_localFilename).exists()) return true; - else return false; - } - { QMutexLocker locker(&m_mapMutex);