Use file descriptors instead of streams (preliminary cleanup for fixes

to come in fdlimit*)
This commit is contained in:
Eric Petit 2006-04-23 17:03:12 +00:00
parent 3db5194740
commit 94b14b6271
3 changed files with 21 additions and 24 deletions

View File

@ -29,7 +29,7 @@
typedef struct tr_openFile_s
{
char path[MAX_PATH_LENGTH];
FILE * file;
int file;
#define STATUS_INVALID 1
#define STATUS_UNUSED 2
@ -99,7 +99,7 @@ tr_fd_t * tr_fdInit()
/***********************************************************************
* tr_fdFileOpen
**********************************************************************/
FILE * tr_fdFileOpen( tr_fd_t * f, char * path )
int tr_fdFileOpen( tr_fd_t * f, char * path )
{
int i, winner;
uint64_t date;
@ -158,12 +158,12 @@ FILE * tr_fdFileOpen( tr_fd_t * f, char * path )
if( winner >= 0 )
{
/* Close the file: we mark it as closing then release the
lock while doing so, because fclose may take same time
lock while doing so, because close may take same time
and we don't want to block other threads */
tr_dbg( "Closing %s", f->open[winner].path );
f->open[winner].status = STATUS_CLOSING;
tr_lockUnlock( &f->lock );
fclose( f->open[winner].file );
close( f->open[winner].file );
tr_lockLock( &f->lock );
goto open;
}
@ -177,7 +177,7 @@ FILE * tr_fdFileOpen( tr_fd_t * f, char * path )
open:
tr_dbg( "Opening %s", path );
snprintf( f->open[winner].path, MAX_PATH_LENGTH, "%s", path );
f->open[winner].file = fopen( path, "r+" );
f->open[winner].file = open( path, O_RDWR, 0 );
done:
f->open[winner].status = STATUS_USED;
@ -190,7 +190,7 @@ done:
/***********************************************************************
* tr_fdFileRelease
**********************************************************************/
void tr_fdFileRelease( tr_fd_t * f, FILE * file )
void tr_fdFileRelease( tr_fd_t * f, int file )
{
int i;
tr_lockLock( &f->lock );
@ -226,7 +226,7 @@ void tr_fdFileClose( tr_fd_t * f, char * path )
if( !strcmp( path, f->open[i].path ) )
{
tr_dbg( "Closing %s", path );
fclose( f->open[i].file );
close( f->open[i].file );
f->open[i].status = STATUS_INVALID;
break;
}

View File

@ -24,8 +24,8 @@ typedef struct tr_fd_s tr_fd_t;
tr_fd_t * tr_fdInit();
FILE * tr_fdFileOpen ( tr_fd_t *, char * );
void tr_fdFileRelease ( tr_fd_t *, FILE * );
int tr_fdFileOpen ( tr_fd_t *, char * );
void tr_fdFileRelease ( tr_fd_t *, int );
void tr_fdFileClose ( tr_fd_t *, char * );
int tr_fdSocketWillCreate ( tr_fd_t *, int );

View File

@ -22,10 +22,6 @@
#include "transmission.h"
#ifdef SYS_BEOS
# define fseeko _fseek
#endif
struct tr_io_s
{
tr_torrent_t * tor;
@ -202,7 +198,7 @@ static int createFiles( tr_io_t * io )
int i;
char * path, * p;
struct stat sb;
FILE * file;
int file;
tr_dbg( "Creating files..." );
@ -234,14 +230,14 @@ static int createFiles( tr_io_t * io )
if( stat( path, &sb ) )
{
/* File doesn't exist yet */
if( !( file = fopen( path, "w" ) ) )
if( ( file = open( path, O_WRONLY|O_CREAT|O_TRUNC ) ) < 0 )
{
tr_err( "Could not create `%s' (%s)", path,
strerror( errno ) );
free( path );
return 1;
}
fclose( file );
close( file );
}
else if( ( sb.st_mode & S_IFMT ) != S_IFREG )
{
@ -357,19 +353,20 @@ static void closeFiles( tr_io_t * io )
***********************************************************************
*
**********************************************************************/
typedef size_t (* iofunc) ( void *, size_t, size_t, FILE * );
typedef size_t (* iofunc) ( int, void *, size_t );
static int readOrWriteBytes( tr_io_t * io, uint64_t offset, int size,
uint8_t * buf, int write )
uint8_t * buf, int isWrite )
{
tr_torrent_t * tor = io->tor;
tr_info_t * inf = &tor->info;
int piece = offset / inf->pieceSize;
int begin = offset % inf->pieceSize;
int i, cur;
int i;
size_t cur;
char * path;
FILE * file;
iofunc readOrWrite = write ? (iofunc) fwrite : (iofunc) fread;
int file;
iofunc readOrWrite = isWrite ? (iofunc) write : (iofunc) read;
/* Release the torrent lock so the UI can still update itself if
this blocks for a while */
@ -415,7 +412,7 @@ static int readOrWriteBytes( tr_io_t * io, uint64_t offset, int size,
/* Now let's get a stream on the file... */
asprintf( &path, "%s/%s", tor->destination, inf->files[i].name );
file = tr_fdFileOpen( tor->fdlimit, path );
if( !file )
if( file < 0 )
{
tr_err( "readOrWriteBytes: could not open file '%s'", path );
free( path );
@ -424,13 +421,13 @@ static int readOrWriteBytes( tr_io_t * io, uint64_t offset, int size,
free( path );
/* seek to the right offset... */
if( fseeko( file, offset, SEEK_SET ) )
if( lseek( file, offset, SEEK_SET ) < 0 )
{
goto fail;
}
/* do what we are here to do... */
if( readOrWrite( buf, cur, 1, file ) != 1 )
if( readOrWrite( file, buf, cur ) != cur )
{
goto fail;
}