jazz icon indicating copy to clipboard operation
jazz copied to clipboard

filesystem bindings

Open naturallymitchell opened this issue 5 years ago • 20 comments

improvements to filesystem bindings, from lighttouch's fs workarounds and gut's fs workarounds

  • [x] rm -r
  • [x] touch
  • [x] join
  • [x] symlink
  • [x] current working directory
  • [x] remove_file
  • [x] move_file
  • [ ] move_dir
  • [ ] #283

naturallymitchell avatar Oct 08 '18 00:10 naturallymitchell

read file ...?

naturallymitchell avatar Nov 08 '18 00:11 naturallymitchell

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

Arnaz87 avatar Nov 08 '18 01:11 Arnaz87

append to start of file (prepend) copy

naturallymitchell avatar Nov 18 '18 23:11 naturallymitchell

open

naturallymitchell avatar Nov 20 '18 16:11 naturallymitchell

basename https://github.com/foundpatterns/machu-picchu/pull/9/files#diff-1f2dfa567dcf95833eddf7aec167fec7R17

naturallymitchell avatar Dec 13 '18 15:12 naturallymitchell

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.

sineemore avatar Dec 18 '18 22:12 sineemore

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).

dariusc93 avatar Dec 18 '18 22:12 dariusc93

https://doc.rust-lang.org/std/env/fn.set_current_dir.html

sineemore avatar Dec 18 '18 22:12 sineemore

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.

sineemore avatar Dec 18 '18 22:12 sineemore

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 avatar Dec 18 '18 22:12 dariusc93

@dariusc93 Oh, where can I read more about that?

sineemore avatar Dec 18 '18 22:12 sineemore

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).

dariusc93 avatar Dec 18 '18 22:12 dariusc93

https://github.com/kyren/rlua/issues/38#issuecomment-325875248

naturallymitchell avatar Dec 18 '18 23:12 naturallymitchell

get current dir https://doc.rust-lang.org/std/env/fn.current_dir.html

naturallymitchell avatar Dec 22 '18 04:12 naturallymitchell

I have this already in a local branch. Let me rebase and ill push it

dariusc93 avatar Dec 22 '18 04:12 dariusc93

is writeable

naturallymitchell avatar Feb 10 '19 03:02 naturallymitchell

#!/usr/bin/env torchbear

print(cwd)

returns nil

I think that's holding back https://github.com/foundpatterns/torchbear-libs/issues/7

naturallymitchell avatar Feb 11 '19 16:02 naturallymitchell

There is no cwd in torchbear. You can use fs.current_dir() to get the current directory.

dariusc93 avatar Feb 11 '19 16:02 dariusc93

cool. thanks

naturallymitchell avatar Feb 11 '19 16:02 naturallymitchell

reopening for https://github.com/foundpatterns/mp-installer/blob/9ee292956dcec2775068707f2fa8c1e91f4731e5/init.lua#L46-L48

naturallymitchell avatar Feb 25 '19 00:02 naturallymitchell