]> granicus.if.org Git - transmission/commitdiff
(trunk libT) convert tr_open_file_for_scanning() and tr_close_file() to use file...
authorCharles Kerr <charles@transmissionbt.com>
Sun, 26 Apr 2009 20:44:18 +0000 (20:44 +0000)
committerCharles Kerr <charles@transmissionbt.com>
Sun, 26 Apr 2009 20:44:18 +0000 (20:44 +0000)
libtransmission/fdlimit.c
libtransmission/fdlimit.h
libtransmission/utils.c
libtransmission/verify.c

index 1795d67400980839bfb901b70cb9b4d42d0d8587..dd58034b7e43089b0fcfee347ac537d12609252f 100644 (file)
@@ -201,7 +201,7 @@ preallocateFileFull( const char * filename, uint64_t length )
     return success;
 }
 
-FILE*
+int
 tr_open_file_for_scanning( const char * filename )
 {
     int fd;
@@ -224,33 +224,27 @@ tr_open_file_for_scanning( const char * filename )
 
     /* open the file */
     fd = open( filename, flags, 0666 );
-    if( fd < 0 )
-        return NULL;
-
+    if( fd >= 0 )
+    {
 #ifdef HAVE_POSIX_FADVISE
-    posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL );
+        posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL );
 #endif
 #ifdef SYS_DARWIN
-    fcntl( fd, F_NOCACHE, 1 );
-    fcntl( fd, F_RDAHEAD, 1 );
+        fcntl( fd, F_NOCACHE, 1 );
+        fcntl( fd, F_RDAHEAD, 1 );
 #endif
+    }
 
-    return fdopen( fd, "r" );
+    return fd;
 }
 
-static void
-flush_before_closing( int fd )
+void
+tr_close_file( int fd )
 {
 #if defined(HAVE_POSIX_FADVISE)
     posix_fadvise( fd, 0, 0, POSIX_FADV_DONTNEED );
 #endif
-}
-
-void
-tr_close_file( FILE * fp )
-{
-    flush_before_closing( fileno( fp ) );
-    fclose( fp );
+    close( fd );
 }
 
 /**
@@ -346,7 +340,7 @@ TrCloseFile( int i )
     assert( i < gFd->openFileLimit );
     assert( fileIsOpen( o ) );
 
-    flush_before_closing( o->fd );
+    tr_close_file( o->fd );
     close( o->fd );
     o->fd = -1;
     o->isCheckedOut = 0;
index 958fccbfcb3e6693fb558deeb7f9bae6238f1b6c..fbf652b140e18421f45569668651268be21f8f17 100644 (file)
@@ -32,9 +32,9 @@
 void tr_fdInit( size_t openFileLimit,
                 size_t globalPeerLimit );
 
-FILE* tr_open_file_for_scanning( const char * filename );
+int tr_open_file_for_scanning( const char * filename );
 
-void tr_close_file( FILE * fp );
+void tr_close_file( int fd );
 
 int64_t tr_lseek( int fd, int64_t offset, int whence );
 
index 1271c8afc5ad0735178066c202b5b457679a1cad..c57b44738ffa1fcd2209adbb540dd85b3a90d894 100644 (file)
@@ -466,9 +466,9 @@ uint8_t *
 tr_loadFile( const char * path,
              size_t *     size )
 {
-    uint8_t *    buf;
+    uint8_t * buf;
     struct stat  sb;
-    FILE *       file;
+    int fd;
     const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" );
 
     /* try to stat the file */
@@ -489,8 +489,8 @@ tr_loadFile( const char * path,
     }
 
     /* Load the torrent file into our buffer */
-    file = tr_open_file_for_scanning( path );
-    if( !file )
+    fd = tr_open_file_for_scanning( path );
+    if( fd < 0 )
     {
         const int err = errno;
         tr_err( err_fmt, path, tr_strerror( errno ) );
@@ -502,21 +502,21 @@ tr_loadFile( const char * path,
     {
         const int err = errno;
         tr_err( err_fmt, path, _( "Memory allocation failed" ) );
-        tr_close_file( file );
+        tr_close_file( fd );
         errno = err;
         return NULL;
     }
-    if( fread( buf, sb.st_size, 1, file ) != 1 )
+    if( read( fd, buf, sb.st_size ) != sb.st_size )
     {
         const int err = errno;
         tr_err( err_fmt, path, tr_strerror( errno ) );
-        tr_close_file( file );
+        tr_close_file( fd );
         free( buf );
         errno = err;
         return NULL;
     }
 
-    tr_close_file( file );
+    tr_close_file( fd );
     *size = sb.st_size;
     return buf;
 }
index b9956995c26e5b02f152151bb5e4cbcbed97d111..d393520d27e7e11279fd9c773f3986c4f8882dbc 100644 (file)
@@ -42,7 +42,7 @@ static tr_bool
 verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
 {
     SHA_CTX sha;
-    FILE * fp = NULL;
+    int fd = -1;
     int64_t filePos = 0;
     tr_bool changed = 0;
     tr_bool hadPiece = 0;
@@ -72,11 +72,11 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
         }
 
         /* if we're starting a new file... */
-        if( !filePos && !fp )
+        if( !filePos && (fd<0) )
         {
             char * filename = tr_buildPath( tor->downloadDir, file->name, NULL );
-            fp = tr_open_file_for_scanning( filename );
-            /* fprintf( stderr, "opening file #%d (%s) -- %p\n", fileIndex, filename, fp ); */
+            fd = tr_open_file_for_scanning( filename );
+            /* fprintf( stderr, "opening file #%d (%s) -- %d\n", fileIndex, filename, fd ); */
             tr_free( filename );
         }
 
@@ -88,8 +88,8 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
         /* fprintf( stderr, "reading this pass: %d\n", (int)bytesThisPass ); */
 
         /* read a bit */
-        if( fp && tr_lseek( fileno(fp), filePos, SEEK_SET ) != -1 ) {
-            const int64_t numRead = fread( buffer, 1, bytesThisPass, fp );
+        if( (fd>=0) && tr_lseek( fd, filePos, SEEK_SET ) != -1 ) {
+            const int64_t numRead = read( fd, buffer, bytesThisPass );
             if( numRead == bytesThisPass )
                 SHA1_Update( &sha, buffer, numRead );
         }
@@ -129,15 +129,15 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
         if( leftInFile == 0 )
         {
             /* fprintf( stderr, "closing file\n" ); */
-            if( fp != NULL ) { tr_close_file( fp ); fp = NULL; }
+            if( fd >= 0 ) { tr_close_file( fd ); fd = -1; }
             ++fileIndex;
             filePos = 0;
         }
     }
 
     /* cleanup */
-    if( fp != NULL )
-        tr_close_file( fp );
+    if( fd >= 0 )
+        tr_close_file( fd );
     tr_free( buffer );
 
 #ifdef STOPWATCH