(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.
This commit is contained in:
Jordan Lee 2011-02-03 00:48:05 +00:00
parent e8614030a2
commit 3282e1ea81
1 changed files with 2 additions and 14 deletions

View 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
{