Better error message for permission denied when accessing device files on macOS

This commit is contained in:
Kovid Goyal 2021-11-17 16:24:59 +05:30
parent 21e76f7d73
commit 37c35a1ec4
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -14,7 +14,7 @@ for a particular device.
import os, time, json, shutil import os, time, json, shutil
from itertools import cycle from itertools import cycle
from calibre.constants import numeric_version from calibre.constants import numeric_version, ismacos
from calibre import prints, isbytestring, fsync from calibre import prints, isbytestring, fsync
from calibre.constants import filesystem_encoding, DEBUG from calibre.constants import filesystem_encoding, DEBUG
from calibre.devices.usbms.cli import CLI from calibre.devices.usbms.cli import CLI
@ -147,17 +147,30 @@ class USBMS(CLI, Device):
def get_device_information(self, end_session=True): def get_device_information(self, end_session=True):
self.report_progress(1.0, _('Get device information...')) self.report_progress(1.0, _('Get device information...'))
self.driveinfo = {} self.driveinfo = {}
def raise_os_error(e):
raise OSError(_('Failed to access files in the main memory of'
' your device. You should contact the device'
' manufacturer for support. Common fixes are:'
' try a different USB cable/USB port on your computer.'
' If you device has a "Reset to factory defaults" type'
' of setting somewhere, use it. Underlying error: %s')
% e) from e
if self._main_prefix is not None: if self._main_prefix is not None:
try: try:
self.driveinfo['main'] = self._update_driveinfo_file(self._main_prefix, 'main') self.driveinfo['main'] = self._update_driveinfo_file(self._main_prefix, 'main')
except PermissionError as e:
if ismacos:
raise PermissionError(_(
'Permission was denied by macOS trying to access files in the main memory of'
' your device. You will need to grant permission explicitly by looking under'
' System Preferences > Security and Privacy > Privacy > Files and Folders.'
' Underlying error: %s'
) % e) from e
raise_os_error(e)
except OSError as e: except OSError as e:
raise OSError(_('Failed to access files in the main memory of' raise_os_error(e)
' your device. You should contact the device'
' manufacturer for support. Common fixes are:'
' try a different USB cable/USB port on your computer.'
' If you device has a "Reset to factory defaults" type'
' of setting somewhere, use it. Underlying error: %s')
% e)
try: try:
if self._card_a_prefix is not None: if self._card_a_prefix is not None:
self.driveinfo['A'] = self._update_driveinfo_file(self._card_a_prefix, 'A') self.driveinfo['A'] = self._update_driveinfo_file(self._card_a_prefix, 'A')