Replace file.flush() calls with flush_sync() helper

Calls to `os.fsync(f.fileno())` need to be accompained by `f.flush()`.

Commit 8bbeedc93f introduces the helper
`ipapython.ipautil.flush_sync()`, which handles all calls in the right
order.

However, `flush_sync()` takes as parameter a file object with fileno
and name, where name must be a path to the file, this isn't possible
in some cases where file descriptors are used.

Issue: https://pagure.io/freeipa/issue/7251

Signed-off-by: Armando Neto <abiagion@redhat.com>
Reviewed-By: Christian Heimes <cheimes@redhat.com>
This commit is contained in:
Armando Neto
2018-07-05 15:12:33 -03:00
committed by Christian Heimes
parent f29412729e
commit b274da726b
5 changed files with 10 additions and 9 deletions

View File

@@ -1274,10 +1274,9 @@ def do_nsupdate(update_txt):
logger.debug("Writing nsupdate commands to %s:", UPDATE_FILE) logger.debug("Writing nsupdate commands to %s:", UPDATE_FILE)
logger.debug("%s", update_txt) logger.debug("%s", update_txt)
update_fd = open(UPDATE_FILE, "w") with open(UPDATE_FILE, "w") as f:
update_fd.write(update_txt) f.write(update_txt)
update_fd.flush() ipautil.flush_sync(f)
update_fd.close()
result = False result = False
try: try:

View File

@@ -46,6 +46,7 @@ from ipalib import api, errors
from ipalib import Bytes, Flag, Str from ipalib import Bytes, Flag, Str
from ipalib.plugable import Registry from ipalib.plugable import Registry
from ipalib import _ from ipalib import _
from ipapython import ipautil
from ipapython.dnsutil import DNSName from ipapython.dnsutil import DNSName
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -590,8 +591,7 @@ class _TransportCertCache(object):
mode='wb') as f: mode='wb') as f:
try: try:
f.write(pem) f.write(pem)
f.flush() ipautil.flush_sync(f)
os.fsync(f.fileno())
f.close() f.close()
os.rename(f.name, filename) os.rename(f.name, filename)
except Exception: except Exception:

View File

@@ -22,6 +22,7 @@ from ipalib.errors import SchemaUpToDate
from ipalib.frontend import Object from ipalib.frontend import Object
from ipalib.output import Output from ipalib.output import Output
from ipalib.parameters import DefaultFrom, Flag, Password, Str from ipalib.parameters import DefaultFrom, Flag, Password, Str
from ipapython import ipautil
from ipapython.ipautil import fsdecode from ipapython.ipautil import fsdecode
from ipapython.dn import DN from ipapython.dn import DN
from ipapython.dnsutil import DNSName from ipapython.dnsutil import DNSName
@@ -492,8 +493,7 @@ class Schema(object):
dir=self._DIR, delete=False) as f: dir=self._DIR, delete=False) as f:
try: try:
self._write_schema_data(f) self._write_schema_data(f)
f.flush() ipautil.flush_sync(f)
os.fsync(f.fileno())
f.close() f.close()
except Exception: except Exception:
os.unlink(f.name) os.unlink(f.name)

View File

@@ -365,7 +365,9 @@ class NSSDatabase(object):
os.O_CREAT | os.O_WRONLY, os.O_CREAT | os.O_WRONLY,
pwdfilemode), 'w', closefd=True) as f: pwdfilemode), 'w', closefd=True) as f:
f.write(ipautil.ipa_generate_password()) f.write(ipautil.ipa_generate_password())
# flush and sync tempfile inode
f.flush() f.flush()
os.fsync(f.fileno())
# In case dbtype is auto, let certutil decide which type of DB # In case dbtype is auto, let certutil decide which type of DB
# to create. # to create.

View File

@@ -628,7 +628,7 @@ class CAInstance(DogtagInstance):
with open(self.cert_file, 'rb') as f: with open(self.cert_file, 'rb') as f:
ext_cert = x509.load_unknown_x509_certificate(f.read()) ext_cert = x509.load_unknown_x509_certificate(f.read())
cert_file.write(ext_cert.public_bytes(x509.Encoding.PEM)) cert_file.write(ext_cert.public_bytes(x509.Encoding.PEM))
cert_file.flush() ipautil.flush_sync(cert_file)
result = ipautil.run( result = ipautil.run(
[paths.OPENSSL, 'crl2pkcs7', [paths.OPENSSL, 'crl2pkcs7',