Skip to content

Commit 97056de

Browse files
committed
Fix WrapFS.removetree so that it respects the root path semantics
1 parent 55d2b37 commit 97056de

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

fs/wrapfs.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .copy import copy_file, copy_dir
1313
from .info import Info
1414
from .move import move_file, move_dir
15-
from .path import abspath, normpath
15+
from .path import abspath, join, normpath
1616
from .error_tools import unwrap_errors
1717

1818
if typing.TYPE_CHECKING:
@@ -217,11 +217,20 @@ def removetree(self, dir_path):
217217
# type: (Text) -> None
218218
self.check()
219219
_path = abspath(normpath(dir_path))
220-
if _path == "/":
221-
raise errors.RemoveRootError()
222-
_fs, _path = self.delegate_path(dir_path)
220+
_delegate_fs, _delegate_path = self.delegate_path(dir_path)
223221
with unwrap_errors(dir_path):
224-
_fs.removetree(_path)
222+
if _path == "/":
223+
# with root path, we must remove the contents but
224+
# not the directory itself, so we can't just directly
225+
# delegate
226+
for info in _delegate_fs.scandir(_delegate_path):
227+
info_path = join(_delegate_path, info.name)
228+
if info.is_dir:
229+
_delegate_fs.removetree(info_path)
230+
else:
231+
_delegate_fs.remove(info_path)
232+
else:
233+
_delegate_fs.removetree(_delegate_path)
225234

226235
def scandir(
227236
self,

0 commit comments

Comments
 (0)