TmpFs: do not mount in __init__
The underlying filesystem was mounted in __init__ and unmonuted in __exit__/__del__. This meant that if the same object was reused in several `with` clauses, only the first one would work as intended. Signed-off-by: Tom Gundersen <teg@jklm.no>
This commit is contained in:
parent
79b2f37cbc
commit
98ce5a7595
1 changed files with 11 additions and 12 deletions
|
|
@ -44,31 +44,30 @@ class AssemblerFailed(Exception):
|
|||
|
||||
class TmpFs:
|
||||
def __init__(self, path="/run/osbuild"):
|
||||
self.root = tempfile.mkdtemp(prefix="osbuild-tmpfs-", dir=path)
|
||||
self.path = path
|
||||
self.root = None
|
||||
self.mounted = False
|
||||
|
||||
def __enter__(self):
|
||||
self.root = tempfile.mkdtemp(prefix="osbuild-tmpfs-", dir=self.path)
|
||||
try:
|
||||
subprocess.run(["mount", "-t", "tmpfs", "-o", "mode=0755", "tmpfs", self.root], check=True)
|
||||
self.mounted = True
|
||||
except subprocess.CalledProcessError:
|
||||
self.unmount()
|
||||
os.rmdir(self.root)
|
||||
self.root = None
|
||||
raise
|
||||
return self.root
|
||||
|
||||
def unmount(self):
|
||||
def __exit__(self, exc_type, exc_value, exc_tb):
|
||||
if not self.root:
|
||||
return
|
||||
if self.mounted:
|
||||
subprocess.run(["umount", "--lazy", self.root], check=True)
|
||||
self.mounted = False
|
||||
os.rmdir(self.root)
|
||||
self.root = None
|
||||
|
||||
def __del__(self):
|
||||
self.unmount()
|
||||
|
||||
def __enter__(self):
|
||||
return self.root
|
||||
|
||||
def __exit__(self, exc_type, exc_value, exc_tb):
|
||||
self.unmount()
|
||||
|
||||
|
||||
class BuildRoot:
|
||||
def __init__(self, path="/run/osbuild"):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue