Check access and devices before attempting trash.

This commit is contained in:
gbn 2011-03-13 14:40:52 -04:00
parent d090156c45
commit aee2b7a8af
1 changed files with 26 additions and 7 deletions

View File

@ -121,15 +121,34 @@ def find_ext_volume_trash(volume_root):
trash_dir = find_ext_volume_fallback_trash(volume_root) trash_dir = find_ext_volume_fallback_trash(volume_root)
return trash_dir return trash_dir
# Pull this out so it's easy to stub (to avoid stubbing lstat itself)
def get_dev(path):
return os.lstat(path).st_dev
def send2trash(path): def send2trash(path):
if not isinstance(path, str): if not isinstance(path, str):
path = str(path, sys.getfilesystemencoding()) path = str(path, sys.getfilesystemencoding())
if not op.exists(path): if not op.exists(path):
raise OSError("File not found: %s" % path) raise OSError("File not found: %s" % path)
try: # ...should check whether the user has the necessary permissions to delete
trash_move(path, HOMETRASH, XDG_DATA_HOME) # it, before starting the trashing operation itself. [2]
except OSError: if not os.access(path, os.W_OK):
# Check if we're on an external volume raise OSError("Permission denied: %s" % path)
mount_point = find_mount_point(path) # if the file to be trashed is on the same device as HOMETRASH we
dest_trash = find_ext_volume_trash(mount_point) # want to move it there.
trash_move(path, dest_trash, mount_point) path_dev = get_dev(path)
# If XDG_DATA_HOME or HOMETRASH do not yet exist we need to stat the
# home directory, and these paths will be created further on if needed.
trash_dev = get_dev(op.expanduser('~'))
if path_dev == trash_dev:
topdir = XDG_DATA_HOME
dest_trash = HOMETRASH
else:
topdir = find_mount_point(path)
trash_dev = get_dev(topdir)
if trash_dev != path_dev:
raise OSError("Couldn't find mount point for %s" % path)
dest_trash = find_ext_volume_trash(topdir)
trash_move(path, dest_trash, topdir)