sapling/hgext/patchrmdir.pyx
Jun Wu fc29bfdf18 patchrmdir: move to hgext
Summary: Also make it compatible with Windows by making it a no-op on Windows.

Test Plan:
`make local` and try from IPython:

```
In [1]: from hgext import patchrmdir

In [2]: os.rmdir
Out[2]: <function posix.rmdir>

In [3]: patchrmdir.uisetup(None)

In [4]: os.rmdir
Out[4]: <functools.partial at 0x375f580>
```

Reviewers: durham, #mercurial

Reviewed By: durham

Subscribers: durham, fried

Differential Revision: https://phabricator.intern.facebook.com/D6681125

Signature: 6681125:1515523144:e186b45978d4a82ec8a0d18a02b0a524fca1c3b3
2018-01-08 19:25:13 -08:00

68 lines
1.8 KiB
Cython

# patchrmdir.py
#
# Copyright 2017 Facebook, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
"""patch rmdir
Check if a directory is empty before trying to call rmdir on it. This works
around some kernel issues.
Have no effect on Windows.
"""
IF UNAME_SYSNAME != "Windows":
from mercurial import (
extensions,
pycompat,
)
import os
import errno
cdef extern from "dirent.h":
ctypedef struct DIR
struct dirent:
pass
DIR *opendir(const char *)
dirent *readdir(DIR *)
int closedir(DIR *)
cdef int _countdir(const char *path):
"""return min(3, the number of entries inside a directory).
return -1 if the directory cannot be opened.
"""
cdef DIR *d = opendir(path)
if d == NULL:
return -1
cdef dirent *e
cdef int n = 0
while True:
e = readdir(d)
if e == NULL:
break
else:
n += 1
if n > 2:
break
closedir(d)
return n
def _rmdir(orig, path):
n = _countdir(path)
if n >= 3:
# The number 3 is because most systems have "." and "..". For systems
# without them, we fallback to the original rmdir, the behavior should
# still be correct.
# Choose a slightly different error message other than "Directory not
# empty" so the test could notice the difference.
raise OSError(errno.ENOTEMPTY, 'Non-empty directory: %r' % path)
else:
return orig(path)
def uisetup(ui):
extensions.wrapfunction(os, 'rmdir', _rmdir)