5 changed files with 120 additions and 0 deletions
@ -0,0 +1,15 @@ |
|||||
|
PORTNAME= sysmount |
||||
|
PORTVERSION= 1.0 |
||||
|
CATEGORIES= sysutils |
||||
|
MASTER_SITES= http://your-server.local |
||||
|
PKGNAMEPREFIX= ${LUA_PKGNAMEPREFIX} |
||||
|
|
||||
|
MAINTAINER= root@dc365.ru |
||||
|
COMMENT= Native getfsstat binding for Lua |
||||
|
WWW= https://git.dc365.ru/digital-freak/lua-sysmount |
||||
|
|
||||
|
USES= lua:module pkgconfig tar:xz |
||||
|
|
||||
|
PLIST_FILES= ${LUA_MODLIBDIR}/sysmount.so |
||||
|
|
||||
|
.include <bsd.port.mk> |
||||
@ -0,0 +1,3 @@ |
|||||
|
TIMESTAMP = 1779060791 |
||||
|
SHA256 (sysmount-1.0.tar.xz) = ddc305dc21b7689a33ae5f4f04ac28cfc6a925bd8019de60627e4e758e8f1fac |
||||
|
SIZE (sysmount-1.0.tar.xz) = 1356 |
||||
@ -0,0 +1,8 @@ |
|||||
|
This is a lightweight, high-performance Lua binding for the native |
||||
|
FreeBSD getfsstat(2) system call. |
||||
|
|
||||
|
It allows Lua scripts to query active storage mounts and filesystem types |
||||
|
(such as UFS, ZFS, msdosfs) directly from the kernel memory space. By |
||||
|
operating entirely through C bindings, it completely eliminates the need |
||||
|
to spawn external processes or parse text output from utilities like |
||||
|
mount(8), ensuring maximum security and execution speed. |
||||
@ -0,0 +1,31 @@ |
|||||
|
LIB_NAME= sysmount |
||||
|
SRC= sysmount.c |
||||
|
|
||||
|
PREFIX?= /usr/local |
||||
|
|
||||
|
.if !empty(LUA_VER) |
||||
|
LUA_PC= lua-${LUA_VER} |
||||
|
.else |
||||
|
LUA_PC!= pkg-config --list-all | grep -E '^lua-[0-9]' | awk '{print $$1}' | sort -V | tail -n 1 |
||||
|
.endif |
||||
|
|
||||
|
LUA_CFLAGS!= pkg-config --cflags ${LUA_PC} |
||||
|
LUA_MODDIR!= pkg-config --variable=INSTALL_CMOD ${LUA_PC} |
||||
|
|
||||
|
CFLAGS+= -O2 -fPIC -Wall ${LUA_CFLAGS} |
||||
|
LDFLAGS+= -shared |
||||
|
|
||||
|
all: ${LIB_NAME}.so |
||||
|
|
||||
|
${LIB_NAME}.so: ${SRC} |
||||
|
${CC} ${CFLAGS} ${LDFLAGS} -o ${LIB_NAME}.so ${SRC} |
||||
|
|
||||
|
clean: |
||||
|
rm -f ${LIB_NAME}.so |
||||
|
|
||||
|
install: all |
||||
|
mkdir -p ${DESTDIR}${LUA_MODDIR} |
||||
|
install -m 755 ${LIB_NAME}.so ${DESTDIR}${LUA_MODDIR}/${LIB_NAME}.so |
||||
|
|
||||
|
.PHONY: all clean install |
||||
|
|
||||
@ -0,0 +1,63 @@ |
|||||
|
#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; |
||||
|
} |
||||
|
|
||||
Reference in new issue