From 9965a2d2cbb97f51cb5eca6bffd7eae2762ee30a Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 14 Oct 2023 21:38:29 +0200 Subject: _mountinfo: replace list of entries with OrderedDict Mapping mount id to entries. --- lbup/_mountinfo.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/lbup/_mountinfo.py b/lbup/_mountinfo.py index 763884b..673378a 100644 --- a/lbup/_mountinfo.py +++ b/lbup/_mountinfo.py @@ -1,3 +1,5 @@ +from collections import OrderedDict + import os.path from ._path import AbsPath @@ -98,32 +100,39 @@ class MountInfo: A wrapper around the contents of the Linux /proc//mountinfo file. """ - "a list of _MountEntry" - entries = None + mounts = None + "OrderedDict of 'mount ID' -> _MountEntry" def __init__(self, data): - self.entries = [] + mounts = OrderedDict() for line in data.splitlines(): try: - self.entries.append(_MountEntry(line)) + e = _MountEntry(line) except NotAbsRoot: pass + if e.mount_id in mounts: + raise ValueError('Duplicate mount ID: %s' % str(e.mount_id)) + + mounts[e.mount_id] = e + + self.mounts = mounts + def __str__(self): - return '%s(%d entries)' % (self.__class__.__name__, len(self.entries)) + return '%s(%d entries)' % (self.__class__.__name__, len(self.mounts)) def entries_for_mountpoint(self, mountpoint): """ Iterate over all mountinfo entries mounted at the given mountpoint. """ - return filter(lambda entry: entry.mount_point == mountpoint, self.entries) + return filter(lambda entry: entry.mount_point == mountpoint, self.mounts.values()) def mountpoint_for_path(self, path): """ Find the longest mountpoint that is a parent of path. """ best_match = None - for e in self.entries: + for e in self.mounts.values(): if (path in e.mount_point and (best_match is None or len(best_match) < len(e.mount_point))): best_match = e.mount_point -- cgit v1.2.3