Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/util.py @ 4508:0026ccc2bf23
Remove atomicfile class.
The interface provided by opener(atomic=True) is inherently unsafe:
if an exception is raised in the code using the atomic file, the
possibly incomplete file will be renamed to its final destination,
defeating the whole purpose of atomic files.
To get around this, we would either need some bad hacks involving
sys.exc_info (to make sure things work in except: blocks), or an
interface to say "file is complete; rename it".
This is the exact interface provided by atomictempfile. Since there
are no remaining users of the atomicfile class, just remove it.
author | Alexis S. L. Carvalho <alexis@cecm.usp.br> |
---|---|
date | Tue, 05 Jun 2007 19:55:27 -0300 |
parents | 62019c4427e3 |
children | 96d8a56d4ef9 |
comparison
equal
deleted
inserted
replaced
4507:289ec1f36b11 | 4508:0026ccc2bf23 |
---|---|
1193 try: | 1193 try: |
1194 os.unlink(self.temp) | 1194 os.unlink(self.temp) |
1195 except: pass | 1195 except: pass |
1196 posixfile.close(self) | 1196 posixfile.close(self) |
1197 | 1197 |
1198 class atomicfile(atomictempfile): | 1198 def o(path, mode="r", text=False, atomictemp=False): |
1199 """the file will only be copied on close""" | |
1200 def __init__(self, name, mode): | |
1201 self._err = False | |
1202 atomictempfile.__init__(self, name, mode) | |
1203 def write(self, s): | |
1204 try: | |
1205 atomictempfile.write(self, s) | |
1206 except: | |
1207 self._err = True | |
1208 raise | |
1209 def close(self): | |
1210 self.rename() | |
1211 def __del__(self): | |
1212 if not self._err: | |
1213 self.rename() | |
1214 | |
1215 def o(path, mode="r", text=False, atomic=False, atomictemp=False): | |
1216 if audit_p: | 1199 if audit_p: |
1217 audit_path(path) | 1200 audit_path(path) |
1218 f = os.path.join(p, path) | 1201 f = os.path.join(p, path) |
1219 | 1202 |
1220 if not text: | 1203 if not text: |
1226 except OSError: | 1209 except OSError: |
1227 nlink = 0 | 1210 nlink = 0 |
1228 d = os.path.dirname(f) | 1211 d = os.path.dirname(f) |
1229 if not os.path.isdir(d): | 1212 if not os.path.isdir(d): |
1230 os.makedirs(d) | 1213 os.makedirs(d) |
1231 if atomic: | 1214 if atomictemp: |
1232 return atomicfile(f, mode) | |
1233 elif atomictemp: | |
1234 return atomictempfile(f, mode) | 1215 return atomictempfile(f, mode) |
1235 if nlink > 1: | 1216 if nlink > 1: |
1236 rename(mktempcopy(f), f) | 1217 rename(mktempcopy(f), f) |
1237 return posixfile(f, mode) | 1218 return posixfile(f, mode) |
1238 | 1219 |