Ignore:
Timestamp:
Jun 21, 2009, 7:36:51 AM (12 years ago)
Author:
charles
Message:

(trunk libT) more efficient closing of a torrent's open files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/fdlimit.c

    r8707 r8725  
    8787    tr_bool    isWritable;
    8888    tr_bool    closeWhenDone;
     89    int        torrentId;
    8990    char       filename[MAX_PATH_LENGTH];
    9091    int        fd;
     
    381382/* returns an fd on success, or a -1 on failure and sets errno */
    382383int
    383 tr_fdFileCheckout( const char             * folder,
     384tr_fdFileCheckout( int                      torrentId,
     385                   const char             * folder,
    384386                   const char             * torrentFile,
    385387                   tr_bool                  doWrite,
     
    391393    char filename[MAX_PATH_LENGTH];
    392394
     395    assert( torrentId > 0 );
    393396    assert( folder && *folder );
    394397    assert( torrentFile && *torrentFile );
     
    400403    tr_lockLock( gFd->lock );
    401404
    402     /* Is it already open? */
    403     for( i = 0; i < gFd->openFileLimit; ++i )
     405    /* is it already open? */
     406    for( i=0; i<gFd->openFileLimit; ++i )
    404407    {
    405408        o = &gFd->openFiles[i];
     
    407410        if( !fileIsOpen( o ) )
    408411            continue;
    409 
     412        if( torrentId != o->torrentId )
     413            continue;
    410414        if( strcmp( filename, o->filename ) )
    411415            continue;
     
    423427        if( doWrite && !o->isWritable )
    424428        {
    425             dbgmsg(
    426                 "found it!  it's open and available, but isn't writable. closing..." );
     429            dbgmsg( "found it!  it's open and available, but isn't writable. closing..." );
    427430            TrCloseFile( i );
    428431            break;
     
    434437    }
    435438
    436     dbgmsg(
    437         "it's not already open.  looking for an open slot or an old file." );
     439    dbgmsg( "it's not already open.  looking for an open slot or an old file." );
    438440    while( winner < 0 )
    439441    {
     
    441443
    442444        /* look for the file that's been open longest */
    443         for( i = 0; i < gFd->openFileLimit; ++i )
     445        for( i=0; i<gFd->openFileLimit; ++i )
    444446        {
    445447            o = &gFd->openFiles[i];
     
    496498
    497499    dbgmsg( "checking out '%s' in slot %d", filename, winner );
     500    o->torrentId = torrentId;
    498501    o->isCheckedOut = 1;
    499502    o->closeWhenDone = 0;
     
    531534{
    532535    int i;
    533 
    534536    tr_lockLock( gFd->lock );
    535537
    536     for( i = 0; i < gFd->openFileLimit; ++i )
     538    for( i=0; i<gFd->openFileLimit; ++i )
    537539    {
    538540        struct tr_openfile * o = &gFd->openFiles[i];
     
    542544        dbgmsg( "tr_fdFileClose closing '%s'", filename );
    543545
    544         if( !o->isCheckedOut )
    545         {
    546             dbgmsg( "not checked out, so closing it now... '%s'", filename );
     546        assert( !o->isCheckedOut && "this is a test assertion... I *think* this is always true now" );
     547
     548        TrCloseFile( i );
     549    }
     550
     551    tr_lockUnlock( gFd->lock );
     552}
     553
     554void
     555tr_fdTorrentClose( int torrentId )
     556{
     557    int i;
     558    tr_lockLock( gFd->lock );
     559
     560    for( i=0; i<gFd->openFileLimit; ++i )
     561    {
     562        struct tr_openfile * o = &gFd->openFiles[i];
     563
     564        assert( !o->isCheckedOut && "this is a test assertion... I *think* this is always true now" );
     565
     566        if( fileIsOpen( o ) && o->torrentId == torrentId )
    547567            TrCloseFile( i );
    548         }
    549         else
    550         {
    551             dbgmsg(
    552                 "flagging file '%s', slot #%d to be closed when checked in",
    553                 gFd->openFiles[i].filename, i );
    554             o->closeWhenDone = 1;
    555         }
    556568    }
    557569
    558570    tr_lockUnlock( gFd->lock );
    559571}
     572
    560573
    561574/***
     
    737750    return gFd ? gFd->socketLimit : -1;
    738751}
    739 
Note: See TracChangeset for help on using the changeset viewer.