From 970deaebe765e8f56bc342aa03e5c81c8d4c9c8f Mon Sep 17 00:00:00 2001 From: stickz Date: Sun, 2 Jun 2024 09:26:25 -0400 Subject: [PATCH] rTorrent: Fix & optimize write_bencode - Test the bencode string before we write it. - Use fdatasync instead of fsync. We only need to sync the metadata if changed. --- rtorrent/src/core/download_store.cc | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/rtorrent/src/core/download_store.cc b/rtorrent/src/core/download_store.cc index ec21e0bf..b3251416 100644 --- a/rtorrent/src/core/download_store.cc +++ b/rtorrent/src/core/download_store.cc @@ -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; @@ -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: