loop: add get_status method

Implement a `Loop.get_status` method, to get the properties of the
loop device, corresponding to LOOP_GET_STATUS64, and counterpart
to the existing `Loop.set_status` method. Use the new `get_status`
call in the `set_status` call, replacing the existing code that
does the same thing.
Add a basic test for the `get_status` method. Also fix an actual
leak, where the loop device was closed but the fd was not cleared
inside the test.
This commit is contained in:
Christian Kellner 2021-08-07 16:06:28 +00:00 committed by Tom Gundersen
parent 2c64c65608
commit 73f24c68a2
2 changed files with 18 additions and 4 deletions

View file

@ -208,8 +208,7 @@ class Loop:
unchanged (default is None)
"""
info = LoopInfo()
fcntl.ioctl(self.fd, self.LOOP_GET_STATUS64, info)
info = self.get_status()
if offset:
info.lo_offset = offset
if sizelimit:
@ -226,6 +225,17 @@ class Loop:
info.lo_flags &= ~self.LO_FLAGS_PARTSCAN
fcntl.ioctl(self.fd, self.LOOP_SET_STATUS64, info)
def get_status(self) -> LoopInfo:
"""Get properties of the loopback device
Return a `LoopInfo` structure with the information of this
loopback device. See loop(4) for more information.
"""
info = LoopInfo()
fcntl.ioctl(self.fd, self.LOOP_GET_STATUS64, info)
return info
def set_direct_io(self, dio=True):
"""Set the direct-IO property on the loopback device

View file

@ -4,8 +4,6 @@
import contextlib
import os
from tempfile import TemporaryDirectory
import pytest
@ -39,9 +37,15 @@ def test_basic(tempdir):
f.flush()
lo = ctl.loop_for_fd(f.fileno())
sb = os.fstat(f.fileno())
assert lo
assert lo.devname
info = lo.get_status()
assert info.lo_inode == sb.st_ino
assert info.lo_number == lo.minor
finally:
if lo:
with contextlib.suppress(OSError):