fall back on regular delete on broken recycle bin (#926)

This commit is contained in:
Damien Elmes 2013-07-03 03:52:16 +09:00
parent 2047e5c93c
commit 64c4f1974b
2 changed files with 16 additions and 0 deletions

View file

@ -6,6 +6,7 @@
from ctypes import cdll, byref, Structure, c_char, c_char_p
from ctypes.util import find_library
import os
Foundation = cdll.LoadLibrary(find_library(u'Foundation'))
CoreServices = cdll.LoadLibrary(find_library(u'CoreServices'))
@ -33,6 +34,13 @@ def check_op_result(op_result):
raise OSError(msg)
def send2trash(path):
try:
_send2trash(path)
except OSError:
# user's system is broken; just delete
os.unlink(path)
def _send2trash(path):
if not isinstance(path, str):
path = path.encode(u'utf-8')
fp = FSRef()

View file

@ -129,6 +129,13 @@ def get_dev(path):
return os.lstat(path).st_dev
def send2trash(path):
try:
_send2trash(path)
except OSError:
# user's system is broken; just delete
os.unlink(path)
def _send2trash(path):
if not isinstance(path, unicode):
path = unicode(path, sys.getfilesystemencoding())
if not op.exists(path):
@ -155,3 +162,4 @@ def send2trash(path):
raise OSError(u"Couldn't find mount point for %s" % path)
dest_trash = find_ext_volume_trash(topdir)
trash_move(path, dest_trash, topdir)