Changeset 238 for trunk/libtransmission


Ignore:
Timestamp:
Apr 23, 2006, 5:03:12 PM (16 years ago)
Author:
titer
Message:

Use file descriptors instead of streams (preliminary cleanup for fixes
to come in fdlimit*)

Location:
trunk/libtransmission
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/fdlimit.c

    r20 r238  
    3030{
    3131    char       path[MAX_PATH_LENGTH];
    32     FILE     * file;
     32    int        file;
    3333
    3434#define STATUS_INVALID 1
     
    100100 * tr_fdFileOpen
    101101 **********************************************************************/
    102 FILE * tr_fdFileOpen( tr_fd_t * f, char * path )
     102int tr_fdFileOpen( tr_fd_t * f, char * path )
    103103{
    104104    int i, winner;
     
    159159        {
    160160            /* Close the file: we mark it as closing then release the
    161                lock while doing so, because fclose may take same time
     161               lock while doing so, because close may take same time
    162162               and we don't want to block other threads */
    163163            tr_dbg( "Closing %s", f->open[winner].path );
    164164            f->open[winner].status = STATUS_CLOSING;
    165165            tr_lockUnlock( &f->lock );
    166             fclose( f->open[winner].file );
     166            close( f->open[winner].file );
    167167            tr_lockLock( &f->lock );
    168168            goto open;
     
    178178    tr_dbg( "Opening %s", path );
    179179    snprintf( f->open[winner].path, MAX_PATH_LENGTH, "%s", path );
    180     f->open[winner].file = fopen( path, "r+" );
     180    f->open[winner].file = open( path, O_RDWR, 0 );
    181181
    182182done:
     
    191191 * tr_fdFileRelease
    192192 **********************************************************************/
    193 void tr_fdFileRelease( tr_fd_t * f, FILE * file )
     193void tr_fdFileRelease( tr_fd_t * f, int file )
    194194{
    195195    int i;
     
    227227        {
    228228            tr_dbg( "Closing %s", path );
    229             fclose( f->open[i].file );
     229            close( f->open[i].file );
    230230            f->open[i].status = STATUS_INVALID;
    231231            break;
  • trunk/libtransmission/fdlimit.h

    r1 r238  
    2525tr_fd_t * tr_fdInit();
    2626
    27 FILE *    tr_fdFileOpen         ( tr_fd_t *, char * );
    28 void      tr_fdFileRelease      ( tr_fd_t *, FILE * );
     27int       tr_fdFileOpen         ( tr_fd_t *, char * );
     28void      tr_fdFileRelease      ( tr_fd_t *, int );
    2929void      tr_fdFileClose        ( tr_fd_t *, char * );
    3030
  • trunk/libtransmission/inout.c

    r104 r238  
    2323#include "transmission.h"
    2424
    25 #ifdef SYS_BEOS
    26 # define fseeko _fseek
    27 #endif
    28 
    2925struct tr_io_s
    3026{
     
    203199    char        * path, * p;
    204200    struct stat   sb;
    205     FILE        * file;
     201    int          file;
    206202
    207203    tr_dbg( "Creating files..." );
     
    235231        {
    236232            /* File doesn't exist yet */
    237             if( !( file = fopen( path, "w" ) ) )
     233            if( ( file = open( path, O_WRONLY|O_CREAT|O_TRUNC ) ) < 0 )
    238234            {
    239235                tr_err( "Could not create `%s' (%s)", path,
     
    242238                return 1;
    243239            }
    244             fclose( file );
     240            close( file );
    245241        }
    246242        else if( ( sb.st_mode & S_IFMT ) != S_IFREG )
     
    358354 *
    359355 **********************************************************************/
    360 typedef size_t (* iofunc) ( void *, size_t, size_t, FILE * );
     356typedef size_t (* iofunc) ( int, void *, size_t );
    361357static int readOrWriteBytes( tr_io_t * io, uint64_t offset, int size,
    362                              uint8_t * buf, int write )
     358                             uint8_t * buf, int isWrite )
    363359{
    364360    tr_torrent_t * tor = io->tor;
     
    367363    int    piece = offset / inf->pieceSize;
    368364    int    begin = offset % inf->pieceSize;
    369     int    i, cur;
     365    int    i;
     366    size_t cur;
    370367    char * path;
    371     FILE * file;
    372     iofunc readOrWrite = write ? (iofunc) fwrite : (iofunc) fread;
     368    int    file;
     369    iofunc readOrWrite = isWrite ? (iofunc) write : (iofunc) read;
    373370
    374371    /* Release the torrent lock so the UI can still update itself if
     
    416413        asprintf( &path, "%s/%s", tor->destination, inf->files[i].name );
    417414        file = tr_fdFileOpen( tor->fdlimit, path );
    418         if( !file )
     415        if( file < 0 )
    419416        {
    420417            tr_err( "readOrWriteBytes: could not open file '%s'", path );
     
    425422
    426423        /* seek to the right offset... */
    427         if( fseeko( file, offset, SEEK_SET ) )
     424        if( lseek( file, offset, SEEK_SET ) < 0 )
    428425        {
    429426            goto fail;
     
    431428
    432429        /* do what we are here to do... */
    433         if( readOrWrite( buf, cur, 1, file ) != 1 )
     430        if( readOrWrite( file, buf, cur ) != cur )
    434431        {
    435432            goto fail;
Note: See TracChangeset for help on using the changeset viewer.