Merge branch 'windows-evented-io' of https://github.com/FireFox317/zig into FireFox317-windows-evented-io
commit
988031c07c
|
@ -666,158 +666,160 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) anyerror!DebugInfo {
|
||||||
|
|
||||||
/// TODO resources https://github.com/ziglang/zig/issues/4353
|
/// TODO resources https://github.com/ziglang/zig/issues/4353
|
||||||
fn openCoffDebugInfo(allocator: *mem.Allocator, coff_file_path: [:0]const u16) !ModuleDebugInfo {
|
fn openCoffDebugInfo(allocator: *mem.Allocator, coff_file_path: [:0]const u16) !ModuleDebugInfo {
|
||||||
const coff_file = try std.fs.openFileAbsoluteW(coff_file_path.ptr, .{});
|
noasync {
|
||||||
errdefer coff_file.close();
|
const coff_file = try std.fs.openFileAbsoluteW(coff_file_path.ptr, .{});
|
||||||
|
errdefer coff_file.close();
|
||||||
|
|
||||||
const coff_obj = try allocator.create(coff.Coff);
|
const coff_obj = try allocator.create(coff.Coff);
|
||||||
coff_obj.* = coff.Coff.init(allocator, coff_file);
|
coff_obj.* = coff.Coff.init(allocator, coff_file);
|
||||||
|
|
||||||
var di = ModuleDebugInfo{
|
var di = ModuleDebugInfo{
|
||||||
.base_address = undefined,
|
.base_address = undefined,
|
||||||
.coff = coff_obj,
|
.coff = coff_obj,
|
||||||
.pdb = undefined,
|
.pdb = undefined,
|
||||||
.sect_contribs = undefined,
|
.sect_contribs = undefined,
|
||||||
.modules = undefined,
|
.modules = undefined,
|
||||||
};
|
|
||||||
|
|
||||||
try di.coff.loadHeader();
|
|
||||||
|
|
||||||
var path_buf: [windows.MAX_PATH]u8 = undefined;
|
|
||||||
const len = try di.coff.getPdbPath(path_buf[0..]);
|
|
||||||
const raw_path = path_buf[0..len];
|
|
||||||
|
|
||||||
const path = try fs.path.resolve(allocator, &[_][]const u8{raw_path});
|
|
||||||
|
|
||||||
try di.pdb.openFile(di.coff, path);
|
|
||||||
|
|
||||||
var pdb_stream = di.pdb.getStream(pdb.StreamType.Pdb) orelse return error.InvalidDebugInfo;
|
|
||||||
const version = try pdb_stream.inStream().readIntLittle(u32);
|
|
||||||
const signature = try pdb_stream.inStream().readIntLittle(u32);
|
|
||||||
const age = try pdb_stream.inStream().readIntLittle(u32);
|
|
||||||
var guid: [16]u8 = undefined;
|
|
||||||
try pdb_stream.inStream().readNoEof(&guid);
|
|
||||||
if (version != 20000404) // VC70, only value observed by LLVM team
|
|
||||||
return error.UnknownPDBVersion;
|
|
||||||
if (!mem.eql(u8, &di.coff.guid, &guid) or di.coff.age != age)
|
|
||||||
return error.PDBMismatch;
|
|
||||||
// We validated the executable and pdb match.
|
|
||||||
|
|
||||||
const string_table_index = str_tab_index: {
|
|
||||||
const name_bytes_len = try pdb_stream.inStream().readIntLittle(u32);
|
|
||||||
const name_bytes = try allocator.alloc(u8, name_bytes_len);
|
|
||||||
try pdb_stream.inStream().readNoEof(name_bytes);
|
|
||||||
|
|
||||||
const HashTableHeader = packed struct {
|
|
||||||
Size: u32,
|
|
||||||
Capacity: u32,
|
|
||||||
|
|
||||||
fn maxLoad(cap: u32) u32 {
|
|
||||||
return cap * 2 / 3 + 1;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
const hash_tbl_hdr = try pdb_stream.inStream().readStruct(HashTableHeader);
|
|
||||||
if (hash_tbl_hdr.Capacity == 0)
|
|
||||||
return error.InvalidDebugInfo;
|
|
||||||
|
|
||||||
if (hash_tbl_hdr.Size > HashTableHeader.maxLoad(hash_tbl_hdr.Capacity))
|
try di.coff.loadHeader();
|
||||||
return error.InvalidDebugInfo;
|
|
||||||
|
|
||||||
const present = try readSparseBitVector(&pdb_stream.inStream(), allocator);
|
var path_buf: [windows.MAX_PATH]u8 = undefined;
|
||||||
if (present.len != hash_tbl_hdr.Size)
|
const len = try di.coff.getPdbPath(path_buf[0..]);
|
||||||
return error.InvalidDebugInfo;
|
const raw_path = path_buf[0..len];
|
||||||
const deleted = try readSparseBitVector(&pdb_stream.inStream(), allocator);
|
|
||||||
|
|
||||||
const Bucket = struct {
|
const path = try fs.path.resolve(allocator, &[_][]const u8{raw_path});
|
||||||
first: u32,
|
|
||||||
second: u32,
|
try di.pdb.openFile(di.coff, path);
|
||||||
};
|
|
||||||
const bucket_list = try allocator.alloc(Bucket, present.len);
|
var pdb_stream = di.pdb.getStream(pdb.StreamType.Pdb) orelse return error.InvalidDebugInfo;
|
||||||
for (present) |_| {
|
const version = try pdb_stream.inStream().readIntLittle(u32);
|
||||||
const name_offset = try pdb_stream.inStream().readIntLittle(u32);
|
const signature = try pdb_stream.inStream().readIntLittle(u32);
|
||||||
const name_index = try pdb_stream.inStream().readIntLittle(u32);
|
const age = try pdb_stream.inStream().readIntLittle(u32);
|
||||||
const name = mem.spanZ(@ptrCast([*:0]u8, name_bytes.ptr + name_offset));
|
var guid: [16]u8 = undefined;
|
||||||
if (mem.eql(u8, name, "/names")) {
|
try pdb_stream.inStream().readNoEof(&guid);
|
||||||
break :str_tab_index name_index;
|
if (version != 20000404) // VC70, only value observed by LLVM team
|
||||||
|
return error.UnknownPDBVersion;
|
||||||
|
if (!mem.eql(u8, &di.coff.guid, &guid) or di.coff.age != age)
|
||||||
|
return error.PDBMismatch;
|
||||||
|
// We validated the executable and pdb match.
|
||||||
|
|
||||||
|
const string_table_index = str_tab_index: {
|
||||||
|
const name_bytes_len = try pdb_stream.inStream().readIntLittle(u32);
|
||||||
|
const name_bytes = try allocator.alloc(u8, name_bytes_len);
|
||||||
|
try pdb_stream.inStream().readNoEof(name_bytes);
|
||||||
|
|
||||||
|
const HashTableHeader = packed struct {
|
||||||
|
Size: u32,
|
||||||
|
Capacity: u32,
|
||||||
|
|
||||||
|
fn maxLoad(cap: u32) u32 {
|
||||||
|
return cap * 2 / 3 + 1;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const hash_tbl_hdr = try pdb_stream.inStream().readStruct(HashTableHeader);
|
||||||
|
if (hash_tbl_hdr.Capacity == 0)
|
||||||
|
return error.InvalidDebugInfo;
|
||||||
|
|
||||||
|
if (hash_tbl_hdr.Size > HashTableHeader.maxLoad(hash_tbl_hdr.Capacity))
|
||||||
|
return error.InvalidDebugInfo;
|
||||||
|
|
||||||
|
const present = try readSparseBitVector(&pdb_stream.inStream(), allocator);
|
||||||
|
if (present.len != hash_tbl_hdr.Size)
|
||||||
|
return error.InvalidDebugInfo;
|
||||||
|
const deleted = try readSparseBitVector(&pdb_stream.inStream(), allocator);
|
||||||
|
|
||||||
|
const Bucket = struct {
|
||||||
|
first: u32,
|
||||||
|
second: u32,
|
||||||
|
};
|
||||||
|
const bucket_list = try allocator.alloc(Bucket, present.len);
|
||||||
|
for (present) |_| {
|
||||||
|
const name_offset = try pdb_stream.inStream().readIntLittle(u32);
|
||||||
|
const name_index = try pdb_stream.inStream().readIntLittle(u32);
|
||||||
|
const name = mem.spanZ(@ptrCast([*:0]u8, name_bytes.ptr + name_offset));
|
||||||
|
if (mem.eql(u8, name, "/names")) {
|
||||||
|
break :str_tab_index name_index;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
return error.MissingDebugInfo;
|
||||||
return error.MissingDebugInfo;
|
};
|
||||||
};
|
|
||||||
|
|
||||||
di.pdb.string_table = di.pdb.getStreamById(string_table_index) orelse return error.MissingDebugInfo;
|
di.pdb.string_table = di.pdb.getStreamById(string_table_index) orelse return error.MissingDebugInfo;
|
||||||
di.pdb.dbi = di.pdb.getStream(pdb.StreamType.Dbi) orelse return error.MissingDebugInfo;
|
di.pdb.dbi = di.pdb.getStream(pdb.StreamType.Dbi) orelse return error.MissingDebugInfo;
|
||||||
|
|
||||||
const dbi = di.pdb.dbi;
|
const dbi = di.pdb.dbi;
|
||||||
|
|
||||||
// Dbi Header
|
// Dbi Header
|
||||||
const dbi_stream_header = try dbi.inStream().readStruct(pdb.DbiStreamHeader);
|
const dbi_stream_header = try dbi.inStream().readStruct(pdb.DbiStreamHeader);
|
||||||
if (dbi_stream_header.VersionHeader != 19990903) // V70, only value observed by LLVM team
|
if (dbi_stream_header.VersionHeader != 19990903) // V70, only value observed by LLVM team
|
||||||
return error.UnknownPDBVersion;
|
return error.UnknownPDBVersion;
|
||||||
if (dbi_stream_header.Age != age)
|
if (dbi_stream_header.Age != age)
|
||||||
return error.UnmatchingPDB;
|
return error.UnmatchingPDB;
|
||||||
|
|
||||||
const mod_info_size = dbi_stream_header.ModInfoSize;
|
const mod_info_size = dbi_stream_header.ModInfoSize;
|
||||||
const section_contrib_size = dbi_stream_header.SectionContributionSize;
|
const section_contrib_size = dbi_stream_header.SectionContributionSize;
|
||||||
|
|
||||||
var modules = ArrayList(Module).init(allocator);
|
var modules = ArrayList(Module).init(allocator);
|
||||||
|
|
||||||
// Module Info Substream
|
// Module Info Substream
|
||||||
var mod_info_offset: usize = 0;
|
var mod_info_offset: usize = 0;
|
||||||
while (mod_info_offset != mod_info_size) {
|
while (mod_info_offset != mod_info_size) {
|
||||||
const mod_info = try dbi.inStream().readStruct(pdb.ModInfo);
|
const mod_info = try dbi.inStream().readStruct(pdb.ModInfo);
|
||||||
var this_record_len: usize = @sizeOf(pdb.ModInfo);
|
var this_record_len: usize = @sizeOf(pdb.ModInfo);
|
||||||
|
|
||||||
const module_name = try dbi.readNullTermString(allocator);
|
const module_name = try dbi.readNullTermString(allocator);
|
||||||
this_record_len += module_name.len + 1;
|
this_record_len += module_name.len + 1;
|
||||||
|
|
||||||
const obj_file_name = try dbi.readNullTermString(allocator);
|
const obj_file_name = try dbi.readNullTermString(allocator);
|
||||||
this_record_len += obj_file_name.len + 1;
|
this_record_len += obj_file_name.len + 1;
|
||||||
|
|
||||||
if (this_record_len % 4 != 0) {
|
if (this_record_len % 4 != 0) {
|
||||||
const round_to_next_4 = (this_record_len | 0x3) + 1;
|
const round_to_next_4 = (this_record_len | 0x3) + 1;
|
||||||
const march_forward_bytes = round_to_next_4 - this_record_len;
|
const march_forward_bytes = round_to_next_4 - this_record_len;
|
||||||
try dbi.seekBy(@intCast(isize, march_forward_bytes));
|
try dbi.seekBy(@intCast(isize, march_forward_bytes));
|
||||||
this_record_len += march_forward_bytes;
|
this_record_len += march_forward_bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
try modules.append(Module{
|
||||||
|
.mod_info = mod_info,
|
||||||
|
.module_name = module_name,
|
||||||
|
.obj_file_name = obj_file_name,
|
||||||
|
|
||||||
|
.populated = false,
|
||||||
|
.symbols = undefined,
|
||||||
|
.subsect_info = undefined,
|
||||||
|
.checksum_offset = null,
|
||||||
|
});
|
||||||
|
|
||||||
|
mod_info_offset += this_record_len;
|
||||||
|
if (mod_info_offset > mod_info_size)
|
||||||
|
return error.InvalidDebugInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
try modules.append(Module{
|
di.modules = modules.toOwnedSlice();
|
||||||
.mod_info = mod_info,
|
|
||||||
.module_name = module_name,
|
|
||||||
.obj_file_name = obj_file_name,
|
|
||||||
|
|
||||||
.populated = false,
|
// Section Contribution Substream
|
||||||
.symbols = undefined,
|
var sect_contribs = ArrayList(pdb.SectionContribEntry).init(allocator);
|
||||||
.subsect_info = undefined,
|
var sect_cont_offset: usize = 0;
|
||||||
.checksum_offset = null,
|
if (section_contrib_size != 0) {
|
||||||
});
|
const ver = @intToEnum(pdb.SectionContrSubstreamVersion, try dbi.inStream().readIntLittle(u32));
|
||||||
|
if (ver != pdb.SectionContrSubstreamVersion.Ver60)
|
||||||
|
return error.InvalidDebugInfo;
|
||||||
|
sect_cont_offset += @sizeOf(u32);
|
||||||
|
}
|
||||||
|
while (sect_cont_offset != section_contrib_size) {
|
||||||
|
const entry = try sect_contribs.addOne();
|
||||||
|
entry.* = try dbi.inStream().readStruct(pdb.SectionContribEntry);
|
||||||
|
sect_cont_offset += @sizeOf(pdb.SectionContribEntry);
|
||||||
|
|
||||||
mod_info_offset += this_record_len;
|
if (sect_cont_offset > section_contrib_size)
|
||||||
if (mod_info_offset > mod_info_size)
|
return error.InvalidDebugInfo;
|
||||||
return error.InvalidDebugInfo;
|
}
|
||||||
|
|
||||||
|
di.sect_contribs = sect_contribs.toOwnedSlice();
|
||||||
|
|
||||||
|
return di;
|
||||||
}
|
}
|
||||||
|
|
||||||
di.modules = modules.toOwnedSlice();
|
|
||||||
|
|
||||||
// Section Contribution Substream
|
|
||||||
var sect_contribs = ArrayList(pdb.SectionContribEntry).init(allocator);
|
|
||||||
var sect_cont_offset: usize = 0;
|
|
||||||
if (section_contrib_size != 0) {
|
|
||||||
const ver = @intToEnum(pdb.SectionContrSubstreamVersion, try dbi.inStream().readIntLittle(u32));
|
|
||||||
if (ver != pdb.SectionContrSubstreamVersion.Ver60)
|
|
||||||
return error.InvalidDebugInfo;
|
|
||||||
sect_cont_offset += @sizeOf(u32);
|
|
||||||
}
|
|
||||||
while (sect_cont_offset != section_contrib_size) {
|
|
||||||
const entry = try sect_contribs.addOne();
|
|
||||||
entry.* = try dbi.inStream().readStruct(pdb.SectionContribEntry);
|
|
||||||
sect_cont_offset += @sizeOf(pdb.SectionContribEntry);
|
|
||||||
|
|
||||||
if (sect_cont_offset > section_contrib_size)
|
|
||||||
return error.InvalidDebugInfo;
|
|
||||||
}
|
|
||||||
|
|
||||||
di.sect_contribs = sect_contribs.toOwnedSlice();
|
|
||||||
|
|
||||||
return di;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn readSparseBitVector(stream: var, allocator: *mem.Allocator) ![]usize {
|
fn readSparseBitVector(stream: var, allocator: *mem.Allocator) ![]usize {
|
||||||
|
@ -1410,59 +1412,61 @@ pub const ModuleDebugInfo = switch (builtin.os.tag) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getSymbolAtAddress(self: *@This(), address: usize) !SymbolInfo {
|
fn getSymbolAtAddress(self: *@This(), address: usize) !SymbolInfo {
|
||||||
// Translate the VA into an address into this object
|
noasync {
|
||||||
const relocated_address = address - self.base_address;
|
// Translate the VA into an address into this object
|
||||||
assert(relocated_address >= 0x100000000);
|
const relocated_address = address - self.base_address;
|
||||||
|
assert(relocated_address >= 0x100000000);
|
||||||
|
|
||||||
// Find the .o file where this symbol is defined
|
// Find the .o file where this symbol is defined
|
||||||
const symbol = machoSearchSymbols(self.symbols, relocated_address) orelse
|
const symbol = machoSearchSymbols(self.symbols, relocated_address) orelse
|
||||||
return SymbolInfo{};
|
return SymbolInfo{};
|
||||||
|
|
||||||
// Take the symbol name from the N_FUN STAB entry, we're going to
|
// Take the symbol name from the N_FUN STAB entry, we're going to
|
||||||
// use it if we fail to find the DWARF infos
|
// use it if we fail to find the DWARF infos
|
||||||
const stab_symbol = mem.spanZ(self.strings[symbol.nlist.n_strx..]);
|
const stab_symbol = mem.spanZ(self.strings[symbol.nlist.n_strx..]);
|
||||||
|
|
||||||
if (symbol.ofile == null)
|
if (symbol.ofile == null)
|
||||||
return SymbolInfo{ .symbol_name = stab_symbol };
|
|
||||||
|
|
||||||
const o_file_path = mem.spanZ(self.strings[symbol.ofile.?.n_strx..]);
|
|
||||||
|
|
||||||
// Check if its debug infos are already in the cache
|
|
||||||
var o_file_di = self.ofiles.getValue(o_file_path) orelse
|
|
||||||
(self.loadOFile(o_file_path) catch |err| switch (err) {
|
|
||||||
error.FileNotFound,
|
|
||||||
error.MissingDebugInfo,
|
|
||||||
error.InvalidDebugInfo,
|
|
||||||
=> {
|
|
||||||
return SymbolInfo{ .symbol_name = stab_symbol };
|
return SymbolInfo{ .symbol_name = stab_symbol };
|
||||||
},
|
|
||||||
else => return err,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Translate again the address, this time into an address inside the
|
const o_file_path = mem.spanZ(self.strings[symbol.ofile.?.n_strx..]);
|
||||||
// .o file
|
|
||||||
const relocated_address_o = relocated_address - symbol.reloc;
|
|
||||||
|
|
||||||
if (o_file_di.findCompileUnit(relocated_address_o)) |compile_unit| {
|
// Check if its debug infos are already in the cache
|
||||||
return SymbolInfo{
|
var o_file_di = self.ofiles.getValue(o_file_path) orelse
|
||||||
.symbol_name = o_file_di.getSymbolName(relocated_address_o) orelse "???",
|
(self.loadOFile(o_file_path) catch |err| switch (err) {
|
||||||
.compile_unit_name = compile_unit.die.getAttrString(&o_file_di, DW.AT_name) catch |err| switch (err) {
|
error.FileNotFound,
|
||||||
error.MissingDebugInfo, error.InvalidDebugInfo => "???",
|
error.MissingDebugInfo,
|
||||||
else => return err,
|
error.InvalidDebugInfo,
|
||||||
|
=> {
|
||||||
|
return SymbolInfo{ .symbol_name = stab_symbol };
|
||||||
},
|
},
|
||||||
.line_info = o_file_di.getLineNumberInfo(compile_unit.*, relocated_address_o) catch |err| switch (err) {
|
else => return err,
|
||||||
error.MissingDebugInfo, error.InvalidDebugInfo => null,
|
});
|
||||||
else => return err,
|
|
||||||
|
// Translate again the address, this time into an address inside the
|
||||||
|
// .o file
|
||||||
|
const relocated_address_o = relocated_address - symbol.reloc;
|
||||||
|
|
||||||
|
if (o_file_di.findCompileUnit(relocated_address_o)) |compile_unit| {
|
||||||
|
return SymbolInfo{
|
||||||
|
.symbol_name = o_file_di.getSymbolName(relocated_address_o) orelse "???",
|
||||||
|
.compile_unit_name = compile_unit.die.getAttrString(&o_file_di, DW.AT_name) catch |err| switch (err) {
|
||||||
|
error.MissingDebugInfo, error.InvalidDebugInfo => "???",
|
||||||
|
else => return err,
|
||||||
|
},
|
||||||
|
.line_info = o_file_di.getLineNumberInfo(compile_unit.*, relocated_address_o) catch |err| switch (err) {
|
||||||
|
error.MissingDebugInfo, error.InvalidDebugInfo => null,
|
||||||
|
else => return err,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} else |err| switch (err) {
|
||||||
|
error.MissingDebugInfo, error.InvalidDebugInfo => {
|
||||||
|
return SymbolInfo{ .symbol_name = stab_symbol };
|
||||||
},
|
},
|
||||||
};
|
else => return err,
|
||||||
} else |err| switch (err) {
|
}
|
||||||
error.MissingDebugInfo, error.InvalidDebugInfo => {
|
|
||||||
return SymbolInfo{ .symbol_name = stab_symbol };
|
unreachable;
|
||||||
},
|
|
||||||
else => return err,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unreachable;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
.uefi, .windows => struct {
|
.uefi, .windows => struct {
|
||||||
|
|
|
@ -639,21 +639,28 @@ pub const Dir = struct {
|
||||||
|
|
||||||
/// Same as `openFile` but Windows-only and the path parameter is
|
/// Same as `openFile` but Windows-only and the path parameter is
|
||||||
/// [WTF-16](https://simonsapin.github.io/wtf-8/#potentially-ill-formed-utf-16) encoded.
|
/// [WTF-16](https://simonsapin.github.io/wtf-8/#potentially-ill-formed-utf-16) encoded.
|
||||||
pub fn openFileW(self: Dir, sub_path_w: [*:0]const u16, flags: File.OpenFlags) File.OpenError!File {
|
pub fn openFileW(self: Dir, sub_path_w: []const u16, flags: File.OpenFlags) File.OpenError!File {
|
||||||
const w = os.windows;
|
const w = os.windows;
|
||||||
const access_mask = w.SYNCHRONIZE |
|
|
||||||
(if (flags.read) @as(u32, w.GENERIC_READ) else 0) |
|
|
||||||
(if (flags.write) @as(u32, w.GENERIC_WRITE) else 0);
|
|
||||||
|
|
||||||
const share_access = switch (flags.lock) {
|
|
||||||
.None => @as(?w.ULONG, null),
|
|
||||||
.Shared => w.FILE_SHARE_READ | w.FILE_SHARE_DELETE,
|
|
||||||
.Exclusive => w.FILE_SHARE_DELETE,
|
|
||||||
};
|
|
||||||
|
|
||||||
return @as(File, .{
|
return @as(File, .{
|
||||||
.handle = try os.windows.OpenFileW(self.fd, sub_path_w, null, access_mask, share_access, flags.lock_nonblocking, w.FILE_OPEN),
|
.handle = try os.windows.OpenFile(sub_path_w, .{
|
||||||
|
.dir = self.fd,
|
||||||
|
.access_mask = w.SYNCHRONIZE |
|
||||||
|
(if (flags.read) @as(u32, w.GENERIC_READ) else 0) |
|
||||||
|
(if (flags.write) @as(u32, w.GENERIC_WRITE) else 0),
|
||||||
|
.share_access = switch (flags.lock) {
|
||||||
|
.None => @as(?w.ULONG, null),
|
||||||
|
.Shared => w.FILE_SHARE_READ | w.FILE_SHARE_DELETE,
|
||||||
|
.Exclusive => w.FILE_SHARE_DELETE,
|
||||||
|
},
|
||||||
|
.share_access_nonblocking = flags.lock_nonblocking,
|
||||||
|
.creation = w.FILE_OPEN,
|
||||||
|
.enable_async_io = std.io.is_async and !flags.always_blocking,
|
||||||
|
}),
|
||||||
.io_mode = .blocking,
|
.io_mode = .blocking,
|
||||||
|
.async_block_allowed = if (flags.always_blocking)
|
||||||
|
File.async_block_allowed_yes
|
||||||
|
else
|
||||||
|
File.async_block_allowed_no,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -713,25 +720,27 @@ pub const Dir = struct {
|
||||||
|
|
||||||
/// Same as `createFile` but Windows-only and the path parameter is
|
/// Same as `createFile` but Windows-only and the path parameter is
|
||||||
/// [WTF-16](https://simonsapin.github.io/wtf-8/#potentially-ill-formed-utf-16) encoded.
|
/// [WTF-16](https://simonsapin.github.io/wtf-8/#potentially-ill-formed-utf-16) encoded.
|
||||||
pub fn createFileW(self: Dir, sub_path_w: [*:0]const u16, flags: File.CreateFlags) File.OpenError!File {
|
pub fn createFileW(self: Dir, sub_path_w: []const u16, flags: File.CreateFlags) File.OpenError!File {
|
||||||
const w = os.windows;
|
const w = os.windows;
|
||||||
const access_mask = w.SYNCHRONIZE | w.GENERIC_WRITE |
|
const read_flag = if (flags.read) @as(u32, w.GENERIC_READ) else 0;
|
||||||
(if (flags.read) @as(u32, w.GENERIC_READ) else 0);
|
|
||||||
const creation = if (flags.exclusive)
|
|
||||||
@as(u32, w.FILE_CREATE)
|
|
||||||
else if (flags.truncate)
|
|
||||||
@as(u32, w.FILE_OVERWRITE_IF)
|
|
||||||
else
|
|
||||||
@as(u32, w.FILE_OPEN_IF);
|
|
||||||
|
|
||||||
const share_access = switch (flags.lock) {
|
|
||||||
.None => @as(?w.ULONG, null),
|
|
||||||
.Shared => w.FILE_SHARE_READ | w.FILE_SHARE_DELETE,
|
|
||||||
.Exclusive => w.FILE_SHARE_DELETE,
|
|
||||||
};
|
|
||||||
|
|
||||||
return @as(File, .{
|
return @as(File, .{
|
||||||
.handle = try os.windows.OpenFileW(self.fd, sub_path_w, null, access_mask, share_access, flags.lock_nonblocking, creation),
|
.handle = try os.windows.OpenFile(sub_path_w, .{
|
||||||
|
.dir = self.fd,
|
||||||
|
.access_mask = w.SYNCHRONIZE | w.GENERIC_WRITE | read_flag,
|
||||||
|
.share_access = switch (flags.lock) {
|
||||||
|
.None => @as(?w.ULONG, null),
|
||||||
|
.Shared => w.FILE_SHARE_READ | w.FILE_SHARE_DELETE,
|
||||||
|
.Exclusive => w.FILE_SHARE_DELETE,
|
||||||
|
},
|
||||||
|
.share_access_nonblocking = flags.lock_nonblocking,
|
||||||
|
.creation = if (flags.exclusive)
|
||||||
|
@as(u32, w.FILE_CREATE)
|
||||||
|
else if (flags.truncate)
|
||||||
|
@as(u32, w.FILE_OVERWRITE_IF)
|
||||||
|
else
|
||||||
|
@as(u32, w.FILE_OPEN_IF),
|
||||||
|
.enable_async_io = std.io.is_async,
|
||||||
|
}),
|
||||||
.io_mode = .blocking,
|
.io_mode = .blocking,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,12 +62,14 @@ pub const File = struct {
|
||||||
|
|
||||||
/// Sets whether or not to wait until the file is locked to return. If set to true,
|
/// Sets whether or not to wait until the file is locked to return. If set to true,
|
||||||
/// `error.WouldBlock` will be returned. Otherwise, the file will wait until the file
|
/// `error.WouldBlock` will be returned. Otherwise, the file will wait until the file
|
||||||
/// is available to proceed.
|
/// is available to proceed. In async I/O mode, non-blocking at the OS level is always
|
||||||
|
/// used, and `true` means `error.WouldBlock` is returned, and `false` means
|
||||||
|
/// `error.WouldBlock` is handled by the event loop.
|
||||||
lock_nonblocking: bool = false,
|
lock_nonblocking: bool = false,
|
||||||
|
|
||||||
/// This prevents `O_NONBLOCK` from being passed even if `std.io.is_async`.
|
/// This prevents `O_NONBLOCK` from being passed even if `std.io.is_async`.
|
||||||
/// It allows the use of `noasync` when calling functions related to opening
|
/// It allows the use of `noasync` when calling functions related to opening
|
||||||
/// the file, reading, and writing.
|
/// the file, reading, writing, as well as locking functionality.
|
||||||
always_blocking: bool = false,
|
always_blocking: bool = false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -295,6 +297,10 @@ pub const File = struct {
|
||||||
pub const PReadError = os.PReadError;
|
pub const PReadError = os.PReadError;
|
||||||
|
|
||||||
pub fn read(self: File, buffer: []u8) ReadError!usize {
|
pub fn read(self: File, buffer: []u8) ReadError!usize {
|
||||||
|
if (builtin.os.tag == .windows) {
|
||||||
|
const enable_async_io = std.io.is_async and !self.async_block_allowed;
|
||||||
|
return windows.ReadFile(self.handle, buffer, null, enable_async_io);
|
||||||
|
}
|
||||||
if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
|
if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
|
||||||
return std.event.Loop.instance.?.read(self.handle, buffer);
|
return std.event.Loop.instance.?.read(self.handle, buffer);
|
||||||
} else {
|
} else {
|
||||||
|
@ -315,6 +321,10 @@ pub const File = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pread(self: File, buffer: []u8, offset: u64) PReadError!usize {
|
pub fn pread(self: File, buffer: []u8, offset: u64) PReadError!usize {
|
||||||
|
if (builtin.os.tag == .windows) {
|
||||||
|
const enable_async_io = std.io.is_async and !self.async_block_allowed;
|
||||||
|
return windows.ReadFile(self.handle, buffer, offset, enable_async_io);
|
||||||
|
}
|
||||||
if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
|
if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
|
||||||
return std.event.Loop.instance.?.pread(self.handle, buffer, offset);
|
return std.event.Loop.instance.?.pread(self.handle, buffer, offset);
|
||||||
} else {
|
} else {
|
||||||
|
@ -406,6 +416,10 @@ pub const File = struct {
|
||||||
pub const PWriteError = os.PWriteError;
|
pub const PWriteError = os.PWriteError;
|
||||||
|
|
||||||
pub fn write(self: File, bytes: []const u8) WriteError!usize {
|
pub fn write(self: File, bytes: []const u8) WriteError!usize {
|
||||||
|
if (builtin.os.tag == .windows) {
|
||||||
|
const enable_async_io = std.io.is_async and !self.async_block_allowed;
|
||||||
|
return windows.WriteFile(self.handle, bytes, null, enable_async_io);
|
||||||
|
}
|
||||||
if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
|
if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
|
||||||
return std.event.Loop.instance.?.write(self.handle, bytes);
|
return std.event.Loop.instance.?.write(self.handle, bytes);
|
||||||
} else {
|
} else {
|
||||||
|
@ -421,6 +435,10 @@ pub const File = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pwrite(self: File, bytes: []const u8, offset: u64) PWriteError!usize {
|
pub fn pwrite(self: File, bytes: []const u8, offset: u64) PWriteError!usize {
|
||||||
|
if (builtin.os.tag == .windows) {
|
||||||
|
const enable_async_io = std.io.is_async and !self.async_block_allowed;
|
||||||
|
return windows.WriteFile(self.handle, bytes, offset, enable_async_io);
|
||||||
|
}
|
||||||
if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
|
if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
|
||||||
return std.event.Loop.instance.?.pwrite(self.handle, bytes, offset);
|
return std.event.Loop.instance.?.pwrite(self.handle, bytes, offset);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -177,6 +177,10 @@ pub fn isAbsoluteWindowsW(path_w: [*:0]const u16) bool {
|
||||||
return isAbsoluteWindowsImpl(u16, mem.spanZ(path_w));
|
return isAbsoluteWindowsImpl(u16, mem.spanZ(path_w));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn isAbsoluteWindowsWTF16(path: []const u16) bool {
|
||||||
|
return isAbsoluteWindowsImpl(u16, path);
|
||||||
|
}
|
||||||
|
|
||||||
pub const isAbsoluteWindowsC = @compileError("deprecated: renamed to isAbsoluteWindowsZ");
|
pub const isAbsoluteWindowsC = @compileError("deprecated: renamed to isAbsoluteWindowsZ");
|
||||||
|
|
||||||
pub fn isAbsoluteWindowsZ(path_c: [*:0]const u8) bool {
|
pub fn isAbsoluteWindowsZ(path_c: [*:0]const u8) bool {
|
||||||
|
|
|
@ -42,10 +42,12 @@ fn getStdOutHandle() os.fd_t {
|
||||||
return os.STDOUT_FILENO;
|
return os.STDOUT_FILENO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: async stdout on windows (https://github.com/ziglang/zig/pull/4816#issuecomment-604521023)
|
||||||
pub fn getStdOut() File {
|
pub fn getStdOut() File {
|
||||||
return File{
|
return File{
|
||||||
.handle = getStdOutHandle(),
|
.handle = getStdOutHandle(),
|
||||||
.io_mode = .blocking,
|
.io_mode = .blocking,
|
||||||
|
.async_block_allowed = if (builtin.os.tag == .windows) File.async_block_allowed_yes else File.async_block_allowed_no,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,10 +83,12 @@ fn getStdInHandle() os.fd_t {
|
||||||
return os.STDIN_FILENO;
|
return os.STDIN_FILENO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: async stdin on windows (https://github.com/ziglang/zig/pull/4816#issuecomment-604521023)
|
||||||
pub fn getStdIn() File {
|
pub fn getStdIn() File {
|
||||||
return File{
|
return File{
|
||||||
.handle = getStdInHandle(),
|
.handle = getStdInHandle(),
|
||||||
.io_mode = .blocking,
|
.io_mode = .blocking,
|
||||||
|
.async_block_allowed = if (builtin.os.tag == .windows) File.async_block_allowed_yes else File.async_block_allowed_no,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -305,7 +305,7 @@ pub const ReadError = error{
|
||||||
/// For POSIX the limit is `math.maxInt(isize)`.
|
/// For POSIX the limit is `math.maxInt(isize)`.
|
||||||
pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
|
pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
|
||||||
if (builtin.os.tag == .windows) {
|
if (builtin.os.tag == .windows) {
|
||||||
return windows.ReadFile(fd, buf, null);
|
return windows.ReadFile(fd, buf, null, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (builtin.os.tag == .wasi and !builtin.link_libc) {
|
if (builtin.os.tag == .wasi and !builtin.link_libc) {
|
||||||
|
@ -370,7 +370,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
|
||||||
const first = iov[0];
|
const first = iov[0];
|
||||||
return read(fd, first.iov_base[0..first.iov_len]);
|
return read(fd, first.iov_base[0..first.iov_len]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const iov_count = math.cast(u31, iov.len) catch math.maxInt(u31);
|
const iov_count = math.cast(u31, iov.len) catch math.maxInt(u31);
|
||||||
while (true) {
|
while (true) {
|
||||||
// TODO handle the case when iov_len is too large and get rid of this @intCast
|
// TODO handle the case when iov_len is too large and get rid of this @intCast
|
||||||
|
@ -408,7 +408,7 @@ pub const PReadError = ReadError || error{Unseekable};
|
||||||
/// used to perform the I/O. `error.WouldBlock` is not possible on Windows.
|
/// used to perform the I/O. `error.WouldBlock` is not possible on Windows.
|
||||||
pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
|
pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
|
||||||
if (builtin.os.tag == .windows) {
|
if (builtin.os.tag == .windows) {
|
||||||
return windows.ReadFile(fd, buf, offset);
|
return windows.ReadFile(fd, buf, offset, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -584,7 +584,7 @@ pub const WriteError = error{
|
||||||
/// The corresponding POSIX limit is `math.maxInt(isize)`.
|
/// The corresponding POSIX limit is `math.maxInt(isize)`.
|
||||||
pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
|
pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
|
||||||
if (builtin.os.tag == .windows) {
|
if (builtin.os.tag == .windows) {
|
||||||
return windows.WriteFile(fd, bytes, null);
|
return windows.WriteFile(fd, bytes, null, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (builtin.os.tag == .wasi and !builtin.link_libc) {
|
if (builtin.os.tag == .wasi and !builtin.link_libc) {
|
||||||
|
@ -709,7 +709,7 @@ pub const PWriteError = WriteError || error{Unseekable};
|
||||||
/// The corresponding POSIX limit is `math.maxInt(isize)`.
|
/// The corresponding POSIX limit is `math.maxInt(isize)`.
|
||||||
pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
|
pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
|
||||||
if (std.Target.current.os.tag == .windows) {
|
if (std.Target.current.os.tag == .windows) {
|
||||||
return windows.WriteFile(fd, bytes, offset);
|
return windows.WriteFile(fd, bytes, offset, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prevent EINVAL.
|
// Prevent EINVAL.
|
||||||
|
@ -1670,38 +1670,40 @@ pub fn renameatZ(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Same as `renameat` except the parameters are null-terminated UTF16LE encoded byte arrays.
|
/// Same as `renameat` but Windows-only and the path parameters are
|
||||||
/// Assumes target is Windows.
|
/// [WTF-16](https://simonsapin.github.io/wtf-8/#potentially-ill-formed-utf-16) encoded.
|
||||||
/// TODO these args can actually be slices when using ntdll. audit the rest of the W functions too.
|
|
||||||
pub fn renameatW(
|
pub fn renameatW(
|
||||||
old_dir_fd: fd_t,
|
old_dir_fd: fd_t,
|
||||||
old_path: [*:0]const u16,
|
old_path_w: []const u16,
|
||||||
new_dir_fd: fd_t,
|
new_dir_fd: fd_t,
|
||||||
new_path_w: [*:0]const u16,
|
new_path_w: []const u16,
|
||||||
ReplaceIfExists: windows.BOOLEAN,
|
ReplaceIfExists: windows.BOOLEAN,
|
||||||
) RenameError!void {
|
) RenameError!void {
|
||||||
const access_mask = windows.SYNCHRONIZE | windows.GENERIC_WRITE | windows.DELETE;
|
const src_fd = windows.OpenFile(old_path_w, .{
|
||||||
const src_fd = windows.OpenFileW(old_dir_fd, old_path, null, access_mask, null, false, windows.FILE_OPEN) catch |err| switch (err) {
|
.dir = old_dir_fd,
|
||||||
error.WouldBlock => unreachable,
|
.access_mask = windows.SYNCHRONIZE | windows.GENERIC_WRITE | windows.DELETE,
|
||||||
|
.creation = windows.FILE_OPEN,
|
||||||
|
.enable_async_io = false,
|
||||||
|
}) catch |err| switch (err) {
|
||||||
|
error.WouldBlock => unreachable, // Not possible without `.share_access_nonblocking = true`.
|
||||||
else => |e| return e,
|
else => |e| return e,
|
||||||
};
|
};
|
||||||
defer windows.CloseHandle(src_fd);
|
defer windows.CloseHandle(src_fd);
|
||||||
|
|
||||||
const struct_buf_len = @sizeOf(windows.FILE_RENAME_INFORMATION) + (MAX_PATH_BYTES - 1);
|
const struct_buf_len = @sizeOf(windows.FILE_RENAME_INFORMATION) + (MAX_PATH_BYTES - 1);
|
||||||
var rename_info_buf: [struct_buf_len]u8 align(@alignOf(windows.FILE_RENAME_INFORMATION)) = undefined;
|
var rename_info_buf: [struct_buf_len]u8 align(@alignOf(windows.FILE_RENAME_INFORMATION)) = undefined;
|
||||||
const new_path = mem.span(new_path_w);
|
const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION) - 1 + new_path_w.len * 2;
|
||||||
const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION) - 1 + new_path.len * 2;
|
|
||||||
if (struct_len > struct_buf_len) return error.NameTooLong;
|
if (struct_len > struct_buf_len) return error.NameTooLong;
|
||||||
|
|
||||||
const rename_info = @ptrCast(*windows.FILE_RENAME_INFORMATION, &rename_info_buf);
|
const rename_info = @ptrCast(*windows.FILE_RENAME_INFORMATION, &rename_info_buf);
|
||||||
|
|
||||||
rename_info.* = .{
|
rename_info.* = .{
|
||||||
.ReplaceIfExists = ReplaceIfExists,
|
.ReplaceIfExists = ReplaceIfExists,
|
||||||
.RootDirectory = if (std.fs.path.isAbsoluteWindowsW(new_path_w)) null else new_dir_fd,
|
.RootDirectory = if (std.fs.path.isAbsoluteWindowsWTF16(new_path_w)) null else new_dir_fd,
|
||||||
.FileNameLength = @intCast(u32, new_path.len * 2), // already checked error.NameTooLong
|
.FileNameLength = @intCast(u32, new_path_w.len * 2), // already checked error.NameTooLong
|
||||||
.FileName = undefined,
|
.FileName = undefined,
|
||||||
};
|
};
|
||||||
std.mem.copy(u16, @as([*]u16, &rename_info.FileName)[0..new_path.len], new_path);
|
std.mem.copy(u16, @as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w);
|
||||||
|
|
||||||
var io_status_block: windows.IO_STATUS_BLOCK = undefined;
|
var io_status_block: windows.IO_STATUS_BLOCK = undefined;
|
||||||
|
|
||||||
|
|
|
@ -103,17 +103,19 @@ pub const OpenError = error{
|
||||||
WouldBlock,
|
WouldBlock,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// TODO rename to CreateFileW
|
pub const OpenFileOptions = struct {
|
||||||
/// TODO actually we don't need the path parameter to be null terminated
|
|
||||||
pub fn OpenFileW(
|
|
||||||
dir: ?HANDLE,
|
|
||||||
sub_path_w: [*:0]const u16,
|
|
||||||
sa: ?*SECURITY_ATTRIBUTES,
|
|
||||||
access_mask: ACCESS_MASK,
|
access_mask: ACCESS_MASK,
|
||||||
share_access_opt: ?ULONG,
|
dir: ?HANDLE = null,
|
||||||
share_access_nonblocking: bool,
|
sa: ?*SECURITY_ATTRIBUTES = null,
|
||||||
|
share_access: ULONG = FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
|
||||||
|
share_access_nonblocking: bool = false,
|
||||||
creation: ULONG,
|
creation: ULONG,
|
||||||
) OpenError!HANDLE {
|
enable_async_io: bool = std.io.is_async,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// TODO when share_access_nonblocking is false, this implementation uses
|
||||||
|
/// untinterruptible sleep() to block. This is not the final iteration of the API.
|
||||||
|
pub fn OpenFile(sub_path_w: []const u16, options: OpenFileOptions) OpenError!HANDLE {
|
||||||
if (sub_path_w[0] == '.' and sub_path_w[1] == 0) {
|
if (sub_path_w[0] == '.' and sub_path_w[1] == 0) {
|
||||||
return error.IsDir;
|
return error.IsDir;
|
||||||
}
|
}
|
||||||
|
@ -123,37 +125,37 @@ pub fn OpenFileW(
|
||||||
|
|
||||||
var result: HANDLE = undefined;
|
var result: HANDLE = undefined;
|
||||||
|
|
||||||
const path_len_bytes = math.cast(u16, mem.lenZ(sub_path_w) * 2) catch |err| switch (err) {
|
const path_len_bytes = math.cast(u16, sub_path_w.len * 2) catch |err| switch (err) {
|
||||||
error.Overflow => return error.NameTooLong,
|
error.Overflow => return error.NameTooLong,
|
||||||
};
|
};
|
||||||
var nt_name = UNICODE_STRING{
|
var nt_name = UNICODE_STRING{
|
||||||
.Length = path_len_bytes,
|
.Length = path_len_bytes,
|
||||||
.MaximumLength = path_len_bytes,
|
.MaximumLength = path_len_bytes,
|
||||||
.Buffer = @intToPtr([*]u16, @ptrToInt(sub_path_w)),
|
.Buffer = @intToPtr([*]u16, @ptrToInt(sub_path_w.ptr)),
|
||||||
};
|
};
|
||||||
var attr = OBJECT_ATTRIBUTES{
|
var attr = OBJECT_ATTRIBUTES{
|
||||||
.Length = @sizeOf(OBJECT_ATTRIBUTES),
|
.Length = @sizeOf(OBJECT_ATTRIBUTES),
|
||||||
.RootDirectory = if (std.fs.path.isAbsoluteWindowsW(sub_path_w)) null else dir,
|
.RootDirectory = if (std.fs.path.isAbsoluteWindowsWTF16(sub_path_w)) null else options.dir,
|
||||||
.Attributes = 0, // Note we do not use OBJ_CASE_INSENSITIVE here.
|
.Attributes = 0, // Note we do not use OBJ_CASE_INSENSITIVE here.
|
||||||
.ObjectName = &nt_name,
|
.ObjectName = &nt_name,
|
||||||
.SecurityDescriptor = if (sa) |ptr| ptr.lpSecurityDescriptor else null,
|
.SecurityDescriptor = if (options.sa) |ptr| ptr.lpSecurityDescriptor else null,
|
||||||
.SecurityQualityOfService = null,
|
.SecurityQualityOfService = null,
|
||||||
};
|
};
|
||||||
var io: IO_STATUS_BLOCK = undefined;
|
var io: IO_STATUS_BLOCK = undefined;
|
||||||
const share_access = share_access_opt orelse (FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE);
|
|
||||||
|
|
||||||
var delay: usize = 1;
|
var delay: usize = 1;
|
||||||
while (true) {
|
while (true) {
|
||||||
|
const blocking_flag: ULONG = if (!options.enable_async_io) FILE_SYNCHRONOUS_IO_NONALERT else 0;
|
||||||
const rc = ntdll.NtCreateFile(
|
const rc = ntdll.NtCreateFile(
|
||||||
&result,
|
&result,
|
||||||
access_mask,
|
options.access_mask,
|
||||||
&attr,
|
&attr,
|
||||||
&io,
|
&io,
|
||||||
null,
|
null,
|
||||||
FILE_ATTRIBUTE_NORMAL,
|
FILE_ATTRIBUTE_NORMAL,
|
||||||
share_access,
|
options.share_access,
|
||||||
creation,
|
options.creation,
|
||||||
FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT,
|
FILE_NON_DIRECTORY_FILE | blocking_flag,
|
||||||
null,
|
null,
|
||||||
0,
|
0,
|
||||||
);
|
);
|
||||||
|
@ -165,14 +167,16 @@ pub fn OpenFileW(
|
||||||
.NO_MEDIA_IN_DEVICE => return error.NoDevice,
|
.NO_MEDIA_IN_DEVICE => return error.NoDevice,
|
||||||
.INVALID_PARAMETER => unreachable,
|
.INVALID_PARAMETER => unreachable,
|
||||||
.SHARING_VIOLATION => {
|
.SHARING_VIOLATION => {
|
||||||
if (share_access_nonblocking) {
|
if (options.share_access_nonblocking) {
|
||||||
return error.WouldBlock;
|
return error.WouldBlock;
|
||||||
}
|
}
|
||||||
|
// TODO sleep in a way that is interruptable
|
||||||
|
// TODO integrate with async I/O
|
||||||
std.time.sleep(delay);
|
std.time.sleep(delay);
|
||||||
if (delay < 1 * std.time.ns_per_s) {
|
if (delay < 1 * std.time.ns_per_s) {
|
||||||
delay *= 2;
|
delay *= 2;
|
||||||
}
|
}
|
||||||
continue; // TODO: don't loop for async
|
continue;
|
||||||
},
|
},
|
||||||
.ACCESS_DENIED => return error.AccessDenied,
|
.ACCESS_DENIED => return error.AccessDenied,
|
||||||
.PIPE_BUSY => return error.PipeBusy,
|
.PIPE_BUSY => return error.PipeBusy,
|
||||||
|
@ -447,10 +451,11 @@ pub const ReadFileError = error{
|
||||||
|
|
||||||
/// If buffer's length exceeds what a Windows DWORD integer can hold, it will be broken into
|
/// If buffer's length exceeds what a Windows DWORD integer can hold, it will be broken into
|
||||||
/// multiple non-atomic reads.
|
/// multiple non-atomic reads.
|
||||||
pub fn ReadFile(in_hFile: HANDLE, buffer: []u8, offset: ?u64) ReadFileError!usize {
|
pub fn ReadFile(in_hFile: HANDLE, buffer: []u8, offset: ?u64, enable_async_io: bool) ReadFileError!usize {
|
||||||
if (std.event.Loop.instance) |loop| {
|
if (std.event.Loop.instance != null and enable_async_io) {
|
||||||
|
const loop = std.event.Loop.instance.?;
|
||||||
// TODO support async ReadFile with no offset
|
// TODO support async ReadFile with no offset
|
||||||
const off = offset.?;
|
const off = if (offset == null) 0 else offset.?;
|
||||||
var resume_node = std.event.Loop.ResumeNode.Basic{
|
var resume_node = std.event.Loop.ResumeNode.Basic{
|
||||||
.base = .{
|
.base = .{
|
||||||
.id = .Basic,
|
.id = .Basic,
|
||||||
|
@ -465,20 +470,20 @@ pub fn ReadFile(in_hFile: HANDLE, buffer: []u8, offset: ?u64) ReadFileError!usiz
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
// TODO only call create io completion port once per fd
|
// TODO only call create io completion port once per fd
|
||||||
_ = windows.CreateIoCompletionPort(fd, loop.os_data.io_port, undefined, undefined) catch undefined;
|
_ = CreateIoCompletionPort(in_hFile, loop.os_data.io_port, undefined, undefined) catch undefined;
|
||||||
loop.beginOneEvent();
|
loop.beginOneEvent();
|
||||||
suspend {
|
suspend {
|
||||||
// TODO handle buffer bigger than DWORD can hold
|
// TODO handle buffer bigger than DWORD can hold
|
||||||
_ = windows.kernel32.ReadFile(fd, buffer.ptr, @intCast(windows.DWORD, buffer.len), null, &resume_node.base.overlapped);
|
_ = kernel32.ReadFile(in_hFile, buffer.ptr, @intCast(DWORD, buffer.len), null, &resume_node.base.overlapped);
|
||||||
}
|
}
|
||||||
var bytes_transferred: windows.DWORD = undefined;
|
var bytes_transferred: DWORD = undefined;
|
||||||
if (windows.kernel32.GetOverlappedResult(fd, &resume_node.base.overlapped, &bytes_transferred, windows.FALSE) == 0) {
|
if (kernel32.GetOverlappedResult(in_hFile, &resume_node.base.overlapped, &bytes_transferred, FALSE) == 0) {
|
||||||
switch (windows.kernel32.GetLastError()) {
|
switch (kernel32.GetLastError()) {
|
||||||
.IO_PENDING => unreachable,
|
.IO_PENDING => unreachable,
|
||||||
.OPERATION_ABORTED => return error.OperationAborted,
|
.OPERATION_ABORTED => return error.OperationAborted,
|
||||||
.BROKEN_PIPE => return error.BrokenPipe,
|
.BROKEN_PIPE => return error.BrokenPipe,
|
||||||
.HANDLE_EOF => return @as(usize, bytes_transferred),
|
.HANDLE_EOF => return @as(usize, bytes_transferred),
|
||||||
else => |err| return windows.unexpectedError(err),
|
else => |err| return unexpectedError(err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return @as(usize, bytes_transferred);
|
return @as(usize, bytes_transferred);
|
||||||
|
@ -520,10 +525,11 @@ pub const WriteFileError = error{
|
||||||
Unexpected,
|
Unexpected,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn WriteFile(handle: HANDLE, bytes: []const u8, offset: ?u64) WriteFileError!usize {
|
pub fn WriteFile(handle: HANDLE, bytes: []const u8, offset: ?u64, enable_async_io: bool) WriteFileError!usize {
|
||||||
if (std.event.Loop.instance) |loop| {
|
if (std.event.Loop.instance != null and enable_async_io) {
|
||||||
|
const loop = std.event.Loop.instance.?;
|
||||||
// TODO support async WriteFile with no offset
|
// TODO support async WriteFile with no offset
|
||||||
const off = offset.?;
|
const off = if (offset == null) 0 else offset.?;
|
||||||
var resume_node = std.event.Loop.ResumeNode.Basic{
|
var resume_node = std.event.Loop.ResumeNode.Basic{
|
||||||
.base = .{
|
.base = .{
|
||||||
.id = .Basic,
|
.id = .Basic,
|
||||||
|
@ -538,14 +544,14 @@ pub fn WriteFile(handle: HANDLE, bytes: []const u8, offset: ?u64) WriteFileError
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
// TODO only call create io completion port once per fd
|
// TODO only call create io completion port once per fd
|
||||||
_ = CreateIoCompletionPort(fd, loop.os_data.io_port, undefined, undefined);
|
_ = CreateIoCompletionPort(handle, loop.os_data.io_port, undefined, undefined) catch undefined;
|
||||||
loop.beginOneEvent();
|
loop.beginOneEvent();
|
||||||
suspend {
|
suspend {
|
||||||
const adjusted_len = math.cast(windows.DWORD, bytes.len) catch maxInt(windows.DWORD);
|
const adjusted_len = math.cast(DWORD, bytes.len) catch maxInt(DWORD);
|
||||||
_ = kernel32.WriteFile(fd, bytes.ptr, adjusted_len, null, &resume_node.base.overlapped);
|
_ = kernel32.WriteFile(handle, bytes.ptr, adjusted_len, null, &resume_node.base.overlapped);
|
||||||
}
|
}
|
||||||
var bytes_transferred: windows.DWORD = undefined;
|
var bytes_transferred: DWORD = undefined;
|
||||||
if (kernel32.GetOverlappedResult(fd, &resume_node.base.overlapped, &bytes_transferred, FALSE) == 0) {
|
if (kernel32.GetOverlappedResult(handle, &resume_node.base.overlapped, &bytes_transferred, FALSE) == 0) {
|
||||||
switch (kernel32.GetLastError()) {
|
switch (kernel32.GetLastError()) {
|
||||||
.IO_PENDING => unreachable,
|
.IO_PENDING => unreachable,
|
||||||
.INVALID_USER_BUFFER => return error.SystemResources,
|
.INVALID_USER_BUFFER => return error.SystemResources,
|
||||||
|
@ -553,7 +559,7 @@ pub fn WriteFile(handle: HANDLE, bytes: []const u8, offset: ?u64) WriteFileError
|
||||||
.OPERATION_ABORTED => return error.OperationAborted,
|
.OPERATION_ABORTED => return error.OperationAborted,
|
||||||
.NOT_ENOUGH_QUOTA => return error.SystemResources,
|
.NOT_ENOUGH_QUOTA => return error.SystemResources,
|
||||||
.BROKEN_PIPE => return error.BrokenPipe,
|
.BROKEN_PIPE => return error.BrokenPipe,
|
||||||
else => |err| return windows.unexpectedError(err),
|
else => |err| return unexpectedError(err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return bytes_transferred;
|
return bytes_transferred;
|
||||||
|
|
|
@ -470,7 +470,7 @@ pub const Pdb = struct {
|
||||||
msf: Msf,
|
msf: Msf,
|
||||||
|
|
||||||
pub fn openFile(self: *Pdb, coff_ptr: *coff.Coff, file_name: []u8) !void {
|
pub fn openFile(self: *Pdb, coff_ptr: *coff.Coff, file_name: []u8) !void {
|
||||||
self.in_file = try fs.cwd().openFile(file_name, .{});
|
self.in_file = try fs.cwd().openFile(file_name, .{ .always_blocking = true });
|
||||||
self.allocator = coff_ptr.allocator;
|
self.allocator = coff_ptr.allocator;
|
||||||
self.coff = coff_ptr;
|
self.coff = coff_ptr;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue