|
| 1 | +#include <Python.h> |
| 2 | +#include <dirent.h> |
| 3 | +#include <fcntl.h> |
| 4 | +#include <stddef.h> |
| 5 | +#include <stdbool.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include <unistd.h> |
| 8 | +#include <stdlib.h> |
| 9 | +#include <sys/stat.h> |
| 10 | +#include <sys/syscall.h> |
| 11 | + |
| 12 | +struct linux_dirent64 { |
| 13 | + uint64_t d_ino; |
| 14 | + int64_t d_off; |
| 15 | + unsigned short d_reclen; |
| 16 | + unsigned char d_type; |
| 17 | + char d_name[]; |
| 18 | +}; |
| 19 | + |
| 20 | +struct getdents_state { |
| 21 | + PyObject_HEAD |
| 22 | + char *buff; |
| 23 | + int bpos; |
| 24 | + int fd; |
| 25 | + int nread; |
| 26 | + size_t buff_size; |
| 27 | + bool ready_for_next_batch; |
| 28 | +}; |
| 29 | + |
| 30 | + |
| 31 | +#ifndef O_GETDENTS |
| 32 | +# define O_GETDENTS (O_DIRECTORY | O_RDONLY | O_NONBLOCK | O_CLOEXEC) |
| 33 | +#endif |
| 34 | + |
| 35 | +#ifndef MIN_GETDENTS_BUFF_SIZE |
| 36 | +# define MIN_GETDENTS_BUFF_SIZE (MAXNAMLEN + sizeof(struct linux_dirent64)) |
| 37 | +#endif |
| 38 | + |
| 39 | +static PyObject * |
| 40 | +getdents_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 41 | +{ |
| 42 | + size_t buff_size; |
| 43 | + int fd; |
| 44 | + |
| 45 | + if (!PyArg_ParseTuple(args, "in", &fd, &buff_size)) |
| 46 | + return NULL; |
| 47 | + |
| 48 | + if (!(fcntl(fd, F_GETFL) & O_DIRECTORY)) { |
| 49 | + PyErr_SetString( |
| 50 | + PyExc_NotADirectoryError, |
| 51 | + "fd must be opened with O_DIRECTORY flag" |
| 52 | + ); |
| 53 | + return NULL; |
| 54 | + } |
| 55 | + |
| 56 | + if (buff_size < MAXNAMLEN + sizeof(struct linux_dirent64)) { |
| 57 | + PyErr_SetString( |
| 58 | + PyExc_ValueError, |
| 59 | + "buff_size is too small" |
| 60 | + ); |
| 61 | + return NULL; |
| 62 | + } |
| 63 | + |
| 64 | + struct getdents_state *state = (void *) type->tp_alloc(type, 0); |
| 65 | + |
| 66 | + if (!state) |
| 67 | + return NULL; |
| 68 | + |
| 69 | + void *buff = malloc(buff_size); |
| 70 | + |
| 71 | + if (!buff) |
| 72 | + return PyErr_NoMemory(); |
| 73 | + |
| 74 | + state->buff = buff; |
| 75 | + state->buff_size = buff_size; |
| 76 | + state->fd = fd; |
| 77 | + state->bpos = 0; |
| 78 | + state->nread = 0; |
| 79 | + state->ready_for_next_batch = true; |
| 80 | + return (PyObject *) state; |
| 81 | +} |
| 82 | + |
| 83 | +static void |
| 84 | +getdents_dealloc(struct getdents_state *state) |
| 85 | +{ |
| 86 | + free(state->buff); |
| 87 | + Py_TYPE(state)->tp_free(state); |
| 88 | +} |
| 89 | + |
| 90 | +static PyObject * |
| 91 | +getdents_next(struct getdents_state *s) |
| 92 | +{ |
| 93 | + s->ready_for_next_batch = s->bpos >= s->nread; |
| 94 | + |
| 95 | + if (s->ready_for_next_batch) { |
| 96 | + s->bpos = 0; |
| 97 | + s->nread = syscall(SYS_getdents64, s->fd, s->buff, s->buff_size); |
| 98 | + |
| 99 | + if (s->nread == 0) |
| 100 | + return NULL; |
| 101 | + |
| 102 | + if (s->nread == -1) { |
| 103 | + PyErr_SetString(PyExc_OSError, "getdents64"); |
| 104 | + return NULL; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + struct linux_dirent64 *d = (struct linux_dirent64 *)(s->buff + s->bpos); |
| 109 | + |
| 110 | + PyObject *py_name = PyUnicode_DecodeFSDefault(d->d_name); |
| 111 | + |
| 112 | + PyObject *result = Py_BuildValue("KbO", d->d_ino, d->d_type, py_name); |
| 113 | + |
| 114 | + s->bpos += d->d_reclen; |
| 115 | + |
| 116 | + return result; |
| 117 | +} |
| 118 | + |
| 119 | +PyTypeObject getdents_type = { |
| 120 | + PyVarObject_HEAD_INIT(NULL, 0) |
| 121 | + "getdents_raw", /* tp_name */ |
| 122 | + sizeof(struct getdents_state), /* tp_basicsize */ |
| 123 | + 0, /* tp_itemsize */ |
| 124 | + (destructor) getdents_dealloc, /* tp_dealloc */ |
| 125 | + 0, /* tp_print */ |
| 126 | + 0, /* tp_getattr */ |
| 127 | + 0, /* tp_setattr */ |
| 128 | + 0, /* tp_reserved */ |
| 129 | + 0, /* tp_repr */ |
| 130 | + 0, /* tp_as_number */ |
| 131 | + 0, /* tp_as_sequence */ |
| 132 | + 0, /* tp_as_mapping */ |
| 133 | + 0, /* tp_hash */ |
| 134 | + 0, /* tp_call */ |
| 135 | + 0, /* tp_str */ |
| 136 | + 0, /* tp_getattro */ |
| 137 | + 0, /* tp_setattro */ |
| 138 | + 0, /* tp_as_buffer */ |
| 139 | + Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 140 | + 0, /* tp_doc */ |
| 141 | + 0, /* tp_traverse */ |
| 142 | + 0, /* tp_clear */ |
| 143 | + 0, /* tp_richcompare */ |
| 144 | + 0, /* tp_weaklistoffset */ |
| 145 | + PyObject_SelfIter, /* tp_iter */ |
| 146 | + (iternextfunc) getdents_next, /* tp_iternext */ |
| 147 | + 0, /* tp_methods */ |
| 148 | + 0, /* tp_members */ |
| 149 | + 0, /* tp_getset */ |
| 150 | + 0, /* tp_base */ |
| 151 | + 0, /* tp_dict */ |
| 152 | + 0, /* tp_descr_get */ |
| 153 | + 0, /* tp_descr_set */ |
| 154 | + 0, /* tp_dictoffset */ |
| 155 | + 0, /* tp_init */ |
| 156 | + PyType_GenericAlloc, /* tp_alloc */ |
| 157 | + getdents_new, /* tp_new */ |
| 158 | +}; |
| 159 | + |
| 160 | +static struct PyModuleDef getdents_module = { |
| 161 | + PyModuleDef_HEAD_INIT, |
| 162 | + "getdents", /* m_name */ |
| 163 | + "", /* m_doc */ |
| 164 | + -1, /* m_size */ |
| 165 | +}; |
| 166 | + |
| 167 | +PyMODINIT_FUNC |
| 168 | +PyInit__getdents(void) |
| 169 | +{ |
| 170 | + if (PyType_Ready(&getdents_type) < 0) |
| 171 | + return NULL; |
| 172 | + |
| 173 | + PyObject *module = PyModule_Create(&getdents_module); |
| 174 | + |
| 175 | + if (!module) |
| 176 | + return NULL; |
| 177 | + |
| 178 | + Py_INCREF(&getdents_type); |
| 179 | + PyModule_AddObject(module, "getdents_raw", (PyObject *) &getdents_type); |
| 180 | + PyModule_AddIntMacro(module, DT_BLK); |
| 181 | + PyModule_AddIntMacro(module, DT_CHR); |
| 182 | + PyModule_AddIntMacro(module, DT_DIR); |
| 183 | + PyModule_AddIntMacro(module, DT_FIFO); |
| 184 | + PyModule_AddIntMacro(module, DT_LNK); |
| 185 | + PyModule_AddIntMacro(module, DT_REG); |
| 186 | + PyModule_AddIntMacro(module, DT_SOCK); |
| 187 | + PyModule_AddIntMacro(module, DT_UNKNOWN); |
| 188 | + PyModule_AddIntMacro(module, O_GETDENTS); |
| 189 | + PyModule_AddIntMacro(module, MIN_GETDENTS_BUFF_SIZE); |
| 190 | + return module; |
| 191 | +} |
0 commit comments