Centralize where download directory is chosen Use site download directory without trailing "%2f" if it doesn't already exist
2010-11-26 Jon TURNEY <jon.tur...@dronecode.org.uk> * package_source.h (site): Add get_local_path() method and storage * package_source.cc (get_local_path): Implement get_local_path() method, to determine local directory to use for downloads from site. * ini.cc (do_remote_ini): Use get_local_path() * download.cc (check_for_cached, download_one): Use get_local_path() Signed-off-by: Jon TURNEY <jon.tur...@dronecode.org.uk> --- download.cc | 7 ++----- ini.cc | 5 ++--- package_source.cc | 36 +++++++++++++++++++++++++++++++++++- package_source.h | 7 ++++++- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/download.cc b/download.cc index 3567715..e9b4048 100644 --- a/download.cc +++ b/download.cc @@ -108,8 +108,7 @@ check_for_cached (packagesource & pkgsource) 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 (); + std::string fullname = "file://" + n->get_local_path() + "/" + pkgsource.Canonical (); if (io_stream::exists(fullname) == IO_STREAM_EXISTS_FILE) { if (validateCachedPackage (fullname, pkgsource)) @@ -150,9 +149,7 @@ download_one (packagesource & pkgsource, HWND owner) for (packagesource::sitestype::const_iterator n = pkgsource.sites.begin(); n != pkgsource.sites.end() && !success; ++n) { - const std::string local = local_dir + "/" + - rfc1738_escape_part (n->key) + "/" + - pkgsource.Canonical (); + const std::string local = n->get_local_path() + "/" + pkgsource.Canonical (); io_stream::mkpath_p (PATH_TO_FILE, "file://" + local, 0); if (get_url_to_file(n->key + "/" + pkgsource.Canonical (), diff --git a/ini.cc b/ini.cc index b983438..b9e256d 100644 --- a/ini.cc +++ b/ini.cc @@ -262,9 +262,8 @@ do_remote_ini (HWND owner) else { /* save known-good setup.ini locally */ - const std::string fp = "file://" + local_dir + "/" + - rfc1738_escape_part (n->url) + - "/" + SETUP_INI_FILENAME; + site parse_site(n->url); + const std::string fp = "file://" + parse_site.get_local_path() + "/" + SETUP_INI_FILENAME; io_stream::mkpath_p (PATH_TO_FILE, fp, 0); if (io_stream *out = io_stream::open (fp, "wb", 0)) { diff --git a/package_source.cc b/package_source.cc index 8accb1f..5248cb8 100644 --- a/package_source.cc +++ b/package_source.cc @@ -25,11 +25,45 @@ static const char *cvsid = #include <stdlib.h> #include <strings.h> #include "package_source.h" +#include "LogSingleton.h" +#include "state.h" +#include "io_stream.h" +#include "csu_util/rfc1738.h" site::site (const std::string& newkey) : key(newkey) { }; - + +const std::string & +site::get_local_path(void) const +{ + /* if we haven't already determined the local_path for this site... */ + if (local_path.empty()) + { + /* check for preexisting directory, if so use that... */ + local_path = local_dir + "/" + rfc1738_escape_part(key); + + if (!io_stream::exists("file://" + local_path)) + { + std::string temp = key; + + /* ... otherwise, strip any terminal '/' (which should always be present in canonical URL) */ + if (temp.at(temp.length()-1) == '/') + temp.erase(temp.length()-1); + + local_path = local_dir + "/" + rfc1738_escape_part(temp); + + log (LOG_BABBLE) << "Will use " << local_path << " for storing downloads" << endLog; + } + else + { + log (LOG_BABBLE) << "Found " << local_path << ", will use for storing downloads" << endLog; + } + } + + return local_path; +} + void packagesource::set_canonical (char const *fn) { diff --git a/package_source.h b/package_source.h index 9dea667..45a4a3c 100644 --- a/package_source.h +++ b/package_source.h @@ -47,11 +47,16 @@ class site public: site (const std::string& newkey); ~site () {} - std::string key; + bool operator == (site const &rhs) { return casecompare(key, rhs.key) == 0; } + + const std::string & get_local_path() const; + + std::string key; + mutable std::string local_path; }; class packagesource -- 1.7.2.3