D588: win32: use fewer system calls for unlink()

abuehl (Adrian Buehlmann) phabricator at mercurial-scm.org
Sun Sep 17 04:08:52 EDT 2017


abuehl added a comment.


  In https://phab.mercurial-scm.org/D588#9989, @indygreg wrote:
  
  > Would it be safe to keep the ``os.stat()`` code and return if the file doesn't exist? That at least allows us to do the "is directory" and "file missing" check with a single system call. That will avoid the random number generation and the 2nd system call for the rename when the file doesn't exist.
  
  
  I guess something like
  
    diff --git a/mercurial/win32.py b/mercurial/win32.py
    --- a/mercurial/win32.py
    +++ b/mercurial/win32.py
    @@ -12,6 +12,7 @@
     import msvcrt
     import os
     import random
    +import stat
     import subprocess
    
     from . import (
    @@ -522,7 +523,8 @@
     def unlink(f):
         '''try to implement POSIX' unlink semantics on Windows'''
    
    -    if os.path.isdir(f):
    +    st = os.stat(f)
    +    if stat.S_ISDIR(st.st_mode):
             # use EPERM because it is POSIX prescribed value, even though
             # unlink(2) on directories returns EISDIR on Linux
             raise IOError(errno.EPERM,
  
  might work (not tested).
  
  If f does not exist, the os.stat call will raise directly, which would spare us another system call in that case.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D588

To: indygreg, #hg-reviewers, quark
Cc: abuehl, durin42, quark, mercurial-devel


More information about the Mercurial-devel mailing list