Skip to content

Commit 6ac6f97

Browse files
authoredFeb 13, 2018
Merge pull request #3304 from takluyver/trash-linux-home-only
Don't trash files on different device to home dir on Linux
2 parents aa9c977 + 4200ea7 commit 6ac6f97

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed
 

‎notebook/services/contents/filemanager.py

+21-6
Original file line numberDiff line numberDiff line change
@@ -492,13 +492,28 @@ def delete_file(self, path):
492492
if not os.path.exists(os_path):
493493
raise web.HTTPError(404, u'File or directory does not exist: %s' % os_path)
494494

495+
def _check_trash(os_path):
496+
if sys.platform in {'win32', 'darwin'}:
497+
return True
498+
499+
# It's a bit more nuanced than this, but until we can better
500+
# distinguish errors from send2trash, assume that we can only trash
501+
# files on the same partition as the home directory.
502+
file_dev = os.stat(os_path).st_dev
503+
home_dev = os.stat(os.path.expanduser('~')).st_dev
504+
return file_dev == home_dev
505+
495506
if self.delete_to_trash:
496-
self.log.debug("Sending %s to trash", os_path)
497-
# Looking at the code in send2trash, I don't think the errors it
498-
# raises let us distinguish permission errors from other errors in
499-
# code. So for now, just let them all get logged as server errors.
500-
send2trash(os_path)
501-
return
507+
if _check_trash(os_path):
508+
self.log.debug("Sending %s to trash", os_path)
509+
# Looking at the code in send2trash, I don't think the errors it
510+
# raises let us distinguish permission errors from other errors in
511+
# code. So for now, just let them all get logged as server errors.
512+
send2trash(os_path)
513+
return
514+
else:
515+
self.log.warning("Skipping trash for %s, on different device "
516+
"to home directory", os_path)
502517

503518
if os.path.isdir(os_path):
504519
listing = os.listdir(os_path)

0 commit comments

Comments
 (0)
Please sign in to comment.