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