2009-07-22 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

Rewritten stream error handling
	* src/DefaultBtProgressInfoFile.cc
pull/1/head
Tatsuhiro Tsujikawa 2009-07-22 12:44:24 +00:00
parent 0d1d88257c
commit cec451951d
2 changed files with 200 additions and 179 deletions

View File

@ -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> 2009-07-22 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Make sure that stream is closed before renaming file. Unit test Make sure that stream is closed before renaming file. Unit test

View File

@ -102,12 +102,12 @@ bool DefaultBtProgressInfoFile::isTorrentDownload()
} }
// Since version 0001, Integers are saved in binary form, network byte order. // 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()); _logger->info(MSG_SAVING_SEGMENT_FILE, _filename.c_str());
std::string filenameTemp = _filename+"__temp"; std::string filenameTemp = _filename+"__temp";
{
std::ofstream o(filenameTemp.c_str(), std::ios::out|std::ios::binary); std::ofstream o(filenameTemp.c_str(), std::ios::out|std::ios::binary);
try {
o.exceptions(std::ios::failbit);
bool torrentDownload = isTorrentDownload(); bool torrentDownload = isTorrentDownload();
// file version: 16 bits // file version: 16 bits
// values: '1' // values: '1'
@ -182,20 +182,29 @@ void DefaultBtProgressInfoFile::save() {
o.write(reinterpret_cast<const char*>((*itr)->getBitfield()), o.write(reinterpret_cast<const char*>((*itr)->getBitfield()),
(*itr)->getBitfieldLength()); (*itr)->getBitfieldLength());
} }
o.flush();
o.close(); if(!o) {
_logger->info(MSG_SAVED_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_WRITE, throw DL_ABORT_EX(StringFormat(EX_SEGMENT_FILE_WRITE,
_filename.c_str(), strerror(errno)).str()); _filename.c_str(), strerror(errno)).str());
} }
_logger->info(MSG_SAVED_SEGMENT_FILE);
}
if(!File(filenameTemp).renameTo(_filename)) { if(!File(filenameTemp).renameTo(_filename)) {
throw DL_ABORT_EX(StringFormat(EX_SEGMENT_FILE_WRITE, throw DL_ABORT_EX(StringFormat(EX_SEGMENT_FILE_WRITE,
_filename.c_str(), strerror(errno)).str()); _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: // It is assumed that integers are saved as:
// 1) host byte order if version == 0000 // 1) host byte order if version == 0000
// 2) network byte order if version == 0001 // 2) network byte order if version == 0001
@ -203,10 +212,10 @@ void DefaultBtProgressInfoFile::load()
{ {
_logger->info(MSG_LOADING_SEGMENT_FILE, _filename.c_str()); _logger->info(MSG_LOADING_SEGMENT_FILE, _filename.c_str());
std::ifstream in(_filename.c_str(), std::ios::in|std::ios::binary); std::ifstream in(_filename.c_str(), std::ios::in|std::ios::binary);
try {
in.exceptions(std::ios::failbit);
unsigned char versionBuf[2]; unsigned char versionBuf[2];
in.read((char*)versionBuf, sizeof(versionBuf)); in.read((char*)versionBuf, sizeof(versionBuf));
CHECK_STREAM(in, sizeof(versionBuf));
std::string versionHex = Util::toHex(versionBuf, sizeof(versionBuf)); std::string versionHex = Util::toHex(versionBuf, sizeof(versionBuf));
int version; int version;
if(DefaultBtProgressInfoFile::V0000 == versionHex) { if(DefaultBtProgressInfoFile::V0000 == versionHex) {
@ -220,7 +229,7 @@ void DefaultBtProgressInfoFile::load()
} }
unsigned char extension[4]; unsigned char extension[4];
in.read((char*)extension, sizeof(extension)); in.read((char*)extension, sizeof(extension));
CHECK_STREAM(in, sizeof(extension));
bool infoHashCheckEnabled = false; bool infoHashCheckEnabled = false;
if(extension[3]&1 && isTorrentDownload()) { if(extension[3]&1 && isTorrentDownload()) {
infoHashCheckEnabled = true; infoHashCheckEnabled = true;
@ -229,6 +238,7 @@ void DefaultBtProgressInfoFile::load()
uint32_t infoHashLength; uint32_t infoHashLength;
in.read(reinterpret_cast<char*>(&infoHashLength), sizeof(infoHashLength)); in.read(reinterpret_cast<char*>(&infoHashLength), sizeof(infoHashLength));
CHECK_STREAM(in, sizeof(infoHashLength));
if(version >= 1) { if(version >= 1) {
infoHashLength = ntohl(infoHashLength); infoHashLength = ntohl(infoHashLength);
} }
@ -241,6 +251,7 @@ void DefaultBtProgressInfoFile::load()
array_ptr<unsigned char> savedInfoHash(new unsigned char[infoHashLength]); array_ptr<unsigned char> savedInfoHash(new unsigned char[infoHashLength]);
in.read(reinterpret_cast<char*> in.read(reinterpret_cast<char*>
(static_cast<unsigned char*>(savedInfoHash)), infoHashLength); (static_cast<unsigned char*>(savedInfoHash)), infoHashLength);
CHECK_STREAM(in, infoHashLength);
if(infoHashCheckEnabled) { if(infoHashCheckEnabled) {
const unsigned char* infoHash = bittorrent::getInfoHash(_dctx); const unsigned char* infoHash = bittorrent::getInfoHash(_dctx);
if(infoHashLength != INFO_HASH_LENGTH || if(infoHashLength != INFO_HASH_LENGTH ||
@ -256,12 +267,14 @@ void DefaultBtProgressInfoFile::load()
uint32_t pieceLength; uint32_t pieceLength;
in.read(reinterpret_cast<char*>(&pieceLength), sizeof(pieceLength)); in.read(reinterpret_cast<char*>(&pieceLength), sizeof(pieceLength));
CHECK_STREAM(in, sizeof(pieceLength));
if(version >= 1) { if(version >= 1) {
pieceLength = ntohl(pieceLength); pieceLength = ntohl(pieceLength);
} }
uint64_t totalLength; uint64_t totalLength;
in.read(reinterpret_cast<char*>(&totalLength), sizeof(totalLength)); in.read(reinterpret_cast<char*>(&totalLength), sizeof(totalLength));
CHECK_STREAM(in, sizeof(totalLength));
if(version >= 1) { if(version >= 1) {
totalLength = ntoh64(totalLength); totalLength = ntoh64(totalLength);
} }
@ -273,6 +286,7 @@ void DefaultBtProgressInfoFile::load()
} }
uint64_t uploadLength; uint64_t uploadLength;
in.read(reinterpret_cast<char*>(&uploadLength), sizeof(uploadLength)); in.read(reinterpret_cast<char*>(&uploadLength), sizeof(uploadLength));
CHECK_STREAM(in, sizeof(uploadLength));
if(version >= 1) { if(version >= 1) {
uploadLength = ntoh64(uploadLength); uploadLength = ntoh64(uploadLength);
} }
@ -284,6 +298,7 @@ void DefaultBtProgressInfoFile::load()
// TODO implement the conversion mechanism between different piece length. // TODO implement the conversion mechanism between different piece length.
uint32_t bitfieldLength; uint32_t bitfieldLength;
in.read(reinterpret_cast<char*>(&bitfieldLength), sizeof(bitfieldLength)); in.read(reinterpret_cast<char*>(&bitfieldLength), sizeof(bitfieldLength));
CHECK_STREAM(in, sizeof(bitfieldLength));
if(version >= 1) { if(version >= 1) {
bitfieldLength = ntohl(bitfieldLength); bitfieldLength = ntohl(bitfieldLength);
} }
@ -299,13 +314,14 @@ void DefaultBtProgressInfoFile::load()
array_ptr<unsigned char> savedBitfield(new unsigned char[bitfieldLength]); array_ptr<unsigned char> savedBitfield(new unsigned char[bitfieldLength]);
in.read(reinterpret_cast<char*> in.read(reinterpret_cast<char*>
(static_cast<unsigned char*>(savedBitfield)), bitfieldLength); (static_cast<unsigned char*>(savedBitfield)), bitfieldLength);
CHECK_STREAM(in, bitfieldLength);
if(pieceLength == _dctx->getPieceLength()) { if(pieceLength == _dctx->getPieceLength()) {
_pieceStorage->setBitfield(savedBitfield, bitfieldLength); _pieceStorage->setBitfield(savedBitfield, bitfieldLength);
uint32_t numInFlightPiece; uint32_t numInFlightPiece;
in.read(reinterpret_cast<char*>(&numInFlightPiece), in.read(reinterpret_cast<char*>(&numInFlightPiece),
sizeof(numInFlightPiece)); sizeof(numInFlightPiece));
CHECK_STREAM(in, sizeof(numInFlightPiece));
if(version >= 1) { if(version >= 1) {
numInFlightPiece = ntohl(numInFlightPiece); numInFlightPiece = ntohl(numInFlightPiece);
} }
@ -313,6 +329,7 @@ void DefaultBtProgressInfoFile::load()
while(numInFlightPiece--) { while(numInFlightPiece--) {
uint32_t index; uint32_t index;
in.read(reinterpret_cast<char*>(&index), sizeof(index)); in.read(reinterpret_cast<char*>(&index), sizeof(index));
CHECK_STREAM(in, sizeof(index));
if(version >= 1) { if(version >= 1) {
index = ntohl(index); index = ntohl(index);
} }
@ -322,6 +339,7 @@ void DefaultBtProgressInfoFile::load()
} }
uint32_t length; uint32_t length;
in.read(reinterpret_cast<char*>(&length), sizeof(length)); in.read(reinterpret_cast<char*>(&length), sizeof(length));
CHECK_STREAM(in, sizeof(length));
if(version >= 1) { if(version >= 1) {
length = ntohl(length); length = ntohl(length);
} }
@ -333,6 +351,7 @@ void DefaultBtProgressInfoFile::load()
uint32_t bitfieldLength; uint32_t bitfieldLength;
in.read(reinterpret_cast<char*>(&bitfieldLength), in.read(reinterpret_cast<char*>(&bitfieldLength),
sizeof(bitfieldLength)); sizeof(bitfieldLength));
CHECK_STREAM(in, sizeof(bitfieldLength));
if(version >= 1) { if(version >= 1) {
bitfieldLength = ntohl(bitfieldLength); bitfieldLength = ntohl(bitfieldLength);
} }
@ -346,6 +365,7 @@ void DefaultBtProgressInfoFile::load()
(new unsigned char[bitfieldLength]); (new unsigned char[bitfieldLength]);
in.read(reinterpret_cast<char*> in.read(reinterpret_cast<char*>
(static_cast<unsigned char*>(pieceBitfield)), bitfieldLength); (static_cast<unsigned char*>(pieceBitfield)), bitfieldLength);
CHECK_STREAM(in, bitfieldLength);
piece->setBitfield(pieceBitfield, bitfieldLength); piece->setBitfield(pieceBitfield, bitfieldLength);
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
@ -361,6 +381,7 @@ void DefaultBtProgressInfoFile::load()
uint32_t numInFlightPiece; uint32_t numInFlightPiece;
in.read(reinterpret_cast<char*>(&numInFlightPiece), in.read(reinterpret_cast<char*>(&numInFlightPiece),
sizeof(numInFlightPiece)); sizeof(numInFlightPiece));
CHECK_STREAM(in, sizeof(numInFlightPiece));
if(version >= 1) { if(version >= 1) {
numInFlightPiece = ntohl(numInFlightPiece); numInFlightPiece = ntohl(numInFlightPiece);
} }
@ -378,11 +399,6 @@ void DefaultBtProgressInfoFile::load()
_pieceStorage->setBitfield(dest.getBitfield(), dest.getBitfieldLength()); _pieceStorage->setBitfield(dest.getBitfield(), dest.getBitfieldLength());
} }
_logger->info(MSG_LOADED_SEGMENT_FILE); _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() void DefaultBtProgressInfoFile::removeFile()