From c217f4825179484e4c95072fcc715f092228c7c0 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 21 Sep 2022 22:00:06 +0200 Subject: [PATCH] Replace the output file atomically When writing the new .data file, first write the new content, then replace the target. This way, there isn't a temporary state in which the file is partially written. This temporary state can be misleading if the build is interrupted. It's annoying if you're watching changes to the output and the changes appear as emptying the file following by the new version appearing. Now interrupted builds don't leave a file that appears to be up to date but isn't, and when watching the output, there's a single transition to the new version. Signed-off-by: Gilles Peskine --- scripts/mbedtls_dev/test_case.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/mbedtls_dev/test_case.py b/scripts/mbedtls_dev/test_case.py index 6a46e4209..d0afa592f 100644 --- a/scripts/mbedtls_dev/test_case.py +++ b/scripts/mbedtls_dev/test_case.py @@ -92,9 +92,11 @@ def write_data_file(filename: str, """ if caller is None: caller = os.path.basename(sys.argv[0]) - with open(filename, 'w') as out: + tempfile = filename + '.new' + with open(tempfile, 'w') as out: out.write('# Automatically generated by {}. Do not edit!\n' .format(caller)) for tc in test_cases: tc.write(out) out.write('\n# End of automatically generated file.\n') + os.replace(tempfile, filename)