This is kind of half-right. It helps make the package database code self-contained (since that needs to use check_for_cached as part of ScanDownloadedFiles), but also pulls apart the 'cache checking' and 'download file and put it in the cache'. There's probably some scope for an package_source interface for "where is the package cache download location for this package from that site." --- download.cc | 104 ++-------------------------------------------- download.h | 6 --- package_meta.cc | 3 +- package_source.cc | 95 ++++++++++++++++++++++++++++++++++++++++++ package_source.h | 4 ++ 5 files changed, 103 insertions(+), 109 deletions(-)
diff --git a/download.cc b/download.cc index fbe36e5..4aba83e 100644 --- a/download.cc +++ b/download.cc @@ -19,7 +19,7 @@ #include "csu_util/rfc1738.h" #include "download.h" - + #include "win32.h" #include <stdio.h> @@ -28,7 +28,6 @@ #include <vector> #include "resource.h" -#include "msg.h" #include "dialog.h" #include "geturl.h" #include "state.h" @@ -48,110 +47,13 @@ extern ThreeBarProgressPage Progress; -// Return true if selected checks pass, false if they don't and the -// user chooses to delete the file; otherwise throw an exception. -static bool -validateCachedPackage (const std::string& fullname, packagesource & pkgsource, - Feedback &feedback, bool check_hash, bool check_size) -{ - try - { - if (check_size) - pkgsource.check_size_and_cache (fullname); - if (check_hash) - pkgsource.check_hash (feedback); - return true; - } - catch (Exception *e) - { - pkgsource.set_cached (""); - const char *filename = fullname.c_str (); - if (strncmp (filename, "file://", 7) == 0) - filename += 7; - if (e->errNo() == APPERR_CORRUPT_PACKAGE - && yesno (feedback.owner(), IDS_QUERY_CORRUPT, filename) == IDYES) - remove (filename); - else - throw e; - } - return false; -} - -/* 0 if not cached; may throw exception if validation fails. - */ -int -check_for_cached (packagesource & pkgsource, Feedback &feedback, - bool mirror_mode, bool check_hash) -{ - /* If the packagesource doesn't have a filename, it can't possibly be in the - cache */ - if (!pkgsource.Canonical()) - { - return 0; - } - - /* Note that the cache dir is represented by a mirror site of file://local_dir */ - std::string prefix = "file://" + local_dir + "/"; - std::string fullname = prefix + pkgsource.Canonical(); - - if (mirror_mode) - { - /* Just assume correctness of mirror. */ - if (!pkgsource.Cached()) - pkgsource.set_cached (fullname); - return 1; - } - - // Already found one, which we can assume to have the right size. - if (pkgsource.Cached()) - { - if (validateCachedPackage (pkgsource.Cached(), pkgsource, feedback, - check_hash, false)) - return 1; - // If we get here, pkgsource.Cached() was corrupt and deleted. - pkgsource.set_cached (""); - } - - /* - 1) is there a legacy version in the cache dir available. - */ - if (io_stream::exists (fullname)) - { - if (validateCachedPackage (fullname, pkgsource, feedback, check_hash, true)) - return 1; - // If we get here, fullname was corrupt and deleted, but it - // might have been cached. - pkgsource.set_cached (""); - } - - /* - 2) is there a version from one of the selected mirror sites available ? - */ - for (packagesource::sitestype::const_iterator n = pkgsource.sites.begin(); - n != pkgsource.sites.end(); ++n) - { - std::string fullname = prefix + rfc1738_escape_part (n->key) + "/" + - pkgsource.Canonical (); - if (io_stream::exists(fullname)) - { - if (validateCachedPackage (fullname, pkgsource, feedback, check_hash, - true)) - return 1; - // If we get here, fullname was corrupt and deleted, but it - // might have been cached. - pkgsource.set_cached (""); - } - } - return 0; -} - /* download a file from a mirror site to the local cache. */ static int download_one (packagesource & pkgsource, Feedback &feedback) { try { - if (check_for_cached (pkgsource, feedback)) + if (pkgsource.check_for_cached(feedback)) return 0; } catch (Exception * e) @@ -295,7 +197,7 @@ do_download_thread (HINSTANCE h, HWND owner) try { - if (!check_for_cached (*version.source(), feedback)) + if (!(version.source()->check_for_cached(feedback))) total_download_bytes += version.source()->size; } catch (Exception * e) diff --git a/download.h b/download.h index 3f65153..e887c92 100644 --- a/download.h +++ b/download.h @@ -16,10 +16,4 @@ #ifndef SETUP_DOWNLOAD_H #define SETUP_DOWNLOAD_H -#include "Feedback.h" - -class packagesource; -int check_for_cached (packagesource & pkgsource, Feedback &feedback, - bool mirror_mode = false, bool check_hash = true); - #endif /* SETUP_DOWNLOAD_H */ diff --git a/package_meta.cc b/package_meta.cc index 0c5e2f5..4baea34 100644 --- a/package_meta.cc +++ b/package_meta.cc @@ -41,7 +41,6 @@ #include <functional> #include "Generic.h" -#include "download.h" #include "Exception.h" #include "resource.h" @@ -841,7 +840,7 @@ packagemeta::scan (const packageversion &pkg, bool mirror_mode, Feedback &feedba try { - if (!check_for_cached (*(pkg.source ()), feedback, mirror_mode, false) + if (!pkg.source()->check_for_cached(feedback, mirror_mode, false) && ::source == IDC_SOURCE_LOCALDIR) return false; } diff --git a/package_source.cc b/package_source.cc index a652a3b..bc90158 100644 --- a/package_source.cc +++ b/package_source.cc @@ -25,6 +25,9 @@ #include "filemanip.h" #include "io_stream.h" #include "Feedback.h" +#include "csu_util/rfc1738.h" +#include "resource.h" +#include "state.h" site::site (const std::string& newkey) : key(newkey) { @@ -185,3 +188,95 @@ packagesource::check_md5 (const std::string fullname, Feedback &feedback) const Log (LOG_BABBLE) << "MD5 verified OK: " << fullname << " " << md5.str() << endLog; } + +// Return true if selected checks pass, false if they don't and the +// user chooses to delete the file; otherwise throw an exception. +static bool +validateCachedPackage (const std::string& fullname, packagesource &pkgsource, + Feedback &feedback, bool check_hash, bool check_size) +{ + try + { + if (check_size) + pkgsource.check_size_and_cache (fullname); + if (check_hash) + pkgsource.check_hash (feedback); + return true; + } + catch (Exception *e) + { + pkgsource.set_cached (""); + const char *filename = fullname.c_str (); + if (strncmp (filename, "file://", 7) == 0) + filename += 7; + if (e->errNo() == APPERR_CORRUPT_PACKAGE + && yesno (feedback.owner(), IDS_QUERY_CORRUPT, filename) == IDYES) + remove (filename); + else + throw e; + } + return false; +} + +int +packagesource::check_for_cached (Feedback &feedback, bool mirror_mode, + bool check_hash) +{ + /* If the packagesource doesn't have a filename, it can't possibly be in the + cache */ + if (!Canonical()) + { + return 0; + } + + /* Note that the cache dir is represented by a mirror site of file://local_dir */ + std::string prefix = "file://" + local_dir + "/"; + std::string fullname = prefix + Canonical(); + + if (mirror_mode) + { + /* Just assume correctness of mirror. */ + if (!Cached()) + set_cached (fullname); + return 1; + } + + // Already found one, which we can assume to have the right size. + if (Cached()) + { + if (validateCachedPackage (Cached(), *this, feedback, check_hash, false)) + return 1; + // If we get here, this.Cached() was corrupt and deleted. + set_cached (""); + } + + /* + 1) is there a legacy version in the cache dir available. + */ + if (io_stream::exists (fullname)) + { + if (validateCachedPackage (fullname, *this, feedback, check_hash, true)) + return 1; + // If we get here, fullname was corrupt and deleted, but it + // might have been cached. + set_cached (""); + } + + /* + 2) is there a version from one of the selected mirror sites available ? + */ + for (packagesource::sitestype::const_iterator n = sites.begin(); + n != sites.end(); ++n) + { + std::string fullname = prefix + rfc1738_escape_part (n->key) + "/" + Canonical (); + if (io_stream::exists(fullname)) + { + if (validateCachedPackage (fullname, *this, feedback, check_hash, true)) + return 1; + // If we get here, fullname was corrupt and deleted, but it + // might have been cached. + set_cached (""); + } + } + return 0; +} diff --git a/package_source.h b/package_source.h index 663a603..52efc4b 100644 --- a/package_source.h +++ b/package_source.h @@ -85,6 +85,10 @@ public: typedef std::vector <site> sitestype; sitestype sites; + /* returns 0 if not cached; may throw exception if validation fails. */ + int check_for_cached (Feedback &feedback, bool mirror_mode = false, + bool check_hash = true); + private: std::string canonical; /* For progress reporting. */ -- 2.43.0