I'm using inotify on a project, and it requires me to read N inotify_event structures and a dynamic sized null-terminated string from the inotify file descriptor every time there's an event. I'm currently implementing it like this (keeping just the important things):
buffer := make([]u8, 4096)
bytes_read = linux.read(in_fd, buffer)
in_events: [dynamic] linux.Inotify_Event
offset := 0
for offset < bytes_read {
event := (^linux.Inotify_Event)(raw_data(buffer[offset:]))^
name := cstring(raw_data(buffer[offset + size_of(event):][:event.len]))
append(&in_events, event) // Currently ignoring name, but shouldn't (I'll need it later)
offset += size_of(event) + int(event.len)
}
I have a few questions, but the biggest one is: Is there a better way to do this read into a struct? Or maybe just the conversion from []u8 to said struct.
Also, the structure kinda acknowledges that name field, but it's defines it as [0]u8. Can I somehow store the name inside the structure without relying on external storage (maybe just a memory allocation) to pass the name forward (as the comment suggests, I'll need it for later). As in, can I store the string, or a pointer to a string on that [0]u8 field?
I should also clarify that I need to read a few bytes more than just size_of(Inotify_Event). I thought about using mem.ptr_to_bytes(&event) (somebody [already recommended](#beginners message) that to me for another code I was writing), but if I do so, the call to read will throw me an EINVAL error. It needs to read at least sizeof(struct inotify_event) + NAME_MAX + 1 (straight from inotify(7)'s man page). I hard-coded a buffer size of 4096 for now, but I'll get back to that later...