Cleanup imgutil open files and processes

Because of the way that compress pipes things between processes it would
leave open file descriptors, and running processes around for a bit.
This waits for them to exit and closes the fds before returning.
This commit is contained in:
Brian C. Lane 2020-10-06 13:44:30 -07:00 committed by Brian C. Lane
parent f1aee05167
commit c3c3605b70
1 changed files with 16 additions and 5 deletions

View File

@ -77,9 +77,20 @@ def compress(command, root, outfile, compression="xz", compressargs=None):
archive.stdin.write(os.path.basename(root).encode("utf-8") + b"\0")
archive.stdin.close()
comp = Popen([compression] + compressargs,
stdin=archive.stdout, stdout=open(outfile, "wb"))
comp.wait()
with open(outfile, "wb") as fout:
comp = Popen([compression] + compressargs,
stdin=archive.stdout, stdout=fout)
comp.wait()
# Clean up the open fds and processes
if find:
find.wait()
find.stdout.close()
archive.wait()
if archive.stdin:
archive.stdin.close()
if archive.stdout:
archive.stdout.close()
return comp.returncode
except OSError as e:
logger.error(e)
@ -129,8 +140,8 @@ def mkrootfsimg(rootdir, outfile, label, size=2, sysroot=""):
def mksparse(outfile, size):
'''use os.ftruncate to create a sparse file of the given size.'''
fobj = open(outfile, "w")
os.ftruncate(fobj.fileno(), size)
with open(outfile, "w") as fobj:
os.ftruncate(fobj.fileno(), size)
def mkqcow2(outfile, size, options=None):
'''use qemu-img to create a file of the given size.