Skip to content

Commit

Permalink
Setup loop devices in read-write mode if permitted
Browse files Browse the repository at this point in the history
See discussion in #237.
  • Loading branch information
coldfix committed Feb 2, 2022
1 parent 1dc2a24 commit 3909ac3
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions udiskie/mount.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ async def remove_all(self, detach=False, eject=False, lock=False):
return success

# loop devices
async def losetup(self, image, read_only=True, offset=None, size=None,
async def losetup(self, image, read_only=None, offset=None, size=None,
no_part_scan=None):
"""
Setup a loop device.
Expand All @@ -595,16 +595,31 @@ async def losetup(self, image, read_only=True, offset=None, size=None,
if not os.path.isfile(image):
self._log.error(_('not setting up {0}: not a file', image))
return None
self._log.debug(_('setting up {0}', image))
fd = os.open(image, os.O_RDONLY)
self._log.debug(_(
'setting up loop device {0} ({1})',
image, 'ro' if read_only else 'rw'))

if not read_only:
try:
fd = os.open(image, os.O_RDWR)
except PermissionError:
self._log.debug(_(
'Insufficient permission to open {0} in read-write mode. '
'Retrying in read-only mode.', image))
read_only = True
if read_only:
fd = os.open(image, os.O_RDONLY)

device = await self.udisks.loop_setup(fd, {
'offset': offset,
'size': size,
'read-only': read_only,
'read-only': bool(read_only),
'no-part-scan': no_part_scan,
})
self._log.info(_('set up {0} as {1}', image,
device.device_presentation))
self._log.info(_(
'set up {0} as {1} ({2})',
image, device.device_presentation,
'ro' if read_only else 'rw'))
return device

@_error_boundary
Expand Down

0 comments on commit 3909ac3

Please sign in to comment.