Read all of file contents using a preallocated buffer.
The returned slice has the same pointer as buffer. If the length matches buffer.len
the situation is ambiguous. It could either mean that the entire file was read, and
it exactly fits the buffer, or it could mean the buffer was not big enough for the
entire file.
file_path should be encoded as WTF-8.file_path should be encoded as valid UTF-8.file_path is an opaque sequence of bytes with no particular encoding.pub fn readFile(dir: Dir, io: Io, file_path: []const u8, buffer: []u8) ReadFileError![]u8
pub fn readFile(dir: Dir, io: Io, file_path: []const u8, buffer: []u8) ReadFileError![]u8 {
var file = try dir.openFile(io, file_path, .{
// We can take advantage of this on Windows since it doesn't involve any extra syscalls,
// so we can get error.IsDir during open rather than during the read.
.allow_directory = if (native_os == .windows) false else true,
});
defer file.close(io);
var reader = file.reader(io, &.{});
const n = reader.interface.readSliceShort(buffer) catch |err| switch (err) {
error.ReadFailed => return reader.err.?,
};
return buffer[0..n];
}