You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We're trying to adopt the lovely ffi-libarchive maintained by @chef, but it's using ffi-inliner to grab a stat struct to pass into one of the methods. I'd like some advice on how to grab a pointer to a stat struct and pass it into a method in an idiomatic way 🙏
libarchive can be used to write new archives. It can copy the stat of a file on a local filesystem. So ffi-libarchive is doing this via some inline C:
I can't help but feel like there must be some way to do File.stat(...) # => a File::Stat and pass that into an FFI function as a pointer. It just needs a way to do TypedData_Get_Struct(self, struct stat, &stat_data_type, st) into an FFI::Pointer.
Is there a good way to do this? MappedType smells useful, but I don't really know how to make it all work.
I also tried grabbing the object_id, casting it to a pointer (object_id << 1), then adding a calculated offset into the RTypedData .. but I can't quite get it to work, and it feels pretty unsafe.
I'd happily build a little C extension that teaches FFI how to turn File::Stat instances into pointers if I could figure out how 😅
The text was updated successfully, but these errors were encountered:
We're trying to adopt the lovely ffi-libarchive maintained by @chef, but it's using ffi-inliner to grab a stat struct to pass into one of the methods. I'd like some advice on how to grab a pointer to a stat struct and pass it into a method in an idiomatic way 🙏
libarchive can be used to write new archives. It can copy the stat of a file on a local filesystem. So ffi-libarchive is doing this via some inline C:
https://github.com/chef/ffi-libarchive/blob/7dd3c6b2c5a5679a4fc8ba4a1194170a6d8f0ff0/lib/ffi-libarchive/stat.rb
https://github.com/chef/ffi-libarchive/blob/7dd3c6b2c5a5679a4fc8ba4a1194170a6d8f0ff0/lib/ffi-libarchive/entry.rb#L202-L216
https://github.com/chef/ffi-libarchive/blob/7dd3c6b2c5a5679a4fc8ba4a1194170a6d8f0ff0/lib/ffi-libarchive/archive.rb#L199
I don't think the inline C would be necessary if we knew the size of the stat struct. The problem is that is platform dependent, etc.
But Ruby already knows the size of the struct, in File::Stat.
https://github.com/ruby/ruby/blob/v3_2_2/file.c#L488
I can't help but feel like there must be some way to do
File.stat(...) # => a File::Stat
and pass that into an FFI function as a pointer. It just needs a way to doTypedData_Get_Struct(self, struct stat, &stat_data_type, st)
into anFFI::Pointer
.Is there a good way to do this? MappedType smells useful, but I don't really know how to make it all work.
I also tried grabbing the object_id, casting it to a pointer (object_id << 1), then adding a calculated offset into the RTypedData .. but I can't quite get it to work, and it feels pretty unsafe.
I'd happily build a little C extension that teaches FFI how to turn File::Stat instances into pointers if I could figure out how 😅
The text was updated successfully, but these errors were encountered: