[PATCH 6 of 8 RFC] vfs: replace invocation of platform depend file APIs of util.py by ones via vfs

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Fri Jun 15 09:45:17 CDT 2012


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1339768794 -32400
# Node ID ecd42350a3f9cd826b660a5ed1852dfd7d5ae899
# Parent  e325d149ed0933c0a794ee118e1ee9a36195eb03
vfs: replace invocation of platform depend file APIs of util.py by ones via vfs

this patch replaces invocation of below platform depend file APIs of
util.py by ones via vfs.

    - cachestat
    - checkexec
    - checklink
    - copymode
    - isexec
    - makedir
    - nlinks
    - oslink
    - posixfile
    - realpath
    - rename
    - samedevice
    - samefile
    - setflags
    - statfiles
    - unlink
    - unlinkpath

replacements of this patch are done in steps below:

    1. replace all invocations of above functions "util.XXXX()" by
        "util.vfs().XXXX()" mechanically: or adding "vfs()." in
        util.py itself

    2. check how many times "vfs()" are invoked in each modified
       functions

    3. introduce "vfs" local var("vfsobj" in util.py itself), and
       replace "util.vfs()" invocations by it, if:

       - vfs() is invoked more than once in the function,
       - vfs() is invoked in the loop, or
       - vfs() is invoked in the internal function invoked repeatedly

[RFC points]

- should we unify some functions ?

    in current implementation, there are some functions having same
    name.

      - "os.rename()" and "util.rename()"
      - "os.unlink()" and "util.unlink()"
      - "os.path.realpath()" and "util.realpath()"

    this patch uses "rename2()", "unlink2()" and "realpath2()" to
    distinguish ones of util.py from ones of os or os.path modules.

    ones of util.py seem to be provided for platform portability, but
    ones of "os"/"os.path" are also used.

    should we unify them into one of them ?

    in addition to them, should we also unify "os.path.makedirs()" and
    "util.makedirs()" ?

diff -r e325d149ed09 -r ecd42350a3f9 contrib/shrink-revlog.py
--- a/contrib/shrink-revlog.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/contrib/shrink-revlog.py	Fri Jun 15 22:59:54 2012 +0900
@@ -252,14 +252,14 @@
         if not opts.get('dry_run'):
             # racy, both files cannot be renamed atomically
             # copy files
-            util.oslink(indexfn, oldindexfn)
-            ignoremissing(util.oslink)(datafn, olddatafn)
+            vfs.oslink(indexfn, oldindexfn)
+            ignoremissing(vfs.oslink)(datafn, olddatafn)
 
             # rename
-            util.rename(tmpindexfn, indexfn)
+            vfs.rename(tmpindexfn, indexfn)
             try:
                 vfs.chmod(tmpdatafn, vfs.stat(datafn).st_mode)
-                util.rename(tmpdatafn, datafn)
+                vfs.rename(tmpdatafn, datafn)
             except OSError, inst:
                 if inst.errno != errno.ENOENT:
                     raise
diff -r e325d149ed09 -r ecd42350a3f9 hgext/convert/subversion.py
--- a/hgext/convert/subversion.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/hgext/convert/subversion.py	Fri Jun 15 22:59:54 2012 +0900
@@ -1045,14 +1045,14 @@
         self.opener = scmutil.opener(self.wc)
         self.wopener = scmutil.opener(self.wc)
         self.childmap = mapfile(ui, self.join('hg-childmap'))
-        self.is_exec = util.checkexec(self.wc) and util.isexec or None
+        self.is_exec = vfs.checkexec(self.wc) and vfs.isexec or None
 
         if created:
             hook = os.path.join(created, 'hooks', 'pre-revprop-change')
             fp = open(hook, 'w')
             fp.write(pre_revprop_change)
             fp.close()
-            util.setflags(hook, False, True)
+            vfs.setflags(hook, False, True)
 
         output = self.run0('info')
         self.uuid = self.uuid_re.search(output).group(1).strip()
@@ -1103,7 +1103,7 @@
                 # systematically is just as expensive and much simpler.
                 was_exec = 'x' not in flags
 
-            util.setflags(self.wjoin(filename), False, 'x' in flags)
+            vfs.setflags(self.wjoin(filename), False, 'x' in flags)
             if was_exec:
                 if 'x' not in flags:
                     self.delexec.append(filename)
diff -r e325d149ed09 -r ecd42350a3f9 hgext/extdiff.py
--- a/hgext/extdiff.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/hgext/extdiff.py	Fri Jun 15 22:59:54 2012 +0900
@@ -103,7 +103,7 @@
         else:
             wopener.write(wfn, data)
             if 'x' in fctx.flags():
-                util.setflags(dest, False, True)
+                vfs.setflags(dest, False, True)
         if node is None:
             fns_and_mtime.append((dest, repo.wjoin(fn),
                                   vfs.lstat(dest).st_mtime))
diff -r e325d149ed09 -r ecd42350a3f9 hgext/largefiles/lfcommands.py
--- a/hgext/largefiles/lfcommands.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/hgext/largefiles/lfcommands.py	Fri Jun 15 22:59:54 2012 +0900
@@ -494,7 +494,7 @@
         # largefile is converted back to a normal file: the standin
         # disappears, but a new (normal) file appears as the lfile.
         if vfs.exists(abslfile) and lfile not in repo[None]:
-            util.unlinkpath(abslfile)
+            vfs.unlinkpath(abslfile)
             ret = -1
     state = repo.dirstate[lfutil.standin(lfile)]
     if state == 'n':
diff -r e325d149ed09 -r ecd42350a3f9 hgext/largefiles/lfutil.py
--- a/hgext/largefiles/lfutil.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/hgext/largefiles/lfutil.py	Fri Jun 15 22:59:54 2012 +0900
@@ -35,9 +35,10 @@
         wlock = repo.wlock()
         try:
             if unlink:
+                vfs = util.vfs()
                 for f in list:
                     try:
-                        util.unlinkpath(repo.wjoin(f))
+                        vfs.unlinkpath(repo.wjoin(f))
                     except OSError, inst:
                         if inst.errno != errno.ENOENT:
                             raise
@@ -73,15 +74,16 @@
     return lfsize
 
 def link(src, dest):
+    vfs = util.vfs()
     try:
-        util.oslink(src, dest)
+        vfs.oslink(src, dest)
     except OSError:
         # if hardlinks fail, fallback on atomic copy
         dst = util.atomictempfile(dest)
         for chunk in util.filechunkiter(open(src, 'rb')):
             dst.write(chunk)
         dst.close()
-        util.vfs().chmod(dest, util.vfs().stat(src).st_mode)
+        vfs.chmod(dest, vfs.stat(src).st_mode)
 
 def usercachepath(ui, hash):
     path = ui.configpath(longname, 'usercache', None)
diff -r e325d149ed09 -r ecd42350a3f9 hgext/largefiles/overrides.py
--- a/hgext/largefiles/overrides.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/hgext/largefiles/overrides.py	Fri Jun 15 22:59:54 2012 +0900
@@ -162,7 +162,7 @@
                 if getattr(repo, "_isaddremove", False):
                     ui.status(_('removing %s\n') % f)
                 if vfs.exists(repo.wjoin(f)):
-                    util.unlinkpath(repo.wjoin(f))
+                    vfs.unlinkpath(repo.wjoin(f))
             lfdirstate.remove(f)
         lfdirstate.write()
         forget = [lfutil.standin(f) for f in forget]
@@ -624,7 +624,7 @@
             if standin not in ctx and (standin in matches or opts.get('all')):
                 if lfile in lfdirstate:
                     lfdirstate.drop(lfile)
-                util.unlinkpath(repo.wjoin(standin))
+                vfs.unlinkpath(repo.wjoin(standin))
         lfdirstate.write()
     finally:
         wlock.release()
diff -r e325d149ed09 -r ecd42350a3f9 hgext/mq.py
--- a/hgext/mq.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/hgext/mq.py	Fri Jun 15 22:59:54 2012 +0900
@@ -581,7 +581,7 @@
                 if copy:
                     util.copyfile(absf, absf + '.orig')
                 else:
-                    util.rename(absf, absf + '.orig')
+                    vfs.rename2(absf, absf + '.orig')
 
     def printdiff(self, repo, diffopts, node1, node2=None, files=None,
                   fp=None, changes=None, opts={}):
@@ -1316,10 +1316,11 @@
                 hg.revert(repo, node, None)
                 # only remove unknown files that we know we touched or
                 # created while patching
+                vfs = util.vfs()
                 for f in all_files:
                     if f not in repo.dirstate:
                         try:
-                            util.unlinkpath(repo.wjoin(f))
+                            vfs.unlinkpath(repo.wjoin(f))
                         except OSError, inst:
                             if inst.errno != errno.ENOENT:
                                 raise
@@ -1430,9 +1431,10 @@
                     self.localchangesfound()
                 self.backup(repo, tobackup)
 
+                vfs = util.vfs()
                 for f in a:
                     try:
-                        util.unlinkpath(repo.wjoin(f))
+                        vfs.unlinkpath(repo.wjoin(f))
                     except OSError, e:
                         if e.errno != errno.ENOENT:
                             raise
@@ -1972,7 +1974,7 @@
 
                     self.ui.write(_('renaming %s to %s\n')
                                         % (filename, patchname))
-                    util.rename(originpath, self.join(patchname))
+                    vfs.rename2(originpath, self.join(patchname))
                 else:
                     patchname = filename
 
@@ -2822,7 +2824,7 @@
     destdir = os.path.dirname(absdest)
     if not vfs.isdir(destdir):
         vfs.makedirs(destdir)
-    util.rename(q.join(patch), absdest)
+    vfs.rename2(q.join(patch), absdest)
     r = q.qrepo()
     if r and patch in r.dirstate:
         wctx = r[None]
@@ -3215,6 +3217,7 @@
     _defaultqueue = 'patches'
     _allqueues = 'patches.queues'
     _activequeue = 'patches.queue'
+    vfs = util.vfs()
 
     def _getcurrent():
         cur = os.path.basename(q.path)
@@ -3288,7 +3291,7 @@
                 continue
             fh.write('%s\n' % (queue,))
         fh.close()
-        util.rename(repo.join('patches.queues.new'), repo.join(_allqueues))
+        vfs.rename2(repo.join('patches.queues.new'), repo.join(_allqueues))
 
     if not name or opts.get('list') or opts.get('active'):
         current = _getcurrent()
@@ -3308,7 +3311,6 @@
                 _('invalid queue name, may not contain the characters ":\\/."'))
 
     existing = _getqueues()
-    vfs = util.vfs()
 
     if opts.get('create'):
         if name in existing:
@@ -3336,11 +3338,11 @@
             if queue == current:
                 fh.write('%s\n' % (name,))
                 if vfs.exists(olddir):
-                    util.rename(olddir, newdir)
+                    vfs.rename2(olddir, newdir)
             else:
                 fh.write('%s\n' % (queue,))
         fh.close()
-        util.rename(repo.join('patches.queues.new'), repo.join(_allqueues))
+        vfs.rename2(repo.join('patches.queues.new'), repo.join(_allqueues))
         _setactivenocheck(name)
     elif opts.get('delete'):
         _delete(name)
diff -r e325d149ed09 -r ecd42350a3f9 hgext/rebase.py
--- a/hgext/rebase.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/hgext/rebase.py	Fri Jun 15 22:59:54 2012 +0900
@@ -333,8 +333,9 @@
 
         clearstatus(repo)
         ui.note(_("rebase completed\n"))
-        if util.vfs().exists(repo.sjoin('undo')):
-            util.unlinkpath(repo.sjoin('undo'))
+        vfs = util.vfs()
+        if vfs.exists(repo.sjoin('undo')):
+            vfs.unlinkpath(repo.sjoin('undo'))
         if skipped:
             ui.note(_("%d revisions have been skipped\n") % len(skipped))
     finally:
@@ -518,8 +519,9 @@
 
 def clearstatus(repo):
     'Remove the status files'
-    if util.vfs().exists(repo.join("rebasestate")):
-        util.unlinkpath(repo.join("rebasestate"))
+    vfs = util.vfs()
+    if vfs.exists(repo.join("rebasestate")):
+        vfs.unlinkpath(repo.join("rebasestate"))
 
 def restorestatus(repo):
     'Restore a previously stored status'
diff -r e325d149ed09 -r ecd42350a3f9 hgext/relink.py
--- a/hgext/relink.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/hgext/relink.py	Fri Jun 15 22:59:54 2012 +0900
@@ -96,15 +96,16 @@
     return candidates
 
 def prune(candidates, src, dst, ui):
+    vfs = util.vfs()
     def linkfilter(src, dst, st):
         try:
-            ts = util.vfs().stat(dst)
+            ts = vfs.stat(dst)
         except OSError:
             # Destination doesn't have this file?
             return False
-        if util.samefile(src, dst):
+        if vfs.samefile(src, dst):
             return False
-        if not util.samedevice(src, dst):
+        if not vfs.samedevice(src, dst):
             # No point in continuing
             raise util.Abort(
                 _('source and destination are on different devices'))
@@ -136,7 +137,7 @@
         bak = dst + '.bak'
         vfs.rename(dst, bak)
         try:
-            util.oslink(src, dst)
+            vfs.oslink(src, dst)
         except OSError:
             vfs.rename(bak, dst)
             raise
diff -r e325d149ed09 -r ecd42350a3f9 hgext/share.py
--- a/hgext/share.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/hgext/share.py	Fri Jun 15 22:59:54 2012 +0900
@@ -50,7 +50,7 @@
         destlock = hg.copystore(ui, repo, repo.path)
 
         sharefile = repo.join('sharedpath')
-        util.rename(sharefile, sharefile + '.old')
+        util.vfs().rename2(sharefile, sharefile + '.old')
 
         repo.requirements.discard('sharedpath')
         repo._writerequirements()
diff -r e325d149ed09 -r ecd42350a3f9 mercurial/bookmarks.py
--- a/mercurial/bookmarks.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/mercurial/bookmarks.py	Fri Jun 15 22:59:54 2012 +0900
@@ -130,7 +130,7 @@
     wlock = repo.wlock()
     try:
         try:
-            util.unlink(repo.join('bookmarks.current'))
+            util.vfs().unlink2(repo.join('bookmarks.current'))
             repo._bookmarkcurrent = None
         except OSError, inst:
             if inst.errno != errno.ENOENT:
diff -r e325d149ed09 -r ecd42350a3f9 mercurial/bundlerepo.py
--- a/mercurial/bundlerepo.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/mercurial/bundlerepo.py	Fri Jun 15 22:59:54 2012 +0900
@@ -184,7 +184,8 @@
             self._url = 'bundle:' + bundlename
 
         self.tempfile = None
-        f = util.posixfile(bundlename, "rb")
+        vfs = util.vfs()
+        f = vfs.posixfile(bundlename, "rb")
         self.bundle = changegroup.readbundle(f, bundlename)
         if self.bundle.compressed():
             fdtemp, temp = tempfile.mkstemp(prefix="hg-bundle-",
@@ -202,7 +203,7 @@
             finally:
                 fptemp.close()
 
-            f = util.posixfile(self.tempfile, "rb")
+            f = vfs.posixfile(self.tempfile, "rb")
             self.bundle = changegroup.readbundle(f, bundlename)
 
         # dict with the mapping 'filename' -> position in the bundle
diff -r e325d149ed09 -r ecd42350a3f9 mercurial/changelog.py
--- a/mercurial/changelog.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/mercurial/changelog.py	Fri Jun 15 22:59:54 2012 +0900
@@ -140,7 +140,7 @@
             nfile = self.opener(self.indexfile + ".a")
             n = nfile.name
             nfile.close()
-            util.rename(n, n[:-2])
+            util.vfs().rename2(n, n[:-2])
         elif self._delaybuf:
             fp = self.opener(self.indexfile, 'a')
             fp.write("".join(self._delaybuf))
diff -r e325d149ed09 -r ecd42350a3f9 mercurial/cmdutil.py
--- a/mercurial/cmdutil.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/mercurial/cmdutil.py	Fri Jun 15 22:59:54 2012 +0900
@@ -354,7 +354,7 @@
                              dryrun=dryrun, cwd=cwd)
         if rename and not dryrun:
             if not after and srcexists and not samefile:
-                util.unlinkpath(repo.wjoin(abssrc))
+                vfs.unlinkpath(repo.wjoin(abssrc))
             wctx.forget([abssrc])
 
     # pat: ossep
@@ -1558,7 +1558,7 @@
                     ui.note(_('saving current version of %s as %s\n') %
                             (rel, bakname))
                     if not opts.get('dry_run'):
-                        util.rename(target, bakname)
+                        vfs.rename2(target, bakname)
                 if ui.verbose or not exact:
                     msg = xlist[1]
                     if not isinstance(msg, basestring):
@@ -1610,7 +1610,7 @@
                     continue
                 audit_path(f)
                 try:
-                    util.unlinkpath(repo.wjoin(f))
+                    vfs.unlinkpath(repo.wjoin(f))
                 except OSError:
                     pass
                 repo.dirstate.remove(f)
diff -r e325d149ed09 -r ecd42350a3f9 mercurial/commands.py
--- a/mercurial/commands.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/mercurial/commands.py	Fri Jun 15 22:59:54 2012 +0900
@@ -1849,12 +1849,13 @@
 @command('debugfsinfo', [], _('[PATH]'))
 def debugfsinfo(ui, path = "."):
     """show information detected about current filesystem"""
+    vfs = util.vfs()
     util.writefile('.debugfsinfo', '')
-    ui.write('exec: %s\n' % (util.checkexec(path) and 'yes' or 'no'))
-    ui.write('symlink: %s\n' % (util.checklink(path) and 'yes' or 'no'))
+    ui.write('exec: %s\n' % (vfs.checkexec(path) and 'yes' or 'no'))
+    ui.write('symlink: %s\n' % (vfs.checklink(path) and 'yes' or 'no'))
     ui.write('case-sensitive: %s\n' % (util.checkcase('.debugfsinfo')
                                 and 'yes' or 'no'))
-    util.vfs().unlink('.debugfsinfo')
+    vfs.unlink('.debugfsinfo')
 
 @command('debuggetbundle',
     [('H', 'head', [], _('id of head node'), _('ID')),
@@ -2790,8 +2791,9 @@
         wlock.release()
 
     # remove state when we complete successfully
-    if not opts.get('dry_run') and util.vfs().exists(repo.join('graftstate')):
-        util.unlinkpath(repo.join('graftstate'))
+    vfs = util.vfs()
+    if not opts.get('dry_run') and vfs.exists(repo.join('graftstate')):
+        vfs.unlinkpath(repo.join('graftstate'))
 
     return 0
 
@@ -4735,7 +4737,7 @@
                 if f in added:
                     continue # we never unlink added files on remove
                 try:
-                    util.unlinkpath(repo.wjoin(f))
+                    vfs.unlinkpath(repo.wjoin(f))
                 except OSError, inst:
                     if inst.errno != errno.ENOENT:
                         raise
@@ -4833,6 +4835,7 @@
     m = scmutil.match(repo[None], pats, opts)
     ret = 0
 
+    vfs = util.vfs()
     for f in ms:
         if m(f):
             if show:
@@ -4863,7 +4866,7 @@
                     ui.setconfig('ui', 'forcemerge', '')
 
                 # replace filemerge's .orig file with our resolve file
-                util.rename(a + ".resolve", a + ".orig")
+                vfs.rename2(a + ".resolve", a + ".orig")
 
     ms.commit()
     return ret
diff -r e325d149ed09 -r ecd42350a3f9 mercurial/config.py
--- a/mercurial/config.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/mercurial/config.py	Fri Jun 15 22:59:54 2012 +0900
@@ -179,5 +179,5 @@
 
     def read(self, path, fp=None, sections=None, remap=None):
         if not fp:
-            fp = util.posixfile(path)
+            fp = util.vfs().posixfile(path)
         self.parse(path, fp.read(), sections, remap, self.read)
diff -r e325d149ed09 -r ecd42350a3f9 mercurial/dirstate.py
--- a/mercurial/dirstate.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/mercurial/dirstate.py	Fri Jun 15 22:59:54 2012 +0900
@@ -137,11 +137,11 @@
 
     @propertycache
     def _checklink(self):
-        return util.checklink(self._root)
+        return util.vfs().checklink(self._root)
 
     @propertycache
     def _checkexec(self):
-        return util.checkexec(self._root)
+        return util.vfs().checkexec(self._root)
 
     @propertycache
     def _checkcase(self):
@@ -159,7 +159,7 @@
                 p = self._join(x)
                 if vfs.islink(p):
                     return 'l'
-                if util.isexec(p):
+                if vfs.isexec(p):
                     return 'x'
                 return ''
             return f
@@ -177,7 +177,7 @@
             def f(x):
                 if 'l' in fallback(x):
                     return 'l'
-                if util.isexec(self._join(x)):
+                if vfs.isexec(self._join(x)):
                     return 'x'
                 return ''
             return f
@@ -593,7 +593,8 @@
         dmap = self._map
         normpath = util.normpath
         listdir = osutil.listdir
-        lstat = util.vfs().lstat
+        vfs = util.vfs()
+        lstat = vfs.lstat
         getkind = stat.S_IFMT
         dirkind = stat.S_IFDIR
         regkind = stat.S_IFREG
@@ -704,7 +705,7 @@
         # step 3: report unseen items in the dmap hash
         if not skipstep3 and not exact:
             visit = sorted([f for f in dmap if f not in results and matchfn(f)])
-            for nf, st in zip(visit, util.statfiles([join(i) for i in visit])):
+            for nf, st in zip(visit, vfs.statfiles([join(i) for i in visit])):
                 if (not st is None and
                     getkind(st.st_mode) not in (regkind, lnkkind)):
                     st = None
diff -r e325d149ed09 -r ecd42350a3f9 mercurial/hg.py
--- a/mercurial/hg.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/mercurial/hg.py	Fri Jun 15 22:59:54 2012 +0900
@@ -139,7 +139,7 @@
 
     if not vfs.isdir(root):
         vfs.mkdir(root)
-    util.makedir(roothg, notindexed=True)
+    vfs.makedir(roothg, notindexed=True)
 
     requirements = ''
     try:
@@ -315,7 +315,7 @@
                 dircleanup.dir_ = hgdir
             try:
                 destpath = hgdir
-                util.makedir(destpath, notindexed=True)
+                vfs.makedir(destpath, notindexed=True)
             except OSError, inst:
                 if inst.errno == errno.EEXIST:
                     dircleanup.close()
diff -r e325d149ed09 -r ecd42350a3f9 mercurial/localrepo.py
--- a/mercurial/localrepo.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/mercurial/localrepo.py	Fri Jun 15 22:59:54 2012 +0900
@@ -57,7 +57,7 @@
             if create:
                 if not vfs.exists(path):
                     util.makedirs(path)
-                util.makedir(self.path, notindexed=True)
+                vfs.makedir(self.path, notindexed=True)
                 requirements = ["revlogv1"]
                 if self.ui.configbool('format', 'usestore', True):
                     vfs.mkdir(os.path.join(self.path, "store"))
@@ -725,7 +725,7 @@
         else:
             self.wopener.write(filename, data)
             if 'x' in flags:
-                util.setflags(self.wjoin(filename), False, True)
+                util.vfs().setflags(self.wjoin(filename), False, True)
 
     def wwritedata(self, filename, data):
         return self._filter(self._decodefilterpats, filename, data)
@@ -833,17 +833,17 @@
         transaction.rollback(self.sopener, self.sjoin('undo'), ui.warn)
         vfs = util.vfs()
         if vfs.exists(self.join('undo.bookmarks')):
-            util.rename(self.join('undo.bookmarks'),
+            vfs.rename2(self.join('undo.bookmarks'),
                         self.join('bookmarks'))
         if vfs.exists(self.sjoin('undo.phaseroots')):
-            util.rename(self.sjoin('undo.phaseroots'),
+            vfs.rename2(self.sjoin('undo.phaseroots'),
                         self.sjoin('phaseroots'))
         self.invalidate()
 
         parentgone = (parents[0] not in self.changelog.nodemap or
                       parents[1] not in self.changelog.nodemap)
         if parentgone:
-            util.rename(self.join('undo.dirstate'), self.join('dirstate'))
+            vfs.rename2(self.join('undo.dirstate'), self.join('dirstate'))
             try:
                 branch = self.opener.read('undo.branch')
                 self.dirstate.setbranch(branch)
@@ -2353,9 +2353,10 @@
 def aftertrans(files):
     renamefiles = [tuple(t) for t in files]
     def a():
+        vfs = util.vfs()
         for src, dest in renamefiles:
             try:
-                util.rename(src, dest)
+                vfs.rename2(src, dest)
             except OSError: # journal file does not yet exist
                 pass
     return a
diff -r e325d149ed09 -r ecd42350a3f9 mercurial/lock.py
--- a/mercurial/lock.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/mercurial/lock.py	Fri Jun 15 22:59:54 2012 +0900
@@ -114,7 +114,7 @@
         # held, or can race and break valid lock.
         try:
             l = lock(self.f + '.break', timeout=0)
-            util.unlink(self.f)
+            util.vfs().unlink2(self.f)
             l.release()
         except error.LockError:
             return locker
@@ -131,7 +131,7 @@
             if self.releasefn:
                 self.releasefn()
             try:
-                util.unlink(self.f)
+                util.vfs().unlink2(self.f)
             except OSError:
                 pass
             for callback in self.postrelease:
diff -r e325d149ed09 -r ecd42350a3f9 mercurial/merge.py
--- a/mercurial/merge.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/mercurial/merge.py	Fri Jun 15 22:59:54 2012 +0900
@@ -358,7 +358,7 @@
             if f == '.hgsubstate': # subrepo states need updating
                 subrepo.submerge(repo, wctx, mctx, wctx, overwrite)
             try:
-                util.unlinkpath(repo.wjoin(f))
+                vfs.unlinkpath(repo.wjoin(f))
             except OSError, inst:
                 if inst.errno != errno.ENOENT:
                     repo.ui.warn(_("update failed to remove %s: %s!\n") %
@@ -400,7 +400,7 @@
                 audit(f)
                 t = wctx.filectx(f).data()
                 repo.wwrite(fd, t, flags)
-                util.unlinkpath(repo.wjoin(f))
+                vfs.unlinkpath(repo.wjoin(f))
             if f2:
                 repo.ui.note(_("getting %s to %s\n") % (f2, fd))
                 t = mctx.filectx(f2).data()
@@ -421,7 +421,7 @@
         elif m == "e": # exec
             flags = a[2]
             repo.wopener.audit(f)
-            util.setflags(repo.wjoin(f), 'l' in flags, 'x' in flags)
+            vfs.setflags(repo.wjoin(f), 'l' in flags, 'x' in flags)
     ms.commit()
     repo.ui.progress(_('updating'), None, total=numupdates, unit=_('files'))
 
diff -r e325d149ed09 -r ecd42350a3f9 mercurial/patch.py
--- a/mercurial/patch.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/mercurial/patch.py	Fri Jun 15 22:59:54 2012 +0900
@@ -430,19 +430,20 @@
 
     def setfile(self, fname, data, mode, copysource):
         islink, isexec = mode
+        vfs = util.vfs()
         if data is None:
-            util.setflags(self._join(fname), islink, isexec)
+            vfs.setflags(self._join(fname), islink, isexec)
             return
         if islink:
             self.opener.symlink(data, fname)
         else:
             self.opener.write(fname, data)
             if isexec:
-                util.setflags(self._join(fname), False, True)
+                vfs.setflags(self._join(fname), False, True)
 
     def unlink(self, fname):
         try:
-            util.unlinkpath(self._join(fname))
+            util.vfs().unlinkpath(self._join(fname))
         except OSError, inst:
             if inst.errno != errno.ENOENT:
                 raise
diff -r e325d149ed09 -r ecd42350a3f9 mercurial/scmutil.py
--- a/mercurial/scmutil.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/mercurial/scmutil.py	Fri Jun 15 22:59:54 2012 +0900
@@ -209,7 +209,7 @@
 
     @util.propertycache
     def _cansymlink(self):
-        return util.checklink(self.base)
+        return util.vfs().checklink(self.base)
 
     def _fixfilemode(self, name):
         if self.createmode is None:
@@ -239,13 +239,13 @@
                 return util.atomictempfile(f, mode, self.createmode)
             try:
                 if 'w' in mode:
-                    util.unlink(f)
+                    vfs.unlink2(f)
                     nlink = 0
                 else:
                     # nlinks() may behave differently for files on Windows
                     # shares if the file is open.
-                    fd = util.posixfile(f)
-                    nlink = util.nlinks(f)
+                    fd = vfs.posixfile(f)
+                    nlink = vfs.nlinks(f)
                     if nlink < 1:
                         nlink = 2 # force mktempcopy (issue1922)
                     fd.close()
@@ -259,8 +259,8 @@
                 if self._trustnlink is None:
                     self._trustnlink = nlink > 1 or util.checknlink(f)
                 if nlink > 1 or not self._trustnlink:
-                    util.rename(util.mktempcopy(f), f)
-        fp = util.posixfile(f, mode)
+                    vfs.rename2(util.mktempcopy(f), f)
+        fp = vfs.posixfile(f, mode)
         if nlink == 0:
             self._fixfilemode(f)
         return fp
@@ -740,7 +740,7 @@
             if gp.op == 'ADD' and not vfs.lexists(dst):
                 flags = (isexec and 'x' or '') + (islink and 'l' or '')
                 repo.wwrite(gp.path, '', flags)
-            util.setflags(dst, islink, isexec)
+            vfs.setflags(dst, islink, isexec)
     addremove(repo, cfiles, similarity=similarity)
     files = patches.keys()
     files.extend([r for r in removes if r not in files])
@@ -828,7 +828,7 @@
     @staticmethod
     def stat(path):
         try:
-            return util.cachestat(path)
+            return util.vfs().cachestat(path)
         except OSError, e:
             if e.errno != errno.ENOENT:
                 raise
diff -r e325d149ed09 -r ecd42350a3f9 mercurial/transaction.py
--- a/mercurial/transaction.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/mercurial/transaction.py	Fri Jun 15 22:59:54 2012 +0900
@@ -24,6 +24,7 @@
     return _active
 
 def _playback(journal, report, opener, entries, unlink=True):
+    vfs = util.vfs()
     for f, o, ignore in entries:
         if o or not unlink:
             try:
@@ -38,11 +39,11 @@
                 fp = opener(f)
                 fn = fp.name
                 fp.close()
-                util.unlink(fn)
+                vfs.unlink2(fn)
             except (IOError, OSError), inst:
                 if inst.errno != errno.ENOENT:
                     raise
-    util.unlink(journal)
+    vfs.unlink2(journal)
 
 class transaction(object):
     def __init__(self, report, opener, journal, after=None, createmode=None):
@@ -56,9 +57,10 @@
         self.journal = journal
         self._queue = []
 
-        self.file = util.posixfile(self.journal, "w")
+        vfs = util.vfs()
+        self.file = vfs.posixfile(self.journal, "w")
         if createmode is not None:
-            util.vfs().chmod(self.journal, createmode & 0666)
+            vfs.chmod(self.journal, createmode & 0666)
 
     def __del__(self):
         if self.journal:
@@ -136,8 +138,9 @@
         self.entries = []
         if self.after:
             self.after()
-        if util.vfs().isfile(self.journal):
-            util.unlink(self.journal)
+        vfs = util.vfs()
+        if vfs.isfile(self.journal):
+            vfs.unlink2(self.journal)
         self.journal = None
 
     @active
@@ -155,7 +158,7 @@
         try:
             if not self.entries:
                 if self.journal:
-                    util.unlink(self.journal)
+                    util.vfs().unlink2(self.journal)
                 return
 
             self.report(_("transaction abort!\n"))
@@ -173,7 +176,7 @@
 def rollback(opener, file, report):
     entries = []
 
-    fp = util.posixfile(file)
+    fp = util.vfs().posixfile(file)
     lines = fp.readlines()
     fp.close()
     for l in lines:
diff -r e325d149ed09 -r ecd42350a3f9 mercurial/util.py
--- a/mercurial/util.py	Fri Jun 15 22:59:54 2012 +0900
+++ b/mercurial/util.py	Fri Jun 15 22:59:54 2012 +0900
@@ -516,7 +516,7 @@
     else:
         if hardlink:
             try:
-                oslink(src, dst)
+                vfsobj.oslink(src, dst)
             except (IOError, OSError):
                 hardlink = False
                 shutil.copy(src, dst)
@@ -589,14 +589,15 @@
     os.close(ld)
 
 def readlock(pathname):
+    vfsobj = vfs()
     try:
-        return vfs().readlink(pathname)
+        return vfsobj.readlink(pathname)
     except OSError, why:
         if why.errno not in (errno.EINVAL, errno.ENOSYS):
             raise
     except AttributeError: # no symlink in os
         pass
-    fp = posixfile(pathname)
+    fp = vfsobj.posixfile(pathname)
     r = fp.read()
     fp.close()
     return r
@@ -716,7 +717,7 @@
     if vfsobj.lexists(f1):
         return False
     try:
-        posixfile(f1, 'w').close()
+        vfsobj.posixfile(f1, 'w').close()
     except IOError:
         return False
 
@@ -724,14 +725,14 @@
     fd = None
     try:
         try:
-            oslink(f1, f2)
+            vfsobj.oslink(f1, f2)
         except OSError:
             return False
 
         # nlinks() may behave differently for files on Windows shares if
         # the file is open.
-        fd = posixfile(f2)
-        return nlinks(f2) > 1
+        fd = vfsobj.posixfile(f2)
+        return vfsobj.nlinks(f2) > 1
     finally:
         if fd is not None:
             fd.close()
@@ -786,25 +787,26 @@
     # Temporary files are created with mode 0600, which is usually not
     # what we want.  If the original file already exists, just copy
     # its mode.  Otherwise, manually obey umask.
-    copymode(name, temp, createmode)
+    vfsobj = vfs()
+    vfsobj.copymode(name, temp, createmode)
     if emptyok:
         return temp
     try:
         try:
-            ifp = posixfile(name, "rb")
+            ifp = vfsobj.posixfile(name, "rb")
         except IOError, inst:
             if inst.errno == errno.ENOENT:
                 return temp
             if not getattr(inst, 'filename', None):
                 inst.filename = name
             raise
-        ofp = posixfile(temp, "wb")
+        ofp = vfsobj.posixfile(temp, "wb")
         for chunk in filechunkiter(ifp):
             ofp.write(chunk)
         ifp.close()
         ofp.close()
     except: # re-raises
-        try: vfs().unlink(temp)
+        try: vfsobj.unlink(temp)
         except OSError: pass
         raise
     return temp
@@ -822,7 +824,7 @@
         self.__name = name      # permanent name
         self._tempname = mktempcopy(name, emptyok=('w' in mode),
                                     createmode=createmode)
-        self._fp = posixfile(self._tempname, mode)
+        self._fp = vfs().posixfile(self._tempname, mode)
 
         # delegated methods
         self.write = self._fp.write
@@ -831,7 +833,7 @@
     def close(self):
         if not self._fp.closed:
             self._fp.close()
-            rename(self._tempname, localpath(self.__name))
+            vfs().rename2(self._tempname, localpath(self.__name))
 
     def discard(self):
         if not self._fp.closed:
@@ -1873,6 +1875,41 @@
     def realpath(self, *args, **kwargs):
         return self._osmod.path.realpath(*args, **kwargs)
 
+    def cachestat(self, *args, **kwargs):
+        return cachestat(*args, **kwargs)
+    def checkexec(self, *args, **kwargs):
+        return checkexec(*args, **kwargs)
+    def checklink(self, *args, **kwargs):
+        return checklink(*args, **kwargs)
+    def copymode(self, *args, **kwargs):
+        return copymode(*args, **kwargs)
+    def isexec(self, *args, **kwargs):
+        return isexec(*args, **kwargs)
+    def makedir(self, *args, **kwargs):
+        return makedir(*args, **kwargs)
+    def nlinks(self, *args, **kwargs):
+        return nlinks(*args, **kwargs)
+    def oslink(self, *args, **kwargs):
+        return oslink(*args, **kwargs)
+    def posixfile(self, *args, **kwargs):
+        return posixfile(*args, **kwargs)
+    def realpath2(self, *args, **kwargs):
+        return realpath(*args, **kwargs)
+    def rename2(self, *args, **kwargs):
+        return rename(*args, **kwargs)
+    def samedevice(self, *args, **kwargs):
+        return samedevice(*args, **kwargs)
+    def samefile(self, *args, **kwargs):
+        return samefile(*args, **kwargs)
+    def setflags(self, *args, **kwargs):
+        return setflags(*args, **kwargs)
+    def statfiles(self, *args, **kwargs):
+        return statfiles(*args, **kwargs)
+    def unlink2(self, *args, **kwargs):
+        return unlink(*args, **kwargs)
+    def unlinkpath(self, *args, **kwargs):
+        return unlinkpath(*args, **kwargs)
+
 _bvfs = _vfs()
 
 def vfs(mode=None):
diff -r e325d149ed09 -r ecd42350a3f9 tests/test-check-code-hg.t
--- a/tests/test-check-code-hg.t	Fri Jun 15 22:59:54 2012 +0900
+++ b/tests/test-check-code-hg.t	Fri Jun 15 22:59:54 2012 +0900
@@ -138,7 +138,7 @@
    >     ui.write('delta size (min/max/avg)             : %d / %d / %d\n'
    warning: unwrapped ui message
   mercurial/commands.py:0:
-   >     ui.write('exec: %s\n' % (util.checkexec(path) and 'yes' or 'no'))
+   >     ui.write('exec: %s\n' % (vfs.checkexec(path) and 'yes' or 'no'))
    warning: unwrapped ui message
   mercurial/commands.py:0:
    >     ui.write('flags  : %s\n' % ', '.join(flags))
@@ -157,7 +157,7 @@
    warning: unwrapped ui message
    warning: unwrapped ui message
   mercurial/commands.py:0:
-   >     ui.write('symlink: %s\n' % (util.checklink(path) and 'yes' or 'no'))
+   >     ui.write('symlink: %s\n' % (vfs.checklink(path) and 'yes' or 'no'))
    warning: unwrapped ui message
   mercurial/i18n.py:0:
    >     if os.path.isdir(localedir):


More information about the Mercurial-devel mailing list