Mercurial > hg
comparison mercurial/util.py @ 12937:6ff784de7c3a stable
util: refactor opener
- Don't call atomictempfile or nlinks() if the path is malformed
(no basename). Let posixfile() raise IOError directly.
- atomictempfile already breaks up hardlinks, no need to poke
at the file with nlinks() if atomictemp.
- No need to copy the file contents to break hardlinks for 'w'rite
modes (w, wb, w+, w+b). Unlinking and recreating the file is faster.
author | Adrian Buehlmann <adrian@cadifra.com> |
---|---|
date | Thu, 04 Nov 2010 09:04:37 +0100 |
parents | 551aa6e27929 |
children | bf826c0b9537 |
comparison
equal
deleted
inserted
replaced
12932:ab93029ab622 | 12937:6ff784de7c3a |
---|---|
856 | 856 |
857 if not text and "b" not in mode: | 857 if not text and "b" not in mode: |
858 mode += "b" # for that other OS | 858 mode += "b" # for that other OS |
859 | 859 |
860 nlink = -1 | 860 nlink = -1 |
861 if mode not in ("r", "rb"): | 861 st_mode = None |
862 dirname, basename = os.path.split(f) | |
863 # If basename is empty, then the path is malformed because it points | |
864 # to a directory. Let the posixfile() call below raise IOError. | |
865 if basename and mode not in ('r', 'rb'): | |
866 if atomictemp: | |
867 if not os.path.isdir(dirname): | |
868 makedirs(dirname, self.createmode) | |
869 return atomictempfile(f, mode, self.createmode) | |
862 try: | 870 try: |
863 nlink = nlinks(f) | 871 if 'w' in mode: |
872 st_mode = os.lstat(f).st_mode & 0777 | |
873 os.unlink(f) | |
874 nlink = 0 | |
875 else: | |
876 nlink = nlinks(f) | |
864 except OSError: | 877 except OSError: |
865 nlink = 0 | 878 nlink = 0 |
866 dirname, basename = os.path.split(f) | 879 if not os.path.isdir(dirname): |
867 # Avoid calling makedirs when the path points to a | |
868 # directory -- the open will raise IOError below. | |
869 if basename and not os.path.isdir(dirname): | |
870 makedirs(dirname, self.createmode) | 880 makedirs(dirname, self.createmode) |
871 if atomictemp: | |
872 return atomictempfile(f, mode, self.createmode) | |
873 if nlink > 1: | 881 if nlink > 1: |
874 rename(mktempcopy(f), f) | 882 rename(mktempcopy(f), f) |
875 fp = posixfile(f, mode) | 883 fp = posixfile(f, mode) |
876 if nlink == 0: | 884 if nlink == 0: |
877 self._fixfilemode(f) | 885 if st_mode is None: |
886 self._fixfilemode(f) | |
887 else: | |
888 os.chmod(f, st_mode) | |
878 return fp | 889 return fp |
879 | 890 |
880 def symlink(self, src, dst): | 891 def symlink(self, src, dst): |
881 self.auditor(dst) | 892 self.auditor(dst) |
882 linkname = os.path.join(self.base, dst) | 893 linkname = os.path.join(self.base, dst) |