A lightweight, high-performance, and secure Lua binding for the native FreeBSD getfsstat(2) system call.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 

63 lines
2.0 KiB

#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#include <stdlib.h>
#include <lua.h>
#include <lauxlib.h>
static int lua_getfsstat(lua_State *L) {
struct statfs *mntbuf;
int num_mounts;
// Вызываем getfsstat первый раз с NULL, чтобы узнать точное количество монтирований
num_mounts = getfsstat(NULL, 0, MNT_NOWAIT);
if (num_mounts <= 0) {
lua_pushnil(L);
lua_pushstring(L, "Ошибка получения количества монтирований");
return 2;
}
// Выделяем память под буфер структур statfs
mntbuf = malloc(num_mounts * sizeof(struct statfs));
if (mntbuf == NULL) {
lua_pushnil(L);
lua_pushstring(L, "Ошибка выделения памяти");
return 2;
}
// Заполняем буфер реальными данными из ядра
num_mounts = getfsstat(mntbuf, num_mounts * sizeof(struct statfs), MNT_NOWAIT);
// Создаем результирующий массив (таблицу) в Lua
lua_newtable(L);
for (int i = 0; i < num_mounts; i++) {
// Создаем подтаблицу для конкретного монтирования
lua_newtable(L);
lua_pushstring(L, mntbuf[i].f_mntfromname);
lua_setfield(L, -2, "dev");
lua_pushstring(L, mntbuf[i].f_mntonname);
lua_setfield(L, -2, "mnt");
lua_pushstring(L, mntbuf[i].f_fstypename);
lua_setfield(L, -2, "type");
// Кладем подтаблицу в общий массив (индексация в Lua с 1)
lua_rawseti(L, -2, i + 1);
}
free(mntbuf);
return 1; // Возвращаем одну таблицу
}
// Точка входа для require("sysmount")
int luaopen_sysmount(lua_State *L) {
static const struct luaL_Reg mylib[] = {
{ "getfsstat", lua_getfsstat },
{ NULL, NULL }
};
luaL_newlib(L, mylib);
return 1;
}