From 803f11bebb904008628fdf51d2a2a94c2850ab8e Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 16 Mar 2016 15:40:29 -0400 Subject: [PATCH] local addresses --- AddressBook.cpp | 55 +++++++++++++++++++++++++++++++++++-------------- AddressBook.h | 3 ++- FS.h | 15 ++++++++++++-- 3 files changed, 54 insertions(+), 19 deletions(-) diff --git a/AddressBook.cpp b/AddressBook.cpp index 2ffebe01..2a6fbaf0 100644 --- a/AddressBook.cpp +++ b/AddressBook.cpp @@ -24,7 +24,7 @@ namespace client { private: i2p::fs::HashedStorage storage; - std::string etagsPath, indexPath; + std::string etagsPath, indexPath, localPath; public: AddressBookFilesystemStorage (): storage("addressbook", "b", "", "b32") {}; @@ -34,22 +34,29 @@ namespace client bool Init (); int Load (std::map& addresses); + int LoadLocal (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 GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified); + private: + + int LoadFromFile (const std::string& filename, std::map& addresses); // returns -1 if can't open file, otherwise number of records + }; bool AddressBookFilesystemStorage::Init() { storage.SetPlace(i2p::fs::GetDataDir()); // init ETags - etagsPath = storage.GetRoot() + i2p::fs::dirSep + "etags"; + etagsPath = i2p::fs::StorageRootPath (storage, "etags"); if (!i2p::fs::Exists (etagsPath)) i2p::fs::CreateDirectory (etagsPath); + // init address files + indexPath = i2p::fs::StorageRootPath (storage, "addresses.csv"); + localPath = i2p::fs::StorageRootPath (storage, "local.csv"); // init storage - indexPath = storage.GetRoot() + i2p::fs::dirSep + "addresses.csv"; return storage.Init(i2p::data::GetBase32SubstitutionTable(), 32); } @@ -96,24 +103,18 @@ namespace client storage.Remove( ident.ToBase32() ); } - int AddressBookFilesystemStorage::Load (std::map& addresses) + int AddressBookFilesystemStorage::LoadFromFile (const std::string& filename, std::map& addresses) { int num = 0; - std::string s; - std::ifstream f (indexPath, std::ifstream::in); // in text mode - - if (f.is_open ()) { - LogPrint(eLogInfo, "Addressbook: using index file ", indexPath); - } else { - LogPrint(eLogWarning, "Addressbook: Can't open ", indexPath); - return 0; - } + std::ifstream f (filename, std::ifstream::in); // in text mode + if (!f) return -1; addresses.clear (); - while (!f.eof ()) { + while (!f.eof ()) + { + std::string s; getline(f, s); - if (!s.length()) - continue; // skip empty line + if (!s.length()) continue; // skip empty line std::size_t pos = s.find(','); if (pos != std::string::npos) @@ -127,8 +128,28 @@ namespace client num++; } } + return num; + } + int AddressBookFilesystemStorage::Load (std::map& addresses) + { + int num = LoadFromFile (indexPath, addresses); + if (num < 0) + { + LogPrint(eLogWarning, "Addressbook: Can't open ", indexPath); + return 0; + } + LogPrint(eLogInfo, "Addressbook: using index file ", indexPath); LogPrint (eLogInfo, "Addressbook: ", num, " addresses loaded from storage"); + + return num; + } + + int AddressBookFilesystemStorage::LoadLocal (std::map& addresses) + { + int num = LoadFromFile (localPath, addresses); + if (num < 0) return 0; + LogPrint (eLogInfo, "Addressbook: ", num, " local addresses loaded"); return num; } @@ -305,6 +326,8 @@ namespace client LoadHostsFromStream (f); m_IsLoaded = true; } + // load local + m_Storage->LoadLocal (m_Addresses); } void AddressBook::LoadHostsFromStream (std::istream& f) diff --git a/AddressBook.h b/AddressBook.h index 2a1e0737..98422fe1 100644 --- a/AddressBook.h +++ b/AddressBook.h @@ -37,6 +37,7 @@ namespace client virtual bool Init () = 0; virtual int Load (std::map& addresses) = 0; + virtual int LoadLocal (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; @@ -79,7 +80,7 @@ namespace client private: std::mutex m_AddressBookMutex; - std::map m_Addresses; + std::map m_Addresses, m_LocalAddresses; AddressBookStorage * m_Storage; volatile bool m_IsLoaded, m_IsDownloading; std::vector m_Subscriptions; diff --git a/FS.h b/FS.h index d7f246dc..0437ccf9 100644 --- a/FS.h +++ b/FS.h @@ -48,8 +48,8 @@ namespace fs { /** create subdirs in storage */ bool Init(const char* chars, size_t cnt); - const std::string & GetRoot() const { return this->root; } - const std::string & GetName() const { return this->name; } + const std::string & GetRoot() const { return root; } + const std::string & GetName() const { return name; } /** set directory where to place storage directory */ void SetPlace(const std::string & path); /** path to file with given ident */ @@ -138,6 +138,17 @@ namespace fs { return s.str(); } + + template + std::string StorageRootPath (const Storage& storage, Filename... filenames) + { + std::stringstream s(""); + s << storage.GetRoot (); + _ExpandPath(s, filenames...); + + return s.str(); + } + } // fs } // i2p