| 97 | } |
| 98 | |
| 99 | std::shared_ptr<const i2p::data::IdentityEx> AddressBookFilesystemStorage::GetAddress (const i2p::data::IdentHash& ident) |
| 100 | { |
| 101 | auto ts = i2p::util::GetMonotonicSeconds (); |
| 102 | { |
| 103 | std::lock_guard<std::mutex> l(m_FullAddressCacheMutex); |
| 104 | auto it = m_FullAddressCache.find (ident); |
| 105 | if (it != m_FullAddressCache.end ()) |
| 106 | { |
| 107 | it->second.second = ts; |
| 108 | return std::make_shared<i2p::data::IdentityEx>(it->second.first.data (), it->second.first.size ()); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (!m_IsPersist) |
| 113 | { |
| 114 | LogPrint(eLogDebug, "Addressbook: Persistence is disabled"); |
| 115 | return nullptr; |
| 116 | } |
| 117 | std::string filename = storage.Path(ident.ToBase32()); |
| 118 | std::ifstream f(filename, std::ifstream::binary); |
| 119 | if (!f.is_open ()) |
| 120 | { |
| 121 | LogPrint(eLogDebug, "Addressbook: Requested, but not found: ", filename); |
| 122 | return nullptr; |
| 123 | } |
| 124 | |
| 125 | f.seekg (0,std::ios::end); |
| 126 | size_t len = f.tellg (); |
| 127 | if (len < i2p::data::DEFAULT_IDENTITY_SIZE) |
| 128 | { |
| 129 | LogPrint (eLogError, "Addressbook: File ", filename, " is too short: ", len); |
| 130 | return nullptr; |
| 131 | } |
| 132 | f.seekg(0, std::ios::beg); |
| 133 | std::vector<uint8_t> buf(len); |
| 134 | f.read((char *)buf.data (), len); |
| 135 | if (!f) |
| 136 | { |
| 137 | LogPrint (eLogError, "Addressbook: Couldn't read ", filename); |
| 138 | return nullptr; |
| 139 | } |
| 140 | { |
| 141 | std::lock_guard<std::mutex> l(m_FullAddressCacheMutex); |
| 142 | m_FullAddressCache.try_emplace (ident, buf, ts); |
| 143 | } |
| 144 | return std::make_shared<i2p::data::IdentityEx>(buf.data (), len); |
| 145 | } |
| 146 | |
| 147 | void AddressBookFilesystemStorage::AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address) |
| 148 | { |
no test coverage detected