diff --git a/NetDb.cpp b/NetDb.cpp index c3022181..b87fcebb 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -260,9 +260,9 @@ namespace data for (boost::filesystem::directory_iterator it1 (it->path ()); it1 != end; ++it1) { #if BOOST_VERSION > 10500 - RouterInfo * r = new RouterInfo (it1->path().string().c_str ()); + RouterInfo * r = new RouterInfo (it1->path().string()); #else - RouterInfo * r = new RouterInfo(it1->path().c_str()); + RouterInfo * r = new RouterInfo(it1->path()); #endif r->DeleteBuffer (); m_RouterInfos[r->GetIdentHash ()] = r; diff --git a/RouterContext.cpp b/RouterContext.cpp index 99579ec2..6697e1d7 100644 --- a/RouterContext.cpp +++ b/RouterContext.cpp @@ -79,7 +79,7 @@ namespace i2p m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag, CryptoPP::Integer (m_Keys.signingPrivateKey, 20)); - m_RouterInfo = i2p::data::RouterInfo (i2p::util::filesystem::GetFullPath (ROUTER_INFO).c_str ()); // TODO + m_RouterInfo = i2p::data::RouterInfo (i2p::util::filesystem::GetFullPath (ROUTER_INFO)); // TODO return true; } diff --git a/RouterInfo.cpp b/RouterInfo.cpp index 7e998070..6105a639 100644 --- a/RouterInfo.cpp +++ b/RouterInfo.cpp @@ -17,11 +17,12 @@ namespace i2p { namespace data { - RouterInfo::RouterInfo (const char * filename): - m_IsUpdated (false), m_IsUnreachable (false), m_SupportedTransports (0), m_Caps (0) + RouterInfo::RouterInfo (const std::string& fullPath): + m_FullPath (fullPath), m_IsUpdated (false), m_IsUnreachable (false), + m_SupportedTransports (0), m_Caps (0) { m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE]; - ReadFromFile (filename); + ReadFromFile (); } RouterInfo::RouterInfo (const uint8_t * buf, int len): @@ -63,24 +64,35 @@ namespace data m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); } - void RouterInfo::ReadFromFile (const char * filename) + bool RouterInfo::LoadFile () { - std::ifstream s(filename, std::ifstream::binary); + std::ifstream s(m_FullPath.c_str (), std::ifstream::binary); if (s.is_open ()) { s.seekg (0,std::ios::end); m_BufferLen = s.tellg (); if (m_BufferLen < 40) { - LogPrint("File", filename, " is malformed"); - return; + LogPrint("File", m_FullPath, " is malformed"); + return false; } s.seekg(0, std::ios::beg); + if (!m_Buffer) + m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE]; s.read((char *)m_Buffer, m_BufferLen); - ReadFromBuffer (); } else - LogPrint ("Can't open file ", filename); + { + LogPrint ("Can't open file ", m_FullPath); + return false; + } + return true; + } + + void RouterInfo::ReadFromFile () + { + if (LoadFile ()) + ReadFromBuffer (); } void RouterInfo::ReadFromBuffer () @@ -334,6 +346,16 @@ namespace data s.write (properties.str ().c_str (), properties.str ().size ()); } + const uint8_t * RouterInfo::GetBuffer () + { + if (!m_Buffer) + { + if (LoadFile ()) + LogPrint ("Buffer for ", m_IdentHashAbbreviation, " loaded from file"); + } + return m_Buffer; + } + void RouterInfo::CreateBuffer () { m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); // refresh timstamp @@ -350,6 +372,7 @@ namespace data void RouterInfo::SaveToFile (const std::string& fullPath) { + m_FullPath = fullPath; if (m_Buffer) { std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out); diff --git a/RouterInfo.h b/RouterInfo.h index 160583c1..65e166aa 100644 --- a/RouterInfo.h +++ b/RouterInfo.h @@ -63,8 +63,8 @@ namespace data std::vector introducers; }; - RouterInfo (const char * filename); - RouterInfo (): m_Buffer (nullptr) {}; + RouterInfo (const std::string& fullPath); + RouterInfo (): m_Buffer (nullptr) { m_IdentHashBase64[0] = 0; m_IdentHashAbbreviation[0] = 0; }; RouterInfo (const RouterInfo& ) = default; RouterInfo& operator=(const RouterInfo& ) = default; RouterInfo (const uint8_t * buf, int len); @@ -99,8 +99,9 @@ namespace data bool IsUnreachable () const { return m_IsUnreachable; }; const uint8_t * GetBuffer () const { return m_Buffer; }; - int GetBufferLen () const { return m_BufferLen; }; - + const uint8_t * GetBuffer (); // load if necessary + int GetBufferLen () const { return m_BufferLen; }; + void CreateBuffer (); void UpdateRoutingKey (); @@ -119,7 +120,8 @@ namespace data private: - void ReadFromFile (const char * filename); + bool LoadFile (); + void ReadFromFile (); void ReadFromStream (std::istream& s); void ReadFromBuffer (); void WriteToStream (std::ostream& s); @@ -131,6 +133,7 @@ namespace data private: + std::string m_FullPath; Identity m_RouterIdentity; IdentHash m_IdentHash; RoutingKey m_RoutingKey;