]> granicus.if.org Git - transmission/commitdiff
(trunk libT) #3975: "tr_bencToFile() contains unnecessary calls to stat() and unlink...
authorJordan Lee <jordan@transmissionbt.com>
Thu, 3 Feb 2011 00:48:05 +0000 (00:48 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Thu, 3 Feb 2011 00:48:05 +0000 (00:48 +0000)
When saving a tr_benc object to disk at $dst, bencode.c saves it to a tmp file in the same directory as $dst, unlinks $dst if it exists, and then renames $tmp as $dst. This commit removes the middle step, which is unnecessary because rename() has guarantees about atomically overwriting $dst.

libtransmission/bencode.c

index 9338815ff70af00153174e84ffab0b8291864f51..d85b59aa2d55269f6834ab1035d3ddd2ab67b25f 100644 (file)
@@ -1707,24 +1707,12 @@ tr_bencToFile( const tr_benc * top, tr_fmt_mode mode, const char * filename )
         }
         else
         {
-            struct stat sb;
-            const tr_bool already_exists = !stat( filename, &sb ) && S_ISREG( sb.st_mode );
-
             tr_fsync( fd );
             tr_close_file( fd );
 
-            if( !already_exists || !unlink( filename ) )
+            if( !rename( tmp, filename ) )
             {
-                if( !rename( tmp, filename ) )
-                {
-                    tr_inf( _( "Saved \"%s\"" ), filename );
-                }
-                else
-                {
-                    err = errno;
-                    tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), filename, tr_strerror( err ) );
-                    unlink( tmp );
-                }
+                tr_inf( _( "Saved \"%s\"" ), filename );
             }
             else
             {