From 11717de1d0a5bc23127f01720fa9f2282fe74dbb Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 13 Aug 2009 17:25:26 +0000 Subject: [PATCH] (trunk libT) fix #2332: filesystem-based error messages lack context --- libtransmission/inout.c | 24 +++++++++++++++--------- libtransmission/inout.h | 22 +++++++++++----------- libtransmission/peer-mgr.c | 27 +++++++++++---------------- libtransmission/peer-msgs.c | 8 +------- libtransmission/torrent.c | 18 ++++++++++++++++-- libtransmission/torrent.h | 4 +++- 6 files changed, 57 insertions(+), 46 deletions(-) diff --git a/libtransmission/inout.c b/libtransmission/inout.c index ba1acb014..20a3c9a9c 100644 --- a/libtransmission/inout.c +++ b/libtransmission/inout.c @@ -155,11 +155,11 @@ tr_ioFindFileLocation( const tr_torrent * tor, /* returns 0 on success, or an errno on failure */ static int -readOrWritePiece( const tr_torrent * tor, +readOrWritePiece( tr_torrent * tor, int ioMode, tr_piece_index_t pieceIndex, uint32_t pieceOffset, - uint8_t * buf, + uint8_t * buf, size_t buflen ) { int err = 0; @@ -185,27 +185,33 @@ readOrWritePiece( const tr_torrent * tor, buflen -= bytesThisPass; ++fileIndex; fileOffset = 0; + + if( err ) { + char * path = tr_buildPath( tor->downloadDir, file->name, NULL ); + tr_torrentSetLocalError( tor, "%s (%s)", tr_strerror( err ), path ); + tr_free( path ); + } } return err; } int -tr_ioRead( const tr_torrent * tor, +tr_ioRead( tr_torrent * tor, tr_piece_index_t pieceIndex, uint32_t begin, uint32_t len, - uint8_t * buf ) + uint8_t * buf ) { return readOrWritePiece( tor, TR_IO_READ, pieceIndex, begin, buf, len ); } int -tr_ioWrite( const tr_torrent * tor, +tr_ioWrite( tr_torrent * tor, tr_piece_index_t pieceIndex, uint32_t begin, uint32_t len, - const uint8_t * buf ) + const uint8_t * buf ) { return readOrWritePiece( tor, TR_IO_WRITE, pieceIndex, begin, (uint8_t*)buf, @@ -217,11 +223,11 @@ tr_ioWrite( const tr_torrent * tor, ****/ static tr_bool -recalculateHash( const tr_torrent * tor, +recalculateHash( tr_torrent * tor, tr_piece_index_t pieceIndex, void * buffer, size_t buflen, - uint8_t * setme ) + uint8_t * setme ) { size_t bytesLeft; uint32_t offset = 0; @@ -263,7 +269,7 @@ recalculateHash( const tr_torrent * tor, } tr_bool -tr_ioTestPiece( const tr_torrent * tor, +tr_ioTestPiece( tr_torrent * tor, tr_piece_index_t pieceIndex, void * buffer, size_t buflen ) diff --git a/libtransmission/inout.h b/libtransmission/inout.h index ff547eec4..78ebf2dac 100644 --- a/libtransmission/inout.h +++ b/libtransmission/inout.h @@ -28,21 +28,21 @@ struct tr_torrent; * Reads the block specified by the piece index, offset, and length. * @return 0 on success, or an errno value on failure. */ -int tr_ioRead( const struct tr_torrent * tor, - tr_piece_index_t pieceIndex, - uint32_t offset, - uint32_t len, - uint8_t * setme ); +int tr_ioRead( struct tr_torrent * tor, + tr_piece_index_t pieceIndex, + uint32_t offset, + uint32_t len, + uint8_t * setme ); /** * Writes the block specified by the piece index, offset, and length. * @return 0 on success, or an errno value on failure. */ -int tr_ioWrite( const struct tr_torrent * tor, - tr_piece_index_t pieceIndex, - uint32_t offset, - uint32_t len, - const uint8_t * writeme ); +int tr_ioWrite( struct tr_torrent * tor, + tr_piece_index_t pieceIndex, + uint32_t offset, + uint32_t len, + const uint8_t * writeme ); /** * @brief Test to see if the piece matches its metainfo's SHA1 checksum. @@ -51,7 +51,7 @@ int tr_ioWrite( const struct tr_torrent * tor, * get best performance by providing a buffer with * tor->info.pieceSize bytes. */ -tr_bool tr_ioTestPiece( const tr_torrent * tor, +tr_bool tr_ioTestPiece( tr_torrent * tor, tr_piece_index_t piece, void * optionalBuffer, size_t optionalBufferLen ); diff --git a/libtransmission/peer-mgr.c b/libtransmission/peer-mgr.c index f6a04b47a..8dd81cfa9 100644 --- a/libtransmission/peer-mgr.c +++ b/libtransmission/peer-mgr.c @@ -1164,27 +1164,16 @@ peerCallbackFunc( void * vpeer, void * vevent, void * vt ) } case TR_PEER_ERROR: - if( e->err == EINVAL ) - { - addStrike( t, peer ); - peer->doPurge = 1; - tordbg( t, "setting %s doPurge flag because we got an EINVAL error", tr_peerIoAddrStr( &peer->addr, peer->port ) ); - } - else if( ( e->err == ERANGE ) - || ( e->err == EMSGSIZE ) - || ( e->err == ENOTCONN ) ) + if( ( e->err == ERANGE ) || ( e->err == EMSGSIZE ) || ( e->err == ENOTCONN ) ) { /* some protocol error from the peer */ peer->doPurge = 1; - tordbg( t, "setting %s doPurge flag because we got an ERANGE, EMSGSIZE, or ENOTCONN error", tr_peerIoAddrStr( &peer->addr, peer->port ) ); + tordbg( t, "setting %s doPurge flag because we got an ERANGE, EMSGSIZE, or ENOTCONN error", + tr_peerIoAddrStr( &peer->addr, peer->port ) ); } - else /* a local error, such as an IO error */ + else { - t->tor->error = TR_STAT_LOCAL_ERROR; - tr_strlcpy( t->tor->errorString, - tr_strerror( e->err ), - sizeof( t->tor->errorString ) ); - tr_torrentStop( t->tor ); + tordbg( t, "unhandled error: %s", tr_strerror( e->err ) ); } break; @@ -2615,6 +2604,12 @@ bandwidthPulse( void * vmgr ) } } + /* possibly stop torrents that have an error */ + tor = NULL; + while(( tor = tr_torrentNext( mgr->session, tor ))) + if( tor->isRunning && (( tor->error == TR_STAT_TRACKER_ERROR ) || ( tor->error == TR_STAT_LOCAL_ERROR ))) + tr_torrentStop( tor ); + managerUnlock( mgr ); return TRUE; } diff --git a/libtransmission/peer-msgs.c b/libtransmission/peer-msgs.c index d66f30c0c..5c4e81147 100644 --- a/libtransmission/peer-msgs.c +++ b/libtransmission/peer-msgs.c @@ -1347,12 +1347,7 @@ readBtPiece( tr_peermsgs * msgs, msgs->incoming.block = evbuffer_new( ); req->length = 0; msgs->state = AWAITING_BT_LENGTH; - if( !err ) - return READ_NOW; - else { - fireError( msgs, err ); - return READ_ERR; - } + return err ? READ_ERR : READ_NOW; } } @@ -1764,7 +1759,6 @@ fillOutputBuffer( tr_peermsgs * msgs, time_t now ) if( err ) { - fireError( msgs, err ); bytesWritten = 0; msgs = NULL; } diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index 928e3350a..5c9c2c892 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -18,6 +18,7 @@ #include #include /* INT_MAX */ #include /* fabs */ +#include #include /* memcmp */ #include /* qsort */ @@ -262,10 +263,23 @@ tr_torrentGetSeedRatio( const tr_torrent * tor, double * ratio ) **** ***/ +void +tr_torrentSetLocalError( tr_torrent * tor, const char * fmt, ... ) +{ + va_list ap; + + assert( tr_isTorrent( tor ) ); + + va_start( ap, fmt ); + tor->error = TR_STAT_LOCAL_ERROR; + evutil_vsnprintf( tor->errorString, sizeof( tor->errorString ), fmt, ap ); + va_end( ap ); +} + static void onTrackerResponse( void * tracker UNUSED, - void * vevent, - void * user_data ) + void * vevent, + void * user_data ) { tr_torrent * tor = user_data; tr_tracker_event * event = vevent; diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index 812d4fd1c..5dbc507b9 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -20,7 +20,7 @@ #include "completion.h" /* tr_completion */ #include "ratecontrol.h" /* tr_ratecontrol */ #include "session.h" /* tr_globalLock(), tr_globalUnlock() */ -#include "utils.h" /* tr_bitfield */ +#include "utils.h" /* TR_GNUC_PRINTF */ struct tr_bandwidth; struct tr_ratecontrol; @@ -121,6 +121,8 @@ void tr_torrentCheckSeedRatio( tr_torrent * tor ); /** save a torrent's .resume file if it's changed since the last time it was saved */ void tr_torrentSave( tr_torrent * tor ); +void tr_torrentSetLocalError( tr_torrent * tor, const char * fmt, ... ) TR_GNUC_PRINTF( 2, 3 ); + typedef enum