mirror of https://github.com/aria2/aria2
2009-07-22 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Rewritten stream error handling * src/DHTAutoSaveCommand.cc * src/DHTRoutingTableDeserializer.cc * src/DHTRoutingTableSerializer.cc * src/DHTSetup.ccpull/1/head
parent
0d101d47c9
commit
65906c07fe
|
@ -1,3 +1,11 @@
|
||||||
|
2009-07-22 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
|
Rewritten stream error handling
|
||||||
|
* src/DHTAutoSaveCommand.cc
|
||||||
|
* src/DHTRoutingTableDeserializer.cc
|
||||||
|
* src/DHTRoutingTableSerializer.cc
|
||||||
|
* src/DHTSetup.cc
|
||||||
|
|
||||||
2009-07-22 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
2009-07-22 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
Check stream status after file is opened.
|
Check stream status after file is opened.
|
||||||
|
|
|
@ -53,6 +53,8 @@
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
#include "a2functional.h"
|
#include "a2functional.h"
|
||||||
#include "FileEntry.h"
|
#include "FileEntry.h"
|
||||||
|
#include "DlAbortEx.h"
|
||||||
|
#include "StringFormat.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
|
@ -112,21 +114,22 @@ void DHTAutoSaveCommand::save()
|
||||||
serializer.setNodes(nodes);
|
serializer.setNodes(nodes);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
{
|
||||||
std::ofstream o(tempFile.c_str(), std::ios::out|std::ios::binary);
|
std::ofstream o(tempFile.c_str(), std::ios::out|std::ios::binary);
|
||||||
o.exceptions(std::ios::failbit);
|
if(!o) {
|
||||||
|
throw DL_ABORT_EX
|
||||||
|
(StringFormat("Failed to save DHT routing table to %s. cause:%s",
|
||||||
|
dhtFile.c_str(), strerror(errno)).str());
|
||||||
|
}
|
||||||
serializer.serialize(o);
|
serializer.serialize(o);
|
||||||
|
}
|
||||||
if(!File(tempFile).renameTo(dhtFile)) {
|
if(!File(tempFile).renameTo(dhtFile)) {
|
||||||
logger->error("Cannot move file from %s to %s.",
|
logger->error("Cannot move file from %s to %s.",
|
||||||
tempFile.c_str(), dhtFile.c_str());
|
tempFile.c_str(), dhtFile.c_str());
|
||||||
}
|
}
|
||||||
} catch(std::ios::failure const& e) {
|
|
||||||
logger->error("Failed to save DHT routing table to %s. cause:%s",
|
|
||||||
tempFile.c_str(), strerror(errno));
|
|
||||||
} catch(RecoverableException& e) {
|
} catch(RecoverableException& e) {
|
||||||
logger->error("Exception caught while saving DHT routing table to %s",
|
logger->error("Exception caught while saving DHT routing table to %s",
|
||||||
e, tempFile.c_str());
|
e, dhtFile.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,16 +56,27 @@ DHTRoutingTableDeserializer::DHTRoutingTableDeserializer() {}
|
||||||
|
|
||||||
DHTRoutingTableDeserializer::~DHTRoutingTableDeserializer() {}
|
DHTRoutingTableDeserializer::~DHTRoutingTableDeserializer() {}
|
||||||
|
|
||||||
static std::istream& readBytes(unsigned char* buf, size_t buflen,
|
static void readBytes(unsigned char* buf, size_t buflen,
|
||||||
std::istream& in, size_t readlen)
|
std::istream& in, size_t readlen)
|
||||||
{
|
{
|
||||||
assert(readlen <= buflen);
|
assert(readlen <= buflen);
|
||||||
return in.read(reinterpret_cast<char*>(buf), readlen);
|
in.read(reinterpret_cast<char*>(buf), readlen);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define CHECK_STREAM(in, length) \
|
||||||
|
if(in.gcount() != length) { \
|
||||||
|
throw DL_ABORT_EX \
|
||||||
|
(StringFormat("Failed to load DHT routing table. cause:%s", \
|
||||||
|
"Unexpected EOF").str()); \
|
||||||
|
} \
|
||||||
|
if(!in) { \
|
||||||
|
throw DL_ABORT_EX \
|
||||||
|
(StringFormat("Failed to load DHT routing table. cause:%s", \
|
||||||
|
strerror(errno)).str()); \
|
||||||
}
|
}
|
||||||
|
|
||||||
void DHTRoutingTableDeserializer::deserialize(std::istream& in)
|
void DHTRoutingTableDeserializer::deserialize(std::istream& in)
|
||||||
{
|
{
|
||||||
try {
|
|
||||||
char header[8];
|
char header[8];
|
||||||
memset(header, 0, sizeof(header));
|
memset(header, 0, sizeof(header));
|
||||||
// magic
|
// magic
|
||||||
|
@ -99,6 +110,7 @@ void DHTRoutingTableDeserializer::deserialize(std::istream& in)
|
||||||
|
|
||||||
// header
|
// header
|
||||||
readBytes(buf, buf.size(), in, 8);
|
readBytes(buf, buf.size(), in, 8);
|
||||||
|
CHECK_STREAM(in, 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) {
|
||||||
|
@ -114,29 +126,38 @@ void DHTRoutingTableDeserializer::deserialize(std::istream& in)
|
||||||
// time
|
// time
|
||||||
if(version == 2) {
|
if(version == 2) {
|
||||||
in.read(reinterpret_cast<char*>(&temp32), sizeof(temp32));
|
in.read(reinterpret_cast<char*>(&temp32), sizeof(temp32));
|
||||||
|
CHECK_STREAM(in, sizeof(temp32));
|
||||||
_serializedTime.setTimeInSec(ntohl(temp32));
|
_serializedTime.setTimeInSec(ntohl(temp32));
|
||||||
// 4bytes reserved
|
// 4bytes reserved
|
||||||
readBytes(buf, buf.size(), in, 4);
|
readBytes(buf, buf.size(), in, 4);
|
||||||
|
CHECK_STREAM(in, 4);
|
||||||
} else {
|
} else {
|
||||||
in.read(reinterpret_cast<char*>(&temp64), sizeof(temp64));
|
in.read(reinterpret_cast<char*>(&temp64), sizeof(temp64));
|
||||||
|
CHECK_STREAM(in, sizeof(temp64));
|
||||||
_serializedTime.setTimeInSec(ntoh64(temp64));
|
_serializedTime.setTimeInSec(ntoh64(temp64));
|
||||||
}
|
}
|
||||||
|
|
||||||
// localnode
|
// localnode
|
||||||
// 8bytes reserved
|
// 8bytes reserved
|
||||||
readBytes(buf, buf.size(), in, 8);
|
readBytes(buf, buf.size(), in, 8);
|
||||||
|
CHECK_STREAM(in, 8);
|
||||||
// localnode ID
|
// localnode ID
|
||||||
readBytes(buf, buf.size(), in, DHT_ID_LENGTH);
|
readBytes(buf, buf.size(), in, DHT_ID_LENGTH);
|
||||||
|
CHECK_STREAM(in, DHT_ID_LENGTH);
|
||||||
SharedHandle<DHTNode> localNode(new DHTNode(buf));
|
SharedHandle<DHTNode> localNode(new DHTNode(buf));
|
||||||
// 4bytes reserved
|
// 4bytes reserved
|
||||||
readBytes(buf, buf.size(), in, 4);
|
readBytes(buf, buf.size(), in, 4);
|
||||||
|
CHECK_STREAM(in, 4);
|
||||||
|
|
||||||
// number of nodes
|
// number of nodes
|
||||||
in.read(reinterpret_cast<char*>(&temp32), sizeof(temp32));
|
in.read(reinterpret_cast<char*>(&temp32), sizeof(temp32));
|
||||||
|
CHECK_STREAM(in, sizeof(temp32));
|
||||||
uint32_t numNodes = ntohl(temp32);
|
uint32_t numNodes = ntohl(temp32);
|
||||||
// 4bytes reserved
|
// 4bytes reserved
|
||||||
readBytes(buf, buf.size(), in, 4);
|
readBytes(buf, buf.size(), in, 4);
|
||||||
|
CHECK_STREAM(in, 4);
|
||||||
|
|
||||||
|
std::deque<SharedHandle<DHTNode> > nodes;
|
||||||
// nodes
|
// nodes
|
||||||
for(size_t i = 0; i < numNodes; ++i) {
|
for(size_t i = 0; i < numNodes; ++i) {
|
||||||
// Currently, only IPv4 addresses are supported.
|
// Currently, only IPv4 addresses are supported.
|
||||||
|
@ -146,15 +167,19 @@ void DHTRoutingTableDeserializer::deserialize(std::istream& in)
|
||||||
if(peerInfoLen != 6) {
|
if(peerInfoLen != 6) {
|
||||||
// skip this entry
|
// skip this entry
|
||||||
readBytes(buf, buf.size(), in, 42+7+6);
|
readBytes(buf, buf.size(), in, 42+7+6);
|
||||||
|
CHECK_STREAM(in, 42+7+6);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// 7bytes reserved
|
// 7bytes reserved
|
||||||
readBytes(buf, buf.size(), in, 7);
|
readBytes(buf, buf.size(), in, 7);
|
||||||
|
CHECK_STREAM(in, 7);
|
||||||
// 6bytes compact peer info
|
// 6bytes compact peer info
|
||||||
readBytes(buf, buf.size(), in, 6);
|
readBytes(buf, buf.size(), in, 6);
|
||||||
|
CHECK_STREAM(in, 6);
|
||||||
if(memcmp(zero, buf, 6) == 0) {
|
if(memcmp(zero, buf, 6) == 0) {
|
||||||
// skip this entry
|
// skip this entry
|
||||||
readBytes(buf, buf.size(), in, 42);
|
readBytes(buf, buf.size(), in, 42);
|
||||||
|
CHECK_STREAM(in, 42);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::pair<std::string, uint16_t> peer =
|
std::pair<std::string, uint16_t> peer =
|
||||||
|
@ -162,30 +187,30 @@ void DHTRoutingTableDeserializer::deserialize(std::istream& in)
|
||||||
if(peer.first.empty()) {
|
if(peer.first.empty()) {
|
||||||
// skip this entry
|
// skip this entry
|
||||||
readBytes(buf, buf.size(), in, 42);
|
readBytes(buf, buf.size(), in, 42);
|
||||||
|
CHECK_STREAM(in, 42);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// 2bytes reserved
|
// 2bytes reserved
|
||||||
readBytes(buf, buf.size(), in, 2);
|
readBytes(buf, buf.size(), in, 2);
|
||||||
|
CHECK_STREAM(in, 2);
|
||||||
// 16byte reserved
|
// 16byte reserved
|
||||||
readBytes(buf, buf.size(), in, 16);
|
readBytes(buf, buf.size(), in, 16);
|
||||||
|
CHECK_STREAM(in, 16);
|
||||||
// localnode ID
|
// localnode ID
|
||||||
readBytes(buf, buf.size(), in, DHT_ID_LENGTH);
|
readBytes(buf, buf.size(), in, DHT_ID_LENGTH);
|
||||||
|
CHECK_STREAM(in, 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(), in, 4);
|
readBytes(buf, buf.size(), in, 4);
|
||||||
|
CHECK_STREAM(in, 4);
|
||||||
|
|
||||||
_nodes.push_back(node);
|
nodes.push_back(node);
|
||||||
}
|
}
|
||||||
_localNode = localNode;
|
_localNode = localNode;
|
||||||
} catch(std::ios::failure const& exception) {
|
_nodes = nodes;
|
||||||
_nodes.clear();
|
|
||||||
throw DL_ABORT_EX
|
|
||||||
(StringFormat("Failed to load DHT routing table. cause:%s",
|
|
||||||
strerror(errno)).str());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -76,7 +76,7 @@ void DHTRoutingTableSerializer::serialize(std::ostream& o)
|
||||||
|
|
||||||
char zero[16];
|
char zero[16];
|
||||||
memset(zero, 0, sizeof(zero));
|
memset(zero, 0, sizeof(zero));
|
||||||
try {
|
|
||||||
o.write(header, 8);
|
o.write(header, 8);
|
||||||
// write save date
|
// write save date
|
||||||
uint64_t ntime = hton64(Time().getTime());
|
uint64_t ntime = hton64(Time().getTime());
|
||||||
|
@ -120,7 +120,9 @@ void DHTRoutingTableSerializer::serialize(std::ostream& o)
|
||||||
// 4bytes reserved
|
// 4bytes reserved
|
||||||
o.write(zero, 4);
|
o.write(zero, 4);
|
||||||
}
|
}
|
||||||
} catch(std::ios::failure const& exception) {
|
|
||||||
|
o.flush();
|
||||||
|
if(!o) {
|
||||||
throw DL_ABORT_EX
|
throw DL_ABORT_EX
|
||||||
(StringFormat("Failed to save DHT routing table. cause:%s",
|
(StringFormat("Failed to save DHT routing table. cause:%s",
|
||||||
strerror(errno)).str());
|
strerror(errno)).str());
|
||||||
|
|
|
@ -94,17 +94,17 @@ void DHTSetup::setup(std::deque<Command*>& commands,
|
||||||
|
|
||||||
DHTRoutingTableDeserializer deserializer;
|
DHTRoutingTableDeserializer deserializer;
|
||||||
std::string dhtFile = option->get(PREF_DHT_FILE_PATH);
|
std::string dhtFile = option->get(PREF_DHT_FILE_PATH);
|
||||||
if(File(dhtFile).isFile()) {
|
|
||||||
try {
|
try {
|
||||||
std::ifstream in(dhtFile.c_str(), std::ios::binary);
|
std::ifstream in(dhtFile.c_str(), std::ios::binary);
|
||||||
in.exceptions(std::ios::failbit);
|
if(!in) {
|
||||||
|
throw DL_ABORT_EX("Could not open file");
|
||||||
|
}
|
||||||
deserializer.deserialize(in);
|
deserializer.deserialize(in);
|
||||||
localNode = deserializer.getLocalNode();
|
localNode = deserializer.getLocalNode();
|
||||||
} catch(RecoverableException& e) {
|
} catch(RecoverableException& e) {
|
||||||
_logger->error("Exception caught while loading DHT routing table from %s",
|
_logger->error("Exception caught while loading DHT routing table from %s",
|
||||||
e, dhtFile.c_str());
|
e, dhtFile.c_str());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if(localNode.isNull()) {
|
if(localNode.isNull()) {
|
||||||
localNode.reset(new DHTNode());
|
localNode.reset(new DHTNode());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue