-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Panic when deleting file with a non-UTF-8 name #105
Comments
Thanks a lot for reporting! The example will probably serve as a perfect test to add to the suite, allowing a fix to be put in place easily. Generally the codebase understands the difference between |
It's this: Lines 445 to 448 in 46585ce
You should do this with OsString. It doesn't seem like you actually need Strings. Example: diff --git a/src/freedesktop.rs b/src/freedesktop.rs
index 8373ef9..0344935 100644
--- a/src/freedesktop.rs
+++ b/src/freedesktop.rs
@@ -7,8 +7,9 @@
//!
use std::{
- borrow::Borrow,
+ borrow::{Borrow, Cow},
collections::HashSet,
+ ffi::OsString,
fs::{self, File, OpenOptions},
io::{BufRead, BufReader, Write},
os::unix::{ffi::OsStrExt, fs::PermissionsExt},
@@ -443,11 +444,18 @@ fn move_to_trash(
loop {
appendage += 1;
let in_trash_name = if appendage > 1 {
- format!("{}.{}", filename.to_str().unwrap(), appendage)
+ let mut buf = filename.to_os_string();
+ buf.push(".");
+ buf.push(appendage.to_string());
+ Cow::Owned(buf)
} else {
- filename.to_str().unwrap().into()
+ Cow::Borrowed(filename)
+ };
+ let info_name = {
+ let mut buf = OsString::from(&in_trash_name);
+ buf.push(".trashinfo");
+ buf
};
- let info_name = format!("{in_trash_name}.trashinfo");
let info_file_path = info_folder.join(&info_name);
let info_result = OpenOptions::new().create_new(true).write(true).open(&info_file_path);
match info_result { There are many other to_str().unwrap() in this code! |
Thanks for researching this, a PR would definitely be welcome. Thanks again for your help. |
I wouldn't be comfortable making the PR, because I don't know the code well enough. But you should be able to fix this easily. It was just a mistake from the start to use Strings for this. |
See: Byron/trash-rs#105 Some operating systems or file systems support non-Unicode paths (e.g. Linux and the BSDs). `trash-rs` panicked when trashing or listing the trash with non-UTF8 names. For COSMIC Files specifically, the program panics on start if the trash contains files with invalid Unicode names. It also panics when attempting to trash files with said names. To replicate: ```sh touch ''$'\250' gio trash ''$'\250' cosmic-files ```
See: Byron/trash-rs#105 Some operating systems or file systems support non-Unicode paths (e.g. Linux and the BSDs). `trash-rs` panicked when trashing or listing the trash with non-UTF8 names. For COSMIC Files specifically, the program panics on start if the trash contains files with invalid Unicode names. It also panics when attempting to trash files with said names. To replicate: ```sh touch ''$'\250' gio trash ''$'\250' cosmic-files ```
When deleting a file created by
touch ''$'\250'
, the program panics:Here's a minimal reproducing code:
I think
trash
should handle non-UTF-8 characters, or at least return an error instead of panicking.The text was updated successfully, but these errors were encountered: