changeset 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
files data/fileio/FileSource.cpp data/fileio/FileSource.h
diffstat 2 files changed, 20 insertions(+), 145 deletions(-) [+]
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);
 
--- a/data/fileio/FileSource.h	Mon Oct 27 20:29:55 2008 +0000
+++ b/data/fileio/FileSource.h	Tue Oct 28 11:07:08 2008 +0000
@@ -51,12 +51,10 @@
  * pass FileSource objects by value.  FileSource only makes sense for
  * stateless URLs that result in the same data on each request.
  *
- * Cached files (in their default temporary mode) share a lifetime
- * with their "owning" FileSource objects.  When the last FileSource
- * referring to a given URL is deleted or goes out of scope, its
- * cached file (if any) is also removed.  You can change this
- * behaviour by using persistent cache mode; \see LocalCacheMode for
- * details and caveats.
+ * Cached files share a lifetime with their "owning" FileSource
+ * objects; when the last FileSource referring to a given URL is
+ * deleted or goes out of scope, its cached file (if any) is also
+ * removed.
  */
 class FileSource : public QObject
 {
@@ -64,36 +62,6 @@
 
 public:
     /**
-     * Type of local cache to be used when retrieving remote files.
-     *
-     * Temporary cache files are created when a FileSource object is
-     * first created for a given URL, and removed when the last extant
-     * temporary cache mode FileSource object referring to a given URL
-     * is deleted (i.e. when its reference count is lowered to zero).
-     * They are also stored in a temporary directory that will be
-     * deleted when the program exits.
-     *
-     * Persistent cache files are created only when first retrieving a
-     * URL for which no persistent cache already exists, and are never
-     * deleted (by FileSource anyway).  They are stored in a directory
-     * that is not deleted when the program exits.  FileSource creates
-     * a unique local file name for each source URL, so as long as the
-     * local cache file remains on disc, the remote URL will not be
-     * retrieved again during any further run of the program.  You can
-     * find out what local file name will be used for the persistent
-     * cache of a given URL by calling getPersistentCacheFilePath, if
-     * you want to do something such as delete it by hand.
-     *
-     * Note that FileSource does not cache local files (i.e. does not
-     * make a copy of files that already appear to be stored on the
-     * local filesystem) in either mode.
-     */
-    enum LocalCacheMode {
-        TemporaryCache,
-        PersistentCache
-    };
-
-    /**
      * Construct a FileSource using the given local file path or URL.
      * The URL may be raw or encoded.
      *
@@ -101,13 +69,8 @@
      * progress status.  Note that the progress() signal will also be
      * emitted regularly during retrieval, even if no reporter is
      * supplied here.  Caller retains ownership of the reporter object.
-     *
-     * If LocalCacheMode is PersistentCache, a persistent cache file
-     * will be used.  See LocalCacheMode documentation for details.
      */
-    FileSource(QString fileOrUrl,
-               ProgressReporter *reporter = 0,
-               LocalCacheMode mode = TemporaryCache);
+    FileSource(QString fileOrUrl, ProgressReporter *reporter = 0);
 
     /**
      * Construct a FileSource using the given remote URL.
@@ -116,13 +79,8 @@
      * progress status.  Note that the progress() signal will also be
      * emitted regularly during retrieval, even if no reporter is
      * supplied here.  Caller retains ownership of the reporter object.
-     *
-     * If LocalCacheMode is PersistentCache, a persistent cache file
-     * will be used.  See LocalCacheMode documentation for details.
      */
-    FileSource(QUrl url,
-               ProgressReporter *reporter = 0,
-               LocalCacheMode mode = TemporaryCache);
+    FileSource(QUrl url, ProgressReporter *reporter = 0);
 
     FileSource(const FileSource &);
 
@@ -200,11 +158,7 @@
     /**
      * Specify whether any local, cached file should remain on disc
      * after this FileSource has been destroyed.  The default is false
-     * (cached files share their FileSource owners' lifespans).  This
-     * is only meaningful in TemporaryCache mode; even if this setting
-     * is true, the temporary cache will still be deleted when the
-     * program exits.  Use PersistentCache mode if you want the cache
-     * to outlast the program.
+     * (cached files share their FileSource owners' lifespans).
      */
     void setLeaveLocalFile(bool leave);
 
@@ -220,17 +174,6 @@
      */
     static bool canHandleScheme(QUrl url);
 
-    /**
-     * Return the path that will be used for the cache file copy of
-     * the given remote URL by a FileSource object constructed in
-     * PersistentCache mode.
-     *
-     * This method also creates the containing directory for such
-     * cache files, if it does not already exist, and so may throw
-     * DirectoryCreationFailed.
-    */
-    static QString getPersistentCacheFilePath(QUrl url);
-
 signals:
     /**
      * Emitted during URL retrieval, when the retrieval progress
@@ -263,7 +206,6 @@
     FileSource &operator=(const FileSource &); // not provided
 
     QUrl m_url;
-    LocalCacheMode m_cacheMode;
     QFtp *m_ftp;
     QHttp *m_http;
     QFile *m_localFile;
@@ -295,8 +237,6 @@
     bool createCacheFile();
     void deleteCacheFile();
 
-    static QString getPersistentCacheDirectory();
-
     static QMutex m_fileCreationMutex;
     static int m_count;
 };