Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/test_empty/cmd/test_empty_cmd_fs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Copyright (C) 2011-2022 Andrea Francia Bereguardo(PV) Italy
import os
import stat
import unittest

import pytest
Expand Down Expand Up @@ -51,3 +53,37 @@ def test_trash_empty_will_skip_unreadable_dir(self):
def tearDown(self):
make_readable(self.unreadable_dir)
self.tmp_dir.clean_up()


class TestTrashEmptyRemovesReadonlyDir:
def setup_method(self):
self.tmp_dir = MyPath.make_temp_dir()
self.trash_files = self.tmp_dir / 'data/Trash/files'
volumes_listing = Mock(spec=VolumesListing)
volumes_listing.list_volumes.return_value = [self.trash_files]
self.err = StringIO()
self.environ = {'XDG_DATA_HOME': self.tmp_dir / 'data'}
self.empty = EmptyCmd(
argv0='trash-empty', out=StringIO(), err=self.err,
volumes_listing=volumes_listing, now=None,
file_reader=RealTopTrashDirRulesReader(),
file_remover=ExistingFileRemover(),
content_reader=FileSystemContentReader(),
dir_reader=FileSystemDirReader(), version='unused',
volumes=StubVolumeOf())

def test_a_directory_without_write_permission_is_removed(self):
target = self.trash_files / 'proj'
os.makedirs(target / 'sub')
with open(target / 'sub' / 'f', 'w') as f:
f.write('x')
os.chmod(target / 'sub', stat.S_IRUSR | stat.S_IXUSR)
os.chmod(target, stat.S_IRUSR | stat.S_IXUSR)

self.empty.run_cmd([], self.environ, uid=123)

assert not os.path.exists(target)
assert self.err.getvalue() == ""

def teardown_method(self):
self.tmp_dir.clean_up()
15 changes: 14 additions & 1 deletion trashcli/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,20 @@ def remove_file2(self, path):
try:
os.remove(path)
except OSError:
shutil.rmtree(path)
try:
shutil.rmtree(path)
except OSError:
_add_write_permission(path)
shutil.rmtree(path)


def _add_write_permission(path):
# add write and search bits to owned directories so their entries can be removed; an unreadable directory is left as is and still reported
for root, dirs, files in os.walk(path):
try:
os.chmod(root, os.stat(root).st_mode | stat.S_IWUSR | stat.S_IXUSR)
except OSError:
pass


class RealRemoveFileIfExists(RemoveFileIfExists, RemoveFile2):
Expand Down