jazz
jazz copied to clipboard
filesystem bindings
improvements to filesystem bindings, from lighttouch's fs workarounds and gut's fs workarounds
read file ...?
read file is in the lua standard io library
it's not crazy to have them separated, some languages have separate libraries for io (read/write file content) and filesystem (directories and metadata), rust being one
append to start of file (prepend) copy
basename https://github.com/foundpatterns/machu-picchu/pull/9/files#diff-1f2dfa567dcf95833eddf7aec167fec7R17
Need something like fs.chdir
to change current working directory with Lua.
Example usage:
local err = fs.chdir("/this/path/exists")
assert(err == nil, "Failed to chdir to existing directory")
local err = fs.chdir("/no/such/path")
assert(err != nil, "Chdir to nonexistent directory succeeded")
err
should be string describing the failed syscall.
I dont believe rust have anything builtin to change the directory, but using libc, you could call libc::chdir
but probably would need to create a wrapper around it since such call would be unsafe.
The current set of fs bindings have an unnatural feel and use due to lua bindings not having any direct access anything such as std::fs::File
. While making use of fs::read
and fs::write
can be useful, improving the overall use of the fs bindings can help with functionality with little bottleneck. This can include making use of std::fs::File
or even std::fs::OpenOptions
(probably might be the preferred choice for a little more flexibility since they both will return std::fs::File
anyway but OpenOptions
expose more options), being able to read/write data seamlessly, be able to seek through the data and call any related functions in a safe manner (in terms of memory and security), which in theory can replace the io calls within lua (especially since rlua doesnt exactly provide safety with embedded lua it has so having alternatives would be best in this case).
https://doc.rust-lang.org/std/env/fn.set_current_dir.html
While making use of fs::read and fs::write can be useful.
There is file:read("a")
in Lua to read all file directly to memory from the start or current position of file handle.
Actually it makes sense to completely replaces the io
with Rust implementation.
Yea I saw that function not to long ago (I dont really look in std::env) so I can implement it in a few.
Yea, lua does have that but the problem is that such calls isnt exactly done in a safe manner and rlua doesnt exactly provide any safety for many (if not all) of lua internal functions.
@dariusc93 Oh, where can I read more about that?
Alot of the information is in rlua description. Other parts can be done by reviewing rlua code and how it interact with the embedded lua code, though everything there is considered to be WIP so things will likely change (which may or may not be breaking on rust side).
https://github.com/kyren/rlua/issues/38#issuecomment-325875248
get current dir https://doc.rust-lang.org/std/env/fn.current_dir.html
I have this already in a local branch. Let me rebase and ill push it
is writeable
#!/usr/bin/env torchbear
print(cwd)
returns nil
I think that's holding back https://github.com/foundpatterns/torchbear-libs/issues/7
There is no cwd
in torchbear. You can use fs.current_dir()
to get the current directory.
cool. thanks
reopening for https://github.com/foundpatterns/mp-installer/blob/9ee292956dcec2775068707f2fa8c1e91f4731e5/init.lua#L46-L48