From 1a894abcff34e4828f14f274329bf9a2f828b20f Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 14 Mar 2016 16:05:57 -0400 Subject: [PATCH] persist etag for addressbook subscription --- AddressBook.cpp | 23 +++++++++++++++++++---- AddressBook.h | 4 +++- FS.cpp | 7 +++++++ FS.h | 2 ++ 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/AddressBook.cpp b/AddressBook.cpp index 3b3f85b4..4248ae32 100644 --- a/AddressBook.cpp +++ b/AddressBook.cpp @@ -24,7 +24,7 @@ namespace client { private: i2p::fs::HashedStorage storage; - std::string indexPath; + std::string etagsPath, indexPath; public: AddressBookFilesystemStorage (): storage("addressbook", "b", "", "b32") {}; @@ -35,11 +35,18 @@ namespace client bool Init (); int Load (std::map& addresses); int Save (const std::map& addresses); + + void SaveEtag (const i2p::data::IdentHash& subsciption, const std::string& etag, const std::string& lastModified); }; bool AddressBookFilesystemStorage::Init() - { + { storage.SetPlace(i2p::fs::GetDataDir()); + // init ETags + etagsPath = storage.GetRoot() + i2p::fs::dirSep + "etags"; + if (!i2p::fs::Exists (etagsPath)) + i2p::fs::CreateDirectory (etagsPath); + // init storage indexPath = storage.GetRoot() + i2p::fs::dirSep + "addresses.csv"; return storage.Init(i2p::data::GetBase32SubstitutionTable(), 32); } @@ -146,6 +153,14 @@ namespace client return num; } + void AddressBookFilesystemStorage::SaveEtag (const i2p::data::IdentHash& subscription, const std::string& etag, const std::string& lastModified) + { + std::string fname = etagsPath + i2p::fs::dirSep + subscription.ToBase32 () + ".txt"; + std::ofstream f (fname, std::ofstream::out | std::ofstream::trunc); + if (f) + f << etag << lastModified; + } + //--------------------------------------------------------------------- AddressBook::AddressBook (): m_Storage(new AddressBookFilesystemStorage), m_IsLoaded (false), m_IsDownloading (false), m_DefaultSubscription (nullptr), m_SubscriptionsUpdateTimer (nullptr) @@ -337,7 +352,7 @@ namespace client LogPrint (eLogError, "Addressbook: subscriptions already loaded"); } - void AddressBook::DownloadComplete (bool success) + void AddressBook::DownloadComplete (bool success, const i2p::data::IdentHash& subscription, const std::string& etag, const std::string& lastModified) { m_IsDownloading = false; int nextUpdateTimeout = CONTINIOUS_SUBSCRIPTION_RETRY_TIMEOUT; @@ -561,7 +576,7 @@ namespace client if (!success) LogPrint (eLogError, "Addressbook: download hosts.txt from ", m_Link, " failed"); - m_Book.DownloadComplete (success); + m_Book.DownloadComplete (success, ident, m_Etag, m_LastModified); } bool AddressBookSubscription::ProcessResponse (std::stringstream& s, bool isGzip) diff --git a/AddressBook.h b/AddressBook.h index 46df12d0..27034aad 100644 --- a/AddressBook.h +++ b/AddressBook.h @@ -38,6 +38,8 @@ namespace client virtual bool Init () = 0; virtual int Load (std::map& addresses) = 0; virtual int Save (const std::map& addresses) = 0; + + virtual void SaveEtag (const i2p::data::IdentHash& subscription, const std::string& etag, const std::string& lastModified) = 0; }; class AddressBookSubscription; @@ -56,7 +58,7 @@ namespace client void InsertAddress (std::shared_ptr address); void LoadHostsFromStream (std::istream& f); - void DownloadComplete (bool success); + void DownloadComplete (bool success, const i2p::data::IdentHash& subscription, const std::string& etag, const std::string& lastModified); //This method returns the ".b32.i2p" address std::string ToAddress(const i2p::data::IdentHash& ident) { return GetB32Address(ident); } std::string ToAddress(std::shared_ptr ident) { return ToAddress(ident->GetIdentHash ()); } diff --git a/FS.cpp b/FS.cpp index 19963195..380ab2e5 100644 --- a/FS.cpp +++ b/FS.cpp @@ -102,6 +102,13 @@ namespace fs { return boost::filesystem::remove(path); } + bool CreateDirectory (const std::string& path) + { + if (boost::filesystem::exists(path) && + boost::filesystem::is_directory (boost::filesystem::status (path))) return true; + return boost::filesystem::create_directory(path); + } + void HashedStorage::SetPlace(const std::string &path) { root = path + i2p::fs::dirSep + name; } diff --git a/FS.h b/FS.h index 833258b9..d7f246dc 100644 --- a/FS.h +++ b/FS.h @@ -108,6 +108,8 @@ namespace fs { * @return true if file exists, false otherwise */ bool Exists(const std::string & path); + + bool CreateDirectory (const std::string& path); template void _ExpandPath(std::stringstream & path, T c) {