experimenting with inotify
This commit is contained in:
@@ -17,7 +17,6 @@ pub fn unload(_: *Self, _: std.mem.Allocator) ![]u8 {
|
||||
|
||||
pub fn reload(_: *Self, _: std.mem.Allocator, _: []u8) !void {
|
||||
std.log.debug("!! reload {s} !!", .{@typeName(@This())});
|
||||
std.log.debug("!! NEW CONTENT !!", .{});
|
||||
}
|
||||
|
||||
pub fn shutdown(_: *Self, _: std.mem.Allocator) void {
|
||||
|
100
src/main.zig
100
src/main.zig
@@ -149,13 +149,6 @@ pub fn main() !void {
|
||||
var dir = try std.fs.openDirAbsolute(mods_path, .{ .iterate = true });
|
||||
defer dir.close();
|
||||
|
||||
// const fd = linux.inotify_init1(linux.IN.NONBLOCK);
|
||||
// defer linux.close(fd);
|
||||
// var fds = [_]linux.pollfd{
|
||||
// .{ .fd = fd, .events = linux.IN.ALL_EVENTS, .revents = undefined },
|
||||
// };
|
||||
// const n = linux.poll(&fds, fds.len, 0);
|
||||
|
||||
var it = dir.iterate();
|
||||
while (try it.next()) |entry| {
|
||||
const mod_path = try std.fs.path.resolve(alloc, &.{ mods_path, entry.name });
|
||||
@@ -170,21 +163,90 @@ pub fn main() !void {
|
||||
}
|
||||
}
|
||||
|
||||
std.log.debug("-" ** 80, .{});
|
||||
for (0..10) |x| {
|
||||
std.log.debug("{d}...", .{x});
|
||||
std.time.sleep(std.time.ns_per_s);
|
||||
}
|
||||
std.log.debug("reloading.", .{});
|
||||
std.time.sleep(std.time.ns_per_s);
|
||||
const fd: i32 = blk: {
|
||||
const ret: isize = @bitCast(linux.inotify_init1(linux.IN.NONBLOCK));
|
||||
if (ret < 0) return error.inotify_init_error;
|
||||
break :blk @intCast(ret);
|
||||
};
|
||||
defer _ = linux.close(fd);
|
||||
|
||||
for (mods.items) |*mod| {
|
||||
if (mod.reload(alloc)) |_| {
|
||||
std.log.debug("loaded {s}", .{mod.curr.path});
|
||||
} else |_| {
|
||||
std.log.debug("rolled back to {s}", .{mod.curr.path});
|
||||
const wd: i32 = blk: {
|
||||
const pathz = try alloc.dupeZ(u8, mods_path);
|
||||
defer alloc.free(pathz);
|
||||
const ret: isize = @bitCast(linux.inotify_add_watch(fd, pathz, linux.IN.ALL_EVENTS));
|
||||
if (ret < 0) return error.inotify_add_watch_error;
|
||||
break :blk @intCast(ret);
|
||||
};
|
||||
defer _ = linux.inotify_rm_watch(fd, wd);
|
||||
|
||||
var fds = [_]linux.pollfd{
|
||||
.{ .fd = fd, .events = linux.POLL.IN, .revents = undefined },
|
||||
};
|
||||
|
||||
const eventbuf = try alloc.alloc(u8, 5 * (@sizeOf(linux.inotify_event) + linux.NAME_MAX));
|
||||
defer alloc.free(eventbuf);
|
||||
|
||||
for (0..100) |_| {
|
||||
std.time.sleep(std.time.ns_per_s);
|
||||
|
||||
const n = linux.poll(&fds, fds.len, std.time.ms_per_s);
|
||||
std.log.debug("polled {d}", .{n});
|
||||
|
||||
if (n > 0) {
|
||||
const c = linux.read(fd, eventbuf.ptr, eventbuf.len);
|
||||
|
||||
var i: usize = 0;
|
||||
while (i < c) {
|
||||
const event: *linux.inotify_event = @alignCast(@ptrCast(eventbuf.ptr + i));
|
||||
|
||||
std.log.info("event: {any} {?s}", .{ event, event.getName() });
|
||||
|
||||
const names = .{
|
||||
"ACCESS",
|
||||
"MODIFY",
|
||||
"ATTRIB",
|
||||
"CLOSE_WRITE",
|
||||
"CLOSE_NOWRITE",
|
||||
"OPEN",
|
||||
"MOVED_FROM",
|
||||
"MOVED_TO",
|
||||
"CREATE",
|
||||
"DELETE",
|
||||
"DELETE_SELF",
|
||||
"MOVE_SELF",
|
||||
};
|
||||
|
||||
// Zig builds the lib in a temp file and then moves that into the destination.
|
||||
// Think i need to handle "MOVE_TO" on the inode... but that might not work right because that inode
|
||||
// is not what gets modified.
|
||||
// Probably want a hashmap keyed by each mod's realpath basename.
|
||||
|
||||
inline for (names) |name| {
|
||||
if (@field(linux.IN, name) & event.mask != 0) {
|
||||
std.log.debug(" {s}", .{name});
|
||||
}
|
||||
}
|
||||
|
||||
i += @sizeOf(linux.inotify_event) + event.len;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// std.log.debug("-" ** 80, .{});
|
||||
// for (0..10) |x| {
|
||||
// std.log.debug("{d}...", .{x});
|
||||
// std.time.sleep(std.time.ns_per_s);
|
||||
// }
|
||||
// std.log.debug("reloading.", .{});
|
||||
// std.time.sleep(std.time.ns_per_s);
|
||||
//
|
||||
// for (mods.items) |*mod| {
|
||||
// if (mod.reload(alloc)) |_| {
|
||||
// std.log.debug("loaded {s}", .{mod.curr.path});
|
||||
// } else |_| {
|
||||
// std.log.debug("rolled back to {s}", .{mod.curr.path});
|
||||
// }
|
||||
// }
|
||||
|
||||
std.log.debug("-" ** 80, .{});
|
||||
}
|
||||
|
Reference in New Issue
Block a user