I was building a simple directory watcher in zig . I have taken some reference from Google gemini . Here is the code
const std = @import("std");
const linux = std.os.linux;
pub fn main() !void {
const fd_usize = linux.inotify_init1(0);
const fd: i32 = @intCast(fd_usize);
defer std.posix.close(fd);
// var buf: [4096]u8 align(@alignOf(linux.inotify_event)) = undefined;
var buf: [4096]u8 = undefined;
if (fd < 0) {
std.debug.print("inotify init", .{});
std.process.exit(1);
}
_ = linux.inotify_add_watch(fd, ".", linux.IN.MODIFY | linux.IN.DELETE | linux.IN.CREATE | linux.IN.MOVED_TO);
std.log.info("Watching current dir for changes", .{});
while (true) {
const length = try std.posix.read(fd, &buf);
var i: usize = 0;
while (i < length) {
const event = @as(*linux.inotify_event, @ptrCast(@alignCast(&buf[i])));
// Check if the event has a filename associated with it
if (event.len > 0) {
const name = event.getName() orelse "genaric";
if (event.mask & linux.IN.CREATE != 0) {
std.debug.print("File Created: {s}\n", .{name});
} else if (event.mask & linux.IN.MODIFY != 0) {
std.debug.print("File Modified: {s}\n", .{name});
} else if (event.mask & linux.IN.DELETE != 0) {
std.debug.print("File Deleted: {s}\n", .{name});
}
}
// Move to the next event in the buffer
i = i + @sizeOf(linux.inotify_event) + event.len;
}
}
}
Here when declaring a buffer for the file descriptor Gemini uses the
// var buf: [4096]u8 align(@alignOf(linux.inotify_event)) = undefined;
var buf: [4096]u8 = undefined;
upper line . The program compiles with both version . What is the use of this align ?