70 lines
1.9 KiB
C++
70 lines
1.9 KiB
C++
// SPDX-License-Identifier: Apache-2.0
|
||
// SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||
|
||
#include <Python.h>
|
||
|
||
#include <unistd.h>
|
||
|
||
#include <vector>
|
||
|
||
extern "C" {
|
||
|
||
static void _batch_lookup(const std::vector<const char*>& paths,
|
||
std::vector<int>& exists_flags) {
|
||
for (size_t i = 0; i < paths.size(); i++) {
|
||
exists_flags[i] = (access(paths[i], F_OK) == 0) ? 1 : 0;
|
||
}
|
||
}
|
||
|
||
/// @brief Check file existence for a batch of paths.
|
||
/// @param paths list[str] – absolute paths to check.
|
||
/// @return list[bool] – True if the corresponding path exists, False otherwise.
|
||
/// @note Releases the GIL for the entire batch. File existence via access(2).
|
||
static PyObject* batch_lookup(PyObject* /*self*/, PyObject* args) {
|
||
PyObject* path_list;
|
||
if (!PyArg_ParseTuple(args, "O!", &PyList_Type, &path_list)) {
|
||
return nullptr;
|
||
}
|
||
|
||
const Py_ssize_t n = PyList_Size(path_list);
|
||
std::vector<const char*> paths(n);
|
||
for (Py_ssize_t i = 0; i < n; i++) {
|
||
paths[i] = PyUnicode_AsUTF8AndSize(PyList_GetItem(path_list, i), nullptr);
|
||
if (paths[i] == nullptr) {
|
||
return nullptr;
|
||
}
|
||
}
|
||
|
||
std::vector<int> exists_flags(n);
|
||
{
|
||
Py_BEGIN_ALLOW_THREADS _batch_lookup(paths, exists_flags);
|
||
Py_END_ALLOW_THREADS
|
||
}
|
||
|
||
PyObject* result = PyList_New(n);
|
||
if (result == nullptr) {
|
||
return nullptr;
|
||
}
|
||
for (Py_ssize_t i = 0; i < n; i++) {
|
||
PyList_SetItem(result, i, PyBool_FromLong(exists_flags[i]));
|
||
}
|
||
return result;
|
||
}
|
||
|
||
static PyMethodDef fs_io_C_methods[] = {
|
||
{"batch_lookup", batch_lookup, METH_VARARGS,
|
||
"batch_lookup(paths: list[str]) -> list[bool]\n"
|
||
"\n"
|
||
"Check file existence for a batch of paths."},
|
||
{nullptr, nullptr, 0, nullptr},
|
||
};
|
||
|
||
static struct PyModuleDef fs_io_C_module = {
|
||
PyModuleDef_HEAD_INIT, "fs_io_C", "Filesystem helpers for KV offload", -1,
|
||
fs_io_C_methods,
|
||
};
|
||
|
||
PyMODINIT_FUNC PyInit_fs_io_C(void) { return PyModule_Create(&fs_io_C_module); }
|
||
|
||
} // extern "C"
|