Mercurial > hg > mercurial-crew-with-dirclash
comparison mercurial/util.py @ 4331:ce52deed83bc
atomicfile: don't copy the original file if it'll be truncated
author | Alexis S. L. Carvalho <alexis@cecm.usp.br> |
---|---|
date | Mon, 09 Apr 2007 04:24:17 -0300 |
parents | 1083ae4b5f0e |
children | 1cc5fc1d0994 f4a1eac52d43 |
comparison
equal
deleted
inserted
replaced
4330:1b9fc3f48861 | 4331:ce52deed83bc |
---|---|
1003 remote file access from higher level code. | 1003 remote file access from higher level code. |
1004 """ | 1004 """ |
1005 p = base | 1005 p = base |
1006 audit_p = audit | 1006 audit_p = audit |
1007 | 1007 |
1008 def mktempcopy(name): | 1008 def mktempcopy(name, emptyok=False): |
1009 d, fn = os.path.split(name) | 1009 d, fn = os.path.split(name) |
1010 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d) | 1010 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d) |
1011 os.close(fd) | 1011 os.close(fd) |
1012 # Temporary files are created with mode 0600, which is usually not | 1012 # Temporary files are created with mode 0600, which is usually not |
1013 # what we want. If the original file already exists, just copy | 1013 # what we want. If the original file already exists, just copy |
1017 except OSError, inst: | 1017 except OSError, inst: |
1018 if inst.errno != errno.ENOENT: | 1018 if inst.errno != errno.ENOENT: |
1019 raise | 1019 raise |
1020 st_mode = 0666 & ~_umask | 1020 st_mode = 0666 & ~_umask |
1021 os.chmod(temp, st_mode) | 1021 os.chmod(temp, st_mode) |
1022 if emptyok: | |
1023 return temp | |
1022 try: | 1024 try: |
1023 try: | 1025 try: |
1024 ifp = posixfile(name, "rb") | 1026 ifp = posixfile(name, "rb") |
1025 except IOError, inst: | 1027 except IOError, inst: |
1026 if inst.errno == errno.ENOENT: | 1028 if inst.errno == errno.ENOENT: |
1041 | 1043 |
1042 class atomictempfile(posixfile): | 1044 class atomictempfile(posixfile): |
1043 """the file will only be copied when rename is called""" | 1045 """the file will only be copied when rename is called""" |
1044 def __init__(self, name, mode): | 1046 def __init__(self, name, mode): |
1045 self.__name = name | 1047 self.__name = name |
1046 self.temp = mktempcopy(name) | 1048 self.temp = mktempcopy(name, emptyok=('w' in mode)) |
1047 posixfile.__init__(self, self.temp, mode) | 1049 posixfile.__init__(self, self.temp, mode) |
1048 def rename(self): | 1050 def rename(self): |
1049 if not self.closed: | 1051 if not self.closed: |
1050 posixfile.close(self) | 1052 posixfile.close(self) |
1051 rename(self.temp, localpath(self.__name)) | 1053 rename(self.temp, localpath(self.__name)) |