-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d1117bd
Showing
10 changed files
with
523 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
|
||
# Created by https://www.gitignore.io/api/python | ||
|
||
### Python ### | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
env/ | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*,cover | ||
.hypothesis/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# celery beat schedule file | ||
celerybeat-schedule | ||
|
||
# dotenv | ||
.env | ||
|
||
# virtualenv | ||
.venv/ | ||
venv/ | ||
ENV/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
|
||
# Rope project settings | ||
.ropeproject |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
BSD 2-Clause License | ||
|
||
Copyright (c) 2016, Anatolii Aniskovych | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
=============== | ||
Python getdents | ||
=============== | ||
|
||
TBD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import os | ||
|
||
from ._getdents import ( # noqa: ignore=F401 | ||
DT_BLK, | ||
DT_CHR, | ||
DT_DIR, | ||
DT_FIFO, | ||
DT_LNK, | ||
DT_REG, | ||
DT_SOCK, | ||
DT_UNKNOWN, | ||
MIN_GETDENTS_BUFF_SIZE, | ||
O_GETDENTS, | ||
getdents_raw, | ||
) | ||
|
||
|
||
def getdents(path, buff_size=32768, close_fd=False): | ||
if hasattr(path, 'fileno'): | ||
fd = path.fileno() | ||
elif isinstance(path, str): | ||
fd = os.open(path, O_GETDENTS) | ||
close_fd = True | ||
elif isinstance(path, int): | ||
fd = path | ||
else: | ||
raise TypeError('Unsupported type: %s', type(path)) | ||
|
||
try: | ||
yield from ( | ||
(inode, type, name) | ||
for inode, type, name in getdents_raw(fd, buff_size) | ||
if not(type == DT_UNKNOWN or inode == 0 or name in ('.', '..')) | ||
) | ||
finally: | ||
if close_fd: | ||
os.close(fd) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
#include <Python.h> | ||
#include <dirent.h> | ||
#include <fcntl.h> | ||
#include <stddef.h> | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <sys/stat.h> | ||
#include <sys/syscall.h> | ||
|
||
struct linux_dirent64 { | ||
uint64_t d_ino; | ||
int64_t d_off; | ||
unsigned short d_reclen; | ||
unsigned char d_type; | ||
char d_name[]; | ||
}; | ||
|
||
struct getdents_state { | ||
PyObject_HEAD | ||
char *buff; | ||
int bpos; | ||
int fd; | ||
int nread; | ||
size_t buff_size; | ||
bool ready_for_next_batch; | ||
}; | ||
|
||
|
||
#ifndef O_GETDENTS | ||
# define O_GETDENTS (O_DIRECTORY | O_RDONLY | O_NONBLOCK | O_CLOEXEC) | ||
#endif | ||
|
||
#ifndef MIN_GETDENTS_BUFF_SIZE | ||
# define MIN_GETDENTS_BUFF_SIZE (MAXNAMLEN + sizeof(struct linux_dirent64)) | ||
#endif | ||
|
||
static PyObject * | ||
getdents_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) | ||
{ | ||
size_t buff_size; | ||
int fd; | ||
|
||
if (!PyArg_ParseTuple(args, "in", &fd, &buff_size)) | ||
return NULL; | ||
|
||
if (!(fcntl(fd, F_GETFL) & O_DIRECTORY)) { | ||
PyErr_SetString( | ||
PyExc_NotADirectoryError, | ||
"fd must be opened with O_DIRECTORY flag" | ||
); | ||
return NULL; | ||
} | ||
|
||
if (buff_size < MAXNAMLEN + sizeof(struct linux_dirent64)) { | ||
PyErr_SetString( | ||
PyExc_ValueError, | ||
"buff_size is too small" | ||
); | ||
return NULL; | ||
} | ||
|
||
struct getdents_state *state = (void *) type->tp_alloc(type, 0); | ||
|
||
if (!state) | ||
return NULL; | ||
|
||
void *buff = malloc(buff_size); | ||
|
||
if (!buff) | ||
return PyErr_NoMemory(); | ||
|
||
state->buff = buff; | ||
state->buff_size = buff_size; | ||
state->fd = fd; | ||
state->bpos = 0; | ||
state->nread = 0; | ||
state->ready_for_next_batch = true; | ||
return (PyObject *) state; | ||
} | ||
|
||
static void | ||
getdents_dealloc(struct getdents_state *state) | ||
{ | ||
free(state->buff); | ||
Py_TYPE(state)->tp_free(state); | ||
} | ||
|
||
static PyObject * | ||
getdents_next(struct getdents_state *s) | ||
{ | ||
s->ready_for_next_batch = s->bpos >= s->nread; | ||
|
||
if (s->ready_for_next_batch) { | ||
s->bpos = 0; | ||
s->nread = syscall(SYS_getdents64, s->fd, s->buff, s->buff_size); | ||
|
||
if (s->nread == 0) | ||
return NULL; | ||
|
||
if (s->nread == -1) { | ||
PyErr_SetString(PyExc_OSError, "getdents64"); | ||
return NULL; | ||
} | ||
} | ||
|
||
struct linux_dirent64 *d = (struct linux_dirent64 *)(s->buff + s->bpos); | ||
|
||
PyObject *py_name = PyUnicode_DecodeFSDefault(d->d_name); | ||
|
||
PyObject *result = Py_BuildValue("KbO", d->d_ino, d->d_type, py_name); | ||
|
||
s->bpos += d->d_reclen; | ||
|
||
return result; | ||
} | ||
|
||
PyTypeObject getdents_type = { | ||
PyVarObject_HEAD_INIT(NULL, 0) | ||
"getdents_raw", /* tp_name */ | ||
sizeof(struct getdents_state), /* tp_basicsize */ | ||
0, /* tp_itemsize */ | ||
(destructor) getdents_dealloc, /* tp_dealloc */ | ||
0, /* tp_print */ | ||
0, /* tp_getattr */ | ||
0, /* tp_setattr */ | ||
0, /* tp_reserved */ | ||
0, /* tp_repr */ | ||
0, /* tp_as_number */ | ||
0, /* tp_as_sequence */ | ||
0, /* tp_as_mapping */ | ||
0, /* tp_hash */ | ||
0, /* tp_call */ | ||
0, /* tp_str */ | ||
0, /* tp_getattro */ | ||
0, /* tp_setattro */ | ||
0, /* tp_as_buffer */ | ||
Py_TPFLAGS_DEFAULT, /* tp_flags */ | ||
0, /* tp_doc */ | ||
0, /* tp_traverse */ | ||
0, /* tp_clear */ | ||
0, /* tp_richcompare */ | ||
0, /* tp_weaklistoffset */ | ||
PyObject_SelfIter, /* tp_iter */ | ||
(iternextfunc) getdents_next, /* tp_iternext */ | ||
0, /* tp_methods */ | ||
0, /* tp_members */ | ||
0, /* tp_getset */ | ||
0, /* tp_base */ | ||
0, /* tp_dict */ | ||
0, /* tp_descr_get */ | ||
0, /* tp_descr_set */ | ||
0, /* tp_dictoffset */ | ||
0, /* tp_init */ | ||
PyType_GenericAlloc, /* tp_alloc */ | ||
getdents_new, /* tp_new */ | ||
}; | ||
|
||
static struct PyModuleDef getdents_module = { | ||
PyModuleDef_HEAD_INIT, | ||
"getdents", /* m_name */ | ||
"", /* m_doc */ | ||
-1, /* m_size */ | ||
}; | ||
|
||
PyMODINIT_FUNC | ||
PyInit__getdents(void) | ||
{ | ||
if (PyType_Ready(&getdents_type) < 0) | ||
return NULL; | ||
|
||
PyObject *module = PyModule_Create(&getdents_module); | ||
|
||
if (!module) | ||
return NULL; | ||
|
||
Py_INCREF(&getdents_type); | ||
PyModule_AddObject(module, "getdents_raw", (PyObject *) &getdents_type); | ||
PyModule_AddIntMacro(module, DT_BLK); | ||
PyModule_AddIntMacro(module, DT_CHR); | ||
PyModule_AddIntMacro(module, DT_DIR); | ||
PyModule_AddIntMacro(module, DT_FIFO); | ||
PyModule_AddIntMacro(module, DT_LNK); | ||
PyModule_AddIntMacro(module, DT_REG); | ||
PyModule_AddIntMacro(module, DT_SOCK); | ||
PyModule_AddIntMacro(module, DT_UNKNOWN); | ||
PyModule_AddIntMacro(module, O_GETDENTS); | ||
PyModule_AddIntMacro(module, MIN_GETDENTS_BUFF_SIZE); | ||
return module; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[pytest] | ||
testpaths = tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[aliases] | ||
test=pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env python | ||
|
||
from setuptools import setup | ||
|
||
from distutils.core import Extension | ||
|
||
|
||
setup( | ||
name='getdents', | ||
version='0.1', | ||
description='Python binding to linux syscall getdents64.', | ||
long_description=open('README.rst').read(), | ||
classifiers=[ | ||
'License :: OSI Approved :: BSD License', | ||
'Operating System :: POSIX :: Linux', | ||
'Programming Language :: Python :: 3', | ||
'Programming Language :: Python :: Implementation :: CPython', | ||
'Topic :: System :: Filesystems', | ||
], | ||
keywords='getdents', | ||
author='Anatolii Aniskovych', | ||
author_email='[email protected]', | ||
url='http://github.com/ZipFile/python-getdents', | ||
license='BSD-2-Clause', | ||
packages=['getdents'], | ||
include_package_data=True, | ||
zip_safe=False, | ||
ext_modules = [ | ||
Extension('getdents._getdents', sources=['getdents/_getdents.c']), | ||
], | ||
install_requires=[ | ||
'setuptools', | ||
], | ||
setup_requires=['pytest-runner'], | ||
tests_require=['pytest'], | ||
) |
Oops, something went wrong.