diff options
author | Anton Khirnov <anton@khirnov.net> | 2023-10-14 21:38:29 +0200 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2023-10-14 21:38:29 +0200 |
commit | 9965a2d2cbb97f51cb5eca6bffd7eae2762ee30a (patch) | |
tree | bdd8e00b3bf23b68742e0d53c04d8dbf8bbc24f9 | |
parent | d24b034677406d9a6bb999fdd6a0875d77497750 (diff) |
_mountinfo: replace list of entries with OrderedDict
Mapping mount id to entries.
-rw-r--r-- | lbup/_mountinfo.py | 23 |
1 files 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/<pid>/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 |