[unpackfs] Reduce file-reading

- Only need to get the list of supported filesystems *once*,
   not for each and every filesystem that is going to be unpacked.
 - Be more Python-idiomatic.
This commit is contained in:
Adriaan de Groot 2019-01-25 06:01:49 -05:00
parent 4ceedf239c
commit 56d05d5834

View File

@ -261,12 +261,28 @@ class UnpackOperation:
subprocess.check_call(["umount", "-l", imgmountdir]) subprocess.check_call(["umount", "-l", imgmountdir])
def get_supported_filesystems():
"""
Reads /proc/filesystems (the list of supported filesystems
for the current kernel) and returns a list of (names of)
those filesystems.
"""
PATH_PROCFS = '/proc/filesystems'
if os.path.isfile(PATH_PROCFS) and os.access(PATH_PROCFS, os.R_OK):
with open(PATH_PROCFS, 'r') as procfile:
filesystems = procfile.read()
filesystems = filesystems.replace(
"nodev", "").replace("\t", "").splitlines()
return filesystems
return []
def run(): def run():
""" """
Unsquash filesystem. Unsquash filesystem.
""" """
PATH_PROCFS = '/proc/filesystems'
root_mount_point = globalstorage.value("rootMountPoint") root_mount_point = globalstorage.value("rootMountPoint")
if not root_mount_point: if not root_mount_point:
@ -279,28 +295,15 @@ def run():
"globalstorage[\"rootMountPoint\"] is \"{}\", which does not " "globalstorage[\"rootMountPoint\"] is \"{}\", which does not "
"exist, doing nothing".format(root_mount_point)) "exist, doing nothing".format(root_mount_point))
supported_filesystems = get_supported_filesystems()
unpack = list() unpack = list()
for entry in job.configuration["unpack"]: for entry in job.configuration["unpack"]:
source = os.path.abspath(entry["source"]) source = os.path.abspath(entry["source"])
sourcefs = entry["sourcefs"] sourcefs = entry["sourcefs"]
# Get supported filesystems if sourcefs not in supported_filesystems:
fs_is_supported = False
if os.path.isfile(PATH_PROCFS) and os.access(PATH_PROCFS, os.R_OK):
with open(PATH_PROCFS, 'r') as procfile:
filesystems = procfile.read()
filesystems = filesystems.replace(
"nodev", "").replace("\t", "").splitlines()
# Check if the source filesystem is supported
for fs in filesystems:
if fs == sourcefs:
fs_is_supported = True
if not fs_is_supported:
return "Bad filesystem", "sourcefs=\"{}\"".format(sourcefs) return "Bad filesystem", "sourcefs=\"{}\"".format(sourcefs)
destination = os.path.abspath(root_mount_point + entry["destination"]) destination = os.path.abspath(root_mount_point + entry["destination"])