andreafrancia / trash-cli

Command line interface to the freedesktop.org trashcan.
GNU General Public License v2.0
3.62k stars 177 forks source link

Crash while removing file/dir wth utf-8 chars #332

Open laluka opened 8 months ago

laluka commented 8 months ago

Describe the bug

Removing a file with specific utf 8 chars breaks trash-put (and probably others) ! :upside_down_face:

trash-cli version Output of: trash-put --version trash-put 0.17.1.14 up to 0.23.11.10

Are you using the latest version of trash-cli? Yes

Have you tried if the bug is present in the latest version of trash-cli? Yes

Have done that? Then continue with the bug report.

Operating system:

To Reproduce Copy and paste the commands (and their output) to execute in order to reproduce the behavior:

mkdir $(echo -ne "\xFF\x2E\xFF\x2E")
trash-put $'\377'.$'\377'.                 # RET CODE 130

# Same with
trash-put "$(echo -ne "\xFF\x2E\xFF\x2E")"

Traceback (most recent call last):
  File "/usr/bin/trash-put", line 5, in <module>
    sys.exit(main())
  File "/usr/lib/python3/dist-packages/trashcli/put.py", line 21, in main
    ).run(sys.argv)
  File "/usr/lib/python3/dist-packages/trashcli/put.py", line 68, in run
    trashcan.trash_all(args)
  File "/usr/lib/python3/dist-packages/trashcli/put.py", line 246, in trash_all
    self.trash(arg)
  File "/usr/lib/python3/dist-packages/trashcli/put.py", line 280, in trash
    trashed_file = trash_dir.trash(file)
  File "/usr/lib/python3/dist-packages/trashcli/put.py", line 399, in trash
    content = self.format_trashinfo(original_location, self.now())
  File "/usr/lib/python3/dist-packages/trashcli/put.py", line 427, in format_trashinfo
    "Path=%s\n" % format_original_location(original_location) +
  File "/usr/lib/python3/dist-packages/trashcli/put.py", line 425, in format_original_location
    return quote(original_location,'/')
  File "/usr/lib/python3.10/urllib/parse.py", line 875, in quote
    string = string.encode(encoding, errors)
UnicodeEncodeError: 'utf-8' codec can't encode character '\udcff' in position 23: surrogates not allowed

Expected behavior File/Dir being removed

Have a marvelous day, sorry for the crash :( :heart_decoration:

penthium2 commented 8 months ago

same bug on : Linux 1fac59b8f594 6.6.12-200.fc39.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Jan 16 01:35:44 UTC 2024 x86_64 GNU/Linux

rennsax commented 7 months ago

I met this problem when I use trash-list to show my trashes.

trash-put IMG_0023.JPG
trash-list

The error:

Traceback (most recent call last):
  File "/Users/renbaijun/.local/bin/trash-list", line 5, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Users/renbaijun/.local/share/pipx/venvs/trash-cli/lib/python3.12/site-packages/trashcli/list/main.py", line 41, in main
    ).run(sys.argv)
      ^^^^^^^^^^^^^
  File "/Users/renbaijun/.local/share/pipx/venvs/trash-cli/lib/python3.12/site-packages/trashcli/list/main.py", line 91, in run
    action.run_action(args)
  File "/Users/renbaijun/.local/share/pipx/venvs/trash-cli/lib/python3.12/site-packages/trashcli/list/list_trash_action.py", line 52, in run_action
    for message in ListTrash(self.environ,
  File "/Users/renbaijun/.local/share/pipx/venvs/trash-cli/lib/python3.12/site-packages/trashcli/list/list_trash_action.py", line 100, in list_all_trash
    for msg in self._print_trashinfo(volume, trash_info,
  File "/Users/renbaijun/.local/share/pipx/venvs/trash-cli/lib/python3.12/site-packages/trashcli/list/list_trash_action.py", line 120, in _print_trashinfo
    contents = self.content_reader.contents_of(trashinfo_path)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/renbaijun/.local/share/pipx/venvs/trash-cli/lib/python3.12/site-packages/trashcli/fs.py", line 123, in contents_of
    return _read_file(path)
           ^^^^^^^^^^^^^^^^
  File "/Users/renbaijun/.local/share/pipx/venvs/trash-cli/lib/python3.12/site-packages/trashcli/fs.py", line 243, in _read_file
    return f.read()
           ^^^^^^^^
  File "<frozen codecs>", line 322, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 37: invalid start byte

trash-restore also report the same error.