From 73f24c68a2b2dcc295f13ee84b6eb88b614e2036 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Sat, 7 Aug 2021 16:06:28 +0000 Subject: [PATCH] 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. --- osbuild/loop.py | 14 ++++++++++++-- test/mod/test_loop.py | 8 ++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/osbuild/loop.py b/osbuild/loop.py index eb89b7a0..9750be6e 100644 --- a/osbuild/loop.py +++ b/osbuild/loop.py @@ -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 diff --git a/test/mod/test_loop.py b/test/mod/test_loop.py index e8ad1493..e5f9f730 100644 --- a/test/mod/test_loop.py +++ b/test/mod/test_loop.py @@ -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):