Skip to content

Commit

Permalink
Watch.zig: add initial windows implementation.
Browse files Browse the repository at this point in the history
More than 64 directories is not supported yet.
  • Loading branch information
jayrod246 committed Jul 23, 2024
1 parent 82b6f8a commit 5fb3f38
Show file tree
Hide file tree
Showing 3 changed files with 336 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 @@ -397,7 +397,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
327 changes: 326 additions & 1 deletion lib/std/Build/Watch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,304 @@ 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.ArrayHashMapUnmanaged(FileId, ReactionSet, FileId.Adapter, false);
const HandleExtra = struct {
dir: *Directory,
wait_handle: windows.HANDLE,
};

const FileId = struct {
volumeSerialNumber: windows.ULONG,
indexNumber: windows.LARGE_INTEGER,

const Adapter = struct {
pub fn hash(self: Adapter, a: FileId) u32 {
_ = self;
var hasher = Hash.init(0);
std.hash.autoHash(&hasher, a);
return @truncate(hasher.final());
}
pub fn eql(self: Adapter, a: FileId, b: FileId, b_index: usize) bool {
_ = self;
_ = b_index;
return a.volumeSerialNumber == b.volumeSerialNumber and a.indexNumber == b.indexNumber;
}
};
};

const Directory = struct {
handle: windows.HANDLE,
id: FileId,
overlapped: windows.OVERLAPPED,
buffer: [64512]u8 align(@alignOf(windows.FILE_NOTIFY_INFORMATION)) = undefined,

extern "kernel32" fn ReadDirectoryChangesW(
hDirectory: windows.HANDLE,
lpBuffer: [*]align(@alignOf(windows.FILE_NOTIFY_INFORMATION)) u8,
nBufferLength: windows.DWORD,
bWatchSubtree: windows.BOOL,
dwNotifyFilter: windows.DWORD,
lpBytesReturned: ?*windows.DWORD,
lpOverlapped: ?*windows.OVERLAPPED,
lpCompletionRoutine: windows.LPOVERLAPPED_COMPLETION_ROUTINE,
) callconv(windows.WINAPI) windows.BOOL;

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 = ReadDirectoryChangesW(self.handle, @ptrCast(&self.buffer), self.buffer.len, 0, notify_filter, null, &self.overlapped, null);
if (r == 0) {
switch (windows.GetLastError()) {
.INVALID_FUNCTION => return error.ReadDirectoryChangesUnsupported,
else => |err| return windows.unexpectedError(err),
}
}
}

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

fn init(gpa: Allocator, path: Cache.Path) !*@This() {
// The following code is a drawn out NtCreateFile call. (mostly adapted from std.fs.Dir.makeOpenDirAccessMaskW)
// It's necessary in order to get the flags are required when calling ReadDirectoryChangesW.
var dir_handle: windows.HANDLE = undefined;
{
const root_fd = path.root_dir.handle.fd;
const sub_path = path.subPathOrDot();
const sub_path_w = try windows.sliceToPrefixedFileW(root_fd, sub_path);
const path_len_bytes = std.math.cast(u16, sub_path_w.len * 2) orelse return error.NameTooLong;

var nt_name = windows.UNICODE_STRING{
.Length = @intCast(path_len_bytes),
.MaximumLength = @intCast(path_len_bytes),
.Buffer = @constCast(sub_path_w.span().ptr),
};
var attr = windows.OBJECT_ATTRIBUTES{
.Length = @sizeOf(windows.OBJECT_ATTRIBUTES),
.RootDirectory = if (std.fs.path.isAbsoluteWindowsW(sub_path_w.span())) null else root_fd,
.Attributes = 0, // Note we do not use OBJ_CASE_INSENSITIVE here.
.ObjectName = &nt_name,
.SecurityDescriptor = null,
.SecurityQualityOfService = null,
};
var io: windows.IO_STATUS_BLOCK = undefined;
const rc = windows.ntdll.NtCreateFile(
&dir_handle,
windows.SYNCHRONIZE | windows.GENERIC_READ | windows.FILE_LIST_DIRECTORY,
&attr,
&io,
null,
0,
windows.FILE_SHARE_READ | windows.FILE_SHARE_WRITE | windows.FILE_SHARE_DELETE,
windows.FILE_OPEN,
windows.FILE_DIRECTORY_FILE | windows.FILE_OPEN_FOR_BACKUP_INTENT,
null,
0,
);

switch (rc) {
.SUCCESS => {},
.OBJECT_NAME_INVALID => return error.BadPathName,
.OBJECT_NAME_NOT_FOUND => return error.FileNotFound,
.OBJECT_NAME_COLLISION => return error.PathAlreadyExists,
.OBJECT_PATH_NOT_FOUND => return error.FileNotFound,
.NOT_A_DIRECTORY => return error.NotDir,
// This can happen if the directory has 'List folder contents' permission set to 'Deny'
.ACCESS_DENIED => return error.AccessDenied,
.INVALID_PARAMETER => unreachable,
else => return windows.unexpectedStatus(rc),
}
}
assert(dir_handle != windows.INVALID_HANDLE_VALUE);
errdefer windows.CloseHandle(dir_handle);

const dir_id = try getFileId(dir_handle);

const wait_handle = try windows.CreateEventExW(
null,
null,
windows.CREATE_EVENT_MANUAL_RESET,
windows.EVENT_ALL_ACCESS,
);
errdefer windows.CloseHandle(wait_handle);

const dir_ptr = try gpa.create(@This());
dir_ptr.* = .{
.handle = dir_handle,
.id = dir_id,
.overlapped = std.mem.zeroInit(
windows.OVERLAPPED,
.{
.hEvent = wait_handle,
},
),
};
return dir_ptr;
}

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

fn getFileId(handle: windows.HANDLE) !FileId {
var file_id: FileId = undefined;
{
var io_status: windows.IO_STATUS_BLOCK = undefined;
var volume_info: windows.FILE_FS_VOLUME_INFORMATION = undefined;
const rc = windows.ntdll.NtQueryVolumeInformationFile(handle, &io_status, &volume_info, @sizeOf(windows.FILE_FS_VOLUME_INFORMATION), .FileFsVolumeInformation);
switch (rc) {
.SUCCESS => {},
// Buffer overflow here indicates that there is more information available than was able to be stored in the buffer
// size provided. This is treated as success because the type of variable-length information that this would be relevant for
// (name, volume name, etc) we don't care about.
.BUFFER_OVERFLOW => {},
else => return windows.unexpectedStatus(rc),
}
file_id.volumeSerialNumber = volume_info.VolumeSerialNumber;
}
{
var io_status: windows.IO_STATUS_BLOCK = undefined;
var internal_info: windows.FILE_INTERNAL_INFORMATION = undefined;
const rc = windows.ntdll.NtQueryInformationFile(handle, &io_status, &internal_info, @sizeOf(windows.FILE_INTERNAL_INFORMATION), .FileInternalInformation);
switch (rc) {
.SUCCESS => {},
else => return windows.unexpectedStatus(rc),
}
file_id.indexNumber = internal_info.IndexNumber;
}
return file_id;
}

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.id)) |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;
}

try 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) {
const dir = try Os.Directory.init(gpa, path);
errdefer dir.deinit(gpa);
// `dir.id` 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.id);
if (dh_gop.found_existing) {
dir.deinit(gpa);
_ = w.dir_table.pop();
} else {
assert(dh_gop.index == gop.index);
dh_gop.value_ptr.* = .{};
try dir.readChanges();
try w.os.handle_extra.insert(gpa, dh_gop.index, .{
.dir = dir,
.wait_handle = dir.getWaitHandle(),
});
}
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 +568,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 +631,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 +669,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"),
}
}
9 changes: 9 additions & 0 deletions lib/std/os/windows.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3206,6 +3206,15 @@ pub const FILE_FS_DEVICE_INFORMATION = extern struct {
Characteristics: ULONG,
};

pub const FILE_FS_VOLUME_INFORMATION = extern struct {
VolumeCreationTime: LARGE_INTEGER,
VolumeSerialNumber: ULONG,
VolumeLabelLength: ULONG,
SupportsObjects: BOOLEAN,
// Flexible array member
VolumeLabel: [1]WCHAR,
};

pub const FS_INFORMATION_CLASS = enum(c_int) {
FileFsVolumeInformation = 1,
FileFsLabelInformation,
Expand Down

0 comments on commit 5fb3f38

Please sign in to comment.