mirror of https://github.com/aria2/aria2
2009-07-22 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Rewritten stream error handling * src/DefaultBtProgressInfoFile.ccpull/1/head
parent
0d1d88257c
commit
cec451951d
|
@ -1,3 +1,8 @@
|
|||
2009-07-22 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||
|
||||
Rewritten stream error handling
|
||||
* src/DefaultBtProgressInfoFile.cc
|
||||
|
||||
2009-07-22 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||
|
||||
Make sure that stream is closed before renaming file. Unit test
|
||||
|
|
|
@ -102,12 +102,12 @@ bool DefaultBtProgressInfoFile::isTorrentDownload()
|
|||
}
|
||||
|
||||
// Since version 0001, Integers are saved in binary form, network byte order.
|
||||
void DefaultBtProgressInfoFile::save() {
|
||||
void DefaultBtProgressInfoFile::save()
|
||||
{
|
||||
_logger->info(MSG_SAVING_SEGMENT_FILE, _filename.c_str());
|
||||
std::string filenameTemp = _filename+"__temp";
|
||||
{
|
||||
std::ofstream o(filenameTemp.c_str(), std::ios::out|std::ios::binary);
|
||||
try {
|
||||
o.exceptions(std::ios::failbit);
|
||||
bool torrentDownload = isTorrentDownload();
|
||||
// file version: 16 bits
|
||||
// values: '1'
|
||||
|
@ -182,20 +182,29 @@ void DefaultBtProgressInfoFile::save() {
|
|||
o.write(reinterpret_cast<const char*>((*itr)->getBitfield()),
|
||||
(*itr)->getBitfieldLength());
|
||||
}
|
||||
|
||||
o.close();
|
||||
_logger->info(MSG_SAVED_SEGMENT_FILE);
|
||||
} catch(std::ios::failure const& exception) {
|
||||
// TODO std::ios::failure doesn't give us the reasons of failure...
|
||||
o.flush();
|
||||
if(!o) {
|
||||
throw DL_ABORT_EX(StringFormat(EX_SEGMENT_FILE_WRITE,
|
||||
_filename.c_str(), strerror(errno)).str());
|
||||
}
|
||||
_logger->info(MSG_SAVED_SEGMENT_FILE);
|
||||
}
|
||||
if(!File(filenameTemp).renameTo(_filename)) {
|
||||
throw DL_ABORT_EX(StringFormat(EX_SEGMENT_FILE_WRITE,
|
||||
_filename.c_str(), strerror(errno)).str());
|
||||
}
|
||||
}
|
||||
|
||||
#define CHECK_STREAM(in, length) \
|
||||
if(in.gcount() != length) { \
|
||||
throw DL_ABORT_EX(StringFormat(EX_SEGMENT_FILE_READ, \
|
||||
_filename.c_str(),"Unexpected EOF").str()); \
|
||||
} \
|
||||
if(!in) { \
|
||||
throw DL_ABORT_EX(StringFormat(EX_SEGMENT_FILE_READ, \
|
||||
_filename.c_str(), strerror(errno)).str()); \
|
||||
}
|
||||
|
||||
// It is assumed that integers are saved as:
|
||||
// 1) host byte order if version == 0000
|
||||
// 2) network byte order if version == 0001
|
||||
|
@ -203,10 +212,10 @@ void DefaultBtProgressInfoFile::load()
|
|||
{
|
||||
_logger->info(MSG_LOADING_SEGMENT_FILE, _filename.c_str());
|
||||
std::ifstream in(_filename.c_str(), std::ios::in|std::ios::binary);
|
||||
try {
|
||||
in.exceptions(std::ios::failbit);
|
||||
|
||||
unsigned char versionBuf[2];
|
||||
in.read((char*)versionBuf, sizeof(versionBuf));
|
||||
CHECK_STREAM(in, sizeof(versionBuf));
|
||||
std::string versionHex = Util::toHex(versionBuf, sizeof(versionBuf));
|
||||
int version;
|
||||
if(DefaultBtProgressInfoFile::V0000 == versionHex) {
|
||||
|
@ -220,7 +229,7 @@ void DefaultBtProgressInfoFile::load()
|
|||
}
|
||||
unsigned char extension[4];
|
||||
in.read((char*)extension, sizeof(extension));
|
||||
|
||||
CHECK_STREAM(in, sizeof(extension));
|
||||
bool infoHashCheckEnabled = false;
|
||||
if(extension[3]&1 && isTorrentDownload()) {
|
||||
infoHashCheckEnabled = true;
|
||||
|
@ -229,6 +238,7 @@ void DefaultBtProgressInfoFile::load()
|
|||
|
||||
uint32_t infoHashLength;
|
||||
in.read(reinterpret_cast<char*>(&infoHashLength), sizeof(infoHashLength));
|
||||
CHECK_STREAM(in, sizeof(infoHashLength));
|
||||
if(version >= 1) {
|
||||
infoHashLength = ntohl(infoHashLength);
|
||||
}
|
||||
|
@ -241,6 +251,7 @@ void DefaultBtProgressInfoFile::load()
|
|||
array_ptr<unsigned char> savedInfoHash(new unsigned char[infoHashLength]);
|
||||
in.read(reinterpret_cast<char*>
|
||||
(static_cast<unsigned char*>(savedInfoHash)), infoHashLength);
|
||||
CHECK_STREAM(in, infoHashLength);
|
||||
if(infoHashCheckEnabled) {
|
||||
const unsigned char* infoHash = bittorrent::getInfoHash(_dctx);
|
||||
if(infoHashLength != INFO_HASH_LENGTH ||
|
||||
|
@ -256,12 +267,14 @@ void DefaultBtProgressInfoFile::load()
|
|||
|
||||
uint32_t pieceLength;
|
||||
in.read(reinterpret_cast<char*>(&pieceLength), sizeof(pieceLength));
|
||||
CHECK_STREAM(in, sizeof(pieceLength));
|
||||
if(version >= 1) {
|
||||
pieceLength = ntohl(pieceLength);
|
||||
}
|
||||
|
||||
uint64_t totalLength;
|
||||
in.read(reinterpret_cast<char*>(&totalLength), sizeof(totalLength));
|
||||
CHECK_STREAM(in, sizeof(totalLength));
|
||||
if(version >= 1) {
|
||||
totalLength = ntoh64(totalLength);
|
||||
}
|
||||
|
@ -273,6 +286,7 @@ void DefaultBtProgressInfoFile::load()
|
|||
}
|
||||
uint64_t uploadLength;
|
||||
in.read(reinterpret_cast<char*>(&uploadLength), sizeof(uploadLength));
|
||||
CHECK_STREAM(in, sizeof(uploadLength));
|
||||
if(version >= 1) {
|
||||
uploadLength = ntoh64(uploadLength);
|
||||
}
|
||||
|
@ -284,6 +298,7 @@ void DefaultBtProgressInfoFile::load()
|
|||
// TODO implement the conversion mechanism between different piece length.
|
||||
uint32_t bitfieldLength;
|
||||
in.read(reinterpret_cast<char*>(&bitfieldLength), sizeof(bitfieldLength));
|
||||
CHECK_STREAM(in, sizeof(bitfieldLength));
|
||||
if(version >= 1) {
|
||||
bitfieldLength = ntohl(bitfieldLength);
|
||||
}
|
||||
|
@ -299,13 +314,14 @@ void DefaultBtProgressInfoFile::load()
|
|||
array_ptr<unsigned char> savedBitfield(new unsigned char[bitfieldLength]);
|
||||
in.read(reinterpret_cast<char*>
|
||||
(static_cast<unsigned char*>(savedBitfield)), bitfieldLength);
|
||||
|
||||
CHECK_STREAM(in, bitfieldLength);
|
||||
if(pieceLength == _dctx->getPieceLength()) {
|
||||
_pieceStorage->setBitfield(savedBitfield, bitfieldLength);
|
||||
|
||||
uint32_t numInFlightPiece;
|
||||
in.read(reinterpret_cast<char*>(&numInFlightPiece),
|
||||
sizeof(numInFlightPiece));
|
||||
CHECK_STREAM(in, sizeof(numInFlightPiece));
|
||||
if(version >= 1) {
|
||||
numInFlightPiece = ntohl(numInFlightPiece);
|
||||
}
|
||||
|
@ -313,6 +329,7 @@ void DefaultBtProgressInfoFile::load()
|
|||
while(numInFlightPiece--) {
|
||||
uint32_t index;
|
||||
in.read(reinterpret_cast<char*>(&index), sizeof(index));
|
||||
CHECK_STREAM(in, sizeof(index));
|
||||
if(version >= 1) {
|
||||
index = ntohl(index);
|
||||
}
|
||||
|
@ -322,6 +339,7 @@ void DefaultBtProgressInfoFile::load()
|
|||
}
|
||||
uint32_t length;
|
||||
in.read(reinterpret_cast<char*>(&length), sizeof(length));
|
||||
CHECK_STREAM(in, sizeof(length));
|
||||
if(version >= 1) {
|
||||
length = ntohl(length);
|
||||
}
|
||||
|
@ -333,6 +351,7 @@ void DefaultBtProgressInfoFile::load()
|
|||
uint32_t bitfieldLength;
|
||||
in.read(reinterpret_cast<char*>(&bitfieldLength),
|
||||
sizeof(bitfieldLength));
|
||||
CHECK_STREAM(in, sizeof(bitfieldLength));
|
||||
if(version >= 1) {
|
||||
bitfieldLength = ntohl(bitfieldLength);
|
||||
}
|
||||
|
@ -346,6 +365,7 @@ void DefaultBtProgressInfoFile::load()
|
|||
(new unsigned char[bitfieldLength]);
|
||||
in.read(reinterpret_cast<char*>
|
||||
(static_cast<unsigned char*>(pieceBitfield)), bitfieldLength);
|
||||
CHECK_STREAM(in, bitfieldLength);
|
||||
piece->setBitfield(pieceBitfield, bitfieldLength);
|
||||
|
||||
#ifdef ENABLE_MESSAGE_DIGEST
|
||||
|
@ -361,6 +381,7 @@ void DefaultBtProgressInfoFile::load()
|
|||
uint32_t numInFlightPiece;
|
||||
in.read(reinterpret_cast<char*>(&numInFlightPiece),
|
||||
sizeof(numInFlightPiece));
|
||||
CHECK_STREAM(in, sizeof(numInFlightPiece));
|
||||
if(version >= 1) {
|
||||
numInFlightPiece = ntohl(numInFlightPiece);
|
||||
}
|
||||
|
@ -378,11 +399,6 @@ void DefaultBtProgressInfoFile::load()
|
|||
_pieceStorage->setBitfield(dest.getBitfield(), dest.getBitfieldLength());
|
||||
}
|
||||
_logger->info(MSG_LOADED_SEGMENT_FILE);
|
||||
} catch(std::ios::failure const& exception) {
|
||||
// TODO std::ios::failure doesn't give us the reasons of failure...
|
||||
throw DL_ABORT_EX(StringFormat(EX_SEGMENT_FILE_READ,
|
||||
_filename.c_str(), strerror(errno)).str());
|
||||
}
|
||||
}
|
||||
|
||||
void DefaultBtProgressInfoFile::removeFile()
|
||||
|
|
Loading…
Reference in New Issue