Changeset 8299


Ignore:
Timestamp:
Apr 26, 2009, 8:44:18 PM (12 years ago)
Author:
charles
Message:

(trunk libT) convert tr_open_file_for_scanning() and tr_close_file() to use file descriptors instead of file pointers so that we don't have to mix and match between them

Location:
trunk/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/fdlimit.c

    r8298 r8299  
    202202}
    203203
    204 FILE*
     204int
    205205tr_open_file_for_scanning( const char * filename )
    206206{
     
    225225    /* open the file */
    226226    fd = open( filename, flags, 0666 );
    227     if( fd < 0 )
    228         return NULL;
    229 
     227    if( fd >= 0 )
     228    {
    230229#ifdef HAVE_POSIX_FADVISE
    231     posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL );
     230        posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL );
    232231#endif
    233232#ifdef SYS_DARWIN
    234     fcntl( fd, F_NOCACHE, 1 );
    235     fcntl( fd, F_RDAHEAD, 1 );
    236 #endif
    237 
    238     return fdopen( fd, "r" );
    239 }
    240 
    241 static void
    242 flush_before_closing( int fd )
     233        fcntl( fd, F_NOCACHE, 1 );
     234        fcntl( fd, F_RDAHEAD, 1 );
     235#endif
     236    }
     237
     238    return fd;
     239}
     240
     241void
     242tr_close_file( int fd )
    243243{
    244244#if defined(HAVE_POSIX_FADVISE)
    245245    posix_fadvise( fd, 0, 0, POSIX_FADV_DONTNEED );
    246246#endif
    247 }
    248 
    249 void
    250 tr_close_file( FILE * fp )
    251 {
    252     flush_before_closing( fileno( fp ) );
    253     fclose( fp );
     247    close( fd );
    254248}
    255249
     
    347341    assert( fileIsOpen( o ) );
    348342
    349     flush_before_closing( o->fd );
     343    tr_close_file( o->fd );
    350344    close( o->fd );
    351345    o->fd = -1;
  • trunk/libtransmission/fdlimit.h

    r8296 r8299  
    3333                size_t globalPeerLimit );
    3434
    35 FILE* tr_open_file_for_scanning( const char * filename );
     35int tr_open_file_for_scanning( const char * filename );
    3636
    37 void tr_close_file( FILE * fp );
     37void tr_close_file( int fd );
    3838
    3939int64_t tr_lseek( int fd, int64_t offset, int whence );
  • trunk/libtransmission/utils.c

    r8293 r8299  
    467467             size_t *     size )
    468468{
    469     uint8_t *    buf;
     469    uint8_t * buf;
    470470    struct stat  sb;
    471     FILE *       file;
     471    int fd;
    472472    const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" );
    473473
     
    490490
    491491    /* Load the torrent file into our buffer */
    492     file = tr_open_file_for_scanning( path );
    493     if( !file )
     492    fd = tr_open_file_for_scanning( path );
     493    if( fd < 0 )
    494494    {
    495495        const int err = errno;
     
    503503        const int err = errno;
    504504        tr_err( err_fmt, path, _( "Memory allocation failed" ) );
    505         tr_close_file( file );
     505        tr_close_file( fd );
    506506        errno = err;
    507507        return NULL;
    508508    }
    509     if( fread( buf, sb.st_size, 1, file ) != 1 )
     509    if( read( fd, buf, sb.st_size ) != sb.st_size )
    510510    {
    511511        const int err = errno;
    512512        tr_err( err_fmt, path, tr_strerror( errno ) );
    513         tr_close_file( file );
     513        tr_close_file( fd );
    514514        free( buf );
    515515        errno = err;
     
    517517    }
    518518
    519     tr_close_file( file );
     519    tr_close_file( fd );
    520520    *size = sb.st_size;
    521521    return buf;
  • trunk/libtransmission/verify.c

    r8296 r8299  
    4343{
    4444    SHA_CTX sha;
    45     FILE * fp = NULL;
     45    int fd = -1;
    4646    int64_t filePos = 0;
    4747    tr_bool changed = 0;
     
    7373
    7474        /* if we're starting a new file... */
    75         if( !filePos && !fp )
     75        if( !filePos && (fd<0) )
    7676        {
    7777            char * filename = tr_buildPath( tor->downloadDir, file->name, NULL );
    78             fp = tr_open_file_for_scanning( filename );
    79             /* fprintf( stderr, "opening file #%d (%s) -- %p\n", fileIndex, filename, fp ); */
     78            fd = tr_open_file_for_scanning( filename );
     79            /* fprintf( stderr, "opening file #%d (%s) -- %d\n", fileIndex, filename, fd ); */
    8080            tr_free( filename );
    8181        }
     
    8989
    9090        /* read a bit */
    91         if( fp && tr_lseek( fileno(fp), filePos, SEEK_SET ) != -1 ) {
    92             const int64_t numRead = fread( buffer, 1, bytesThisPass, fp );
     91        if( (fd>=0) && tr_lseek( fd, filePos, SEEK_SET ) != -1 ) {
     92            const int64_t numRead = read( fd, buffer, bytesThisPass );
    9393            if( numRead == bytesThisPass )
    9494                SHA1_Update( &sha, buffer, numRead );
     
    130130        {
    131131            /* fprintf( stderr, "closing file\n" ); */
    132             if( fp != NULL ) { tr_close_file( fp ); fp = NULL; }
     132            if( fd >= 0 ) { tr_close_file( fd ); fd = -1; }
    133133            ++fileIndex;
    134134            filePos = 0;
     
    137137
    138138    /* cleanup */
    139     if( fp != NULL )
    140         tr_close_file( fp );
     139    if( fd >= 0 )
     140        tr_close_file( fd );
    141141    tr_free( buffer );
    142142
Note: See TracChangeset for help on using the changeset viewer.