Ferenc Wagner <wferi at niif.hu> writes:
> "H. Peter Anvin" <hpa at zytor.com> writes:
>
>> On 08/29/2013 04:14 AM, Ferenc Wagner wrote:
>>
>>> "H. Peter Anvin" <hpa at zytor.com> writes:
>>>
>>>> On 08/22/2013 10:20 AM, Ferenc Wagner wrote:
>>>>
>>>>> Now that Syslinux has ls.c32 and lua.c32, it should be
possible to build
>>>>> a customizable boot menu in the bootloader itself, instead
of generating
>>>>> it beforehand by hooking into the kernel
installation/removal process.
>>>>> Sure that would not work over TFTP (no ls), but the already
mentioned
>>>>> 20th century way would not either. Sadly, directory
handling is not
>>>>> implemented in the embedded Lua interpreter, or at least I
failed to
>>>>> find it. Is there perhaps some deep reason for this? Or
is this idea
>>>>> useless anyway?
>>>>
>>>> No, there isn't any reason for that. It's just a
matter of noone having
>>>> done it yet.
Please find below a proof of concept patch. The inconsistent
indentation is actually consistent with the original code at
https://github.com/keplerproject/luafilesystem/blob/master/src/lfs.c.
I'd much prefer using a core stat() implementation if at all possible
(the lfs directory iterator unfortunately does not return the entry
type), but I didn't have the wits to implement something based on
searchdir() for example... What would be the best way?
Otherwise, code like this:
for name in lfs.dir (".") do
if lfs.attributes (name,"mode") == "file" then
from,to,version = string.find (name,"^vmlinuz%-(.*)")
if from then
initrd = "initrd.img-"..version
if lfs.attributes (initrd,"size") then
print (name,initrd)
else
print (name)
end
end
end
end
works now, which is progress, I think.
>>>> It would be a very good thing to get this done.
>>>
>>> Thanks, glad to hear it. And would it be necessary to reimplement
the
>>> menu system in Lua, or would it be possible to hand over the
generated
>>> config to menu.c32 somehow? If not, then maybe extending menu.c32
with
>>> some "dynamic" directives would be a lot easier.
Unfortunately that
>>> would restrict the functionality to the simple menu system and also
make
>>> it more rigid, but that's probably the bulk of the use cases
anyway...
>>
>> One of the main reasons for the code restructuring into ELF libraries
is
>> that we should be able to set up configurations in memory. There are
>> two ways we could do that... either by manipulating the menu data
>> structures and just making them persistent, or by introducing a concept
>> of "in-memory files" which persist across modules.
>>
>> Which one would you think would be easier? The downside of the latter
>> is that the existing menu structure would not be available, but the
>> upside is that we wouldn't be introducing a whole new interface...
>
> Unfortunately I'm not familiar with the current (planned?) architecture
> for this. Should the core profit from this? I added some DHCP macros
> before, so I think it should (optionally). A wholesale solution would
> be adding the possibility of Lua configuration files, for example using
> syslinux.lua if lua.c32 is present). This may even be possible without
> changing the internal data structures representing the configuration,
> and would only require a new entry point into menu.c32, skipping the
> config file parsing.
>
> Another way would be adding Lua "escapes" to the current
configuration
> language, executing files or code blocks and substituting strings. This
> would still require the current parser in the end, which feels wasteful.
>
> Rewriting the complex menu system in Lua would open new UI possibilities
> as well, while removing the need for recompilation, lifting its main
> weakness. Or maybe adding new dotcommands to menu.c32 which invoke Lua
> functions manipulating its internal data structures would mean a shorter
> path there...
>
> Anyway, I would prefer optional deeper integration to ad-hoc bolts-on.
Any take on these issues? For my urgent needs, implementing a very
simple menu system in Lua looks like the immediate way forward.
Actually, moving all menu code to Lua may not be such a terrible idea...
Regards,
Feri.
commit 06f8e89209cf49b1e26cc980a39e06c27316405e
Author: Ferenc W?gner <wferi at niif.hu>
Date: Thu Oct 3 18:14:48 2013 +0200
lua: Add the LuaFileSystem library
Port the read-only part of the LuaFileSystem File System Library to lua.c32.
The implementation of lfs.attributes required simulating stat() via fstat(),
which -- besides being ugly -- reduced functionality further.
diff --git a/com32/lua/src/Makefile b/com32/lua/src/Makefile
index f3625e1..3c6a529 100644
--- a/com32/lua/src/Makefile
+++ b/com32/lua/src/Makefile
@@ -44,6 +44,7 @@ LIBLUA_OBJS += cpu.o
LIBLUA_OBJS += pci.o
LIBLUA_OBJS += vesa.o
LIBLUA_OBJS += dhcp.o
+LIBLUA_OBJS += lfs.o
CFLAGS += -DLUA_ANSI
diff --git a/com32/lua/src/lfs.c b/com32/lua/src/lfs.c
new file mode 100644
index 0000000..0b94e03
--- /dev/null
+++ b/com32/lua/src/lfs.c
@@ -0,0 +1,296 @@
+/*
+** Read only code copied from:
+**
+** LuaFileSystem
+** Copyright Kepler Project 2003 (http://www.keplerproject.org/luafilesystem)
+**
+** File system manipulation library.
+** This library offers these functions:
+** lfs.attributes (filepath [, attributename])
+** lfs.chdir (path)
+** lfs.currentdir ()
+** lfs.dir (path)
+**
+** $Id: lfs.c,v 1.61 2009/07/04 02:10:16 mascarenhas Exp $
+*/
+
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+
+#define chdir_error strerror(errno)
+
+#define LFS_LIBNAME "lfs"
+
+/* Size of path buffer string, stolen from pwd.c */
+#ifndef PATH_MAX
+# ifdef NAME_MAX
+# define PATH_MAX NAME_MAX
+# elif FILENAME_MAX
+# define PATH_MAX FILENAME_MAX
+# else
+# define PATH_MAX 256
+# endif /* NAME_MAX */
+#endif /* PATH_MAX */
+
+
+#define DIR_METATABLE "directory metatable"
+typedef struct dir_data {
+ int closed;
+ DIR *dir;
+} dir_data;
+
+
+#define STAT_STRUCT struct stat
+#define STAT_FUNC stat_via_fstat
+
+/* Emulate stat via fstat */
+int stat_via_fstat (const char *path, struct stat *buf)
+{
+ int fd = open (path, O_RDONLY);
+ if (fd == -1) {
+ DIR *dir = opendir (path);
+ if (!dir) return -1;
+ closedir (dir);
+ buf->st_mode=S_IFDIR;
+ buf->st_size=0;
+ return 0;
+ }
+ if (fstat (fd, buf) == -1) {
+ int err = errno;
+ close (fd);
+ errno = err;
+ return -1;
+ }
+ close (fd);
+ return 0;
+}
+
+/*
+** This function changes the working (current) directory
+*/
+static int change_dir (lua_State *L) {
+ const char *path = luaL_checkstring(L, 1);
+ if (chdir(path)) {
+ lua_pushnil (L);
+ lua_pushfstring (L,"Unable to change working directory to
'%s'\n%s\n",
+ path, chdir_error);
+ return 2;
+ } else {
+ lua_pushboolean (L, 1);
+ return 1;
+ }
+}
+
+
+/*
+** This function returns the current directory
+** If unable to get the current directory, it returns nil
+** and a string describing the error
+*/
+static int get_dir (lua_State *L) {
+ char *path;
+ /* Passing (NULL, 0) is not guaranteed to work. Use a temp buffer and size
instead. */
+ char buf[PATH_MAX];
+ if ((path = getcwd(buf, PATH_MAX)) == NULL) {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno));
+ return 2;
+ }
+ else {
+ lua_pushstring(L, path);
+ return 1;
+ }
+}
+
+
+/*
+** Directory iterator
+*/
+static int dir_iter (lua_State *L) {
+ struct dirent *entry;
+ dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE);
+ luaL_argcheck (L, d->closed == 0, 1, "closed directory");
+ if ((entry = readdir (d->dir)) != NULL) {
+ lua_pushstring (L, entry->d_name);
+ return 1;
+ } else {
+ /* no more entries => close directory */
+ closedir (d->dir);
+ d->closed = 1;
+ return 0;
+ }
+}
+
+
+/*
+** Closes directory iterators
+*/
+static int dir_close (lua_State *L) {
+ dir_data *d = (dir_data *)lua_touserdata (L, 1);
+ if (!d->closed && d->dir) {
+ closedir (d->dir);
+ }
+ d->closed = 1;
+ return 0;
+}
+
+
+/*
+** Factory of directory iterators
+*/
+static int dir_iter_factory (lua_State *L) {
+ const char *path = luaL_checkstring (L, 1);
+ dir_data *d;
+ lua_pushcfunction (L, dir_iter);
+ d = (dir_data *) lua_newuserdata (L, sizeof(dir_data));
+ luaL_getmetatable (L, DIR_METATABLE);
+ lua_setmetatable (L, -2);
+ d->closed = 0;
+ d->dir = opendir (path);
+ if (d->dir == NULL)
+ luaL_error (L, "cannot open %s: %s", path, strerror
(errno));
+ return 2;
+}
+
+
+/*
+** Creates directory metatable.
+*/
+static int dir_create_meta (lua_State *L) {
+ luaL_newmetatable (L, DIR_METATABLE);
+
+ /* Method table */
+ lua_newtable(L);
+ lua_pushcfunction (L, dir_iter);
+ lua_setfield(L, -2, "next");
+ lua_pushcfunction (L, dir_close);
+ lua_setfield(L, -2, "close");
+
+ /* Metamethods */
+ lua_setfield(L, -2, "__index");
+ lua_pushcfunction (L, dir_close);
+ lua_setfield (L, -2, "__gc");
+ return 1;
+}
+
+
+/*
+** Convert the inode protection mode to a string.
+*/
+static const char *mode2string (mode_t mode) {
+ if ( S_ISREG(mode) )
+ return "file";
+ else if ( S_ISDIR(mode) )
+ return "directory";
+ else if ( S_ISLNK(mode) )
+ return "link";
+ else if ( S_ISSOCK(mode) )
+ return "socket";
+ else if ( S_ISFIFO(mode) )
+ return "named pipe";
+ else if ( S_ISCHR(mode) )
+ return "char device";
+ else if ( S_ISBLK(mode) )
+ return "block device";
+ else
+ return "other";
+}
+
+
+/* inode protection mode */
+static void push_st_mode (lua_State *L, STAT_STRUCT *info) {
+ lua_pushstring (L, mode2string (info->st_mode));
+}
+/* file size, in bytes */
+static void push_st_size (lua_State *L, STAT_STRUCT *info) {
+ lua_pushnumber (L, (lua_Number)info->st_size);
+}
+static void push_invalid (lua_State *L, STAT_STRUCT *info) {
+ luaL_error(L, "invalid attribute name");
+ info->st_size = 0; /* never reached */
+}
+
+typedef void (*_push_function) (lua_State *L, STAT_STRUCT *info);
+
+struct _stat_members {
+ const char *name;
+ _push_function push;
+};
+
+struct _stat_members members[] = {
+ { "mode", push_st_mode },
+ { "size", push_st_size },
+ { NULL, push_invalid }
+};
+
+/*
+** Get file or symbolic link information
+*/
+static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) {
+ int i;
+ STAT_STRUCT info;
+ const char *file = luaL_checkstring (L, 1);
+
+ if (st(file, &info)) {
+ lua_pushnil (L);
+ lua_pushfstring (L, "cannot obtain information from file
`%s'", file);
+ return 2;
+ }
+ if (lua_isstring (L, 2)) {
+ int v;
+ const char *member = lua_tostring (L, 2);
+ if (strcmp (member, "mode") == 0) v = 0;
+#ifndef _WIN32
+ else if (strcmp (member, "blocks") == 0) v = 11;
+ else if (strcmp (member, "blksize") == 0) v = 12;
+#endif
+ else /* look for member */
+ for (v = 1; members[v].name; v++)
+ if (*members[v].name == *member)
+ break;
+ /* push member value and return */
+ members[v].push (L, &info);
+ return 1;
+ } else if (!lua_istable (L, 2))
+ /* creates a table if none is given */
+ lua_newtable (L);
+ /* stores all members in table on top of the stack */
+ for (i = 0; members[i].name; i++) {
+ lua_pushstring (L, members[i].name);
+ members[i].push (L, &info);
+ lua_rawset (L, -3);
+ }
+ return 1;
+}
+
+
+/*
+** Get file information using stat.
+*/
+static int file_info (lua_State *L) {
+ return _file_info_ (L, STAT_FUNC);
+}
+
+
+static const struct luaL_Reg fslib[] = {
+ {"attributes", file_info},
+ {"chdir", change_dir},
+ {"currentdir", get_dir},
+ {"dir", dir_iter_factory},
+ {NULL, NULL},
+};
+
+LUALIB_API int luaopen_lfs (lua_State *L) {
+ dir_create_meta (L);
+ luaL_openlib (L, LFS_LIBNAME, fslib, 0);
+ return 1;
+}
diff --git a/com32/lua/src/linit.c b/com32/lua/src/linit.c
index 6e97873..e50589e 100644
--- a/com32/lua/src/linit.c
+++ b/com32/lua/src/linit.c
@@ -35,6 +35,7 @@ static const luaL_Reg lualibs[] = {
{LUA_VESALIBNAME, luaopen_vesa},
{LUA_DHCPLIBNAME, luaopen_dhcp},
#endif
+ {LUA_LFSLIBNAME, luaopen_lfs},
{NULL, NULL}
};
diff --git a/com32/lua/src/lualib.h b/com32/lua/src/lualib.h
index 40d1bf2..9e3f490 100644
--- a/com32/lua/src/lualib.h
+++ b/com32/lua/src/lualib.h
@@ -59,6 +59,9 @@ LUALIB_API int (luaopen_cpu) (lua_State *L);
LUALIB_API int (luaopen_dhcp) (lua_State *L);
#endif
+#define LUA_LFSLIBNAME "lfs"
+LUALIB_API int (luaopen_lfs) (lua_State *L);
+
/* open all previous libraries */
LUALIB_API void (luaL_openlibs) (lua_State *L);