diff --git a/freebsd-port/sysutils/lua-sysmount/Makefile b/freebsd-port/sysutils/lua-sysmount/Makefile new file mode 100644 index 0000000..22ac1e1 --- /dev/null +++ b/freebsd-port/sysutils/lua-sysmount/Makefile @@ -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 diff --git a/freebsd-port/sysutils/lua-sysmount/distinfo b/freebsd-port/sysutils/lua-sysmount/distinfo new file mode 100644 index 0000000..a9dcb75 --- /dev/null +++ b/freebsd-port/sysutils/lua-sysmount/distinfo @@ -0,0 +1,3 @@ +TIMESTAMP = 1779060791 +SHA256 (sysmount-1.0.tar.xz) = ddc305dc21b7689a33ae5f4f04ac28cfc6a925bd8019de60627e4e758e8f1fac +SIZE (sysmount-1.0.tar.xz) = 1356 diff --git a/freebsd-port/sysutils/lua-sysmount/pkg-descr b/freebsd-port/sysutils/lua-sysmount/pkg-descr new file mode 100644 index 0000000..5961526 --- /dev/null +++ b/freebsd-port/sysutils/lua-sysmount/pkg-descr @@ -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. diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..ac11559 --- /dev/null +++ b/src/Makefile @@ -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 + diff --git a/src/sysmount.c b/src/sysmount.c new file mode 100644 index 0000000..ae20c42 --- /dev/null +++ b/src/sysmount.c @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include + +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; +} +