Commit a870228ab4

Andrew Kelley <superjoe30@gmail.com>
2018-07-26 05:34:57
self-hosted: use std.event.fs.readFile
1 parent cc45527
Changed files (2)
src-self-hosted
std
event
src-self-hosted/compilation.zig
@@ -30,6 +30,9 @@ const Package = @import("package.zig").Package;
 const link = @import("link.zig").link;
 const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
 const CInt = @import("c_int.zig").CInt;
+const fs = event.fs;
+
+const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
 
 /// Data that is local to the event loop.
 pub const EventLoopLocal = struct {
@@ -757,8 +760,11 @@ pub const Compilation = struct {
             const root_scope = blk: {
                 errdefer self.gpa().free(root_src_real_path);
 
-                // TODO async/await readFileAlloc()
-                const source_code = io.readFileAlloc(self.gpa(), root_src_real_path) catch |err| {
+                const source_code = (await (async fs.readFile(
+                    self.loop,
+                    root_src_real_path,
+                    max_src_size,
+                ) catch unreachable)) catch |err| {
                     try printError("unable to open '{}': {}", root_src_real_path, err);
                     return err;
                 };
std/event/fs.zig
@@ -273,6 +273,7 @@ pub async fn writeFileMode(loop: *event.Loop, path: []const u8, contents: []cons
 
 /// The promise resumes when the last data has been confirmed written, but before the file handle
 /// is closed.
+/// Caller owns returned memory.
 pub async fn readFile(loop: *event.Loop, file_path: []const u8, max_size: usize) ![]u8 {
     var close_op = try CloseOperation.create(loop);
     defer close_op.deinit();
@@ -292,6 +293,9 @@ pub async fn readFile(loop: *event.Loop, file_path: []const u8, max_size: usize)
         const buf_array = [][]u8{buf};
         const amt = try await (async preadv(loop, fd, list.len, buf_array) catch unreachable);
         list.len += amt;
+        if (list.len > max_size) {
+            return error.FileTooBig;
+        }
         if (amt < buf.len) {
             return list.toOwnedSlice();
         }