chiark / gitweb /
make-secnet-sites: Write output to ...~tmp~ and rename
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 7 Nov 2019 00:42:02 +0000 (00:42 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 15 Feb 2020 21:56:51 +0000 (21:56 +0000)
This avoids leaving a truncated output file if we crash.  It also
avoids concurrent readers ever seeing a truncated output file.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
make-secnet-sites

index 66b4cb1ddc7891ce4eaeba267ec5f01277a8f963..574eb20a3acc8616e304f2c739c43a3ad49d1e5b 100755 (executable)
@@ -211,6 +211,7 @@ def parse_args():
        global header
        global groupfiledir
        global sitesfile
+       global outputfile
        global group
        global user
        global of
@@ -258,8 +259,6 @@ def parse_args():
                        print("Too many arguments")
                        sys.exit(1)
                (inputfile, outputfile) = (av.arg + [None]*2)[0:2]
-               if outputfile is None: of=sys.stdout
-               else: of=open(outputfile,'w')
 
 parse_args()
 
@@ -776,4 +775,11 @@ if service:
        f.close()
        os.rename(sitesfile+"-tmp",sitesfile)
 else:
+       if outputfile is None:
+               of=sys.stdout
+       else:
+               tmp_outputfile=outputfile+'~tmp~'
+               of=open(tmp_outputfile,'w')
        outputsites(of)
+       if outputfile is not None:
+               os.rename(tmp_outputfile,outputfile)