From 29d2fcf40c058bd3d01fb55a2402bd2394b3f0af Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 24 Jun 2021 18:17:28 +0530 Subject: [PATCH] Start work on replacing dbus in udisks --- src/calibre/devices/udisks.py | 77 +++-------------------------------- 1 file changed, 5 insertions(+), 72 deletions(-) diff --git a/src/calibre/devices/udisks.py b/src/calibre/devices/udisks.py index d0ad9e1985..182971e9e1 100644 --- a/src/calibre/devices/udisks.py +++ b/src/calibre/devices/udisks.py @@ -28,57 +28,10 @@ def node_mountpoint(node): return None -class NoUDisks1(Exception): - pass - - def basic_mount_options(): return ['rw', 'noexec', 'nosuid', 'nodev', 'uid=%d'%os.geteuid(), 'gid=%d'%os.getegid()] -class UDisks(object): - - def __init__(self): - import dbus - self.bus = dbus.SystemBus() - try: - self.main = dbus.Interface(self.bus.get_object('org.freedesktop.UDisks', - '/org/freedesktop/UDisks'), 'org.freedesktop.UDisks') - except dbus.exceptions.DBusException as e: - if getattr(e, '_dbus_error_name', None) == 'org.freedesktop.DBus.Error.ServiceUnknown': - raise NoUDisks1() - raise - - def device(self, device_node_path): - import dbus - devpath = self.main.FindDeviceByDeviceFile(device_node_path) - return dbus.Interface(self.bus.get_object('org.freedesktop.UDisks', - devpath), 'org.freedesktop.UDisks.Device') - - def mount(self, device_node_path): - d = self.device(device_node_path) - try: - return unicode_type(d.FilesystemMount('', - ['auth_no_user_interaction'] + basic_mount_options())) - except Exception: - # May be already mounted, check - mp = node_mountpoint(unicode_type(device_node_path)) - if mp is None: - raise - return mp - - def unmount(self, device_node_path): - d = self.device(device_node_path) - d.FilesystemUnmount(['force']) - - def eject(self, device_node_path): - parent = device_node_path - while parent[-1] in '0123456789': - parent = parent[:-1] - d = self.device(parent) - d.DriveEject([]) - - class NoUDisks2(Exception): pass @@ -167,42 +120,22 @@ class UDisks2(object): dbus_interface=self.DRIVE) -def get_udisks(ver=None): - if ver is None: - try: - u = UDisks2() - except NoUDisks2: - u = UDisks() - return u - return UDisks2() if ver == 2 else UDisks() - - -def get_udisks1(): - u = None - try: - u = UDisks() - except NoUDisks1: - try: - u = UDisks2() - except NoUDisks2: - pass - if u is None: - raise EnvironmentError('UDisks not available on your system') - return u +def get_udisks(): + return UDisks2() def mount(node_path): - u = get_udisks1() + u = get_udisks() u.mount(node_path) def eject(node_path): - u = get_udisks1() + u = get_udisks() u.eject(node_path) def umount(node_path): - u = get_udisks1() + u = get_udisks() u.unmount(node_path)