Close file in safe_open

Since this is intended to be used as a context manager, it should
close the file once we're done with it.  Also, I can't see why we
would want to reset the selinux state if an exception was raised -- it
seems there may be no file to actually do this on.  Add it to the
regular exit path.

Change-Id: I7e7dba66a1c446fa82ca3facde19f89c40e9e935
This commit is contained in:
Ian Wienand 2018-07-25 11:01:01 +10:00
parent ac8458dac8
commit bcef0fe1c2
1 changed files with 6 additions and 7 deletions

View File

@ -50,13 +50,12 @@ HAVE_SELINUX = os.path.exists(SELINUX_RESTORECON)
@contextlib.contextmanager
def safe_open(*args, **kwargs):
f = open(*args, **kwargs)
try:
yield f
finally:
path = os.path.abspath(f.name)
if HAVE_SELINUX:
logging.debug("Restoring selinux context for %s" % path)
subprocess.call([SELINUX_RESTORECON, path])
yield f
f.close()
path = os.path.abspath(f.name)
if HAVE_SELINUX:
logging.debug("Restoring selinux context for %s" % path)
subprocess.call([SELINUX_RESTORECON, path])
def _exists_rh_interface(name, distro):