Skip to content
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

Fix crashes when deleting to trash (#1798) #1871

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 45 additions & 4 deletions ranger/config/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,30 +727,71 @@ def is_directory_with_files(path):
return os.path.isdir(path) and not os.path.islink(path) and len(os.listdir(path)) > 0

if self.rest(1):
files = shlex.split(self.rest(1))
many_files = (len(files) > 1 or is_directory_with_files(files[0]))
file_names = shlex.split(self.rest(1))
files = self.paths_to_filesystem_objects(file_names)
if files is None:
return
many_files = (len(files) > 1 or is_directory_with_files(files[0].path))
else:
cwd = self.fm.thisdir
tfile = self.fm.thisfile
if not cwd or not tfile:
self.fm.notify("Error: no file selected for deletion!", bad=True)
return

files = self.fm.thistab.get_selection()
# relative_path used for a user-friendly output in the confirmation.
files = [f.relative_path for f in self.fm.thistab.get_selection()]
file_names = [f.relative_path for f in files]
many_files = (cwd.marked_items or is_directory_with_files(tfile.path))

confirm = self.fm.settings.confirm_on_delete
if confirm != 'never' and (confirm != 'multiple' or many_files):
self.fm.ui.console.ask(
"Confirm deletion of: %s (y/N)" % ', '.join(files),
toonn marked this conversation as resolved.
Show resolved Hide resolved
"Confirm deletion of: %s (y/N)" % ', '.join(file_names),
partial(self._question_callback, files),
('n', 'N', 'y', 'Y'),
)
else:
# no need for a confirmation, just delete
self.fm.execute_file(files, label='trash')

@staticmethod
def group_paths_by_dirname(paths):
"""
Groups the paths into a dictionary with their dirnames as keys and a set of
basenames as entries.
"""
groups = dict()
for path in paths:
abspath = os.path.abspath(os.path.expanduser(path))
dirname, basename = os.path.split(abspath)
groups.setdefault(dirname, set()).add(basename)
return groups

def paths_to_filesystem_objects(self, paths):
toonn marked this conversation as resolved.
Show resolved Hide resolved
"""
Find FileSystemObjects corresponding to the paths if they are already in
memory and load those that are not.
"""
result = []
# Grouping the files by dirname avoids the potentially quadratic running time of doing
# a linear search in the directory for each entry name that is supposed to be deleted.
groups = trash.group_paths_by_dirname(paths)
for dirname, basenames in groups.items():
directory = self.fm.get_directory(dirname)
directory.load_content_if_outdated()
for entry in directory.files_all:
if entry.basename in basenames:
result.append(entry)
basenames.remove(entry.basename)
if basenames != set():
# Abort the operation with an error message if there are entries
# that weren't found.
names = ', '.join(basenames)
self.fm.notify('Error: No such file or directory: {}'.format(names), bad=True)
return None
return result

def tab(self, tabnum):
return self._tab_directory_content()

Expand Down