Changeset 7115


Ignore:
Timestamp:
Nov 15, 2008, 7:59:18 PM (12 years ago)
Author:
charles
Message:

(libT) get file preallocation working on Windows by copying how fsutil does it. (source: http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/4dabec8e-2909-40b3-b398-66bd4c587b9b/)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/fdlimit.c

    r7099 r7115  
    9696***/
    9797
     98#ifndef O_LARGEFILE
     99#define O_LARGEFILE 0
     100#endif
     101
    98102static int
    99 preallocateFile( int fd UNUSED, uint64_t length UNUSED )
    100 {
    101 #ifdef HAVE_FALLOCATE
    102 
    103     return fallocate( fd, FALLOC_FL_KEEP_SIZE, 0, length );
    104 
    105 #elif defined(HAVE_POSIX_FALLOCATE)
    106 
    107     return posix_fallocate( fd, 0, length );
    108 
    109 #elif defined(SYS_DARWIN)
    110 
    111     fstore_t fst;
    112     fst.fst_flags = F_ALLOCATECONTIG;
    113     fst.fst_posmode = F_PEOFPOSMODE;
    114     fst.fst_offset = 0;
    115     fst.fst_length = length;
    116     fst.fst_bytesalloc = 0;
    117     return fcntl( fd, F_PREALLOCATE, &fst );
     103preallocateFile( const char * filename, uint64_t length )
     104{
     105    int success = 0;
     106
     107#ifdef WIN32
     108
     109    HANDLE hFile = CreateFile( filename, GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0 );
     110    if( hFile != INVALID_HANDLE_VALUE )
     111    {
     112        LARGE_INTEGER li;
     113        li.QuadPart = desiredFileSize;
     114        success = SetFilePointerEx( hFile, desiredFileSize, NULL, FILE_BEGIN )
     115               && SetEndOfFile( hFile );
     116        CloseHandle( hFile );
     117    }
    118118
    119119#else
    120120
    121     #warning no known method to preallocate files on this platform
    122     return -1;
    123 
    124 #endif
     121    int flags = O_RDWR | O_CREAT | O_LARGEFILE;
     122    int fd = open( filename, flags, 0666 );
     123    if( fd >= 0 )
     124    {
     125       
     126# ifdef HAVE_FALLOCATE
     127
     128        success = !fallocate( fd, FALLOC_FL_KEEP_SIZE, 0, length );
     129
     130# elif defined(HAVE_POSIX_FALLOCATE)
     131
     132        success = !posix_fallocate( fd, 0, length );
     133
     134# elif defined(SYS_DARWIN)
     135
     136        fstore_t fst;
     137        fst.fst_flags = F_ALLOCATECONTIG;
     138        fst.fst_posmode = F_PEOFPOSMODE;
     139        fst.fst_offset = 0;
     140        fst.fst_length = length;
     141        fst.fst_bytesalloc = 0;
     142        success = !fcntl( fd, F_PREALLOCATE, &fst );
     143
     144# else
     145
     146        #warning no known method to preallocate files on this platform
     147        success = 0;
     148
     149# endif
     150
     151        close( fd );
     152    }
     153
     154#endif
     155
     156    return success;
    125157}
    126158
     
    162194
    163195    alreadyExisted = !stat( filename, &sb ) && S_ISREG( sb.st_mode );
     196
     197    if( doWrite && !alreadyExisted && doPreallocate )
     198        if( preallocateFile( filename, desiredFileSize ) )
     199            tr_inf( _( "Preallocated file \"%s\"" ), filename );
    164200   
    165201    /* open the file */
     
    181217    }
    182218
    183     if( ( file->fd >= 0 ) && !alreadyExisted && doPreallocate )
    184         if( !preallocateFile( file->fd, desiredFileSize ) )
    185             tr_inf( _( "Preallocated file \"%s\"" ), filename );
    186        
    187219    tr_free( filename );
    188220    return 0;
Note: See TracChangeset for help on using the changeset viewer.