Skip to content

Commit

Permalink
Watch.zig: add windows implementation.
Browse files Browse the repository at this point in the history
This does not support more than 64 directories yet.
  • Loading branch information
jayrod246 committed Jul 17, 2024
1 parent d375a20 commit 5524342
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/compiler/build_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ pub fn main() !void {
if (!watch) return cleanExit();

switch (builtin.os.tag) {
.linux => {},
.linux, .windows => {},
else => fatal("--watch not yet implemented for {s}", .{@tagName(builtin.os.tag)}),
}

Expand Down
217 changes: 216 additions & 1 deletion lib/std/Build/Watch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,194 @@ const Os = switch (builtin.os.tag) {
}
}
},
.windows => struct {
const posix = std.posix;
const windows = std.os.windows;

/// Keyed differently but indexes correspond 1:1 with `dir_table`.
handle_table: HandleTable,
handle_extra: std.MultiArrayList(HandleExtra),

const HandleTable = std.AutoArrayHashMapUnmanaged(windows.HANDLE, ReactionSet);
const HandleExtra = struct {
dir: *Directory,
wait_handle: windows.HANDLE,
};
const Directory = struct {
handle: windows.HANDLE,
overlapped: windows.OVERLAPPED,
buffer: [64512]u8 align(@alignOf(windows.FILE_NOTIFY_INFORMATION)) = undefined,

fn readChanges(self: *@This()) void {
const notify_filter =
windows.FILE_NOTIFY_CHANGE_CREATION |
windows.FILE_NOTIFY_CHANGE_DIR_NAME |
windows.FILE_NOTIFY_CHANGE_FILE_NAME |
windows.FILE_NOTIFY_CHANGE_LAST_WRITE |
windows.FILE_NOTIFY_CHANGE_SIZE;
const r = windows.kernel32.ReadDirectoryChangesW(self.handle, @ptrCast(&self.buffer), self.buffer.len, 0, notify_filter, null, &self.overlapped, null);
assert(r != 0);
}

fn getWaitHandle(self: @This()) windows.HANDLE {
return self.overlapped.hEvent.?;
}

fn init(gpa: Allocator, handle: windows.HANDLE) !*@This() {
const event = try windows.CreateEventExW(
null,
null,
windows.CREATE_EVENT_MANUAL_RESET,
windows.EVENT_ALL_ACCESS,
);

const result = try gpa.create(@This());
result.* = .{
.handle = handle,
.overlapped = std.mem.zeroInit(
windows.OVERLAPPED,
.{
.hEvent = event,
},
),
};
return result;
}

fn deinit(self: *@This(), gpa: Allocator) void {
_ = windows.kernel32.CancelIo(self.handle);
windows.CloseHandle(self.getWaitHandle());
windows.CloseHandle(self.handle);
gpa.destroy(self);
}
};

fn markDirtySteps(w: *Watch, gpa: Allocator, dir: *Directory) !bool {
var any_dirty = false;
const bytes_returned = try windows.GetOverlappedResult(dir.handle, &dir.overlapped, false);
if (bytes_returned == 0) {
std.log.warn("file system watch queue overflowed; falling back to fstat", .{});
markAllFilesDirty(w, gpa);
return true;
}
var file_name_buf: [std.fs.max_path_bytes]u8 = undefined;
var notify: *align(1) windows.FILE_NOTIFY_INFORMATION = undefined;
var offset: usize = 0;
while (true) {
notify = @ptrCast(&dir.buffer[offset]);
const file_name_field: [*]u16 = @ptrFromInt(@intFromPtr(notify) + @sizeOf(windows.FILE_NOTIFY_INFORMATION));
const file_name_len = std.unicode.wtf16LeToWtf8(&file_name_buf, file_name_field[0 .. notify.FileNameLength / 2]);
const file_name = file_name_buf[0..file_name_len];
if (w.os.handle_table.getIndex(dir.handle)) |reaction_set_i| {
const reaction_set = w.os.handle_table.values()[reaction_set_i];
if (reaction_set.getPtr(".")) |glob_set|
any_dirty = markStepSetDirty(gpa, glob_set, any_dirty);
if (reaction_set.getPtr(file_name)) |step_set| {
any_dirty = markStepSetDirty(gpa, step_set, any_dirty);
}
}
if (notify.NextEntryOffset == 0)
break;

offset += notify.NextEntryOffset;
}

dir.readChanges();
return any_dirty;
}

fn update(w: *Watch, gpa: Allocator, steps: []const *Step) !void {
// Add missing marks and note persisted ones.
for (steps) |step| {
for (step.inputs.table.keys(), step.inputs.table.values()) |path, *files| {
const reaction_set = rs: {
const gop = try w.dir_table.getOrPut(gpa, path);
if (!gop.found_existing) {
var realpath_buf: [std.fs.max_path_bytes]u8 = undefined;
const realpath = try path.root_dir.handle.realpath(path.sub_path, &realpath_buf);
const realpath_w = try windows.sliceToPrefixedFileW(null, realpath);
const dir_handle = windows.kernel32.CreateFileW(
realpath_w.span().ptr,
windows.GENERIC_READ,
windows.FILE_SHARE_DELETE | windows.FILE_SHARE_READ | windows.FILE_SHARE_WRITE,
null,
windows.OPEN_EXISTING,
windows.FILE_FLAG_BACKUP_SEMANTICS | windows.FILE_FLAG_OVERLAPPED,
null,
);

assert(dir_handle != windows.INVALID_HANDLE_VALUE);

// `dir_handle` may already be present in the table in
// the case that we have multiple Cache.Path instances
// that compare inequal but ultimately point to the same
// directory on the file system.
// In such case, we must revert adding this directory, but keep
// the additions to the step set.
const dh_gop = try w.os.handle_table.getOrPut(gpa, dir_handle);
if (dh_gop.found_existing) {
_ = w.dir_table.pop();
} else {
assert(dh_gop.index == gop.index);
dh_gop.value_ptr.* = .{};
const dir = try Os.Directory.init(gpa, dir_handle);
try w.os.handle_extra.insert(gpa, dh_gop.index, .{
.dir = dir,
.wait_handle = dir.getWaitHandle(),
});
dir.readChanges();
}
break :rs &w.os.handle_table.values()[dh_gop.index];
}
break :rs &w.os.handle_table.values()[gop.index];
};
for (files.items) |basename| {
const gop = try reaction_set.getOrPut(gpa, basename);
if (!gop.found_existing) gop.value_ptr.* = .{};
try gop.value_ptr.put(gpa, step, w.generation);
}
}
}

{
// Remove marks for files that are no longer inputs.
var i: usize = 0;
while (i < w.os.handle_table.entries.len) {
{
const reaction_set = &w.os.handle_table.values()[i];
var step_set_i: usize = 0;
while (step_set_i < reaction_set.entries.len) {
const step_set = &reaction_set.values()[step_set_i];
var dirent_i: usize = 0;
while (dirent_i < step_set.entries.len) {
const generations = step_set.values();
if (generations[dirent_i] == w.generation) {
dirent_i += 1;
continue;
}
step_set.swapRemoveAt(dirent_i);
}
if (step_set.entries.len > 0) {
step_set_i += 1;
continue;
}
reaction_set.swapRemoveAt(step_set_i);
}
if (reaction_set.entries.len > 0) {
i += 1;
continue;
}
}

w.os.handle_extra.items(.dir)[i].deinit(gpa);
w.os.handle_extra.swapRemove(i);
w.dir_table.swapRemoveAt(i);
w.os.handle_table.swapRemoveAt(i);
}
w.generation +%= 1;
}
}
},
else => void,
};

Expand Down Expand Up @@ -270,6 +458,19 @@ pub fn init() !Watch {
.generation = 0,
};
},
.windows => {
return .{
.dir_table = .{},
.os = switch (builtin.os.tag) {
.windows => .{
.handle_table = .{},
.handle_extra = .{},
},
else => {},
},
.generation = 0,
};
},
else => @panic("unimplemented"),
}
}
Expand Down Expand Up @@ -320,7 +521,7 @@ fn markStepSetDirty(gpa: Allocator, step_set: *StepSet, any_dirty: bool) bool {

pub fn update(w: *Watch, gpa: Allocator, steps: []const *Step) !void {
switch (builtin.os.tag) {
.linux => return Os.update(w, gpa, steps),
.linux, .windows => return Os.update(w, gpa, steps),
else => @compileError("unimplemented"),
}
}
Expand Down Expand Up @@ -358,6 +559,20 @@ pub fn wait(w: *Watch, gpa: Allocator, timeout: Timeout) !WaitResult {
else
.clean;
},
.windows => {
const handles = w.os.handle_extra.items(.wait_handle);
if (handles.len > std.os.windows.MAXIMUM_WAIT_OBJECTS) {
@panic("todo: implement WaitForMultipleObjects > 64");
}
const wr = std.os.windows.WaitForMultipleObjectsEx(handles, false, @bitCast(timeout.to_i32_ms()), false) catch |err| switch (err) {
error.WaitTimeOut => return .timeout,
else => return err,
};
return if (try Os.markDirtySteps(w, gpa, w.os.handle_extra.items(.dir)[wr]))
.dirty
else
.clean;
},
else => @compileError("unimplemented"),
}
}

0 comments on commit 5524342

Please sign in to comment.