Byron / trash-rs

A Rust library for moving files to the Recycle Bin
MIT License
170 stars 28 forks source link

Trying to delete the path `""` unexpectedly deletes the current working directory #73

Closed ericcornelissen closed 1 year ago

ericcornelissen commented 1 year ago

Calling trash::delete(""); will result in the current working directory being deleted (tested on Ubuntu 22.04). I think this is unexpected because:

  1. In Rust std::path::Path::new("").exists() returns false, whereas replacing "" with a file that does exist in the current working directory returns true.
  2. In Rust std::fs::remove_dir("") will not canonicalize the path to the current working directory, instead it will error with a NotFound error.
  3. Any CLI program operating on the file system I tested complaints that "" doesn't exist, e.g.:

    $ rm ""
    rm: cannot remove '': No such file or directory

The behavior seems to originate from this logic:

https://github.com/Byron/trash-rs/blob/e20fe6ae94aa73d07ff31d911ad9ecf98b17f3a8/src/lib.rs#L226-L230

I'd guess adding an explicit check for a "" path that returns an error would resolve the unexpected behavior, but I don't know the code base well enough to say if that makes sense.

Byron commented 1 year ago

Thanks a lot for reporting and for investigating this! I think the fix is exactly what you describe, and I will publish a fix soon.

Byron commented 1 year ago

The fix was released in v3.0.3, such that it will now error if the path is empty. Please see the linked commit in case you have any suggestions on how to do that better.

ericcornelissen commented 1 year ago

Thank you for the fix (and quick response)! The implementation looks fine to me.