loop: add LoopInfo.is_bound_to helper

Add a helper that will check if the loop devices is backed by
the file identified via the stat(2) result, i.e. the inode on
the correspoding device.
Add a correspoding test for the new helper.
This commit is contained in:
Christian Kellner 2021-08-08 09:20:18 +00:00 committed by Tom Gundersen
parent 61d5f15420
commit d6c421faf3
2 changed files with 16 additions and 1 deletions

View file

@ -43,6 +43,11 @@ class LoopInfo(ctypes.Structure):
"""Return if `LO_FLAGS_AUTOCLEAR` is set in `lo_flags`"""
return bool(self.lo_flags & Loop.LO_FLAGS_AUTOCLEAR)
def is_bound_to(self, info: os.stat_result) -> bool:
"""Return if the loop device is bound to the file `info`"""
return (self.lo_device == info.st_dev and
self.lo_inode == info.st_ino)
class Loop:
"""Loopback device

View file

@ -4,7 +4,7 @@
import contextlib
import os
from tempfile import TemporaryDirectory
from tempfile import TemporaryDirectory, TemporaryFile
import pytest
@ -46,6 +46,16 @@ def test_basic(tempdir):
assert info.lo_inode == sb.st_ino
assert info.lo_number == lo.minor
# check for `LoopInfo.is_bound_to` helper
assert info.is_bound_to(sb)
with TemporaryFile(dir=tempdir) as t:
t.write(b"")
t.flush()
st = os.fstat(t.fileno())
assert not info.is_bound_to(st)
# check for autoclear flags setting and helpers
assert info.autoclear