Skip to content

Commit

Permalink
rTorrent: Fix & optimize write_bencode
Browse files Browse the repository at this point in the history
- Test the bencode string before we write it.

- Use fdatasync instead of fsync. We only need to sync the metadata if changed.
  • Loading branch information
stickz committed Jun 2, 2024
1 parent d0a39f8 commit 970deae
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions rtorrent/src/core/download_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,6 @@ DownloadStore::write_bencode(const std::string& filename, const torrent::Object&

output.close();

// Ensure that the new file is actually written to the disk
fd = ::open(filename.c_str(), O_WRONLY);
if (fd < 0)
goto download_store_save_error;

fsync(fd);
::close(fd);

// Test the new file, to ensure it is a valid bencode string.
output.open(filename.c_str(), std::ios::in);
output >> tmp;
Expand All @@ -131,6 +123,15 @@ DownloadStore::write_bencode(const std::string& filename, const torrent::Object&
goto download_store_save_error;

output.close();

// Ensure that the new file is actually written to the disk
fd = ::open(filename.c_str(), O_WRONLY);
if (fd < 0)
goto download_store_save_error;

fdatasync(fd);
::close(fd);

return true;

download_store_save_error:
Expand Down

0 comments on commit 970deae

Please sign in to comment.