#Error trying to print [:0]const u8

1 messages · Page 1 of 1 (latest)

patent fulcrum
#

Hey! Im working with inotify to check filesystem events. For some reason i want to print the name of the event. I'm calling the 'event.getName()' function and when I try to print it i got ascii chars like if like memory was freeing before printing(?

I don't know if it's a bug with my code or something else.


        while (true) {
            const bytes_read = linux.read(self.fd, &buffer, buffer.len);
            if (bytes_read < 0) {
                log.err("Failed to read from inotify file descriptor: {d}", .{bytes_read});
                return error.ReadFailed;
            }

            var offset: usize = 0;

            while (offset < bytes_read) {
                const event_ptr: *const linux.inotify_event = @ptrCast(@alignCast(&buffer[offset]));
                const event = event_ptr.*;
                // var stat: linux.Stat = undefined;
                // _ = linux.stat("./text-directory", &stat);

                if (event.len > 0) {
                    const name: ?[:0]const u8 = event.getName();

                    if (name) |n| {
                         // Here is when the log is not working.
                        std.debug.print("{s}\n", .{n[0..n.len]});
                    }
                }

                if (self.config.debug_logs) log.info("Event on watch descriptor: {d}, mask: {x}, cookie: {x}, len: {d}\n", .{ event.wd, event.mask, event.cookie, event.len });

                try self.process_event(event);
                offset += @sizeOf(linux.inotify_event) + event.len;
            }
        }

#

im getting somethin like this

sly carbon
#

have you tried n.len-1?

kindred notch
#
const event_ptr: *const linux.inotify_event = @ptrCast(@alignCast(&buffer[offset]));
const event = event_ptr.*;

replace this with:

const event = std.mem.bytesToValue(linux.inotify-event, buffer[offset..]);
plucky lynx
#

the name is a FAM right?

kindred notch
#

your loop also does not check whether there's enough bytes for the event, and when you read again, you'd need proper windowing in case the data from previous loop was incomplete

#

if (comptime Environment.allow_assert) bun.assert(this.name_len > 0);
is wild lol

#

seems like bun doesn't do proper windowing either

patent fulcrum
#

Okay thank you. I understand know. Also, thank you for share the bun file watcher Will take a look. I'm new in theis level stuff and in building a watcher library. Thank you!

patent fulcrum
#

const event = std.mem.bytesToValue(linux.inotify-event, buffer[offset..]);
what this function actually do?

Because im getting the same error, can you explain me a little bit so I dig deep about it and try to find a solution?

kindred notch
#

it's only a safer way to cast from bytes to a value

#

I'm not familiar with inotify api so I can't say where your actual problem is. However if you get valid data at first but corrupt data later, then it may definitely be the lack of handling incomplete data properly in your loop.