Changeset 7720


Ignore:
Timestamp:
Jan 16, 2009, 4:38:16 PM (12 years ago)
Author:
charles
Message:

(trunk libT) make file preallocation optional via settings.json. choices are: full, sparse, and none

Location:
trunk/libtransmission
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/fdlimit.c

    r7643 r7720  
    109109#endif
    110110
    111 static int
    112 preallocateFile( const char * filename, uint64_t length )
    113 {
    114     int success = 0;
     111static tr_bool
     112preallocateFileSparse( int fd, uint64_t length )
     113{
     114    const char zero = '\0';
     115
     116    if( length == 0 )
     117        return TRUE;
     118
     119    if( lseek( fd, length-1, SEEK_SET ) == -1 )
     120        return FALSE;
     121    if( write( fd, &zero, 1 ) == -1 )
     122        return FALSE;
     123    if( ftruncate( fd, length ) == -1 )
     124        return FALSE;
     125
     126    return TRUE;
     127}
     128
     129static tr_bool
     130preallocateFileFull( const char * filename, uint64_t length )
     131{
     132    tr_bool success = 0;
    115133
    116134#ifdef WIN32
     
    171189 */
    172190static int
    173 TrOpenFile( int          i,
    174             const char * folder,
    175             const char * torrentFile,
    176             int          doWrite,
    177             int          doPreallocate,
    178             uint64_t     desiredFileSize )
     191TrOpenFile( int                      i,
     192            const char             * folder,
     193            const char             * torrentFile,
     194            tr_bool                  doWrite,
     195            tr_preallocation_mode    preallocationMode,
     196            uint64_t                 desiredFileSize )
    179197{
    180198    struct tr_openfile * file = &gFd->openFiles[i];
     
    203221    alreadyExisted = !stat( filename, &sb ) && S_ISREG( sb.st_mode );
    204222
    205     if( doWrite && !alreadyExisted && doPreallocate )
    206         if( preallocateFile( filename, desiredFileSize ) )
     223    if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_FULL ) )
     224        if( preallocateFileFull( filename, desiredFileSize ) )
    207225            tr_inf( _( "Preallocated file \"%s\"" ), filename );
    208226   
     
    225243    }
    226244
     245    if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_SPARSE ) )
     246        preallocateFileSparse( file->fd, desiredFileSize );
     247
    227248    tr_free( filename );
    228249    return 0;
     
    257278/* returns an fd on success, or a -1 on failure and sets errno */
    258279int
    259 tr_fdFileCheckout( const char * folder,
    260                    const char * torrentFile,
    261                    int          doWrite,
    262                    int          doPreallocate,
    263                    uint64_t     desiredFileSize )
     280tr_fdFileCheckout( const char             * folder,
     281                   const char             * torrentFile,
     282                   tr_bool                  doWrite,
     283                   tr_preallocation_mode    preallocationMode,
     284                   uint64_t                 desiredFileSize )
    264285{
    265286    int i, winner = -1;
     
    359380    if( !fileIsOpen( o ) )
    360381    {
    361         const int err = TrOpenFile( winner, folder, torrentFile, doWrite, doPreallocate, desiredFileSize );
     382        const int err = TrOpenFile( winner, folder, torrentFile, doWrite, preallocationMode, desiredFileSize );
    362383        if( err ) {
    363384            tr_lockUnlock( gFd->lock );
  • trunk/libtransmission/fdlimit.h

    r7643 r7720  
    2727#endif
    2828
     29#include "transmission.h"
    2930#include "net.h"
    3031
     
    5152 * @see tr_fdFileClose
    5253 */
    53 int  tr_fdFileCheckout( const char * folder,
    54                         const char * torrentFile,
    55                         int          doWrite,
    56                         int          doPreallocate,
    57                         uint64_t     desiredFileSize );
     54int  tr_fdFileCheckout( const char             * folder,
     55                        const char             * torrentFile,
     56                        tr_bool                  doWrite,
     57                        tr_preallocation_mode    preallocationMode,
     58                        uint64_t                 desiredFileSize );
    5859
    5960/**
  • trunk/libtransmission/inout.c

    r7658 r7720  
    7272    const tr_info * info = &tor->info;
    7373    const tr_file * file = &info->files[fileIndex];
     74    tr_preallocation_mode preallocationMode;
    7475
    7576    typedef size_t ( *iofunc )( int, void *, size_t );
     
    9495        return 0;
    9596
     97    if( ( file->dnd ) || ( ioMode != TR_IO_WRITE ) )
     98        preallocationMode = TR_PREALLOCATE_NONE;
     99    else
     100        preallocationMode = tor->session->preallocationMode;
     101
    96102    if( ( ioMode == TR_IO_READ ) && !fileExists ) /* does file exist? */
    97103        err = errno;
    98     else if( ( fd = tr_fdFileCheckout ( tor->downloadDir, file->name, ioMode == TR_IO_WRITE, !file->dnd, file->length ) ) < 0 )
     104    else if( ( fd = tr_fdFileCheckout ( tor->downloadDir, file->name, ioMode == TR_IO_WRITE, preallocationMode, file->length ) ) < 0 )
    99105        err = errno;
    100106    else if( tr_lseek( fd, (int64_t)fileOffset, SEEK_SET ) == -1 )
  • trunk/libtransmission/session.c

    r7713 r7720  
    238238    tr_bencDictAddInt( d, TR_PREFS_KEY_PEX_ENABLED,              TRUE );
    239239    tr_bencDictAddInt( d, TR_PREFS_KEY_PORT_FORWARDING,          TRUE );
     240    tr_bencDictAddInt( d, TR_PREFS_KEY_PREALLOCATION,            TR_PREALLOCATE_SPARSE );
    240241    tr_bencDictAddStr( d, TR_PREFS_KEY_PROXY,                    "" );
    241242    tr_bencDictAddInt( d, TR_PREFS_KEY_PROXY_AUTH_ENABLED,       FALSE );
     
    283284    tr_bencDictAddInt( d, TR_PREFS_KEY_PEX_ENABLED,              s->isPexEnabled );
    284285    tr_bencDictAddInt( d, TR_PREFS_KEY_PORT_FORWARDING,          tr_sessionIsPortForwardingEnabled( s ) );
     286    tr_bencDictAddInt( d, TR_PREFS_KEY_PREALLOCATION,            s->preallocationMode );
    285287    tr_bencDictAddStr( d, TR_PREFS_KEY_PROXY,                    s->proxy );
    286288    tr_bencDictAddInt( d, TR_PREFS_KEY_PROXY_AUTH_ENABLED,       s->isProxyAuthEnabled );
     
    404406    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_ENCRYPTION, &i );
    405407    assert( found );
     408    assert( tr_isEncryptionMode( i ) );
    406409    session->encryptionMode = i;
     410
     411    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PREALLOCATION, &i );
     412    assert( found );
     413    assert( tr_isPreallocationMode( i ) );
     414    session->preallocationMode = i;
    407415 
    408416    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PEER_SOCKET_TOS, &i );
  • trunk/libtransmission/session.h

    r7646 r7720  
    7171
    7272    tr_encryption_mode           encryptionMode;
     73
     74    tr_preallocation_mode        preallocationMode;
    7375
    7476    struct tr_event_handle *     events;
  • trunk/libtransmission/transmission.h

    r7707 r7720  
    107107typedef enum
    108108{
     109    TR_PREALLOCATE_NONE   = 0,
     110    TR_PREALLOCATE_SPARSE = 1,
     111    TR_PREALLOCATE_FULL   = 2
     112}
     113tr_preallocation_mode;
     114
     115static TR_INLINE tr_bool tr_isPreallocationMode( tr_preallocation_mode m  )
     116{
     117    return ( m == TR_PREALLOCATE_NONE )
     118        || ( m == TR_PREALLOCATE_SPARSE )
     119        || ( m == TR_PREALLOCATE_FULL );
     120}
     121
     122typedef enum
     123{
    109124    TR_PROXY_HTTP,
    110125    TR_PROXY_SOCKS4,
     
    120135}
    121136tr_encryption_mode;
     137
     138static TR_INLINE tr_bool tr_isEncryptionMode( tr_encryption_mode m )
     139{
     140    return ( m == TR_CLEAR_PREFERRED )
     141        || ( m == TR_ENCRYPTION_PREFERRED )
     142        || ( m == TR_ENCRYPTION_REQUIRED );
     143}
     144
    122145
    123146#define TR_DEFAULT_OPEN_FILE_LIMIT_STR "32"
     
    147170#define TR_PREFS_KEY_PORT_FORWARDING            "port-forwarding-enabled"
    148171#define TR_PREFS_KEY_PROXY_AUTH_ENABLED         "proxy-auth-enabled"
     172#define TR_PREFS_KEY_PREALLOCATION              "preallocation"
    149173#define TR_PREFS_KEY_PROXY_ENABLED              "proxy-enabled"
    150174#define TR_PREFS_KEY_PROXY_PASSWORD             "proxy-auth-password"
Note: See TracChangeset for help on using the changeset viewer.