summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Harring <ferringb@gentoo.org>2005-12-24 09:56:42 +0000
committerBrian Harring <ferringb@gentoo.org>2005-12-24 09:56:42 +0000
commit934ef15d2e0e1397ea0d4361991b1c90dcfc428d (patch)
tree172a2db2e7aac49f5c3971a2201be0119c914af7
parent29c01db1bce425531bd268d629c251e25d32ee51 (diff)
downloadportage-934ef15d2e0e1397ea0d4361991b1c90dcfc428d.tar.gz
portage-934ef15d2e0e1397ea0d4361991b1c90dcfc428d.tar.bz2
portage-934ef15d2e0e1397ea0d4361991b1c90dcfc428d.zip
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
-rw-r--r--pym/portage_util.py41
1 files 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):