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
pull/1/head
Tatsuhiro Tsujikawa 2009-07-22 12:54:35 +00:00
parent 0d101d47c9
commit 65906c07fe
5 changed files with 213 additions and 175 deletions

View File

@ -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>
Check stream status after file is opened.

View File

@ -53,6 +53,8 @@
#include "Logger.h"
#include "a2functional.h"
#include "FileEntry.h"
#include "DlAbortEx.h"
#include "StringFormat.h"
namespace aria2 {
@ -112,21 +114,22 @@ void DHTAutoSaveCommand::save()
serializer.setNodes(nodes);
try {
{
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);
}
if(!File(tempFile).renameTo(dhtFile)) {
logger->error("Cannot move file from %s to %s.",
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) {
logger->error("Exception caught while saving DHT routing table to %s",
e, tempFile.c_str());
e, dhtFile.c_str());
}
}

View File

@ -56,16 +56,27 @@ 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)
{
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)
{
try {
char header[8];
memset(header, 0, sizeof(header));
// magic
@ -99,6 +110,7 @@ void DHTRoutingTableDeserializer::deserialize(std::istream& in)
// header
readBytes(buf, buf.size(), in, 8);
CHECK_STREAM(in, 8);
if(memcmp(header, buf, 8) == 0) {
version = 3;
} else if(memcmp(headerCompat, buf, 8) == 0) {
@ -114,29 +126,38 @@ void DHTRoutingTableDeserializer::deserialize(std::istream& in)
// time
if(version == 2) {
in.read(reinterpret_cast<char*>(&temp32), sizeof(temp32));
CHECK_STREAM(in, sizeof(temp32));
_serializedTime.setTimeInSec(ntohl(temp32));
// 4bytes reserved
readBytes(buf, buf.size(), in, 4);
CHECK_STREAM(in, 4);
} else {
in.read(reinterpret_cast<char*>(&temp64), sizeof(temp64));
CHECK_STREAM(in, sizeof(temp64));
_serializedTime.setTimeInSec(ntoh64(temp64));
}
// localnode
// 8bytes reserved
readBytes(buf, buf.size(), in, 8);
CHECK_STREAM(in, 8);
// localnode ID
readBytes(buf, buf.size(), in, DHT_ID_LENGTH);
CHECK_STREAM(in, DHT_ID_LENGTH);
SharedHandle<DHTNode> localNode(new DHTNode(buf));
// 4bytes reserved
readBytes(buf, buf.size(), in, 4);
CHECK_STREAM(in, 4);
// number of nodes
in.read(reinterpret_cast<char*>(&temp32), sizeof(temp32));
CHECK_STREAM(in, sizeof(temp32));
uint32_t numNodes = ntohl(temp32);
// 4bytes reserved
readBytes(buf, buf.size(), in, 4);
CHECK_STREAM(in, 4);
std::deque<SharedHandle<DHTNode> > nodes;
// nodes
for(size_t i = 0; i < numNodes; ++i) {
// Currently, only IPv4 addresses are supported.
@ -146,15 +167,19 @@ void DHTRoutingTableDeserializer::deserialize(std::istream& in)
if(peerInfoLen != 6) {
// skip this entry
readBytes(buf, buf.size(), in, 42+7+6);
CHECK_STREAM(in, 42+7+6);
continue;
}
// 7bytes reserved
readBytes(buf, buf.size(), in, 7);
CHECK_STREAM(in, 7);
// 6bytes compact peer info
readBytes(buf, buf.size(), in, 6);
CHECK_STREAM(in, 6);
if(memcmp(zero, buf, 6) == 0) {
// skip this entry
readBytes(buf, buf.size(), in, 42);
CHECK_STREAM(in, 42);
continue;
}
std::pair<std::string, uint16_t> peer =
@ -162,30 +187,30 @@ void DHTRoutingTableDeserializer::deserialize(std::istream& in)
if(peer.first.empty()) {
// skip this entry
readBytes(buf, buf.size(), in, 42);
CHECK_STREAM(in, 42);
continue;
}
// 2bytes reserved
readBytes(buf, buf.size(), in, 2);
CHECK_STREAM(in, 2);
// 16byte reserved
readBytes(buf, buf.size(), in, 16);
CHECK_STREAM(in, 16);
// localnode ID
readBytes(buf, buf.size(), in, DHT_ID_LENGTH);
CHECK_STREAM(in, DHT_ID_LENGTH);
SharedHandle<DHTNode> node(new DHTNode(buf));
node->setIPAddress(peer.first);
node->setPort(peer.second);
// 4bytes reserved
readBytes(buf, buf.size(), in, 4);
CHECK_STREAM(in, 4);
_nodes.push_back(node);
nodes.push_back(node);
}
_localNode = localNode;
} catch(std::ios::failure const& exception) {
_nodes.clear();
throw DL_ABORT_EX
(StringFormat("Failed to load DHT routing table. cause:%s",
strerror(errno)).str());
}
_nodes = nodes;
}
} // namespace aria2

View File

@ -76,7 +76,7 @@ void DHTRoutingTableSerializer::serialize(std::ostream& o)
char zero[16];
memset(zero, 0, sizeof(zero));
try {
o.write(header, 8);
// write save date
uint64_t ntime = hton64(Time().getTime());
@ -120,7 +120,9 @@ void DHTRoutingTableSerializer::serialize(std::ostream& o)
// 4bytes reserved
o.write(zero, 4);
}
} catch(std::ios::failure const& exception) {
o.flush();
if(!o) {
throw DL_ABORT_EX
(StringFormat("Failed to save DHT routing table. cause:%s",
strerror(errno)).str());

View File

@ -94,17 +94,17 @@ void DHTSetup::setup(std::deque<Command*>& commands,
DHTRoutingTableDeserializer deserializer;
std::string dhtFile = option->get(PREF_DHT_FILE_PATH);
if(File(dhtFile).isFile()) {
try {
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);
localNode = deserializer.getLocalNode();
} catch(RecoverableException& e) {
_logger->error("Exception caught while loading DHT routing table from %s",
e, dhtFile.c_str());
}
}
if(localNode.isNull()) {
localNode.reset(new DHTNode());
}