libtorrent icon indicating copy to clipboard operation
libtorrent copied to clipboard

Renaming a file overwrites the existing file

Open glassez opened this issue 4 months ago • 5 comments

Renaming a file using torrent_handle::rename_file() overwrites the existing file which is hardly desirable behavior. It should fail instead.

glassez avatar Aug 10 '25 08:08 glassez

I believe this happens when the file being renamed has not been created yet (because nothing has been downloaded to it). This case is currently treated the same as adding a torrent where some files may already exist on disk, except, the new file won't be checked for data matching the torrent. It will just be overwritten.

Perhaps a better solution would be to always fail when creating a file for the first time (during download) if it already exists. Failing if the target file already exists when renaming would be an optimization of that, failing earlier.

arvidn avatar Aug 16 '25 13:08 arvidn

oh, it also happens because if rename() fails, we fall back to copy()

arvidn avatar Aug 16 '25 13:08 arvidn

Perhaps a better solution would be to always fail when creating a file for the first time (during download) if it already exists. Failing if the target file already exists when renaming would be an optimization of that, failing earlier.

👍

glassez avatar Aug 16 '25 14:08 glassez

oh, it also happens because if rename() fails, we fall back to copy()

Yes. But even rename() doesn't guarantee that it fails if target file exists. `std::rename() describes its behavior if target file exists as implementation-defined.

glassez avatar Aug 16 '25 14:08 glassez

https://github.com/arvidn/libtorrent/pull/8028

arvidn avatar Sep 08 '25 06:09 arvidn