From 934ef15d2e0e1397ea0d4361991b1c90dcfc428d Mon Sep 17 00:00:00 2001 From: Brian Harring Date: Sat, 24 Dec 2005 09:56:42 +0000 Subject: [PATCH] bug 114133, write a tmp file then rename it for write* funcs. This removes the possibility of out of space errors to occur, and blocks unexpected flat out bails (segfault fex) from hosing important files. svn path=/main/trunk/; revision=2447 --- pym/portage_util.py | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/pym/portage_util.py b/pym/portage_util.py index 090ce6735..e7779e0c4 100644 --- a/pym/portage_util.py +++ b/pym/portage_util.py @@ -195,33 +195,40 @@ def grablines(myfilename,recursive=0): return mylines def writeints(mydict,myfilename): + myfile = None + myf2 = "%s.%i" % (myf2, os.getpid()) try: - myfile=open(myfilename,"w") + myfile=open(myf2,"w") + for x in mydict: + myfile.write("%s %s\n" % (x, str(mydict[x]))) + myfile.close() + os.rename(myf2, myfilename) except IOError: + if myfile is not None: + os.unlink(myf2) return 0 - for x in mydict.keys(): - myfile.write(x+" "+`mydict[x]`+"\n") - myfile.close() return 1 -def writedict(mydict,myfilename,writekey=1): +def writedict(mydict,myfilename,writekey=True): """Writes out a dict to a file; writekey=0 mode doesn't write out the key and assumes all values are strings, not lists.""" + myfile = None + myf2 = "%s.%i" % (myfilename, os.getpid()) try: - myfile=open(myfilename,"w") + myfile=open(myf2,"w") + if not writekey: + for x in mydict.values(): + myfile.write(x+"\n") + else: + for x in mydict.keys(): + myfile.write("%s %s\n" % (x, " ".join(mydict[x]))) + myfile.close() + os.rename(myf2, myfilename) + except IOError: - writemsg("Failed to open file for writedict(): "+str(myfilename)+"\n") + if myfile is not None: + os.unlink(myf2) return 0 - if not writekey: - for x in mydict.values(): - myfile.write(x+"\n") - else: - for x in mydict.keys(): - myfile.write(x+" ") - for y in mydict[x]: - myfile.write(y+" ") - myfile.write("\n") - myfile.close() return 1 def getconfig(mycfg,tolerant=0,allow_sourcing=False): -- 2.26.2