Crystal bindings to XATTR new shard

Hey,

Be gentle with me! I’ve been lurking Crystal for long and finally had an occasion to create a tiny library to manage XATTR metadata.
You can find it here: https://github.com/ettomatic/xattr

Possibly not the most idiomatic Crystal and some more work is necessary, but it’s my first real attempt and I enjoyed it *a lot!
I’m now using it as part of a personal project to manage metadata on my KDE box :slight_smile:

Feedback more than welcome!

3 Likes

Looks good!

Stylistically I would just reopen the stdlib LibC or define the lib inside the shard’s namespace, so module XAttr; lib LibXAttr. Generally we prefix lib types with Lib :) A toplevel C type seems a bit invasive :)

For char* definitions we type them generally as LibC::Char* in bindings.

It looks like at least setxattr (didn’t check others) is quite platform specific. On macOS I see:

 int setxattr(const char *path, const char *name, void *value, size_t size, u_int32_t position, int options);

and on Linux

int setxattr(const char *path, const char *name, const void *value, size_t size, int flags);

Neither seems to quite match what you have. You can have different definitions per target with {% if flag?(:linux) %} style macro conditions. It’s important to get this right and complete, including exact argument count types, otherwise you’ll easily corrupt the stack frame when calling the function.

As a final tip, checkout https://crystal-lang.org/api/0.35.0/SystemError/ClassMethods.html#from_errno(message:String?=nil,errno:Errno=Errno.value,**opts)-instance-method included into many stdlib exception types.

5 Likes