(libT) finish killing tr_errno.

This commit is contained in:
Charles Kerr 2008-10-03 04:49:06 +00:00
parent 2918e2efaf
commit 4fab4c9e9b
17 changed files with 165 additions and 245 deletions

View File

@ -1,4 +1,5 @@
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "transmission.h"
@ -43,34 +44,34 @@ testInt( void )
end = NULL;
val = 888;
err = tr_bencParseInt( buf, buf + 3, &end, &val );
check( err == TR_ERROR );
check( err == EILSEQ );
check( val == 888 );
check( end == NULL );
/* empty buffer */
err = tr_bencParseInt( buf, buf + 0, &end, &val );
check( err == TR_ERROR );
check( err == EILSEQ );
check( val == 888 );
check( end == NULL );
/* bad number */
tr_snprintf( (char*)buf, sizeof( buf ), "i6z4e" );
err = tr_bencParseInt( buf, buf + 5, &end, &val );
check( err == TR_ERROR );
check( err == EILSEQ );
check( val == 888 );
check( end == NULL );
/* negative number */
tr_snprintf( (char*)buf, sizeof( buf ), "i-3e" );
err = tr_bencParseInt( buf, buf + 4, &end, &val );
check( err == TR_OK );
check( err == 0 );
check( val == -3 );
check( end == buf + 4 );
/* zero */
tr_snprintf( (char*)buf, sizeof( buf ), "i0e" );
err = tr_bencParseInt( buf, buf + 4, &end, &val );
check( err == TR_OK );
check( err == 0 );
check( val == 0 );
check( end == buf + 3 );
@ -79,7 +80,7 @@ testInt( void )
end = NULL;
tr_snprintf( (char*)buf, sizeof( buf ), "i04e" );
err = tr_bencParseInt( buf, buf + 4, &end, &val );
check( err == TR_ERROR );
check( err == EILSEQ );
check( val == 0 );
check( end == NULL );
@ -98,7 +99,7 @@ testStr( void )
/* good string */
tr_snprintf( (char*)buf, sizeof( buf ), "4:boat" );
err = tr_bencParseStr( buf, buf + 6, &end, &str, &len );
check( err == TR_OK );
check( err == 0 );
check( !strncmp( (char*)str, "boat", len ) );
check( len == 4 );
check( end == buf + 6 );
@ -108,7 +109,7 @@ testStr( void )
/* string goes past end of buffer */
err = tr_bencParseStr( buf, buf + 5, &end, &str, &len );
check( err == TR_ERROR );
check( err == EILSEQ );
check( str == NULL );
check( end == NULL );
check( !len );
@ -116,7 +117,7 @@ testStr( void )
/* empty string */
tr_snprintf( (char*)buf, sizeof( buf ), "0:" );
err = tr_bencParseStr( buf, buf + 2, &end, &str, &len );
check( err == TR_OK );
check( err == 0 );
check( !*str );
check( !len );
check( end == buf + 2 );
@ -127,7 +128,7 @@ testStr( void )
/* short string */
tr_snprintf( (char*)buf, sizeof( buf ), "3:boat" );
err = tr_bencParseStr( buf, buf + 6, &end, &str, &len );
check( err == TR_OK );
check( err == 0 );
check( !strncmp( (char*)str, "boa", len ) );
check( len == 3 );
check( end == buf + 5 );

View File

@ -80,28 +80,28 @@ tr_bencParseInt( const uint8_t * buf,
const uint8_t ** setme_end,
int64_t * setme_val )
{
int err = TR_OK;
int err = 0;
char * endptr;
const void * begin;
const void * end;
int64_t val;
if( buf >= bufend )
return TR_ERROR;
return EILSEQ;
if( *buf != 'i' )
return TR_ERROR;
return EILSEQ;
begin = buf + 1;
end = memchr( begin, 'e', ( bufend - buf ) - 1 );
if( end == NULL )
return TR_ERROR;
return EILSEQ;
errno = 0;
val = strtoll( begin, &endptr, 10 );
if( errno || ( endptr != end ) ) /* incomplete parse */
err = TR_ERROR;
err = EILSEQ;
else if( val && *(const char*)begin == '0' ) /* no leading zeroes! */
err = TR_ERROR;
err = EILSEQ;
else
{
*setme_end = end + 1;
@ -129,27 +129,27 @@ tr_bencParseStr( const uint8_t * buf,
char * endptr;
if( buf >= bufend )
return TR_ERROR;
return EILSEQ;
if( !isdigit( *buf ) )
return TR_ERROR;
return EILSEQ;
end = memchr( buf, ':', bufend - buf );
if( end == NULL )
return TR_ERROR;
return EILSEQ;
errno = 0;
len = strtoul( (const char*)buf, &endptr, 10 );
if( errno || endptr != end )
return TR_ERROR;
return EILSEQ;
if( (const uint8_t*)end + 1 + len > bufend )
return TR_ERROR;
return EILSEQ;
*setme_end = end + 1 + len;
*setme_str = end + 1;
*setme_strlen = len;
return TR_OK;
return 0;
}
/* set to 1 to help expose bugs with tr_bencListAdd and tr_bencDictAdd */
@ -239,7 +239,7 @@ tr_bencParseImpl( const void * buf_in,
node = getNode( top, parentStack, TYPE_INT );
if( !node )
return TR_ERROR;
return EILSEQ;
tr_bencInitInt( node, val );
buf = end;
@ -251,7 +251,7 @@ tr_bencParseImpl( const void * buf_in,
{
tr_benc * node = getNode( top, parentStack, TYPE_LIST );
if( !node )
return TR_ERROR;
return EILSEQ;
tr_bencInit( node, TYPE_LIST );
tr_ptrArrayAppend( parentStack, node );
++buf;
@ -260,7 +260,7 @@ tr_bencParseImpl( const void * buf_in,
{
tr_benc * node = getNode( top, parentStack, TYPE_DICT );
if( !node )
return TR_ERROR;
return EILSEQ;
tr_bencInit( node, TYPE_DICT );
tr_ptrArrayAppend( parentStack, node );
++buf;
@ -270,14 +270,14 @@ tr_bencParseImpl( const void * buf_in,
tr_benc * node;
++buf;
if( tr_ptrArrayEmpty( parentStack ) )
return TR_ERROR;
return EILSEQ;
node = tr_ptrArrayBack( parentStack );
if( tr_bencIsDict( node ) && ( node->val.l.count % 2 ) )
{
/* odd # of children in dict */
tr_bencFree( &node->val.l.vals[--node->val.l.count] );
return TR_ERROR;
return EILSEQ;
}
tr_ptrArrayPop( parentStack );
@ -297,7 +297,7 @@ tr_bencParseImpl( const void * buf_in,
node = getNode( top, parentStack, TYPE_STR );
if( !node )
return TR_ERROR;
return EILSEQ;
tr_bencInitStr( node, str, str_len );
buf = end;
@ -1414,21 +1414,22 @@ saveFile( const char * filename,
const char * content,
size_t len )
{
int err = TR_OK;
int err = 0;
FILE * out = NULL;
out = fopen( filename, "wb+" );
if( !out )
{
err = errno;
tr_err( _( "Couldn't open \"%1$s\": %2$s" ),
filename, tr_strerror( errno ) );
err = TR_EINVALID;
filename, tr_strerror( errno ) );
}
else if( fwrite( content, sizeof( char ), len, out ) != (size_t)len )
{
err = errno;
tr_err( _( "Couldn't save file \"%1$s\": %2$s" ),
filename, tr_strerror( errno ) );
err = TR_EINVALID;
}
if( !err )
@ -1470,27 +1471,35 @@ int
tr_bencLoadFile( const char * filename,
tr_benc * b )
{
int ret;
int err;
size_t contentLen;
uint8_t * content = tr_loadFile( filename, &contentLen );
uint8_t * content;
content = tr_loadFile( filename, &contentLen );
if( !content )
err = errno;
else
err = tr_bencLoad( content, contentLen, b, NULL );
ret = content ? tr_bencLoad( content, contentLen, b, NULL )
: TR_ERROR_IO_OTHER;
tr_free( content );
return ret;
return err;
}
int
tr_bencLoadJSONFile( const char * filename,
tr_benc * b )
{
int ret;
size_t contentLen;
uint8_t * content = tr_loadFile( filename, &contentLen );
int err;
size_t contentLen;
uint8_t * content;
content = tr_loadFile( filename, &contentLen );
if( !content )
err = errno;
else
err = tr_jsonParse( content, contentLen, b, NULL );
ret = content ? tr_jsonParse( content, contentLen, b, NULL )
: TR_ERROR_IO_OTHER;
tr_free( content );
return ret;
return err;
}

View File

@ -100,7 +100,12 @@ static struct tr_fd_s * gFd = NULL;
****
***/
static tr_errno
/**
* returns 0 on success, or an errno value on failure.
* errno values include ENOENT if the parent folder doesn't exist,
* plus the errno values set by tr_mkdirp() and open().
*/
static int
TrOpenFile( int i,
const char * folder,
const char * torrentFile,
@ -113,7 +118,7 @@ TrOpenFile( int i,
/* confirm the parent folder exists */
if( stat( folder, &sb ) || !S_ISDIR( sb.st_mode ) )
return TR_ERROR_IO_PARENT;
return ENOENT;
/* create subfolders, if any */
tr_buildPath ( filename, sizeof( filename ), folder, torrentFile, NULL );
@ -123,7 +128,7 @@ TrOpenFile( int i,
const int err = tr_mkdirp( dirname( tmp ), 0777 ) ? errno : 0;
tr_free( tmp );
if( err )
return tr_ioErrorFromErrno( err );
return err;
}
/* open the file */
@ -140,10 +145,10 @@ TrOpenFile( int i,
const int err = errno;
tr_err( _( "Couldn't open \"%1$s\": %2$s" ), filename,
tr_strerror( err ) );
return tr_ioErrorFromErrno( err );
return err;
}
return TR_OK;
return 0;
}
static int
@ -172,6 +177,7 @@ fileIsCheckedOut( const struct tr_openfile * o )
return fileIsOpen( o ) && o->isCheckedOut;
}
/* returns an fd on success, or a -1 on failure and sets errno */
int
tr_fdFileCheckout( const char * folder,
const char * torrentFile,
@ -274,11 +280,11 @@ tr_fdFileCheckout( const char * folder,
o = &gFd->open[winner];
if( !fileIsOpen( o ) )
{
const tr_errno err = TrOpenFile( winner, folder, torrentFile, write );
if( err )
{
const int err = TrOpenFile( winner, folder, torrentFile, write );
if( err ) {
tr_lockUnlock( gFd->lock );
return err;
errno = err;
return -1;
}
dbgmsg( "opened '%s' in slot %d, write %c", filename, winner,

View File

@ -42,16 +42,15 @@ void tr_fdClose( void );
* write to the file at a time. Callers check out a file, use it,
* and then check it back in via tr_fdFileReturn() when done.
*
* - if `folder' doesn't exist, TR_ERROR_IO_PARENT is returned.
* - if `folder' doesn't exist, errno is set to ENOENT.
* - if doWrite is true, subfolders in torrentFile are created if necessary.
* - if doWrite is true, the target file is created if necessary.
*
* on success, a file descriptor >= 0 is returned.
* on failure, a negative number corresponding to tr_errno is returned.
* on failure, a -1 is returned and errno is set.
*
* @see tr_fdFileReturn
* @see tr_fdFileClose
* @see tr_errno
*/
int tr_fdFileCheckout( const char * folder,
const char * torrentFile,

View File

@ -40,7 +40,8 @@
enum { TR_IO_READ, TR_IO_WRITE };
static tr_errno
/* returns 0 on success, or an errno on failure */
static int
readOrWriteBytes( const tr_torrent * tor,
int ioMode,
tr_file_index_t fileIndex,
@ -68,22 +69,22 @@ readOrWriteBytes( const tr_torrent * tor,
fileExists = !stat( path, &sb );
if( !file->length )
return TR_OK;
return 0;
if( ( ioMode == TR_IO_READ ) && !fileExists ) /* does file exist? */
err = tr_ioErrorFromErrno( errno );
err = errno;
else if( ( fd = tr_fdFileCheckout ( tor->downloadDir,
file->name,
ioMode == TR_IO_WRITE ) ) < 0 )
err = fd;
err = errno;
else if( lseek( fd, (off_t)fileOffset, SEEK_SET ) == ( (off_t)-1 ) )
err = tr_ioErrorFromErrno( errno );
err = errno;
else if( func( fd, buf, buflen ) != buflen )
err = tr_ioErrorFromErrno( errno );
err = errno;
else
err = TR_OK;
err = 0;
if( ( err == TR_OK ) && ( !fileExists ) && ( ioMode == TR_IO_WRITE ) )
if( ( !err ) && ( !fileExists ) && ( ioMode == TR_IO_WRITE ) )
tr_statsFileCreated( tor->session );
if( fd >= 0 )
@ -128,13 +129,14 @@ tr_ioFindFileLocation( const tr_torrent * tor,
}
#ifdef WIN32
static tr_errno
/* return 0 on success, or an errno on failure */
static int
ensureMinimumFileSize( const tr_torrent * tor,
tr_file_index_t fileIndex,
uint64_t minBytes )
{
int fd;
tr_errno err;
int err;
struct stat sb;
const tr_file * file = &tor->info.files[fileIndex];
@ -143,15 +145,15 @@ ensureMinimumFileSize( const tr_torrent * tor,
fd = tr_fdFileCheckout( tor->downloadDir, file->name, TRUE );
if( fd < 0 ) /* bad fd */
err = fd;
err = errno;
else if( fstat ( fd, &sb ) ) /* how big is the file? */
err = tr_ioErrorFromErrno( errno );
err = errno;
else if( sb.st_size >= (off_t)minBytes ) /* already big enough */
err = TR_OK;
err = 0;
else if( !ftruncate( fd, minBytes ) ) /* grow it */
err = TR_OK;
err = 0;
else /* couldn't grow it */
err = tr_ioErrorFromErrno( errno );
err = errno;
if( fd >= 0 )
tr_fdFileReturn( fd );
@ -161,7 +163,8 @@ ensureMinimumFileSize( const tr_torrent * tor,
#endif
static tr_errno
/* returns 0 on success, or an errno on failure */
static int
readOrWritePiece( const tr_torrent * tor,
int ioMode,
tr_piece_index_t pieceIndex,
@ -169,15 +172,15 @@ readOrWritePiece( const tr_torrent * tor,
uint8_t * buf,
size_t buflen )
{
tr_errno err = 0;
int err = 0;
tr_file_index_t fileIndex;
uint64_t fileOffset;
const tr_info * info = &tor->info;
if( pieceIndex >= tor->info.pieceCount )
return TR_ERROR_ASSERT;
return EINVAL;
if( pieceOffset + buflen > tr_torPieceCountBytes( tor, pieceIndex ) )
return TR_ERROR_ASSERT;
return EINVAL;
tr_ioFindFileLocation( tor, pieceIndex, pieceOffset,
&fileIndex, &fileOffset );
@ -205,7 +208,7 @@ readOrWritePiece( const tr_torrent * tor,
return err;
}
tr_errno
int
tr_ioRead( const tr_torrent * tor,
tr_piece_index_t pieceIndex,
uint32_t begin,
@ -215,7 +218,7 @@ tr_ioRead( const tr_torrent * tor,
return readOrWritePiece( tor, TR_IO_READ, pieceIndex, begin, buf, len );
}
tr_errno
int
tr_ioWrite( const tr_torrent * tor,
tr_piece_index_t pieceIndex,
uint32_t begin,

View File

@ -29,25 +29,23 @@ struct tr_torrent;
/**
* Reads the block specified by the piece index, offset, and length.
* @return 0 on success, TR_ERROR_ASSERT if the arguments are incorrect,
* or TR_ERROR_IO_* otherwise.
* @return 0 on success, or an errno value on failure.
*/
tr_errno tr_ioRead( const struct tr_torrent * tor,
tr_piece_index_t pieceIndex,
uint32_t offset,
uint32_t len,
uint8_t * setme );
int tr_ioRead( const 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, TR_ERROR_ASSERT if the arguments are incorrect,
* or TR_ERROR_IO_* otherwise.
* @return 0 on success, or an errno value on failure.
*/
tr_errno 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( const struct tr_torrent * tor,
tr_piece_index_t pieceIndex,
uint32_t offset,
uint32_t len,
const uint8_t * writeme );
/**
* returns nonzero if the piece matches its metainfo's SHA1 checksum.

View File

@ -12,6 +12,7 @@
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <stdio.h> /* printf */
@ -153,7 +154,7 @@ tr_jsonParse( const void * vbuf,
++buf;
if( buf != bufend )
err = TR_ERROR;
err = EILSEQ;
if( setme_end )
*setme_end = (const uint8_t*) buf;

View File

@ -154,7 +154,7 @@ getfile( char ** setme,
*setme = tr_strndup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
/* fprintf( stderr, "[%s]\n", *setme ); */
evbuffer_free( buf );
err = TR_OK;
err = 0;
}
return err;
@ -454,7 +454,7 @@ tr_metainfoParse( const tr_handle * handle,
tr_metainfoFree( inf );
return TR_EINVALID;
}
return TR_OK;
return 0;
}
void

View File

@ -53,7 +53,7 @@ typedef struct
uint32_t offset; /* for GOT_BLOCK */
uint32_t length; /* for GOT_BLOCK + GOT_DATA */
float progress; /* for TR_PEER_PEER_PROGRESS */
tr_errno err; /* for TR_PEER_GOT_ERROR */
int err; /* errno for TR_PEER_GOT_ERROR */
}
tr_peer_event;

View File

@ -989,18 +989,27 @@ peerCallbackFunc( void * vpeer,
}
case TR_PEER_ERROR:
if( TR_ERROR_IS_IO( e->err ) )
{
t->tor->error = e->err;
tr_strlcpy( t->tor->errorString, tr_errorString(
e->err ), sizeof( t->tor->errorString ) );
tr_torrentStop( t->tor );
}
else if( e->err == TR_ERROR_ASSERT )
if( e->err == EINVAL )
{
addStrike( t, peer );
peer->doPurge = 1;
}
else if( ( e->err == EPROTO )
|| ( e->err == ERANGE )
|| ( e->err == EMSGSIZE )
|| ( e->err == ENOTCONN ) )
{
/* some protocol error from the peer */
peer->doPurge = 1;
}
else /* a local error, such as an IO error */
{
t->tor->error = e->err;
tr_strlcpy( t->tor->errorString,
tr_strerror( t->tor->error ),
sizeof( t->tor->errorString ) );
tr_torrentStop( t->tor );
}
peer->doPurge = 1;
break;
default:

View File

@ -482,7 +482,7 @@ publish( tr_peermsgs * msgs,
static void
fireError( tr_peermsgs * msgs,
tr_errno err )
int err )
{
tr_peer_event e = blankEvent;
@ -1424,7 +1424,7 @@ readBtMessage( tr_peermsgs * msgs,
{
dbgmsg( msgs, "bad packet - BT message #%d with a length of %d",
(int)id, (int)msglen );
fireError( msgs, TR_ERROR );
fireError( msgs, EPROTO );
return READ_ERR;
}
@ -1457,7 +1457,7 @@ readBtMessage( tr_peermsgs * msgs,
tr_peerIoReadUint32( msgs->io, inbuf, &ui32 );
dbgmsg( msgs, "got Have: %u", ui32 );
if( tr_bitfieldAdd( msgs->info->have, ui32 ) )
fireError( msgs, TR_ERROR_PEER_MESSAGE );
fireError( msgs, ERANGE );
updatePeerProgress( msgs );
tr_rcTransferred( msgs->torrent->swarmSpeed,
msgs->torrent->info.pieceSize );
@ -1603,7 +1603,8 @@ addPeerToBlamefield( tr_peermsgs * msgs,
tr_bitfieldAdd( msgs->info->blame, index );
}
static tr_errno
/* returns 0 on success, or an errno on failure */
static int
clientGotBlock( tr_peermsgs * msgs,
const uint8_t * data,
const struct peer_request * req )
@ -1619,7 +1620,7 @@ clientGotBlock( tr_peermsgs * msgs,
{
dbgmsg( msgs, "wrong block size -- expected %u, got %d",
tr_torBlockCountBytes( msgs->torrent, block ), req->length );
return TR_ERROR;
return EMSGSIZE;
}
/* save the block */
@ -1656,7 +1657,7 @@ clientGotBlock( tr_peermsgs * msgs,
**/
msgs->info->peerSentPieceDataAt = time( NULL );
if( ( err = tr_ioWrite( tor, req->index, req->offset, req->length, data ) ) )
if(( err = tr_ioWrite( tor, req->index, req->offset, req->length, data )))
return err;
addPeerToBlamefield( msgs, req->index );
@ -1855,7 +1856,7 @@ gotError( struct bufferevent * evbuf UNUSED,
if( what & ( EVBUFFER_EOF | EVBUFFER_ERROR ) )
dbgmsg( vmsgs, "libevent got an error! what=%hd, errno=%d (%s)",
what, errno, tr_strerror( errno ) );
fireError( vmsgs, TR_ERROR );
fireError( vmsgs, ENOTCONN );
}
static void

View File

@ -132,11 +132,4 @@ void tr_globalUnlock( tr_session * );
int tr_globalIsLocked( const tr_session * );
#define TR_ERROR_IS_IO( e ) ( TR_ERROR_IO_PARENT <= ( e ) && ( e ) <=\
TR_ERROR_IO_OTHER )
#define TR_ERROR_IS_TC( e ) ( TR_ERROR_TC_ERROR <= ( e ) && ( e ) <=\
TR_ERROR_TC_WARNING )
#endif

View File

@ -10,6 +10,7 @@
* $Id$
*/
#include <errno.h>
#include <libgen.h> /* basename */
#include "transmission.h"
#include "bencode.h"
@ -138,10 +139,11 @@ tr_ctorSetMetainfoFromHash( tr_ctor * ctor,
int err;
const char * filename;
if( ( filename = tr_sessionFindTorrentFile( ctor->handle, hashString ) ) )
err = tr_ctorSetMetainfoFromFile( ctor, filename );
filename = tr_sessionFindTorrentFile( ctor->handle, hashString );
if( !filename )
err = EINVAL;
else
err = TR_ERROR;
err = tr_ctorSetMetainfoFromFile( ctor, filename );
return err;
}

View File

@ -232,14 +232,14 @@ onTrackerResponse( void * tracker UNUSED,
case TR_TRACKER_WARNING:
tr_torerr( tor, _( "Tracker warning: \"%s\"" ), event->text );
tor->error = TR_ERROR_TC_WARNING;
tor->error = -1;
tr_strlcpy( tor->errorString, event->text,
sizeof( tor->errorString ) );
break;
case TR_TRACKER_ERROR:
tr_torerr( tor, _( "Tracker error: \"%s\"" ), event->text );
tor->error = TR_ERROR_TC_ERROR;
tor->error = -2;
tr_strlcpy( tor->errorString, event->text,
sizeof( tor->errorString ) );
break;
@ -522,7 +522,7 @@ torrentRealInit( tr_handle * h,
assert( !tor->downloadedCur );
assert( !tor->uploadedCur );
tor->error = TR_OK;
tor->error = 0;
tor->checkedPieces = tr_bitfieldNew( tor->info.pieceCount );
tr_torrentUncheck( tor );

View File

@ -787,7 +787,7 @@ const char* tr_ctorGetSourceFile( const tr_ctor * ctor );
/**
* Parses the specified metainfo.
* Returns TR_OK if it parsed and can be added to Transmission.
* Returns 0 if it parsed successfully and can be added to Transmission.
* Returns TR_EINVALID if it couldn't be parsed.
* Returns TR_EDUPLICATE if it parsed but can't be added.
* "download-dir" must be set to test for TR_EDUPLICATE.
@ -1176,43 +1176,13 @@ tr_torrent_status;
#define TR_STATUS_IS_ACTIVE( s ) ( ( s ) != TR_STATUS_STOPPED )
/**
* Transmission error codes
* errors are always negative, and 0 refers to no error.
*/
typedef enum tr_errno
{
TR_OK = 0,
/* general errors */
TR_ERROR = -100,
TR_ERROR_ASSERT,
/* io errors */
TR_ERROR_IO_PARENT = -200,
TR_ERROR_IO_PERMISSIONS,
TR_ERROR_IO_SPACE,
TR_ERROR_IO_FILE_TOO_BIG,
TR_ERROR_IO_OPEN_FILES,
TR_ERROR_IO_DUP_DOWNLOAD,
TR_ERROR_IO_CHECKSUM,
TR_ERROR_IO_OTHER,
/* tracker errors */
TR_ERROR_TC_ERROR = -300,
TR_ERROR_TC_WARNING,
/* peer errors */
TR_ERROR_PEER_MESSAGE = -400
}
tr_errno;
typedef enum
{
TR_LOCKFILE_SUCCESS = 0,
TR_LOCKFILE_EOPEN,
TR_LOCKFILE_ELOCK
} tr_lockfile_state_t;
}
tr_lockfile_state_t;
tr_torrent_status tr_torrentGetStatus( tr_torrent * );
@ -1250,8 +1220,8 @@ typedef struct tr_stat
becomes unreachable. */
char * scrapeURL;
/** The error status for this torrent. 0 means everything's fine. */
tr_errno error;
/** The errno status for this torrent. 0 means everything's fine. */
int error;
/** Typically an error string returned from the tracker. */
char errorString[128];

View File

@ -408,13 +408,16 @@ tr_loadFile( const char * path,
errno = 0;
if( stat( path, &sb ) )
{
const int err = errno;
tr_dbg( err_fmt, path, tr_strerror( errno ) );
errno = err;
return NULL;
}
if( ( sb.st_mode & S_IFMT ) != S_IFREG )
{
tr_err( err_fmt, path, _( "Not a regular file" ) );
errno = EISDIR;
return NULL;
}
@ -422,27 +425,32 @@ tr_loadFile( const char * path,
file = fopen( path, "rb" );
if( !file )
{
const int err = errno;
tr_err( err_fmt, path, tr_strerror( errno ) );
errno = err;
return NULL;
}
buf = malloc( sb.st_size );
if( NULL == buf )
if( !buf )
{
const int err = errno;
tr_err( err_fmt, path, _( "Memory allocation failed" ) );
fclose( file );
errno = err;
return NULL;
}
if( fread( buf, sb.st_size, 1, file ) != 1 )
{
const int err = errno;
tr_err( err_fmt, path, tr_strerror( errno ) );
fclose( file );
free( buf );
errno = err;
return NULL;
}
fclose( file );
*size = sb.st_size;
return buf;
}
@ -555,86 +563,6 @@ tr_buildPath( char * buf,
evbuffer_free( evbuf );
}
int
tr_ioErrorFromErrno( int err )
{
switch( err )
{
case 0:
return TR_OK;
case EACCES:
case EROFS:
return TR_ERROR_IO_PERMISSIONS;
case ENOSPC:
return TR_ERROR_IO_SPACE;
case EMFILE:
return TR_ERROR_IO_OPEN_FILES;
case EFBIG:
return TR_ERROR_IO_FILE_TOO_BIG;
default:
tr_err( "generic i/o errno from errno: %s", tr_strerror( errno ) );
return TR_ERROR_IO_OTHER;
}
}
const char *
tr_errorString( int code )
{
switch( code )
{
case TR_OK:
return _( "No error" );
case TR_ERROR:
return _( "Unspecified error" );
case TR_ERROR_ASSERT:
return _( "Assert error" );
case TR_ERROR_IO_PARENT:
return _( "Destination folder doesn't exist" );
case TR_ERROR_IO_PERMISSIONS:
return tr_strerror( EACCES );
case TR_ERROR_IO_SPACE:
return tr_strerror( ENOSPC );
case TR_ERROR_IO_FILE_TOO_BIG:
return tr_strerror( EFBIG );
case TR_ERROR_IO_OPEN_FILES:
return tr_strerror( EMFILE );
case TR_ERROR_IO_DUP_DOWNLOAD:
return _(
"A torrent with this name and destination folder already exists." );
case TR_ERROR_IO_CHECKSUM:
return _( "Checksum failed" );
case TR_ERROR_IO_OTHER:
return _( "Unspecified I/O error" );
case TR_ERROR_TC_ERROR:
return _( "Tracker error" );
case TR_ERROR_TC_WARNING:
return _( "Tracker warning" );
case TR_ERROR_PEER_MESSAGE:
return _( "Peer sent a bad message" );
default:
return _( "Unknown error" );
}
}
/****
*****
****/

View File

@ -166,6 +166,10 @@ int tr_mkdirp( const char * path,
int permissions );
/**
* Loads a file and returns its contents.
* On failure, NULL is returned and errno is set.
*/
uint8_t* tr_loadFile( const char * filename,
size_t * size ) TR_GNUC_MALLOC;
@ -181,10 +185,6 @@ TR_GNUC_NULL_TERMINATED;
struct timeval tr_timevalMsec( uint64_t milliseconds );
int tr_ioErrorFromErrno( int err );
const char * tr_errorString( int code );
/* return the current date in milliseconds */
uint64_t tr_date( void );