Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: LuaFileSystem for redbean #215

Closed
username-computer opened this issue Jul 19, 2021 · 4 comments
Closed

Request: LuaFileSystem for redbean #215

username-computer opened this issue Jul 19, 2021 · 4 comments

Comments

@username-computer
Copy link

I see a lot of potential in redbean. I really keep this project on my radar.
Using the browser as GUI and Lua as the dynamic language in combination with HTML, CSS and Javascript, really flexible.
But at the moment there is something missing, something to see and traverse the computers filesystem.

Lua on itself is lacking in that department, but the cross-platform third-party library LuaFileSystem fixes this.

First I tried building LuaFileSystem so I had a .so file and placing it in the /.lua directory so require could find it. This obviously failed.
I have read the closed and open issues and found: using shared libraries #137.
Now I understand that conflicts may arise when using shared libraries in combo with Cosmopolitan.
Still, I think it would be a great addition if somehow the LuaFileSystem library could be included with redbean.

If I could help with some things in relation to this issue, please give me some directions.
Thanks and keep up the good work ✌️:bowtie:

@mundusnine
Copy link

This would be trivial to implement as cosmopolitan libc implements the C API to access the filesystem.

You have two possible solutions:

  1. You copy the files from the LuaFilesystem into a third_party folder in cosmo libc and change the includes in the LuaFilesystem c files to use cosmo's libc(fopen,fwrite etc.)
  2. You make it from scratch and learn how to code in C. A good starting point would be to use new.sh to generate the cruft(gnumake file and add deps etc.)

@pkulchenko
Copy link
Collaborator

@username-computer, it's not too difficult to add LFS (for example, see #185 that added lsqlite support), but I think @jart is striving to keep the code small and simple, so the functions in LFS will likely duplicate those already available in cosmopolitan (or those that should/will be available), so it's not likely to be accepted.

As @mundusnine described, you should still be able to add it yourself though, if you want to, as it's just one file, so the changes will be very similar to those in #185.

Another option is to identify those functions you really need (out of those that LFS provides) and check which ones may already be available in cosmopolitan and which ones may need to be added. For example, lstat, fcntl (lock/unlock), mkdir/rmdir/opendir, chmod/chown, and some other functions are already available, but not exposed to the Lua code, so it may be sufficiently simple to add them. Others like chdir() and currentdir() may not be applicable to redbean.

pkulchenko added a commit to pkulchenko/cosmopolitan that referenced this issue Jul 24, 2021
pkulchenko added a commit to pkulchenko/cosmopolitan that referenced this issue Jul 25, 2021
pkulchenko added a commit to pkulchenko/cosmopolitan that referenced this issue Jul 25, 2021
@pkulchenko
Copy link
Collaborator

@username-computer, something like this should work: https://github.com/pkulchenko/cosmopolitan/tree/redbean-lua-lfs. This would include lfs into redbean and allow the code like print(require("lfs").attributes("redbean.com").size) to execute. I've only superficially tested it, but those methods I tried worked.

I'm not proposing to merge this, as I suspect @jart has other ways of implementing the same functionality, but it's available if you need it.

@username-computer
Copy link
Author

Thank you @pkulchenko.
I cloned your branche and checked out the ee68aed commit.
When I tried to build it failed (to build).
It's probably on my end (lack of knowledge), but the ee68aed commit showed me how to edit the project to add LuaFileSystem extension.
So I cloned the cosmopolitan, added and edited the files, then build it with make -j8 -O o//tool/net/redbean.com
This worked.

I added lfs = require("lfs") to the .init.lua file so lfs is usable in every other .lua file.
Ran some simple checks on Linux and Win10 and on both the most useful of the functions work.
Of the bunch I checked lfs.touch and lfs.link didn't work on Win10, I quess that is expected.

Very cool.
As @mundusnine has posted, libc contains many of the functions are already.
It only needs some composition, a Lua wrapper for redbean and off you go.
But for me, I never worked with C so the learning curve is steep.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants