From 3282e1ea815fb0c220d08d5c38564554b00e2b3e Mon Sep 17 00:00:00 2001 From: Jordan Lee Date: Thu, 3 Feb 2011 00:48:05 +0000 Subject: [PATCH] (trunk libT) #3975: "tr_bencToFile() contains unnecessary calls to stat() and unlink()" -- fixed. 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 | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/libtransmission/bencode.c b/libtransmission/bencode.c index 9338815ff..d85b59aa2 100644 --- a/libtransmission/bencode.c +++ b/libtransmission/bencode.c @@ -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 {