mirror of https://github.com/aria2/aria2
Rewritten DHTRoutingTable I/O using BufferedFile.
parent
ef27370243
commit
410d88710b
|
@ -49,6 +49,7 @@
|
|||
#include "util.h"
|
||||
#include "array_fun.h"
|
||||
#include "LogFactory.h"
|
||||
#include "BufferedFile.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
|
@ -57,17 +58,18 @@ DHTRoutingTableDeserializer::DHTRoutingTableDeserializer(int family):
|
|||
|
||||
DHTRoutingTableDeserializer::~DHTRoutingTableDeserializer() {}
|
||||
|
||||
#define FREAD_CHECK(ptr, count, fp) \
|
||||
if(fread((ptr), 1, (count), (fp)) != (count)) { \
|
||||
throw DL_ABORT_EX("Failed to load DHT routing table."); \
|
||||
#define READ_CHECK(fp, ptr, count) \
|
||||
if(fp.read((ptr), (count)) != (count)) { \
|
||||
throw DL_ABORT_EX("Failed to load DHT routing table."); \
|
||||
}
|
||||
|
||||
namespace {
|
||||
void readBytes(unsigned char* buf, size_t buflen,
|
||||
FILE* fp, size_t readlen)
|
||||
void readBytes(BufferedFile& fp,
|
||||
unsigned char* buf, size_t buflen,
|
||||
size_t readlen)
|
||||
{
|
||||
assert(readlen <= buflen);
|
||||
FREAD_CHECK(buf, readlen, fp);
|
||||
READ_CHECK(fp, buf, readlen);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
|
@ -75,12 +77,11 @@ void DHTRoutingTableDeserializer::deserialize(const std::string& filename)
|
|||
{
|
||||
A2_LOG_INFO(fmt("Loading DHT routing table from %s.",
|
||||
utf8ToNative(filename).c_str()));
|
||||
FILE* fp = a2fopen(utf8ToWChar(filename).c_str(), "rb");
|
||||
BufferedFile fp(filename, BufferedFile::READ);
|
||||
if(!fp) {
|
||||
throw DL_ABORT_EX(fmt("Failed to load DHT routing table from %s",
|
||||
utf8ToNative(filename).c_str()));
|
||||
}
|
||||
auto_delete_r<FILE*, int> deleter(fp, fclose);
|
||||
char header[8];
|
||||
memset(header, 0, sizeof(header));
|
||||
// magic
|
||||
|
@ -113,7 +114,7 @@ void DHTRoutingTableDeserializer::deserialize(const std::string& filename)
|
|||
array_wrapper<unsigned char, 255> buf;
|
||||
|
||||
// header
|
||||
readBytes(buf, buf.size(), fp, 8);
|
||||
readBytes(fp, buf, buf.size(), 8);
|
||||
if(memcmp(header, buf, 8) == 0) {
|
||||
version = 3;
|
||||
} else if(memcmp(headerCompat, buf, 8) == 0) {
|
||||
|
@ -129,29 +130,29 @@ void DHTRoutingTableDeserializer::deserialize(const std::string& filename)
|
|||
uint64_t temp64;
|
||||
// time
|
||||
if(version == 2) {
|
||||
FREAD_CHECK(&temp32, sizeof(temp32), fp);
|
||||
READ_CHECK(fp, &temp32, sizeof(temp32));
|
||||
serializedTime_.setTimeInSec(ntohl(temp32));
|
||||
// 4bytes reserved
|
||||
readBytes(buf, buf.size(), fp, 4);
|
||||
readBytes(fp, buf, buf.size(), 4);
|
||||
} else {
|
||||
FREAD_CHECK(&temp64, sizeof(temp64), fp);
|
||||
READ_CHECK(fp, &temp64, sizeof(temp64));
|
||||
serializedTime_.setTimeInSec(ntoh64(temp64));
|
||||
}
|
||||
|
||||
// localnode
|
||||
// 8bytes reserved
|
||||
readBytes(buf, buf.size(), fp, 8);
|
||||
readBytes(fp, buf, buf.size(), 8);
|
||||
// localnode ID
|
||||
readBytes(buf, buf.size(), fp, DHT_ID_LENGTH);
|
||||
readBytes(fp, buf, buf.size(), DHT_ID_LENGTH);
|
||||
SharedHandle<DHTNode> localNode(new DHTNode(buf));
|
||||
// 4bytes reserved
|
||||
readBytes(buf, buf.size(), fp, 4);
|
||||
readBytes(fp, buf, buf.size(), 4);
|
||||
|
||||
// number of nodes
|
||||
FREAD_CHECK(&temp32, sizeof(temp32), fp);
|
||||
READ_CHECK(fp, &temp32, sizeof(temp32));
|
||||
uint32_t numNodes = ntohl(temp32);
|
||||
// 4bytes reserved
|
||||
readBytes(buf, buf.size(), fp, 4);
|
||||
readBytes(fp, buf, buf.size(), 4);
|
||||
|
||||
std::vector<SharedHandle<DHTNode> > nodes;
|
||||
// nodes
|
||||
|
@ -159,38 +160,38 @@ void DHTRoutingTableDeserializer::deserialize(const std::string& filename)
|
|||
for(size_t i = 0; i < numNodes; ++i) {
|
||||
// 1byte compact peer info length
|
||||
uint8_t peerInfoLen;
|
||||
FREAD_CHECK(&peerInfoLen, sizeof(peerInfoLen), fp);
|
||||
READ_CHECK(fp, &peerInfoLen, sizeof(peerInfoLen));
|
||||
if(peerInfoLen != compactlen) {
|
||||
// skip this entry
|
||||
readBytes(buf, buf.size(), fp, 7+48);
|
||||
readBytes(fp, buf, buf.size(), 7+48);
|
||||
continue;
|
||||
}
|
||||
// 7bytes reserved
|
||||
readBytes(buf, buf.size(), fp, 7);
|
||||
readBytes(fp, buf, buf.size(), 7);
|
||||
// compactlen bytes compact peer info
|
||||
readBytes(buf, buf.size(), fp, compactlen);
|
||||
readBytes(fp, buf, buf.size(), compactlen);
|
||||
if(memcmp(zero, buf, compactlen) == 0) {
|
||||
// skip this entry
|
||||
readBytes(buf, buf.size(), fp, 48-compactlen);
|
||||
readBytes(fp, buf, buf.size(), 48-compactlen);
|
||||
continue;
|
||||
}
|
||||
std::pair<std::string, uint16_t> peer =
|
||||
bittorrent::unpackcompact(buf, family_);
|
||||
if(peer.first.empty()) {
|
||||
// skip this entry
|
||||
readBytes(buf, buf.size(), fp, 48-compactlen);
|
||||
readBytes(fp, buf, buf.size(), 48-compactlen);
|
||||
continue;
|
||||
}
|
||||
// 24-compactlen bytes reserved
|
||||
readBytes(buf, buf.size(), fp, 24-compactlen);
|
||||
readBytes(fp, buf, buf.size(), 24-compactlen);
|
||||
// node ID
|
||||
readBytes(buf, buf.size(), fp, DHT_ID_LENGTH);
|
||||
readBytes(fp, buf, buf.size(), DHT_ID_LENGTH);
|
||||
|
||||
SharedHandle<DHTNode> node(new DHTNode(buf));
|
||||
node->setIPAddress(peer.first);
|
||||
node->setPort(peer.second);
|
||||
// 4bytes reserved
|
||||
readBytes(buf, buf.size(), fp, 4);
|
||||
readBytes(fp, buf, buf.size(), 4);
|
||||
|
||||
nodes.push_back(node);
|
||||
}
|
||||
|
|
|
@ -48,6 +48,7 @@
|
|||
#include "fmt.h"
|
||||
#include "File.h"
|
||||
#include "LogFactory.h"
|
||||
#include "BufferedFile.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
|
@ -68,9 +69,8 @@ void DHTRoutingTableSerializer::setNodes
|
|||
nodes_ = nodes;
|
||||
}
|
||||
|
||||
#define FWRITE_CHECK(ptr, count, fp) \
|
||||
if(fwrite((ptr), 1, (count), (fp)) != (count)) { \
|
||||
fclose(fp); \
|
||||
#define WRITE_CHECK(fp, ptr, count) \
|
||||
if(fp.write((ptr), (count)) != (count)) { \
|
||||
throw DL_ABORT_EX(fmt("Failed to save DHT routing table to %s.", \
|
||||
utf8ToNative(filename).c_str())); \
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ void DHTRoutingTableSerializer::serialize(const std::string& filename)
|
|||
A2_LOG_INFO(fmt("Saving DHT routing table to %s.",
|
||||
utf8ToNative(filename).c_str()));
|
||||
std::string filenameTemp = filename+"__temp";
|
||||
FILE* fp = a2fopen(utf8ToWChar(filenameTemp).c_str(), "wb");
|
||||
BufferedFile fp(filenameTemp, BufferedFile::WRITE);
|
||||
if(!fp) {
|
||||
throw DL_ABORT_EX(fmt("Failed to save DHT routing table to %s.",
|
||||
utf8ToNative(filename).c_str()));
|
||||
|
@ -99,24 +99,24 @@ void DHTRoutingTableSerializer::serialize(const std::string& filename)
|
|||
char zero[18];
|
||||
memset(zero, 0, sizeof(zero));
|
||||
|
||||
FWRITE_CHECK(header, 8, fp);
|
||||
WRITE_CHECK(fp, header, 8);
|
||||
// write save date
|
||||
uint64_t ntime = hton64(Time().getTime());
|
||||
FWRITE_CHECK(&ntime, sizeof(ntime), fp);
|
||||
WRITE_CHECK(fp, &ntime, sizeof(ntime));
|
||||
|
||||
// localnode
|
||||
// 8bytes reserved
|
||||
FWRITE_CHECK(zero, 8, fp);
|
||||
WRITE_CHECK(fp, zero, 8);
|
||||
// 20bytes localnode ID
|
||||
FWRITE_CHECK(localNode_->getID(), DHT_ID_LENGTH, fp);
|
||||
WRITE_CHECK(fp, localNode_->getID(), DHT_ID_LENGTH);
|
||||
// 4bytes reserved
|
||||
FWRITE_CHECK(zero, 4, fp);
|
||||
WRITE_CHECK(fp, zero, 4);
|
||||
|
||||
// number of nodes
|
||||
uint32_t numNodes = htonl(nodes_.size());
|
||||
FWRITE_CHECK(&numNodes, sizeof(uint32_t), fp);
|
||||
WRITE_CHECK(fp, &numNodes, sizeof(uint32_t));
|
||||
// 4bytes reserved
|
||||
FWRITE_CHECK(zero, 4, fp);
|
||||
WRITE_CHECK(fp, zero, 4);
|
||||
|
||||
const int clen = bittorrent::getCompactLength(family_);
|
||||
// nodes
|
||||
|
@ -132,19 +132,19 @@ void DHTRoutingTableSerializer::serialize(const std::string& filename)
|
|||
}
|
||||
uint8_t clen1 = clen;
|
||||
// 1byte compact peer format length
|
||||
FWRITE_CHECK(&clen1, sizeof(clen1), fp);
|
||||
WRITE_CHECK(fp, &clen1, sizeof(clen1));
|
||||
// 7bytes reserved
|
||||
FWRITE_CHECK(zero, 7, fp);
|
||||
WRITE_CHECK(fp, zero, 7);
|
||||
// clen bytes compact peer
|
||||
FWRITE_CHECK(compactPeer, static_cast<size_t>(clen), fp);
|
||||
WRITE_CHECK(fp, compactPeer, static_cast<size_t>(clen));
|
||||
// 24-clen bytes reserved
|
||||
FWRITE_CHECK(zero, 24-clen, fp);
|
||||
WRITE_CHECK(fp, zero, 24-clen);
|
||||
// 20bytes: node ID
|
||||
FWRITE_CHECK(node->getID(), DHT_ID_LENGTH, fp);
|
||||
WRITE_CHECK(fp, node->getID(), DHT_ID_LENGTH);
|
||||
// 4bytes reserved
|
||||
FWRITE_CHECK(zero, 4, fp);
|
||||
WRITE_CHECK(fp, zero, 4);
|
||||
}
|
||||
if(fclose(fp) == EOF) {
|
||||
if(fp.close() == EOF) {
|
||||
throw DL_ABORT_EX(fmt("Failed to save DHT routing table to %s.",
|
||||
utf8ToNative(filename).c_str()));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue