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.
}
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
{