]> granicus.if.org Git - transmission/commitdiff
(trunk libT) tr_torrentGetFileMTime() used to require two stat() calls.. now it only...
authorJordan Lee <jordan@transmissionbt.com>
Sat, 2 Apr 2011 07:36:34 +0000 (07:36 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Sat, 2 Apr 2011 07:36:34 +0000 (07:36 +0000)
libtransmission/inout.c
libtransmission/torrent.c
libtransmission/torrent.h

index 4503f182114a0a4ddf929ca1d4ac11d942afb229..b9da47c34c03d07af61969db7ab63ac7ff77bfb8 100644 (file)
@@ -70,7 +70,7 @@ readOrWriteBytes( tr_session       * session,
         const char * base;
 
         /* see if the file exists... */
-        if( !tr_torrentFindFile2( tor, fileIndex, &base, &subpath ) )
+        if( !tr_torrentFindFile2( tor, fileIndex, &base, &subpath, NULL ) )
         {
             /* we can't read a file that doesn't exist... */
             if( !doWrite )
index effdb3a3d845cd5707ab6039e37ddef7a43ef1a8..0e2ca0132fea0ee4137ed3340d5347c01e875f86 100644 (file)
@@ -764,7 +764,7 @@ hasAnyLocalData( const tr_torrent * tor )
     tr_file_index_t i;
 
     for( i=0; i<tor->info.fileCount; ++i )
-        if( tr_torrentFindFile2( tor, i, NULL, NULL ) )
+        if( tr_torrentFindFile2( tor, i, NULL, NULL, NULL ) )
             return true;
 
     return false;
@@ -2371,20 +2371,8 @@ tr_torrentCheckPiece( tr_torrent * tor, tr_piece_index_t pieceIndex )
 time_t
 tr_torrentGetFileMTime( const tr_torrent * tor, tr_file_index_t i )
 {
-    struct stat sb;
     time_t mtime = 0;
-    char * path = tr_torrentFindFile( tor, i );
-
-    if( ( path != NULL ) && !stat( path, &sb ) && S_ISREG( sb.st_mode ) )
-    {
-#ifdef SYS_DARWIN
-        mtime = sb.st_mtimespec.tv_sec;
-#else
-        mtime = sb.st_mtime;
-#endif
-    }
-
-    tr_free( path );
+    tr_torrentFindFile2( tor, i, NULL, NULL, &mtime );
     return mtime;
 }
 
@@ -2832,7 +2820,7 @@ setLocation( void * vdata )
             const tr_file * f = &tor->info.files[i];
             const char * oldbase;
             char * sub;
-            if( tr_torrentFindFile2( tor, i, &oldbase, &sub ) )
+            if( tr_torrentFindFile2( tor, i, &oldbase, &sub, NULL ) )
             {
                 char * oldpath = tr_buildPath( oldbase, sub, NULL );
                 char * newpath = tr_buildPath( location, sub, NULL );
@@ -2942,7 +2930,7 @@ tr_torrentFileCompleted( tr_torrent * tor, tr_file_index_t fileNum )
     /* if the torrent's current filename isn't the same as the one in the
      * metadata -- for example, if it had the ".part" suffix appended to
      * it until now -- then rename it to match the one in the metadata */
-    if( tr_torrentFindFile2( tor, fileNum, &base, &sub ) )
+    if( tr_torrentFindFile2( tor, fileNum, &base, &sub, NULL ) )
     {
         if( strcmp( sub, f->name ) )
         {
@@ -2965,16 +2953,26 @@ tr_torrentFileCompleted( tr_torrent * tor, tr_file_index_t fileNum )
 ***/
 
 static bool
-fileExists( const char * filename )
+fileExists( const char * filename, time_t * mtime )
 {
     struct stat sb;
     const bool ok = !stat( filename, &sb );
+
+    if( ok && ( mtime != NULL ) )
+    {
+#ifdef SYS_DARWIN
+        *mtime = sb.st_mtimespec.tv_sec;
+#else
+        *mtime = sb.st_mtime;
+#endif
+    }
+
     return ok;
 }
 
 bool
 tr_torrentFindFile2( const tr_torrent * tor, tr_file_index_t fileNum,
-                     const char ** base, char ** subpath )
+                     const char ** base, char ** subpath, time_t * mtime )
 {
     char * part = NULL;
     const tr_file * file;
@@ -2988,7 +2986,7 @@ tr_torrentFindFile2( const tr_torrent * tor, tr_file_index_t fileNum,
 
     if( b == NULL ) {
         char * filename = tr_buildPath( tor->downloadDir, file->name, NULL );
-        if( fileExists( filename ) ) {
+        if( fileExists( filename, mtime ) ) {
             b = tor->downloadDir;
             s = file->name;
         }
@@ -2997,7 +2995,7 @@ tr_torrentFindFile2( const tr_torrent * tor, tr_file_index_t fileNum,
 
     if( ( b == NULL ) && ( tor->incompleteDir != NULL ) ) {
         char * filename = tr_buildPath( tor->incompleteDir, file->name, NULL );
-        if( fileExists( filename ) ) {
+        if( fileExists( filename, mtime ) ) {
             b = tor->incompleteDir;
             s = file->name;
         }
@@ -3009,7 +3007,7 @@ tr_torrentFindFile2( const tr_torrent * tor, tr_file_index_t fileNum,
 
     if( ( b == NULL ) && ( tor->incompleteDir != NULL ) ) {
         char * filename = tr_buildPath( tor->incompleteDir, part, NULL );
-        if( fileExists( filename ) ) {
+        if( fileExists( filename, mtime ) ) {
             b = tor->incompleteDir;
             s = part;
         }
@@ -3018,7 +3016,7 @@ tr_torrentFindFile2( const tr_torrent * tor, tr_file_index_t fileNum,
 
     if( b == NULL) {
         char * filename = tr_buildPath( tor->downloadDir, part, NULL );
-        if( fileExists( filename ) ) {
+        if( fileExists( filename, mtime ) ) {
             b = tor->downloadDir;
             s = part;
         }
@@ -3041,7 +3039,7 @@ tr_torrentFindFile( const tr_torrent * tor, tr_file_index_t fileNum )
     char * ret = NULL;
     const char * base;
 
-    if( tr_torrentFindFile2( tor, fileNum, &base, &subpath ) )
+    if( tr_torrentFindFile2( tor, fileNum, &base, &subpath, NULL ) )
     {
         ret = tr_buildPath( base, subpath, NULL );
         tr_free( subpath );
@@ -3060,7 +3058,7 @@ refreshCurrentDir( tr_torrent * tor )
         dir = tor->downloadDir;
     else if( !tr_torrentHasMetadata( tor ) ) /* no files to find */
         dir = tor->incompleteDir;
-    else if( !tr_torrentFindFile2( tor, 0, &dir, NULL ) )
+    else if( !tr_torrentFindFile2( tor, 0, &dir, NULL, NULL ) )
         dir = tor->incompleteDir;
 
     assert( dir != NULL );
index 7612e867317e4b860e9fae57e7f620820d3465a8..a25ac67798c4b2bf256a0edfb672e4185c3b7b0f 100644 (file)
@@ -395,7 +395,7 @@ void tr_torrentFileCompleted( tr_torrent * tor, tr_file_index_t fileNo );
  *                string holding the second half of the filename.
  */
 bool tr_torrentFindFile2( const tr_torrent *, tr_file_index_t fileNo,
-                          const char ** base, char ** subpath );
+                          const char ** base, char ** subpath, time_t * mtime );
 
 
 /* Returns a newly-allocated version of the tr_file.name string