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

Don't trash non-empty directories on Windows #3673

Merged
merged 2 commits into from
Jun 12, 2018
Merged
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
23 changes: 16 additions & 7 deletions notebook/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,21 @@ def _check_trash(os_path):
home_dev = os.stat(os.path.expanduser('~')).st_dev
return file_dev == home_dev

def is_non_empty_dir(os_path):
if os.path.isdir(os_path):
# A directory containing only leftover checkpoints is
# considered empty.
cp_dir = getattr(self.checkpoints, 'checkpoint_dir', None)
if set(os.listdir(os_path)) - {cp_dir}:
return True

return False

if self.delete_to_trash:
if sys.platform == 'win32' and is_non_empty_dir(os_path):
# send2trash can really delete files on Windows, so disallow
# deleting non-empty files. See Github issue 3631.
raise web.HTTPError(400, u'Directory %s not empty' % os_path)
if _check_trash(os_path):
self.log.debug("Sending %s to trash", os_path)
# Looking at the code in send2trash, I don't think the errors it
Expand All @@ -530,14 +544,9 @@ def _check_trash(os_path):
"to home directory", os_path)

if os.path.isdir(os_path):
listing = os.listdir(os_path)
# Don't permanently delete non-empty directories.
# A directory containing only leftover checkpoints is
# considered empty.
cp_dir = getattr(self.checkpoints, 'checkpoint_dir', None)
for entry in listing:
if entry != cp_dir:
raise web.HTTPError(400, u'Directory %s not empty' % os_path)
if is_non_empty_dir(os_path):
raise web.HTTPError(400, u'Directory %s not empty' % os_path)
self.log.debug("Removing directory %s", os_path)
with self.perm_to_403():
shutil.rmtree(os_path)
Expand Down
3 changes: 3 additions & 0 deletions notebook/services/contents/tests/test_contents_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import os
import shutil
import sys
from unicodedata import normalize

pjoin = os.path.join
Expand Down Expand Up @@ -523,6 +524,8 @@ def test_delete_dirs(self):
self.assertEqual(listing, [])

def test_delete_non_empty_dir(self):
if sys.platform == 'win32':
self.skipTest("Disabled deleting non-empty dirs on Windows")
# Test that non empty directory can be deleted
self.api.delete(u'å b')
# Check if directory has actually been deleted
Expand Down