Changeset 12175


Ignore:
Timestamp:
Mar 16, 2011, 4:44:38 AM (11 years ago)
Author:
jordan
Message:

(trunk libT) make readOrWriteBytes() more readable

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/inout.c

    r12173 r12175  
    4747                  size_t             buflen )
    4848{
    49     const tr_info * info = &tor->info;
    50     const tr_file * file = &info->files[fileIndex];
    51 
    52     int             fd = -1;
    53     int             err = 0;
     49    int fd;
     50    int err = 0;
    5451    const tr_bool doWrite = ioMode >= TR_IO_WRITE;
     52    const tr_info * const info = &tor->info;
     53    const tr_file * const file = &info->files[fileIndex];
    5554
    5655    assert( fileIndex < info->fileCount );
     
    6160        return 0;
    6261
     62    /***
     63    ****  Find the fd
     64    ***/
     65
    6366    fd = tr_fdFileGetCached( session, tr_torrentId( tor ), fileIndex, doWrite );
    64 
    6567    if( fd < 0 )
    6668    {
    67         /* the fd cache doesn't have this file...
    68          * we'll need to open it and maybe create it */
     69        /* it's not cached, so open/create it now */
    6970        char * subpath;
    7071        const char * base;
    71         tr_bool fileExists;
    72         tr_preallocation_mode preallocationMode;
    73 
    74         fileExists = tr_torrentFindFile2( tor, fileIndex, &base, &subpath );
    75 
    76         if( !fileExists )
     72
     73        /* see if the file exists... */
     74        if( !tr_torrentFindFile2( tor, fileIndex, &base, &subpath ) )
    7775        {
     76            /* we can't read a file that doesn't exist... */
     77            if( !doWrite )
     78                err = ENOENT;
     79
     80            /* figure out where the file should go, so we can create it */
    7881            base = tr_torrentGetCurrentDir( tor );
    79 
    80             if( tr_sessionIsIncompleteFileNamingEnabled( tor->session ) )
    81                 subpath = tr_torrentBuildPartial( tor, fileIndex );
    82             else
    83                 subpath = tr_strdup( file->name );
     82            subpath = tr_sessionIsIncompleteFileNamingEnabled( tor->session )
     83                    ? tr_torrentBuildPartial( tor, fileIndex )
     84                    : tr_strdup( file->name );
     85
    8486        }
    8587
    86         if( ( file->dnd ) || ( ioMode < TR_IO_WRITE ) )
    87             preallocationMode = TR_PREALLOCATE_NONE;
    88         else
    89             preallocationMode = tor->session->preallocationMode;
    90 
    91         if( ( ioMode < TR_IO_WRITE ) && !fileExists ) /* does file exist? */
     88        if( !err )
    9289        {
    93             err = ENOENT;
    94         }
    95         else
    96         {
     90            /* open (and maybe create) the file */
    9791            char * filename = tr_buildPath( base, subpath, NULL );
    98 
    99             if( ( fd = tr_fdFileCheckout( session, tor->uniqueId, fileIndex,
    100                                           base, filename,
    101                                           doWrite, preallocationMode, file->length ) ) < 0 )
     92            const int prealloc = file->dnd || !doWrite
     93                               ? TR_PREALLOCATE_NONE
     94                               : tor->session->preallocationMode;
     95            if((( fd = tr_fdFileCheckout( session, tor->uniqueId, fileIndex,
     96                                          base, filename, doWrite,
     97                                          prealloc, file->length ))) < 0 )
    10298            {
    10399                err = errno;
    104                 tr_torerr( tor, "tr_fdFileCheckout failed for \"%s\": %s", filename, tr_strerror( err ) );
     100                tr_torerr( tor, "tr_fdFileCheckout failed for \"%s\": %s",
     101                           filename, tr_strerror( err ) );
     102            }
     103            else if( doWrite )
     104            {
     105                /* make a note that we just created a file */
     106                tr_statsFileCreated( tor->session );
    105107            }
    106108
     
    108110        }
    109111
    110         if( doWrite && !err )
    111             tr_statsFileCreated( tor->session );
    112 
    113112        tr_free( subpath );
    114113    }
     114
     115    /***
     116    ****  Use the fd
     117    ***/
    115118
    116119    if( !err )
Note: See TracChangeset for help on using the changeset viewer.