Ticket #1486: inout.c-cleaned-code.patch

File inout.c-cleaned-code.patch, 2.9 KB (added by spry, 12 years ago)
  • inout.c

     
    8585        err = errno;
    8686    else if( ( fd = tr_fdFileCheckout ( tor->downloadDir, file->name, ioMode == TR_IO_WRITE, !file->dnd, file->length ) ) < 0 )
    8787        err = errno;
     88#if defined(WIN32)
     89
     90// Means we are compiling from MSVS
     91# if defined(_MSC_VER)
     92    // _lseeki64 declaration is
     93    // __int64 __cdecl _lseeki64(__in int _FileHandle, __in __int64 _Offset, __in int _Origin)
     94    // So result should be compared long
     95    else if( lseek( fd, fileOffset, SEEK_SET ) == ( -1L ) )
     96# else
     97    //
     98    // As lseek is redefined above to _lseeki64, casting to off_t (1) cannot be
     99    // performed on MinGW because large file support is not supported and (2) is
     100    // not necessary on MSVC because the function accepts __int64 anyway.
     101    //
     102    else if( lseek( fd, fileOffset, SEEK_SET ) == ( -1 ) )
     103# endif
     104#else
    88105    else if( lseek( fd, (off_t)fileOffset, SEEK_SET ) == ( (off_t)-1 ) )
     106#endif
    89107        err = errno;
    90108    else if( func( fd, buf, buflen ) != buflen )
    91109        err = errno;
     
    136154    assert( tor->info.files[*fileIndex].offset + *fileOffset == offset );
    137155}
    138156
    139 #ifdef WIN32
    140 /* return 0 on success, or an errno on failure */
    141 static int
    142 ensureMinimumFileSize( const tr_torrent * tor,
    143                        tr_file_index_t    fileIndex,
    144                        uint64_t           minBytes )
    145 {
    146     int             fd;
    147     int             err;
    148     struct stat     sb;
    149     const tr_file * file = &tor->info.files[fileIndex];
    150 
    151     assert( 0 <= fileIndex && fileIndex < tor->info.fileCount );
    152     assert( minBytes <= file->length );
    153 
    154     fd = tr_fdFileCheckout( tor->downloadDir,
    155                             file->name, TRUE, !file->dnd, file->length );
    156 
    157     if( fd < 0 ) /* bad fd */
    158         err = errno;
    159     else if( fstat ( fd, &sb ) ) /* how big is the file? */
    160         err = errno;
    161     else if( sb.st_size >= (off_t)minBytes ) /* already big enough */
    162         err = 0;
    163     else if( !tr_ftruncate( fd, minBytes ) )  /* grow it */
    164         err = 0;
    165     else /* couldn't grow it */
    166         err = errno;
    167 
    168     if( fd >= 0 )
    169         tr_fdFileReturn( fd );
    170 
    171     return err;
    172 }
    173 
    174 #endif
    175 
    176157/* returns 0 on success, or an errno on failure */
    177158static int
    178159readOrWritePiece( const tr_torrent * tor,
     
    201182        const uint64_t  bytesThisPass = MIN( buflen,
    202183                                             file->length - fileOffset );
    203184
    204 #ifdef WIN32
    205         if( ioMode == TR_IO_WRITE )
    206             err = ensureMinimumFileSize( tor, fileIndex,
    207                                          fileOffset + bytesThisPass );
    208         if( !err )
    209 #endif
    210185        err = readOrWriteBytes( tor, ioMode,
    211186                                fileIndex, fileOffset, buf, bytesThisPass );
    212187        buf += bytesThisPass;