mirror of
https://github.com/PurpleI2P/i2pd
synced 2024-11-10 00:00:29 +03:00
local addresses
This commit is contained in:
parent
7c8036807a
commit
803f11bebb
@ -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<std::string, i2p::data::IdentHash>& addresses);
|
||||
int LoadLocal (std::map<std::string, i2p::data::IdentHash>& addresses);
|
||||
int Save (const std::map<std::string, i2p::data::IdentHash>& 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<std::string, i2p::data::IdentHash>& 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<std::string, i2p::data::IdentHash>& addresses)
|
||||
int AddressBookFilesystemStorage::LoadFromFile (const std::string& filename, std::map<std::string, i2p::data::IdentHash>& 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<std::string, i2p::data::IdentHash>& 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<std::string, i2p::data::IdentHash>& 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)
|
||||
|
@ -37,6 +37,7 @@ namespace client
|
||||
|
||||
virtual bool Init () = 0;
|
||||
virtual int Load (std::map<std::string, i2p::data::IdentHash>& addresses) = 0;
|
||||
virtual int LoadLocal (std::map<std::string, i2p::data::IdentHash>& addresses) = 0;
|
||||
virtual int Save (const std::map<std::string, i2p::data::IdentHash>& 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<std::string, i2p::data::IdentHash> m_Addresses;
|
||||
std::map<std::string, i2p::data::IdentHash> m_Addresses, m_LocalAddresses;
|
||||
AddressBookStorage * m_Storage;
|
||||
volatile bool m_IsLoaded, m_IsDownloading;
|
||||
std::vector<AddressBookSubscription *> m_Subscriptions;
|
||||
|
15
FS.h
15
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<typename Storage, typename... Filename>
|
||||
std::string StorageRootPath (const Storage& storage, Filename... filenames)
|
||||
{
|
||||
std::stringstream s("");
|
||||
s << storage.GetRoot ();
|
||||
_ExpandPath(s, filenames...);
|
||||
|
||||
return s.str();
|
||||
}
|
||||
|
||||
} // fs
|
||||
} // i2p
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user